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.

218 lines
6.5 KiB

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