Skip to content

add RLP cheats #11232

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

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
40 changes: 40 additions & 0 deletions crates/cheatcodes/assets/cheatcodes.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions crates/cheatcodes/spec/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2946,6 +2946,13 @@ interface Vm {
/// Generates a ready-to-sign digest of human-readable typed data following the EIP-712 standard.
#[cheatcode(group = Utilities)]
function eip712HashTypedData(string calldata jsonData) external pure returns (bytes32 digest);

/// RLP encodes a list of bytes into an RLP payload.
#[cheatcode(group = Utilities)]
function toRlp(bytes[] calldata data) external pure returns (bytes memory);
/// RLP decodes an RLP payload into a list of bytes.
#[cheatcode(group = Utilities)]
function fromRlp(bytes calldata rlp) external pure returns (bytes[] memory data);
Comment on lines +2950 to +2955
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unsure how useful this really is because this can only bytes

but imo these dont really hurt

any opinions here @grandizzy

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think is good for a start and then we could improve, @qiweiii wonder though why it is marked as breaking change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you mean in pr checklist? It should not, I just removed it

}
}

Expand Down
23 changes: 23 additions & 0 deletions crates/cheatcodes/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{Cheatcode, Cheatcodes, CheatcodesExecutor, CheatsCtxt, Result, Vm::*
use alloy_dyn_abi::{DynSolType, DynSolValue, Resolver, TypedData, eip712_parser::EncodeType};
use alloy_ens::namehash;
use alloy_primitives::{B64, Bytes, U256, aliases::B32, keccak256, map::HashMap};
use alloy_rlp::{Decodable, Encodable};
use alloy_sol_types::SolValue;
use foundry_common::{TYPE_BINDING_PREFIX, fs};
use foundry_config::fs_permissions::FsAccessKind;
Expand Down Expand Up @@ -470,3 +471,25 @@ fn get_struct_hash(primary: &str, type_def: &String, abi_encoded_data: &Bytes) -

Ok(keccak256(&bytes_to_hash).to_vec())
}

impl Cheatcode for toRlpCall {
fn apply(&self, _state: &mut Cheatcodes) -> Result {
let Self { data } = self;

let mut buf = Vec::new();
data.encode(&mut buf);

Ok(Bytes::from(buf).abi_encode())
}
}

impl Cheatcode for fromRlpCall {
fn apply(&self, _state: &mut Cheatcodes) -> Result {
let Self { rlp } = self;

let decoded: Vec<Bytes> = Vec::<Bytes>::decode(&mut rlp.as_ref())
.map_err(|e| fmt_err!("Failed to decode RLP: {e}"))?;

Ok(decoded.abi_encode())
}
}
2 changes: 2 additions & 0 deletions testdata/cheats/Vm.sol

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 48 additions & 0 deletions testdata/default/cheats/Rlp.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity ^0.8.18;

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

contract Rlp is DSTest {
Vm constant vm = Vm(HEVM_ADDRESS);

function testToRlp() public {
bytes[] memory data = new bytes[](2);
data[0] = hex"01";
data[1] = hex"02";

bytes memory rlp = vm.toRlp(data);

// Assert the expected RLP encoding for [0x01, 0x02]
// 0xc2 = list with 2 bytes total length
// 0x01 = first byte
// 0x02 = second byte
assertEq(rlp, hex"c20102");
}

function testFromRlp() public {
// RLP encoded [0x01, 0x02]
bytes memory rlp = hex"c20102";

bytes[] memory decoded = vm.fromRlp(rlp);
assertEq(decoded.length, 2);
assertEq(decoded[0], hex"01");
assertEq(decoded[1], hex"02");
}

function testRoundTrip() public {
bytes[] memory original = new bytes[](3);
original[0] = hex"deadbeef";
original[1] = hex"cafebabe";
original[2] = hex"01020304";

bytes memory rlp = vm.toRlp(original);
bytes[] memory decoded = vm.fromRlp(rlp);

assertEq(decoded.length, original.length);
for (uint256 i = 0; i < original.length; i++) {
assertEq(decoded[i], original[i]);
}
}
}
Loading