Skip to content

Commit

Permalink
Add CLI arg to override max epochs to retain states in cache value
Browse files Browse the repository at this point in the history
  • Loading branch information
povi committed Jan 15, 2025
1 parent 2efb783 commit 24a937c
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 3 deletions.
7 changes: 5 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, MAX_EPOCHS_TO_RETAIN_STATES_IN_CACHE},
store_config::StoreConfig,
supersets::MultiPhaseAggregateAndProofSets as AggregateAndProofSupersets,
validations::validate_merge_block,
};
Expand Down Expand Up @@ -2468,9 +2468,12 @@ impl<P: Preset> Store<P> {
}

fn prune_state_cache(&self) -> Result<()> {
let retain_slots =
self.store_config.max_epochs_to_retain_states_in_cache * P::SlotsPerEpoch::U64;

let prune_slot = self
.slot()
.saturating_sub(P::SlotsPerEpoch::U64 * MAX_EPOCHS_TO_RETAIN_STATES_IN_CACHE)
.saturating_sub(retain_slots)
.max(self.finalized_slot());

self.state_cache.prune(prune_slot)
Expand Down
3 changes: 2 additions & 1 deletion fork_choice_store/src/store_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ 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 = 64;

#[derive(Clone, Copy, Derivative)]
#[derivative(Default)]
pub struct StoreConfig {
#[derivative(Default(value = "32"))]
pub max_empty_slots: u64,
#[derivative(Default(value = "64"))]
pub max_epochs_to_retain_states_in_cache: u64,
#[derivative(Default(value = "Duration::from_millis(DEFAULT_CACHE_LOCK_TIMEOUT_MILLIS)"))]
pub state_cache_lock_timeout: Duration,
#[derivative(Default(value = "128"))]
Expand Down
6 changes: 6 additions & 0 deletions grandine/src/grandine_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,10 @@ struct BeaconNodeOptions {
#[clap(long, default_value_t = DEFAULT_REQUEST_TIMEOUT)]
request_timeout: u64,

/// Max amount of epochs to retain beacon states in state cache
#[clap(long, default_value_t = StoreConfig::default().max_epochs_to_retain_states_in_cache)]
max_epochs_to_retain_states_in_cache: u64,

/// Default state cache lock timeout in milliseconds
#[clap(long, default_value_t = DEFAULT_CACHE_LOCK_TIMEOUT_MILLIS)]
state_cache_lock_timeout: u64,
Expand Down Expand Up @@ -896,6 +900,7 @@ impl GrandineArgs {
prune_storage,
unfinalized_states_in_memory,
request_timeout,
max_epochs_to_retain_states_in_cache,
state_cache_lock_timeout,
state_slot,
subscribe_all_subnets,
Expand Down Expand Up @@ -1259,6 +1264,7 @@ impl GrandineArgs {
storage_config,
unfinalized_states_in_memory,
request_timeout: Duration::from_millis(request_timeout),
max_epochs_to_retain_states_in_cache,
state_cache_lock_timeout: Duration::from_millis(state_cache_lock_timeout),
command,
slashing_enabled,
Expand Down
1 change: 1 addition & 0 deletions grandine/src/grandine_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub struct GrandineConfig {
pub storage_config: StorageConfig,
pub unfinalized_states_in_memory: u64,
pub request_timeout: Duration,
pub max_epochs_to_retain_states_in_cache: u64,
pub state_cache_lock_timeout: Duration,
pub command: Option<GrandineCommand>,
pub slashing_enabled: bool,
Expand Down
2 changes: 2 additions & 0 deletions grandine/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ fn try_main() -> Result<()> {
network_config,
storage_config,
request_timeout,
max_epochs_to_retain_states_in_cache,
state_cache_lock_timeout,
unfinalized_states_in_memory,
command,
Expand Down Expand Up @@ -429,6 +430,7 @@ fn try_main() -> Result<()> {

let store_config = StoreConfig {
max_empty_slots,
max_epochs_to_retain_states_in_cache,
state_cache_lock_timeout,
unfinalized_states_in_memory,
};
Expand Down

0 comments on commit 24a937c

Please sign in to comment.