Browse Source

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
pull/2735/head
Ismail Khoffi 6 years ago
committed by Ethan Buchman
parent
commit
a530352f61
5 changed files with 46 additions and 25 deletions
  1. +3
    -3
      Gopkg.lock
  2. +8
    -8
      docs/spec/blockchain/blockchain.md
  3. +10
    -4
      types/proposal.go
  4. +5
    -4
      types/vote.go
  5. +20
    -6
      types/vote_test.go

+ 3
- 3
Gopkg.lock View File

@ -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"


+ 8
- 8
docs/spec/blockchain/blockchain.md View File

@ -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
}
```


+ 10
- 4
types/proposal.go 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
types/vote.go 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
types/vote_test.go 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)
}
}

Loading…
Cancel
Save