Skip to content

Commit

Permalink
feat(randomBytes): adding support to generate different bytes via Rng…
Browse files Browse the repository at this point in the history
…Core (#8996)

* feat(randomBytes): adding support to generate different bytes via RngCore

Signed-off-by: Abhishekkochar <[email protected]>

* Added needed changes to util file

Signed-off-by: Abhishekkochar <[email protected]>

* Added support to get random 4 and 8 bytes

Signed-off-by: Abhishekkochar <[email protected]>

* Refractor code to suggested changes

Signed-off-by: Abhishekkochar <[email protected]>

* Fixed import with B32

Signed-off-by: Abhishekkochar <[email protected]>

* updated cheatcodes.json file

Signed-off-by: Abhishekkochar <[email protected]>

* docs

---------

Signed-off-by: Abhishekkochar <[email protected]>
Co-authored-by: DaniPopes <[email protected]>
  • Loading branch information
Abhishekkochar and DaniPopes authored Oct 3, 2024
1 parent df2e91b commit d4649bf
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 11 deletions.
50 changes: 45 additions & 5 deletions crates/cheatcodes/assets/cheatcodes.json

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

18 changes: 13 additions & 5 deletions crates/cheatcodes/spec/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2405,30 +2405,38 @@ interface Vm {
#[cheatcode(group = Utilities)]
function randomUint(uint256 min, uint256 max) external returns (uint256);

/// Returns an random `uint256` value of given bits.
/// Returns a random `uint256` value of given bits.
#[cheatcode(group = Utilities)]
function randomUint(uint256 bits) external view returns (uint256);

/// Returns a random `address`.
#[cheatcode(group = Utilities)]
function randomAddress() external returns (address);

/// Returns an random `int256` value.
/// Returns a random `int256` value.
#[cheatcode(group = Utilities)]
function randomInt() external view returns (int256);

/// Returns an random `int256` value of given bits.
/// Returns a random `int256` value of given bits.
#[cheatcode(group = Utilities)]
function randomInt(uint256 bits) external view returns (int256);

/// Returns an random `bool`.
/// Returns a random `bool`.
#[cheatcode(group = Utilities)]
function randomBool() external view returns (bool);

/// Returns an random byte array value of the given length.
/// Returns a random byte array value of the given length.
#[cheatcode(group = Utilities)]
function randomBytes(uint256 len) external view returns (bytes memory);

/// Returns a random fixed-size byte array of length 4.
#[cheatcode(group = Utilities)]
function randomBytes4() external view returns (bytes4);

/// Returns a random fixed-size byte array of length 8.
#[cheatcode(group = Utilities)]
function randomBytes8() external view returns (bytes8);

/// Pauses collection of call traces. Useful in cases when you want to skip tracing of
/// complex calls which are not useful for debugging.
#[cheatcode(group = Utilities)]
Expand Down
16 changes: 15 additions & 1 deletion crates/cheatcodes/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use crate::{Cheatcode, Cheatcodes, CheatcodesExecutor, CheatsCtxt, Result, Vm::*};
use alloy_dyn_abi::{DynSolType, DynSolValue};
use alloy_primitives::{map::HashMap, U256};
use alloy_primitives::{aliases::B32, map::HashMap, B64, U256};
use alloy_sol_types::SolValue;
use foundry_common::ens::namehash;
use foundry_evm_core::constants::DEFAULT_CREATE2_DEPLOYER;
Expand Down Expand Up @@ -133,6 +133,20 @@ impl Cheatcode for randomBytesCall {
}
}

impl Cheatcode for randomBytes4Call {
fn apply(&self, state: &mut Cheatcodes) -> Result {
let rand_u32 = state.rng().next_u32();
Ok(B32::from(rand_u32).abi_encode())
}
}

impl Cheatcode for randomBytes8Call {
fn apply(&self, state: &mut Cheatcodes) -> Result {
let rand_u64 = state.rng().next_u64();
Ok(B64::from(rand_u64).abi_encode())
}
}

impl Cheatcode for pauseTracingCall {
fn apply_full(
&self,
Expand Down
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.

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

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

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

function testRandomBytes4() public {
vm.randomBytes4();
}

function testRandomBytes8() public {
vm.randomBytes8();
}

function testFillrandomBytes() public view {
uint256 len = 16;
vm.randomBytes(len);
}
}

0 comments on commit d4649bf

Please sign in to comment.