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.

100 lines
2.8 KiB

6 years ago
9 years ago
7 years ago
7 years ago
  1. package types
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/stretchr/testify/require"
  6. wire "github.com/tendermint/tendermint/wire"
  7. )
  8. var testProposal *Proposal
  9. func init() {
  10. var stamp, err = time.Parse(TimeFormat, "2018-02-11T07:09:22.765Z")
  11. if err != nil {
  12. panic(err)
  13. }
  14. testProposal = &Proposal{
  15. Height: 12345,
  16. Round: 23456,
  17. BlockPartsHeader: PartSetHeader{111, []byte("blockparts")},
  18. POLRound: -1,
  19. Timestamp: stamp,
  20. }
  21. }
  22. func TestProposalSignable(t *testing.T) {
  23. signBytes := testProposal.SignBytes("test_chain_id")
  24. signStr := string(signBytes)
  25. expected := `{"chain_id":"test_chain_id","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"}}`
  26. if signStr != expected {
  27. t.Errorf("Got unexpected sign string for Proposal. Expected:\n%v\nGot:\n%v", expected, signStr)
  28. }
  29. }
  30. func TestProposalString(t *testing.T) {
  31. str := testProposal.String()
  32. expected := `Proposal{12345/23456 111:626C6F636B70 (-1,:0:000000000000) {<nil>} @ 2018-02-11T07:09:22.765Z}`
  33. if str != expected {
  34. t.Errorf("Got unexpected string for Proposal. Expected:\n%v\nGot:\n%v", expected, str)
  35. }
  36. }
  37. func TestProposalVerifySignature(t *testing.T) {
  38. privVal := GenPrivValidatorFS("")
  39. pubKey := privVal.GetPubKey()
  40. prop := NewProposal(4, 2, PartSetHeader{777, []byte("proper")}, 2, BlockID{})
  41. signBytes := prop.SignBytes("test_chain_id")
  42. // sign it
  43. signature, err := privVal.Signer.Sign(signBytes)
  44. require.NoError(t, err)
  45. // verify the same proposal
  46. valid := pubKey.VerifyBytes(prop.SignBytes("test_chain_id"), signature)
  47. require.True(t, valid)
  48. // serialize, deserialize and verify again....
  49. newProp := new(Proposal)
  50. bs, err := wire.MarshalBinary(prop)
  51. require.NoError(t, err)
  52. err = wire.UnmarshalBinary(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, 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 := GenPrivValidatorFS("")
  67. for i := 0; i < b.N; i++ {
  68. _, err := privVal.Signer.Sign(testProposal.SignBytes("test_chain_id"))
  69. if err != nil {
  70. b.Error(err)
  71. }
  72. }
  73. }
  74. func BenchmarkProposalVerifySignature(b *testing.B) {
  75. signBytes := testProposal.SignBytes("test_chain_id")
  76. privVal := GenPrivValidatorFS("")
  77. signature, _ := privVal.Signer.Sign(signBytes)
  78. pubKey := privVal.GetPubKey()
  79. for i := 0; i < b.N; i++ {
  80. pubKey.VerifyBytes(testProposal.SignBytes("test_chain_id"), signature)
  81. }
  82. }