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.

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