Skip to content

Commit

Permalink
Make BuildLastCommitInfo and BuildExtendedCommitInfo public (cometbft…
Browse files Browse the repository at this point in the history
…#1502)

* Make BuildExtendedCommitInfo public

* Make BuildLastCommitInfo exported

* Fix validator set heights

* Split BuildCommitInfo functions into public and private parts

* Fix name in comment for buildLastCommitInfoFromStore

* Add doc comments for public Build_CommitInfo functions
  • Loading branch information
p-offtermatt authored Oct 18, 2023
1 parent 4109e33 commit d4708b4
Showing 1 changed file with 35 additions and 11 deletions.
46 changes: 35 additions & 11 deletions state/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func (blockExec *BlockExecutor) CreateProposalBlock(
&abci.RequestPrepareProposal{
MaxTxBytes: maxDataBytes,
Txs: block.Txs.ToSliceOfBytes(),
LocalLastCommit: buildExtendedCommitInfo(lastExtCommit, blockExec.store, state.InitialHeight, state.ConsensusParams.ABCI),
LocalLastCommit: buildExtendedCommitInfoFromStore(lastExtCommit, blockExec.store, state.InitialHeight, state.ConsensusParams.ABCI),
Misbehavior: block.Evidence.Evidence.ToABCI(),
Height: block.Height,
Time: block.Time,
Expand Down Expand Up @@ -175,7 +175,7 @@ func (blockExec *BlockExecutor) ProcessProposal(
Height: block.Header.Height,
Time: block.Header.Time,
Txs: block.Data.Txs.ToSliceOfBytes(),
ProposedLastCommit: buildLastCommitInfo(block, blockExec.store, state.InitialHeight),
ProposedLastCommit: buildLastCommitInfoFromStore(block, blockExec.store, state.InitialHeight),
Misbehavior: block.Evidence.Evidence.ToABCI(),
ProposerAddress: block.ProposerAddress,
NextValidatorsHash: block.NextValidatorsHash,
Expand Down Expand Up @@ -215,16 +215,14 @@ func (blockExec *BlockExecutor) ApplyBlock(
return state, ErrInvalidBlock(err)
}

commitInfo := buildLastCommitInfo(block, blockExec.store, state.InitialHeight)

startTime := time.Now().UnixNano()
abciResponse, err := blockExec.proxyApp.FinalizeBlock(context.TODO(), &abci.RequestFinalizeBlock{
Hash: block.Hash(),
NextValidatorsHash: block.NextValidatorsHash,
ProposerAddress: block.ProposerAddress,
Height: block.Height,
Time: block.Time,
DecidedLastCommit: commitInfo,
DecidedLastCommit: buildLastCommitInfoFromStore(block, blockExec.store, state.InitialHeight),
Misbehavior: block.Evidence.Evidence.ToABCI(),
Txs: block.Txs.ToSliceOfBytes(),
})
Expand Down Expand Up @@ -329,12 +327,13 @@ func (blockExec *BlockExecutor) ExtendVote(
if vote.Height != block.Height {
panic(fmt.Sprintf("vote's and block's heights do not match %d!=%d", block.Height, vote.Height))
}

req := abci.RequestExtendVote{
Hash: vote.BlockID.Hash,
Height: vote.Height,
Time: block.Time,
Txs: block.Txs.ToSliceOfBytes(),
ProposedLastCommit: buildLastCommitInfo(block, blockExec.store, state.InitialHeight),
ProposedLastCommit: buildLastCommitInfoFromStore(block, blockExec.store, state.InitialHeight),
Misbehavior: block.Evidence.Evidence.ToABCI(),
NextValidatorsHash: block.NextValidatorsHash,
ProposerAddress: block.ProposerAddress,
Expand Down Expand Up @@ -423,8 +422,8 @@ func (blockExec *BlockExecutor) Commit(
//---------------------------------------------------------
// Helper functions for executing blocks and updating state

func buildLastCommitInfo(block *types.Block, store Store, initialHeight int64) abci.CommitInfo {
if block.Height == initialHeight {
func buildLastCommitInfoFromStore(block *types.Block, store Store, initialHeight int64) abci.CommitInfo {
if block.Height == initialHeight { // check for initial height before loading validators
// there is no last commit for the initial height.
// return an empty value.
return abci.CommitInfo{}
Expand All @@ -435,6 +434,19 @@ func buildLastCommitInfo(block *types.Block, store Store, initialHeight int64) a
panic(fmt.Errorf("failed to load validator set at height %d: %w", block.Height-1, err))
}

return BuildLastCommitInfo(block, lastValSet, initialHeight)
}

// BuildLastCommitInfo builds a CommitInfo from the given block and validator set.
// If you want to load the validator set from the store instead of providing it,
// use buildLastCommitInfoFromStore.
func BuildLastCommitInfo(block *types.Block, lastValSet *types.ValidatorSet, initialHeight int64) abci.CommitInfo {
if block.Height == initialHeight {
// there is no last commit for the initial height.
// return an empty value.
return abci.CommitInfo{}
}

var (
commitSize = block.LastCommit.Size()
valSetLen = len(lastValSet.Validators)
Expand Down Expand Up @@ -464,7 +476,7 @@ func buildLastCommitInfo(block *types.Block, store Store, initialHeight int64) a
}
}

// buildExtendedCommitInfo populates an ABCI extended commit from the
// buildExtendedCommitInfoFromStore populates an ABCI extended commit from the
// corresponding CometBFT extended commit ec, using the stored validator set
// from ec. It requires ec to include the original precommit votes along with
// the vote extensions from the last commit.
Expand All @@ -473,7 +485,7 @@ func buildLastCommitInfo(block *types.Block, store Store, initialHeight int64) a
// data, it returns an empty record.
//
// Assumes that the commit signatures are sorted according to validator index.
func buildExtendedCommitInfo(ec *types.ExtendedCommit, store Store, initialHeight int64, ap types.ABCIParams) abci.ExtendedCommitInfo {
func buildExtendedCommitInfoFromStore(ec *types.ExtendedCommit, store Store, initialHeight int64, ap types.ABCIParams) abci.ExtendedCommitInfo {
if ec.Height < initialHeight {
// There are no extended commits for heights below the initial height.
return abci.ExtendedCommitInfo{}
Expand All @@ -484,6 +496,18 @@ func buildExtendedCommitInfo(ec *types.ExtendedCommit, store Store, initialHeigh
panic(fmt.Errorf("failed to load validator set at height %d, initial height %d: %w", ec.Height, initialHeight, err))
}

return BuildExtendedCommitInfo(ec, valSet, initialHeight, ap)
}

// BuildExtendedCommitInfo builds an ExtendedCommitInfo from the given block and validator set.
// If you want to load the validator set from the store instead of providing it,
// use buildExtendedCommitInfoFromStore.
func BuildExtendedCommitInfo(ec *types.ExtendedCommit, valSet *types.ValidatorSet, initialHeight int64, ap types.ABCIParams) abci.ExtendedCommitInfo {
if ec.Height < initialHeight {
// There are no extended commits for heights below the initial height.
return abci.ExtendedCommitInfo{}
}

var (
ecSize = ec.Size()
valSetLen = len(valSet.Validators)
Expand Down Expand Up @@ -704,7 +728,7 @@ func ExecCommitBlock(
store Store,
initialHeight int64,
) ([]byte, error) {
commitInfo := buildLastCommitInfo(block, store, initialHeight)
commitInfo := buildLastCommitInfoFromStore(block, store, initialHeight)

resp, err := appConnConsensus.FinalizeBlock(context.TODO(), &abci.RequestFinalizeBlock{
Hash: block.Hash(),
Expand Down

0 comments on commit d4708b4

Please sign in to comment.