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.

319 lines
9.7 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. MaxNum int64
  31. GotNum 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, got %d", err.MaxNum, err.GotNum)
  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(MockRandomGoodEvidence{}, "tendermint/MockRandomGoodEvidence", nil)
  60. cdc.RegisterConcrete(MockBadEvidence{}, "tendermint/MockBadEvidence", nil)
  61. }
  62. const (
  63. MaxEvidenceBytesDenominator = 10
  64. )
  65. // MaxEvidencePerBlock returns the maximum number of evidences
  66. // allowed in the block and their maximum total size (limitted to 1/10th
  67. // of the maximum block size).
  68. // TODO: change to a constant, or to a fraction of the validator set size.
  69. // See https://github.com/tendermint/tendermint/issues/2590
  70. func MaxEvidencePerBlock(blockMaxBytes int64) (int64, int64) {
  71. maxBytes := blockMaxBytes / MaxEvidenceBytesDenominator
  72. maxNum := maxBytes / MaxEvidenceBytes
  73. return maxNum, maxBytes
  74. }
  75. //-------------------------------------------
  76. // DuplicateVoteEvidence contains evidence a validator signed two conflicting
  77. // votes.
  78. type DuplicateVoteEvidence struct {
  79. PubKey crypto.PubKey
  80. VoteA *Vote
  81. VoteB *Vote
  82. }
  83. var _ Evidence = &DuplicateVoteEvidence{}
  84. // String returns a string representation of the evidence.
  85. func (dve *DuplicateVoteEvidence) String() string {
  86. return fmt.Sprintf("VoteA: %v; VoteB: %v", dve.VoteA, dve.VoteB)
  87. }
  88. // Height returns the height this evidence refers to.
  89. func (dve *DuplicateVoteEvidence) Height() int64 {
  90. return dve.VoteA.Height
  91. }
  92. // Address returns the address of the validator.
  93. func (dve *DuplicateVoteEvidence) Address() []byte {
  94. return dve.PubKey.Address()
  95. }
  96. // Hash returns the hash of the evidence.
  97. func (dve *DuplicateVoteEvidence) Bytes() []byte {
  98. return cdcEncode(dve)
  99. }
  100. // Hash returns the hash of the evidence.
  101. func (dve *DuplicateVoteEvidence) Hash() []byte {
  102. return tmhash.Sum(cdcEncode(dve))
  103. }
  104. // Verify returns an error if the two votes aren't conflicting.
  105. // To be conflicting, they must be from the same validator, for the same H/R/S, but for different blocks.
  106. func (dve *DuplicateVoteEvidence) Verify(chainID string, pubKey crypto.PubKey) error {
  107. // H/R/S must be the same
  108. if dve.VoteA.Height != dve.VoteB.Height ||
  109. dve.VoteA.Round != dve.VoteB.Round ||
  110. dve.VoteA.Type != dve.VoteB.Type {
  111. return fmt.Errorf("DuplicateVoteEvidence Error: H/R/S does not match. Got %v and %v", dve.VoteA, dve.VoteB)
  112. }
  113. // Address must be the same
  114. if !bytes.Equal(dve.VoteA.ValidatorAddress, dve.VoteB.ValidatorAddress) {
  115. return fmt.Errorf(
  116. "DuplicateVoteEvidence Error: Validator addresses do not match. Got %X and %X",
  117. dve.VoteA.ValidatorAddress,
  118. dve.VoteB.ValidatorAddress,
  119. )
  120. }
  121. // Index must be the same
  122. if dve.VoteA.ValidatorIndex != dve.VoteB.ValidatorIndex {
  123. return fmt.Errorf(
  124. "DuplicateVoteEvidence Error: Validator indices do not match. Got %d and %d",
  125. dve.VoteA.ValidatorIndex,
  126. dve.VoteB.ValidatorIndex,
  127. )
  128. }
  129. // BlockIDs must be different
  130. if dve.VoteA.BlockID.Equals(dve.VoteB.BlockID) {
  131. return fmt.Errorf(
  132. "DuplicateVoteEvidence Error: BlockIDs are the same (%v) - not a real duplicate vote",
  133. dve.VoteA.BlockID,
  134. )
  135. }
  136. // pubkey must match address (this should already be true, sanity check)
  137. addr := dve.VoteA.ValidatorAddress
  138. if !bytes.Equal(pubKey.Address(), addr) {
  139. return fmt.Errorf("DuplicateVoteEvidence FAILED SANITY CHECK - address (%X) doesn't match pubkey (%v - %X)",
  140. addr, pubKey, pubKey.Address())
  141. }
  142. // Signatures must be valid
  143. if !pubKey.VerifyBytes(dve.VoteA.SignBytes(chainID), dve.VoteA.Signature) {
  144. return fmt.Errorf("DuplicateVoteEvidence Error verifying VoteA: %v", ErrVoteInvalidSignature)
  145. }
  146. if !pubKey.VerifyBytes(dve.VoteB.SignBytes(chainID), dve.VoteB.Signature) {
  147. return fmt.Errorf("DuplicateVoteEvidence Error verifying VoteB: %v", ErrVoteInvalidSignature)
  148. }
  149. return nil
  150. }
  151. // Equal checks if two pieces of evidence are equal.
  152. func (dve *DuplicateVoteEvidence) Equal(ev Evidence) bool {
  153. if _, ok := ev.(*DuplicateVoteEvidence); !ok {
  154. return false
  155. }
  156. // just check their hashes
  157. dveHash := tmhash.Sum(cdcEncode(dve))
  158. evHash := tmhash.Sum(cdcEncode(ev))
  159. return bytes.Equal(dveHash, evHash)
  160. }
  161. // ValidateBasic performs basic validation.
  162. func (dve *DuplicateVoteEvidence) ValidateBasic() error {
  163. if len(dve.PubKey.Bytes()) == 0 {
  164. return errors.New("Empty PubKey")
  165. }
  166. if dve.VoteA == nil || dve.VoteB == nil {
  167. return fmt.Errorf("One or both of the votes are empty %v, %v", dve.VoteA, dve.VoteB)
  168. }
  169. if err := dve.VoteA.ValidateBasic(); err != nil {
  170. return fmt.Errorf("Invalid VoteA: %v", err)
  171. }
  172. if err := dve.VoteB.ValidateBasic(); err != nil {
  173. return fmt.Errorf("Invalid VoteB: %v", err)
  174. }
  175. return nil
  176. }
  177. //-----------------------------------------------------------------
  178. // UNSTABLE
  179. type MockRandomGoodEvidence struct {
  180. MockGoodEvidence
  181. randBytes []byte
  182. }
  183. var _ Evidence = &MockRandomGoodEvidence{}
  184. // UNSTABLE
  185. func NewMockRandomGoodEvidence(height int64, address []byte, randBytes []byte) MockRandomGoodEvidence {
  186. return MockRandomGoodEvidence{
  187. MockGoodEvidence{height, address}, randBytes,
  188. }
  189. }
  190. func (e MockRandomGoodEvidence) Hash() []byte {
  191. return []byte(fmt.Sprintf("%d-%x", e.Height_, e.randBytes))
  192. }
  193. // UNSTABLE
  194. type MockGoodEvidence struct {
  195. Height_ int64
  196. Address_ []byte
  197. }
  198. var _ Evidence = &MockGoodEvidence{}
  199. // UNSTABLE
  200. func NewMockGoodEvidence(height int64, idx int, address []byte) MockGoodEvidence {
  201. return MockGoodEvidence{height, address}
  202. }
  203. func (e MockGoodEvidence) Height() int64 { return e.Height_ }
  204. func (e MockGoodEvidence) Address() []byte { return e.Address_ }
  205. func (e MockGoodEvidence) Hash() []byte {
  206. return []byte(fmt.Sprintf("%d-%x", e.Height_, e.Address_))
  207. }
  208. func (e MockGoodEvidence) Bytes() []byte {
  209. return []byte(fmt.Sprintf("%d-%x", e.Height_, e.Address_))
  210. }
  211. func (e MockGoodEvidence) Verify(chainID string, pubKey crypto.PubKey) error { return nil }
  212. func (e MockGoodEvidence) Equal(ev Evidence) bool {
  213. e2 := ev.(MockGoodEvidence)
  214. return e.Height_ == e2.Height_ &&
  215. bytes.Equal(e.Address_, e2.Address_)
  216. }
  217. func (e MockGoodEvidence) ValidateBasic() error { return nil }
  218. func (e MockGoodEvidence) String() string {
  219. return fmt.Sprintf("GoodEvidence: %d/%s", e.Height_, e.Address_)
  220. }
  221. // UNSTABLE
  222. type MockBadEvidence struct {
  223. MockGoodEvidence
  224. }
  225. func (e MockBadEvidence) Verify(chainID string, pubKey crypto.PubKey) error {
  226. return fmt.Errorf("MockBadEvidence")
  227. }
  228. func (e MockBadEvidence) Equal(ev Evidence) bool {
  229. e2 := ev.(MockBadEvidence)
  230. return e.Height_ == e2.Height_ &&
  231. bytes.Equal(e.Address_, e2.Address_)
  232. }
  233. func (e MockBadEvidence) ValidateBasic() error { return nil }
  234. func (e MockBadEvidence) String() string {
  235. return fmt.Sprintf("BadEvidence: %d/%s", e.Height_, e.Address_)
  236. }
  237. //-------------------------------------------
  238. // EvidenceList is a list of Evidence. Evidences is not a word.
  239. type EvidenceList []Evidence
  240. // Hash returns the simple merkle root hash of the EvidenceList.
  241. func (evl EvidenceList) Hash() []byte {
  242. // These allocations are required because Evidence is not of type Bytes, and
  243. // golang slices can't be typed cast. This shouldn't be a performance problem since
  244. // the Evidence size is capped.
  245. evidenceBzs := make([][]byte, len(evl))
  246. for i := 0; i < len(evl); i++ {
  247. evidenceBzs[i] = evl[i].Bytes()
  248. }
  249. return merkle.SimpleHashFromByteSlices(evidenceBzs)
  250. }
  251. func (evl EvidenceList) String() string {
  252. s := ""
  253. for _, e := range evl {
  254. s += fmt.Sprintf("%s\t\t", e)
  255. }
  256. return s
  257. }
  258. // Has returns true if the evidence is in the EvidenceList.
  259. func (evl EvidenceList) Has(evidence Evidence) bool {
  260. for _, ev := range evl {
  261. if ev.Equal(evidence) {
  262. return true
  263. }
  264. }
  265. return false
  266. }