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.

227 lines
7.0 KiB

max-bytes PR follow-up (#2318) * ReapMaxTxs: return all txs if max is negative this mirrors ReapMaxBytes behavior See https://github.com/tendermint/tendermint/pull/2184#discussion_r214439950 * increase MaxAminoOverheadForBlock tested with: ``` func TestMaxAminoOverheadForBlock(t *testing.T) { maxChainID := "" for i := 0; i < MaxChainIDLen; i++ { maxChainID += "𠜎" } h := Header{ ChainID: maxChainID, Height: 10, Time: time.Now().UTC(), NumTxs: 100, TotalTxs: 200, LastBlockID: makeBlockID(make([]byte, 20), 300, make([]byte, 20)), LastCommitHash: tmhash.Sum([]byte("last_commit_hash")), DataHash: tmhash.Sum([]byte("data_hash")), ValidatorsHash: tmhash.Sum([]byte("validators_hash")), NextValidatorsHash: tmhash.Sum([]byte("next_validators_hash")), ConsensusHash: tmhash.Sum([]byte("consensus_hash")), AppHash: tmhash.Sum([]byte("app_hash")), LastResultsHash: tmhash.Sum([]byte("last_results_hash")), EvidenceHash: tmhash.Sum([]byte("evidence_hash")), ProposerAddress: tmhash.Sum([]byte("proposer_address")), } b := Block{ Header: h, Data: Data{Txs: makeTxs(10000, 100)}, Evidence: EvidenceData{}, LastCommit: &Commit{}, } bz, err := cdc.MarshalBinary(b) require.NoError(t, err) assert.Equal(t, MaxHeaderBytes+MaxAminoOverheadForBlock-2, len(bz)-1000000-20000-1) } ``` * fix MaxYYY constants calculation by using math.MaxInt64 See https://github.com/tendermint/tendermint/pull/2184#discussion_r214444244 * pass mempool filter as an option See https://github.com/tendermint/tendermint/pull/2184#discussion_r214445869 * fixes after Dev's comments
6 years ago
  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 = 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. func MaxEvidenceBytesPerBlock(blockMaxBytes int) int {
  44. return blockMaxBytes / 10
  45. }
  46. //-------------------------------------------
  47. // DuplicateVoteEvidence contains evidence a validator signed two conflicting votes.
  48. type DuplicateVoteEvidence struct {
  49. PubKey crypto.PubKey
  50. VoteA *Vote
  51. VoteB *Vote
  52. }
  53. // String returns a string representation of the evidence.
  54. func (dve *DuplicateVoteEvidence) String() string {
  55. return fmt.Sprintf("VoteA: %v; VoteB: %v", dve.VoteA, dve.VoteB)
  56. }
  57. // Height returns the height this evidence refers to.
  58. func (dve *DuplicateVoteEvidence) Height() int64 {
  59. return dve.VoteA.Height
  60. }
  61. // Address returns the address of the validator.
  62. func (dve *DuplicateVoteEvidence) Address() []byte {
  63. return dve.PubKey.Address()
  64. }
  65. // Hash returns the hash of the evidence.
  66. func (dve *DuplicateVoteEvidence) Hash() []byte {
  67. return aminoHasher(dve).Hash()
  68. }
  69. // Verify returns an error if the two votes aren't conflicting.
  70. // To be conflicting, they must be from the same validator, for the same H/R/S, but for different blocks.
  71. func (dve *DuplicateVoteEvidence) Verify(chainID string, pubKey crypto.PubKey) error {
  72. // H/R/S must be the same
  73. if dve.VoteA.Height != dve.VoteB.Height ||
  74. dve.VoteA.Round != dve.VoteB.Round ||
  75. dve.VoteA.Type != dve.VoteB.Type {
  76. return fmt.Errorf("DuplicateVoteEvidence Error: H/R/S does not match. Got %v and %v", dve.VoteA, dve.VoteB)
  77. }
  78. // Address must be the same
  79. if !bytes.Equal(dve.VoteA.ValidatorAddress, dve.VoteB.ValidatorAddress) {
  80. return fmt.Errorf("DuplicateVoteEvidence Error: Validator addresses do not match. Got %X and %X", dve.VoteA.ValidatorAddress, dve.VoteB.ValidatorAddress)
  81. }
  82. // Index must be the same
  83. if dve.VoteA.ValidatorIndex != dve.VoteB.ValidatorIndex {
  84. return fmt.Errorf("DuplicateVoteEvidence Error: Validator indices do not match. Got %d and %d", dve.VoteA.ValidatorIndex, dve.VoteB.ValidatorIndex)
  85. }
  86. // BlockIDs must be different
  87. if dve.VoteA.BlockID.Equals(dve.VoteB.BlockID) {
  88. return fmt.Errorf("DuplicateVoteEvidence Error: BlockIDs are the same (%v) - not a real duplicate vote", dve.VoteA.BlockID)
  89. }
  90. // pubkey must match address (this should already be true, sanity check)
  91. addr := dve.VoteA.ValidatorAddress
  92. if !bytes.Equal(pubKey.Address(), addr) {
  93. return fmt.Errorf("DuplicateVoteEvidence FAILED SANITY CHECK - address (%X) doesn't match pubkey (%v - %X)",
  94. addr, pubKey, pubKey.Address())
  95. }
  96. // Signatures must be valid
  97. if !pubKey.VerifyBytes(dve.VoteA.SignBytes(chainID), dve.VoteA.Signature) {
  98. return fmt.Errorf("DuplicateVoteEvidence Error verifying VoteA: %v", ErrVoteInvalidSignature)
  99. }
  100. if !pubKey.VerifyBytes(dve.VoteB.SignBytes(chainID), dve.VoteB.Signature) {
  101. return fmt.Errorf("DuplicateVoteEvidence Error verifying VoteB: %v", ErrVoteInvalidSignature)
  102. }
  103. return nil
  104. }
  105. // Equal checks if two pieces of evidence are equal.
  106. func (dve *DuplicateVoteEvidence) Equal(ev Evidence) bool {
  107. if _, ok := ev.(*DuplicateVoteEvidence); !ok {
  108. return false
  109. }
  110. // just check their hashes
  111. dveHash := aminoHasher(dve).Hash()
  112. evHash := aminoHasher(ev).Hash()
  113. return bytes.Equal(dveHash, evHash)
  114. }
  115. //-----------------------------------------------------------------
  116. // UNSTABLE
  117. type MockGoodEvidence struct {
  118. Height_ int64
  119. Address_ []byte
  120. }
  121. // UNSTABLE
  122. func NewMockGoodEvidence(height int64, idx int, address []byte) MockGoodEvidence {
  123. return MockGoodEvidence{height, address}
  124. }
  125. func (e MockGoodEvidence) Height() int64 { return e.Height_ }
  126. func (e MockGoodEvidence) Address() []byte { return e.Address_ }
  127. func (e MockGoodEvidence) Hash() []byte {
  128. return []byte(fmt.Sprintf("%d-%x", e.Height_, e.Address_))
  129. }
  130. func (e MockGoodEvidence) Verify(chainID string, pubKey crypto.PubKey) error { return nil }
  131. func (e MockGoodEvidence) Equal(ev Evidence) bool {
  132. e2 := ev.(MockGoodEvidence)
  133. return e.Height_ == e2.Height_ &&
  134. bytes.Equal(e.Address_, e2.Address_)
  135. }
  136. func (e MockGoodEvidence) String() string {
  137. return fmt.Sprintf("GoodEvidence: %d/%s", e.Height_, e.Address_)
  138. }
  139. // UNSTABLE
  140. type MockBadEvidence struct {
  141. MockGoodEvidence
  142. }
  143. func (e MockBadEvidence) Verify(chainID string, pubKey crypto.PubKey) error {
  144. return fmt.Errorf("MockBadEvidence")
  145. }
  146. func (e MockBadEvidence) Equal(ev Evidence) bool {
  147. e2 := ev.(MockBadEvidence)
  148. return e.Height_ == e2.Height_ &&
  149. bytes.Equal(e.Address_, e2.Address_)
  150. }
  151. func (e MockBadEvidence) String() string {
  152. return fmt.Sprintf("BadEvidence: %d/%s", e.Height_, e.Address_)
  153. }
  154. //-------------------------------------------
  155. // EvidenceList is a list of Evidence. Evidences is not a word.
  156. type EvidenceList []Evidence
  157. // Hash returns the simple merkle root hash of the EvidenceList.
  158. func (evl EvidenceList) Hash() []byte {
  159. // Recursive impl.
  160. // Copied from crypto/merkle to avoid allocations
  161. switch len(evl) {
  162. case 0:
  163. return nil
  164. case 1:
  165. return evl[0].Hash()
  166. default:
  167. left := EvidenceList(evl[:(len(evl)+1)/2]).Hash()
  168. right := EvidenceList(evl[(len(evl)+1)/2:]).Hash()
  169. return merkle.SimpleHashFromTwoHashes(left, right)
  170. }
  171. }
  172. func (evl EvidenceList) String() string {
  173. s := ""
  174. for _, e := range evl {
  175. s += fmt.Sprintf("%s\t\t", e)
  176. }
  177. return s
  178. }
  179. // Has returns true if the evidence is in the EvidenceList.
  180. func (evl EvidenceList) Has(evidence Evidence) bool {
  181. for _, ev := range evl {
  182. if ev.Equal(evidence) {
  183. return true
  184. }
  185. }
  186. return false
  187. }