You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

140 lines
3.0 KiB

11 years ago
11 years ago
11 years ago
11 years ago
  1. package blocks
  2. import (
  3. . "github.com/tendermint/tendermint/binary"
  4. "io"
  5. )
  6. /* Adjustment
  7. 1. Bond New validator posts a bond
  8. 2. Unbond Validator leaves
  9. 3. Timeout Validator times out
  10. 4. Dupeout Validator dupes out (signs twice)
  11. TODO: signing a bad checkpoint (block)
  12. */
  13. type Adjustment interface {
  14. Type() Byte
  15. Binary
  16. }
  17. const (
  18. ADJ_TYPE_BOND = Byte(0x01)
  19. ADJ_TYPE_UNBOND = Byte(0x02)
  20. ADJ_TYPE_TIMEOUT = Byte(0x03)
  21. ADJ_TYPE_DUPEOUT = Byte(0x04)
  22. )
  23. func ReadAdjustment(r io.Reader) Adjustment {
  24. switch t := ReadByte(r); t {
  25. case ADJ_TYPE_BOND:
  26. return &Bond{
  27. Fee: ReadUInt64(r),
  28. UnbondTo: ReadAccountId(r),
  29. Amount: ReadUInt64(r),
  30. Signature: ReadSignature(r),
  31. }
  32. case ADJ_TYPE_UNBOND:
  33. return &Unbond{
  34. Fee: ReadUInt64(r),
  35. Amount: ReadUInt64(r),
  36. Signature: ReadSignature(r),
  37. }
  38. case ADJ_TYPE_TIMEOUT:
  39. return &Timeout{
  40. Account: ReadAccountId(r),
  41. Penalty: ReadUInt64(r),
  42. }
  43. case ADJ_TYPE_DUPEOUT:
  44. return &Dupeout{
  45. VoteA: ReadVote(r),
  46. VoteB: ReadVote(r),
  47. }
  48. default:
  49. panicf("Unknown Adjustment type %x", t)
  50. return nil
  51. }
  52. }
  53. /* Bond < Adjustment */
  54. type Bond struct {
  55. Fee UInt64
  56. UnbondTo AccountId
  57. Amount UInt64
  58. Signature
  59. }
  60. func (self *Bond) Type() Byte {
  61. return ADJ_TYPE_BOND
  62. }
  63. func (self *Bond) WriteTo(w io.Writer) (n int64, err error) {
  64. n, err = WriteOnto(self.Type(), w, n, err)
  65. n, err = WriteOnto(self.Fee, w, n, err)
  66. n, err = WriteOnto(self.UnbondTo, w, n, err)
  67. n, err = WriteOnto(self.Amount, w, n, err)
  68. n, err = WriteOnto(self.Signature, w, n, err)
  69. return
  70. }
  71. /* Unbond < Adjustment */
  72. type Unbond struct {
  73. Fee UInt64
  74. Amount UInt64
  75. Signature
  76. }
  77. func (self *Unbond) Type() Byte {
  78. return ADJ_TYPE_UNBOND
  79. }
  80. func (self *Unbond) WriteTo(w io.Writer) (n int64, err error) {
  81. n, err = WriteOnto(self.Type(), w, n, err)
  82. n, err = WriteOnto(self.Fee, w, n, err)
  83. n, err = WriteOnto(self.Amount, w, n, err)
  84. n, err = WriteOnto(self.Signature, w, n, err)
  85. return
  86. }
  87. /* Timeout < Adjustment */
  88. type Timeout struct {
  89. Account AccountId
  90. Penalty UInt64
  91. }
  92. func (self *Timeout) Type() Byte {
  93. return ADJ_TYPE_TIMEOUT
  94. }
  95. func (self *Timeout) WriteTo(w io.Writer) (n int64, err error) {
  96. n, err = WriteOnto(self.Type(), w, n, err)
  97. n, err = WriteOnto(self.Account, w, n, err)
  98. n, err = WriteOnto(self.Penalty, w, n, err)
  99. return
  100. }
  101. /* Dupeout < Adjustment */
  102. type Dupeout struct {
  103. VoteA Vote
  104. VoteB Vote
  105. }
  106. func (self *Dupeout) Type() Byte {
  107. return ADJ_TYPE_DUPEOUT
  108. }
  109. func (self *Dupeout) WriteTo(w io.Writer) (n int64, err error) {
  110. n, err = WriteOnto(self.VoteA, w, n, err)
  111. n, err = WriteOnto(self.VoteB, w, n, err)
  112. return
  113. }