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.

300 lines
9.5 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. // (field_number << 3) | wire_type
  125. 0x32,
  126. 0xd, 0x74, 0x65, 0x73, 0x74, 0x5f, 0x63, 0x68, 0x61, 0x69, 0x6e, 0x5f, 0x69, 0x64}, // chainID
  127. },
  128. }
  129. for i, tc := range tests {
  130. got := tc.vote.SignBytes(tc.chainID)
  131. require.Equal(t, tc.want, got, "test case #%v: got unexpected sign bytes for Vote.", i)
  132. }
  133. }
  134. func TestVoteProposalNotEq(t *testing.T) {
  135. cv := CanonicalizeVote("", &Vote{Height: 1, Round: 1})
  136. p := CanonicalizeProposal("", &Proposal{Height: 1, Round: 1})
  137. vb, err := cdc.MarshalBinaryLengthPrefixed(cv)
  138. require.NoError(t, err)
  139. pb, err := cdc.MarshalBinaryLengthPrefixed(p)
  140. require.NoError(t, err)
  141. require.NotEqual(t, vb, pb)
  142. }
  143. func TestVoteVerifySignature(t *testing.T) {
  144. privVal := NewMockPV()
  145. pubkey := privVal.GetPubKey()
  146. vote := examplePrecommit()
  147. signBytes := vote.SignBytes("test_chain_id")
  148. // sign it
  149. err := privVal.SignVote("test_chain_id", vote)
  150. require.NoError(t, err)
  151. // verify the same vote
  152. valid := pubkey.VerifyBytes(vote.SignBytes("test_chain_id"), vote.Signature)
  153. require.True(t, valid)
  154. // serialize, deserialize and verify again....
  155. precommit := new(Vote)
  156. bs, err := cdc.MarshalBinaryLengthPrefixed(vote)
  157. require.NoError(t, err)
  158. err = cdc.UnmarshalBinaryLengthPrefixed(bs, &precommit)
  159. require.NoError(t, err)
  160. // verify the transmitted vote
  161. newSignBytes := precommit.SignBytes("test_chain_id")
  162. require.Equal(t, string(signBytes), string(newSignBytes))
  163. valid = pubkey.VerifyBytes(newSignBytes, precommit.Signature)
  164. require.True(t, valid)
  165. }
  166. func TestIsVoteTypeValid(t *testing.T) {
  167. tc := []struct {
  168. name string
  169. in SignedMsgType
  170. out bool
  171. }{
  172. {"Prevote", PrevoteType, true},
  173. {"Precommit", PrecommitType, true},
  174. {"InvalidType", SignedMsgType(0x3), false},
  175. }
  176. for _, tt := range tc {
  177. tt := tt
  178. t.Run(tt.name, func(st *testing.T) {
  179. if rs := IsVoteTypeValid(tt.in); rs != tt.out {
  180. t.Errorf("Got unexpected Vote type. Expected:\n%v\nGot:\n%v", rs, tt.out)
  181. }
  182. })
  183. }
  184. }
  185. func TestVoteVerify(t *testing.T) {
  186. privVal := NewMockPV()
  187. pubkey := privVal.GetPubKey()
  188. vote := examplePrevote()
  189. vote.ValidatorAddress = pubkey.Address()
  190. err := vote.Verify("test_chain_id", ed25519.GenPrivKey().PubKey())
  191. if assert.Error(t, err) {
  192. assert.Equal(t, ErrVoteInvalidValidatorAddress, err)
  193. }
  194. err = vote.Verify("test_chain_id", pubkey)
  195. if assert.Error(t, err) {
  196. assert.Equal(t, ErrVoteInvalidSignature, err)
  197. }
  198. }
  199. func TestMaxVoteBytes(t *testing.T) {
  200. // time is varint encoded so need to pick the max.
  201. // year int, month Month, day, hour, min, sec, nsec int, loc *Location
  202. timestamp := time.Date(math.MaxInt64, 0, 0, 0, 0, 0, math.MaxInt64, time.UTC)
  203. vote := &Vote{
  204. ValidatorAddress: crypto.AddressHash([]byte("validator_address")),
  205. ValidatorIndex: math.MaxInt64,
  206. Height: math.MaxInt64,
  207. Round: math.MaxInt64,
  208. Timestamp: timestamp,
  209. Type: PrevoteType,
  210. BlockID: BlockID{
  211. Hash: tmhash.Sum([]byte("blockID_hash")),
  212. PartsHeader: PartSetHeader{
  213. Total: math.MaxInt64,
  214. Hash: tmhash.Sum([]byte("blockID_part_set_header_hash")),
  215. },
  216. },
  217. }
  218. privVal := NewMockPV()
  219. err := privVal.SignVote("test_chain_id", vote)
  220. require.NoError(t, err)
  221. bz, err := cdc.MarshalBinaryLengthPrefixed(vote)
  222. require.NoError(t, err)
  223. assert.EqualValues(t, MaxVoteBytes, len(bz))
  224. }
  225. func TestVoteString(t *testing.T) {
  226. str := examplePrecommit().String()
  227. expected := `Vote{56789:6AF1F4111082 12345/02/2(Precommit) 8B01023386C3 000000000000 @ 2017-12-25T03:00:01.234Z}`
  228. if str != expected {
  229. t.Errorf("Got unexpected string for Vote. Expected:\n%v\nGot:\n%v", expected, str)
  230. }
  231. str2 := examplePrevote().String()
  232. expected = `Vote{56789:6AF1F4111082 12345/02/1(Prevote) 8B01023386C3 000000000000 @ 2017-12-25T03:00:01.234Z}`
  233. if str2 != expected {
  234. t.Errorf("Got unexpected string for Vote. Expected:\n%v\nGot:\n%v", expected, str2)
  235. }
  236. }
  237. func TestVoteValidateBasic(t *testing.T) {
  238. privVal := NewMockPV()
  239. testCases := []struct {
  240. testName string
  241. malleateVote func(*Vote)
  242. expectErr bool
  243. }{
  244. {"Good Vote", func(v *Vote) {}, false},
  245. {"Negative Height", func(v *Vote) { v.Height = -1 }, true},
  246. {"Negative Round", func(v *Vote) { v.Round = -1 }, true},
  247. {"Invalid BlockID", func(v *Vote) {
  248. v.BlockID = BlockID{[]byte{1, 2, 3}, PartSetHeader{111, []byte("blockparts")}}
  249. }, true},
  250. {"Invalid Address", func(v *Vote) { v.ValidatorAddress = make([]byte, 1) }, true},
  251. {"Invalid ValidatorIndex", func(v *Vote) { v.ValidatorIndex = -1 }, true},
  252. {"Invalid Signature", func(v *Vote) { v.Signature = nil }, true},
  253. {"Too big Signature", func(v *Vote) { v.Signature = make([]byte, MaxSignatureSize+1) }, true},
  254. }
  255. for _, tc := range testCases {
  256. tc := tc
  257. t.Run(tc.testName, func(t *testing.T) {
  258. vote := examplePrecommit()
  259. err := privVal.SignVote("test_chain_id", vote)
  260. require.NoError(t, err)
  261. tc.malleateVote(vote)
  262. assert.Equal(t, tc.expectErr, vote.ValidateBasic() != nil, "Validate Basic had an unexpected result")
  263. })
  264. }
  265. }