Skip to content

Commit b3c872b

Browse files
authored
fix: vm rpc encoding (#8288)
* fix: vm rpc encoding * fix test * convert fixedbytes to bytes * forge fmt * take size from fixed bytes * convert addr to bytes
1 parent b0e562f commit b3c872b

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

crates/cheatcodes/src/evm/fork.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::{Cheatcode, Cheatcodes, CheatsCtxt, DatabaseExt, Result, Vm::*};
2+
use alloy_dyn_abi::DynSolValue;
23
use alloy_primitives::{B256, U256};
34
use alloy_provider::Provider;
45
use alloy_rpc_types::Filter;
@@ -243,8 +244,16 @@ impl Cheatcode for rpcCall {
243244
foundry_common::block_on(provider.raw_request(method.clone().into(), params_json))
244245
.map_err(|err| fmt_err!("{method:?}: {err}"))?;
245246

246-
let result_as_tokens = crate::json::json_value_to_token(&result)
247-
.map_err(|err| fmt_err!("failed to parse result: {err}"))?;
247+
let result_as_tokens = match crate::json::json_value_to_token(&result)
248+
.map_err(|err| fmt_err!("failed to parse result: {err}"))?
249+
{
250+
DynSolValue::FixedBytes(bytes, size) => {
251+
// converted fixed bytes to bytes to prevent evm encoding issues: <https://github.com/foundry-rs/foundry/issues/8287>
252+
DynSolValue::Bytes(bytes.as_slice()[..size].to_vec())
253+
}
254+
DynSolValue::Address(addr) => DynSolValue::Bytes(addr.to_vec()),
255+
val => val,
256+
};
248257

249258
Ok(result_as_tokens.abi_encode())
250259
}

crates/forge/tests/it/repros.rs

+3
Original file line numberDiff line numberDiff line change
@@ -342,3 +342,6 @@ test_repro!(2851, false, None, |res| {
342342

343343
// https://github.com/foundry-rs/foundry/issues/8006
344344
test_repro!(8006);
345+
346+
// https://github.com/foundry-rs/foundry/issues/8287
347+
test_repro!(8287);
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
// https://github.com/foundry-rs/foundry/issues/8287
8+
contract Issue8287Test is DSTest {
9+
Vm constant vm = Vm(HEVM_ADDRESS);
10+
11+
function testRpcBalance() public {
12+
uint256 f2 = vm.createSelectFork("rpcAlias", 10);
13+
bytes memory data = vm.rpc("eth_getBalance", "[\"0x551e7784778ef8e048e495df49f2614f84a4f1dc\",\"0x0\"]");
14+
string memory m = vm.toString(data);
15+
assertEq(m, "0x2086ac351052600000");
16+
}
17+
18+
function testRpcStorage() public {
19+
uint256 f2 = vm.createSelectFork("rpcAlias", 10);
20+
bytes memory data = vm.rpc(
21+
"eth_getStorageAt",
22+
"[\"0x551e7784778ef8e048e495df49f2614f84a4f1dc\",\"0x40BdB4497614bAe1A67061EE20AAdE3c2067AC9e\",\"0x0\"]"
23+
);
24+
string memory m = vm.toString(data);
25+
assertEq(m, "0x0000000000000000000000000000000000000000000000000000000000000000");
26+
}
27+
}

0 commit comments

Comments
 (0)