Skip to content

Commit

Permalink
perf(consensus): add simplistic block validation cache (cometbft#3070) (
Browse files Browse the repository at this point in the history
cometbft#78)

Closes cometbft#2854

Follow up from cometbft#2960

This PR adds a simplistic 1-element block validation cache in
`BlockExecutor`. This addresses a performance bottleneck raised as part
of cometbft#2854

---

- [ ] Tests written/updated
- [ ] Changelog entry added in `.changelog` (we use
[unclog](https://github.com/informalsystems/unclog) to manage our
changelog)
- [ ] Updated relevant documentation (`docs/` or `spec/`) and code
comments
- [ ] Title follows the [Conventional
Commits](https://www.conventionalcommits.org/en/v1.0.0/) spec

---------

Co-authored-by: Andy Nogueira <[email protected]>
(cherry picked from commit 2dd4e03)

Co-authored-by: Sergio Mena <[email protected]>
  • Loading branch information
mergify[bot] and sergio-mena authored May 28, 2024
1 parent 17609cf commit a17c8bc
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 a17c8bc

Please sign in to comment.