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.

328 lines
10 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
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/gogo/protobuf/proto"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/stretchr/testify/require"
  9. "github.com/tendermint/tendermint/crypto"
  10. "github.com/tendermint/tendermint/crypto/ed25519"
  11. "github.com/tendermint/tendermint/crypto/tmhash"
  12. "github.com/tendermint/tendermint/libs/protoio"
  13. tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
  14. )
  15. func examplePrevote() *Vote {
  16. return exampleVote(byte(tmproto.PrevoteType))
  17. }
  18. func examplePrecommit() *Vote {
  19. return exampleVote(byte(tmproto.PrecommitType))
  20. }
  21. func exampleVote(t byte) *Vote {
  22. var stamp, err = time.Parse(TimeFormat, "2017-12-25T03:00:01.234Z")
  23. if err != nil {
  24. panic(err)
  25. }
  26. return &Vote{
  27. Type: tmproto.SignedMsgType(t),
  28. Height: 12345,
  29. Round: 2,
  30. Timestamp: stamp,
  31. BlockID: BlockID{
  32. Hash: tmhash.Sum([]byte("blockID_hash")),
  33. PartSetHeader: PartSetHeader{
  34. Total: 1000000,
  35. Hash: tmhash.Sum([]byte("blockID_part_set_header_hash")),
  36. },
  37. },
  38. ValidatorAddress: crypto.AddressHash([]byte("validator_address")),
  39. ValidatorIndex: 56789,
  40. }
  41. }
  42. func TestVoteSignable(t *testing.T) {
  43. vote := examplePrecommit()
  44. v := vote.ToProto()
  45. signBytes := VoteSignBytes("test_chain_id", v)
  46. pb := CanonicalizeVote("test_chain_id", v)
  47. expected, err := protoio.MarshalDelimited(&pb)
  48. require.NoError(t, err)
  49. require.Equal(t, expected, signBytes, "Got unexpected sign bytes for Vote.")
  50. }
  51. func TestVoteSignBytesTestVectors(t *testing.T) {
  52. tests := []struct {
  53. chainID string
  54. vote *Vote
  55. want []byte
  56. }{
  57. 0: {
  58. "", &Vote{},
  59. // NOTE: Height and Round are skipped here. This case needs to be considered while parsing.
  60. []byte{0xd, 0x2a, 0xb, 0x8, 0x80, 0x92, 0xb8, 0xc3, 0x98, 0xfe, 0xff, 0xff, 0xff, 0x1},
  61. },
  62. // with proper (fixed size) height and round (PreCommit):
  63. 1: {
  64. "", &Vote{Height: 1, Round: 1, Type: tmproto.PrecommitType},
  65. []byte{
  66. 0x21, // length
  67. 0x8, // (field_number << 3) | wire_type
  68. 0x2, // PrecommitType
  69. 0x11, // (field_number << 3) | wire_type
  70. 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // height
  71. 0x19, // (field_number << 3) | wire_type
  72. 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // round
  73. 0x2a, // (field_number << 3) | wire_type
  74. // remaining fields (timestamp):
  75. 0xb, 0x8, 0x80, 0x92, 0xb8, 0xc3, 0x98, 0xfe, 0xff, 0xff, 0xff, 0x1},
  76. },
  77. // with proper (fixed size) height and round (PreVote):
  78. 2: {
  79. "", &Vote{Height: 1, Round: 1, Type: tmproto.PrevoteType},
  80. []byte{
  81. 0x21, // length
  82. 0x8, // (field_number << 3) | wire_type
  83. 0x1, // PrevoteType
  84. 0x11, // (field_number << 3) | wire_type
  85. 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // height
  86. 0x19, // (field_number << 3) | wire_type
  87. 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // round
  88. 0x2a, // (field_number << 3) | wire_type
  89. // remaining fields (timestamp):
  90. 0xb, 0x8, 0x80, 0x92, 0xb8, 0xc3, 0x98, 0xfe, 0xff, 0xff, 0xff, 0x1},
  91. },
  92. 3: {
  93. "", &Vote{Height: 1, Round: 1},
  94. []byte{
  95. 0x1f, // length
  96. 0x11, // (field_number << 3) | wire_type
  97. 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // height
  98. 0x19, // (field_number << 3) | wire_type
  99. 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // round
  100. // remaining fields (timestamp):
  101. 0x2a,
  102. 0xb, 0x8, 0x80, 0x92, 0xb8, 0xc3, 0x98, 0xfe, 0xff, 0xff, 0xff, 0x1},
  103. },
  104. // containing non-empty chain_id:
  105. 4: {
  106. "test_chain_id", &Vote{Height: 1, Round: 1},
  107. []byte{
  108. 0x2e, // length
  109. 0x11, // (field_number << 3) | wire_type
  110. 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // height
  111. 0x19, // (field_number << 3) | wire_type
  112. 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // round
  113. // remaining fields:
  114. 0x2a, // (field_number << 3) | wire_type
  115. 0xb, 0x8, 0x80, 0x92, 0xb8, 0xc3, 0x98, 0xfe, 0xff, 0xff, 0xff, 0x1, // timestamp
  116. // (field_number << 3) | wire_type
  117. 0x32,
  118. 0xd, 0x74, 0x65, 0x73, 0x74, 0x5f, 0x63, 0x68, 0x61, 0x69, 0x6e, 0x5f, 0x69, 0x64}, // chainID
  119. },
  120. }
  121. for i, tc := range tests {
  122. v := tc.vote.ToProto()
  123. got := VoteSignBytes(tc.chainID, v)
  124. assert.Equal(t, len(tc.want), len(got), "test case #%v: got unexpected sign bytes length for Vote.", i)
  125. assert.Equal(t, tc.want, got, "test case #%v: got unexpected sign bytes for Vote.", i)
  126. }
  127. }
  128. func TestVoteProposalNotEq(t *testing.T) {
  129. cv := CanonicalizeVote("", &tmproto.Vote{Height: 1, Round: 1})
  130. p := CanonicalizeProposal("", &tmproto.Proposal{Height: 1, Round: 1})
  131. vb, err := proto.Marshal(&cv)
  132. require.NoError(t, err)
  133. pb, err := proto.Marshal(&p)
  134. require.NoError(t, err)
  135. require.NotEqual(t, vb, pb)
  136. }
  137. func TestVoteVerifySignature(t *testing.T) {
  138. privVal := NewMockPV()
  139. pubkey, err := privVal.GetPubKey()
  140. require.NoError(t, err)
  141. vote := examplePrecommit()
  142. v := vote.ToProto()
  143. signBytes := VoteSignBytes("test_chain_id", v)
  144. // sign it
  145. err = privVal.SignVote("test_chain_id", v)
  146. require.NoError(t, err)
  147. // verify the same vote
  148. valid := pubkey.VerifySignature(VoteSignBytes("test_chain_id", v), v.Signature)
  149. require.True(t, valid)
  150. // serialize, deserialize and verify again....
  151. precommit := new(tmproto.Vote)
  152. bs, err := proto.Marshal(v)
  153. require.NoError(t, err)
  154. err = proto.Unmarshal(bs, precommit)
  155. require.NoError(t, err)
  156. // verify the transmitted vote
  157. newSignBytes := VoteSignBytes("test_chain_id", precommit)
  158. require.Equal(t, string(signBytes), string(newSignBytes))
  159. valid = pubkey.VerifySignature(newSignBytes, precommit.Signature)
  160. require.True(t, valid)
  161. }
  162. func TestIsVoteTypeValid(t *testing.T) {
  163. tc := []struct {
  164. name string
  165. in tmproto.SignedMsgType
  166. out bool
  167. }{
  168. {"Prevote", tmproto.PrevoteType, true},
  169. {"Precommit", tmproto.PrecommitType, true},
  170. {"InvalidType", tmproto.SignedMsgType(0x3), false},
  171. }
  172. for _, tt := range tc {
  173. tt := tt
  174. t.Run(tt.name, func(st *testing.T) {
  175. if rs := IsVoteTypeValid(tt.in); rs != tt.out {
  176. t.Errorf("got unexpected Vote type. Expected:\n%v\nGot:\n%v", rs, tt.out)
  177. }
  178. })
  179. }
  180. }
  181. func TestVoteVerify(t *testing.T) {
  182. privVal := NewMockPV()
  183. pubkey, err := privVal.GetPubKey()
  184. require.NoError(t, err)
  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.MaxInt32,
  203. Height: math.MaxInt64,
  204. Round: math.MaxInt32,
  205. Timestamp: timestamp,
  206. Type: tmproto.PrevoteType,
  207. BlockID: BlockID{
  208. Hash: tmhash.Sum([]byte("blockID_hash")),
  209. PartSetHeader: PartSetHeader{
  210. Total: math.MaxInt32,
  211. Hash: tmhash.Sum([]byte("blockID_part_set_header_hash")),
  212. },
  213. },
  214. }
  215. v := vote.ToProto()
  216. privVal := NewMockPV()
  217. err := privVal.SignVote("test_chain_id", v)
  218. require.NoError(t, err)
  219. bz, err := proto.Marshal(v)
  220. require.NoError(t, err)
  221. assert.EqualValues(t, MaxVoteBytes, len(bz))
  222. }
  223. func TestVoteString(t *testing.T) {
  224. str := examplePrecommit().String()
  225. expected := `Vote{56789:6AF1F4111082 12345/02/SIGNED_MSG_TYPE_PRECOMMIT(Precommit) 8B01023386C3 000000000000 @ 2017-12-25T03:00:01.234Z}` //nolint:lll //ignore line length for tests
  226. if str != expected {
  227. t.Errorf("got unexpected string for Vote. Expected:\n%v\nGot:\n%v", expected, str)
  228. }
  229. str2 := examplePrevote().String()
  230. expected = `Vote{56789:6AF1F4111082 12345/02/SIGNED_MSG_TYPE_PREVOTE(Prevote) 8B01023386C3 000000000000 @ 2017-12-25T03:00:01.234Z}` //nolint:lll //ignore line length for tests
  231. if str2 != expected {
  232. t.Errorf("got unexpected string for Vote. Expected:\n%v\nGot:\n%v", expected, str2)
  233. }
  234. }
  235. func TestVoteValidateBasic(t *testing.T) {
  236. privVal := NewMockPV()
  237. testCases := []struct {
  238. testName string
  239. malleateVote func(*Vote)
  240. expectErr bool
  241. }{
  242. {"Good Vote", func(v *Vote) {}, false},
  243. {"Negative Height", func(v *Vote) { v.Height = -1 }, true},
  244. {"Negative Round", func(v *Vote) { v.Round = -1 }, true},
  245. {"Invalid BlockID", func(v *Vote) {
  246. v.BlockID = BlockID{[]byte{1, 2, 3}, PartSetHeader{111, []byte("blockparts")}}
  247. }, true},
  248. {"Invalid Address", func(v *Vote) { v.ValidatorAddress = make([]byte, 1) }, true},
  249. {"Invalid ValidatorIndex", func(v *Vote) { v.ValidatorIndex = -1 }, true},
  250. {"Invalid Signature", func(v *Vote) { v.Signature = nil }, true},
  251. {"Too big Signature", func(v *Vote) { v.Signature = make([]byte, MaxSignatureSize+1) }, true},
  252. }
  253. for _, tc := range testCases {
  254. tc := tc
  255. t.Run(tc.testName, func(t *testing.T) {
  256. vote := examplePrecommit()
  257. v := vote.ToProto()
  258. err := privVal.SignVote("test_chain_id", v)
  259. vote.Signature = v.Signature
  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. }
  266. func TestVoteProtobuf(t *testing.T) {
  267. privVal := NewMockPV()
  268. vote := examplePrecommit()
  269. v := vote.ToProto()
  270. err := privVal.SignVote("test_chain_id", v)
  271. vote.Signature = v.Signature
  272. require.NoError(t, err)
  273. testCases := []struct {
  274. msg string
  275. v1 *Vote
  276. expPass bool
  277. }{
  278. {"success", vote, true},
  279. {"fail vote validate basic", &Vote{}, false},
  280. {"failure nil", nil, false},
  281. }
  282. for _, tc := range testCases {
  283. protoProposal := tc.v1.ToProto()
  284. v, err := VoteFromProto(protoProposal)
  285. if tc.expPass {
  286. require.NoError(t, err)
  287. require.Equal(t, tc.v1, v, tc.msg)
  288. } else {
  289. require.Error(t, err)
  290. }
  291. }
  292. }