Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

types.NewCommit #3275

Merged
merged 7 commits into from
Feb 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions blockchain/reactor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@ func newBlockchainReactor(logger log.Logger, genDoc *types.GenesisDoc, privVals

// let's add some blocks in
for blockHeight := int64(1); blockHeight <= maxBlockHeight; blockHeight++ {
lastCommit := &types.Commit{}
lastCommit := types.NewCommit(types.BlockID{}, nil)
if blockHeight > 1 {
lastBlockMeta := blockStore.LoadBlockMeta(blockHeight - 1)
lastBlock := blockStore.LoadBlock(blockHeight - 1)

vote := makeVote(&lastBlock.Header, lastBlockMeta.BlockID, state.Validators, privVals[0]).CommitSig()
lastCommit = &types.Commit{Precommits: []*types.CommitSig{vote}, BlockID: lastBlockMeta.BlockID}
lastCommit = types.NewCommit(lastBlockMeta.BlockID, []*types.CommitSig{vote})
}

thisBlock := makeBlock(blockHeight, state, lastCommit)
Expand Down
7 changes: 2 additions & 5 deletions blockchain/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,8 @@ import (

// make a Commit with a single vote containing just the height and a timestamp
func makeTestCommit(height int64, timestamp time.Time) *types.Commit {
return &types.Commit{
Precommits: []*types.CommitSig{
{Height: height, Timestamp: timestamp},
},
}
commitSigs := []*types.CommitSig{{Height: height, Timestamp: timestamp}}
return types.NewCommit(types.BlockID{}, commitSigs)
}

func makeStateAndBlockStore(logger log.Logger) (sm.State, *BlockStore) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/tendermint/commands/reset_priv_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func resetFilePV(privValKeyFile, privValStateFile string, logger log.Logger) {
} else {
pv := privval.GenFilePV(privValKeyFile, privValStateFile)
pv.Save()
logger.Info("Generated private validator file", "file", "keyFile", privValKeyFile,
logger.Info("Generated private validator file", "keyFile", privValKeyFile,
"stateFile", privValStateFile)
}
}
Expand Down
6 changes: 2 additions & 4 deletions consensus/replay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -537,10 +537,8 @@ func makeBlockchainFromWAL(wal WAL) ([]*types.Block, []*types.Commit, error) {
}
case *types.Vote:
if p.Type == types.PrecommitType {
thisBlockCommit = &types.Commit{
BlockID: p.BlockID,
Precommits: []*types.CommitSig{p.CommitSig()},
}
commitSigs := []*types.CommitSig{p.CommitSig()}
thisBlockCommit = types.NewCommit(p.BlockID, commitSigs)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion consensus/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -954,7 +954,7 @@ func (cs *ConsensusState) createProposalBlock() (block *types.Block, blockParts
if cs.Height == 1 {
// We're creating a proposal for the first block.
// The commit is empty, but not nil.
commit = &types.Commit{}
commit = types.NewCommit(types.BlockID{}, nil)
} else if cs.LastCommit.HasTwoThirdsMajority() {
// Make the commit from LastCommit
commit = cs.LastCommit.MakeCommit()
Expand Down
7 changes: 2 additions & 5 deletions consensus/types/round_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,8 @@ func BenchmarkRoundStateDeepCopy(b *testing.B) {
Data: types.Data{
Txs: txs,
},
Evidence: types.EvidenceData{},
LastCommit: &types.Commit{
BlockID: blockID,
Precommits: precommits,
},
Evidence: types.EvidenceData{},
LastCommit: types.NewCommit(blockID, precommits),
}
parts := block.MakePartSet(4096)
// Random Proposal
Expand Down
8 changes: 2 additions & 6 deletions lite/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,8 @@ func (pkz privKeys) signHeader(header *types.Header, first, last int) *types.Com
vote := makeVote(header, vset, pkz[i])
commitSigs[vote.ValidatorIndex] = vote.CommitSig()
}

res := &types.Commit{
BlockID: types.BlockID{Hash: header.Hash()},
Precommits: commitSigs,
}
return res
blockID := types.BlockID{Hash: header.Hash()}
return types.NewCommit(blockID, commitSigs)
}

func makeVote(header *types.Header, valset *types.ValidatorSet, key crypto.PrivKey) *types.Vote {
Expand Down
8 changes: 4 additions & 4 deletions lite/proxy/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func TestValidateBlock(t *testing.T) {
},
signedHeader: types.SignedHeader{
Header: &types.Header{Height: 11},
Commit: &types.Commit{BlockID: types.BlockID{Hash: []byte("0xDEADBEEF")}},
Commit: types.NewCommit(types.BlockID{Hash: []byte("0xDEADBEEF")}, nil),
},
wantErr: "Data hash doesn't match header",
},
Expand All @@ -81,7 +81,7 @@ func TestValidateBlock(t *testing.T) {
},
signedHeader: types.SignedHeader{
Header: &types.Header{Height: 11},
Commit: &types.Commit{BlockID: types.BlockID{Hash: []byte("DEADBEEF")}},
Commit: types.NewCommit(types.BlockID{Hash: []byte("DEADBEEF")}, nil),
},
},
// End Header.Data hash mismatch test
Expand Down Expand Up @@ -169,7 +169,7 @@ func TestValidateBlockMeta(t *testing.T) {
ValidatorsHash: []byte("Tendermint"),
Time: testTime2,
},
Commit: &types.Commit{BlockID: types.BlockID{Hash: []byte("DEADBEEF")}},
Commit: types.NewCommit(types.BlockID{Hash: []byte("DEADBEEF")}, nil),
},
wantErr: "Headers don't match",
},
Expand All @@ -188,7 +188,7 @@ func TestValidateBlockMeta(t *testing.T) {
ValidatorsHash: []byte("Tendermint-x"),
Time: testTime2,
},
Commit: &types.Commit{BlockID: types.BlockID{Hash: []byte("DEADBEEF")}},
Commit: types.NewCommit(types.BlockID{Hash: []byte("DEADBEEF")}, nil),
},
wantErr: "Headers don't match",
},
Expand Down
2 changes: 1 addition & 1 deletion node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ func TestCreateProposalBlock(t *testing.T) {
evidencePool,
)

commit := &types.Commit{}
commit := types.NewCommit(types.BlockID{}, nil)
block, _ := blockExec.CreateProposalBlock(
height,
state, commit,
Expand Down
4 changes: 2 additions & 2 deletions state/execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func TestBeginBlockValidators(t *testing.T) {
}

for _, tc := range testCases {
lastCommit := &types.Commit{BlockID: prevBlockID, Precommits: tc.lastCommitPrecommits}
lastCommit := types.NewCommit(prevBlockID, tc.lastCommitPrecommits)

// block for height 2
block, _ := state.MakeBlock(2, makeTxs(2), lastCommit, nil, state.Validators.GetProposer().Address)
Expand Down Expand Up @@ -139,7 +139,7 @@ func TestBeginBlockByzantineValidators(t *testing.T) {
commitSig0 := (&types.Vote{ValidatorIndex: 0, Timestamp: now, Type: types.PrecommitType}).CommitSig()
commitSig1 := (&types.Vote{ValidatorIndex: 1, Timestamp: now}).CommitSig()
commitSigs := []*types.CommitSig{commitSig0, commitSig1}
lastCommit := &types.Commit{BlockID: prevBlockID, Precommits: commitSigs}
lastCommit := types.NewCommit(prevBlockID, commitSigs)
for _, tc := range testCases {

block, _ := state.MakeBlock(10, makeTxs(2), lastCommit, nil, state.Validators.GetProposer().Address)
Expand Down
34 changes: 20 additions & 14 deletions types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,8 +490,8 @@ func (cs *CommitSig) String() string {
}

// toVote converts the CommitSig to a vote.
// Once CommitSig has fewer fields than vote,
// converting to a Vote will require more information.
// TODO: deprecate for #1648. Converting to Vote will require
// access to ValidatorSet.
func (cs *CommitSig) toVote() *Vote {
if cs == nil {
return nil
Expand All @@ -509,18 +509,30 @@ type Commit struct {
BlockID BlockID `json:"block_id"`
Precommits []*CommitSig `json:"precommits"`

// Volatile
// memoized in first call to corresponding method
// NOTE: can't memoize in constructor because constructor
// isn't used for unmarshaling
height int64
round int
hash cmn.HexBytes
bitArray *cmn.BitArray
}

// NewCommit returns a new Commit with the given blockID and precommits.
// TODO: memoize ValidatorSet in constructor so votes can be easily reconstructed
// from CommitSig after #1648.
func NewCommit(blockID BlockID, precommits []*CommitSig) *Commit {
return &Commit{
BlockID: blockID,
Precommits: precommits,
}
}

// 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)
return commit.ToVote(cs).SignBytes(chainID)
}

// memoizeHeightRound memoizes the height and round of the commit using
Expand All @@ -543,27 +555,20 @@ func (commit *Commit) memoizeHeightRound() {

// 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 {
// TODO: use commit.validatorSet to reconstruct vote
// and deprecate .toVote
return cs.toVote()
}

// Height returns the height of the commit
func (commit *Commit) Height() int64 {
if len(commit.Precommits) == 0 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So now a commit always has some precommits?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This already should have been the case, but regardless the check exists in memoizeHeightAndRound and the default value is still 0

return 0
}
commit.memoizeHeightRound()
return commit.height
}

// Round returns the round of the commit
func (commit *Commit) Round() int {
if len(commit.Precommits) == 0 {
return 0
}
commit.memoizeHeightRound()
return commit.round
}
Expand Down Expand Up @@ -595,9 +600,10 @@ func (commit *Commit) BitArray() *cmn.BitArray {
}

// GetByIndex returns the vote corresponding to a given validator index.
// Panics if `index >= commit.Size()`.
// Implements VoteSetReader.
func (commit *Commit) GetByIndex(index int) *Vote {
return commit.Precommits[index].toVote()
return commit.ToVote(commit.Precommits[index])
}

// IsCommit returns true if there is at least one vote.
Expand Down
10 changes: 2 additions & 8 deletions types/validator_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -563,18 +563,12 @@ func TestValidatorSetVerifyCommit(t *testing.T) {
sig, err := privKey.Sign(vote.SignBytes(chainID))
assert.NoError(t, err)
vote.Signature = sig
commit := &Commit{
BlockID: blockID,
Precommits: []*CommitSig{vote.CommitSig()},
}
commit := NewCommit(blockID, []*CommitSig{vote.CommitSig()})

badChainID := "notmychainID"
badBlockID := BlockID{Hash: []byte("goodbye")}
badHeight := height + 1
badCommit := &Commit{
BlockID: blockID,
Precommits: []*CommitSig{nil},
}
badCommit := NewCommit(blockID, []*CommitSig{nil})

// test some error cases
// TODO: test more cases!
Expand Down
5 changes: 1 addition & 4 deletions types/vote_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -545,10 +545,7 @@ func (voteSet *VoteSet) MakeCommit() *Commit {
for i, v := range voteSet.votes {
commitSigs[i] = v.CommitSig()
}
return &Commit{
BlockID: *voteSet.maj23,
Precommits: commitSigs,
}
return NewCommit(*voteSet.maj23, commitSigs)
}

//--------------------------------------------------------------------------------
Expand Down