Skip to content

Commit

Permalink
fix: prune uses the latest block height as the target by default (bnb…
Browse files Browse the repository at this point in the history
  • Loading branch information
welkin22 authored Jan 30, 2024
1 parent 371bc44 commit 9114a97
Showing 1 changed file with 9 additions and 16 deletions.
25 changes: 9 additions & 16 deletions core/state/pruner/pruner.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,24 +274,17 @@ func (p *Pruner) Prune(root common.Hash) error {
if stateBloomRoot != (common.Hash{}) {
return RecoverPruning(p.config.Datadir, p.db, p.config.Cachedir, p.triesInMemory)
}
// If the target state root is not specified, use the HEAD-(n-1) as the
// If the target state root is not specified, use the HEAD as the
// target. The reason for picking it is:
// - in most of the normal cases, the related state is available
// - the probability of this layer being reorg is very low
// - At opBNB, the possibility of reorg is very small, we don't need to roll back the block height,
// we just need to wait for the block height to be finalized during pruning.
// - Rolling back the block height currently causes the node to get stuck after startup.
// Instead of fixing the complex logic that causes the node to get stuck,
// we choose a more gentle way to solve the problem.
var layers []snapshot.Snapshot
if root == (common.Hash{}) {
// Retrieve all snapshot layers from the current HEAD.
// In theory there are n difflayers + 1 disk layer present,
// so n diff layers are expected to be returned.
layers = p.snaptree.Snapshots(p.chainHeader.Root, int(p.triesInMemory), true)
if len(layers) != int(p.triesInMemory) {
// Reject if the accumulated diff layers are less than n. It
// means in most of normal cases, there is no associated state
// with bottom-most diff layer.
return fmt.Errorf("snapshot not old enough yet: need %d more blocks", int(p.triesInMemory)-len(layers))
}
// Use the bottom-most diff layer as the target
root = layers[len(layers)-1].Root()
// Use the latest block header root as the target
root = p.chainHeader.Root
}
// Ensure the root is really present. The weak assumption
// is the presence of root can indicate the presence of the
Expand Down Expand Up @@ -327,7 +320,7 @@ func (p *Pruner) Prune(root common.Hash) error {
if len(layers) > 0 {
log.Info("Selecting bottom-most difflayer as the pruning target", "root", root, "height", p.chainHeader.Number.Uint64()-(p.triesInMemory-1))
} else {
log.Info("Selecting user-specified state as the pruning target", "root", root)
log.Info("Selecting user-specified state as the pruning target", "root", root, "height", p.chainHeader.Number.Uint64())
}
}
// Before start the pruning, delete the clean trie cache first.
Expand Down

0 comments on commit 9114a97

Please sign in to comment.