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.

286 lines
9.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
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"
  9. "github.com/tendermint/tendermint/crypto/ed25519"
  10. "github.com/tendermint/tendermint/crypto/tmhash"
  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. Type: SignedMsgType(t),
  25. Height: 12345,
  26. Round: 2,
  27. Timestamp: stamp,
  28. BlockID: BlockID{
  29. Hash: tmhash.Sum([]byte("blockID_hash")),
  30. PartsHeader: PartSetHeader{
  31. Total: 1000000,
  32. Hash: tmhash.Sum([]byte("blockID_part_set_header_hash")),
  33. },
  34. },
  35. ValidatorAddress: crypto.AddressHash([]byte("validator_address")),
  36. ValidatorIndex: 56789,
  37. }
  38. }
  39. func TestVoteSignable(t *testing.T) {
  40. vote := examplePrecommit()
  41. signBytes := vote.SignBytes("test_chain_id")
  42. expected, err := cdc.MarshalBinaryLengthPrefixed(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 TestVoteSignBytesTestVectors(t *testing.T) {
  47. tests := []struct {
  48. chainID string
  49. vote *Vote
  50. want []byte
  51. }{
  52. 0: {
  53. "", &Vote{},
  54. // NOTE: Height and Round are skipped here. This case needs to be considered while parsing.
  55. []byte{0xd, 0x2a, 0xb, 0x8, 0x80, 0x92, 0xb8, 0xc3, 0x98, 0xfe, 0xff, 0xff, 0xff, 0x1},
  56. },
  57. // with proper (fixed size) height and round (PreCommit):
  58. 1: {
  59. "", &Vote{Height: 1, Round: 1, Type: PrecommitType},
  60. []byte{
  61. 0x21, // length
  62. 0x8, // (field_number << 3) | wire_type
  63. 0x2, // PrecommitType
  64. 0x11, // (field_number << 3) | wire_type
  65. 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // height
  66. 0x19, // (field_number << 3) | wire_type
  67. 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // round
  68. 0x2a, // (field_number << 3) | wire_type
  69. // remaining fields (timestamp):
  70. 0xb, 0x8, 0x80, 0x92, 0xb8, 0xc3, 0x98, 0xfe, 0xff, 0xff, 0xff, 0x1},
  71. },
  72. // with proper (fixed size) height and round (PreVote):
  73. 2: {
  74. "", &Vote{Height: 1, Round: 1, Type: PrevoteType},
  75. []byte{
  76. 0x21, // length
  77. 0x8, // (field_number << 3) | wire_type
  78. 0x1, // PrevoteType
  79. 0x11, // (field_number << 3) | wire_type
  80. 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // height
  81. 0x19, // (field_number << 3) | wire_type
  82. 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // round
  83. 0x2a, // (field_number << 3) | wire_type
  84. // remaining fields (timestamp):
  85. 0xb, 0x8, 0x80, 0x92, 0xb8, 0xc3, 0x98, 0xfe, 0xff, 0xff, 0xff, 0x1},
  86. },
  87. 3: {
  88. "", &Vote{Height: 1, Round: 1},
  89. []byte{
  90. 0x1f, // length
  91. 0x11, // (field_number << 3) | wire_type
  92. 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // height
  93. 0x19, // (field_number << 3) | wire_type
  94. 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // round
  95. // remaining fields (timestamp):
  96. 0x2a,
  97. 0xb, 0x8, 0x80, 0x92, 0xb8, 0xc3, 0x98, 0xfe, 0xff, 0xff, 0xff, 0x1},
  98. },
  99. // containing non-empty chain_id:
  100. 4: {
  101. "test_chain_id", &Vote{Height: 1, Round: 1},
  102. []byte{
  103. 0x2e, // length
  104. 0x11, // (field_number << 3) | wire_type
  105. 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // height
  106. 0x19, // (field_number << 3) | wire_type
  107. 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // round
  108. // remaining fields:
  109. 0x2a, // (field_number << 3) | wire_type
  110. 0xb, 0x8, 0x80, 0x92, 0xb8, 0xc3, 0x98, 0xfe, 0xff, 0xff, 0xff, 0x1, // timestamp
  111. // (field_number << 3) | wire_type
  112. 0x32,
  113. 0xd, 0x74, 0x65, 0x73, 0x74, 0x5f, 0x63, 0x68, 0x61, 0x69, 0x6e, 0x5f, 0x69, 0x64}, // chainID
  114. },
  115. }
  116. for i, tc := range tests {
  117. got := tc.vote.SignBytes(tc.chainID)
  118. require.Equal(t, tc.want, got, "test case #%v: got unexpected sign bytes for Vote.", i)
  119. }
  120. }
  121. func TestVoteProposalNotEq(t *testing.T) {
  122. cv := CanonicalizeVote("", &Vote{Height: 1, Round: 1})
  123. p := CanonicalizeProposal("", &Proposal{Height: 1, Round: 1})
  124. vb, err := cdc.MarshalBinaryLengthPrefixed(cv)
  125. require.NoError(t, err)
  126. pb, err := cdc.MarshalBinaryLengthPrefixed(p)
  127. require.NoError(t, err)
  128. require.NotEqual(t, vb, pb)
  129. }
  130. func TestVoteVerifySignature(t *testing.T) {
  131. privVal := NewMockPV()
  132. pubkey := privVal.GetPubKey()
  133. vote := examplePrecommit()
  134. signBytes := vote.SignBytes("test_chain_id")
  135. // sign it
  136. err := privVal.SignVote("test_chain_id", vote)
  137. require.NoError(t, err)
  138. // verify the same vote
  139. valid := pubkey.VerifyBytes(vote.SignBytes("test_chain_id"), vote.Signature)
  140. require.True(t, valid)
  141. // serialize, deserialize and verify again....
  142. precommit := new(Vote)
  143. bs, err := cdc.MarshalBinaryLengthPrefixed(vote)
  144. require.NoError(t, err)
  145. err = cdc.UnmarshalBinaryLengthPrefixed(bs, &precommit)
  146. require.NoError(t, err)
  147. // verify the transmitted vote
  148. newSignBytes := precommit.SignBytes("test_chain_id")
  149. require.Equal(t, string(signBytes), string(newSignBytes))
  150. valid = pubkey.VerifyBytes(newSignBytes, precommit.Signature)
  151. require.True(t, valid)
  152. }
  153. func TestIsVoteTypeValid(t *testing.T) {
  154. tc := []struct {
  155. name string
  156. in SignedMsgType
  157. out bool
  158. }{
  159. {"Prevote", PrevoteType, true},
  160. {"Precommit", PrecommitType, true},
  161. {"InvalidType", SignedMsgType(0x3), false},
  162. }
  163. for _, tt := range tc {
  164. tt := tt
  165. t.Run(tt.name, func(st *testing.T) {
  166. if rs := IsVoteTypeValid(tt.in); rs != tt.out {
  167. t.Errorf("got unexpected Vote type. Expected:\n%v\nGot:\n%v", rs, tt.out)
  168. }
  169. })
  170. }
  171. }
  172. func TestVoteVerify(t *testing.T) {
  173. privVal := NewMockPV()
  174. pubkey := privVal.GetPubKey()
  175. vote := examplePrevote()
  176. vote.ValidatorAddress = pubkey.Address()
  177. err := vote.Verify("test_chain_id", ed25519.GenPrivKey().PubKey())
  178. if assert.Error(t, err) {
  179. assert.Equal(t, ErrVoteInvalidValidatorAddress, err)
  180. }
  181. err = vote.Verify("test_chain_id", pubkey)
  182. if assert.Error(t, err) {
  183. assert.Equal(t, ErrVoteInvalidSignature, err)
  184. }
  185. }
  186. func TestMaxVoteBytes(t *testing.T) {
  187. // time is varint encoded so need to pick the max.
  188. // year int, month Month, day, hour, min, sec, nsec int, loc *Location
  189. timestamp := time.Date(math.MaxInt64, 0, 0, 0, 0, 0, math.MaxInt64, time.UTC)
  190. vote := &Vote{
  191. ValidatorAddress: crypto.AddressHash([]byte("validator_address")),
  192. ValidatorIndex: math.MaxInt64,
  193. Height: math.MaxInt64,
  194. Round: math.MaxInt64,
  195. Timestamp: timestamp,
  196. Type: PrevoteType,
  197. BlockID: BlockID{
  198. Hash: tmhash.Sum([]byte("blockID_hash")),
  199. PartsHeader: PartSetHeader{
  200. Total: math.MaxInt64,
  201. Hash: tmhash.Sum([]byte("blockID_part_set_header_hash")),
  202. },
  203. },
  204. }
  205. privVal := NewMockPV()
  206. err := privVal.SignVote("test_chain_id", vote)
  207. require.NoError(t, err)
  208. bz, err := cdc.MarshalBinaryLengthPrefixed(vote)
  209. require.NoError(t, err)
  210. assert.EqualValues(t, MaxVoteBytes, len(bz))
  211. }
  212. func TestVoteString(t *testing.T) {
  213. str := examplePrecommit().String()
  214. expected := `Vote{56789:6AF1F4111082 12345/02/2(Precommit) 8B01023386C3 000000000000 @ 2017-12-25T03:00:01.234Z}`
  215. if str != expected {
  216. t.Errorf("got unexpected string for Vote. Expected:\n%v\nGot:\n%v", expected, str)
  217. }
  218. str2 := examplePrevote().String()
  219. expected = `Vote{56789:6AF1F4111082 12345/02/1(Prevote) 8B01023386C3 000000000000 @ 2017-12-25T03:00:01.234Z}`
  220. if str2 != expected {
  221. t.Errorf("got unexpected string for Vote. Expected:\n%v\nGot:\n%v", expected, str2)
  222. }
  223. }
  224. func TestVoteValidateBasic(t *testing.T) {
  225. privVal := NewMockPV()
  226. testCases := []struct {
  227. testName string
  228. malleateVote func(*Vote)
  229. expectErr bool
  230. }{
  231. {"Good Vote", func(v *Vote) {}, false},
  232. {"Negative Height", func(v *Vote) { v.Height = -1 }, true},
  233. {"Negative Round", func(v *Vote) { v.Round = -1 }, true},
  234. {"Invalid BlockID", func(v *Vote) {
  235. v.BlockID = BlockID{[]byte{1, 2, 3}, PartSetHeader{111, []byte("blockparts")}}
  236. }, true},
  237. {"Invalid Address", func(v *Vote) { v.ValidatorAddress = make([]byte, 1) }, true},
  238. {"Invalid ValidatorIndex", func(v *Vote) { v.ValidatorIndex = -1 }, true},
  239. {"Invalid Signature", func(v *Vote) { v.Signature = nil }, true},
  240. {"Too big Signature", func(v *Vote) { v.Signature = make([]byte, MaxSignatureSize+1) }, true},
  241. }
  242. for _, tc := range testCases {
  243. tc := tc
  244. t.Run(tc.testName, func(t *testing.T) {
  245. vote := examplePrecommit()
  246. err := privVal.SignVote("test_chain_id", vote)
  247. require.NoError(t, err)
  248. tc.malleateVote(vote)
  249. assert.Equal(t, tc.expectErr, vote.ValidateBasic() != nil, "Validate Basic had an unexpected result")
  250. })
  251. }
  252. }