2015-08-10 20:38:45 -07:00
|
|
|
package types
|
2015-04-27 20:55:28 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2017-12-11 09:15:39 -08:00
|
|
|
"time"
|
2015-04-27 20:55:28 -07:00
|
|
|
)
|
|
|
|
|
2017-12-11 09:15:39 -08: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) {
|
2016-12-17 10:57:37 -08:00
|
|
|
signBytes := SignBytes("test_chain_id", testProposal)
|
2015-04-27 20:55:28 -07:00
|
|
|
signStr := string(signBytes)
|
2015-06-24 14:04:40 -07:00
|
|
|
|
2017-12-11 09:15:39 -08:00
|
|
|
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"}}`
|
2015-04-27 20:55:28 -07:00
|
|
|
if signStr != expected {
|
2016-08-20 15:08:26 -07:00
|
|
|
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 BenchmarkProposalWriteSignBytes(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
SignBytes("test_chain_id", testProposal)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkProposalSign(b *testing.B) {
|
2017-09-18 20:16:14 -07:00
|
|
|
privVal := GenPrivValidatorFS("")
|
2016-12-17 10:57:37 -08:00
|
|
|
for i := 0; i < b.N; i++ {
|
2017-09-21 08:42:44 -07:00
|
|
|
_, err := privVal.Signer.Sign(SignBytes("test_chain_id", testProposal))
|
|
|
|
if err != nil {
|
|
|
|
b.Error(err)
|
|
|
|
}
|
2016-12-17 10:57:37 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkProposalVerifySignature(b *testing.B) {
|
|
|
|
signBytes := SignBytes("test_chain_id", testProposal)
|
2017-09-18 20:16:14 -07:00
|
|
|
privVal := GenPrivValidatorFS("")
|
2017-09-18 15:12:31 -07:00
|
|
|
signature, _ := privVal.Signer.Sign(signBytes)
|
2017-09-21 13:32:02 -07:00
|
|
|
pubKey := privVal.GetPubKey()
|
2016-12-17 10:57:37 -08:00
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
pubKey.VerifyBytes(SignBytes("test_chain_id", testProposal), signature)
|
|
|
|
}
|
|
|
|
}
|