Skip to content

Commit 17e74c0

Browse files
arnetheduckzah
authored andcommitted
don't check legacy tables for pruning (#5116)
These tables can't be deleted from (read-only) and would be too slow to delete from anyway due to the inefficient storage format in use. * slow down startup clearing too * remove unused del function
1 parent 1bc9f3a commit 17e74c0

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

beacon_chain/beacon_chain_db.nim

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -885,18 +885,11 @@ proc delBlock*(db: BeaconChainDB, fork: ConsensusFork, key: Eth2Digest): bool =
885885
proc delState*(db: BeaconChainDB, fork: ConsensusFork, key: Eth2Digest) =
886886
discard db.statesNoVal[fork].del(key.data).expectDb()
887887

888-
proc clearBlocks*(db: BeaconChainDB, fork: ConsensusFork) =
889-
discard db.blocks[fork].clear().expectDb()
888+
proc clearBlocks*(db: BeaconChainDB, fork: ConsensusFork): bool =
889+
db.blocks[fork].clear().expectDb()
890890

891-
proc clearStates*(db: BeaconChainDB, fork: ConsensusFork) =
892-
discard db.statesNoVal[fork].clear().expectDb()
893-
894-
proc delKeyValue*(db: BeaconChainDB, key: array[1, byte]) =
895-
discard db.keyValues.del(key).expectDb()
896-
discard db.v0.backend.del(key).expectDb()
897-
898-
proc delKeyValue*(db: BeaconChainDB, key: DbKeyKind) =
899-
db.delKeyValue(subkey(key))
891+
proc clearStates*(db: BeaconChainDB, fork: ConsensusFork): bool =
892+
db.statesNoVal[fork].clear().expectDb()
900893

901894
proc delStateRoot*(db: BeaconChainDB, root: Eth2Digest, slot: Slot) =
902895
discard db.stateRoots.del(stateRootKey(root, slot)).expectDb()

beacon_chain/consensus_object_pools/blockchain_dag.nim

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -683,13 +683,13 @@ proc getState(
683683

684684
proc 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

22672274
proc loadExecutionBlockHash*(dag: ChainDAGRef, bid: BlockId): Eth2Digest =
22682275
if dag.cfg.consensusForkAtEpoch(bid.slot.epoch) < ConsensusFork.Bellatrix:

0 commit comments

Comments
 (0)