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.

212 lines
6.4 KiB

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