From d9bff54d570aff49ce7ee78ada9a4eed6fb21a25 Mon Sep 17 00:00:00 2001 From: Jacek Sieka Date: Sat, 24 Jun 2023 16:14:28 +0200 Subject: [PATCH] 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 --- beacon_chain/beacon_chain_db.nim | 15 ++++--------- .../consensus_object_pools/blockchain_dag.nim | 21 ++++++++++++------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/beacon_chain/beacon_chain_db.nim b/beacon_chain/beacon_chain_db.nim index 9652bbb03..97b913f48 100644 --- a/beacon_chain/beacon_chain_db.nim +++ b/beacon_chain/beacon_chain_db.nim @@ -885,18 +885,11 @@ proc delBlock*(db: BeaconChainDB, fork: ConsensusFork, key: Eth2Digest): bool = proc delState*(db: BeaconChainDB, fork: ConsensusFork, key: Eth2Digest) = discard db.statesNoVal[fork].del(key.data).expectDb() -proc clearBlocks*(db: BeaconChainDB, fork: ConsensusFork) = - discard db.blocks[fork].clear().expectDb() +proc clearBlocks*(db: BeaconChainDB, fork: ConsensusFork): bool = + db.blocks[fork].clear().expectDb() -proc clearStates*(db: BeaconChainDB, fork: ConsensusFork) = - discard db.statesNoVal[fork].clear().expectDb() - -proc delKeyValue*(db: BeaconChainDB, key: array[1, byte]) = - discard db.keyValues.del(key).expectDb() - discard db.v0.backend.del(key).expectDb() - -proc delKeyValue*(db: BeaconChainDB, key: DbKeyKind) = - db.delKeyValue(subkey(key)) +proc clearStates*(db: BeaconChainDB, fork: ConsensusFork): bool = + db.statesNoVal[fork].clear().expectDb() proc delStateRoot*(db: BeaconChainDB, root: Eth2Digest, slot: Slot) = discard db.stateRoots.del(stateRootKey(root, slot)).expectDb() diff --git a/beacon_chain/consensus_object_pools/blockchain_dag.nim b/beacon_chain/consensus_object_pools/blockchain_dag.nim index 9b2081f40..7d8f89639 100644 --- a/beacon_chain/consensus_object_pools/blockchain_dag.nim +++ b/beacon_chain/consensus_object_pools/blockchain_dag.nim @@ -683,13 +683,13 @@ proc getState( proc containsState*( db: BeaconChainDB, cfg: RuntimeConfig, block_root: Eth2Digest, - slots: Slice[Slot]): bool = + slots: Slice[Slot], legacy = true): bool = var slot = slots.b while slot >= slots.a: let state_root = db.getStateRoot(block_root, slot) if state_root.isSome() and db.containsState( - cfg.consensusForkAtEpoch(slot.epoch), state_root.get()): + cfg.consensusForkAtEpoch(slot.epoch), state_root.get(), legacy): return true if slot == slots.a: # avoid underflow at genesis @@ -2192,7 +2192,10 @@ proc pruneHistory*(dag: ChainDAGRef, startup = false) = var first = true while cur.isSome(): let bs = cur.get() - if dag.db.containsState(dag.cfg, bs.bid.root, bs.slot..bs.slot): + # We don't delete legacy states because the legacy database is openend + # in read-only and slow to delete from due to its sub-optimal structure + if dag.db.containsState( + dag.cfg, bs.bid.root, bs.slot..bs.slot, legacy = first): if first: # We leave the state on the prune horizon intact and update the tail # to point to this state, indicating the new point in time from @@ -2252,17 +2255,21 @@ proc pruneHistory*(dag: ChainDAGRef, startup = false) = # Once during start, we'll clear all "old fork" data - this ensures we get # rid of any leftover junk in the tables - we do so after linear pruning # so as to "mostly" clean up the phase0 tables as well (which cannot be - # pruned easily by fork) + # pruned easily by fork) - one fork at a time, so as not to take too long let stateFork = dag.cfg.consensusForkAtEpoch(dag.tail.slot.epoch) + var clearedStates = false if stateFork > ConsensusFork.Phase0: for fork in ConsensusFork.Phase0.. ConsensusFork.Phase0: + if not clearedStates and blockFork > ConsensusFork.Phase0: for fork in ConsensusFork.Phase0..