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.

102 lines
2.8 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. signBytes := testProposal.SignBytes("test_chain_id")
  23. signStr := string(signBytes)
  24. expected := `{"@chain_id":"test_chain_id","@type":"proposal","block_parts_header":{"hash":"626C6F636B7061727473","total":"111"},"height":"12345","pol_block_id":{},"pol_round":"-1","round":"23456","timestamp":"2018-02-11T07:09:22.765Z"}`
  25. if signStr != expected {
  26. t.Errorf("Got unexpected sign string for Proposal. Expected:\n%v\nGot:\n%v", expected, signStr)
  27. }
  28. if signStr != expected {
  29. t.Errorf("Got unexpected sign string for Proposal. Expected:\n%v\nGot:\n%v", expected, signStr)
  30. }
  31. }
  32. func TestProposalString(t *testing.T) {
  33. str := testProposal.String()
  34. expected := `Proposal{12345/23456 111:626C6F636B70 (-1,:0:000000000000) 000000000000 @ 2018-02-11T07:09:22.765Z}`
  35. if str != expected {
  36. t.Errorf("Got unexpected string for Proposal. Expected:\n%v\nGot:\n%v", expected, str)
  37. }
  38. }
  39. func TestProposalVerifySignature(t *testing.T) {
  40. privVal := NewMockPV()
  41. pubKey := privVal.GetPubKey()
  42. prop := NewProposal(4, 2, PartSetHeader{777, []byte("proper")}, 2, BlockID{})
  43. signBytes := prop.SignBytes("test_chain_id")
  44. // sign it
  45. err := privVal.SignProposal("test_chain_id", prop)
  46. require.NoError(t, err)
  47. // verify the same proposal
  48. valid := pubKey.VerifyBytes(signBytes, prop.Signature)
  49. require.True(t, valid)
  50. // serialize, deserialize and verify again....
  51. newProp := new(Proposal)
  52. bs, err := cdc.MarshalBinary(prop)
  53. require.NoError(t, err)
  54. err = cdc.UnmarshalBinary(bs, &newProp)
  55. require.NoError(t, err)
  56. // verify the transmitted proposal
  57. newSignBytes := newProp.SignBytes("test_chain_id")
  58. require.Equal(t, string(signBytes), string(newSignBytes))
  59. valid = pubKey.VerifyBytes(newSignBytes, newProp.Signature)
  60. require.True(t, valid)
  61. }
  62. func BenchmarkProposalWriteSignBytes(b *testing.B) {
  63. for i := 0; i < b.N; i++ {
  64. testProposal.SignBytes("test_chain_id")
  65. }
  66. }
  67. func BenchmarkProposalSign(b *testing.B) {
  68. privVal := NewMockPV()
  69. for i := 0; i < b.N; i++ {
  70. err := privVal.SignProposal("test_chain_id", testProposal)
  71. if err != nil {
  72. b.Error(err)
  73. }
  74. }
  75. }
  76. func BenchmarkProposalVerifySignature(b *testing.B) {
  77. privVal := NewMockPV()
  78. err := privVal.SignProposal("test_chain_id", testProposal)
  79. require.Nil(b, err)
  80. pubKey := privVal.GetPubKey()
  81. for i := 0; i < b.N; i++ {
  82. pubKey.VerifyBytes(testProposal.SignBytes("test_chain_id"), testProposal.Signature)
  83. }
  84. }