@@ -683,13 +683,13 @@ proc getState(
683683
684684proc containsState * (
685685 db: BeaconChainDB , cfg: RuntimeConfig , block_root: Eth2Digest ,
686- slots: Slice [Slot ]): bool =
686+ slots: Slice [Slot ], legacy = true ): bool =
687687 var slot = slots.b
688688 while slot >= slots.a:
689689 let state_root = db.getStateRoot (block_root, slot)
690690 if state_root.isSome () and
691691 db.containsState (
692- cfg.consensusForkAtEpoch (slot.epoch), state_root.get ()):
692+ cfg.consensusForkAtEpoch (slot.epoch), state_root.get (), legacy ):
693693 return true
694694
695695 if slot == slots.a: # avoid underflow at genesis
@@ -2192,7 +2192,10 @@ proc pruneHistory*(dag: ChainDAGRef, startup = false) =
21922192 var first = true
21932193 while cur.isSome ():
21942194 let bs = cur.get ()
2195- if dag.db.containsState (dag.cfg, bs.bid.root, bs.slot.. bs.slot):
2195+ # We don't delete legacy states because the legacy database is openend
2196+ # in read-only and slow to delete from due to its sub-optimal structure
2197+ if dag.db.containsState (
2198+ dag.cfg, bs.bid.root, bs.slot.. bs.slot, legacy = first):
21962199 if first:
21972200 # We leave the state on the prune horizon intact and update the tail
21982201 # to point to this state, indicating the new point in time from
@@ -2252,17 +2255,21 @@ proc pruneHistory*(dag: ChainDAGRef, startup = false) =
22522255 # Once during start, we'll clear all "old fork" data - this ensures we get
22532256 # rid of any leftover junk in the tables - we do so after linear pruning
22542257 # so as to "mostly" clean up the phase0 tables as well (which cannot be
2255- # pruned easily by fork)
2258+ # pruned easily by fork) - one fork at a time, so as not to take too long
22562259
22572260 let stateFork = dag.cfg.consensusForkAtEpoch (dag.tail.slot.epoch)
2261+ var clearedStates = false
22582262 if stateFork > ConsensusFork .Phase0 :
22592263 for fork in ConsensusFork .Phase0 ..< stateFork:
2260- dag.db.clearStates (fork)
2264+ if dag.db.clearStates (fork):
2265+ clearedStates = true
2266+ break
22612267
22622268 let blockFork = dag.cfg.consensusForkAtEpoch (blockHorizon.epoch)
2263- if blockFork > ConsensusFork .Phase0 :
2269+ if not clearedStates and blockFork > ConsensusFork .Phase0 :
22642270 for fork in ConsensusFork .Phase0 ..< blockFork:
2265- dag.db.clearBlocks (fork)
2271+ if dag.db.clearBlocks (fork):
2272+ break
22662273
22672274proc loadExecutionBlockHash * (dag: ChainDAGRef , bid: BlockId ): Eth2Digest =
22682275 if dag.cfg.consensusForkAtEpoch (bid.slot.epoch) < ConsensusFork .Bellatrix :
0 commit comments