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.

174 lines
5.9 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
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
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. "math"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/stretchr/testify/require"
  7. "github.com/tendermint/tendermint/crypto/secp256k1"
  8. "github.com/tendermint/tendermint/crypto/tmhash"
  9. )
  10. type voteData struct {
  11. vote1 *Vote
  12. vote2 *Vote
  13. valid bool
  14. }
  15. func makeVote(val PrivValidator, chainID string, valIndex int, height int64, round, step int, blockID BlockID) *Vote {
  16. addr := val.GetPubKey().Address()
  17. v := &Vote{
  18. ValidatorAddress: addr,
  19. ValidatorIndex: valIndex,
  20. Height: height,
  21. Round: round,
  22. Type: SignedMsgType(step),
  23. BlockID: blockID,
  24. }
  25. err := val.SignVote(chainID, v)
  26. if err != nil {
  27. panic(err)
  28. }
  29. return v
  30. }
  31. func TestEvidence(t *testing.T) {
  32. val := NewMockPV()
  33. val2 := NewMockPV()
  34. blockID := makeBlockID([]byte("blockhash"), 1000, []byte("partshash"))
  35. blockID2 := makeBlockID([]byte("blockhash2"), 1000, []byte("partshash"))
  36. blockID3 := makeBlockID([]byte("blockhash"), 10000, []byte("partshash"))
  37. blockID4 := makeBlockID([]byte("blockhash"), 10000, []byte("partshash2"))
  38. const chainID = "mychain"
  39. vote1 := makeVote(val, chainID, 0, 10, 2, 1, blockID)
  40. badVote := makeVote(val, chainID, 0, 10, 2, 1, blockID)
  41. err := val2.SignVote(chainID, badVote)
  42. if err != nil {
  43. panic(err)
  44. }
  45. cases := []voteData{
  46. {vote1, makeVote(val, chainID, 0, 10, 2, 1, blockID2), true}, // different block ids
  47. {vote1, makeVote(val, chainID, 0, 10, 2, 1, blockID3), true},
  48. {vote1, makeVote(val, chainID, 0, 10, 2, 1, blockID4), true},
  49. {vote1, makeVote(val, chainID, 0, 10, 2, 1, blockID), false}, // wrong block id
  50. {vote1, makeVote(val, "mychain2", 0, 10, 2, 1, blockID2), false}, // wrong chain id
  51. {vote1, makeVote(val, chainID, 1, 10, 2, 1, blockID2), false}, // wrong val index
  52. {vote1, makeVote(val, chainID, 0, 11, 2, 1, blockID2), false}, // wrong height
  53. {vote1, makeVote(val, chainID, 0, 10, 3, 1, blockID2), false}, // wrong round
  54. {vote1, makeVote(val, chainID, 0, 10, 2, 2, blockID2), false}, // wrong step
  55. {vote1, makeVote(val2, chainID, 0, 10, 2, 1, blockID), false}, // wrong validator
  56. {vote1, badVote, false}, // signed by wrong key
  57. }
  58. pubKey := val.GetPubKey()
  59. for _, c := range cases {
  60. ev := &DuplicateVoteEvidence{
  61. VoteA: c.vote1,
  62. VoteB: c.vote2,
  63. }
  64. if c.valid {
  65. assert.Nil(t, ev.Verify(chainID, pubKey), "evidence should be valid")
  66. } else {
  67. assert.NotNil(t, ev.Verify(chainID, pubKey), "evidence should be invalid")
  68. }
  69. }
  70. }
  71. func TestDuplicatedVoteEvidence(t *testing.T) {
  72. ev := randomDuplicatedVoteEvidence()
  73. assert.True(t, ev.Equal(ev))
  74. assert.False(t, ev.Equal(&DuplicateVoteEvidence{}))
  75. }
  76. func TestEvidenceList(t *testing.T) {
  77. ev := randomDuplicatedVoteEvidence()
  78. evl := EvidenceList([]Evidence{ev})
  79. assert.NotNil(t, evl.Hash())
  80. assert.True(t, evl.Has(ev))
  81. assert.False(t, evl.Has(&DuplicateVoteEvidence{}))
  82. }
  83. func TestMaxEvidenceBytes(t *testing.T) {
  84. val := NewMockPV()
  85. blockID := makeBlockID(tmhash.Sum([]byte("blockhash")), math.MaxInt64, tmhash.Sum([]byte("partshash")))
  86. blockID2 := makeBlockID(tmhash.Sum([]byte("blockhash2")), math.MaxInt64, tmhash.Sum([]byte("partshash")))
  87. const chainID = "mychain"
  88. ev := &DuplicateVoteEvidence{
  89. PubKey: secp256k1.GenPrivKey().PubKey(), // use secp because it's pubkey is longer
  90. VoteA: makeVote(val, chainID, math.MaxInt64, math.MaxInt64, math.MaxInt64, math.MaxInt64, blockID),
  91. VoteB: makeVote(val, chainID, math.MaxInt64, math.MaxInt64, math.MaxInt64, math.MaxInt64, blockID2),
  92. }
  93. bz, err := cdc.MarshalBinaryLengthPrefixed(ev)
  94. require.NoError(t, err)
  95. assert.EqualValues(t, MaxEvidenceBytes, len(bz))
  96. }
  97. func randomDuplicatedVoteEvidence() *DuplicateVoteEvidence {
  98. val := NewMockPV()
  99. blockID := makeBlockID([]byte("blockhash"), 1000, []byte("partshash"))
  100. blockID2 := makeBlockID([]byte("blockhash2"), 1000, []byte("partshash"))
  101. const chainID = "mychain"
  102. return &DuplicateVoteEvidence{
  103. VoteA: makeVote(val, chainID, 0, 10, 2, 1, blockID),
  104. VoteB: makeVote(val, chainID, 0, 10, 2, 1, blockID2),
  105. }
  106. }
  107. func TestDuplicateVoteEvidenceValidation(t *testing.T) {
  108. val := NewMockPV()
  109. blockID := makeBlockID(tmhash.Sum([]byte("blockhash")), math.MaxInt64, tmhash.Sum([]byte("partshash")))
  110. blockID2 := makeBlockID(tmhash.Sum([]byte("blockhash2")), math.MaxInt64, tmhash.Sum([]byte("partshash")))
  111. const chainID = "mychain"
  112. testCases := []struct {
  113. testName string
  114. malleateEvidence func(*DuplicateVoteEvidence)
  115. expectErr bool
  116. }{
  117. {"Good DuplicateVoteEvidence", func(ev *DuplicateVoteEvidence) {}, false},
  118. {"Nil vote A", func(ev *DuplicateVoteEvidence) { ev.VoteA = nil }, true},
  119. {"Nil vote B", func(ev *DuplicateVoteEvidence) { ev.VoteB = nil }, true},
  120. {"Nil votes", func(ev *DuplicateVoteEvidence) {
  121. ev.VoteA = nil
  122. ev.VoteB = nil
  123. }, true},
  124. {"Invalid vote type", func(ev *DuplicateVoteEvidence) {
  125. ev.VoteA = makeVote(val, chainID, math.MaxInt64, math.MaxInt64, math.MaxInt64, 0, blockID2)
  126. }, true},
  127. {"Invalid vote order", func(ev *DuplicateVoteEvidence) {
  128. swap := ev.VoteA.Copy()
  129. ev.VoteA = ev.VoteB.Copy()
  130. ev.VoteB = swap
  131. }, true},
  132. }
  133. for _, tc := range testCases {
  134. tc := tc
  135. t.Run(tc.testName, func(t *testing.T) {
  136. pk := secp256k1.GenPrivKey().PubKey()
  137. vote1 := makeVote(val, chainID, math.MaxInt64, math.MaxInt64, math.MaxInt64, 0x02, blockID)
  138. vote2 := makeVote(val, chainID, math.MaxInt64, math.MaxInt64, math.MaxInt64, 0x02, blockID2)
  139. ev := NewDuplicateVoteEvidence(pk, vote1, vote2)
  140. tc.malleateEvidence(ev)
  141. assert.Equal(t, tc.expectErr, ev.ValidateBasic() != nil, "Validate Basic had an unexpected result")
  142. })
  143. }
  144. }
  145. func TestMockGoodEvidenceValidateBasic(t *testing.T) {
  146. goodEvidence := NewMockGoodEvidence(int64(1), 1, []byte{1})
  147. assert.Nil(t, goodEvidence.ValidateBasic())
  148. }
  149. func TestMockBadEvidenceValidateBasic(t *testing.T) {
  150. badEvidence := MockBadEvidence{MockGoodEvidence: NewMockGoodEvidence(int64(1), 1, []byte{1})}
  151. assert.Nil(t, badEvidence.ValidateBasic())
  152. }