From b85ab1833e2e83328110d01d34f11b92fc477383 Mon Sep 17 00:00:00 2001 From: Giulio rebuffo Date: Sat, 3 Aug 2024 21:38:53 +0200 Subject: [PATCH] Caplin: Fixed pruning algorithm (#11472) Co-authored-by: Kewei --- cl/phase1/forkchoice/fork_graph/fork_graph_disk.go | 12 ++++-------- cl/phase1/forkchoice/utils.go | 4 ++-- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/cl/phase1/forkchoice/fork_graph/fork_graph_disk.go b/cl/phase1/forkchoice/fork_graph/fork_graph_disk.go index 00ce755a447..2899deab2a6 100644 --- a/cl/phase1/forkchoice/fork_graph/fork_graph_disk.go +++ b/cl/phase1/forkchoice/fork_graph/fork_graph_disk.go @@ -417,28 +417,24 @@ func (f *forkGraphDisk) MarkHeaderAsInvalid(blockRoot libcommon.Hash) { } func (f *forkGraphDisk) hasBeaconState(blockRoot libcommon.Hash) bool { - _, err := f.fs.Stat(getBeaconStateFilename(blockRoot)) - return err == nil + exists, err := afero.Exists(f.fs, getBeaconStateFilename(blockRoot)) + return err == nil && exists } func (f *forkGraphDisk) Prune(pruneSlot uint64) (err error) { - pruneSlot -= f.beaconCfg.SlotsPerEpoch * 2 oldRoots := make([]libcommon.Hash, 0, f.beaconCfg.SlotsPerEpoch) highestStoredBeaconStateSlot := uint64(0) f.blocks.Range(func(key, value interface{}) bool { hash := key.(libcommon.Hash) signedBlock := value.(*cltypes.SignedBeaconBlock) - if signedBlock.Block.Slot < highestStoredBeaconStateSlot { - return true - } - if f.hasBeaconState(hash) { + if f.hasBeaconState(hash) && highestStoredBeaconStateSlot < signedBlock.Block.Slot { highestStoredBeaconStateSlot = signedBlock.Block.Slot } if signedBlock.Block.Slot >= pruneSlot { return true } - oldRoots = append(oldRoots, hash) + oldRoots = append(oldRoots, hash) return true }) if pruneSlot >= highestStoredBeaconStateSlot { diff --git a/cl/phase1/forkchoice/utils.go b/cl/phase1/forkchoice/utils.go index 5bd7eca9e09..b30d7966bc7 100644 --- a/cl/phase1/forkchoice/utils.go +++ b/cl/phase1/forkchoice/utils.go @@ -62,8 +62,8 @@ func (f *ForkChoiceStore) onNewFinalized(newFinalized solid.Checkpoint) { } return true }) - - f.forkGraph.Prune(newFinalized.Epoch() * f.beaconCfg.SlotsPerEpoch) + slotToPrune := ((newFinalized.Epoch() - 1) * f.beaconCfg.SlotsPerEpoch) - 1 + f.forkGraph.Prune(slotToPrune) } // updateCheckpoints updates the justified and finalized checkpoints if new checkpoints have higher epochs.