Skip to content

Commit 9af381f

Browse files
authored
fix(anvil): impl maybe_as_full_db for ForkedDatabase (#9465)
1 parent 7f8154c commit 9af381f

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

crates/anvil/src/eth/backend/mem/fork_db.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{
55
},
66
revm::primitives::AccountInfo,
77
};
8-
use alloy_primitives::{Address, B256, U256, U64};
8+
use alloy_primitives::{map::HashMap, Address, B256, U256, U64};
99
use alloy_rpc_types::BlockId;
1010
use foundry_evm::{
1111
backend::{
@@ -14,7 +14,7 @@ use foundry_evm::{
1414
fork::database::ForkDbStateSnapshot,
1515
revm::{primitives::BlockEnv, Database},
1616
};
17-
use revm::DatabaseRef;
17+
use revm::{db::DbAccount, DatabaseRef};
1818

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

@@ -92,6 +92,10 @@ impl MaybeFullDatabase for ForkedDatabase {
9292
self
9393
}
9494

95+
fn maybe_as_full_db(&self) -> Option<&HashMap<Address, DbAccount>> {
96+
Some(&self.database().accounts)
97+
}
98+
9599
fn clear_into_state_snapshot(&mut self) -> StateSnapshot {
96100
let db = self.inner().db();
97101
let accounts = std::mem::take(&mut *db.accounts.write());
@@ -127,6 +131,10 @@ impl MaybeFullDatabase for ForkDbStateSnapshot {
127131
self
128132
}
129133

134+
fn maybe_as_full_db(&self) -> Option<&HashMap<Address, DbAccount>> {
135+
Some(&self.local.accounts)
136+
}
137+
130138
fn clear_into_state_snapshot(&mut self) -> StateSnapshot {
131139
std::mem::take(&mut self.state_snapshot)
132140
}

crates/anvil/tests/it/fork.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1473,3 +1473,42 @@ async fn test_reset_dev_account_nonce() {
14731473

14741474
assert!(receipt.status());
14751475
}
1476+
1477+
#[tokio::test(flavor = "multi_thread")]
1478+
async fn test_fork_get_account() {
1479+
let (_api, handle) = spawn(fork_config()).await;
1480+
let provider = handle.http_provider();
1481+
1482+
let accounts = handle.dev_accounts().collect::<Vec<_>>();
1483+
1484+
let alice = accounts[0];
1485+
let bob = accounts[1];
1486+
1487+
let init_block = provider.get_block_number().await.unwrap();
1488+
let alice_bal = provider.get_balance(alice).await.unwrap();
1489+
let alice_nonce = provider.get_transaction_count(alice).await.unwrap();
1490+
let alice_acc_init = provider.get_account(alice).await.unwrap();
1491+
1492+
assert_eq!(alice_acc_init.balance, alice_bal);
1493+
assert_eq!(alice_acc_init.nonce, alice_nonce);
1494+
1495+
let tx = TransactionRequest::default().from(alice).to(bob).value(U256::from(142));
1496+
1497+
let tx = WithOtherFields::new(tx);
1498+
let receipt = provider.send_transaction(tx).await.unwrap().get_receipt().await.unwrap();
1499+
1500+
assert!(receipt.status());
1501+
assert_eq!(init_block + 1, receipt.block_number.unwrap());
1502+
1503+
let alice_acc = provider.get_account(alice).await.unwrap();
1504+
1505+
assert_eq!(
1506+
alice_acc.balance,
1507+
alice_bal - (U256::from(142) + U256::from(receipt.gas_used * receipt.effective_gas_price)),
1508+
);
1509+
assert_eq!(alice_acc.nonce, alice_nonce + 1);
1510+
1511+
let alice_acc_prev_block = provider.get_account(alice).number(init_block).await.unwrap();
1512+
1513+
assert_eq!(alice_acc_init, alice_acc_prev_block);
1514+
}

0 commit comments

Comments
 (0)