Skip to content

Commit

Permalink
feat(anvil): support sub-second block time granularity (#7380)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes authored Mar 12, 2024
1 parent f218563 commit edb3a4b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
2 changes: 1 addition & 1 deletion crates/anvil/src/anvil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub enum AnvilSubcommand {
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
async fn main() -> eyre::Result<()> {
utils::load_dotenv();

let mut app = Anvil::parse();
Expand Down
16 changes: 12 additions & 4 deletions crates/anvil/src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ pub struct NodeArgs {
pub hardfork: Option<Hardfork>,

/// Block time in seconds for interval mining.
#[arg(short, long, visible_alias = "blockTime", value_name = "SECONDS")]
pub block_time: Option<u64>,
#[arg(short, long, visible_alias = "blockTime", value_name = "SECONDS", value_parser = duration_from_secs_f64)]
pub block_time: Option<Duration>,

/// Slots in an epoch
#[arg(long, value_name = "SLOTS_IN_AN_EPOCH", default_value_t = 32)]
Expand Down Expand Up @@ -198,7 +198,7 @@ impl NodeArgs {
.disable_block_gas_limit(self.evm_opts.disable_block_gas_limit)
.with_gas_price(self.evm_opts.gas_price.map(U256::from))
.with_hardfork(self.hardfork)
.with_blocktime(self.block_time.map(Duration::from_secs))
.with_blocktime(self.block_time)
.with_no_mining(self.no_mining)
.with_account_generator(self.account_generator())
.with_genesis_balance(genesis_balance)
Expand Down Expand Up @@ -269,7 +269,7 @@ impl NodeArgs {
/// Starts the node
///
/// See also [crate::spawn()]
pub async fn run(self) -> Result<(), Box<dyn std::error::Error>> {
pub async fn run(self) -> eyre::Result<()> {
let dump_state = self.dump_state_path();
let dump_interval =
self.state_interval.map(Duration::from_secs).unwrap_or(DEFAULT_DUMP_INTERVAL);
Expand Down Expand Up @@ -677,6 +677,14 @@ fn read_genesis_file(path: &str) -> Result<Genesis, String> {
foundry_common::fs::read_json_file(path.as_ref()).map_err(|err| err.to_string())
}

fn duration_from_secs_f64(s: &str) -> Result<Duration, String> {
let s = s.parse::<f64>().map_err(|e| e.to_string())?;
if s == 0.0 {
return Err("Duration must be greater than 0".to_string());
}
Duration::try_from_secs_f64(s).map_err(|e| e.to_string())
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
6 changes: 5 additions & 1 deletion crates/config/src/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,24 @@ struct TomlFile {
}

impl TomlFile {
fn open(path: impl AsRef<Path>) -> Result<Self, Box<dyn std::error::Error>> {
fn open(path: impl AsRef<Path>) -> eyre::Result<Self> {
let path = path.as_ref().to_owned();
let doc = fs::read_to_string(&path)?.parse()?;
Ok(Self { doc, path })
}

fn doc(&self) -> &toml_edit::Document {
&self.doc
}

fn doc_mut(&mut self) -> &mut toml_edit::Document {
&mut self.doc
}

fn path(&self) -> &Path {
self.path.as_ref()
}

fn save(&self) -> io::Result<()> {
fs::write(self.path(), self.doc().to_string())
}
Expand Down

0 comments on commit edb3a4b

Please sign in to comment.