Skip to content

Commit

Permalink
feat: use proptest traits instead of custom impl
Browse files Browse the repository at this point in the history
  • Loading branch information
Evalir committed Oct 17, 2023
1 parent adc2786 commit 8bc67d2
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 33 deletions.
35 changes: 3 additions & 32 deletions crates/evm/src/fuzz/strategies/param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,8 @@ pub const MAX_ARRAY_LEN: usize = 256;
/// Given a parameter type, returns a strategy for generating values for that type.
///
/// Works with ABI Encoder v2 tuples.
pub fn fuzz_param(param: &DynSolType) -> BoxedStrategy<DynSolValue> {
match param {
DynSolType::Address => {
// The key to making this work is the `boxed()` call which type erases everything
// https://altsysrq.github.io/proptest-book/proptest/tutorial/transforming-strategies.html
any::<[u8; 20]>().prop_map(|x| DynSolValue::Address(x.into())).boxed()
}
DynSolType::Bytes => any::<Vec<u8>>().prop_map(|x| DynSolValue::Bytes(x)).boxed(),
DynSolType::Int(n) => {
super::IntStrategy::new(*n, vec![]).prop_map(|x| DynSolValue::Int(x, 256)).boxed()
}
DynSolType::Uint(n) => {
super::UintStrategy::new(*n, vec![]).prop_map(|x| DynSolValue::Uint(x, 256)).boxed()
}
DynSolType::Bool => any::<bool>().prop_map(|x| DynSolValue::Bool(x)).boxed(),
DynSolType::String => any::<Vec<u8>>()
.prop_map(|x| DynSolValue::String(unsafe { String::from_utf8_unchecked(x) }))
.boxed(),
DynSolType::Array(param) => proptest::collection::vec(fuzz_param(param), 0..MAX_ARRAY_LEN)
.prop_map(DynSolValue::Array)
.boxed(),
DynSolType::FixedBytes(size) => prop::collection::vec(any::<u8>(), *size)
.prop_map(|e| DynSolValue::FixedBytes(FixedBytes::from_slice(&e), *size))
.boxed(),
DynSolType::FixedArray(param, size) => prop::collection::vec(fuzz_param(param), *size)
.prop_map(DynSolValue::FixedArray)
.boxed(),
DynSolType::Tuple(params) => {
params.iter().map(fuzz_param).collect::<Vec<_>>().prop_map(DynSolValue::Tuple).boxed()
}
_ => panic!("Unimplemented"),
}
pub fn fuzz_param(param: &DynSolType) -> SBoxedStrategy<DynSolValue> {
return DynSolValue::type_strategy(param)
}

/// Given a parameter type, returns a strategy for generating values for that type, given some EVM
Expand Down Expand Up @@ -143,6 +113,7 @@ mod tests {
use foundry_config::FuzzDictionaryConfig;
use revm::db::{CacheDB, EmptyDB};

// TODO: Need a human readable function parser to re-enable the test.
// #[test]
// fn can_fuzz_array() {
// let f = "function testArray(uint64[2] calldata values)";
Expand Down
2 changes: 1 addition & 1 deletion crates/evm/src/fuzz/strategies/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
fuzz::invariant::{ArtifactFilters, FuzzRunIdentifiedContracts},
utils,
};
use alloy_dyn_abi::{DynSolType, DynSolValue, JsonAbiExt};
use alloy_dyn_abi::{DynSolType, JsonAbiExt};
use alloy_json_abi::Function;
use alloy_primitives::{Address, B256, U256};
use bytes::Bytes;
Expand Down

0 comments on commit 8bc67d2

Please sign in to comment.