From a2b8c6ee69f157101e262f75e2742522ebba1442 Mon Sep 17 00:00:00 2001 From: Emilia Hane Date: Fri, 13 Jan 2023 22:09:15 +0100 Subject: [PATCH] Save fetching state for blobs pruning --- beacon_node/beacon_chain/src/beacon_chain.rs | 8 ++-- beacon_node/store/src/forwards_iter.rs | 6 +-- beacon_node/store/src/hot_cold_store.rs | 47 +++++++++++--------- 3 files changed, 32 insertions(+), 29 deletions(-) diff --git a/beacon_node/beacon_chain/src/beacon_chain.rs b/beacon_node/beacon_chain/src/beacon_chain.rs index 52390f36a6..b74f7309e2 100644 --- a/beacon_node/beacon_chain/src/beacon_chain.rs +++ b/beacon_node/beacon_chain/src/beacon_chain.rs @@ -612,10 +612,10 @@ impl BeaconChain { start_slot, end_slot, || { - ( + Ok(( head.beacon_state.clone_with_only_committee_caches(), head.beacon_block_root, - ) + )) }, &self.spec, )?; @@ -709,10 +709,10 @@ impl BeaconChain { start_slot, end_slot, || { - ( + Ok(( head.beacon_state.clone_with_only_committee_caches(), head.beacon_state_root(), - ) + )) }, &self.spec, )?; diff --git a/beacon_node/store/src/forwards_iter.rs b/beacon_node/store/src/forwards_iter.rs index 353be6bf05..a78b2b469f 100644 --- a/beacon_node/store/src/forwards_iter.rs +++ b/beacon_node/store/src/forwards_iter.rs @@ -150,7 +150,7 @@ impl<'a, E: EthSpec, F: Root, Hot: ItemStore, Cold: ItemStore> store: &'a HotColdDB, start_slot: Slot, end_slot: Option, - get_state: impl FnOnce() -> (BeaconState, Hash256), + get_state: impl FnOnce() -> Result<(BeaconState, Hash256)>, spec: &ChainSpec, ) -> Result { use HybridForwardsIterator::*; @@ -172,7 +172,7 @@ impl<'a, E: EthSpec, F: Root, Hot: ItemStore, Cold: ItemStore> if end_slot.map_or(false, |end_slot| end_slot < latest_restore_point_slot) { None } else { - Some(Box::new(get_state())) + Some(Box::new(get_state()?)) }; PreFinalization { iter, @@ -180,7 +180,7 @@ impl<'a, E: EthSpec, F: Root, Hot: ItemStore, Cold: ItemStore> } } else { PostFinalizationLazy { - continuation_data: Some(Box::new(get_state())), + continuation_data: Some(Box::new(get_state()?)), store, start_slot, } diff --git a/beacon_node/store/src/hot_cold_store.rs b/beacon_node/store/src/hot_cold_store.rs index 4d00b836a5..2c60205eb4 100644 --- a/beacon_node/store/src/hot_cold_store.rs +++ b/beacon_node/store/src/hot_cold_store.rs @@ -657,7 +657,7 @@ impl, Cold: ItemStore> HotColdDB self, start_slot, None, - || (end_state, end_block_root), + || Ok((end_state, end_block_root)), spec, ) } @@ -666,7 +666,7 @@ impl, Cold: ItemStore> HotColdDB &self, start_slot: Slot, end_slot: Slot, - get_state: impl FnOnce() -> (BeaconState, Hash256), + get_state: impl FnOnce() -> Result<(BeaconState, Hash256), Error>, spec: &ChainSpec, ) -> Result, Error> { HybridForwardsBlockRootsIterator::new(self, start_slot, Some(end_slot), get_state, spec) @@ -683,7 +683,7 @@ impl, Cold: ItemStore> HotColdDB self, start_slot, None, - || (end_state, end_state_root), + || Ok((end_state, end_state_root)), spec, ) } @@ -692,7 +692,7 @@ impl, Cold: ItemStore> HotColdDB &self, start_slot: Slot, end_slot: Slot, - get_state: impl FnOnce() -> (BeaconState, Hash256), + get_state: impl FnOnce() -> Result<(BeaconState, Hash256), Error>, spec: &ChainSpec, ) -> Result, Error> { HybridForwardsStateRootsIterator::new(self, start_slot, Some(end_slot), get_state, spec) @@ -1067,7 +1067,7 @@ impl, Cold: ItemStore> HotColdDB let state_root_iter = self.forwards_state_roots_iterator_until( low_restore_point.slot(), slot, - || (high_restore_point, Hash256::zero()), + || Ok((high_restore_point, Hash256::zero())), &self.spec, )?; @@ -1694,9 +1694,7 @@ impl, Cold: ItemStore> HotColdDB /// Try to prune blobs older than the data availability boundary. pub fn try_prune_blobs(&self, force: bool) -> Result<(), Error> { let blob_info = match self.get_blob_info() { - Some(old_blob_info) => { - old_blob_info - } + Some(old_blob_info) => old_blob_info, None => { return Ok(()); } @@ -1709,14 +1707,6 @@ impl, Cold: ItemStore> HotColdDB let dab_state_root = blob_info.data_availability_boundary.state_root; - // Load the state from which to prune blobs so we can backtrack. - let dab_state = self - .get_state(&dab_state_root, None)? - .ok_or(HotColdDBError::MissingStateToPruneBlobs(dab_state_root))?; - - let dab_block_root = dab_state.get_latest_block_root(dab_state_root); - let dab_slot = dab_state.slot(); - // Iterate block roots backwards to oldest blob slot. warn!( self.log, @@ -1727,9 +1717,19 @@ impl, Cold: ItemStore> HotColdDB let mut ops = vec![]; let mut last_pruned_block_root = None; - for res in std::iter::once(Ok((dab_block_root, dab_slot))) - .chain(BlockRootsIterator::new(self, &dab_state)) - { + for res in self.forwards_block_roots_iterator_until( + blob_info.oldest_blob_slot, + blob_info.data_availability_boundary.slot, + || { + let dab_state = self + .get_state(&dab_state_root, None)? + .ok_or(HotColdDBError::MissingStateToPruneBlobs(dab_state_root))?; + let dab_block_root = dab_state.get_latest_block_root(dab_state_root); + + Ok((dab_state, dab_block_root)) + }, + &self.spec, + )? { let (block_root, slot) = match res { Ok(tuple) => tuple, Err(e) => { @@ -1761,7 +1761,6 @@ impl, Cold: ItemStore> HotColdDB "Blobs sidecar pruning reached earliest available blobs sidecar"; "slot" => slot ); - blob_info.oldest_blob_slot = dab_slot + 1; break; } } @@ -1774,8 +1773,12 @@ impl, Cold: ItemStore> HotColdDB "blobs_sidecars_pruned" => blobs_sidecars_pruned, ); - blob_info.last_pruned_epoch = dab_state.current_epoch(); - self.compare_and_set_blob_info_with_write(self.get_blob_info(), Some(blob_info))?; + if let Some(mut new_blob_info) = self.get_blob_info() { + new_blob_info.last_pruned_epoch = + (blob_info.data_availability_boundary.slot + 1).epoch(E::slots_per_epoch()); + new_blob_info.oldest_blob_slot = blob_info.data_availability_boundary.slot + 1; + self.compare_and_set_blob_info_with_write(self.get_blob_info(), Some(new_blob_info))?; + } Ok(()) }