Skip to content

Commit b3cd2ab

Browse files
grandizzyDaniPopes
andauthored
fix(cheatcode): use storage access address instead account access (#9646)
* fix(cheatcode): use storage access address instead account access * Update crates/cheatcodes/src/evm.rs Co-authored-by: DaniPopes <[email protected]> * Fix fmt --------- Co-authored-by: DaniPopes <[email protected]>
1 parent ad09bbe commit b3cd2ab

File tree

3 files changed

+62
-3
lines changed

3 files changed

+62
-3
lines changed

crates/cheatcodes/src/evm.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,11 +1149,12 @@ fn get_recorded_state_diffs(state: &mut Cheatcodes) -> BTreeMap<Address, Account
11491149
account_access.oldBalance != account_access.newBalance
11501150
})
11511151
.for_each(|account_access| {
1152-
let account_diff =
1153-
state_diffs.entry(account_access.account).or_insert(AccountStateDiffs {
1152+
let account_diff = state_diffs.entry(account_access.account).or_insert_with(|| {
1153+
AccountStateDiffs {
11541154
label: state.labels.get(&account_access.account).cloned(),
11551155
..Default::default()
1156-
});
1156+
}
1157+
});
11571158

11581159
// Record account balance diffs.
11591160
if account_access.oldBalance != account_access.newBalance {
@@ -1171,6 +1172,12 @@ fn get_recorded_state_diffs(state: &mut Cheatcodes) -> BTreeMap<Address, Account
11711172
// Record account state diffs.
11721173
for storage_access in &account_access.storageAccesses {
11731174
if storage_access.isWrite && !storage_access.reverted {
1175+
let account_diff = state_diffs
1176+
.entry(storage_access.account)
1177+
.or_insert_with(|| AccountStateDiffs {
1178+
label: state.labels.get(&storage_access.account).cloned(),
1179+
..Default::default()
1180+
});
11741181
// Update state diff. Do not overwrite the initial value if already set.
11751182
match account_diff.state_diff.entry(storage_access.slot) {
11761183
Entry::Vacant(slot_state_diff) => {

crates/forge/tests/it/repros.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,3 +392,6 @@ test_repro!(8639);
392392

393393
// https://github.com/foundry-rs/foundry/issues/8566
394394
test_repro!(8566);
395+
396+
// https://github.com/foundry-rs/foundry/issues/9643
397+
test_repro!(9643);
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// SPDX-License-Identifier: MIT OR Apache-2.0
2+
pragma solidity ^0.8.18;
3+
4+
import "ds-test/test.sol";
5+
import "cheats/Vm.sol";
6+
7+
contract Mock {
8+
uint256 private counter;
9+
10+
function setCounter(uint256 _counter) external {
11+
counter = _counter;
12+
}
13+
}
14+
15+
contract DelegateProxy {
16+
address internal implementation;
17+
18+
constructor(address mock) {
19+
implementation = mock;
20+
}
21+
22+
fallback() external payable {
23+
address addr = implementation;
24+
25+
assembly {
26+
calldatacopy(0, 0, calldatasize())
27+
let result := delegatecall(gas(), addr, 0, calldatasize(), 0, 0)
28+
returndatacopy(0, 0, returndatasize())
29+
switch result
30+
case 0 { revert(0, returndatasize()) }
31+
default { return(0, returndatasize()) }
32+
}
33+
}
34+
}
35+
36+
contract Issue9643Test is DSTest {
37+
Vm constant vm = Vm(HEVM_ADDRESS);
38+
39+
function test_storage_json_diff() public {
40+
vm.startStateDiffRecording();
41+
Mock proxied = Mock(address(new DelegateProxy(address(new Mock()))));
42+
proxied.setCounter(42);
43+
string memory rawDiff = vm.getStateDiffJson();
44+
assertEq(
45+
"{\"0x2e234dae75c793f67a35089c9d99245e1c58470b\":{\"label\":null,\"balanceDiff\":null,\"stateDiff\":{\"0x0000000000000000000000000000000000000000000000000000000000000000\":{\"previousValue\":\"0x0000000000000000000000000000000000000000000000000000000000000000\",\"newValue\":\"0x000000000000000000000000000000000000000000000000000000000000002a\"}}},\"0x5615deb798bb3e4dfa0139dfa1b3d433cc23b72f\":{\"label\":null,\"balanceDiff\":null,\"stateDiff\":{}}}",
46+
rawDiff
47+
);
48+
}
49+
}

0 commit comments

Comments
 (0)