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.

279 lines
8.8 KiB

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