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.

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