Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add max cached states env var #4945

Merged
merged 2 commits into from
Mar 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions crates/edr_provider/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::{
cmp,
cmp::Ordering,
collections::BTreeMap,
ffi::OsString,
fmt::Debug,
num::NonZeroUsize,
sync::Arc,
Expand Down Expand Up @@ -76,7 +77,8 @@ use crate::{
};

const DEFAULT_INITIAL_BASE_FEE_PER_GAS: u64 = 1_000_000_000;
const MAX_CACHED_STATES: usize = 10;
const EDR_MAX_CACHED_STATES_ENV_VAR: &str = "__EDR_MAX_CACHED_STATES";
const DEFAULT_MAX_CACHED_STATES: usize = 10;

/// The result of executing an `eth_call`.
#[derive(Clone, Debug)]
Expand Down Expand Up @@ -112,6 +114,8 @@ pub enum CreationError {
/// Invalid initial date
#[error("The initial date configuration value {0:?} is before the UNIX epoch")]
InvalidInitialDate(SystemTime),
#[error("Invalid max cached states environment variable value: '{0:?}'. Please provide a non-zero integer!")]
InvalidMaxCachedStates(OsString),
/// An error that occurred while constructing a local blockchain.
#[error(transparent)]
LocalBlockchainCreation(#[from] LocalCreationError),
Expand Down Expand Up @@ -183,8 +187,19 @@ impl<LoggerErrorT: Debug> ProviderData<LoggerErrorT> {
next_block_base_fee_per_gas,
} = create_blockchain_and_state(runtime_handle.clone(), &config, genesis_accounts)?;

let mut block_state_cache =
LruCache::new(NonZeroUsize::new(MAX_CACHED_STATES).expect("constant is non-zero"));
let max_cached_states = std::env::var(EDR_MAX_CACHED_STATES_ENV_VAR).map_or_else(
|err| match err {
std::env::VarError::NotPresent => {
Ok(NonZeroUsize::new(DEFAULT_MAX_CACHED_STATES).expect("constant is non-zero"))
}
std::env::VarError::NotUnicode(s) => Err(CreationError::InvalidMaxCachedStates(s)),
},
|s| {
s.parse()
.map_err(|_err| CreationError::InvalidMaxCachedStates(s.into()))
},
)?;
let mut block_state_cache = LruCache::new(max_cached_states);
let mut block_number_to_state_id = BTreeMap::new();

let current_state_id = StateId::default();
Expand Down
Loading