Skip to content

Commit

Permalink
fix(cheatcodes): convert fixed bytes to bytes in vm.rpc tuple result (f…
Browse files Browse the repository at this point in the history
…oundry-rs#9117)

* fix(cheatcodes): convert fixed bytes to bytes in vm.rpc tuple result

* Changes after review: recursive convert_to_bytes fn
  • Loading branch information
grandizzy authored and rplusq committed Nov 29, 2024
1 parent 4b40d59 commit 6bb42a3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
30 changes: 20 additions & 10 deletions crates/cheatcodes/src/evm/fork.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::{Cheatcode, Cheatcodes, CheatcodesExecutor, CheatsCtxt, DatabaseExt, Result, Vm::*};
use crate::{
json::json_value_to_token, Cheatcode, Cheatcodes, CheatcodesExecutor, CheatsCtxt, DatabaseExt,
Result, Vm::*,
};
use alloy_dyn_abi::DynSolValue;
use alloy_primitives::{B256, U256};
use alloy_provider::Provider;
Expand Down Expand Up @@ -375,18 +378,25 @@ fn rpc_call(url: &str, method: &str, params: &str) -> Result {
let result =
foundry_common::block_on(provider.raw_request(method.to_string().into(), params_json))
.map_err(|err| fmt_err!("{method:?}: {err}"))?;
let result_as_tokens = convert_to_bytes(
&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}"))?
{
// Convert fixed bytes to bytes to prevent encoding issues.
Ok(result_as_tokens.abi_encode())
}

/// Convert fixed bytes and address values to bytes in order to prevent encoding issues.
fn convert_to_bytes(token: &DynSolValue) -> DynSolValue {
match token {
// Convert fixed bytes to prevent encoding issues.
// See: <https://github.com/foundry-rs/foundry/issues/8287>
DynSolValue::FixedBytes(bytes, size) => {
DynSolValue::Bytes(bytes.as_slice()[..size].to_vec())
DynSolValue::Bytes(bytes.as_slice()[..*size].to_vec())
}
DynSolValue::Address(addr) => DynSolValue::Bytes(addr.to_vec()),
val => val,
};

Ok(result_as_tokens.abi_encode())
// Convert tuple values to prevent encoding issues.
// See: <https://github.com/foundry-rs/foundry/issues/7858>
DynSolValue::Tuple(vals) => DynSolValue::Tuple(vals.iter().map(convert_to_bytes).collect()),
val => val.clone(),
}
}
6 changes: 6 additions & 0 deletions testdata/default/cheats/Fork2.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,12 @@ contract ForkTest is DSTest {
uint256 decodedResult = vm.parseUint(vm.toString(result));
assertGt(decodedResult, 20_000_000);
}

// <https://github.com/foundry-rs/foundry/issues/7858>
function testRpcTransactionByHash() public {
string memory param = string.concat('["0xe1a0fba63292976050b2fbf4379a1901691355ed138784b4e0d1854b4cf9193e"]');
vm.rpc("sepolia", "eth_getTransactionByHash", param);
}
}

contract DummyContract {
Expand Down

0 comments on commit 6bb42a3

Please sign in to comment.