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

feat: add vm serialize address cheatcode #28

Open
wants to merge 4 commits into
base: grw/feat-cheatcode-tracer
Choose a base branch
from
Open
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: 12 additions & 1 deletion e2e-tests/contracts/TestCheatcodes.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ contract TestCheatcodes {
require(finalBlockNumber == blockNumber, "block number was not changed");
}

function testSerializeAddress(address addr) external {
(bool success, bytes memory data) = CHEATCODE_ADDRESS.call(
abi.encodeWithSignature("serializeAddress(string,string,address)", "test", "test", addr)
);
require(success, "serializeAddress failed");
bytes32 serialized_data = abi.decode(data, (bytes32));
require(serialized_data == bytes32(data), "serialized data mismatch");
}

function testSetNonce(address account, uint64 nonce) external {
(bool success, bytes memory data) = CHEATCODE_ADDRESS.call(
abi.encodeWithSignature("setNonce(address,uint64)", account, nonce)
Expand Down Expand Up @@ -151,7 +160,9 @@ contract TestCheatcodes {

function testLoad(bytes32 slot) external {
TestLoadTarget testLoadTarget = new TestLoadTarget();
(bool success, bytes memory data) = CHEATCODE_ADDRESS.call(abi.encodeWithSignature("load(address,bytes32)", address(testLoadTarget), slot));
(bool success, bytes memory data) = CHEATCODE_ADDRESS.call(
abi.encodeWithSignature("load(address,bytes32)", address(testLoadTarget), slot)
);
require(success, "load failed");
bytes32 loadedValue = abi.decode(data, (bytes32));
require(loadedValue == bytes32(uint256(1337)), "address mismatch");
Expand Down
17 changes: 17 additions & 0 deletions e2e-tests/test/cheatcodes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,23 @@ describe("Cheatcodes", function () {
expect(receipt.status).to.eq(1);
});

it("Should test vm.serializeAddress", async function () {
// Arrange
const wallet = new Wallet(RichAccounts[0].PrivateKey);
const deployer = new Deployer(hre, wallet);
const randomWallet = Wallet.createRandom().connect(provider);

// Act
const cheatcodes = await deployContract(deployer, "TestCheatcodes", []);
const tx = await cheatcodes.testSerializeAddress(randomWallet.address, {
gasLimit: 1000000,
});
const receipt = await tx.wait();

// Assert
expect(receipt.status).to.eq(1);
});

it("Should test vm.setNonce and vm.getNonce", async function () {
// Arrange
const wallet = new Wallet(RichAccounts[0].PrivateKey);
Expand Down
39 changes: 39 additions & 0 deletions src/cheatcodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
},
};
use std::{
collections::HashMap,
fmt::Debug,
sync::{Arc, Mutex, RwLock},
};
Expand Down Expand Up @@ -67,6 +68,7 @@
near_calls: usize,
storage_write_queue: Vec<StorageWrite>,
start_prank_opts: Option<StartPrankOpts>,
serialized_objects: HashMap<String, String>,

Check failure on line 71 in src/cheatcodes.rs

View workflow job for this annotation

GitHub Actions / unit-tests (macos-latest)

field `serialized_objects` is never read

Check failure on line 71 in src/cheatcodes.rs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

field `serialized_objects` is never read

Check failure on line 71 in src/cheatcodes.rs

View workflow job for this annotation

GitHub Actions / lint

field `serialized_objects` is never read
Jrigada marked this conversation as resolved.
Show resolved Hide resolved
}

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -96,6 +98,7 @@
function getNonce(address account)
function load(address account, bytes32 slot)
function roll(uint256 blockNumber)
function serializeAddress(string objectKey, string valueKey, address value)
function setNonce(address account, uint64 nonce)
function startPrank(address sender)
function startPrank(address sender, address origin)
Expand Down Expand Up @@ -230,6 +233,7 @@
return_ptr: None,
near_calls: 0,
storage_write_queue: Vec::new(),
serialized_objects: HashMap::new(),
}
}

Expand Down Expand Up @@ -311,6 +315,27 @@
let value = storage.read_value(&key);
self.returndata = Some(vec![h256_to_u256(value)]);
}
SerializeAddress(SerializeAddressCall {
object_key,
value_key,
value,
}) => {
tracing::info!(
"Serializing address {:?} with key {:?} to object {:?}",
value,
value_key,
object_key
);
let json_value = serde_json::json!({
value_key: value
});

let serialized = json_value.to_string();

let serialized_u256 = Self::serialize_to_u256_vec(&serialized);

self.returndata = Some(serialized_u256)
}
SetNonce(SetNonceCall { account, nonce }) => {
tracing::info!("Setting nonce for {account:?} to {nonce}");
let nonce_key = get_nonce_key(&account);
Expand Down Expand Up @@ -422,6 +447,20 @@
write_value: value,
});
}

fn serialize_to_u256_vec(serialized: &str) -> Vec<U256> {
let serialized_bytes = serialized.as_bytes().to_vec();
let serialized_u256 = serialized_bytes
.chunks(32)
.map(|chunk| {
let mut bytes = [0u8; 32];
bytes[..chunk.len()].copy_from_slice(chunk);
U256::from(bytes)
})
.collect::<Vec<U256>>();

serialized_u256
}
}

pub struct CheatcodeNodeContext<T> {
Expand Down
Loading