Skip to content

Commit

Permalink
Set max amount of epochs for state cache to retain states, in order t…
Browse files Browse the repository at this point in the history
…o avoid high memory usage during long non-finalization periods.
  • Loading branch information
povi committed Jan 14, 2025
1 parent 047520e commit be538b3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
14 changes: 12 additions & 2 deletions fork_choice_store/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ use crate::{
},
segment::{Position, Segment},
state_cache_processor::StateCacheProcessor,
store_config::StoreConfig,
store_config::{StoreConfig, MAX_EPOCHS_TO_RETAIN_STATES_IN_CACHE},
supersets::MultiPhaseAggregateAndProofSets as AggregateAndProofSupersets,
validations::validate_merge_block,
};
Expand Down Expand Up @@ -1900,6 +1900,7 @@ impl<P: Preset> Store<P> {
self.update_head_segment_id();

self.blob_cache.on_slot(new_tick.slot);
self.prune_state_cache()?;

let changes = if self.reorganized(old_head_segment_id) {
ApplyTickChanges::Reorganized {
Expand Down Expand Up @@ -2461,11 +2462,20 @@ impl<P: Preset> Store<P> {
self.accepted_blob_sidecars
.retain(|(slot, _, _), _| finalized_slot <= *slot);
self.prune_checkpoint_states();
self.state_cache.prune(finalized_slot).ok();
self.prune_state_cache().ok();
self.aggregate_and_proof_supersets
.prune(self.finalized_epoch());
}

fn prune_state_cache(&self) -> Result<()> {
let prune_slot = self
.slot()
.saturating_sub(P::SlotsPerEpoch::U64 * MAX_EPOCHS_TO_RETAIN_STATES_IN_CACHE)
.max(self.finalized_slot());

self.state_cache.prune(prune_slot)
}

/// Applies changes to [`Store.latest_messages`] and computes changes to attesting balances.
///
/// Roughly corresponds to [`update_latest_messages`] from the Fork Choice specification.
Expand Down
1 change: 1 addition & 0 deletions fork_choice_store/src/store_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use derivative::Derivative;
use types::config::Config as ChainConfig;

pub const DEFAULT_CACHE_LOCK_TIMEOUT_MILLIS: u64 = 1500;
pub const MAX_EPOCHS_TO_RETAIN_STATES_IN_CACHE: u64 = 8;

#[derive(Clone, Copy, Derivative)]
#[derivative(Default)]
Expand Down

0 comments on commit be538b3

Please sign in to comment.