diff --git a/crates/anvil/src/cmd.rs b/crates/anvil/src/cmd.rs index fca14e75dca7..c16328ee246f 100644 --- a/crates/anvil/src/cmd.rs +++ b/crates/anvil/src/cmd.rs @@ -86,6 +86,10 @@ pub struct NodeArgs { #[arg(short, long, visible_alias = "blockTime", value_name = "SECONDS")] pub block_time: Option, + /// Slots in an epoch + #[arg(long, value_name = "SLOTS_IN_AN_EPOCH", default_value_t = 32)] + pub slots_in_an_epoch: u64, + /// Writes output of `anvil` as json to user-specified file. #[arg(long, value_name = "OUT_FILE")] pub config_out: Option, @@ -230,6 +234,7 @@ impl NodeArgs { .with_transaction_block_keeper(self.transaction_block_keeper) .with_optimism(self.evm_opts.optimism) .with_disable_default_create2_deployer(self.evm_opts.disable_default_create2_deployer) + .with_slots_in_an_epoch(self.slots_in_an_epoch) } fn account_generator(&self) -> AccountGenerator { diff --git a/crates/anvil/src/config.rs b/crates/anvil/src/config.rs index 32db9e25f031..7c1098ec70af 100644 --- a/crates/anvil/src/config.rs +++ b/crates/anvil/src/config.rs @@ -170,6 +170,8 @@ pub struct NodeConfig { pub disable_default_create2_deployer: bool, /// Enable Optimism deposit transaction pub enable_optimism: bool, + /// Slots in an epoch + pub slots_in_an_epoch: u64, } impl NodeConfig { @@ -404,6 +406,7 @@ impl Default for NodeConfig { transaction_block_keeper: None, disable_default_create2_deployer: false, enable_optimism: false, + slots_in_an_epoch: 32, } } } @@ -596,6 +599,13 @@ impl NodeConfig { self } + /// Sets the slots in an epoch + #[must_use] + pub fn with_slots_in_an_epoch(mut self, slots_in_an_epoch: u64) -> Self { + self.slots_in_an_epoch = slots_in_an_epoch; + self + } + /// Sets the port to use #[must_use] pub fn with_port(mut self, port: u16) -> Self { diff --git a/crates/anvil/src/eth/backend/mem/mod.rs b/crates/anvil/src/eth/backend/mem/mod.rs index 202cc880e929..64e6942550e6 100644 --- a/crates/anvil/src/eth/backend/mem/mod.rs +++ b/crates/anvil/src/eth/backend/mem/mod.rs @@ -168,6 +168,8 @@ pub struct Backend { /// max number of blocks with transactions in memory transaction_block_keeper: Option, node_config: Arc>, + /// Slots in an epoch + slots_in_an_epoch: u64, } impl Backend { @@ -214,6 +216,8 @@ impl Backend { Default::default() }; + let slots_in_an_epoch = node_config.read().await.slots_in_an_epoch; + let backend = Self { db, blockchain, @@ -230,6 +234,7 @@ impl Backend { prune_state_history_config, transaction_block_keeper, node_config, + slots_in_an_epoch, }; if let Some(interval_block_time) = automine_block_time { @@ -1478,7 +1483,7 @@ impl Backend { BlockId::Hash(hash) => hash.block_hash, BlockId::Number(number) => { let storage = self.blockchain.storage.read(); - let slots_in_an_epoch = U64::from(32u64); + let slots_in_an_epoch = U64::from(self.slots_in_an_epoch); match number { BlockNumber::Latest => storage.best_hash, BlockNumber::Earliest => storage.genesis_hash, @@ -1599,7 +1604,6 @@ impl Backend { block_id: Option, ) -> Result { let current = self.best_number(); - let slots_in_an_epoch = 32u64; let requested = match block_id.map(Into::into).unwrap_or(BlockId::Number(BlockNumber::Latest)) { BlockId::Hash(hash) => self @@ -1614,12 +1618,8 @@ impl Backend { BlockNumber::Latest | BlockNumber::Pending => self.best_number(), BlockNumber::Earliest => U64::ZERO.to::(), BlockNumber::Number(num) => num, - BlockNumber::Safe => { - U64::from(current).saturating_sub(U64::from(slots_in_an_epoch)).to::() - } - BlockNumber::Finalized => U64::from(current) - .saturating_sub(U64::from(slots_in_an_epoch) * U64::from(2)) - .to::(), + BlockNumber::Safe => current.saturating_sub(self.slots_in_an_epoch), + BlockNumber::Finalized => current.saturating_sub(self.slots_in_an_epoch * 2), }, }; @@ -1632,13 +1632,12 @@ impl Backend { pub fn convert_block_number(&self, block: Option) -> u64 { let current = self.best_number(); - let slots_in_an_epoch = 32u64; match block.unwrap_or(BlockNumber::Latest) { BlockNumber::Latest | BlockNumber::Pending => current, BlockNumber::Earliest => 0, BlockNumber::Number(num) => num, - BlockNumber::Safe => current.saturating_sub(slots_in_an_epoch), - BlockNumber::Finalized => current.saturating_sub(slots_in_an_epoch * 2), + BlockNumber::Safe => current.saturating_sub(self.slots_in_an_epoch), + BlockNumber::Finalized => current.saturating_sub(self.slots_in_an_epoch * 2), } }