Skip to content

Commit

Permalink
Caplin: Fixed pruning algorithm (#11472)
Browse files Browse the repository at this point in the history
Co-authored-by: Kewei <[email protected]>
  • Loading branch information
Giulio2002 and domiwei authored Aug 3, 2024
1 parent ffbac7f commit b85ab18
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 10 deletions.
12 changes: 4 additions & 8 deletions cl/phase1/forkchoice/fork_graph/fork_graph_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions cl/phase1/forkchoice/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit b85ab18

Please sign in to comment.