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.

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