Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(anvil): add anvil_getIntervalMining API #9290

Merged
merged 3 commits into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions crates/anvil/core/src/eth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,10 @@ pub enum EthRequest {
)]
SetIntervalMining(u64),

/// Gets the current mining behavior
#[cfg_attr(feature = "serde", serde(rename = "anvil_getIntervalMing", with = "empty_params"))]
GetIntervalMining(()),

/// Removes transactions from the pool
#[cfg_attr(
feature = "serde",
Expand Down
9 changes: 9 additions & 0 deletions crates/anvil/src/eth/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ impl EthApi {
EthRequest::SetIntervalMining(interval) => {
self.anvil_set_interval_mining(interval).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()
}
Expand Down Expand Up @@ -1665,6 +1666,14 @@ impl EthApi {
Ok(self.miner.is_auto_mine())
}

/// Returns the value of mining interval, if set.
///
/// Handler for ETH RPC call: `anvil_getIntervalMing`
pub fn anvil_get_interval_ming(&self) -> Result<Option<u64>> {
Copy link

@holic holic Nov 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be anvil_get_interval_mining not anvil_get_interval_ming? (and the comment above)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you! yes indeed, missed the typo 😁 preparing an update in #9292

node_info!("anvil_getIntervalMining");
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.
///
Expand Down
7 changes: 5 additions & 2 deletions crates/anvil/src/eth/miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,12 @@ impl Miner {
matches!(*mode, MiningMode::Auto(_))
}

pub fn is_interval(&self) -> bool {
pub fn get_interval(&self) -> Option<u64> {
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
Expand Down
3 changes: 3 additions & 0 deletions crates/anvil/tests/it/anvil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +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_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_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();
Expand All @@ -30,6 +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_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);
Expand Down
Loading