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.

149 lines
3.6 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/ed25519"
  9. "github.com/tendermint/tendermint/crypto/tmhash"
  10. tmtime "github.com/tendermint/tendermint/types/time"
  11. )
  12. func examplePrevote() *Vote {
  13. return exampleVote(VoteTypePrevote)
  14. }
  15. func examplePrecommit() *Vote {
  16. return exampleVote(VoteTypePrecommit)
  17. }
  18. func exampleVote(t byte) *Vote {
  19. var stamp, err = time.Parse(TimeFormat, "2017-12-25T03:00:01.234Z")
  20. if err != nil {
  21. panic(err)
  22. }
  23. return &Vote{
  24. ValidatorAddress: tmhash.Sum([]byte("validator_address")),
  25. ValidatorIndex: 56789,
  26. Height: 12345,
  27. Round: 2,
  28. Timestamp: stamp,
  29. Type: t,
  30. BlockID: BlockID{
  31. Hash: tmhash.Sum([]byte("blockID_hash")),
  32. PartsHeader: PartSetHeader{
  33. Total: 1000000,
  34. Hash: tmhash.Sum([]byte("blockID_part_set_header_hash")),
  35. },
  36. },
  37. }
  38. }
  39. func TestVoteSignable(t *testing.T) {
  40. vote := examplePrecommit()
  41. signBytes := vote.SignBytes("test_chain_id")
  42. expected, err := cdc.MarshalBinary(CanonicalizeVote("test_chain_id", vote))
  43. require.NoError(t, err)
  44. require.Equal(t, expected, signBytes, "Got unexpected sign bytes for Vote.")
  45. }
  46. func TestVoteVerifySignature(t *testing.T) {
  47. privVal := NewMockPV()
  48. pubkey := privVal.GetPubKey()
  49. vote := examplePrecommit()
  50. signBytes := vote.SignBytes("test_chain_id")
  51. // sign it
  52. err := privVal.SignVote("test_chain_id", vote)
  53. require.NoError(t, err)
  54. // verify the same vote
  55. valid := pubkey.VerifyBytes(vote.SignBytes("test_chain_id"), vote.Signature)
  56. require.True(t, valid)
  57. // serialize, deserialize and verify again....
  58. precommit := new(Vote)
  59. bs, err := cdc.MarshalBinary(vote)
  60. require.NoError(t, err)
  61. err = cdc.UnmarshalBinary(bs, &precommit)
  62. require.NoError(t, err)
  63. // verify the transmitted vote
  64. newSignBytes := precommit.SignBytes("test_chain_id")
  65. require.Equal(t, string(signBytes), string(newSignBytes))
  66. valid = pubkey.VerifyBytes(newSignBytes, precommit.Signature)
  67. require.True(t, valid)
  68. }
  69. func TestIsVoteTypeValid(t *testing.T) {
  70. tc := []struct {
  71. name string
  72. in byte
  73. out bool
  74. }{
  75. {"Prevote", VoteTypePrevote, true},
  76. {"Precommit", VoteTypePrecommit, true},
  77. {"InvalidType", byte(3), false},
  78. }
  79. for _, tt := range tc {
  80. tt := tt
  81. t.Run(tt.name, func(st *testing.T) {
  82. if rs := IsVoteTypeValid(tt.in); rs != tt.out {
  83. t.Errorf("Got unexpected Vote type. Expected:\n%v\nGot:\n%v", rs, tt.out)
  84. }
  85. })
  86. }
  87. }
  88. func TestVoteVerify(t *testing.T) {
  89. privVal := NewMockPV()
  90. pubkey := privVal.GetPubKey()
  91. vote := examplePrevote()
  92. vote.ValidatorAddress = pubkey.Address()
  93. err := vote.Verify("test_chain_id", ed25519.GenPrivKey().PubKey())
  94. if assert.Error(t, err) {
  95. assert.Equal(t, ErrVoteInvalidValidatorAddress, err)
  96. }
  97. err = vote.Verify("test_chain_id", pubkey)
  98. if assert.Error(t, err) {
  99. assert.Equal(t, ErrVoteInvalidSignature, err)
  100. }
  101. }
  102. func TestMaxVoteBytes(t *testing.T) {
  103. vote := &Vote{
  104. ValidatorAddress: tmhash.Sum([]byte("validator_address")),
  105. ValidatorIndex: math.MaxInt64,
  106. Height: math.MaxInt64,
  107. Round: math.MaxInt64,
  108. Timestamp: tmtime.Now(),
  109. Type: VoteTypePrevote,
  110. BlockID: BlockID{
  111. Hash: tmhash.Sum([]byte("blockID_hash")),
  112. PartsHeader: PartSetHeader{
  113. Total: math.MaxInt64,
  114. Hash: tmhash.Sum([]byte("blockID_part_set_header_hash")),
  115. },
  116. },
  117. }
  118. privVal := NewMockPV()
  119. err := privVal.SignVote("test_chain_id", vote)
  120. require.NoError(t, err)
  121. bz, err := cdc.MarshalBinary(vote)
  122. require.NoError(t, err)
  123. assert.EqualValues(t, MaxVoteBytes, len(bz))
  124. }