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.

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