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

fix(anvil): impl maybe_as_full_db for ForkedDatabase #9465

Merged
merged 2 commits into from
Dec 3, 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
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);
}
Loading