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.

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