Skip to content

Commit

Permalink
fix(anvil): reset to latest on none (foundry-rs#2838)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Aug 19, 2022
1 parent 8dfc605 commit 1a17022
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 28 deletions.
50 changes: 28 additions & 22 deletions anvil/src/eth/backend/fork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ impl ClientFork {
pub async fn reset(
&self,
url: Option<String>,
block_number: Option<u64>,
block_number: impl Into<BlockId>,
) -> Result<(), BlockchainError> {
let block_number = block_number.into();
{
self.database
.write()
Expand All @@ -69,19 +70,20 @@ impl ClientFork {
self.config.write().chain_id = chain_id.as_u64();
}

let block = if let Some(block_number) = block_number {
let provider = self.provider();
let block =
provider.get_block(block_number).await?.ok_or(BlockchainError::BlockNotFound)?;
let block_hash = block.hash.ok_or(BlockchainError::BlockNotFound)?;
let timestamp = block.timestamp.as_u64();
let base_fee = block.base_fee_per_gas;
Some((block_number, block_hash, timestamp, base_fee))
} else {
None
};

self.config.write().update_block(block);
let provider = self.provider();
let block =
provider.get_block(block_number).await?.ok_or(BlockchainError::BlockNotFound)?;
let block_hash = block.hash.ok_or(BlockchainError::BlockNotFound)?;
let timestamp = block.timestamp.as_u64();
let base_fee = block.base_fee_per_gas;

self.config.write().update_block(
block.number.ok_or(BlockchainError::BlockNotFound)?.as_u64(),
block_hash,
timestamp,
base_fee,
);

self.clear_cached_storage();
Ok(())
}
Expand Down Expand Up @@ -446,14 +448,18 @@ impl ClientForkConfig {
Ok(())
}
/// Updates the block forked off `(block number, block hash, timestamp)`
pub fn update_block(&mut self, block: Option<(u64, H256, u64, Option<U256>)>) {
if let Some((block_number, block_hash, timestamp, base_fee)) = block {
self.block_number = block_number;
self.block_hash = block_hash;
self.timestamp = timestamp;
self.base_fee = base_fee;
trace!(target: "fork", "Updated block number={} hash={:?}", block_number, block_hash);
}
pub fn update_block(
&mut self,
block_number: u64,
block_hash: H256,
timestamp: u64,
base_fee: Option<U256>,
) {
self.block_number = block_number;
self.block_hash = block_hash;
self.timestamp = timestamp;
self.base_fee = base_fee;
trace!(target: "fork", "Updated block number={} hash={:?}", block_number, block_hash);
}
}

Expand Down
4 changes: 3 additions & 1 deletion anvil/src/eth/backend/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,10 @@ impl Backend {
/// Resets the fork to a fresh state
pub async fn reset_fork(&self, forking: Forking) -> Result<(), BlockchainError> {
if let Some(fork) = self.get_fork() {
let block_number =
forking.block_number.map(BlockNumber::from).unwrap_or(BlockNumber::Latest);
// reset the fork entirely and reapply the genesis config
fork.reset(forking.json_rpc_url.clone(), forking.block_number).await?;
fork.reset(forking.json_rpc_url.clone(), block_number).await?;
let fork_block_number = fork.block_number();
let fork_block = fork
.block_by_number(fork_block_number)
Expand Down
6 changes: 6 additions & 0 deletions anvil/tests/it/fork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,12 @@ async fn test_fork_reset() {
assert_eq!(balance, handle.genesis_balance());
let balance = provider.get_balance(to, None).await.unwrap();
assert_eq!(balance, handle.genesis_balance());

// reset to latest
api.anvil_reset(Some(Forking::default())).await.unwrap();

let new_block_num = provider.get_block_number().await.unwrap();
assert!(new_block_num > block_number);
}

#[tokio::test(flavor = "multi_thread")]
Expand Down
15 changes: 10 additions & 5 deletions evm/src/executor/fork/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ use crate::{
},
revm::db::CacheDB,
};
use ethers::prelude::{Address, H256, U256};
use ethers::{
prelude::{Address, H256, U256},
types::BlockId,
};
use hashbrown::HashMap as Map;
use parking_lot::Mutex;
use revm::{db::DatabaseRef, Account, AccountInfo, Bytecode, Database, DatabaseCommit};
Expand Down Expand Up @@ -64,10 +67,12 @@ impl ForkedDatabase {
}

/// Reset the fork to a fresh forked state, and optionally update the fork config
pub fn reset(&mut self, _url: Option<String>, block_number: Option<u64>) -> Result<(), String> {
if let Some(block_number) = block_number {
self.backend.set_pinned_block(block_number).map_err(|err| err.to_string())?;
}
pub fn reset(
&mut self,
_url: Option<String>,
block_number: impl Into<BlockId>,
) -> Result<(), String> {
self.backend.set_pinned_block(block_number).map_err(|err| err.to_string())?;

// TODO need to find a way to update generic provider via url

Expand Down

0 comments on commit 1a17022

Please sign in to comment.