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: vm rpc encoding #8288

Merged
merged 6 commits into from
Jun 28, 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
13 changes: 11 additions & 2 deletions crates/cheatcodes/src/evm/fork.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{Cheatcode, Cheatcodes, CheatsCtxt, DatabaseExt, Result, Vm::*};
use alloy_dyn_abi::DynSolValue;
use alloy_primitives::{B256, U256};
use alloy_provider::Provider;
use alloy_rpc_types::Filter;
Expand Down Expand Up @@ -243,8 +244,16 @@ impl Cheatcode for rpcCall {
foundry_common::block_on(provider.raw_request(method.clone().into(), params_json))
.map_err(|err| fmt_err!("{method:?}: {err}"))?;

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

Ok(result_as_tokens.abi_encode())
}
Expand Down
3 changes: 3 additions & 0 deletions crates/forge/tests/it/repros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,3 +342,6 @@ test_repro!(2851, false, None, |res| {

// https://github.com/foundry-rs/foundry/issues/8006
test_repro!(8006);

// https://github.com/foundry-rs/foundry/issues/8287
test_repro!(8287);
27 changes: 27 additions & 0 deletions testdata/default/repros/Issue8287.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity 0.8.18;

import "ds-test/test.sol";
import "cheats/Vm.sol";

// https://github.com/foundry-rs/foundry/issues/8287
contract Issue8287Test is DSTest {
Vm constant vm = Vm(HEVM_ADDRESS);

function testRpcBalance() public {
uint256 f2 = vm.createSelectFork("rpcAlias", 10);
bytes memory data = vm.rpc("eth_getBalance", "[\"0x551e7784778ef8e048e495df49f2614f84a4f1dc\",\"0x0\"]");
string memory m = vm.toString(data);
assertEq(m, "0x2086ac351052600000");
}

function testRpcStorage() public {
uint256 f2 = vm.createSelectFork("rpcAlias", 10);
bytes memory data = vm.rpc(
"eth_getStorageAt",
"[\"0x551e7784778ef8e048e495df49f2614f84a4f1dc\",\"0x40BdB4497614bAe1A67061EE20AAdE3c2067AC9e\",\"0x0\"]"
);
string memory m = vm.toString(data);
assertEq(m, "0x0000000000000000000000000000000000000000000000000000000000000000");
}
}
Loading