Skip to content

Commit 8eaee8d

Browse files
committed
Sketch finalization migration
1 parent d923d90 commit 8eaee8d

File tree

4 files changed

+54
-22
lines changed

4 files changed

+54
-22
lines changed

beacon_node/beacon_chain/src/migrate.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -709,10 +709,12 @@ impl<E: EthSpec, Hot: ItemStore<E>, Cold: ItemStore<E>> BackgroundMigrator<E, Ho
709709
]
710710
})
711711
.chain(abandoned_states.into_iter().flat_map(|(slot, state_hash)| {
712-
[
713-
StoreOp::DeleteState(state_hash.into(), Some(slot)),
714-
StoreOp::DeleteStateHotDiff(state_hash.into()),
715-
]
712+
[StoreOp::DeleteState {
713+
state_root: state_hash.into(),
714+
state_slot: Some(slot),
715+
// Abandoned forks will never be used, so we can safely delete the diffs
716+
prune_hot_diff: true,
717+
}]
716718
}))
717719
.collect();
718720

beacon_node/store/src/garbage_collection.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ where
2020
self.iter_temporary_state_roots()
2121
.try_fold(vec![], |mut ops, state_root| {
2222
let state_root = state_root?;
23-
ops.push(StoreOp::DeleteState(state_root, None));
23+
ops.push(StoreOp::DeleteState {
24+
state_root,
25+
state_slot: None,
26+
// This states will never be used, safe to delete the hot diffs
27+
prune_hot_diff: true,
28+
});
2429
Result::<_, Error>::Ok(ops)
2530
})?;
2631

beacon_node/store/src/hot_cold_store.rs

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,11 +1051,17 @@ impl<E: EthSpec, Hot: ItemStore<E>, Cold: ItemStore<E>> HotColdDB<E, Hot, Cold>
10511051
/// than the split point. You shouldn't delete states from the finalized portion of the chain
10521052
/// (which are frozen, and won't be deleted), or valid descendents of the finalized checkpoint
10531053
/// (which will be deleted by this function but shouldn't be).
1054-
pub fn delete_state(&self, state_root: &Hash256, slot: Slot) -> Result<(), Error> {
1055-
self.do_atomically_with_block_and_blobs_cache(vec![StoreOp::DeleteState(
1056-
*state_root,
1057-
Some(slot),
1058-
)])
1054+
pub fn delete_state(
1055+
&self,
1056+
state_root: &Hash256,
1057+
slot: Slot,
1058+
prune_hot_diff: bool,
1059+
) -> Result<(), Error> {
1060+
self.do_atomically_with_block_and_blobs_cache(vec![StoreOp::DeleteState {
1061+
state_root: *state_root,
1062+
state_slot: Some(slot),
1063+
prune_hot_diff,
1064+
}])
10591065
}
10601066

10611067
pub fn forwards_block_roots_iterator(
@@ -1196,7 +1202,11 @@ impl<E: EthSpec, Hot: ItemStore<E>, Cold: ItemStore<E>> HotColdDB<E, Hot, Cold>
11961202
}
11971203
}
11981204

1199-
StoreOp::DeleteState(state_root, slot) => {
1205+
StoreOp::DeleteState {
1206+
state_root,
1207+
state_slot: slot,
1208+
prune_hot_diff,
1209+
} => {
12001210
// Delete the hot state summary.
12011211
let state_summary_key =
12021212
get_key_for_col(DBColumn::BeaconStateSummary.into(), state_root.as_slice());
@@ -1211,6 +1221,12 @@ impl<E: EthSpec, Hot: ItemStore<E>, Cold: ItemStore<E>> HotColdDB<E, Hot, Cold>
12111221
key_value_batch.push(KeyValueStoreOp::DeleteKey(state_temp_key));
12121222

12131223
// TODO(hdiff): Should delete the diff under this state root if any
1224+
if prune_hot_diff {
1225+
key_value_batch.push(KeyValueStoreOp::DeleteKey(get_key_for_col(
1226+
DBColumn::BeaconStateHotDiff.into(),
1227+
state_root.as_slice(),
1228+
)));
1229+
}
12141230

12151231
// TODO(hdiff): Review under HDiff
12161232
if slot.map_or(true, |slot| slot % E::slots_per_epoch() == 0) {
@@ -1220,10 +1236,6 @@ impl<E: EthSpec, Hot: ItemStore<E>, Cold: ItemStore<E>> HotColdDB<E, Hot, Cold>
12201236
}
12211237
}
12221238

1223-
StoreOp::DeleteStateHotDiff(_) => {
1224-
todo!();
1225-
}
1226-
12271239
StoreOp::DeleteExecutionPayload(block_root) => {
12281240
let key = get_key_for_col(DBColumn::ExecPayload.into(), block_root.as_slice());
12291241
key_value_batch.push(KeyValueStoreOp::DeleteKey(key));
@@ -1372,12 +1384,10 @@ impl<E: EthSpec, Hot: ItemStore<E>, Cold: ItemStore<E>> HotColdDB<E, Hot, Cold>
13721384
self.state_cache.lock().delete_block_states(&block_root);
13731385
}
13741386

1375-
StoreOp::DeleteState(state_root, _) => {
1387+
StoreOp::DeleteState { state_root, .. } => {
13761388
self.state_cache.lock().delete_state(&state_root)
13771389
}
13781390

1379-
StoreOp::DeleteStateHotDiff(_) => todo!(),
1380-
13811391
StoreOp::DeleteBlobs(_) => (),
13821392

13831393
StoreOp::DeleteDataColumns(_, _) => (),
@@ -3070,7 +3080,12 @@ impl<E: EthSpec, Hot: ItemStore<E>, Cold: ItemStore<E>> HotColdDB<E, Hot, Cold>
30703080
"slot" => summary.slot,
30713081
"reason" => reason,
30723082
);
3073-
state_delete_batch.push(StoreOp::DeleteState(state_root, Some(summary.slot)));
3083+
state_delete_batch.push(StoreOp::DeleteState {
3084+
state_root,
3085+
state_slot: Some(summary.slot),
3086+
// TODO(hdiff): The logic here is duplicated from `migrate_database`
3087+
prune_hot_diff: false,
3088+
});
30743089
}
30753090
}
30763091
}
@@ -3172,7 +3187,14 @@ pub fn migrate_database<E: EthSpec, Hot: ItemStore<E>, Cold: ItemStore<E>>(
31723187

31733188
// Delete the old summary, and the full state if we lie on an epoch boundary.
31743189
if !required_finalized_diff_state_roots.contains(&state_root) {
3175-
hot_db_ops.push(StoreOp::DeleteState(state_root, Some(slot)));
3190+
hot_db_ops.push(StoreOp::DeleteState {
3191+
state_root,
3192+
state_slot: Some(slot),
3193+
// TODO(hdiff): must not delete *all* finalized hdiffs. Instead, keep
3194+
// the more recent diff of each layer including the snapshot.
3195+
// Implement a routine somewhere to figure out which diffs should be kept
3196+
prune_hot_diff: false,
3197+
});
31763198
}
31773199

31783200
// Do not try to store states if a restore point is yet to be stored, or will never be

beacon_node/store/src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,11 @@ pub enum StoreOp<'a, E: EthSpec> {
241241
DeleteBlock(Hash256),
242242
DeleteBlobs(Hash256),
243243
DeleteDataColumns(Hash256, Vec<ColumnIndex>),
244-
DeleteState(Hash256, Option<Slot>),
245-
DeleteStateHotDiff(Hash256),
244+
DeleteState {
245+
state_root: Hash256,
246+
state_slot: Option<Slot>,
247+
prune_hot_diff: bool,
248+
},
246249
DeleteExecutionPayload(Hash256),
247250
DeleteSyncCommitteeBranch(Hash256),
248251
KeyValueOp(KeyValueStoreOp),

0 commit comments

Comments
 (0)