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.

363 lines
11 KiB

internal/libs/protoio: optimize MarshalDelimited by plain byteslice allocations+sync.Pool (#7325) Noticed in profiles that invoking *VoteSignBytes always created a bytes.Buffer, then discarded it inside protoio.MarshalDelimited. I dug further and examined the call paths and noticed that we unconditionally create the bytes.Buffer, even though we might have proto messages (in the common case) that implement MarshalTo([]byte), and invoked varintWriter. Instead by inlining this case, we skip a bunch of allocations and CPU cycles, which then reflects properly on all calling functions. Here are the benchmark results: ```shell $ benchstat before.txt after.txt name old time/op new time/op delta types.VoteSignBytes-8 705ns ± 3% 573ns ± 6% -18.74% (p=0.000 n=18+20) types.CommitVoteSignBytes-8 8.15µs ± 9% 6.81µs ± 4% -16.51% (p=0.000 n=20+19) protoio.MarshalDelimitedWithMarshalTo-8 788ns ± 8% 772ns ± 3% -2.01% (p=0.050 n=20+20) protoio.MarshalDelimitedNoMarshalTo-8 989ns ± 4% 845ns ± 2% -14.51% (p=0.000 n=20+18) name old alloc/op new alloc/op delta types.VoteSignBytes-8 792B ± 0% 600B ± 0% -24.24% (p=0.000 n=20+20) types.CommitVoteSignBytes-8 9.52kB ± 0% 7.60kB ± 0% -20.17% (p=0.000 n=20+20) protoio.MarshalDelimitedNoMarshalTo-8 808B ± 0% 440B ± 0% -45.54% (p=0.000 n=20+20) name old allocs/op new allocs/op delta types.VoteSignBytes-8 13.0 ± 0% 10.0 ± 0% -23.08% (p=0.000 n=20+20) types.CommitVoteSignBytes-8 140 ± 0% 110 ± 0% -21.43% (p=0.000 n=20+20) protoio.MarshalDelimitedNoMarshalTo-8 10.0 ± 0% 7.0 ± 0% -30.00% (p=0.000 n=20+20) ``` Thanks to Tharsis who tasked me to help them increase TPS and who are keen on improving Tendermint and efficiency.
3 years ago
internal/libs/protoio: optimize MarshalDelimited by plain byteslice allocations+sync.Pool (#7325) Noticed in profiles that invoking *VoteSignBytes always created a bytes.Buffer, then discarded it inside protoio.MarshalDelimited. I dug further and examined the call paths and noticed that we unconditionally create the bytes.Buffer, even though we might have proto messages (in the common case) that implement MarshalTo([]byte), and invoked varintWriter. Instead by inlining this case, we skip a bunch of allocations and CPU cycles, which then reflects properly on all calling functions. Here are the benchmark results: ```shell $ benchstat before.txt after.txt name old time/op new time/op delta types.VoteSignBytes-8 705ns ± 3% 573ns ± 6% -18.74% (p=0.000 n=18+20) types.CommitVoteSignBytes-8 8.15µs ± 9% 6.81µs ± 4% -16.51% (p=0.000 n=20+19) protoio.MarshalDelimitedWithMarshalTo-8 788ns ± 8% 772ns ± 3% -2.01% (p=0.050 n=20+20) protoio.MarshalDelimitedNoMarshalTo-8 989ns ± 4% 845ns ± 2% -14.51% (p=0.000 n=20+18) name old alloc/op new alloc/op delta types.VoteSignBytes-8 792B ± 0% 600B ± 0% -24.24% (p=0.000 n=20+20) types.CommitVoteSignBytes-8 9.52kB ± 0% 7.60kB ± 0% -20.17% (p=0.000 n=20+20) protoio.MarshalDelimitedNoMarshalTo-8 808B ± 0% 440B ± 0% -45.54% (p=0.000 n=20+20) name old allocs/op new allocs/op delta types.VoteSignBytes-8 13.0 ± 0% 10.0 ± 0% -23.08% (p=0.000 n=20+20) types.CommitVoteSignBytes-8 140 ± 0% 110 ± 0% -21.43% (p=0.000 n=20+20) protoio.MarshalDelimitedNoMarshalTo-8 10.0 ± 0% 7.0 ± 0% -30.00% (p=0.000 n=20+20) ``` Thanks to Tharsis who tasked me to help them increase TPS and who are keen on improving Tendermint and efficiency.
3 years ago
internal/libs/protoio: optimize MarshalDelimited by plain byteslice allocations+sync.Pool (#7325) Noticed in profiles that invoking *VoteSignBytes always created a bytes.Buffer, then discarded it inside protoio.MarshalDelimited. I dug further and examined the call paths and noticed that we unconditionally create the bytes.Buffer, even though we might have proto messages (in the common case) that implement MarshalTo([]byte), and invoked varintWriter. Instead by inlining this case, we skip a bunch of allocations and CPU cycles, which then reflects properly on all calling functions. Here are the benchmark results: ```shell $ benchstat before.txt after.txt name old time/op new time/op delta types.VoteSignBytes-8 705ns ± 3% 573ns ± 6% -18.74% (p=0.000 n=18+20) types.CommitVoteSignBytes-8 8.15µs ± 9% 6.81µs ± 4% -16.51% (p=0.000 n=20+19) protoio.MarshalDelimitedWithMarshalTo-8 788ns ± 8% 772ns ± 3% -2.01% (p=0.050 n=20+20) protoio.MarshalDelimitedNoMarshalTo-8 989ns ± 4% 845ns ± 2% -14.51% (p=0.000 n=20+18) name old alloc/op new alloc/op delta types.VoteSignBytes-8 792B ± 0% 600B ± 0% -24.24% (p=0.000 n=20+20) types.CommitVoteSignBytes-8 9.52kB ± 0% 7.60kB ± 0% -20.17% (p=0.000 n=20+20) protoio.MarshalDelimitedNoMarshalTo-8 808B ± 0% 440B ± 0% -45.54% (p=0.000 n=20+20) name old allocs/op new allocs/op delta types.VoteSignBytes-8 13.0 ± 0% 10.0 ± 0% -23.08% (p=0.000 n=20+20) types.CommitVoteSignBytes-8 140 ± 0% 110 ± 0% -21.43% (p=0.000 n=20+20) protoio.MarshalDelimitedNoMarshalTo-8 10.0 ± 0% 7.0 ± 0% -30.00% (p=0.000 n=20+20) ``` Thanks to Tharsis who tasked me to help them increase TPS and who are keen on improving Tendermint and efficiency.
3 years ago
internal/libs/protoio: optimize MarshalDelimited by plain byteslice allocations+sync.Pool (#7325) Noticed in profiles that invoking *VoteSignBytes always created a bytes.Buffer, then discarded it inside protoio.MarshalDelimited. I dug further and examined the call paths and noticed that we unconditionally create the bytes.Buffer, even though we might have proto messages (in the common case) that implement MarshalTo([]byte), and invoked varintWriter. Instead by inlining this case, we skip a bunch of allocations and CPU cycles, which then reflects properly on all calling functions. Here are the benchmark results: ```shell $ benchstat before.txt after.txt name old time/op new time/op delta types.VoteSignBytes-8 705ns ± 3% 573ns ± 6% -18.74% (p=0.000 n=18+20) types.CommitVoteSignBytes-8 8.15µs ± 9% 6.81µs ± 4% -16.51% (p=0.000 n=20+19) protoio.MarshalDelimitedWithMarshalTo-8 788ns ± 8% 772ns ± 3% -2.01% (p=0.050 n=20+20) protoio.MarshalDelimitedNoMarshalTo-8 989ns ± 4% 845ns ± 2% -14.51% (p=0.000 n=20+18) name old alloc/op new alloc/op delta types.VoteSignBytes-8 792B ± 0% 600B ± 0% -24.24% (p=0.000 n=20+20) types.CommitVoteSignBytes-8 9.52kB ± 0% 7.60kB ± 0% -20.17% (p=0.000 n=20+20) protoio.MarshalDelimitedNoMarshalTo-8 808B ± 0% 440B ± 0% -45.54% (p=0.000 n=20+20) name old allocs/op new allocs/op delta types.VoteSignBytes-8 13.0 ± 0% 10.0 ± 0% -23.08% (p=0.000 n=20+20) types.CommitVoteSignBytes-8 140 ± 0% 110 ± 0% -21.43% (p=0.000 n=20+20) protoio.MarshalDelimitedNoMarshalTo-8 10.0 ± 0% 7.0 ± 0% -30.00% (p=0.000 n=20+20) ``` Thanks to Tharsis who tasked me to help them increase TPS and who are keen on improving Tendermint and efficiency.
3 years ago
internal/libs/protoio: optimize MarshalDelimited by plain byteslice allocations+sync.Pool (#7325) Noticed in profiles that invoking *VoteSignBytes always created a bytes.Buffer, then discarded it inside protoio.MarshalDelimited. I dug further and examined the call paths and noticed that we unconditionally create the bytes.Buffer, even though we might have proto messages (in the common case) that implement MarshalTo([]byte), and invoked varintWriter. Instead by inlining this case, we skip a bunch of allocations and CPU cycles, which then reflects properly on all calling functions. Here are the benchmark results: ```shell $ benchstat before.txt after.txt name old time/op new time/op delta types.VoteSignBytes-8 705ns ± 3% 573ns ± 6% -18.74% (p=0.000 n=18+20) types.CommitVoteSignBytes-8 8.15µs ± 9% 6.81µs ± 4% -16.51% (p=0.000 n=20+19) protoio.MarshalDelimitedWithMarshalTo-8 788ns ± 8% 772ns ± 3% -2.01% (p=0.050 n=20+20) protoio.MarshalDelimitedNoMarshalTo-8 989ns ± 4% 845ns ± 2% -14.51% (p=0.000 n=20+18) name old alloc/op new alloc/op delta types.VoteSignBytes-8 792B ± 0% 600B ± 0% -24.24% (p=0.000 n=20+20) types.CommitVoteSignBytes-8 9.52kB ± 0% 7.60kB ± 0% -20.17% (p=0.000 n=20+20) protoio.MarshalDelimitedNoMarshalTo-8 808B ± 0% 440B ± 0% -45.54% (p=0.000 n=20+20) name old allocs/op new allocs/op delta types.VoteSignBytes-8 13.0 ± 0% 10.0 ± 0% -23.08% (p=0.000 n=20+20) types.CommitVoteSignBytes-8 140 ± 0% 110 ± 0% -21.43% (p=0.000 n=20+20) protoio.MarshalDelimitedNoMarshalTo-8 10.0 ± 0% 7.0 ± 0% -30.00% (p=0.000 n=20+20) ``` Thanks to Tharsis who tasked me to help them increase TPS and who are keen on improving Tendermint and efficiency.
3 years ago
internal/libs/protoio: optimize MarshalDelimited by plain byteslice allocations+sync.Pool (#7325) Noticed in profiles that invoking *VoteSignBytes always created a bytes.Buffer, then discarded it inside protoio.MarshalDelimited. I dug further and examined the call paths and noticed that we unconditionally create the bytes.Buffer, even though we might have proto messages (in the common case) that implement MarshalTo([]byte), and invoked varintWriter. Instead by inlining this case, we skip a bunch of allocations and CPU cycles, which then reflects properly on all calling functions. Here are the benchmark results: ```shell $ benchstat before.txt after.txt name old time/op new time/op delta types.VoteSignBytes-8 705ns ± 3% 573ns ± 6% -18.74% (p=0.000 n=18+20) types.CommitVoteSignBytes-8 8.15µs ± 9% 6.81µs ± 4% -16.51% (p=0.000 n=20+19) protoio.MarshalDelimitedWithMarshalTo-8 788ns ± 8% 772ns ± 3% -2.01% (p=0.050 n=20+20) protoio.MarshalDelimitedNoMarshalTo-8 989ns ± 4% 845ns ± 2% -14.51% (p=0.000 n=20+18) name old alloc/op new alloc/op delta types.VoteSignBytes-8 792B ± 0% 600B ± 0% -24.24% (p=0.000 n=20+20) types.CommitVoteSignBytes-8 9.52kB ± 0% 7.60kB ± 0% -20.17% (p=0.000 n=20+20) protoio.MarshalDelimitedNoMarshalTo-8 808B ± 0% 440B ± 0% -45.54% (p=0.000 n=20+20) name old allocs/op new allocs/op delta types.VoteSignBytes-8 13.0 ± 0% 10.0 ± 0% -23.08% (p=0.000 n=20+20) types.CommitVoteSignBytes-8 140 ± 0% 110 ± 0% -21.43% (p=0.000 n=20+20) protoio.MarshalDelimitedNoMarshalTo-8 10.0 ± 0% 7.0 ± 0% -30.00% (p=0.000 n=20+20) ``` Thanks to Tharsis who tasked me to help them increase TPS and who are keen on improving Tendermint and efficiency.
3 years ago
internal/libs/protoio: optimize MarshalDelimited by plain byteslice allocations+sync.Pool (#7325) Noticed in profiles that invoking *VoteSignBytes always created a bytes.Buffer, then discarded it inside protoio.MarshalDelimited. I dug further and examined the call paths and noticed that we unconditionally create the bytes.Buffer, even though we might have proto messages (in the common case) that implement MarshalTo([]byte), and invoked varintWriter. Instead by inlining this case, we skip a bunch of allocations and CPU cycles, which then reflects properly on all calling functions. Here are the benchmark results: ```shell $ benchstat before.txt after.txt name old time/op new time/op delta types.VoteSignBytes-8 705ns ± 3% 573ns ± 6% -18.74% (p=0.000 n=18+20) types.CommitVoteSignBytes-8 8.15µs ± 9% 6.81µs ± 4% -16.51% (p=0.000 n=20+19) protoio.MarshalDelimitedWithMarshalTo-8 788ns ± 8% 772ns ± 3% -2.01% (p=0.050 n=20+20) protoio.MarshalDelimitedNoMarshalTo-8 989ns ± 4% 845ns ± 2% -14.51% (p=0.000 n=20+18) name old alloc/op new alloc/op delta types.VoteSignBytes-8 792B ± 0% 600B ± 0% -24.24% (p=0.000 n=20+20) types.CommitVoteSignBytes-8 9.52kB ± 0% 7.60kB ± 0% -20.17% (p=0.000 n=20+20) protoio.MarshalDelimitedNoMarshalTo-8 808B ± 0% 440B ± 0% -45.54% (p=0.000 n=20+20) name old allocs/op new allocs/op delta types.VoteSignBytes-8 13.0 ± 0% 10.0 ± 0% -23.08% (p=0.000 n=20+20) types.CommitVoteSignBytes-8 140 ± 0% 110 ± 0% -21.43% (p=0.000 n=20+20) protoio.MarshalDelimitedNoMarshalTo-8 10.0 ± 0% 7.0 ± 0% -30.00% (p=0.000 n=20+20) ``` Thanks to Tharsis who tasked me to help them increase TPS and who are keen on improving Tendermint and efficiency.
3 years ago
  1. package types
  2. import (
  3. "context"
  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/internal/libs/protoio"
  13. tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
  14. )
  15. func examplePrevote(t *testing.T) *Vote {
  16. t.Helper()
  17. return exampleVote(t, byte(tmproto.PrevoteType))
  18. }
  19. func examplePrecommit(t testing.TB) *Vote {
  20. t.Helper()
  21. return exampleVote(t, byte(tmproto.PrecommitType))
  22. }
  23. func exampleVote(tb testing.TB, t byte) *Vote {
  24. tb.Helper()
  25. var stamp, err = time.Parse(TimeFormat, "2017-12-25T03:00:01.234Z")
  26. require.NoError(tb, err)
  27. return &Vote{
  28. Type: tmproto.SignedMsgType(t),
  29. Height: 12345,
  30. Round: 2,
  31. Timestamp: stamp,
  32. BlockID: BlockID{
  33. Hash: tmhash.Sum([]byte("blockID_hash")),
  34. PartSetHeader: PartSetHeader{
  35. Total: 1000000,
  36. Hash: tmhash.Sum([]byte("blockID_part_set_header_hash")),
  37. },
  38. },
  39. ValidatorAddress: crypto.AddressHash([]byte("validator_address")),
  40. ValidatorIndex: 56789,
  41. }
  42. }
  43. func TestVoteSignable(t *testing.T) {
  44. vote := examplePrecommit(t)
  45. v := vote.ToProto()
  46. signBytes := VoteSignBytes("test_chain_id", v)
  47. pb := CanonicalizeVote("test_chain_id", v)
  48. expected, err := protoio.MarshalDelimited(&pb)
  49. require.NoError(t, err)
  50. require.Equal(t, expected, signBytes, "Got unexpected sign bytes for Vote.")
  51. }
  52. func TestVoteSignBytesTestVectors(t *testing.T) {
  53. tests := []struct {
  54. chainID string
  55. vote *Vote
  56. want []byte
  57. }{
  58. 0: {
  59. "", &Vote{},
  60. // NOTE: Height and Round are skipped here. This case needs to be considered while parsing.
  61. []byte{0xd, 0x2a, 0xb, 0x8, 0x80, 0x92, 0xb8, 0xc3, 0x98, 0xfe, 0xff, 0xff, 0xff, 0x1},
  62. },
  63. // with proper (fixed size) height and round (PreCommit):
  64. 1: {
  65. "", &Vote{Height: 1, Round: 1, Type: tmproto.PrecommitType},
  66. []byte{
  67. 0x21, // length
  68. 0x8, // (field_number << 3) | wire_type
  69. 0x2, // PrecommitType
  70. 0x11, // (field_number << 3) | wire_type
  71. 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // height
  72. 0x19, // (field_number << 3) | wire_type
  73. 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // round
  74. 0x2a, // (field_number << 3) | wire_type
  75. // remaining fields (timestamp):
  76. 0xb, 0x8, 0x80, 0x92, 0xb8, 0xc3, 0x98, 0xfe, 0xff, 0xff, 0xff, 0x1},
  77. },
  78. // with proper (fixed size) height and round (PreVote):
  79. 2: {
  80. "", &Vote{Height: 1, Round: 1, Type: tmproto.PrevoteType},
  81. []byte{
  82. 0x21, // length
  83. 0x8, // (field_number << 3) | wire_type
  84. 0x1, // PrevoteType
  85. 0x11, // (field_number << 3) | wire_type
  86. 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // height
  87. 0x19, // (field_number << 3) | wire_type
  88. 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // round
  89. 0x2a, // (field_number << 3) | wire_type
  90. // remaining fields (timestamp):
  91. 0xb, 0x8, 0x80, 0x92, 0xb8, 0xc3, 0x98, 0xfe, 0xff, 0xff, 0xff, 0x1},
  92. },
  93. 3: {
  94. "", &Vote{Height: 1, Round: 1},
  95. []byte{
  96. 0x1f, // length
  97. 0x11, // (field_number << 3) | wire_type
  98. 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // height
  99. 0x19, // (field_number << 3) | wire_type
  100. 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // round
  101. // remaining fields (timestamp):
  102. 0x2a,
  103. 0xb, 0x8, 0x80, 0x92, 0xb8, 0xc3, 0x98, 0xfe, 0xff, 0xff, 0xff, 0x1},
  104. },
  105. // containing non-empty chain_id:
  106. 4: {
  107. "test_chain_id", &Vote{Height: 1, Round: 1},
  108. []byte{
  109. 0x2e, // length
  110. 0x11, // (field_number << 3) | wire_type
  111. 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // height
  112. 0x19, // (field_number << 3) | wire_type
  113. 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // round
  114. // remaining fields:
  115. 0x2a, // (field_number << 3) | wire_type
  116. 0xb, 0x8, 0x80, 0x92, 0xb8, 0xc3, 0x98, 0xfe, 0xff, 0xff, 0xff, 0x1, // timestamp
  117. // (field_number << 3) | wire_type
  118. 0x32,
  119. 0xd, 0x74, 0x65, 0x73, 0x74, 0x5f, 0x63, 0x68, 0x61, 0x69, 0x6e, 0x5f, 0x69, 0x64}, // chainID
  120. },
  121. }
  122. for i, tc := range tests {
  123. v := tc.vote.ToProto()
  124. got := VoteSignBytes(tc.chainID, v)
  125. assert.Equal(t, len(tc.want), len(got), "test case #%v: got unexpected sign bytes length for Vote.", i)
  126. assert.Equal(t, tc.want, got, "test case #%v: got unexpected sign bytes for Vote.", i)
  127. }
  128. }
  129. func TestVoteProposalNotEq(t *testing.T) {
  130. cv := CanonicalizeVote("", &tmproto.Vote{Height: 1, Round: 1})
  131. p := CanonicalizeProposal("", &tmproto.Proposal{Height: 1, Round: 1})
  132. vb, err := proto.Marshal(&cv)
  133. require.NoError(t, err)
  134. pb, err := proto.Marshal(&p)
  135. require.NoError(t, err)
  136. require.NotEqual(t, vb, pb)
  137. }
  138. func TestVoteVerifySignature(t *testing.T) {
  139. ctx, cancel := context.WithCancel(context.Background())
  140. defer cancel()
  141. privVal := NewMockPV()
  142. pubkey, err := privVal.GetPubKey(ctx)
  143. require.NoError(t, err)
  144. vote := examplePrecommit(t)
  145. v := vote.ToProto()
  146. signBytes := VoteSignBytes("test_chain_id", v)
  147. // sign it
  148. err = privVal.SignVote(ctx, "test_chain_id", v)
  149. require.NoError(t, err)
  150. // verify the same vote
  151. valid := pubkey.VerifySignature(VoteSignBytes("test_chain_id", v), v.Signature)
  152. require.True(t, valid)
  153. // serialize, deserialize and verify again....
  154. precommit := new(tmproto.Vote)
  155. bs, err := proto.Marshal(v)
  156. require.NoError(t, err)
  157. err = proto.Unmarshal(bs, precommit)
  158. require.NoError(t, err)
  159. // verify the transmitted vote
  160. newSignBytes := VoteSignBytes("test_chain_id", precommit)
  161. require.Equal(t, string(signBytes), string(newSignBytes))
  162. valid = pubkey.VerifySignature(newSignBytes, precommit.Signature)
  163. require.True(t, valid)
  164. }
  165. func TestIsVoteTypeValid(t *testing.T) {
  166. tc := []struct {
  167. name string
  168. in tmproto.SignedMsgType
  169. out bool
  170. }{
  171. {"Prevote", tmproto.PrevoteType, true},
  172. {"Precommit", tmproto.PrecommitType, true},
  173. {"InvalidType", tmproto.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. ctx, cancel := context.WithCancel(context.Background())
  186. defer cancel()
  187. privVal := NewMockPV()
  188. pubkey, err := privVal.GetPubKey(ctx)
  189. require.NoError(t, err)
  190. vote := examplePrevote(t)
  191. vote.ValidatorAddress = pubkey.Address()
  192. err = vote.Verify("test_chain_id", ed25519.GenPrivKey().PubKey())
  193. if assert.Error(t, err) {
  194. assert.Equal(t, ErrVoteInvalidValidatorAddress, err)
  195. }
  196. err = vote.Verify("test_chain_id", pubkey)
  197. if assert.Error(t, err) {
  198. assert.Equal(t, ErrVoteInvalidSignature, err)
  199. }
  200. }
  201. func TestVoteString(t *testing.T) {
  202. str := examplePrecommit(t).String()
  203. expected := `Vote{56789:6AF1F4111082 12345/02/SIGNED_MSG_TYPE_PRECOMMIT(Precommit) 8B01023386C3 000000000000 @ 2017-12-25T03:00:01.234Z}`
  204. if str != expected {
  205. t.Errorf("got unexpected string for Vote. Expected:\n%v\nGot:\n%v", expected, str)
  206. }
  207. str2 := examplePrevote(t).String()
  208. expected = `Vote{56789:6AF1F4111082 12345/02/SIGNED_MSG_TYPE_PREVOTE(Prevote) 8B01023386C3 000000000000 @ 2017-12-25T03:00:01.234Z}`
  209. if str2 != expected {
  210. t.Errorf("got unexpected string for Vote. Expected:\n%v\nGot:\n%v", expected, str2)
  211. }
  212. }
  213. func TestVoteValidateBasic(t *testing.T) {
  214. privVal := NewMockPV()
  215. testCases := []struct {
  216. testName string
  217. malleateVote func(*Vote)
  218. expectErr bool
  219. }{
  220. {"Good Vote", func(v *Vote) {}, false},
  221. {"Negative Height", func(v *Vote) { v.Height = -1 }, true},
  222. {"Negative Round", func(v *Vote) { v.Round = -1 }, true},
  223. {"Invalid BlockID", func(v *Vote) {
  224. v.BlockID = BlockID{[]byte{1, 2, 3}, PartSetHeader{111, []byte("blockparts")}}
  225. }, true},
  226. {"Invalid Address", func(v *Vote) { v.ValidatorAddress = make([]byte, 1) }, true},
  227. {"Invalid ValidatorIndex", func(v *Vote) { v.ValidatorIndex = -1 }, true},
  228. {"Invalid Signature", func(v *Vote) { v.Signature = nil }, true},
  229. {"Too big Signature", func(v *Vote) { v.Signature = make([]byte, MaxSignatureSize+1) }, true},
  230. }
  231. for _, tc := range testCases {
  232. tc := tc
  233. t.Run(tc.testName, func(t *testing.T) {
  234. ctx, cancel := context.WithCancel(context.Background())
  235. defer cancel()
  236. vote := examplePrecommit(t)
  237. v := vote.ToProto()
  238. err := privVal.SignVote(ctx, "test_chain_id", v)
  239. vote.Signature = v.Signature
  240. require.NoError(t, err)
  241. tc.malleateVote(vote)
  242. assert.Equal(t, tc.expectErr, vote.ValidateBasic() != nil, "Validate Basic had an unexpected result")
  243. })
  244. }
  245. }
  246. func TestVoteProtobuf(t *testing.T) {
  247. ctx, cancel := context.WithCancel(context.Background())
  248. defer cancel()
  249. privVal := NewMockPV()
  250. vote := examplePrecommit(t)
  251. v := vote.ToProto()
  252. err := privVal.SignVote(ctx, "test_chain_id", v)
  253. vote.Signature = v.Signature
  254. require.NoError(t, err)
  255. testCases := []struct {
  256. msg string
  257. v1 *Vote
  258. expPass bool
  259. }{
  260. {"success", vote, true},
  261. {"fail vote validate basic", &Vote{}, false},
  262. {"failure nil", nil, false},
  263. }
  264. for _, tc := range testCases {
  265. protoProposal := tc.v1.ToProto()
  266. v, err := VoteFromProto(protoProposal)
  267. if tc.expPass {
  268. require.NoError(t, err)
  269. require.Equal(t, tc.v1, v, tc.msg)
  270. } else {
  271. require.Error(t, err)
  272. }
  273. }
  274. }
  275. var sink interface{}
  276. func getSampleCommit(ctx context.Context, t testing.TB) *Commit {
  277. t.Helper()
  278. lastID := makeBlockIDRandom()
  279. voteSet, _, vals := randVoteSet(ctx, t, 2, 1, tmproto.PrecommitType, 10, 1)
  280. commit, err := makeCommit(ctx, lastID, 2, 1, voteSet, vals, time.Now())
  281. require.NoError(t, err)
  282. return commit
  283. }
  284. func BenchmarkVoteSignBytes(b *testing.B) {
  285. protoVote := examplePrecommit(b).ToProto()
  286. b.ReportAllocs()
  287. b.ResetTimer()
  288. for i := 0; i < b.N; i++ {
  289. sink = VoteSignBytes("test_chain_id", protoVote)
  290. }
  291. if sink == nil {
  292. b.Fatal("Benchmark did not run")
  293. }
  294. // Reset the sink.
  295. sink = (interface{})(nil)
  296. }
  297. func BenchmarkCommitVoteSignBytes(b *testing.B) {
  298. ctx, cancel := context.WithCancel(context.Background())
  299. defer cancel()
  300. sampleCommit := getSampleCommit(ctx, b)
  301. b.ReportAllocs()
  302. b.ResetTimer()
  303. for i := 0; i < b.N; i++ {
  304. for index := range sampleCommit.Signatures {
  305. sink = sampleCommit.VoteSignBytes("test_chain_id", int32(index))
  306. }
  307. }
  308. if sink == nil {
  309. b.Fatal("Benchmark did not run")
  310. }
  311. // Reset the sink.
  312. sink = (interface{})(nil)
  313. }