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.

241 lines
7.4 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
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(byte(PrevoteType))
  14. }
  15. func examplePrecommit() *Vote {
  16. return exampleVote(byte(PrecommitType))
  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: SignedMsgType(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 TestVoteSignableTestVectors(t *testing.T) {
  47. voteWithVersion := CanonicalizeVote("", &Vote{Height: 1, Round: 1})
  48. voteWithVersion.Version = 123
  49. tests := []struct {
  50. canonicalVote CanonicalVote
  51. want []byte
  52. }{
  53. {
  54. CanonicalizeVote("", &Vote{}),
  55. // NOTE: Height and Round are skipped here. This case needs to be considered while parsing.
  56. []byte{0xb, 0x2a, 0x9, 0x9, 0x0, 0x9, 0x6e, 0x88, 0xf1, 0xff, 0xff, 0xff},
  57. },
  58. // with proper (fixed size) height and round (PreCommit):
  59. {
  60. CanonicalizeVote("", &Vote{Height: 1, Round: 1, Type: PrecommitType}),
  61. []byte{
  62. 0x1f, // total length
  63. 0x11, // (field_number << 3) | wire_type (version is missing)
  64. 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // height
  65. 0x19, // (field_number << 3) | wire_type
  66. 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // round
  67. 0x20, // (field_number << 3) | wire_type
  68. 0x2, // PrecommitType
  69. 0x2a, // (field_number << 3) | wire_type
  70. // remaining fields (timestamp):
  71. 0x9, 0x9, 0x0, 0x9, 0x6e, 0x88, 0xf1, 0xff, 0xff, 0xff},
  72. },
  73. // with proper (fixed size) height and round (PreVote):
  74. {
  75. CanonicalizeVote("", &Vote{Height: 1, Round: 1, Type: PrevoteType}),
  76. []byte{
  77. 0x1f, // total length
  78. 0x11, // (field_number << 3) | wire_type (version is missing)
  79. 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // height
  80. 0x19, // (field_number << 3) | wire_type
  81. 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // round
  82. 0x20, // (field_number << 3) | wire_type
  83. 0x1, // PrevoteType
  84. 0x2a, // (field_number << 3) | wire_type
  85. // remaining fields (timestamp):
  86. 0x9, 0x9, 0x0, 0x9, 0x6e, 0x88, 0xf1, 0xff, 0xff, 0xff},
  87. },
  88. // containing version (empty type)
  89. {
  90. voteWithVersion,
  91. []byte{
  92. 0x26, // total length
  93. 0x9, // (field_number << 3) | wire_type
  94. 0x7b, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // version (123)
  95. 0x11, // (field_number << 3) | wire_type
  96. 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // height
  97. 0x19, // (field_number << 3) | wire_type
  98. 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // round
  99. // remaining fields (timestamp):
  100. 0x2a,
  101. 0x9, 0x9, 0x0, 0x9, 0x6e, 0x88, 0xf1, 0xff, 0xff, 0xff},
  102. },
  103. // containing non-empty chain_id:
  104. {
  105. CanonicalizeVote("test_chain_id", &Vote{Height: 1, Round: 1}),
  106. []byte{
  107. 0x2c, // total length
  108. 0x11, // (field_number << 3) | wire_type
  109. 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // height
  110. 0x19, // (field_number << 3) | wire_type
  111. 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // round
  112. // remaining fields:
  113. 0x2a, // (field_number << 3) | wire_type
  114. 0x9, 0x9, 0x0, 0x9, 0x6e, 0x88, 0xf1, 0xff, 0xff, 0xff, // timestamp
  115. 0x3a, // (field_number << 3) | wire_type
  116. 0xd, 0x74, 0x65, 0x73, 0x74, 0x5f, 0x63, 0x68, 0x61, 0x69, 0x6e, 0x5f, 0x69, 0x64}, // chainID
  117. },
  118. }
  119. for i, tc := range tests {
  120. got, err := cdc.MarshalBinary(tc.canonicalVote)
  121. require.NoError(t, err)
  122. require.Equal(t, tc.want, got, "test case #%v: got unexpected sign bytes for Vote.", i)
  123. }
  124. }
  125. func TestVoteProposalNotEq(t *testing.T) {
  126. cv := CanonicalizeVote("", &Vote{Height: 1, Round: 1})
  127. p := CanonicalizeProposal("", &Proposal{Height: 1, Round: 1})
  128. vb, err := cdc.MarshalBinary(cv)
  129. require.NoError(t, err)
  130. pb, err := cdc.MarshalBinary(p)
  131. require.NoError(t, err)
  132. require.NotEqual(t, vb, pb)
  133. }
  134. func TestVoteVerifySignature(t *testing.T) {
  135. privVal := NewMockPV()
  136. pubkey := privVal.GetPubKey()
  137. vote := examplePrecommit()
  138. signBytes := vote.SignBytes("test_chain_id")
  139. // sign it
  140. err := privVal.SignVote("test_chain_id", vote)
  141. require.NoError(t, err)
  142. // verify the same vote
  143. valid := pubkey.VerifyBytes(vote.SignBytes("test_chain_id"), vote.Signature)
  144. require.True(t, valid)
  145. // serialize, deserialize and verify again....
  146. precommit := new(Vote)
  147. bs, err := cdc.MarshalBinary(vote)
  148. require.NoError(t, err)
  149. err = cdc.UnmarshalBinary(bs, &precommit)
  150. require.NoError(t, err)
  151. // verify the transmitted vote
  152. newSignBytes := precommit.SignBytes("test_chain_id")
  153. require.Equal(t, string(signBytes), string(newSignBytes))
  154. valid = pubkey.VerifyBytes(newSignBytes, precommit.Signature)
  155. require.True(t, valid)
  156. }
  157. func TestIsVoteTypeValid(t *testing.T) {
  158. tc := []struct {
  159. name string
  160. in SignedMsgType
  161. out bool
  162. }{
  163. {"Prevote", PrevoteType, true},
  164. {"Precommit", PrecommitType, true},
  165. {"InvalidType", SignedMsgType(0x3), false},
  166. }
  167. for _, tt := range tc {
  168. tt := tt
  169. t.Run(tt.name, func(st *testing.T) {
  170. if rs := IsVoteTypeValid(tt.in); rs != tt.out {
  171. t.Errorf("Got unexpected Vote type. Expected:\n%v\nGot:\n%v", rs, tt.out)
  172. }
  173. })
  174. }
  175. }
  176. func TestVoteVerify(t *testing.T) {
  177. privVal := NewMockPV()
  178. pubkey := privVal.GetPubKey()
  179. vote := examplePrevote()
  180. vote.ValidatorAddress = pubkey.Address()
  181. err := vote.Verify("test_chain_id", ed25519.GenPrivKey().PubKey())
  182. if assert.Error(t, err) {
  183. assert.Equal(t, ErrVoteInvalidValidatorAddress, err)
  184. }
  185. err = vote.Verify("test_chain_id", pubkey)
  186. if assert.Error(t, err) {
  187. assert.Equal(t, ErrVoteInvalidSignature, err)
  188. }
  189. }
  190. func TestMaxVoteBytes(t *testing.T) {
  191. vote := &Vote{
  192. ValidatorAddress: tmhash.Sum([]byte("validator_address")),
  193. ValidatorIndex: math.MaxInt64,
  194. Height: math.MaxInt64,
  195. Round: math.MaxInt64,
  196. Timestamp: tmtime.Now(),
  197. Type: PrevoteType,
  198. BlockID: BlockID{
  199. Hash: tmhash.Sum([]byte("blockID_hash")),
  200. PartsHeader: PartSetHeader{
  201. Total: math.MaxInt64,
  202. Hash: tmhash.Sum([]byte("blockID_part_set_header_hash")),
  203. },
  204. },
  205. }
  206. privVal := NewMockPV()
  207. err := privVal.SignVote("test_chain_id", vote)
  208. require.NoError(t, err)
  209. bz, err := cdc.MarshalBinary(vote)
  210. require.NoError(t, err)
  211. assert.EqualValues(t, MaxVoteBytes, len(bz))
  212. }