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.

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