Replace struct literals with constructor calls

This commit is contained in:
Jeremiah Andrews 2018-08-14 11:16:40 -07:00
parent 632ac41d79
commit 170364a472
5 changed files with 44 additions and 41 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -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)
}
//--------------------------------------------------------------------------------