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.

99 lines
2.5 KiB

  1. package types
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/stretchr/testify/require"
  6. )
  7. var testProposal *Proposal
  8. func init() {
  9. var stamp, err = time.Parse(TimeFormat, "2018-02-11T07:09:22.765Z")
  10. if err != nil {
  11. panic(err)
  12. }
  13. testProposal = &Proposal{
  14. Height: 12345,
  15. Round: 23456,
  16. BlockID: BlockID{[]byte{1, 2, 3}, PartSetHeader{111, []byte("blockparts")}},
  17. POLRound: -1,
  18. Timestamp: stamp,
  19. }
  20. }
  21. func TestProposalSignable(t *testing.T) {
  22. chainID := "test_chain_id"
  23. signBytes := testProposal.SignBytes(chainID)
  24. expected, err := cdc.MarshalBinaryLengthPrefixed(CanonicalizeProposal(chainID, testProposal))
  25. require.NoError(t, err)
  26. require.Equal(t, expected, signBytes, "Got unexpected sign bytes for Proposal")
  27. }
  28. func TestProposalString(t *testing.T) {
  29. str := testProposal.String()
  30. expected := `Proposal{12345/23456 (010203:111:626C6F636B70, -1) 000000000000 @ 2018-02-11T07:09:22.765Z}`
  31. if str != expected {
  32. t.Errorf("Got unexpected string for Proposal. Expected:\n%v\nGot:\n%v", expected, str)
  33. }
  34. }
  35. func TestProposalVerifySignature(t *testing.T) {
  36. privVal := NewMockPV()
  37. pubKey := privVal.GetPubKey()
  38. prop := NewProposal(
  39. 4, 2, 2,
  40. BlockID{[]byte{1, 2, 3}, PartSetHeader{777, []byte("proper")}})
  41. signBytes := prop.SignBytes("test_chain_id")
  42. // sign it
  43. err := privVal.SignProposal("test_chain_id", prop)
  44. require.NoError(t, err)
  45. // verify the same proposal
  46. valid := pubKey.VerifyBytes(signBytes, prop.Signature)
  47. require.True(t, valid)
  48. // serialize, deserialize and verify again....
  49. newProp := new(Proposal)
  50. bs, err := cdc.MarshalBinaryLengthPrefixed(prop)
  51. require.NoError(t, err)
  52. err = cdc.UnmarshalBinaryLengthPrefixed(bs, &newProp)
  53. require.NoError(t, err)
  54. // verify the transmitted proposal
  55. newSignBytes := newProp.SignBytes("test_chain_id")
  56. require.Equal(t, string(signBytes), string(newSignBytes))
  57. valid = pubKey.VerifyBytes(newSignBytes, newProp.Signature)
  58. require.True(t, valid)
  59. }
  60. func BenchmarkProposalWriteSignBytes(b *testing.B) {
  61. for i := 0; i < b.N; i++ {
  62. testProposal.SignBytes("test_chain_id")
  63. }
  64. }
  65. func BenchmarkProposalSign(b *testing.B) {
  66. privVal := NewMockPV()
  67. for i := 0; i < b.N; i++ {
  68. err := privVal.SignProposal("test_chain_id", testProposal)
  69. if err != nil {
  70. b.Error(err)
  71. }
  72. }
  73. }
  74. func BenchmarkProposalVerifySignature(b *testing.B) {
  75. privVal := NewMockPV()
  76. err := privVal.SignProposal("test_chain_id", testProposal)
  77. require.Nil(b, err)
  78. pubKey := privVal.GetPubKey()
  79. for i := 0; i < b.N; i++ {
  80. pubKey.VerifyBytes(testProposal.SignBytes("test_chain_id"), testProposal.Signature)
  81. }
  82. }