Skip to content

Commit

Permalink
feat(anvil): configure slots_in_an_epoch (#7335)
Browse files Browse the repository at this point in the history
* feat(anvil): configure slots_in_an_epoch

* Option nit

Co-authored-by: DaniPopes <57450786+DaniPopes@users.noreply.github.com>

* nit: use primitive u64

* nit: semicolon

* nits

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>

* nit: anvil config

---------

Co-authored-by: DaniPopes <57450786+DaniPopes@users.noreply.github.com>
Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
  • Loading branch information
3 people authored Mar 8, 2024
1 parent 18bffa6 commit 345858f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
5 changes: 5 additions & 0 deletions crates/anvil/src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ pub struct NodeArgs {
#[arg(short, long, visible_alias = "blockTime", value_name = "SECONDS")]
pub block_time: Option<u64>,

/// 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<String>,
Expand Down Expand Up @@ -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 {
Expand Down
10 changes: 10 additions & 0 deletions crates/anvil/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
}
}
}
Expand Down Expand Up @@ -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 {
Expand Down
21 changes: 10 additions & 11 deletions crates/anvil/src/eth/backend/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ pub struct Backend {
/// max number of blocks with transactions in memory
transaction_block_keeper: Option<usize>,
node_config: Arc<AsyncRwLock<NodeConfig>>,
/// Slots in an epoch
slots_in_an_epoch: u64,
}

impl Backend {
Expand Down Expand Up @@ -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,
Expand All @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -1599,7 +1604,6 @@ impl Backend {
block_id: Option<T>,
) -> Result<u64, BlockchainError> {
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
Expand All @@ -1614,12 +1618,8 @@ impl Backend {
BlockNumber::Latest | BlockNumber::Pending => self.best_number(),
BlockNumber::Earliest => U64::ZERO.to::<u64>(),
BlockNumber::Number(num) => num,
BlockNumber::Safe => {
U64::from(current).saturating_sub(U64::from(slots_in_an_epoch)).to::<u64>()
}
BlockNumber::Finalized => U64::from(current)
.saturating_sub(U64::from(slots_in_an_epoch) * U64::from(2))
.to::<u64>(),
BlockNumber::Safe => current.saturating_sub(self.slots_in_an_epoch),
BlockNumber::Finalized => current.saturating_sub(self.slots_in_an_epoch * 2),
},
};

Expand All @@ -1632,13 +1632,12 @@ impl Backend {

pub fn convert_block_number(&self, block: Option<BlockNumber>) -> 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),
}
}

Expand Down

0 comments on commit 345858f

Please sign in to comment.