Skip to content

Commit 37be9ae

Browse files
committed
Tweaks in prune_hot_db.
1 parent f6786eb commit 37be9ae

File tree

2 files changed

+23
-22
lines changed

2 files changed

+23
-22
lines changed

beacon_node/beacon_chain/src/migrate.rs

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ impl<E: EthSpec, Hot: ItemStore<E>, Cold: ItemStore<E>> BackgroundMigrator<E, Ho
454454
/// space.
455455
fn prune_hot_db(
456456
store: Arc<HotColdDB<E, Hot, Cold>>,
457-
new_finalized_state_hash: Hash256,
457+
new_finalized_state_root: Hash256,
458458
new_finalized_state: &BeaconState<E>,
459459
new_finalized_checkpoint: Checkpoint,
460460
log: &Logger,
@@ -482,7 +482,7 @@ impl<E: EthSpec, Hot: ItemStore<E>, Cold: ItemStore<E>> BackgroundMigrator<E, Ho
482482
log,
483483
"Starting database pruning";
484484
"new_finalized_checkpoint" => ?new_finalized_checkpoint,
485-
"new_finalized_state_hash" => ?new_finalized_state_hash,
485+
"new_finalized_state_root" => ?new_finalized_state_root,
486486
);
487487

488488
let state_summaries_dag = {
@@ -527,8 +527,8 @@ impl<E: EthSpec, Hot: ItemStore<E>, Cold: ItemStore<E>> BackgroundMigrator<E, Ho
527527
.map_err(|e| PruningError::SummariesDagError("creating StateSumariesDAG", e))?
528528
};
529529

530-
// To debug faulty trees log unexpected that have more than one root. These trees may not
531-
// result in an error, as may not be queried in the codepaths below.
530+
// To debug faulty trees log if we unexpectedly have more than one root. These trees may not
531+
// result in an error, as they may not be queried in the codepaths below.
532532
let state_summaries_dag_roots = state_summaries_dag.tree_roots();
533533
if state_summaries_dag_roots.len() > 1 {
534534
warn!(
@@ -538,14 +538,14 @@ impl<E: EthSpec, Hot: ItemStore<E>, Cold: ItemStore<E>> BackgroundMigrator<E, Ho
538538
);
539539
}
540540

541-
// `new_finalized_state_hash` is the *state at the slot of the finalized epoch*,
541+
// `new_finalized_state_root` is the *state at the slot of the finalized epoch*,
542542
// rather than the state of the latest finalized block. These two values will only
543543
// differ when the first slot of the finalized epoch is a skip slot.
544544
let finalized_and_descendant_state_roots_of_finalized_checkpoint =
545545
HashSet::<Hash256>::from_iter(
546-
std::iter::once(new_finalized_state_hash).chain(
546+
std::iter::once(new_finalized_state_root).chain(
547547
state_summaries_dag
548-
.descendants_of(&new_finalized_state_hash)
548+
.descendants_of(&new_finalized_state_root)
549549
.map_err(|e| {
550550
PruningError::SummariesDagError("state summaries descendants_of", e)
551551
})?,
@@ -554,7 +554,8 @@ impl<E: EthSpec, Hot: ItemStore<E>, Cold: ItemStore<E>> BackgroundMigrator<E, Ho
554554

555555
// Collect all `latest_block_roots` of the
556556
// finalized_and_descendant_state_roots_of_finalized_checkpoint set. Includes the finalized
557-
// block as `new_finalized_state_hash` always has a latest block root the finalized block.
557+
// block as `new_finalized_state_root` always has a latest block root equal to the finalized
558+
// block.
558559
let finalized_and_descendant_block_roots_of_finalized_checkpoint =
559560
HashSet::<Hash256>::from_iter(
560561
state_summaries_dag
@@ -573,7 +574,7 @@ impl<E: EthSpec, Hot: ItemStore<E>, Cold: ItemStore<E>> BackgroundMigrator<E, Ho
573574

574575
// Note: ancestors_of includes the finalized state root
575576
let newly_finalized_state_summaries = state_summaries_dag
576-
.ancestors_of(new_finalized_state_hash)
577+
.ancestors_of(new_finalized_state_root)
577578
.map_err(|e| PruningError::SummariesDagError("state summaries ancestors_of", e))?;
578579
let newly_finalized_state_roots = newly_finalized_state_summaries
579580
.iter()
@@ -629,30 +630,29 @@ impl<E: EthSpec, Hot: ItemStore<E>, Cold: ItemStore<E>> BackgroundMigrator<E, Ho
629630
}
630631
}
631632

632-
for block in state_summaries_dag.iter_blocks() {
633-
let (block_root, slot) = block;
634-
633+
for (block_root, slot) in state_summaries_dag.iter_blocks() {
635634
// Blocks both finalized and unfinalized are in the same DB column. We must only
636635
// prune blocks from abandoned forks. Note that block pruning and state pruning differ.
637636
// The blocks DB column is shared for hot and cold data, while the states have different
638637
// columns. Thus, we only prune unviable blocks or from abandoned forks.
639638
let should_prune = if finalized_and_descendant_block_roots_of_finalized_checkpoint
640639
.contains(&block_root)
641640
{
642-
// Keep unfinalized blocks descendant of finalized checkpoint + finalized block itself
643-
// Note that we anchor this set on the finalied checkpoint instead of the finalized
644-
// block. A diagram above shows a relevant example.
641+
// Keep unfinalized blocks descendant of finalized checkpoint + finalized block
642+
// itself Note that we anchor this set on the finalized checkpoint instead of the
643+
// finalized block. A diagram above shows a relevant example.
645644
false
646-
} else if newly_finalized_blocks.contains(&block) {
645+
} else if newly_finalized_blocks.contains(&(block_root, slot)) {
647646
// Keep recently finalized blocks
648647
false
649648
} else if slot < newly_finalized_states_min_slot {
650649
// Keep recently finalized blocks that we know are canonical. Blocks with slots <
651650
// that `newly_finalized_blocks_min_slot` we don't have canonical information so we
652651
// assume they are part of the finalized pruned chain
653652
//
654-
// Pruning those risks breaking the DB by deleting canonical blocks once the HDiff
655-
// grid advances. If the pruning routine is correct this condition should never hit.
653+
// Pruning these would risk breaking the DB by deleting canonical blocks once the
654+
// HDiff grid advances. If the pruning routine is correct this condition should
655+
// never be hit.
656656
false
657657
} else {
658658
// Everything else, prune
@@ -685,10 +685,10 @@ impl<E: EthSpec, Hot: ItemStore<E>, Cold: ItemStore<E>> BackgroundMigrator<E, Ho
685685
// Don't log the full `states_to_prune` in the log statement above as it can result in a
686686
// single log line of +1Kb and break logging setups.
687687
for block_root in &blocks_to_prune {
688-
debug!(log, "block to prune"; "block_root" => ?block_root);
688+
debug!(log, "Pruning block"; "block_root" => ?block_root);
689689
}
690690
for (slot, state_root) in &states_to_prune {
691-
debug!(log, "state to prune"; "state_root" => ?state_root, "slot" => slot);
691+
debug!(log, "Pruning state"; "state_root" => ?state_root, "slot" => slot);
692692
}
693693

694694
let mut batch: Vec<StoreOp<E>> = blocks_to_prune
@@ -709,6 +709,7 @@ impl<E: EthSpec, Hot: ItemStore<E>, Cold: ItemStore<E>> BackgroundMigrator<E, Ho
709709

710710
// Prune sync committee branches of non-checkpoint canonical finalized blocks
711711
Self::prune_non_checkpoint_sync_committee_branches(&newly_finalized_blocks, &mut batch);
712+
712713
// Prune all payloads of the canonical finalized blocks
713714
if store.get_config().prune_payloads {
714715
Self::prune_finalized_payloads(&newly_finalized_blocks, &mut batch);

beacon_node/beacon_chain/src/test_utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -926,10 +926,10 @@ where
926926
let fork_choice = self.chain.canonical_head.fork_choice_read_lock();
927927
if heads.is_empty() {
928928
let nodes = &fork_choice.proto_array().core_proto_array().nodes;
929-
panic!("Expected to known head block root {head_block_root:?}, but heads is empty. Nodes: {nodes:#?}");
929+
panic!("Expected to know head block root {head_block_root:?}, but heads is empty. Nodes: {nodes:#?}");
930930
} else {
931931
panic!(
932-
"Expected to known head block root {head_block_root:?}, known heads {heads:#?}"
932+
"Expected to know head block root {head_block_root:?}, known heads {heads:#?}"
933933
);
934934
}
935935
}

0 commit comments

Comments
 (0)