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.

166 lines
3.7 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 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, n *int64, err *error) Adjustment {
  25. switch t := ReadByte(r, n, err); t {
  26. case ADJ_TYPE_BOND:
  27. return &Bond{
  28. Fee: ReadUInt64(r, n, err),
  29. UnbondTo: ReadUInt64(r, n, err),
  30. Amount: ReadUInt64(r, n, err),
  31. Signature: ReadSignature(r, n, err),
  32. }
  33. case ADJ_TYPE_UNBOND:
  34. return &Unbond{
  35. Fee: ReadUInt64(r, n, err),
  36. Amount: ReadUInt64(r, n, err),
  37. Signature: ReadSignature(r, n, err),
  38. }
  39. case ADJ_TYPE_TIMEOUT:
  40. return &Timeout{
  41. AccountId: ReadUInt64(r, n, err),
  42. Penalty: ReadUInt64(r, n, err),
  43. }
  44. case ADJ_TYPE_DUPEOUT:
  45. return &Dupeout{
  46. VoteA: ReadBlockVote(r, n, err),
  47. VoteB: ReadBlockVote(r, n, err),
  48. }
  49. default:
  50. Panicf("Unknown Adjustment type %x", t)
  51. return nil
  52. }
  53. }
  54. //-----------------------------------------------------------------------------
  55. /* Bond < Adjustment */
  56. type Bond struct {
  57. Fee uint64
  58. UnbondTo uint64
  59. Amount uint64
  60. Signature
  61. }
  62. func (self *Bond) Type() byte {
  63. return ADJ_TYPE_BOND
  64. }
  65. func (self *Bond) WriteTo(w io.Writer) (n int64, err error) {
  66. WriteByte(w, self.Type(), &n, &err)
  67. WriteUInt64(w, self.Fee, &n, &err)
  68. WriteUInt64(w, self.UnbondTo, &n, &err)
  69. WriteUInt64(w, self.Amount, &n, &err)
  70. WriteBinary(w, self.Signature, &n, &err)
  71. return
  72. }
  73. //-----------------------------------------------------------------------------
  74. /* Unbond < Adjustment */
  75. type Unbond struct {
  76. Fee uint64
  77. Amount uint64
  78. Signature
  79. }
  80. func (self *Unbond) Type() byte {
  81. return ADJ_TYPE_UNBOND
  82. }
  83. func (self *Unbond) WriteTo(w io.Writer) (n int64, err error) {
  84. WriteByte(w, self.Type(), &n, &err)
  85. WriteUInt64(w, self.Fee, &n, &err)
  86. WriteUInt64(w, self.Amount, &n, &err)
  87. WriteBinary(w, self.Signature, &n, &err)
  88. return
  89. }
  90. //-----------------------------------------------------------------------------
  91. /* Timeout < Adjustment */
  92. type Timeout struct {
  93. AccountId uint64
  94. Penalty uint64
  95. }
  96. func (self *Timeout) Type() byte {
  97. return ADJ_TYPE_TIMEOUT
  98. }
  99. func (self *Timeout) WriteTo(w io.Writer) (n int64, err error) {
  100. WriteByte(w, self.Type(), &n, &err)
  101. WriteUInt64(w, self.AccountId, &n, &err)
  102. WriteUInt64(w, self.Penalty, &n, &err)
  103. return
  104. }
  105. //-----------------------------------------------------------------------------
  106. /*
  107. The full vote structure is only needed when presented as evidence.
  108. Typically only the signature is passed around, as the hash & height are implied.
  109. */
  110. type BlockVote struct {
  111. Height uint64
  112. BlockHash []byte
  113. Signature
  114. }
  115. func ReadBlockVote(r io.Reader, n *int64, err *error) BlockVote {
  116. return BlockVote{
  117. Height: ReadUInt64(r, n, err),
  118. BlockHash: ReadByteSlice(r, n, err),
  119. Signature: ReadSignature(r, n, err),
  120. }
  121. }
  122. func (self BlockVote) WriteTo(w io.Writer) (n int64, err error) {
  123. WriteUInt64(w, self.Height, &n, &err)
  124. WriteByteSlice(w, self.BlockHash, &n, &err)
  125. WriteBinary(w, self.Signature, &n, &err)
  126. return
  127. }
  128. /* Dupeout < Adjustment */
  129. type Dupeout struct {
  130. VoteA BlockVote
  131. VoteB BlockVote
  132. }
  133. func (self *Dupeout) Type() byte {
  134. return ADJ_TYPE_DUPEOUT
  135. }
  136. func (self *Dupeout) WriteTo(w io.Writer) (n int64, err error) {
  137. WriteByte(w, self.Type(), &n, &err)
  138. WriteBinary(w, self.VoteA, &n, &err)
  139. WriteBinary(w, self.VoteB, &n, &err)
  140. return
  141. }