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.

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