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
8.3 KiB

  1. package types
  2. import (
  3. "context"
  4. "math"
  5. "testing"
  6. "time"
  7. "github.com/gogo/protobuf/proto"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/stretchr/testify/require"
  10. "github.com/tendermint/tendermint/crypto/tmhash"
  11. "github.com/tendermint/tendermint/internal/libs/protoio"
  12. tmrand "github.com/tendermint/tendermint/libs/rand"
  13. tmtime "github.com/tendermint/tendermint/libs/time"
  14. tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
  15. )
  16. func getTestProposal(t testing.TB) *Proposal {
  17. t.Helper()
  18. stamp, err := time.Parse(TimeFormat, "2018-02-11T07:09:22.765Z")
  19. require.NoError(t, err)
  20. return &Proposal{
  21. Height: 12345,
  22. Round: 23456,
  23. BlockID: BlockID{Hash: []byte("--June_15_2020_amino_was_removed"),
  24. PartSetHeader: PartSetHeader{Total: 111, Hash: []byte("--June_15_2020_amino_was_removed")}},
  25. POLRound: -1,
  26. Timestamp: stamp,
  27. }
  28. }
  29. func TestProposalSignable(t *testing.T) {
  30. chainID := "test_chain_id"
  31. signBytes := ProposalSignBytes(chainID, getTestProposal(t).ToProto())
  32. pb := CanonicalizeProposal(chainID, getTestProposal(t).ToProto())
  33. expected, err := protoio.MarshalDelimited(&pb)
  34. require.NoError(t, err)
  35. require.Equal(t, expected, signBytes, "Got unexpected sign bytes for Proposal")
  36. }
  37. func TestProposalString(t *testing.T) {
  38. str := getTestProposal(t).String()
  39. expected := `Proposal{12345/23456 (2D2D4A756E655F31355F323032305F616D696E6F5F7761735F72656D6F766564:111:2D2D4A756E65, -1) 000000000000 @ 2018-02-11T07:09:22.765Z}`
  40. if str != expected {
  41. t.Errorf("got unexpected string for Proposal. Expected:\n%v\nGot:\n%v", expected, str)
  42. }
  43. }
  44. func TestProposalVerifySignature(t *testing.T) {
  45. ctx, cancel := context.WithCancel(context.Background())
  46. defer cancel()
  47. privVal := NewMockPV()
  48. pubKey, err := privVal.GetPubKey(ctx)
  49. require.NoError(t, err)
  50. prop := NewProposal(
  51. 4, 2, 2,
  52. BlockID{tmrand.Bytes(tmhash.Size), PartSetHeader{777, tmrand.Bytes(tmhash.Size)}}, tmtime.Now())
  53. p := prop.ToProto()
  54. signBytes := ProposalSignBytes("test_chain_id", p)
  55. // sign it
  56. err = privVal.SignProposal(ctx, "test_chain_id", p)
  57. require.NoError(t, err)
  58. prop.Signature = p.Signature
  59. // verify the same proposal
  60. valid := pubKey.VerifySignature(signBytes, prop.Signature)
  61. require.True(t, valid)
  62. // serialize, deserialize and verify again....
  63. newProp := new(tmproto.Proposal)
  64. pb := prop.ToProto()
  65. bs, err := proto.Marshal(pb)
  66. require.NoError(t, err)
  67. err = proto.Unmarshal(bs, newProp)
  68. require.NoError(t, err)
  69. np, err := ProposalFromProto(newProp)
  70. require.NoError(t, err)
  71. // verify the transmitted proposal
  72. newSignBytes := ProposalSignBytes("test_chain_id", pb)
  73. require.Equal(t, string(signBytes), string(newSignBytes))
  74. valid = pubKey.VerifySignature(newSignBytes, np.Signature)
  75. require.True(t, valid)
  76. }
  77. func BenchmarkProposalWriteSignBytes(b *testing.B) {
  78. pbp := getTestProposal(b).ToProto()
  79. b.ResetTimer()
  80. for i := 0; i < b.N; i++ {
  81. ProposalSignBytes("test_chain_id", pbp)
  82. }
  83. }
  84. func BenchmarkProposalSign(b *testing.B) {
  85. ctx, cancel := context.WithCancel(context.Background())
  86. defer cancel()
  87. privVal := NewMockPV()
  88. pbp := getTestProposal(b).ToProto()
  89. b.ResetTimer()
  90. for i := 0; i < b.N; i++ {
  91. err := privVal.SignProposal(ctx, "test_chain_id", pbp)
  92. if err != nil {
  93. b.Error(err)
  94. }
  95. }
  96. }
  97. func BenchmarkProposalVerifySignature(b *testing.B) {
  98. testProposal := getTestProposal(b)
  99. pbp := testProposal.ToProto()
  100. ctx, cancel := context.WithCancel(context.Background())
  101. defer cancel()
  102. privVal := NewMockPV()
  103. err := privVal.SignProposal(ctx, "test_chain_id", pbp)
  104. require.NoError(b, err)
  105. pubKey, err := privVal.GetPubKey(ctx)
  106. require.NoError(b, err)
  107. b.ResetTimer()
  108. for i := 0; i < b.N; i++ {
  109. pubKey.VerifySignature(ProposalSignBytes("test_chain_id", pbp), testProposal.Signature)
  110. }
  111. }
  112. func TestProposalValidateBasic(t *testing.T) {
  113. privVal := NewMockPV()
  114. testCases := []struct {
  115. testName string
  116. malleateProposal func(*Proposal)
  117. expectErr bool
  118. }{
  119. {"Good Proposal", func(p *Proposal) {}, false},
  120. {"Invalid Type", func(p *Proposal) { p.Type = tmproto.PrecommitType }, true},
  121. {"Invalid Height", func(p *Proposal) { p.Height = -1 }, true},
  122. {"Invalid Round", func(p *Proposal) { p.Round = -1 }, true},
  123. {"Invalid POLRound", func(p *Proposal) { p.POLRound = -2 }, true},
  124. {"Invalid BlockId", func(p *Proposal) {
  125. p.BlockID = BlockID{[]byte{1, 2, 3}, PartSetHeader{111, []byte("blockparts")}}
  126. }, true},
  127. {"Invalid Signature", func(p *Proposal) {
  128. p.Signature = make([]byte, 0)
  129. }, true},
  130. {"Too big Signature", func(p *Proposal) {
  131. p.Signature = make([]byte, MaxSignatureSize+1)
  132. }, true},
  133. }
  134. blockID := makeBlockID(tmhash.Sum([]byte("blockhash")), math.MaxInt32, tmhash.Sum([]byte("partshash")))
  135. for _, tc := range testCases {
  136. tc := tc
  137. t.Run(tc.testName, func(t *testing.T) {
  138. ctx, cancel := context.WithCancel(context.Background())
  139. defer cancel()
  140. prop := NewProposal(
  141. 4, 2, 2,
  142. blockID, tmtime.Now())
  143. p := prop.ToProto()
  144. err := privVal.SignProposal(ctx, "test_chain_id", p)
  145. prop.Signature = p.Signature
  146. require.NoError(t, err)
  147. tc.malleateProposal(prop)
  148. assert.Equal(t, tc.expectErr, prop.ValidateBasic() != nil, "Validate Basic had an unexpected result")
  149. })
  150. }
  151. }
  152. func TestProposalProtoBuf(t *testing.T) {
  153. proposal := NewProposal(1, 2, 3, makeBlockID([]byte("hash"), 2, []byte("part_set_hash")), tmtime.Now())
  154. proposal.Signature = []byte("sig")
  155. proposal2 := NewProposal(1, 2, 3, BlockID{}, tmtime.Now())
  156. testCases := []struct {
  157. msg string
  158. p1 *Proposal
  159. expPass bool
  160. }{
  161. {"success", proposal, true},
  162. {"success", proposal2, false}, // blcokID cannot be empty
  163. {"empty proposal failure validatebasic", &Proposal{}, false},
  164. {"nil proposal", nil, false},
  165. }
  166. for _, tc := range testCases {
  167. protoProposal := tc.p1.ToProto()
  168. p, err := ProposalFromProto(protoProposal)
  169. if tc.expPass {
  170. require.NoError(t, err)
  171. require.Equal(t, tc.p1, p, tc.msg)
  172. } else {
  173. require.Error(t, err)
  174. }
  175. }
  176. }
  177. func TestIsTimely(t *testing.T) {
  178. genesisTime, err := time.Parse(time.RFC3339, "2019-03-13T23:00:00Z")
  179. require.NoError(t, err)
  180. testCases := []struct {
  181. name string
  182. proposalTime time.Time
  183. recvTime time.Time
  184. precision time.Duration
  185. msgDelay time.Duration
  186. expectTimely bool
  187. round int32
  188. }{
  189. // proposalTime - precision <= localTime <= proposalTime + msgDelay + precision
  190. {
  191. // Checking that the following inequality evaluates to true:
  192. // 0 - 2 <= 1 <= 0 + 1 + 2
  193. name: "basic timely",
  194. proposalTime: genesisTime,
  195. recvTime: genesisTime.Add(1 * time.Nanosecond),
  196. precision: time.Nanosecond * 2,
  197. msgDelay: time.Nanosecond,
  198. expectTimely: true,
  199. },
  200. {
  201. // Checking that the following inequality evaluates to false:
  202. // 0 - 2 <= 4 <= 0 + 1 + 2
  203. name: "local time too large",
  204. proposalTime: genesisTime,
  205. recvTime: genesisTime.Add(4 * time.Nanosecond),
  206. precision: time.Nanosecond * 2,
  207. msgDelay: time.Nanosecond,
  208. expectTimely: false,
  209. },
  210. {
  211. // Checking that the following inequality evaluates to false:
  212. // 4 - 2 <= 0 <= 4 + 2 + 1
  213. name: "proposal time too large",
  214. proposalTime: genesisTime.Add(4 * time.Nanosecond),
  215. recvTime: genesisTime,
  216. precision: time.Nanosecond * 2,
  217. msgDelay: time.Nanosecond,
  218. expectTimely: false,
  219. },
  220. {
  221. // Checking that the following inequality evaluates to true:
  222. // 0 - (2 * 2) <= 4 <= 0 + (1 * 2) + 2
  223. name: "message delay adapts after 10 rounds",
  224. proposalTime: genesisTime,
  225. recvTime: genesisTime.Add(4 * time.Nanosecond),
  226. precision: time.Nanosecond * 2,
  227. msgDelay: time.Nanosecond,
  228. expectTimely: true,
  229. round: 10,
  230. },
  231. {
  232. // check that values that overflow time.Duration still correctly register
  233. // as timely when round relaxation applied.
  234. name: "message delay fixed to not overflow time.Duration",
  235. proposalTime: genesisTime,
  236. recvTime: genesisTime.Add(4 * time.Nanosecond),
  237. precision: time.Nanosecond * 2,
  238. msgDelay: time.Nanosecond,
  239. expectTimely: true,
  240. round: 5000,
  241. },
  242. }
  243. for _, testCase := range testCases {
  244. t.Run(testCase.name, func(t *testing.T) {
  245. p := Proposal{
  246. Timestamp: testCase.proposalTime,
  247. }
  248. sp := SynchronyParams{
  249. Precision: testCase.precision,
  250. MessageDelay: testCase.msgDelay,
  251. }
  252. ti := p.IsTimely(testCase.recvTime, sp, testCase.round)
  253. assert.Equal(t, testCase.expectTimely, ti)
  254. })
  255. }
  256. }