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.

249 lines
7.7 KiB

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