Skip to content

Commit e2acdc5

Browse files
authored
feat: add max cached states env var (#4945)
1 parent d789f3c commit e2acdc5

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

crates/edr_provider/src/data.rs

+18-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::{
77
cmp,
88
cmp::Ordering,
99
collections::BTreeMap,
10+
ffi::OsString,
1011
fmt::Debug,
1112
num::NonZeroUsize,
1213
sync::Arc,
@@ -76,7 +77,8 @@ use crate::{
7677
};
7778

7879
const DEFAULT_INITIAL_BASE_FEE_PER_GAS: u64 = 1_000_000_000;
79-
const MAX_CACHED_STATES: usize = 10;
80+
const EDR_MAX_CACHED_STATES_ENV_VAR: &str = "__EDR_MAX_CACHED_STATES";
81+
const DEFAULT_MAX_CACHED_STATES: usize = 10;
8082

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

186-
let mut block_state_cache =
187-
LruCache::new(NonZeroUsize::new(MAX_CACHED_STATES).expect("constant is non-zero"));
190+
let max_cached_states = std::env::var(EDR_MAX_CACHED_STATES_ENV_VAR).map_or_else(
191+
|err| match err {
192+
std::env::VarError::NotPresent => {
193+
Ok(NonZeroUsize::new(DEFAULT_MAX_CACHED_STATES).expect("constant is non-zero"))
194+
}
195+
std::env::VarError::NotUnicode(s) => Err(CreationError::InvalidMaxCachedStates(s)),
196+
},
197+
|s| {
198+
s.parse()
199+
.map_err(|_err| CreationError::InvalidMaxCachedStates(s.into()))
200+
},
201+
)?;
202+
let mut block_state_cache = LruCache::new(max_cached_states);
188203
let mut block_number_to_state_id = BTreeMap::new();
189204

190205
let current_state_id = StateId::default();

0 commit comments

Comments
 (0)