Skip to content

Commit 3786b27

Browse files
authored
fix(cheatcodes): convert fixed bytes to bytes in vm.rpc tuple result (#9117)
* fix(cheatcodes): convert fixed bytes to bytes in vm.rpc tuple result * Changes after review: recursive convert_to_bytes fn
1 parent cc8e430 commit 3786b27

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

crates/cheatcodes/src/evm/fork.rs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use crate::{Cheatcode, Cheatcodes, CheatcodesExecutor, CheatsCtxt, DatabaseExt, Result, Vm::*};
1+
use crate::{
2+
json::json_value_to_token, Cheatcode, Cheatcodes, CheatcodesExecutor, CheatsCtxt, DatabaseExt,
3+
Result, Vm::*,
4+
};
25
use alloy_dyn_abi::DynSolValue;
36
use alloy_primitives::{B256, U256};
47
use alloy_provider::Provider;
@@ -375,18 +378,25 @@ fn rpc_call(url: &str, method: &str, params: &str) -> Result {
375378
let result =
376379
foundry_common::block_on(provider.raw_request(method.to_string().into(), params_json))
377380
.map_err(|err| fmt_err!("{method:?}: {err}"))?;
381+
let result_as_tokens = convert_to_bytes(
382+
&json_value_to_token(&result).map_err(|err| fmt_err!("failed to parse result: {err}"))?,
383+
);
378384

379-
let result_as_tokens = match crate::json::json_value_to_token(&result)
380-
.map_err(|err| fmt_err!("failed to parse result: {err}"))?
381-
{
382-
// Convert fixed bytes to bytes to prevent encoding issues.
385+
Ok(result_as_tokens.abi_encode())
386+
}
387+
388+
/// Convert fixed bytes and address values to bytes in order to prevent encoding issues.
389+
fn convert_to_bytes(token: &DynSolValue) -> DynSolValue {
390+
match token {
391+
// Convert fixed bytes to prevent encoding issues.
383392
// See: <https://github.com/foundry-rs/foundry/issues/8287>
384393
DynSolValue::FixedBytes(bytes, size) => {
385-
DynSolValue::Bytes(bytes.as_slice()[..size].to_vec())
394+
DynSolValue::Bytes(bytes.as_slice()[..*size].to_vec())
386395
}
387396
DynSolValue::Address(addr) => DynSolValue::Bytes(addr.to_vec()),
388-
val => val,
389-
};
390-
391-
Ok(result_as_tokens.abi_encode())
397+
// Convert tuple values to prevent encoding issues.
398+
// See: <https://github.com/foundry-rs/foundry/issues/7858>
399+
DynSolValue::Tuple(vals) => DynSolValue::Tuple(vals.iter().map(convert_to_bytes).collect()),
400+
val => val.clone(),
401+
}
392402
}

testdata/default/cheats/Fork2.t.sol

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,12 @@ contract ForkTest is DSTest {
234234
uint256 decodedResult = vm.parseUint(vm.toString(result));
235235
assertGt(decodedResult, 20_000_000);
236236
}
237+
238+
// <https://github.com/foundry-rs/foundry/issues/7858>
239+
function testRpcTransactionByHash() public {
240+
string memory param = string.concat('["0xe1a0fba63292976050b2fbf4379a1901691355ed138784b4e0d1854b4cf9193e"]');
241+
vm.rpc("sepolia", "eth_getTransactionByHash", param);
242+
}
237243
}
238244

239245
contract DummyContract {

0 commit comments

Comments
 (0)