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(anvil): --cache-path #9343

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
7 changes: 6 additions & 1 deletion crates/anvil/src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ pub struct NodeArgs {

#[command(flatten)]
pub server_config: ServerConfig,

/// Path to the cache directory where states are stored.
#[arg(long, value_name = "PATH")]
pub cache_path: Option<PathBuf>,
}

#[cfg(windows)]
Expand Down Expand Up @@ -274,7 +278,8 @@ impl NodeArgs {
.with_alphanet(self.evm_opts.alphanet)
.with_disable_default_create2_deployer(self.evm_opts.disable_default_create2_deployer)
.with_slots_in_an_epoch(self.slots_in_an_epoch)
.with_memory_limit(self.evm_opts.memory_limit))
.with_memory_limit(self.evm_opts.memory_limit)
.with_cache_path(self.cache_path))
}

fn account_generator(&self) -> AccountGenerator {
Expand Down
11 changes: 11 additions & 0 deletions crates/anvil/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ pub struct NodeConfig {
pub alphanet: bool,
/// Do not print log messages.
pub silent: bool,
/// The path where old states are cached.
pub cache_path: Option<PathBuf>,
Comment on lines +192 to +193
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// The path where old states are cached.
pub cache_path: Option<PathBuf>,
/// The path where states are cached.
pub cache_path: Option<PathBuf>,

}

impl NodeConfig {
Expand Down Expand Up @@ -465,6 +467,7 @@ impl Default for NodeConfig {
precompile_factory: None,
alphanet: false,
silent: false,
cache_path: None,
}
}
}
Expand Down Expand Up @@ -969,6 +972,13 @@ impl NodeConfig {
self
}

/// Sets the path where old states are cached
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Sets the path where old states are cached
/// Sets the path where states are cached

#[must_use]
pub fn with_cache_path(mut self, cache_path: Option<PathBuf>) -> Self {
self.cache_path = cache_path;
self
}

/// Configures everything related to env, backend and database and returns the
/// [Backend](mem::Backend)
///
Expand Down Expand Up @@ -1051,6 +1061,7 @@ impl NodeConfig {
self.max_persisted_states,
self.transaction_block_keeper,
self.block_time,
self.cache_path.clone(),
Arc::new(tokio::sync::RwLock::new(self.clone())),
)
.await;
Expand Down
4 changes: 4 additions & 0 deletions crates/anvil/src/eth/backend/mem/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ pub struct DiskStateCache {
}

impl DiskStateCache {
/// Specify the path where to create the tempdir in
pub fn with_path(self, temp_path: PathBuf) -> Self {
Self { temp_path: Some(temp_path), temp_dir: None }
}
/// Returns the cache file for the given hash
fn with_cache_file<F, R>(&mut self, hash: B256, f: F) -> Option<R>
where
Expand Down
9 changes: 7 additions & 2 deletions crates/anvil/src/eth/backend/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,12 @@ use revm::{
use std::{
collections::BTreeMap,
io::{Read, Write},
path::PathBuf,
sync::Arc,
time::Duration,
};
use storage::{Blockchain, MinedTransaction, DEFAULT_HISTORY_LIMIT};
use tokio::sync::RwLock as AsyncRwLock;

pub mod cache;
pub mod fork_db;
pub mod in_memory_db;
Expand Down Expand Up @@ -223,6 +223,7 @@ impl Backend {
max_persisted_states: Option<usize>,
transaction_block_keeper: Option<usize>,
automine_block_time: Option<Duration>,
cache_path: Option<PathBuf>,
node_config: Arc<AsyncRwLock<NodeConfig>>,
) -> Self {
// if this is a fork then adjust the blockchain storage
Expand All @@ -245,7 +246,7 @@ impl Backend {
genesis.timestamp
};

let states = if prune_state_history_config.is_config_enabled() {
let mut states = if prune_state_history_config.is_config_enabled() {
// if prune state history is enabled, configure the state cache only for memory
prune_state_history_config
.max_memory_history
Expand All @@ -260,6 +261,10 @@ impl Backend {
Default::default()
};

if let Some(cache_path) = cache_path {
states = states.disk_path(cache_path);
}

let (slots_in_an_epoch, precompile_factory) = {
let cfg = node_config.read().await;
(cfg.slots_in_an_epoch, cfg.precompile_factory.clone())
Expand Down
8 changes: 7 additions & 1 deletion crates/anvil/src/eth/backend/mem/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ use foundry_evm::{
};
use parking_lot::RwLock;
use revm::primitives::SpecId;
use std::{collections::VecDeque, fmt, sync::Arc, time::Duration};
use std::{collections::VecDeque, fmt, path::PathBuf, sync::Arc, time::Duration};
// use yansi::Paint;

// === various limits in number of blocks ===
Expand Down Expand Up @@ -94,6 +94,12 @@ impl InMemoryBlockStates {
self
}

/// Configures the path on disk where the states will cached.
pub fn disk_path(mut self, path: PathBuf) -> Self {
self.disk_cache = self.disk_cache.with_path(path);
self
}

/// This modifies the `limit` what to keep stored in memory.
///
/// This will ensure the new limit adjusts based on the block time.
Expand Down
Loading