Skip to content

Commit f00c799

Browse files
committed
core/state/pruner: capping the snapshot after pruning
1 parent fcb716f commit f00c799

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

core/state/pruner/pruner.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,9 @@ func (p *Pruner) Prune(root common.Hash) error {
240240
}
241241
}
242242
if !found {
243+
if len(layers) > 0 {
244+
return errors.New("no snapshot paired state")
245+
}
243246
return fmt.Errorf("associated state[%x] is not present", root)
244247
}
245248
} else {
@@ -250,6 +253,9 @@ func (p *Pruner) Prune(root common.Hash) error {
250253
}
251254
}
252255
// Before start the pruning, delete the clean trie cache first.
256+
// It's necessary otherwise in the next restart we will hit the
257+
// deleted state root in the "clean cache" so that the incomplete
258+
// state is picked for usage.
253259
os.RemoveAll(p.cachePath)
254260
log.Info("Deleted trie clean cache", "path", p.cachePath)
255261

@@ -265,6 +271,21 @@ func (p *Pruner) Prune(root common.Hash) error {
265271
if err := prune(p.db, p.stateBloom, start); err != nil {
266272
return err
267273
}
274+
// Pruning is done, now drop the "useless" layers from the snapshot.
275+
// Firstly, flushing the target layer into the disk. After that all
276+
// diff layers below the target will all be merged into the disk.
277+
p.snaptree.Cap(root, 0)
278+
279+
// Secondly, flushing the snapshot journal into the disk. All diff
280+
// layers upon are dropped silently. Eventually the entire snapshot
281+
// tree is converted into a single disk layer with the pruning target
282+
// as the root.
283+
p.snaptree.Journal(root)
284+
285+
// Delete the state bloom, it marks the entire pruning procedure is
286+
// finished. If any crashes or manual exit happens before this,
287+
// `RecoverPruning` will pick it up in the next restarts to redo all
288+
// the things.
268289
os.RemoveAll(p.stateBloomPath)
269290
return nil
270291
}

0 commit comments

Comments
 (0)