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.

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