Skip to content

Commit 5cc266c

Browse files
committed
Clean up DB migrations
1 parent 7abbaeb commit 5cc266c

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

beacon_node/beacon_chain/src/migrate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,7 @@ impl<E: EthSpec, Hot: ItemStore<E>, Cold: ItemStore<E>> BackgroundMigrator<E, Ho
688688
debug!(log, "Pruning block"; "block_root" => ?block_root);
689689
}
690690
for (slot, state_root) in &states_to_prune {
691-
debug!(log, "Pruning state"; "state_root" => ?state_root, "slot" => slot);
691+
debug!(log, "Pruning hot state"; "state_root" => ?state_root, "slot" => slot);
692692
}
693693

694694
let mut batch: Vec<StoreOp<E>> = blocks_to_prune

beacon_node/beacon_chain/src/schema_change/migration_schema_v23.rs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,39 +19,42 @@ pub fn upgrade_to_v23<T: BeaconChainTypes>(
1919
_log: Logger,
2020
) -> Result<Vec<KeyValueStoreOp>, Error> {
2121
// Set the head-tracker to empty
22-
2322
let Some(persisted_beacon_chain_v22) =
2423
db.get_item::<PersistedBeaconChainV22>(&BEACON_CHAIN_DB_KEY)?
2524
else {
26-
// If there is no persisted beacon chain, ignore the upgrade
27-
return Ok(vec![]);
25+
return Err(Error::MigrationError(
26+
"No persisted beacon chain found in DB. Datadir could be incorrect or DB could be corrupt".to_string()
27+
));
2828
};
2929

3030
let persisted_beacon_chain = PersistedBeaconChain {
3131
genesis_block_root: persisted_beacon_chain_v22.genesis_block_root,
3232
};
3333

34-
db.put_item::<PersistedBeaconChain>(&BEACON_CHAIN_DB_KEY, &persisted_beacon_chain)?;
34+
let ops = vec![persisted_beacon_chain.as_kv_store_op(BEACON_CHAIN_DB_KEY)];
3535

36-
todo!();
36+
Ok(ops)
3737
}
3838

3939
pub fn downgrade_from_v23<T: BeaconChainTypes>(
4040
db: Arc<HotColdDB<T::EthSpec, T::HotStore, T::ColdStore>>,
4141
log: Logger,
4242
) -> Result<Vec<KeyValueStoreOp>, Error> {
43-
// recreate head-tracker from the fork-choice
44-
45-
let Some(persisted_fork_choice) = db.get_item::<PersistedForkChoice>(&FORK_CHOICE_DB_KEY)?
43+
let Some(persisted_beacon_chain) = db.get_item::<PersistedBeaconChain>(&BEACON_CHAIN_DB_KEY)?
4644
else {
47-
// Is it possible for the fork-choice to not exist on a downgrade?
48-
return Ok(vec![]);
45+
// The `PersistedBeaconChain` must exist if fork choice exists.
46+
return Err(Error::MigrationError(
47+
"No persisted beacon chain found in DB. Datadir could be incorrect or DB could be corrupt".to_string(),
48+
));
4949
};
5050

51-
let Some(persisted_beacon_chain) = db.get_item::<PersistedBeaconChain>(&BEACON_CHAIN_DB_KEY)?
51+
// Recreate head-tracker from fork choice.
52+
let Some(persisted_fork_choice) = db.get_item::<PersistedForkChoice>(&FORK_CHOICE_DB_KEY)?
5253
else {
53-
// TODO: Is it possible for persisted beacon chain to be missing if the fork choice exists?
54-
return Ok(vec![]);
54+
// Fork choice should exist if the database exists.
55+
return Err(Error::MigrationError(
56+
"No fork choice found in DB".to_string(),
57+
));
5558
};
5659

5760
let fc_store =
@@ -62,7 +65,8 @@ pub fn downgrade_from_v23<T: BeaconChainTypes>(
6265
))
6366
})?;
6467

65-
// TODO: what value to choose here?
68+
// Doesn't matter what policy we use for invalid payloads, as our head calculation just
69+
// considers descent from finalization.
6670
let reset_payload_statuses = ResetPayloadStatuses::OnlyWithInvalidPayload;
6771
let fork_choice = ForkChoice::from_persisted(
6872
persisted_fork_choice.fork_choice,
@@ -91,9 +95,9 @@ pub fn downgrade_from_v23<T: BeaconChainTypes>(
9195
},
9296
};
9397

94-
db.put_item::<PersistedBeaconChainV22>(&BEACON_CHAIN_DB_KEY, &persisted_beacon_chain_v22)?;
98+
let ops = vec![persisted_beacon_chain_v22.as_kv_store_op(BEACON_CHAIN_DB_KEY)];
9599

96-
todo!();
100+
Ok(ops)
97101
}
98102

99103
/// Helper struct that is used to encode/decode the state of the `HeadTracker` as SSZ bytes.

0 commit comments

Comments
 (0)