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(evm/cache): improve json file caching #1025

Merged
merged 12 commits into from
Mar 23, 2022
Merged
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
67 changes: 29 additions & 38 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ tokio = { version = "1.11.0", features = ["macros"] }
regex = { version = "1.5.4", default-features = false }
ansi_term = "0.12.1"
rpassword = "5.0.1"
tracing-subscriber = "0.2.20"
tracing-error = "0.2.0"
tracing-subscriber = { version = "0.3", features = ["registry", "env-filter", "fmt"] }
tracing = "0.1.26"
hex = "0.4.3"
rayon = "1.5.1"
Expand Down
62 changes: 29 additions & 33 deletions cli/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ use std::{
time::Duration,
};

use ethers::{
providers::{Middleware, Provider},
solc::EvmVersion,
types::U256,
};
use ethers::{solc::EvmVersion, types::U256};
use forge::executor::{opts::EvmOpts, Fork, SpecId};
use foundry_config::{caching::StorageCachingConfig, Config};
use tracing_error::ErrorLayer;
use tracing_subscriber::prelude::*;

// reexport all `foundry_config::utils`
#[doc(hidden)]
pub use foundry_config::utils::*;
Expand Down Expand Up @@ -60,10 +59,11 @@ impl<T: AsRef<Path>> FoundryPathExt for T {
/// Initializes a tracing Subscriber for logging
#[allow(dead_code)]
pub fn subscriber() {
tracing_subscriber::FmtSubscriber::builder()
// .with_timer(tracing_subscriber::fmt::time::uptime())
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
.init();
tracing_subscriber::Registry::default()
.with(tracing_subscriber::EnvFilter::from_default_env())
.with(ErrorLayer::default())
.with(tracing_subscriber::fmt::layer())
.init()
}

pub fn evm_spec(evm: &EvmVersion) -> SpecId {
Expand Down Expand Up @@ -159,14 +159,20 @@ pub fn block_on<F: Future>(future: F) -> F::Output {
/// - storage is allowed (`no_storage_caching = false`)
///
/// If all these criteria are met, then storage caching is enabled and storage info will be written
/// to [Config::data_dir()]/<str(chainid)>/block/storage.json
/// to [Config::foundry_cache_dir()]/<str(chainid)>/<block>/storage.json
///
/// for `mainnet` and `--fork-block-number 14435000` on mac the corresponding storage cache will be
/// at `$HOME`/Library/Application Support/foundry/mainnet/14435000/storage.json`
/// at `~/.foundry/cache/mainnet/14435000/storage.json`
pub fn get_fork(evm_opts: &EvmOpts, config: &StorageCachingConfig) -> Option<Fork> {
fn get_cache_storage_path(
/// Returns the path where the cache file should be stored
///
/// or `None` if caching should not be enabled
///
/// See also [ Config::foundry_block_cache_file()]
fn get_block_storage_path(
evm_opts: &EvmOpts,
config: &StorageCachingConfig,
chain_id: u64,
) -> Option<PathBuf> {
if evm_opts.no_storage_caching {
// storage caching explicitly opted out of
Expand All @@ -175,33 +181,23 @@ pub fn get_fork(evm_opts: &EvmOpts, config: &StorageCachingConfig) -> Option<For
let url = evm_opts.fork_url.as_ref()?;
// cache only if block explicitly pinned
let block = evm_opts.fork_block_number?;
if config.enable_for_endpoint(url) {
// also need to get the chain id to compute the cache path
let provider = Provider::try_from(url.as_str()).expect("Failed to establish provider");
match block_on(provider.get_chainid()) {
Ok(chain_id) => {
let chain_id: u64 = chain_id.try_into().ok()?;
if config.enable_for_chain_id(chain_id) {
let chain = if let Ok(chain) = ethers::types::Chain::try_from(chain_id) {
chain.to_string()
} else {
format!("{}", chain_id)
};
return Some(Config::data_dir().ok()?.join(chain).join(format!("{}", block)))
}
}
Err(err) => {
tracing::warn!("Failed to get chain id for {}: {:?}", url, err);
}
}

if config.enable_for_endpoint(url) && config.enable_for_chain_id(chain_id) {
return Config::foundry_block_cache_file(chain_id, block)
}

None
}

if let Some(ref url) = evm_opts.fork_url {
let cache_storage = get_cache_storage_path(evm_opts, config);
let fork = Fork { url: url.clone(), pin_block: evm_opts.fork_block_number, cache_storage };
let chain_id = evm_opts.get_chain_id();
let cache_storage = get_block_storage_path(evm_opts, config, chain_id);
let fork = Fork {
url: url.clone(),
pin_block: evm_opts.fork_block_number,
cache_path: cache_storage,
chain_id,
};
return Some(fork)
}

Expand Down
62 changes: 53 additions & 9 deletions config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ extern crate core;

use std::{
borrow::Cow,
fmt,
path::{Path, PathBuf},
str::FromStr,
};
Expand Down Expand Up @@ -709,6 +710,22 @@ impl Config {
dirs_next::home_dir().map(|p| p.join(Config::FOUNDRY_DIR_NAME))
}

/// Returns the path to foundry's cache dir `~/.foundry/cache`
pub fn foundry_cache_dir() -> Option<PathBuf> {
Self::foundry_dir().map(|p| p.join("cache"))
}

/// Returns the path to the cache file of the `block` on the `chain`
/// `~/.foundry/cache/<chain>/<block>/storage.json`
pub fn foundry_block_cache_file(chain_id: impl Into<Chain>, block: u64) -> Option<PathBuf> {
Some(
Config::foundry_cache_dir()?
.join(chain_id.into().to_string())
.join(format!("{}", block))
.join("storage.json"),
)
}

#[doc = r#"Returns the path to `foundry`'s data directory inside the user's data directory
|Platform | Value | Example |
| ------- | ------------------------------------- | -------------------------------- |
Expand Down Expand Up @@ -1278,6 +1295,42 @@ impl Chain {
}
}

impl fmt::Display for Chain {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Chain::Named(chain) => chain.fmt(f),
Chain::Id(id) => {
if let Ok(chain) = ethers_core::types::Chain::try_from(*id) {
chain.fmt(f)
} else {
id.fmt(f)
}
}
}
}
}

impl From<ethers_core::types::Chain> for Chain {
fn from(id: ethers_core::types::Chain) -> Self {
Chain::Named(id)
}
}

impl From<u64> for Chain {
fn from(id: u64) -> Self {
Chain::Id(id)
}
}

impl From<Chain> for u64 {
fn from(c: Chain) -> Self {
match c {
Chain::Named(c) => c as u64,
Chain::Id(id) => id,
}
}
}

impl<'de> Deserialize<'de> for Chain {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
Expand Down Expand Up @@ -1324,15 +1377,6 @@ mod from_str_lowercase {
}
}

impl From<Chain> for u64 {
fn from(c: Chain) -> Self {
match c {
Chain::Named(c) => c as u64,
Chain::Id(id) => id,
}
}
}

fn canonic(path: impl Into<PathBuf>) -> PathBuf {
let path = path.into();
ethers_solc::utils::canonicalize(&path).unwrap_or(path)
Expand Down
Loading