From a530352f6165ac4c9dc64921fae2ab7500c9bc39 Mon Sep 17 00:00:00 2001 From: Ismail Khoffi Date: Tue, 30 Oct 2018 17:16:55 +0100 Subject: [PATCH] Align Vote/Proposal fields with canonical order and fields (#2730) * reorder fields * add TestVoteString & update tests * remove redundant info from Proposal.String() * update spec * revert changes on vote.String() -> more human friendly --- Gopkg.lock | 6 +++--- docs/spec/blockchain/blockchain.md | 16 ++++++++-------- types/proposal.go | 14 ++++++++++---- types/vote.go | 9 +++++---- types/vote_test.go | 26 ++++++++++++++++++++------ 5 files changed, 46 insertions(+), 25 deletions(-) diff --git a/Gopkg.lock b/Gopkg.lock index f4656e6b..59e42f92 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -408,14 +408,14 @@ [[projects]] branch = "master" - digest = "1:fd98d154bf152ad5a49600ede7d7341851bcdfe358b9b82e5ccdba818618167c" + digest = "1:5207b4bc950fd0e45544263103af3e119c94fba6717f9d61931f7a19a7c0706a" name = "golang.org/x/sys" packages = [ "cpu", "unix", ] pruneopts = "UT" - revision = "2772b66316d2c587efeb188dcd5ebc6987656e84" + revision = "f7626d0b1519d8323581a047ca8b372ebf28de9a" [[projects]] digest = "1:a2ab62866c75542dd18d2b069fec854577a20211d7c0ea6ae746072a1dccdd18" @@ -446,7 +446,7 @@ name = "google.golang.org/genproto" packages = ["googleapis/rpc/status"] pruneopts = "UT" - revision = "94acd270e44e65579b9ee3cdab25034d33fed608" + revision = "b69ba1387ce2108ac9bc8e8e5e5a46e7d5c72313" [[projects]] digest = "1:2dab32a43451e320e49608ff4542fdfc653c95dcc35d0065ec9c6c3dd540ed74" diff --git a/docs/spec/blockchain/blockchain.md b/docs/spec/blockchain/blockchain.md index c5291ed4..06168537 100644 --- a/docs/spec/blockchain/blockchain.md +++ b/docs/spec/blockchain/blockchain.md @@ -146,14 +146,14 @@ The vote includes information about the validator signing it. ```go type Vote struct { - ValidatorAddress []byte - ValidatorIndex int - Height int64 - Round int - Timestamp Time - Type int8 - BlockID BlockID - Signature []byte + Type SignedMsgType // byte + Height int64 + Round int + Timestamp time.Time + BlockID BlockID + ValidatorAddress Address + ValidatorIndex int + Signature []byte } ``` diff --git a/types/proposal.go b/types/proposal.go index 5d70a3c8..fa82fdbb 100644 --- a/types/proposal.go +++ b/types/proposal.go @@ -20,11 +20,12 @@ var ( // to be considered valid. It may depend on votes from a previous round, // a so-called Proof-of-Lock (POL) round, as noted in the POLRound and POLBlockID. type Proposal struct { + Type SignedMsgType Height int64 `json:"height"` Round int `json:"round"` + POLRound int `json:"pol_round"` // -1 if null. Timestamp time.Time `json:"timestamp"` BlockPartsHeader PartSetHeader `json:"block_parts_header"` - POLRound int `json:"pol_round"` // -1 if null. POLBlockID BlockID `json:"pol_block_id"` // zero if null. Signature []byte `json:"signature"` } @@ -33,11 +34,12 @@ type Proposal struct { // If there is no POLRound, polRound should be -1. func NewProposal(height int64, round int, blockPartsHeader PartSetHeader, polRound int, polBlockID BlockID) *Proposal { return &Proposal{ + Type: ProposalType, Height: height, Round: round, + POLRound: polRound, Timestamp: tmtime.Now(), BlockPartsHeader: blockPartsHeader, - POLRound: polRound, POLBlockID: polBlockID, } } @@ -45,9 +47,13 @@ func NewProposal(height int64, round int, blockPartsHeader PartSetHeader, polRou // String returns a string representation of the Proposal. func (p *Proposal) String() string { return fmt.Sprintf("Proposal{%v/%v %v (%v,%v) %X @ %s}", - p.Height, p.Round, p.BlockPartsHeader, p.POLRound, + p.Height, + p.Round, + p.BlockPartsHeader, + p.POLRound, p.POLBlockID, - cmn.Fingerprint(p.Signature), CanonicalTime(p.Timestamp)) + cmn.Fingerprint(p.Signature), + CanonicalTime(p.Timestamp)) } // SignBytes returns the Proposal bytes for signing diff --git a/types/vote.go b/types/vote.go index 333684fc..826330d5 100644 --- a/types/vote.go +++ b/types/vote.go @@ -48,13 +48,13 @@ type Address = crypto.Address // Represents a prevote, precommit, or commit vote from validators for consensus. type Vote struct { - ValidatorAddress Address `json:"validator_address"` - ValidatorIndex int `json:"validator_index"` + Type SignedMsgType `json:"type"` Height int64 `json:"height"` Round int `json:"round"` Timestamp time.Time `json:"timestamp"` - Type SignedMsgType `json:"type"` BlockID BlockID `json:"block_id"` // zero if vote is nil. + ValidatorAddress Address `json:"validator_address"` + ValidatorIndex int `json:"validator_index"` Signature []byte `json:"signature"` } @@ -94,7 +94,8 @@ func (vote *Vote) String() string { typeString, cmn.Fingerprint(vote.BlockID.Hash), cmn.Fingerprint(vote.Signature), - CanonicalTime(vote.Timestamp)) + CanonicalTime(vote.Timestamp), + ) } func (vote *Vote) Verify(chainID string, pubKey crypto.PubKey) error { diff --git a/types/vote_test.go b/types/vote_test.go index 1d7e3daf..57273585 100644 --- a/types/vote_test.go +++ b/types/vote_test.go @@ -26,12 +26,10 @@ func exampleVote(t byte) *Vote { } return &Vote{ - ValidatorAddress: tmhash.Sum([]byte("validator_address")), - ValidatorIndex: 56789, - Height: 12345, - Round: 2, - Timestamp: stamp, - Type: SignedMsgType(t), + Type: SignedMsgType(t), + Height: 12345, + Round: 2, + Timestamp: stamp, BlockID: BlockID{ Hash: tmhash.Sum([]byte("blockID_hash")), PartsHeader: PartSetHeader{ @@ -39,6 +37,8 @@ func exampleVote(t byte) *Vote { Hash: tmhash.Sum([]byte("blockID_part_set_header_hash")), }, }, + ValidatorAddress: tmhash.Sum([]byte("validator_address")), + ValidatorIndex: 56789, } } @@ -235,3 +235,17 @@ func TestMaxVoteBytes(t *testing.T) { assert.EqualValues(t, MaxVoteBytes, len(bz)) } + +func TestVoteString(t *testing.T) { + str := examplePrecommit().String() + expected := `Vote{56789:6AF1F4111082 12345/02/2(Precommit) 8B01023386C3 000000000000 @ 2017-12-25T03:00:01.234Z}` + if str != expected { + t.Errorf("Got unexpected string for Vote. Expected:\n%v\nGot:\n%v", expected, str) + } + + str2 := examplePrevote().String() + expected = `Vote{56789:6AF1F4111082 12345/02/1(Prevote) 8B01023386C3 000000000000 @ 2017-12-25T03:00:01.234Z}` + if str2 != expected { + t.Errorf("Got unexpected string for Vote. Expected:\n%v\nGot:\n%v", expected, str2) + } +}