Skip to content

Commit 5fb55be

Browse files
committed
Fix migration of dense diffs
1 parent 9acb1cf commit 5fb55be

File tree

4 files changed

+45
-15
lines changed

4 files changed

+45
-15
lines changed

beacon_node/beacon_chain/src/beacon_chain.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6783,6 +6783,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
67836783
}
67846784

67856785
/// As for `chain_dump` but dumping only the portion of the chain newer than `from_slot`.
6786+
#[allow(clippy::type_complexity)]
67866787
pub fn chain_dump_from_slot(
67876788
&self,
67886789
from_slot: Slot,

beacon_node/beacon_chain/src/schema_change/migration_schema_v24.rs

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,32 @@ pub fn store_full_state_v22<E: EthSpec>(
3636
Ok(())
3737
}
3838

39+
/// Fetch a V22 state from the database either as a full state or using block replay.
40+
pub fn get_state_v22<T: BeaconChainTypes>(
41+
db: &Arc<HotColdDB<T::EthSpec, T::HotStore, T::ColdStore>>,
42+
state_root: &Hash256,
43+
spec: &ChainSpec,
44+
) -> Result<Option<BeaconState<T::EthSpec>>, Error> {
45+
let Some(summary) = db.get_item::<HotStateSummaryV22>(state_root)? else {
46+
return Ok(None);
47+
};
48+
let Some(base_state) =
49+
get_full_state_v22(&db.hot_db, &summary.epoch_boundary_state_root, spec)?
50+
else {
51+
return Ok(None);
52+
};
53+
// Loading hot states via block replay doesn't care about the schema version, so we can use
54+
// the DB's current method for this.
55+
let update_cache = false;
56+
db.load_hot_state_using_replay(
57+
base_state,
58+
summary.slot,
59+
summary.latest_block_root,
60+
update_cache,
61+
)
62+
.map(Some)
63+
}
64+
3965
pub fn get_full_state_v22<KV: KeyValueStore<E>, E: EthSpec>(
4066
db: &KV,
4167
state_root: &Hash256,
@@ -139,6 +165,20 @@ pub struct HotStateSummaryV22 {
139165
epoch_boundary_state_root: Hash256,
140166
}
141167

168+
impl StoreItem for HotStateSummaryV22 {
169+
fn db_column() -> DBColumn {
170+
DBColumn::BeaconStateSummary
171+
}
172+
173+
fn as_store_bytes(&self) -> Vec<u8> {
174+
self.as_ssz_bytes()
175+
}
176+
177+
fn from_store_bytes(bytes: &[u8]) -> Result<Self, Error> {
178+
Ok(Self::from_ssz_bytes(bytes)?)
179+
}
180+
}
181+
142182
pub fn upgrade_to_v24<T: BeaconChainTypes>(
143183
db: Arc<HotColdDB<T::EthSpec, T::HotStore, T::ColdStore>>,
144184
) -> Result<Vec<KeyValueStoreOp>, Error> {
@@ -319,19 +359,8 @@ pub fn upgrade_to_v24<T: BeaconChainTypes>(
319359

320360
match storage_strategy {
321361
StorageStrategy::DiffFrom(_) | StorageStrategy::Snapshot => {
322-
// We choose to not compute states during the epoch with block replay for
323-
// simplicity. Therefore we can only support a hot heriarchy config where the
324-
// lowest layer is >= log2(SLOTS_PER_EPOCH).
325-
// FIXME(tree-states): we need to remove this restriction.
326-
if slot % T::EthSpec::slots_per_epoch() != 0 {
327-
return Err(Error::MigrationError(format!(
328-
"Hot hierarchy config lowest value must be >= {}",
329-
T::EthSpec::slots_per_epoch().trailing_zeros()
330-
)));
331-
}
332-
333-
// Load the full state and re-store it as a snapshot or diff.
334-
let state = get_full_state_v22(&db.hot_db, &state_root, &db.spec)?
362+
// Load the state and re-store it as a snapshot or diff.
363+
let state = get_state_v22::<T>(&db, &state_root, &db.spec)?
335364
.ok_or(Error::MissingState(state_root))?;
336365

337366
// Store immediately so that future diffs can load and diff from it.

beacon_node/beacon_chain/tests/store_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3681,7 +3681,7 @@ async fn ancestor_state_root_prior_to_split() {
36813681
store
36823682
.load_hot_state_summary(&ancestor_state_root)
36833683
.unwrap()
3684-
.expect(&format!(
3684+
.unwrap_or_else(|| panic!(
36853685
"no summary found for {ancestor_state_root:?} (slot {ancestor_slot})"
36863686
))
36873687
.slot,

beacon_node/store/src/hot_cold_store.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1791,7 +1791,7 @@ impl<E: EthSpec, Hot: ItemStore<E>, Cold: ItemStore<E>> HotColdDB<E, Hot, Cold>
17911791
}
17921792
}
17931793

1794-
fn load_hot_state_using_replay(
1794+
pub fn load_hot_state_using_replay(
17951795
&self,
17961796
base_state: BeaconState<E>,
17971797
slot: Slot,

0 commit comments

Comments
 (0)