tendermint/types/proposal_test.go

99 lines
2.7 KiB
Go
Raw Normal View History

package types
2015-04-27 20:55:28 -07:00
import (
"testing"
"time"
"github.com/stretchr/testify/require"
2015-04-27 20:55:28 -07:00
)
var testProposal *Proposal
func init() {
var stamp, err = time.Parse(TimeFormat, "2018-02-11T07:09:22.765Z")
if err != nil {
panic(err)
}
testProposal = &Proposal{
Height: 12345,
Round: 23456,
BlockPartsHeader: PartSetHeader{111, []byte("blockparts")},
POLRound: -1,
Timestamp: stamp,
}
2016-12-17 10:57:37 -08:00
}
2015-04-27 20:55:28 -07:00
func TestProposalSignable(t *testing.T) {
2018-01-14 18:30:40 -08:00
signBytes := testProposal.SignBytes("test_chain_id")
2015-04-27 20:55:28 -07:00
signStr := string(signBytes)
2015-06-24 14:04:40 -07:00
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"}`
2015-04-27 20:55:28 -07:00
if signStr != expected {
t.Errorf("Got unexpected sign string for Proposal. Expected:\n%v\nGot:\n%v", expected, signStr)
2015-04-27 20:55:28 -07:00
}
}
2016-12-17 10:57:37 -08:00
func TestProposalString(t *testing.T) {
str := testProposal.String()
expected := `Proposal{12345/23456 111:626C6F636B70 (-1,:0:000000000000) <nil> @ 2018-02-11T07:09:22.765Z}`
if str != expected {
t.Errorf("Got unexpected string for Proposal. Expected:\n%v\nGot:\n%v", expected, str)
}
}
func TestProposalVerifySignature(t *testing.T) {
privVal := NewMockPV()
pubKey := privVal.GetPubKey()
prop := NewProposal(4, 2, PartSetHeader{777, []byte("proper")}, 2, BlockID{})
2018-01-14 18:30:40 -08:00
signBytes := prop.SignBytes("test_chain_id")
// sign it
err := privVal.SignProposal("test_chain_id", prop)
require.NoError(t, err)
// verify the same proposal
valid := pubKey.VerifyBytes(signBytes, prop.Signature)
require.True(t, valid)
// serialize, deserialize and verify again....
newProp := new(Proposal)
bs, err := cdc.MarshalBinary(prop)
2018-01-14 18:30:40 -08:00
require.NoError(t, err)
err = cdc.UnmarshalBinary(bs, &newProp)
require.NoError(t, err)
// verify the transmitted proposal
2018-01-14 18:30:40 -08:00
newSignBytes := newProp.SignBytes("test_chain_id")
require.Equal(t, string(signBytes), string(newSignBytes))
valid = pubKey.VerifyBytes(newSignBytes, newProp.Signature)
require.True(t, valid)
}
2016-12-17 10:57:37 -08:00
func BenchmarkProposalWriteSignBytes(b *testing.B) {
for i := 0; i < b.N; i++ {
2018-01-14 18:30:40 -08:00
testProposal.SignBytes("test_chain_id")
2016-12-17 10:57:37 -08:00
}
}
func BenchmarkProposalSign(b *testing.B) {
privVal := NewMockPV()
2016-12-17 10:57:37 -08:00
for i := 0; i < b.N; i++ {
err := privVal.SignProposal("test_chain_id", testProposal)
2017-09-21 08:42:44 -07:00
if err != nil {
b.Error(err)
}
2016-12-17 10:57:37 -08:00
}
}
func BenchmarkProposalVerifySignature(b *testing.B) {
privVal := NewMockPV()
err := privVal.SignProposal("test_chain_id", testProposal)
require.Nil(b, err)
pubKey := privVal.GetPubKey()
2016-12-17 10:57:37 -08:00
for i := 0; i < b.N; i++ {
pubKey.VerifyBytes(testProposal.SignBytes("test_chain_id"), testProposal.Signature)
2016-12-17 10:57:37 -08:00
}
}