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

10 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/go-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 := SignBytes("test_chain_id", testProposal)
  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 := SignBytes("test_chain_id", prop)
  42. // sign it
  43. signature, err := privVal.Signer.Sign(signBytes)
  44. require.NoError(t, err)
  45. // verify the same proposal
  46. valid := pubKey.VerifyBytes(SignBytes("test_chain_id", prop), signature)
  47. require.True(t, valid)
  48. // serialize, deserialize and verify again....
  49. newProp := new(Proposal)
  50. bs := wire.BinaryBytes(prop)
  51. err = wire.ReadBinaryBytes(bs, &newProp)
  52. require.NoError(t, err)
  53. // verify the transmitted proposal
  54. newSignBytes := SignBytes("test_chain_id", newProp)
  55. require.Equal(t, string(signBytes), string(newSignBytes))
  56. valid = pubKey.VerifyBytes(newSignBytes, signature)
  57. require.True(t, valid)
  58. }
  59. func BenchmarkProposalWriteSignBytes(b *testing.B) {
  60. for i := 0; i < b.N; i++ {
  61. SignBytes("test_chain_id", testProposal)
  62. }
  63. }
  64. func BenchmarkProposalSign(b *testing.B) {
  65. privVal := GenPrivValidatorFS("")
  66. for i := 0; i < b.N; i++ {
  67. _, err := privVal.Signer.Sign(SignBytes("test_chain_id", testProposal))
  68. if err != nil {
  69. b.Error(err)
  70. }
  71. }
  72. }
  73. func BenchmarkProposalVerifySignature(b *testing.B) {
  74. signBytes := SignBytes("test_chain_id", testProposal)
  75. privVal := GenPrivValidatorFS("")
  76. signature, _ := privVal.Signer.Sign(signBytes)
  77. pubKey := privVal.GetPubKey()
  78. for i := 0; i < b.N; i++ {
  79. pubKey.VerifyBytes(SignBytes("test_chain_id", testProposal), signature)
  80. }
  81. }