Skip to content

Commit

Permalink
refactor: chain-specific provider config
Browse files Browse the repository at this point in the history
  • Loading branch information
Wodann committed Jul 31, 2024
1 parent d02728b commit 1a572b4
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 24 deletions.
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.

4 changes: 2 additions & 2 deletions crates/edr_napi/src/provider/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
time::{Duration, SystemTime},
};

use edr_eth::HashMap;
use edr_eth::{chain_spec::L1ChainSpec, HashMap};
use edr_provider::AccountConfig;
use napi::{
bindgen_prelude::{BigInt, Buffer},
Expand Down Expand Up @@ -203,7 +203,7 @@ impl TryFrom<MiningConfig> for edr_provider::MiningConfig {
}
}

impl TryFrom<ProviderConfig> for edr_provider::ProviderConfig {
impl TryFrom<ProviderConfig> for edr_provider::ProviderConfig<L1ChainSpec> {
type Error = napi::Error;

fn try_from(value: ProviderConfig) -> Result<Self, Self::Error> {
Expand Down
5 changes: 3 additions & 2 deletions crates/edr_napi/src/scenarios.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::time::{SystemTime, UNIX_EPOCH};

use edr_eth::chain_spec::L1ChainSpec;
use edr_provider::ProviderRequest;
use napi::tokio::{fs::File, io::AsyncWriteExt, sync::Mutex};
use rand::{distributions::Alphanumeric, Rng};
Expand All @@ -9,12 +10,12 @@ const SCENARIO_FILE_PREFIX: &str = "EDR_SCENARIO_PREFIX";

#[derive(Clone, Debug, Serialize)]
struct ScenarioConfig<'a> {
provider_config: &'a edr_provider::ProviderConfig,
provider_config: &'a edr_provider::ProviderConfig<L1ChainSpec>,
logger_enabled: bool,
}

pub(crate) async fn scenario_file(
provider_config: &edr_provider::ProviderConfig,
provider_config: &edr_provider::ProviderConfig<L1ChainSpec>,
logger_enabled: bool,
) -> Result<Option<Mutex<File>>, napi::Error> {
if let Ok(scenario_prefix) = std::env::var(SCENARIO_FILE_PREFIX) {
Expand Down
1 change: 1 addition & 0 deletions crates/edr_provider/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ alloy-dyn-abi = { version = "0.7.6", features = ["eip712"] }
alloy-sol-types = { version = "0.5.1", default-features = false, features = ["std"] }
anyhow = { version = "1.0.75", optional = true }
auto_impl = { version = "1.2", default-features = false }
derive-where = { version = "1.2.7", default-features = false }
dyn-clone = { version = "1.0.13", default-features = false }
edr_defaults = { version = "0.3.5", path = "../edr_defaults" }
edr_eth = { version = "0.3.5", path = "../edr_eth", features = ["rand"] }
Expand Down
21 changes: 12 additions & 9 deletions crates/edr_provider/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use std::{num::NonZeroU64, path::PathBuf, time::SystemTime};

use edr_eth::{
block::BlobGas, chain_spec::L1ChainSpec, AccountInfo, Address, ChainId, HashMap, SpecId, B256,
U256,
};
use edr_evm::{hardfork, MineOrdering};
use derive_where::derive_where;
use edr_eth::{block::BlobGas, AccountInfo, Address, ChainId, HashMap, SpecId, B256, U256};
use edr_evm::{chain_spec::ChainSpec, hardfork, MineOrdering};
use rand::Rng;
use serde::{Deserialize, Serialize};
use serde::{de::DeserializeOwned, Deserialize, Serialize};

use crate::requests::{hardhat::rpc_types::ForkConfig, IntervalConfig as IntervalConfigRequest};

Expand Down Expand Up @@ -72,8 +70,13 @@ pub struct MiningConfig {
}

/// Configuration for the provider
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct ProviderConfig {
#[derive_where(Clone, Debug; ChainSpecT::Hardfork)]
#[derive(Deserialize, Serialize)]
#[serde(bound(
deserialize = "ChainSpecT::Hardfork: DeserializeOwned",
serialize = "ChainSpecT::Hardfork: Serialize"
))]
pub struct ProviderConfig<ChainSpecT: ChainSpec> {
pub allow_blocks_with_same_timestamp: bool,
pub allow_unlimited_contract_size: bool,
pub accounts: Vec<AccountConfig>,
Expand All @@ -84,7 +87,7 @@ pub struct ProviderConfig {
pub block_gas_limit: NonZeroU64,
pub cache_dir: PathBuf,
pub chain_id: ChainId,
pub chains: HashMap<ChainId, hardfork::Activations<L1ChainSpec>>,
pub chains: HashMap<ChainId, hardfork::Activations<ChainSpecT>>,
pub coinbase: Address,
pub enable_rip_7212: bool,
pub fork: Option<ForkConfig>,
Expand Down
12 changes: 6 additions & 6 deletions crates/edr_provider/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ where

pub struct ProviderData<LoggerErrorT: Debug, TimerT: Clone + TimeSinceEpoch = CurrentTime> {
runtime_handle: runtime::Handle,
initial_config: ProviderConfig,
initial_config: ProviderConfig<L1ChainSpec>,
blockchain: Box<dyn SyncBlockchain<L1ChainSpec, BlockchainError<L1ChainSpec>, StateError>>,
pub irregular_state: IrregularState,
mem_pool: MemPool,
Expand Down Expand Up @@ -228,7 +228,7 @@ impl<LoggerErrorT: Debug, TimerT: Clone + TimeSinceEpoch> ProviderData<LoggerErr
>,
subscriber_callback: Box<dyn SyncSubscriberCallback>,
call_override: Option<Arc<dyn SyncCallOverride>>,
config: ProviderConfig,
config: ProviderConfig<L1ChainSpec>,
timer: TimerT,
) -> Result<Self, CreationError<L1ChainSpec>> {
let InitialAccounts {
Expand Down Expand Up @@ -2395,7 +2395,7 @@ impl StateId {
}

fn block_time_offset_seconds(
config: &ProviderConfig,
config: &ProviderConfig<L1ChainSpec>,
timer: &impl TimeSinceEpoch,
) -> Result<i64, CreationError<L1ChainSpec>> {
config.initial_date.map_or(Ok(0), |initial_date| {
Expand Down Expand Up @@ -2427,7 +2427,7 @@ struct BlockchainAndState {

fn create_blockchain_and_state(
runtime: runtime::Handle,
config: &ProviderConfig,
config: &ProviderConfig<L1ChainSpec>,
timer: &impl TimeSinceEpoch,
mut genesis_accounts: HashMap<Address, Account>,
) -> Result<BlockchainAndState, CreationError<L1ChainSpec>> {
Expand Down Expand Up @@ -2652,7 +2652,7 @@ pub(crate) mod test_utils {

pub(crate) struct ProviderTestFixture {
_runtime: runtime::Runtime,
pub config: ProviderConfig,
pub config: ProviderConfig<L1ChainSpec>,
pub provider_data: ProviderData<Infallible>,
pub impersonated_account: Address,
}
Expand Down Expand Up @@ -2693,7 +2693,7 @@ pub(crate) mod test_utils {

pub fn new(
runtime: tokio::runtime::Runtime,
mut config: ProviderConfig,
mut config: ProviderConfig<L1ChainSpec>,
) -> anyhow::Result<Self> {
let logger = Box::<NoopLogger>::default();
let subscription_callback_noop = Box::new(|_| ());
Expand Down
3 changes: 2 additions & 1 deletion crates/edr_provider/src/data/account.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use edr_eth::{
chain_spec::L1ChainSpec,
signature::public_key_to_address,
state::{Account, AccountStatus},
AccountInfo, Address, HashMap, KECCAK_EMPTY,
Expand All @@ -12,7 +13,7 @@ pub(super) struct InitialAccounts {
pub genesis_accounts: HashMap<Address, Account>,
}

pub(super) fn create_accounts(config: &ProviderConfig) -> InitialAccounts {
pub(super) fn create_accounts(config: &ProviderConfig<L1ChainSpec>) -> InitialAccounts {
let mut local_accounts = IndexMap::default();

let genesis_accounts = config
Expand Down
2 changes: 1 addition & 1 deletion crates/edr_provider/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ impl<LoggerErrorT: Debug + Send + Sync + 'static, TimerT: Clone + TimeSinceEpoch
>,
>,
subscriber_callback: Box<dyn SyncSubscriberCallback>,
config: ProviderConfig,
config: ProviderConfig<L1ChainSpec>,
timer: TimerT,
) -> Result<Self, CreationError<L1ChainSpec>> {
let data = ProviderData::new(
Expand Down
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 @@ -19,15 +19,15 @@ pub const TEST_SECRET_KEY_SIGN_TYPED_DATA_V4: &str =
pub const FORK_BLOCK_NUMBER: u64 = 18_725_000;

/// Constructs a test config with a single account with 1 ether
pub fn create_test_config() -> ProviderConfig {
pub fn create_test_config() -> ProviderConfig<L1ChainSpec> {
create_test_config_with_fork(None)
}

pub fn one_ether() -> U256 {
U256::from(10).pow(U256::from(18))
}

pub fn create_test_config_with_fork(fork: Option<ForkConfig>) -> ProviderConfig {
pub fn create_test_config_with_fork(fork: Option<ForkConfig>) -> ProviderConfig<L1ChainSpec> {
ProviderConfig {
accounts: vec![
AccountConfig {
Expand Down
2 changes: 1 addition & 1 deletion crates/tools/src/scenario.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use tracing_subscriber::{prelude::*, Registry};

#[derive(Clone, Debug, Deserialize)]
struct ScenarioConfig {
provider_config: edr_provider::ProviderConfig,
provider_config: edr_provider::ProviderConfig<L1ChainSpec>,
logger_enabled: bool,
}

Expand Down

0 comments on commit 1a572b4

Please sign in to comment.