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.

258 lines
8.1 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 = 436
  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. Bytes() []byte // bytes which compromise the evidence
  46. Hash() []byte // hash of the evidence
  47. Verify(chainID string, pubKey crypto.PubKey) error // verify the evidence
  48. Equal(Evidence) bool // check equality of evidence
  49. String() string
  50. }
  51. func RegisterEvidences(cdc *amino.Codec) {
  52. cdc.RegisterInterface((*Evidence)(nil), nil)
  53. cdc.RegisterConcrete(&DuplicateVoteEvidence{}, "tendermint/DuplicateVoteEvidence", nil)
  54. }
  55. func RegisterMockEvidences(cdc *amino.Codec) {
  56. cdc.RegisterConcrete(MockGoodEvidence{}, "tendermint/MockGoodEvidence", nil)
  57. cdc.RegisterConcrete(MockBadEvidence{}, "tendermint/MockBadEvidence", nil)
  58. }
  59. // MaxEvidenceBytesPerBlock returns the maximum evidence size per block -
  60. // 1/10th of the maximum block size.
  61. func MaxEvidenceBytesPerBlock(blockMaxBytes int64) int64 {
  62. return blockMaxBytes / 10
  63. }
  64. //-------------------------------------------
  65. // DuplicateVoteEvidence contains evidence a validator signed two conflicting
  66. // votes.
  67. type DuplicateVoteEvidence struct {
  68. PubKey crypto.PubKey
  69. VoteA *Vote
  70. VoteB *Vote
  71. }
  72. var _ Evidence = &DuplicateVoteEvidence{}
  73. // String returns a string representation of the evidence.
  74. func (dve *DuplicateVoteEvidence) String() string {
  75. return fmt.Sprintf("VoteA: %v; VoteB: %v", dve.VoteA, dve.VoteB)
  76. }
  77. // Height returns the height this evidence refers to.
  78. func (dve *DuplicateVoteEvidence) Height() int64 {
  79. return dve.VoteA.Height
  80. }
  81. // Address returns the address of the validator.
  82. func (dve *DuplicateVoteEvidence) Address() []byte {
  83. return dve.PubKey.Address()
  84. }
  85. // Hash returns the hash of the evidence.
  86. func (dve *DuplicateVoteEvidence) Bytes() []byte {
  87. return cdcEncode(dve)
  88. }
  89. // Hash returns the hash of the evidence.
  90. func (dve *DuplicateVoteEvidence) Hash() []byte {
  91. return tmhash.Sum(cdcEncode(dve))
  92. }
  93. // Verify returns an error if the two votes aren't conflicting.
  94. // To be conflicting, they must be from the same validator, for the same H/R/S, but for different blocks.
  95. func (dve *DuplicateVoteEvidence) Verify(chainID string, pubKey crypto.PubKey) error {
  96. // H/R/S must be the same
  97. if dve.VoteA.Height != dve.VoteB.Height ||
  98. dve.VoteA.Round != dve.VoteB.Round ||
  99. dve.VoteA.Type != dve.VoteB.Type {
  100. return fmt.Errorf("DuplicateVoteEvidence Error: H/R/S does not match. Got %v and %v", dve.VoteA, dve.VoteB)
  101. }
  102. // Address must be the same
  103. if !bytes.Equal(dve.VoteA.ValidatorAddress, dve.VoteB.ValidatorAddress) {
  104. return fmt.Errorf("DuplicateVoteEvidence Error: Validator addresses do not match. Got %X and %X", dve.VoteA.ValidatorAddress, dve.VoteB.ValidatorAddress)
  105. }
  106. // Index must be the same
  107. if dve.VoteA.ValidatorIndex != dve.VoteB.ValidatorIndex {
  108. return fmt.Errorf("DuplicateVoteEvidence Error: Validator indices do not match. Got %d and %d", dve.VoteA.ValidatorIndex, dve.VoteB.ValidatorIndex)
  109. }
  110. // BlockIDs must be different
  111. if dve.VoteA.BlockID.Equals(dve.VoteB.BlockID) {
  112. return fmt.Errorf("DuplicateVoteEvidence Error: BlockIDs are the same (%v) - not a real duplicate vote", dve.VoteA.BlockID)
  113. }
  114. // pubkey must match address (this should already be true, sanity check)
  115. addr := dve.VoteA.ValidatorAddress
  116. if !bytes.Equal(pubKey.Address(), addr) {
  117. return fmt.Errorf("DuplicateVoteEvidence FAILED SANITY CHECK - address (%X) doesn't match pubkey (%v - %X)",
  118. addr, pubKey, pubKey.Address())
  119. }
  120. // Signatures must be valid
  121. if !pubKey.VerifyBytes(dve.VoteA.SignBytes(chainID), dve.VoteA.Signature) {
  122. return fmt.Errorf("DuplicateVoteEvidence Error verifying VoteA: %v", ErrVoteInvalidSignature)
  123. }
  124. if !pubKey.VerifyBytes(dve.VoteB.SignBytes(chainID), dve.VoteB.Signature) {
  125. return fmt.Errorf("DuplicateVoteEvidence Error verifying VoteB: %v", ErrVoteInvalidSignature)
  126. }
  127. return nil
  128. }
  129. // Equal checks if two pieces of evidence are equal.
  130. func (dve *DuplicateVoteEvidence) Equal(ev Evidence) bool {
  131. if _, ok := ev.(*DuplicateVoteEvidence); !ok {
  132. return false
  133. }
  134. // just check their hashes
  135. dveHash := tmhash.Sum(cdcEncode(dve))
  136. evHash := tmhash.Sum(cdcEncode(ev))
  137. return bytes.Equal(dveHash, evHash)
  138. }
  139. //-----------------------------------------------------------------
  140. // UNSTABLE
  141. type MockGoodEvidence struct {
  142. Height_ int64
  143. Address_ []byte
  144. }
  145. var _ Evidence = &MockGoodEvidence{}
  146. // UNSTABLE
  147. func NewMockGoodEvidence(height int64, idx int, address []byte) MockGoodEvidence {
  148. return MockGoodEvidence{height, address}
  149. }
  150. func (e MockGoodEvidence) Height() int64 { return e.Height_ }
  151. func (e MockGoodEvidence) Address() []byte { return e.Address_ }
  152. func (e MockGoodEvidence) Hash() []byte {
  153. return []byte(fmt.Sprintf("%d-%x", e.Height_, e.Address_))
  154. }
  155. func (e MockGoodEvidence) Bytes() []byte {
  156. return []byte(fmt.Sprintf("%d-%x", e.Height_, e.Address_))
  157. }
  158. func (e MockGoodEvidence) Verify(chainID string, pubKey crypto.PubKey) 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. }
  164. func (e MockGoodEvidence) String() string {
  165. return fmt.Sprintf("GoodEvidence: %d/%s", e.Height_, e.Address_)
  166. }
  167. // UNSTABLE
  168. type MockBadEvidence struct {
  169. MockGoodEvidence
  170. }
  171. func (e MockBadEvidence) Verify(chainID string, pubKey crypto.PubKey) error {
  172. return fmt.Errorf("MockBadEvidence")
  173. }
  174. func (e MockBadEvidence) Equal(ev Evidence) bool {
  175. e2 := ev.(MockBadEvidence)
  176. return e.Height_ == e2.Height_ &&
  177. bytes.Equal(e.Address_, e2.Address_)
  178. }
  179. func (e MockBadEvidence) String() string {
  180. return fmt.Sprintf("BadEvidence: %d/%s", e.Height_, e.Address_)
  181. }
  182. //-------------------------------------------
  183. // EvidenceList is a list of Evidence. Evidences is not a word.
  184. type EvidenceList []Evidence
  185. // Hash returns the simple merkle root hash of the EvidenceList.
  186. func (evl EvidenceList) Hash() []byte {
  187. // These allocations are required because Evidence is not of type Bytes, and
  188. // golang slices can't be typed cast. This shouldn't be a performance problem since
  189. // the Evidence size is capped.
  190. evidenceBzs := make([][]byte, len(evl))
  191. for i := 0; i < len(evl); i++ {
  192. evidenceBzs[i] = evl[i].Bytes()
  193. }
  194. return merkle.SimpleHashFromByteSlices(evidenceBzs)
  195. }
  196. func (evl EvidenceList) String() string {
  197. s := ""
  198. for _, e := range evl {
  199. s += fmt.Sprintf("%s\t\t", e)
  200. }
  201. return s
  202. }
  203. // Has returns true if the evidence is in the EvidenceList.
  204. func (evl EvidenceList) Has(evidence Evidence) bool {
  205. for _, ev := range evl {
  206. if ev.Equal(evidence) {
  207. return true
  208. }
  209. }
  210. return false
  211. }