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
This commit is contained in:
Ismail Khoffi
2018-10-30 12:16:55 -04:00
committed by Ethan Buchman
parent 60437953ac
commit a530352f61
5 changed files with 46 additions and 25 deletions
+10 -4
View File
@@ -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
+5 -4
View File
@@ -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 {
+20 -6
View File
@@ -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)
}
}