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.

151 lines
3.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/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. signStr := string(signBytes)
  43. expected := `{"@chain_id":"test_chain_id","@type":"vote","block_id":{"hash":"8B01023386C371778ECB6368573E539AFC3CC860","parts":{"hash":"72DB3D959635DFF1BB567BEDAA70573392C51596","total":"1000000"}},"height":"12345","round":"2","timestamp":"2017-12-25T03:00:01.234Z","type":2}`
  44. if signStr != expected {
  45. // NOTE: when this fails, you probably want to fix up consensus/replay_test too
  46. t.Errorf("Got unexpected sign string for Vote. Expected:\n%v\nGot:\n%v", expected, signStr)
  47. }
  48. }
  49. func TestVoteVerifySignature(t *testing.T) {
  50. privVal := NewMockPV()
  51. pubkey := privVal.GetPubKey()
  52. vote := examplePrecommit()
  53. signBytes := vote.SignBytes("test_chain_id")
  54. // sign it
  55. err := privVal.SignVote("test_chain_id", vote)
  56. require.NoError(t, err)
  57. // verify the same vote
  58. valid := pubkey.VerifyBytes(vote.SignBytes("test_chain_id"), vote.Signature)
  59. require.True(t, valid)
  60. // serialize, deserialize and verify again....
  61. precommit := new(Vote)
  62. bs, err := cdc.MarshalBinary(vote)
  63. require.NoError(t, err)
  64. err = cdc.UnmarshalBinary(bs, &precommit)
  65. require.NoError(t, err)
  66. // verify the transmitted vote
  67. newSignBytes := precommit.SignBytes("test_chain_id")
  68. require.Equal(t, string(signBytes), string(newSignBytes))
  69. valid = pubkey.VerifyBytes(newSignBytes, precommit.Signature)
  70. require.True(t, valid)
  71. }
  72. func TestIsVoteTypeValid(t *testing.T) {
  73. tc := []struct {
  74. name string
  75. in byte
  76. out bool
  77. }{
  78. {"Prevote", VoteTypePrevote, true},
  79. {"Precommit", VoteTypePrecommit, true},
  80. {"InvalidType", byte(3), false},
  81. }
  82. for _, tt := range tc {
  83. tt := tt
  84. t.Run(tt.name, func(st *testing.T) {
  85. if rs := IsVoteTypeValid(tt.in); rs != tt.out {
  86. t.Errorf("Got unexpected Vote type. Expected:\n%v\nGot:\n%v", rs, tt.out)
  87. }
  88. })
  89. }
  90. }
  91. func TestVoteVerify(t *testing.T) {
  92. privVal := NewMockPV()
  93. pubkey := privVal.GetPubKey()
  94. vote := examplePrevote()
  95. vote.ValidatorAddress = pubkey.Address()
  96. err := vote.Verify("test_chain_id", ed25519.GenPrivKey().PubKey())
  97. if assert.Error(t, err) {
  98. assert.Equal(t, ErrVoteInvalidValidatorAddress, err)
  99. }
  100. err = vote.Verify("test_chain_id", pubkey)
  101. if assert.Error(t, err) {
  102. assert.Equal(t, ErrVoteInvalidSignature, err)
  103. }
  104. }
  105. func TestMaxVoteBytes(t *testing.T) {
  106. vote := &Vote{
  107. ValidatorAddress: tmhash.Sum([]byte("validator_address")),
  108. ValidatorIndex: math.MaxInt64,
  109. Height: math.MaxInt64,
  110. Round: math.MaxInt64,
  111. Timestamp: tmtime.Now(),
  112. Type: VoteTypePrevote,
  113. BlockID: BlockID{
  114. Hash: tmhash.Sum([]byte("blockID_hash")),
  115. PartsHeader: PartSetHeader{
  116. Total: math.MaxInt64,
  117. Hash: tmhash.Sum([]byte("blockID_part_set_header_hash")),
  118. },
  119. },
  120. }
  121. privVal := NewMockPV()
  122. err := privVal.SignVote("test_chain_id", vote)
  123. require.NoError(t, err)
  124. bz, err := cdc.MarshalBinary(vote)
  125. require.NoError(t, err)
  126. assert.Equal(t, MaxVoteBytes, len(bz))
  127. }