Introduce CommitSig alias for Vote in Commit (#3245)

* types: memoize height/round in commit instead of first vote

* types: commit.ValidateBasic in VerifyCommit

* types: new CommitSig alias for Vote

In preparation for reducing the redundancy in Commits, we introduce the
CommitSig as an alias for Vote. This is non-breaking on the protocol,
and minor breaking on the Go API, as Commit now contains a list of
CommitSig instead of Vote.

* remove dependence on ToVote

* update some comments

* fix tests

* fix tests

* fixes from review
This commit is contained in:
Ethan Buchman
2019-02-04 13:01:59 -05:00
committed by GitHub
parent 39eba4e154
commit 1809efa350
16 changed files with 136 additions and 70 deletions

View File

@ -477,39 +477,77 @@ func (h *Header) StringIndented(indent string) string {
//-------------------------------------
// CommitSig is a vote included in a Commit.
// For now, it is identical to a vote,
// but in the future it will contain fewer fields
// to eliminate the redundancy in commits.
// See https://github.com/tendermint/tendermint/issues/1648.
type CommitSig Vote
// String returns the underlying Vote.String()
func (cs *CommitSig) String() string {
return cs.toVote().String()
}
// toVote converts the CommitSig to a vote.
// Once CommitSig has fewer fields than vote,
// converting to a Vote will require more information.
func (cs *CommitSig) toVote() *Vote {
if cs == nil {
return nil
}
v := Vote(*cs)
return &v
}
// Commit contains the evidence that a block was committed by a set of validators.
// NOTE: Commit is empty for height 1, but never nil.
type Commit struct {
// NOTE: The Precommits are in order of address to preserve the bonded ValidatorSet order.
// Any peer with a block can gossip precommits by index with a peer without recalculating the
// active ValidatorSet.
BlockID BlockID `json:"block_id"`
Precommits []*Vote `json:"precommits"`
BlockID BlockID `json:"block_id"`
Precommits []*CommitSig `json:"precommits"`
// Volatile
firstPrecommit *Vote
hash cmn.HexBytes
bitArray *cmn.BitArray
height int64
round int
hash cmn.HexBytes
bitArray *cmn.BitArray
}
// FirstPrecommit returns the first non-nil precommit in the commit.
// If all precommits are nil, it returns an empty precommit with height 0.
func (commit *Commit) FirstPrecommit() *Vote {
// VoteSignBytes constructs the SignBytes for the given CommitSig.
// The only unique part of the SignBytes is the Timestamp - all other fields
// signed over are otherwise the same for all validators.
func (commit *Commit) VoteSignBytes(chainID string, cs *CommitSig) []byte {
return cs.toVote().SignBytes(chainID)
}
// memoizeHeightRound memoizes the height and round of the commit using
// the first non-nil vote.
func (commit *Commit) memoizeHeightRound() {
if len(commit.Precommits) == 0 {
return nil
return
}
if commit.firstPrecommit != nil {
return commit.firstPrecommit
if commit.height > 0 {
return
}
for _, precommit := range commit.Precommits {
if precommit != nil {
commit.firstPrecommit = precommit
return precommit
commit.height = precommit.Height
commit.round = precommit.Round
return
}
}
return &Vote{
Type: PrecommitType,
}
}
// ToVote converts a CommitSig to a Vote.
// If the CommitSig is nil, the Vote will be nil.
// When CommitSig is reduced to contain fewer fields,
// this will need access to the ValidatorSet to properly
// reconstruct the vote.
func (commit *Commit) ToVote(cs *CommitSig) *Vote {
return cs.toVote()
}
// Height returns the height of the commit
@ -517,7 +555,8 @@ func (commit *Commit) Height() int64 {
if len(commit.Precommits) == 0 {
return 0
}
return commit.FirstPrecommit().Height
commit.memoizeHeightRound()
return commit.height
}
// Round returns the round of the commit
@ -525,7 +564,8 @@ func (commit *Commit) Round() int {
if len(commit.Precommits) == 0 {
return 0
}
return commit.FirstPrecommit().Round
commit.memoizeHeightRound()
return commit.round
}
// Type returns the vote type of the commit, which is always VoteTypePrecommit
@ -554,12 +594,13 @@ func (commit *Commit) BitArray() *cmn.BitArray {
return commit.bitArray
}
// GetByIndex returns the vote corresponding to a given validator index
// GetByIndex returns the vote corresponding to a given validator index.
// Implements VoteSetReader.
func (commit *Commit) GetByIndex(index int) *Vote {
return commit.Precommits[index]
return commit.Precommits[index].toVote()
}
// IsCommit returns true if there is at least one vote
// IsCommit returns true if there is at least one vote.
func (commit *Commit) IsCommit() bool {
return len(commit.Precommits) != 0
}