Browse Source

types: first field in Canonical structs is Type (#2675)

* types: first field in Canonical structs is Type

* fix spec
pull/2688/head
Ethan Buchman 6 years ago
committed by GitHub
parent
commit
30519e8361
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 21 deletions
  1. +2
    -2
      docs/spec/blockchain/encoding.md
  2. +7
    -7
      types/canonical.go
  3. +12
    -12
      types/vote_test.go

+ 2
- 2
docs/spec/blockchain/encoding.md View File

@ -301,15 +301,15 @@ Where the `"value"` is the base64 encoding of the raw pubkey bytes, and the
Signed messages (eg. votes, proposals) in the consensus are encoded using Amino. Signed messages (eg. votes, proposals) in the consensus are encoded using Amino.
When signing, the elements of a message are re-ordered so the fixed-length fields When signing, the elements of a message are re-ordered so the fixed-length fields
are first, making it easy to quickly check the version, height, round, and type.
are first, making it easy to quickly check the type, height, and round.
The `ChainID` is also appended to the end. The `ChainID` is also appended to the end.
We call this encoding the SignBytes. For instance, SignBytes for a vote is the Amino encoding of the following struct: We call this encoding the SignBytes. For instance, SignBytes for a vote is the Amino encoding of the following struct:
```go ```go
type CanonicalVote struct { type CanonicalVote struct {
Type byte
Height int64 `binary:"fixed64"` Height int64 `binary:"fixed64"`
Round int64 `binary:"fixed64"` Round int64 `binary:"fixed64"`
VoteType byte
Timestamp time.Time Timestamp time.Time
BlockID CanonicalBlockID BlockID CanonicalBlockID
ChainID string ChainID string


+ 7
- 7
types/canonical.go View File

@ -23,9 +23,9 @@ type CanonicalPartSetHeader struct {
} }
type CanonicalProposal struct { type CanonicalProposal struct {
Type SignedMsgType // type alias for byte
Height int64 `binary:"fixed64"` Height int64 `binary:"fixed64"`
Round int64 `binary:"fixed64"` Round int64 `binary:"fixed64"`
Type SignedMsgType // type alias for byte
POLRound int64 `binary:"fixed64"` POLRound int64 `binary:"fixed64"`
Timestamp time.Time Timestamp time.Time
BlockPartsHeader CanonicalPartSetHeader BlockPartsHeader CanonicalPartSetHeader
@ -34,19 +34,19 @@ type CanonicalProposal struct {
} }
type CanonicalVote struct { type CanonicalVote struct {
Type SignedMsgType // type alias for byte
Height int64 `binary:"fixed64"` Height int64 `binary:"fixed64"`
Round int64 `binary:"fixed64"` Round int64 `binary:"fixed64"`
Type SignedMsgType // type alias for byte
Timestamp time.Time Timestamp time.Time
BlockID CanonicalBlockID BlockID CanonicalBlockID
ChainID string ChainID string
} }
type CanonicalHeartbeat struct { type CanonicalHeartbeat struct {
Type byte
Height int64 `binary:"fixed64"` Height int64 `binary:"fixed64"`
Round int `binary:"fixed64"` Round int `binary:"fixed64"`
Type byte
Sequence int `binary:"fixed64"`
Sequence int `binary:"fixed64"`
ValidatorAddress Address ValidatorAddress Address
ValidatorIndex int ValidatorIndex int
ChainID string ChainID string
@ -71,9 +71,9 @@ func CanonicalizePartSetHeader(psh PartSetHeader) CanonicalPartSetHeader {
func CanonicalizeProposal(chainID string, proposal *Proposal) CanonicalProposal { func CanonicalizeProposal(chainID string, proposal *Proposal) CanonicalProposal {
return CanonicalProposal{ return CanonicalProposal{
Type: ProposalType,
Height: proposal.Height, Height: proposal.Height,
Round: int64(proposal.Round), // cast int->int64 to make amino encode it fixed64 (does not work for int) Round: int64(proposal.Round), // cast int->int64 to make amino encode it fixed64 (does not work for int)
Type: ProposalType,
POLRound: int64(proposal.POLRound), POLRound: int64(proposal.POLRound),
Timestamp: proposal.Timestamp, Timestamp: proposal.Timestamp,
BlockPartsHeader: CanonicalizePartSetHeader(proposal.BlockPartsHeader), BlockPartsHeader: CanonicalizePartSetHeader(proposal.BlockPartsHeader),
@ -84,9 +84,9 @@ func CanonicalizeProposal(chainID string, proposal *Proposal) CanonicalProposal
func CanonicalizeVote(chainID string, vote *Vote) CanonicalVote { func CanonicalizeVote(chainID string, vote *Vote) CanonicalVote {
return CanonicalVote{ return CanonicalVote{
Type: vote.Type,
Height: vote.Height, Height: vote.Height,
Round: int64(vote.Round), // cast int->int64 to make amino encode it fixed64 (does not work for int) Round: int64(vote.Round), // cast int->int64 to make amino encode it fixed64 (does not work for int)
Type: vote.Type,
Timestamp: vote.Timestamp, Timestamp: vote.Timestamp,
BlockID: CanonicalizeBlockID(vote.BlockID), BlockID: CanonicalizeBlockID(vote.BlockID),
ChainID: chainID, ChainID: chainID,
@ -95,9 +95,9 @@ func CanonicalizeVote(chainID string, vote *Vote) CanonicalVote {
func CanonicalizeHeartbeat(chainID string, heartbeat *Heartbeat) CanonicalHeartbeat { func CanonicalizeHeartbeat(chainID string, heartbeat *Heartbeat) CanonicalHeartbeat {
return CanonicalHeartbeat{ return CanonicalHeartbeat{
Type: byte(HeartbeatType),
Height: heartbeat.Height, Height: heartbeat.Height,
Round: heartbeat.Round, Round: heartbeat.Round,
Type: byte(HeartbeatType),
Sequence: heartbeat.Sequence, Sequence: heartbeat.Sequence,
ValidatorAddress: heartbeat.ValidatorAddress, ValidatorAddress: heartbeat.ValidatorAddress,
ValidatorIndex: heartbeat.ValidatorIndex, ValidatorIndex: heartbeat.ValidatorIndex,


+ 12
- 12
types/vote_test.go View File

@ -70,12 +70,12 @@ func TestVoteSignableTestVectors(t *testing.T) {
CanonicalizeVote("", &Vote{Height: 1, Round: 1, Type: PrecommitType}), CanonicalizeVote("", &Vote{Height: 1, Round: 1, Type: PrecommitType}),
[]byte{ []byte{
0x1f, // total length 0x1f, // total length
0x9, // (field_number << 3) | wire_type
0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // height
0x8, // (field_number << 3) | wire_type
0x2, // PrecommitType
0x11, // (field_number << 3) | wire_type 0x11, // (field_number << 3) | wire_type
0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // height
0x19, // (field_number << 3) | wire_type
0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // round 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // round
0x18, // (field_number << 3) | wire_type
0x2, // PrecommitType
0x22, // (field_number << 3) | wire_type 0x22, // (field_number << 3) | wire_type
// remaining fields (timestamp): // remaining fields (timestamp):
0x9, 0x9, 0x0, 0x9, 0x6e, 0x88, 0xf1, 0xff, 0xff, 0xff}, 0x9, 0x9, 0x0, 0x9, 0x6e, 0x88, 0xf1, 0xff, 0xff, 0xff},
@ -85,12 +85,12 @@ func TestVoteSignableTestVectors(t *testing.T) {
CanonicalizeVote("", &Vote{Height: 1, Round: 1, Type: PrevoteType}), CanonicalizeVote("", &Vote{Height: 1, Round: 1, Type: PrevoteType}),
[]byte{ []byte{
0x1f, // total length 0x1f, // total length
0x9, // (field_number << 3) | wire_type
0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // height
0x8, // (field_number << 3) | wire_type
0x1, // PrevoteType
0x11, // (field_number << 3) | wire_type 0x11, // (field_number << 3) | wire_type
0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // height
0x19, // (field_number << 3) | wire_type
0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // round 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // round
0x18, // (field_number << 3) | wire_type
0x1, // PrevoteType
0x22, // (field_number << 3) | wire_type 0x22, // (field_number << 3) | wire_type
// remaining fields (timestamp): // remaining fields (timestamp):
0x9, 0x9, 0x0, 0x9, 0x6e, 0x88, 0xf1, 0xff, 0xff, 0xff}, 0x9, 0x9, 0x0, 0x9, 0x6e, 0x88, 0xf1, 0xff, 0xff, 0xff},
@ -99,9 +99,9 @@ func TestVoteSignableTestVectors(t *testing.T) {
vote, vote,
[]byte{ []byte{
0x1d, // total length 0x1d, // total length
0x9, // (field_number << 3) | wire_type
0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // height
0x11, // (field_number << 3) | wire_type 0x11, // (field_number << 3) | wire_type
0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // height
0x19, // (field_number << 3) | wire_type
0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // round 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // round
// remaining fields (timestamp): // remaining fields (timestamp):
0x22, 0x22,
@ -112,9 +112,9 @@ func TestVoteSignableTestVectors(t *testing.T) {
CanonicalizeVote("test_chain_id", &Vote{Height: 1, Round: 1}), CanonicalizeVote("test_chain_id", &Vote{Height: 1, Round: 1}),
[]byte{ []byte{
0x2c, // total length 0x2c, // total length
0x9, // (field_number << 3) | wire_type
0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // height
0x11, // (field_number << 3) | wire_type 0x11, // (field_number << 3) | wire_type
0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // height
0x19, // (field_number << 3) | wire_type
0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // round 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // round
// remaining fields: // remaining fields:
0x22, // (field_number << 3) | wire_type 0x22, // (field_number << 3) | wire_type


Loading…
Cancel
Save