diff --git a/lite/helpers.go b/lite/helpers.go index 8d6865927..a4a22a3a8 100644 --- a/lite/helpers.go +++ b/lite/helpers.go @@ -79,19 +79,11 @@ func (pkz privKeys) signHeader(header *types.Header, first, last int) *types.Com // Fill in the votes we want. for i := first; i < last && i < len(pkz); i++ { vote := makeVote(header, vset, pkz[i]) - commitSigs[vote.ValidatorIndex] = &types.CommitSig{ - Signature: vote.Signature, - Timestamp: vote.Timestamp, - } + commitSigs[vote.ValidatorIndex] = vote.ToCommitSig() } - res := &types.Commit{ - BlockID: types.BlockID{Hash: header.Hash()}, - Precommits: commitSigs, - RoundNum: 1, - HeightNum: header.Height, - } - return res + blockID := types.BlockID{Hash: header.Hash()} + return types.NewCommit(header.Height, 1, blockID, commitSigs) } func makeVote(header *types.Header, valset *types.ValidatorSet, key crypto.PrivKey) *types.Vote { diff --git a/types/block.go b/types/block.go index a204fa495..3f02420ba 100644 --- a/types/block.go +++ b/types/block.go @@ -296,21 +296,37 @@ 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. + HeightNum int64 + RoundNum int BlockID BlockID `json:"block_id"` Precommits []*CommitSig `json:"precommits"` - RoundNum int - HeightNum int64 // Volatile hash cmn.HexBytes bitArray *cmn.BitArray } +func NewCommit(height int64, round int, blockID BlockID, precommits []*CommitSig) *Commit { + return &Commit{ + HeightNum: height, + RoundNum: round, + BlockID: blockID, + Precommits: precommits, + } +} + type CommitSig struct { Signature []byte Timestamp time.Time } +func NewCommitSig(signature []byte, timestamp time.Time) *CommitSig { + return &CommitSig{ + signature, + timestamp, + } +} + func (commitSig *CommitSig) String(index int, address Address, height int64, round int, blockID BlockID) string { return fmt.Sprintf("Vote{%v:%X %v/%02d/%v(%v) %X %X @ %s}", index, cmn.Fingerprint(address), @@ -320,6 +336,18 @@ func (commitSig *CommitSig) String(index int, address Address, height int64, rou CanonicalTime(commitSig.Timestamp)) } +func (commitSig *CommitSig) ToVote(index int, height int64, round int, blockID BlockID) *Vote { + return &Vote{ + ValidatorIndex: index, + Height: height, + Round: round, + Timestamp: commitSig.Timestamp, + Type: VoteTypePrecommit, + BlockID: blockID, + Signature: commitSig.Signature, + } +} + // Height returns the height of the commit func (commit *Commit) Height() int64 { return commit.HeightNum @@ -358,15 +386,7 @@ func (commit *Commit) BitArray() *cmn.BitArray { // GetByIndex returns the vote corresponding to a given validator index func (commit *Commit) GetByIndex(index int) *Vote { - return &Vote{ - ValidatorIndex: index, - Height: commit.HeightNum, - Round: commit.RoundNum, - Timestamp: commit.Precommits[index].Timestamp, - Type: VoteTypePrecommit, - BlockID: commit.BlockID, - Signature: commit.Precommits[index].Signature, - } + return commit.Precommits[index].ToVote(index, commit.Height(), commit.Round(), commit.BlockID) } // IsCommit returns true if there is at least one vote diff --git a/types/validator_set_test.go b/types/validator_set_test.go index 39967fc83..b00e4048f 100644 --- a/types/validator_set_test.go +++ b/types/validator_set_test.go @@ -392,15 +392,10 @@ func TestValidatorSetVerifyCommit(t *testing.T) { assert.NoError(t, err) vote.Signature = sig commit := &Commit{ - BlockID: blockID, - Precommits: []*CommitSig{ - &CommitSig{ - Signature: sig, - Timestamp: vote.Timestamp, - }, - }, - HeightNum: height, - RoundNum: 0, + BlockID: blockID, + Precommits: []*CommitSig{NewCommitSig(sig, vote.Timestamp)}, + HeightNum: height, + RoundNum: 0, } badChainID := "notmychainID" diff --git a/types/vote.go b/types/vote.go index 9a6180d75..6fc7e91bd 100644 --- a/types/vote.go +++ b/types/vote.go @@ -116,3 +116,7 @@ func (vote *Vote) Verify(chainID string, pubKey crypto.PubKey) error { } return nil } + +func (vote *Vote) ToCommitSig() *CommitSig { + return NewCommitSig(vote.Signature, vote.Timestamp) +} diff --git a/types/vote_set.go b/types/vote_set.go index 00a1f25c3..592052060 100644 --- a/types/vote_set.go +++ b/types/vote_set.go @@ -547,18 +547,10 @@ func (voteSet *VoteSet) MakeCommit() *Commit { precommits := make([]*CommitSig, len(voteSet.votes)) for i, v := range votesCopy { if v != nil && v.BlockID.Equals(blockID) { - precommits[i] = &CommitSig{ - Signature: v.Signature, - Timestamp: v.Timestamp, - } + precommits[i] = v.ToCommitSig() } } - return &Commit{ - BlockID: blockID, - Precommits: precommits, - RoundNum: voteSet.round, - HeightNum: voteSet.height, - } + return NewCommit(voteSet.height, voteSet.round, blockID, precommits) } //--------------------------------------------------------------------------------