Skip to content

Commit

Permalink
fix(anvil): impl maybe_as_full_db for ForkedDatabase (#9465)
Browse files Browse the repository at this point in the history
  • Loading branch information
yash-atreya authored Dec 3, 2024
1 parent 7f8154c commit 9af381f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
12 changes: 10 additions & 2 deletions crates/anvil/src/eth/backend/mem/fork_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
},
revm::primitives::AccountInfo,
};
use alloy_primitives::{Address, B256, U256, U64};
use alloy_primitives::{map::HashMap, Address, B256, U256, U64};
use alloy_rpc_types::BlockId;
use foundry_evm::{
backend::{
Expand All @@ -14,7 +14,7 @@ use foundry_evm::{
fork::database::ForkDbStateSnapshot,
revm::{primitives::BlockEnv, Database},
};
use revm::DatabaseRef;
use revm::{db::DbAccount, DatabaseRef};

pub use foundry_evm::fork::database::ForkedDatabase;

Expand Down Expand Up @@ -92,6 +92,10 @@ impl MaybeFullDatabase for ForkedDatabase {
self
}

fn maybe_as_full_db(&self) -> Option<&HashMap<Address, DbAccount>> {
Some(&self.database().accounts)
}

fn clear_into_state_snapshot(&mut self) -> StateSnapshot {
let db = self.inner().db();
let accounts = std::mem::take(&mut *db.accounts.write());
Expand Down Expand Up @@ -127,6 +131,10 @@ impl MaybeFullDatabase for ForkDbStateSnapshot {
self
}

fn maybe_as_full_db(&self) -> Option<&HashMap<Address, DbAccount>> {
Some(&self.local.accounts)
}

fn clear_into_state_snapshot(&mut self) -> StateSnapshot {
std::mem::take(&mut self.state_snapshot)
}
Expand Down
39 changes: 39 additions & 0 deletions crates/anvil/tests/it/fork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1473,3 +1473,42 @@ async fn test_reset_dev_account_nonce() {

assert!(receipt.status());
}

#[tokio::test(flavor = "multi_thread")]
async fn test_fork_get_account() {
let (_api, handle) = spawn(fork_config()).await;
let provider = handle.http_provider();

let accounts = handle.dev_accounts().collect::<Vec<_>>();

let alice = accounts[0];
let bob = accounts[1];

let init_block = provider.get_block_number().await.unwrap();
let alice_bal = provider.get_balance(alice).await.unwrap();
let alice_nonce = provider.get_transaction_count(alice).await.unwrap();
let alice_acc_init = provider.get_account(alice).await.unwrap();

assert_eq!(alice_acc_init.balance, alice_bal);
assert_eq!(alice_acc_init.nonce, alice_nonce);

let tx = TransactionRequest::default().from(alice).to(bob).value(U256::from(142));

let tx = WithOtherFields::new(tx);
let receipt = provider.send_transaction(tx).await.unwrap().get_receipt().await.unwrap();

assert!(receipt.status());
assert_eq!(init_block + 1, receipt.block_number.unwrap());

let alice_acc = provider.get_account(alice).await.unwrap();

assert_eq!(
alice_acc.balance,
alice_bal - (U256::from(142) + U256::from(receipt.gas_used * receipt.effective_gas_price)),
);
assert_eq!(alice_acc.nonce, alice_nonce + 1);

let alice_acc_prev_block = provider.get_account(alice).number(init_block).await.unwrap();

assert_eq!(alice_acc_init, alice_acc_prev_block);
}

0 comments on commit 9af381f

Please sign in to comment.