Skip to content

Commit 5d6d065

Browse files
authored
fix(anvil): load account first before setting storage (#2699)
* test: add bsc test * fix: cache account issue
1 parent dfa0bd0 commit 5d6d065

File tree

4 files changed

+42
-4
lines changed

4 files changed

+42
-4
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::{
44
Address, U256,
55
};
66
use ethers::prelude::H256;
7+
use forge::revm::Database;
78
use foundry_evm::executor::fork::database::ForkDbSnapshot;
89
pub use foundry_evm::executor::fork::database::ForkedDatabase;
910

@@ -14,6 +15,8 @@ impl Db for ForkedDatabase {
1415
}
1516

1617
fn set_storage_at(&mut self, address: Address, slot: U256, val: U256) {
18+
// this ensures the account is loaded first
19+
let _ = Database::basic(self, address);
1720
self.database_mut().set_storage_at(address, slot, val)
1821
}
1922

anvil/tests/it/abi.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,9 @@ abigen!(
3939
_exists(uint256)(bool)
4040
]"#
4141
);
42+
abigen!(
43+
BUSD,
44+
r#"[
45+
balanceOf(address)(uint256)
46+
]"#
47+
);

anvil/tests/it/anvil_api.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ use crate::{abi::*, fork::fork_config};
33
use anvil::{spawn, Hardfork, NodeConfig};
44
use anvil_core::eth::EthRequest;
55
use ethers::{
6-
abi::ethereum_types::BigEndianHash,
6+
abi::{ethereum_types::BigEndianHash, AbiDecode},
77
prelude::{Middleware, SignerMiddleware},
88
types::{Address, BlockNumber, TransactionRequest, H256, U256},
9+
utils::hex,
910
};
1011
use std::{
1112
sync::Arc,
@@ -237,3 +238,31 @@ async fn test_timestamp_interval() {
237238
// check interval is disabled
238239
assert!(another_block.timestamp - new_block.timestamp < U256::from(interval));
239240
}
241+
242+
// <https://github.com/foundry-rs/foundry/issues/2341>
243+
#[tokio::test(flavor = "multi_thread")]
244+
async fn test_can_set_storage_bsc_fork() {
245+
let (api, handle) =
246+
spawn(NodeConfig::test().with_eth_rpc_url(Some("https://bsc-dataseed.binance.org/"))).await;
247+
let provider = Arc::new(handle.http_provider());
248+
249+
let busd_addr: Address = "0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56".parse().unwrap();
250+
let idx: U256 =
251+
"0xa6eef7e35abe7026729641147f7915573c7e97b47efa546f5f6e3230263bcb49".parse().unwrap();
252+
let value: H256 =
253+
"0x0000000000000000000000000000000000000000000000000000000000003039".parse().unwrap();
254+
255+
api.anvil_set_storage_at(busd_addr, idx, value).await.unwrap();
256+
let storage = api.storage_at(busd_addr, idx, None).await.unwrap();
257+
assert_eq!(storage, value);
258+
259+
let input =
260+
hex::decode("70a082310000000000000000000000000000000000000000000000000000000000000000")
261+
.unwrap();
262+
263+
let busd = BUSD::new(busd_addr, provider);
264+
let call = busd::BalanceOfCall::decode(&input).unwrap();
265+
266+
let balance = busd.balance_of(call.0).call().await.unwrap();
267+
assert_eq!(balance, U256::from(12345u64));
268+
}

evm/src/executor/fork/database.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,19 +141,19 @@ impl ForkedDatabase {
141141

142142
impl Database for ForkedDatabase {
143143
fn basic(&mut self, address: Address) -> AccountInfo {
144-
self.cache_db.basic(address)
144+
Database::basic(&mut self.cache_db, address)
145145
}
146146

147147
fn code_by_hash(&mut self, code_hash: H256) -> Bytecode {
148-
self.cache_db.code_by_hash(code_hash)
148+
Database::code_by_hash(&mut self.cache_db, code_hash)
149149
}
150150

151151
fn storage(&mut self, address: Address, index: U256) -> U256 {
152152
Database::storage(&mut self.cache_db, address, index)
153153
}
154154

155155
fn block_hash(&mut self, number: U256) -> H256 {
156-
self.cache_db.block_hash(number)
156+
Database::block_hash(&mut self.cache_db, number)
157157
}
158158
}
159159

0 commit comments

Comments
 (0)