Skip to content

Commit 2dd4e03

Browse files
sergio-menaandynog
authored andcommitted
perf(consensus): add simplistic block validation cache (cometbft#3070)
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 <me@andynogueira.dev>
1 parent 2cea495 commit 2dd4e03

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

state/execution.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ type BlockExecutor struct {
3636
mempool mempool.Mempool
3737
evpool EvidencePool
3838

39+
// 1-element cache of validated blocks
40+
lastValidatedBlock *types.Block
41+
3942
logger log.Logger
4043

4144
metrics *Metrics
@@ -172,9 +175,11 @@ func (blockExec *BlockExecutor) ProcessProposal(
172175
// Validation does not mutate state, but does require historical information from the stateDB,
173176
// ie. to verify evidence from a validator at an old height.
174177
func (blockExec *BlockExecutor) ValidateBlock(state State, block *types.Block) error {
175-
err := validateBlock(state, block)
176-
if err != nil {
177-
return err
178+
if !blockExec.lastValidatedBlock.HashesTo(block.Hash()) {
179+
if err := validateBlock(state, block); err != nil {
180+
return err
181+
}
182+
blockExec.lastValidatedBlock = block
178183
}
179184
return blockExec.evpool.CheckEvidence(block.Evidence.Evidence)
180185
}
@@ -195,11 +200,12 @@ func (blockExec *BlockExecutor) ApplyVerifiedBlock(
195200
func (blockExec *BlockExecutor) ApplyBlock(
196201
state State, blockID types.BlockID, block *types.Block,
197202
) (State, int64, error) {
198-
199-
if err := validateBlock(state, block); err != nil {
200-
return state, 0, ErrInvalidBlock(err)
203+
if !blockExec.lastValidatedBlock.HashesTo(block.Hash()) {
204+
if err := validateBlock(state, block); err != nil {
205+
return state, 0, ErrInvalidBlock(err)
206+
}
207+
blockExec.lastValidatedBlock = block
201208
}
202-
203209
return blockExec.applyBlock(state, blockID, block)
204210
}
205211

0 commit comments

Comments
 (0)