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.

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