From 95dbd32d8e30862c9426b3dac807591c94178442 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 9 Nov 2024 17:01:08 +0800 Subject: [PATCH 1/2] feat: add anvil_get_interval_mine method --- crates/anvil/core/src/eth/mod.rs | 4 ++++ crates/anvil/src/eth/api.rs | 9 +++++++++ crates/anvil/src/eth/miner.rs | 7 +++++-- crates/anvil/tests/it/anvil.rs | 3 +++ 4 files changed, 21 insertions(+), 2 deletions(-) diff --git a/crates/anvil/core/src/eth/mod.rs b/crates/anvil/core/src/eth/mod.rs index d53473666e1f..276dc0f92270 100644 --- a/crates/anvil/core/src/eth/mod.rs +++ b/crates/anvil/core/src/eth/mod.rs @@ -393,6 +393,10 @@ pub enum EthRequest { )] SetIntervalMining(u64), + /// Gets the current mining behavior + #[cfg_attr(feature = "serde", serde(rename = "anvil_getIntervalMine", with = "empty_params"))] + GetIntervalMine(()), + /// Removes transactions from the pool #[cfg_attr( feature = "serde", diff --git a/crates/anvil/src/eth/api.rs b/crates/anvil/src/eth/api.rs index a85862664684..6f9b5f644665 100644 --- a/crates/anvil/src/eth/api.rs +++ b/crates/anvil/src/eth/api.rs @@ -319,6 +319,7 @@ impl EthApi { EthRequest::SetIntervalMining(interval) => { self.anvil_set_interval_mining(interval).to_rpc_result() } + EthRequest::GetIntervalMine(()) => self.anvil_get_interval_mine().to_rpc_result(), EthRequest::DropTransaction(tx) => { self.anvil_drop_transaction(tx).await.to_rpc_result() } @@ -1650,6 +1651,14 @@ impl EthApi { Ok(self.miner.is_auto_mine()) } + /// Return the interval value of mining + /// + /// Handler for ETH RPC call: `anvil_getMiningMode` + pub fn anvil_get_interval_mine(&self) -> Result> { + node_info!("anvil_getIntervalMine"); + Ok(self.miner.get_interval()) + } + /// Enables or disables, based on the single boolean argument, the automatic mining of new /// blocks with each new transaction submitted to the network. /// diff --git a/crates/anvil/src/eth/miner.rs b/crates/anvil/src/eth/miner.rs index defb6624a787..90c4550907fe 100644 --- a/crates/anvil/src/eth/miner.rs +++ b/crates/anvil/src/eth/miner.rs @@ -64,9 +64,12 @@ impl Miner { matches!(*mode, MiningMode::Auto(_)) } - pub fn is_interval(&self) -> bool { + pub fn get_interval(&self) -> Option { let mode = self.mode.read(); - matches!(*mode, MiningMode::FixedBlockTime(_)) + if let MiningMode::FixedBlockTime(ref mm) = *mode { + return Some(mm.interval.period().as_secs()) + } + None } /// Sets the mining mode to operate in diff --git a/crates/anvil/tests/it/anvil.rs b/crates/anvil/tests/it/anvil.rs index 50e27c57aa57..eca2dd38020e 100644 --- a/crates/anvil/tests/it/anvil.rs +++ b/crates/anvil/tests/it/anvil.rs @@ -11,12 +11,14 @@ async fn test_can_change_mining_mode() { let provider = handle.http_provider(); assert!(api.anvil_get_auto_mine().unwrap()); + assert!(api.anvil_get_interval_mine().unwrap().is_none()); let num = provider.get_block_number().await.unwrap(); assert_eq!(num, 0); api.anvil_set_interval_mining(1).unwrap(); assert!(!api.anvil_get_auto_mine().unwrap()); + assert!(matches!(api.anvil_get_interval_mine().unwrap(), Some(1))); // changing the mining mode will instantly mine a new block tokio::time::sleep(std::time::Duration::from_millis(500)).await; let num = provider.get_block_number().await.unwrap(); @@ -29,6 +31,7 @@ async fn test_can_change_mining_mode() { // assert that no block is mined when the interval is set to 0 api.anvil_set_interval_mining(0).unwrap(); assert!(!api.anvil_get_auto_mine().unwrap()); + assert!(api.anvil_get_interval_mine().unwrap().is_none()); tokio::time::sleep(std::time::Duration::from_millis(1000)).await; let num = provider.get_block_number().await.unwrap(); assert_eq!(num, 1); From 4717a86b79fa6c6ed0a52821d9d67c8e45e1f84a Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 9 Nov 2024 23:24:06 +0800 Subject: [PATCH 2/2] refactor: keep consistent naming --- crates/anvil/core/src/eth/mod.rs | 4 ++-- crates/anvil/src/eth/api.rs | 10 +++++----- crates/anvil/tests/it/anvil.rs | 6 +++--- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/crates/anvil/core/src/eth/mod.rs b/crates/anvil/core/src/eth/mod.rs index e6833b7eb8d0..38691ba0d535 100644 --- a/crates/anvil/core/src/eth/mod.rs +++ b/crates/anvil/core/src/eth/mod.rs @@ -395,8 +395,8 @@ pub enum EthRequest { SetIntervalMining(u64), /// Gets the current mining behavior - #[cfg_attr(feature = "serde", serde(rename = "anvil_getIntervalMine", with = "empty_params"))] - GetIntervalMine(()), + #[cfg_attr(feature = "serde", serde(rename = "anvil_getIntervalMing", with = "empty_params"))] + GetIntervalMining(()), /// Removes transactions from the pool #[cfg_attr( diff --git a/crates/anvil/src/eth/api.rs b/crates/anvil/src/eth/api.rs index a6bd751783c1..e5210b0c23f6 100644 --- a/crates/anvil/src/eth/api.rs +++ b/crates/anvil/src/eth/api.rs @@ -326,7 +326,7 @@ impl EthApi { EthRequest::SetIntervalMining(interval) => { self.anvil_set_interval_mining(interval).to_rpc_result() } - EthRequest::GetIntervalMine(()) => self.anvil_get_interval_mine().to_rpc_result(), + EthRequest::GetIntervalMining(()) => self.anvil_get_interval_ming().to_rpc_result(), EthRequest::DropTransaction(tx) => { self.anvil_drop_transaction(tx).await.to_rpc_result() } @@ -1666,11 +1666,11 @@ impl EthApi { Ok(self.miner.is_auto_mine()) } - /// Return the interval value of mining + /// Returns the value of mining interval, if set. /// - /// Handler for ETH RPC call: `anvil_getMiningMode` - pub fn anvil_get_interval_mine(&self) -> Result> { - node_info!("anvil_getIntervalMine"); + /// Handler for ETH RPC call: `anvil_getIntervalMing` + pub fn anvil_get_interval_ming(&self) -> Result> { + node_info!("anvil_getIntervalMining"); Ok(self.miner.get_interval()) } diff --git a/crates/anvil/tests/it/anvil.rs b/crates/anvil/tests/it/anvil.rs index 84dcf8567613..17b94f041ab5 100644 --- a/crates/anvil/tests/it/anvil.rs +++ b/crates/anvil/tests/it/anvil.rs @@ -12,14 +12,14 @@ async fn test_can_change_mining_mode() { let provider = handle.http_provider(); assert!(api.anvil_get_auto_mine().unwrap()); - assert!(api.anvil_get_interval_mine().unwrap().is_none()); + assert!(api.anvil_get_interval_ming().unwrap().is_none()); let num = provider.get_block_number().await.unwrap(); assert_eq!(num, 0); api.anvil_set_interval_mining(1).unwrap(); assert!(!api.anvil_get_auto_mine().unwrap()); - assert!(matches!(api.anvil_get_interval_mine().unwrap(), Some(1))); + assert!(matches!(api.anvil_get_interval_ming().unwrap(), Some(1))); // changing the mining mode will instantly mine a new block tokio::time::sleep(std::time::Duration::from_millis(500)).await; let num = provider.get_block_number().await.unwrap(); @@ -32,7 +32,7 @@ async fn test_can_change_mining_mode() { // assert that no block is mined when the interval is set to 0 api.anvil_set_interval_mining(0).unwrap(); assert!(!api.anvil_get_auto_mine().unwrap()); - assert!(api.anvil_get_interval_mine().unwrap().is_none()); + assert!(api.anvil_get_interval_ming().unwrap().is_none()); tokio::time::sleep(std::time::Duration::from_millis(1000)).await; let num = provider.get_block_number().await.unwrap(); assert_eq!(num, 1);