Skip to content

Commit

Permalink
Merge pull request #73 from osmosis-labs/bp/sergio/2854-add-validatio…
Browse files Browse the repository at this point in the history
…n-cache

perf(consensus): add simplistic block validation cache (cometbft#3070)
  • Loading branch information
ValarDragon authored May 25, 2024
2 parents 2cea495 + 2dd4e03 commit bdbed26
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions state/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ type BlockExecutor struct {
mempool mempool.Mempool
evpool EvidencePool

// 1-element cache of validated blocks
lastValidatedBlock *types.Block

logger log.Logger

metrics *Metrics
Expand Down Expand Up @@ -172,9 +175,11 @@ func (blockExec *BlockExecutor) ProcessProposal(
// Validation does not mutate state, but does require historical information from the stateDB,
// ie. to verify evidence from a validator at an old height.
func (blockExec *BlockExecutor) ValidateBlock(state State, block *types.Block) error {
err := validateBlock(state, block)
if err != nil {
return err
if !blockExec.lastValidatedBlock.HashesTo(block.Hash()) {
if err := validateBlock(state, block); err != nil {
return err
}
blockExec.lastValidatedBlock = block
}
return blockExec.evpool.CheckEvidence(block.Evidence.Evidence)
}
Expand All @@ -195,11 +200,12 @@ func (blockExec *BlockExecutor) ApplyVerifiedBlock(
func (blockExec *BlockExecutor) ApplyBlock(
state State, blockID types.BlockID, block *types.Block,
) (State, int64, error) {

if err := validateBlock(state, block); err != nil {
return state, 0, ErrInvalidBlock(err)
if !blockExec.lastValidatedBlock.HashesTo(block.Hash()) {
if err := validateBlock(state, block); err != nil {
return state, 0, ErrInvalidBlock(err)
}
blockExec.lastValidatedBlock = block
}

return blockExec.applyBlock(state, blockID, block)
}

Expand Down

0 comments on commit bdbed26

Please sign in to comment.