Add test to isolate precommit failure

types/vote_test.go now checks signature on a serialized and
then deserialized vote. Turns out go-wire time encoding doesn't
respect timezones, and the signatures don't check out.
This commit is contained in:
Ethan Frey 2017-12-11 19:42:29 +01:00
parent a29c781295
commit 850310b034
1 changed files with 38 additions and 3 deletions

View File

@ -3,15 +3,18 @@ package types
import (
"testing"
"time"
"github.com/stretchr/testify/require"
wire "github.com/tendermint/go-wire"
)
func TestVoteSignable(t *testing.T) {
func exampleVote() *Vote {
var stamp, err = time.Parse(timeFormat, "2017-12-25T03:00:01.234Z")
if err != nil {
t.Fatal(err)
panic(err)
}
vote := &Vote{
return &Vote{
ValidatorAddress: []byte("addr"),
ValidatorIndex: 56789,
Height: 12345,
@ -26,6 +29,10 @@ func TestVoteSignable(t *testing.T) {
},
},
}
}
func TestVoteSignable(t *testing.T) {
vote := exampleVote()
signBytes := SignBytes("test_chain_id", vote)
signStr := string(signBytes)
@ -35,3 +42,31 @@ func TestVoteSignable(t *testing.T) {
t.Errorf("Got unexpected sign string for Vote. Expected:\n%v\nGot:\n%v", expected, signStr)
}
}
func TestVoteVerifySignature(t *testing.T) {
privVal := GenPrivValidatorFS("")
pubKey := privVal.GetPubKey()
vote := exampleVote()
signBytes := SignBytes("test_chain_id", vote)
// sign it
signature, err := privVal.Signer.Sign(signBytes)
require.NoError(t, err)
// verify the same vote
valid := pubKey.VerifyBytes(SignBytes("test_chain_id", vote), signature)
require.True(t, valid)
// serialize, deserialize and verify again....
precommit := new(Vote)
bs := wire.BinaryBytes(vote)
err = wire.ReadBinaryBytes(bs, &precommit)
require.NoError(t, err)
// verify the transmitted vote
newSignBytes := SignBytes("test_chain_id", precommit)
require.Equal(t, string(signBytes), string(newSignBytes))
valid = pubKey.VerifyBytes(newSignBytes, signature)
require.True(t, valid)
}