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.

138 lines
2.7 KiB

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