Skip to content

Commit

Permalink
fix(anvil): support overlapping anvil_mine calls (#8594)
Browse files Browse the repository at this point in the history
* fix(anvil): support overlapping anvil_mine calls

* no manual drop

---------

Co-authored-by: Matthias Seitz <[email protected]>
  • Loading branch information
evan-gray and mattsse authored Aug 3, 2024
1 parent 00ff367 commit 9108500
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
4 changes: 4 additions & 0 deletions crates/anvil/src/eth/backend/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ pub struct Backend {
slots_in_an_epoch: u64,
/// Precompiles to inject to the EVM.
precompile_factory: Option<Arc<dyn PrecompileFactory>>,
/// Prevent race conditions during mining
mining: Arc<tokio::sync::Mutex<()>>,
}

impl Backend {
Expand Down Expand Up @@ -259,6 +261,7 @@ impl Backend {
node_config,
slots_in_an_epoch,
precompile_factory,
mining: Arc::new(tokio::sync::Mutex::new(())),
};

if let Some(interval_block_time) = automine_block_time {
Expand Down Expand Up @@ -927,6 +930,7 @@ impl Backend {
&self,
pool_transactions: Vec<Arc<PoolTransaction>>,
) -> MinedBlockOutcome {
let _mining_guard = self.mining.lock().await;
trace!(target: "backend", "creating new block with {} transactions", pool_transactions.len());

let (outcome, header, block_hash) = {
Expand Down
42 changes: 42 additions & 0 deletions crates/anvil/tests/it/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use alloy_rpc_types::{
};
use alloy_serde::WithOtherFields;
use anvil::{eth::api::CLIENT_VERSION, spawn, NodeConfig, CHAIN_ID};
use futures::join;
use std::{collections::HashMap, time::Duration};

#[tokio::test(flavor = "multi_thread")]
Expand Down Expand Up @@ -375,3 +376,44 @@ async fn can_call_with_state_override() {
// `value` *is* changed with state
assert_eq!(value, "");
}

#[tokio::test(flavor = "multi_thread")]
async fn can_mine_while_mining() {
let (api, _) = spawn(NodeConfig::test()).await;

let total_blocks = 200;

let block_number = api
.block_by_number(BlockNumberOrTag::Latest)
.await
.unwrap()
.unwrap()
.header
.number
.unwrap();
assert_eq!(block_number, 0);

let block = api.block_by_number(BlockNumberOrTag::Number(block_number)).await.unwrap().unwrap();
assert_eq!(block.header.number.unwrap(), 0);

let result = join!(
api.anvil_mine(Some(U256::from(total_blocks / 2)), None),
api.anvil_mine(Some(U256::from(total_blocks / 2)), None)
);
result.0.unwrap();
result.1.unwrap();
tokio::time::sleep(Duration::from_millis(100)).await;

let block_number = api
.block_by_number(BlockNumberOrTag::Latest)
.await
.unwrap()
.unwrap()
.header
.number
.unwrap();
assert_eq!(block_number, total_blocks);

let block = api.block_by_number(BlockNumberOrTag::Number(block_number)).await.unwrap().unwrap();
assert_eq!(block.header.number.unwrap(), total_blocks);
}

0 comments on commit 9108500

Please sign in to comment.