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

Move cache_dir config option to ForkConfig - feat/multichain #749

Closed
wants to merge 4 commits into from
Closed
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
1 change: 1 addition & 0 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 crates/edr_generic/tests/issue_570.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ fn get_provider() -> anyhow::Result<Provider<GenericChainSpec>> {
let subscriber = Box::new(|_event| {});

let mut config = create_test_config_with_fork(Some(ForkConfig {
json_rpc_url: get_alchemy_url().replace("eth-mainnet", "base-sepolia"),
url: get_alchemy_url().replace("eth-mainnet", "base-sepolia"),
block_number: Some(BLOCK_NUMBER),
http_headers: None,
cache_dir: edr_defaults::CACHE_DIR.into(),
}));

config.chains.insert(
Expand Down
1 change: 1 addition & 0 deletions crates/edr_napi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ alloy-dyn-abi = { version = "0.7.6", default-features = false, features = ["eip7
alloy-json-abi = { version = "0.7.4", default-features = false }
alloy-sol-types = { version = "0.5.1", default-features = false, features = ["std"] }
derive-where = { version = "1.2.7", default-features = false }
edr_defaults = { path = "../edr_defaults" }
edr_eth = { path = "../edr_eth" }
edr_evm = { path = "../edr_evm" }
edr_generic = { path = "../edr_generic" }
Expand Down
6 changes: 3 additions & 3 deletions crates/edr_napi/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,16 @@ export interface ChainConfig {
/** Configuration for forking a blockchain */
export interface ForkConfig {
/** The URL of the JSON-RPC endpoint to fork from */
jsonRpcUrl: string
url: string
/**
* The block number to fork from. If not provided, the latest safe block is
* used.
*/
blockNumber?: bigint
/** The HTTP headers to use when making requests to the JSON-RPC endpoint */
httpHeaders?: Array<HttpHeader>
/** The directory to cache remote JSON-RPC responses */
cacheDir?: string
}
export interface HttpHeader {
name: string
Expand Down Expand Up @@ -192,8 +194,6 @@ export interface ProviderConfig {
bailOnTransactionFailure: boolean
/** The gas limit of each block */
blockGasLimit: bigint
/** The directory to cache remote JSON-RPC responses */
cacheDir?: string
/** The chain ID of the blockchain */
chainId: bigint
/** The configuration for chains */
Expand Down
18 changes: 13 additions & 5 deletions crates/edr_napi/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use std::{
num::NonZeroU64,
path::PathBuf,
time::{Duration, SystemTime},
};

use edr_defaults;
use edr_eth::HashMap;
use edr_provider::AccountConfig;
use napi::{
Expand All @@ -26,12 +28,14 @@ pub struct ChainConfig {
#[napi(object)]
pub struct ForkConfig {
/// The URL of the JSON-RPC endpoint to fork from
pub json_rpc_url: String,
pub url: String,
/// The block number to fork from. If not provided, the latest safe block is
/// used.
pub block_number: Option<BigInt>,
/// The HTTP headers to use when making requests to the JSON-RPC endpoint
pub http_headers: Option<Vec<HttpHeader>>,
/// The directory to cache remote JSON-RPC responses
pub cache_dir: Option<String>,
}

#[napi(object)]
Expand Down Expand Up @@ -91,8 +95,6 @@ pub struct ProviderConfig {
pub bail_on_transaction_failure: bool,
/// The gas limit of each block
pub block_gas_limit: BigInt,
/// The directory to cache remote JSON-RPC responses
pub cache_dir: Option<String>,
/// The chain ID of the blockchain
pub chain_id: BigInt,
/// The configuration for chains
Expand Down Expand Up @@ -139,9 +141,12 @@ impl TryFrom<ForkConfig> for edr_provider::hardhat_rpc_types::ForkConfig {
});

Ok(Self {
json_rpc_url: value.json_rpc_url,
url: value.url,
block_number,
http_headers,
cache_dir: value
.cache_dir
.map_or(edr_defaults::CACHE_DIR.into(), PathBuf::from),
})
}
}
Expand Down Expand Up @@ -256,7 +261,10 @@ impl TryFrom<ProviderConfig> for edr_napi_core::provider::Config {
bail_on_call_failure: value.bail_on_call_failure,
bail_on_transaction_failure: value.bail_on_transaction_failure,
block_gas_limit,
cache_dir: value.cache_dir,
iosh marked this conversation as resolved.
Show resolved Hide resolved
cache_dir: value
.fork
.as_ref()
.and_then(|fork_config| fork_config.cache_dir.clone()),
chain_id: value.chain_id.try_cast()?,
chains,
coinbase: value.coinbase.try_cast()?,
Expand Down
2 changes: 1 addition & 1 deletion crates/edr_napi/test/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ describe("Provider", () => {
GENERIC_CHAIN_TYPE,
{
fork: {
jsonRpcUrl: ALCHEMY_URL,
url: ALCHEMY_URL,
},
...providerConfig,
},
Expand Down
9 changes: 1 addition & 8 deletions crates/edr_napi_core/src/provider/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use core::num::NonZeroU64;
use std::{path::PathBuf, time::SystemTime};
use std::time::SystemTime;

use edr_eth::{account::AccountInfo, block::BlobGas, Address, ChainId, HashMap, B256, U256};
use edr_provider::{
Expand Down Expand Up @@ -52,12 +52,6 @@ where
ChainSpecT: RuntimeSpec<Hardfork: for<'s> From<&'s str>>,
{
fn from(value: Config) -> Self {
let cache_dir = PathBuf::from(
value
.cache_dir
.unwrap_or(String::from(edr_defaults::CACHE_DIR)),
);

let chains = value
.chains
.into_iter()
Expand Down Expand Up @@ -90,7 +84,6 @@ where
bail_on_call_failure: value.bail_on_call_failure,
bail_on_transaction_failure: value.bail_on_transaction_failure,
block_gas_limit: value.block_gas_limit,
cache_dir,
chain_id: value.chain_id,
chains,
coinbase: value.coinbase,
Expand Down
3 changes: 2 additions & 1 deletion crates/edr_optimism/tests/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ async fn sepolia_call_with_remote_chain_id() -> anyhow::Result<()> {
let subscriber = Box::new(|_event| {});

let mut config = create_test_config_with_fork(Some(ForkConfig {
json_rpc_url: sepolia_url(),
url: sepolia_url(),
block_number: None,
http_headers: None,
cache_dir: edr_defaults::CACHE_DIR.into(),
}));

// Set a different chain ID than the forked chain ID
Expand Down
3 changes: 1 addition & 2 deletions crates/edr_provider/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{num::NonZeroU64, path::PathBuf, time::SystemTime};
use std::{num::NonZeroU64, time::SystemTime};

use derive_where::derive_where;
use edr_eth::{account::AccountInfo, block::BlobGas, Address, ChainId, HashMap, B256, U256};
Expand Down Expand Up @@ -85,7 +85,6 @@ pub struct ProviderConfig<ChainSpecT: RuntimeSpec> {
/// Whether to return an `Err` when a `eth_sendTransaction` fails
pub bail_on_transaction_failure: bool,
pub block_gas_limit: NonZeroU64,
pub cache_dir: PathBuf,
pub chain_id: ChainId,
pub chains: HashMap<ChainId, hardfork::Activations<ChainSpecT>>,
pub coinbase: Address,
Expand Down
10 changes: 6 additions & 4 deletions crates/edr_provider/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2639,8 +2639,8 @@ fn create_blockchain_and_state<ChainSpecT: SyncRuntimeSpec<Hardfork: Debug>>(
.transpose()?;

let rpc_client = Arc::new(EthRpcClient::<ChainSpecT>::new(
&fork_config.json_rpc_url,
config.cache_dir.clone(),
&fork_config.url,
fork_config.cache_dir.clone(),
http_headers.clone(),
)?);

Expand Down Expand Up @@ -3818,10 +3818,11 @@ mod tests {
let mut fixture = ProviderTestFixture::<L1ChainSpec>::new_local()?;

let fork_config = Some(ForkConfig {
json_rpc_url: get_alchemy_url(),
url: get_alchemy_url(),
// Random recent block for better cache consistency
block_number: Some(FORK_BLOCK_NUMBER),
http_headers: None,
cache_dir: edr_defaults::CACHE_DIR.into(),
});

let block_spec = BlockSpec::Number(FORK_BLOCK_NUMBER);
Expand Down Expand Up @@ -3910,9 +3911,10 @@ mod tests {
.build()?;

let default_config = create_test_config_with_fork(Some(ForkConfig {
json_rpc_url: get_alchemy_url(),
url: get_alchemy_url(),
block_number: Some(EIP_1559_ACTIVATION_BLOCK),
http_headers: None,
cache_dir: edr_defaults::CACHE_DIR.into(),
}));

let config = ProviderConfig {
Expand Down
5 changes: 3 additions & 2 deletions crates/edr_provider/src/requests/hardhat/rpc_types/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashMap;
use std::{collections::HashMap, path::PathBuf};

#[derive(Clone, Debug, PartialEq, serde::Deserialize, serde::Serialize)]
pub struct ResetProviderConfig {
Expand All @@ -9,7 +9,8 @@ pub struct ResetProviderConfig {
#[derive(Clone, Debug, PartialEq, serde::Deserialize, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ForkConfig {
pub json_rpc_url: String,
pub url: String,
pub block_number: Option<u64>,
pub http_headers: Option<HashMap<String, String>>,
pub cache_dir: PathBuf,
}
4 changes: 2 additions & 2 deletions crates/edr_provider/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ pub fn create_test_config_with_fork<ChainSpecT: RuntimeSpec>(
min_gas_price: U256::ZERO,
mining: MiningConfig::default(),
network_id: 123,
cache_dir: edr_defaults::CACHE_DIR.into(),
}
}

Expand Down Expand Up @@ -166,10 +165,11 @@ impl<ChainSpecT: Debug + SyncProviderSpec<CurrentTime>> ProviderTestFixture<Chai
fn with_fork(fork: Option<String>) -> anyhow::Result<Self> {
let fork = fork.map(|json_rpc_url| {
ForkConfig {
json_rpc_url,
url: json_rpc_url,
// Random recent block for better cache consistency
block_number: Some(FORK_BLOCK_NUMBER),
http_headers: None,
cache_dir: edr_defaults::CACHE_DIR.into(),
}
});

Expand Down
3 changes: 2 additions & 1 deletion crates/edr_provider/tests/hardhat_request_serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,10 @@ fn serde_hardhat_reset() {
help_test_method_invocation_serde(MethodInvocation::<L1ChainSpec>::Reset(Some(
ResetProviderConfig {
forking: Some(ForkConfig {
json_rpc_url: String::from("http://whatever.com/whatever"),
url: String::from("http://whatever.com/whatever"),
block_number: Some(123456),
http_headers: None,
cache_dir: edr_defaults::CACHE_DIR.into(),
}),
},
)));
Expand Down
3 changes: 2 additions & 1 deletion crates/edr_provider/tests/issues/issue_324.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ async fn issue_324() -> anyhow::Result<()> {
let subscriber = Box::new(|_event| {});

let mut config = create_test_config_with_fork(Some(ForkConfig {
json_rpc_url: get_alchemy_url().replace("mainnet", "sepolia"),
url: get_alchemy_url().replace("mainnet", "sepolia"),
block_number: Some(DEPLOYMENT_BLOCK_NUMBER),
http_headers: None,
cache_dir: edr_defaults::CACHE_DIR.into(),
}));
config.hardfork = l1::SpecId::CANCUN;

Expand Down
3 changes: 2 additions & 1 deletion crates/edr_provider/tests/issues/issue_356.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ async fn issue_356() -> anyhow::Result<()> {
let subscriber = Box::new(|_event| {});

let mut config = create_test_config_with_fork(Some(ForkConfig {
json_rpc_url: get_alchemy_url().replace("mainnet", "sepolia"),
url: get_alchemy_url().replace("mainnet", "sepolia"),
// Pre-cancun Sepolia block
block_number: Some(4243456),
http_headers: None,
cache_dir: edr_defaults::CACHE_DIR.into(),
}));
config.hardfork = l1::SpecId::CANCUN;

Expand Down
3 changes: 2 additions & 1 deletion crates/edr_provider/tests/issues/issue_384.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ async fn avalanche_chain_mine_local_block() -> anyhow::Result<()> {
let subscriber = Box::new(|_event| {});

let config = create_test_config_with_fork(Some(ForkConfig {
json_rpc_url: get_infura_url().replace("mainnet", "avalanche-mainnet"),
url: get_infura_url().replace("mainnet", "avalanche-mainnet"),
block_number: Some(BLOCK_NUMBER),
http_headers: None,
cache_dir: edr_defaults::CACHE_DIR.into(),
}));

let provider = Provider::new(
Expand Down
3 changes: 2 additions & 1 deletion crates/edr_provider/tests/issues/issue_503.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ async fn issue_503() -> anyhow::Result<()> {
let subscriber = Box::new(|_event| {});

let mut config = create_test_config_with_fork(Some(ForkConfig {
json_rpc_url: get_alchemy_url(),
url: get_alchemy_url(),
block_number: Some(19_909_475),
http_headers: None,
cache_dir: edr_defaults::CACHE_DIR.into(),
}));
config.hardfork = l1::SpecId::CANCUN;

Expand Down
3 changes: 2 additions & 1 deletion crates/edr_provider/tests/issues/issue_533.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ async fn issue_533() -> anyhow::Result<()> {
let subscriber = Box::new(|_event| {});

let mut config = create_test_config_with_fork(Some(ForkConfig {
json_rpc_url: get_alchemy_url(),
url: get_alchemy_url(),
block_number: Some(20_384_300),
http_headers: None,
cache_dir: edr_defaults::CACHE_DIR.into(),
}));

// The default chain id set by Hardhat
Expand Down
3 changes: 2 additions & 1 deletion crates/edr_provider/tests/issues/issue_588.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ async fn issue_588() -> anyhow::Result<()> {
let subscriber = Box::new(|_event| {});

let early_mainnet_fork = create_test_config_with_fork(Some(ForkConfig {
json_rpc_url: get_alchemy_url(),
url: get_alchemy_url(),
block_number: Some(2_675_000),
http_headers: None,
cache_dir: edr_defaults::CACHE_DIR.into(),
}));

let current_time_is_1970 = Arc::new(MockTime::with_seconds(0));
Expand Down