fix test-vectors to actually match the sign bytes: (#3312)

- sign bytes are length prefixed
 - change test to use SignBytes methods instead of calling amino methods
 directly

Resolves #3311
ref: #2455 #2508
This commit is contained in:
Ismail Khoffi 2019-02-16 06:12:00 +01:00 committed by Anton Kaliaev
parent cf737ec85c
commit af3ba5145a

View File

@ -67,23 +67,23 @@ func TestVoteSignable(t *testing.T) {
require.Equal(t, expected, signBytes, "Got unexpected sign bytes for Vote.") require.Equal(t, expected, signBytes, "Got unexpected sign bytes for Vote.")
} }
func TestVoteSignableTestVectors(t *testing.T) { func TestVoteSignBytesTestVectors(t *testing.T) {
vote := CanonicalizeVote("", &Vote{Height: 1, Round: 1})
tests := []struct { tests := []struct {
canonicalVote CanonicalVote chainID string
vote *Vote
want []byte want []byte
}{ }{
{ 0: {
CanonicalizeVote("", &Vote{}), "", &Vote{},
// NOTE: Height and Round are skipped here. This case needs to be considered while parsing. // NOTE: Height and Round are skipped here. This case needs to be considered while parsing.
// []byte{0x2a, 0x9, 0x9, 0x0, 0x9, 0x6e, 0x88, 0xf1, 0xff, 0xff, 0xff}, []byte{0xd, 0x2a, 0xb, 0x8, 0x80, 0x92, 0xb8, 0xc3, 0x98, 0xfe, 0xff, 0xff, 0xff, 0x1},
[]byte{0x2a, 0xb, 0x8, 0x80, 0x92, 0xb8, 0xc3, 0x98, 0xfe, 0xff, 0xff, 0xff, 0x1},
}, },
// with proper (fixed size) height and round (PreCommit): // with proper (fixed size) height and round (PreCommit):
{ 1: {
CanonicalizeVote("", &Vote{Height: 1, Round: 1, Type: PrecommitType}), "", &Vote{Height: 1, Round: 1, Type: PrecommitType},
[]byte{ []byte{
0x21, // length
0x8, // (field_number << 3) | wire_type 0x8, // (field_number << 3) | wire_type
0x2, // PrecommitType 0x2, // PrecommitType
0x11, // (field_number << 3) | wire_type 0x11, // (field_number << 3) | wire_type
@ -95,9 +95,10 @@ func TestVoteSignableTestVectors(t *testing.T) {
0xb, 0x8, 0x80, 0x92, 0xb8, 0xc3, 0x98, 0xfe, 0xff, 0xff, 0xff, 0x1}, 0xb, 0x8, 0x80, 0x92, 0xb8, 0xc3, 0x98, 0xfe, 0xff, 0xff, 0xff, 0x1},
}, },
// with proper (fixed size) height and round (PreVote): // with proper (fixed size) height and round (PreVote):
{ 2: {
CanonicalizeVote("", &Vote{Height: 1, Round: 1, Type: PrevoteType}), "", &Vote{Height: 1, Round: 1, Type: PrevoteType},
[]byte{ []byte{
0x21, // length
0x8, // (field_number << 3) | wire_type 0x8, // (field_number << 3) | wire_type
0x1, // PrevoteType 0x1, // PrevoteType
0x11, // (field_number << 3) | wire_type 0x11, // (field_number << 3) | wire_type
@ -108,9 +109,10 @@ func TestVoteSignableTestVectors(t *testing.T) {
// remaining fields (timestamp): // remaining fields (timestamp):
0xb, 0x8, 0x80, 0x92, 0xb8, 0xc3, 0x98, 0xfe, 0xff, 0xff, 0xff, 0x1}, 0xb, 0x8, 0x80, 0x92, 0xb8, 0xc3, 0x98, 0xfe, 0xff, 0xff, 0xff, 0x1},
}, },
{ 3: {
vote, "", &Vote{Height: 1, Round: 1},
[]byte{ []byte{
0x1f, // length
0x11, // (field_number << 3) | wire_type 0x11, // (field_number << 3) | wire_type
0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // height 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // height
0x19, // (field_number << 3) | wire_type 0x19, // (field_number << 3) | wire_type
@ -120,9 +122,10 @@ func TestVoteSignableTestVectors(t *testing.T) {
0xb, 0x8, 0x80, 0x92, 0xb8, 0xc3, 0x98, 0xfe, 0xff, 0xff, 0xff, 0x1}, 0xb, 0x8, 0x80, 0x92, 0xb8, 0xc3, 0x98, 0xfe, 0xff, 0xff, 0xff, 0x1},
}, },
// containing non-empty chain_id: // containing non-empty chain_id:
{ 4: {
CanonicalizeVote("test_chain_id", &Vote{Height: 1, Round: 1}), "test_chain_id", &Vote{Height: 1, Round: 1},
[]byte{ []byte{
0x2e, // length
0x11, // (field_number << 3) | wire_type 0x11, // (field_number << 3) | wire_type
0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // height 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // height
0x19, // (field_number << 3) | wire_type 0x19, // (field_number << 3) | wire_type
@ -135,9 +138,7 @@ func TestVoteSignableTestVectors(t *testing.T) {
}, },
} }
for i, tc := range tests { for i, tc := range tests {
got, err := cdc.MarshalBinaryBare(tc.canonicalVote) got := tc.vote.SignBytes(tc.chainID)
require.NoError(t, err)
require.Equal(t, tc.want, got, "test case #%v: got unexpected sign bytes for Vote.", i) require.Equal(t, tc.want, got, "test case #%v: got unexpected sign bytes for Vote.", i)
} }
} }