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.

247 lines
7.6 KiB

  1. package types
  2. import (
  3. "bytes"
  4. "fmt"
  5. amino "github.com/tendermint/go-amino"
  6. "github.com/tendermint/tendermint/crypto"
  7. "github.com/tendermint/tendermint/crypto/merkle"
  8. )
  9. const (
  10. // MaxEvidenceBytes is a maximum size of any evidence (including amino overhead).
  11. MaxEvidenceBytes int64 = 440
  12. )
  13. // ErrEvidenceInvalid wraps a piece of evidence and the error denoting how or why it is invalid.
  14. type ErrEvidenceInvalid struct {
  15. Evidence Evidence
  16. ErrorValue error
  17. }
  18. // NewErrEvidenceInvalid returns a new EvidenceInvalid with the given err.
  19. func NewErrEvidenceInvalid(ev Evidence, err error) *ErrEvidenceInvalid {
  20. return &ErrEvidenceInvalid{ev, err}
  21. }
  22. // Error returns a string representation of the error.
  23. func (err *ErrEvidenceInvalid) Error() string {
  24. return fmt.Sprintf("Invalid evidence: %v. Evidence: %v", err.ErrorValue, err.Evidence)
  25. }
  26. // ErrEvidenceOverflow is for when there is too much evidence in a block.
  27. type ErrEvidenceOverflow struct {
  28. MaxBytes int64
  29. GotBytes int64
  30. }
  31. // NewErrEvidenceOverflow returns a new ErrEvidenceOverflow where got > max.
  32. func NewErrEvidenceOverflow(max, got int64) *ErrEvidenceOverflow {
  33. return &ErrEvidenceOverflow{max, got}
  34. }
  35. // Error returns a string representation of the error.
  36. func (err *ErrEvidenceOverflow) Error() string {
  37. return fmt.Sprintf("Too much evidence: Max %d bytes, got %d bytes", err.MaxBytes, err.GotBytes)
  38. }
  39. //-------------------------------------------
  40. // Evidence represents any provable malicious activity by a validator
  41. type Evidence interface {
  42. Height() int64 // height of the equivocation
  43. Address() []byte // address of the equivocating validator
  44. Hash() []byte // hash of the evidence
  45. Verify(chainID string, pubKey crypto.PubKey) error // verify the evidence
  46. Equal(Evidence) bool // check equality of evidence
  47. String() string
  48. }
  49. func RegisterEvidences(cdc *amino.Codec) {
  50. cdc.RegisterInterface((*Evidence)(nil), nil)
  51. cdc.RegisterConcrete(&DuplicateVoteEvidence{}, "tendermint/DuplicateVoteEvidence", nil)
  52. }
  53. func RegisterMockEvidences(cdc *amino.Codec) {
  54. cdc.RegisterConcrete(MockGoodEvidence{}, "tendermint/MockGoodEvidence", nil)
  55. cdc.RegisterConcrete(MockBadEvidence{}, "tendermint/MockBadEvidence", nil)
  56. }
  57. // MaxEvidenceBytesPerBlock returns the maximum evidence size per block -
  58. // 1/10th of the maximum block size.
  59. func MaxEvidenceBytesPerBlock(blockMaxBytes int64) int64 {
  60. return blockMaxBytes / 10
  61. }
  62. //-------------------------------------------
  63. // DuplicateVoteEvidence contains evidence a validator signed two conflicting
  64. // votes.
  65. type DuplicateVoteEvidence struct {
  66. PubKey crypto.PubKey
  67. VoteA *Vote
  68. VoteB *Vote
  69. }
  70. // String returns a string representation of the evidence.
  71. func (dve *DuplicateVoteEvidence) String() string {
  72. return fmt.Sprintf("VoteA: %v; VoteB: %v", dve.VoteA, dve.VoteB)
  73. }
  74. // Height returns the height this evidence refers to.
  75. func (dve *DuplicateVoteEvidence) Height() int64 {
  76. return dve.VoteA.Height
  77. }
  78. // Address returns the address of the validator.
  79. func (dve *DuplicateVoteEvidence) Address() []byte {
  80. return dve.PubKey.Address()
  81. }
  82. // Hash returns the hash of the evidence.
  83. func (dve *DuplicateVoteEvidence) Hash() []byte {
  84. return aminoHasher(dve).Hash()
  85. }
  86. // Verify returns an error if the two votes aren't conflicting.
  87. // To be conflicting, they must be from the same validator, for the same H/R/S, but for different blocks.
  88. func (dve *DuplicateVoteEvidence) Verify(chainID string, pubKey crypto.PubKey) error {
  89. // H/R/S must be the same
  90. if dve.VoteA.Height != dve.VoteB.Height ||
  91. dve.VoteA.Round != dve.VoteB.Round ||
  92. dve.VoteA.Type != dve.VoteB.Type {
  93. return fmt.Errorf("DuplicateVoteEvidence Error: H/R/S does not match. Got %v and %v", dve.VoteA, dve.VoteB)
  94. }
  95. // Address must be the same
  96. if !bytes.Equal(dve.VoteA.ValidatorAddress, dve.VoteB.ValidatorAddress) {
  97. return fmt.Errorf("DuplicateVoteEvidence Error: Validator addresses do not match. Got %X and %X", dve.VoteA.ValidatorAddress, dve.VoteB.ValidatorAddress)
  98. }
  99. // Index must be the same
  100. if dve.VoteA.ValidatorIndex != dve.VoteB.ValidatorIndex {
  101. return fmt.Errorf("DuplicateVoteEvidence Error: Validator indices do not match. Got %d and %d", dve.VoteA.ValidatorIndex, dve.VoteB.ValidatorIndex)
  102. }
  103. // BlockIDs must be different
  104. if dve.VoteA.BlockID.Equals(dve.VoteB.BlockID) {
  105. return fmt.Errorf("DuplicateVoteEvidence Error: BlockIDs are the same (%v) - not a real duplicate vote", dve.VoteA.BlockID)
  106. }
  107. // pubkey must match address (this should already be true, sanity check)
  108. addr := dve.VoteA.ValidatorAddress
  109. if !bytes.Equal(pubKey.Address(), addr) {
  110. return fmt.Errorf("DuplicateVoteEvidence FAILED SANITY CHECK - address (%X) doesn't match pubkey (%v - %X)",
  111. addr, pubKey, pubKey.Address())
  112. }
  113. // Signatures must be valid
  114. if !pubKey.VerifyBytes(dve.VoteA.SignBytes(chainID), dve.VoteA.Signature) {
  115. return fmt.Errorf("DuplicateVoteEvidence Error verifying VoteA: %v", ErrVoteInvalidSignature)
  116. }
  117. if !pubKey.VerifyBytes(dve.VoteB.SignBytes(chainID), dve.VoteB.Signature) {
  118. return fmt.Errorf("DuplicateVoteEvidence Error verifying VoteB: %v", ErrVoteInvalidSignature)
  119. }
  120. return nil
  121. }
  122. // Equal checks if two pieces of evidence are equal.
  123. func (dve *DuplicateVoteEvidence) Equal(ev Evidence) bool {
  124. if _, ok := ev.(*DuplicateVoteEvidence); !ok {
  125. return false
  126. }
  127. // just check their hashes
  128. dveHash := aminoHasher(dve).Hash()
  129. evHash := aminoHasher(ev).Hash()
  130. return bytes.Equal(dveHash, evHash)
  131. }
  132. //-----------------------------------------------------------------
  133. // UNSTABLE
  134. type MockGoodEvidence struct {
  135. Height_ int64
  136. Address_ []byte
  137. }
  138. // UNSTABLE
  139. func NewMockGoodEvidence(height int64, idx int, address []byte) MockGoodEvidence {
  140. return MockGoodEvidence{height, address}
  141. }
  142. func (e MockGoodEvidence) Height() int64 { return e.Height_ }
  143. func (e MockGoodEvidence) Address() []byte { return e.Address_ }
  144. func (e MockGoodEvidence) Hash() []byte {
  145. return []byte(fmt.Sprintf("%d-%x", e.Height_, e.Address_))
  146. }
  147. func (e MockGoodEvidence) Verify(chainID string, pubKey crypto.PubKey) error { return nil }
  148. func (e MockGoodEvidence) Equal(ev Evidence) bool {
  149. e2 := ev.(MockGoodEvidence)
  150. return e.Height_ == e2.Height_ &&
  151. bytes.Equal(e.Address_, e2.Address_)
  152. }
  153. func (e MockGoodEvidence) String() string {
  154. return fmt.Sprintf("GoodEvidence: %d/%s", e.Height_, e.Address_)
  155. }
  156. // UNSTABLE
  157. type MockBadEvidence struct {
  158. MockGoodEvidence
  159. }
  160. func (e MockBadEvidence) Verify(chainID string, pubKey crypto.PubKey) error {
  161. return fmt.Errorf("MockBadEvidence")
  162. }
  163. func (e MockBadEvidence) Equal(ev Evidence) bool {
  164. e2 := ev.(MockBadEvidence)
  165. return e.Height_ == e2.Height_ &&
  166. bytes.Equal(e.Address_, e2.Address_)
  167. }
  168. func (e MockBadEvidence) String() string {
  169. return fmt.Sprintf("BadEvidence: %d/%s", e.Height_, e.Address_)
  170. }
  171. //-------------------------------------------
  172. // EvidenceList is a list of Evidence. Evidences is not a word.
  173. type EvidenceList []Evidence
  174. // Hash returns the simple merkle root hash of the EvidenceList.
  175. func (evl EvidenceList) Hash() []byte {
  176. // Recursive impl.
  177. // Copied from crypto/merkle to avoid allocations
  178. switch len(evl) {
  179. case 0:
  180. return nil
  181. case 1:
  182. return evl[0].Hash()
  183. default:
  184. left := EvidenceList(evl[:(len(evl)+1)/2]).Hash()
  185. right := EvidenceList(evl[(len(evl)+1)/2:]).Hash()
  186. return merkle.SimpleHashFromTwoHashes(left, right)
  187. }
  188. }
  189. func (evl EvidenceList) String() string {
  190. s := ""
  191. for _, e := range evl {
  192. s += fmt.Sprintf("%s\t\t", e)
  193. }
  194. return s
  195. }
  196. // Has returns true if the evidence is in the EvidenceList.
  197. func (evl EvidenceList) Has(evidence Evidence) bool {
  198. for _, ev := range evl {
  199. if ev.Equal(evidence) {
  200. return true
  201. }
  202. }
  203. return false
  204. }