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.

97 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. BlockPartsHeader: 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.MarshalBinary(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 111:626C6F636B70 (-1,:0:000000000000) 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(4, 2, PartSetHeader{777, []byte("proper")}, 2, BlockID{})
  39. signBytes := prop.SignBytes("test_chain_id")
  40. // sign it
  41. err := privVal.SignProposal("test_chain_id", prop)
  42. require.NoError(t, err)
  43. // verify the same proposal
  44. valid := pubKey.VerifyBytes(signBytes, prop.Signature)
  45. require.True(t, valid)
  46. // serialize, deserialize and verify again....
  47. newProp := new(Proposal)
  48. bs, err := cdc.MarshalBinary(prop)
  49. require.NoError(t, err)
  50. err = cdc.UnmarshalBinary(bs, &newProp)
  51. require.NoError(t, err)
  52. // verify the transmitted proposal
  53. newSignBytes := newProp.SignBytes("test_chain_id")
  54. require.Equal(t, string(signBytes), string(newSignBytes))
  55. valid = pubKey.VerifyBytes(newSignBytes, newProp.Signature)
  56. require.True(t, valid)
  57. }
  58. func BenchmarkProposalWriteSignBytes(b *testing.B) {
  59. for i := 0; i < b.N; i++ {
  60. testProposal.SignBytes("test_chain_id")
  61. }
  62. }
  63. func BenchmarkProposalSign(b *testing.B) {
  64. privVal := NewMockPV()
  65. for i := 0; i < b.N; i++ {
  66. err := privVal.SignProposal("test_chain_id", testProposal)
  67. if err != nil {
  68. b.Error(err)
  69. }
  70. }
  71. }
  72. func BenchmarkProposalVerifySignature(b *testing.B) {
  73. privVal := NewMockPV()
  74. err := privVal.SignProposal("test_chain_id", testProposal)
  75. require.Nil(b, err)
  76. pubKey := privVal.GetPubKey()
  77. for i := 0; i < b.N; i++ {
  78. pubKey.VerifyBytes(testProposal.SignBytes("test_chain_id"), testProposal.Signature)
  79. }
  80. }