From dc4fa1d67f3742c3b0edacb2445ffe29fcd24d16 Mon Sep 17 00:00:00 2001 From: Satoshi Otomakan Date: Thu, 5 Oct 2023 16:31:59 +0200 Subject: [PATCH] [eth]: Minor refactor --- rust/Cargo.lock | 1 - rust/tw_evm/Cargo.toml | 4 ---- rust/tw_evm/fuzz/Cargo.toml | 1 - .../fuzz/fuzz_targets/abi_decode_value.rs | 14 ++---------- .../fuzz/fuzz_targets/abi_encode_function.rs | 21 ++---------------- .../fuzz_targets/abi_function_decode_input.rs | 22 +++---------------- rust/tw_evm/src/abi/non_empty_array.rs | 17 +------------- rust/tw_evm/src/abi/param.rs | 1 - rust/tw_evm/src/abi/param_token.rs | 1 - rust/tw_evm/src/abi/param_type/mod.rs | 1 - rust/tw_evm/src/abi/token.rs | 1 - rust/tw_evm/src/abi/uint.rs | 11 ---------- rust/tw_evm/src/address.rs | 1 - rust/tw_evm/src/modules/abi_encoder.rs | 2 +- 14 files changed, 9 insertions(+), 89 deletions(-) diff --git a/rust/Cargo.lock b/rust/Cargo.lock index 9fbf99adf40..7ae598db1fd 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -1498,7 +1498,6 @@ dependencies = [ name = "tw_evm" version = "0.1.0" dependencies = [ - "arbitrary", "itertools", "lazy_static", "rlp", diff --git a/rust/tw_evm/Cargo.toml b/rust/tw_evm/Cargo.toml index 7ca9948ad5d..43c3f334c72 100644 --- a/rust/tw_evm/Cargo.toml +++ b/rust/tw_evm/Cargo.toml @@ -3,11 +3,7 @@ name = "tw_evm" version = "0.1.0" edition = "2021" -[features] -arbitrary = ["dep:arbitrary", "tw_hash/arbitrary", "tw_number/arbitrary"] - [dependencies] -arbitrary = { version = "1", features = ["derive"], optional = true } itertools = "0.10.5" lazy_static = "1.4.0" rlp = "0.5.2" diff --git a/rust/tw_evm/fuzz/Cargo.toml b/rust/tw_evm/fuzz/Cargo.toml index bc553ac5071..6fad417a7d8 100644 --- a/rust/tw_evm/fuzz/Cargo.toml +++ b/rust/tw_evm/fuzz/Cargo.toml @@ -15,7 +15,6 @@ tw_proto = { path = "../../tw_proto", features = ["fuzz"] } [dependencies.tw_evm] path = ".." -features = ["arbitrary"] # Prevent this from interfering with workspaces [workspace] diff --git a/rust/tw_evm/fuzz/fuzz_targets/abi_decode_value.rs b/rust/tw_evm/fuzz/fuzz_targets/abi_decode_value.rs index 4b9088794b9..4da7cf941e9 100644 --- a/rust/tw_evm/fuzz/fuzz_targets/abi_decode_value.rs +++ b/rust/tw_evm/fuzz/fuzz_targets/abi_decode_value.rs @@ -6,21 +6,11 @@ #![no_main] -use libfuzzer_sys::{arbitrary, fuzz_target}; +use libfuzzer_sys::fuzz_target; use tw_evm::evm_context::StandardEvmContext; use tw_evm::modules::abi_encoder::AbiEncoder; use tw_proto::EthereumAbi::Proto; -#[derive(arbitrary::Arbitrary, Debug)] -struct ValueDecodingInput<'a> { - encoded: &'a [u8], - param_type: &'a str, -} - -fuzz_target!(|data: ValueDecodingInput<'_>| { - let input = Proto::ValueDecodingInput { - encoded: data.encoded.into(), - param_type: data.param_type.into(), - }; +fuzz_target!(|input: Proto::ValueDecodingInput<'_>| { let _ = AbiEncoder::::decode_value(input); }); diff --git a/rust/tw_evm/fuzz/fuzz_targets/abi_encode_function.rs b/rust/tw_evm/fuzz/fuzz_targets/abi_encode_function.rs index 265357eeefd..a8652534169 100644 --- a/rust/tw_evm/fuzz/fuzz_targets/abi_encode_function.rs +++ b/rust/tw_evm/fuzz/fuzz_targets/abi_encode_function.rs @@ -6,28 +6,11 @@ #![no_main] -use libfuzzer_sys::{arbitrary, fuzz_target}; -use tw_evm::abi::token::Token; +use libfuzzer_sys::fuzz_target; use tw_evm::evm_context::StandardEvmContext; use tw_evm::modules::abi_encoder::AbiEncoder; use tw_proto::EthereumAbi::Proto; -#[derive(arbitrary::Arbitrary, Debug)] -struct FunctionEncodingInput<'a> { - function_name: &'a str, - tokens: Vec, -} - -fuzz_target!(|data: FunctionEncodingInput<'_>| { - let tokens = data - .tokens - .into_iter() - .map(AbiEncoder::::token_to_proto) - .collect(); - - let input = Proto::FunctionEncodingInput { - function_name: data.function_name.into(), - tokens, - }; +fuzz_target!(|input: Proto::FunctionEncodingInput<'_>| { let _ = AbiEncoder::::encode_contract_call(input); }); diff --git a/rust/tw_evm/fuzz/fuzz_targets/abi_function_decode_input.rs b/rust/tw_evm/fuzz/fuzz_targets/abi_function_decode_input.rs index 762ce0a16b2..cfba6120348 100644 --- a/rust/tw_evm/fuzz/fuzz_targets/abi_function_decode_input.rs +++ b/rust/tw_evm/fuzz/fuzz_targets/abi_function_decode_input.rs @@ -6,27 +6,11 @@ #![no_main] -use libfuzzer_sys::{arbitrary, fuzz_target}; -use tw_evm::abi::param::Param; +use libfuzzer_sys::fuzz_target; use tw_evm::evm_context::StandardEvmContext; use tw_evm::modules::abi_encoder::AbiEncoder; use tw_proto::EthereumAbi::Proto; -#[derive(arbitrary::Arbitrary, Debug)] -struct ContractCallDecodingInput<'a> { - encoded: &'a [u8], - inputs: Vec, -} - -fuzz_target!(|data: ContractCallDecodingInput<'_>| { - let params = data - .inputs - .into_iter() - .map(AbiEncoder::::param_to_proto) - .collect(); - let decoding_input = Proto::ParamsDecodingInput { - encoded: data.encoded.into(), - abi: Proto::mod_ParamsDecodingInput::OneOfabi::abi_params(Proto::AbiParams { params }), - }; - let _ = AbiEncoder::::decode_params(decoding_input); +fuzz_target!(|input: Proto::ParamsDecodingInput<'_>| { + let _ = AbiEncoder::::decode_params(input); }); diff --git a/rust/tw_evm/src/abi/non_empty_array.rs b/rust/tw_evm/src/abi/non_empty_array.rs index b0d7763e99d..b6a7a535c28 100644 --- a/rust/tw_evm/src/abi/non_empty_array.rs +++ b/rust/tw_evm/src/abi/non_empty_array.rs @@ -13,7 +13,6 @@ pub type NonEmptyBytes = NonEmptyArray; /// A convenient wrapper over `NonZeroUsize`. #[derive(Copy, Clone, PartialEq)] -#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))] pub struct NonZeroLen(NonZeroUsize); impl NonZeroLen { @@ -52,7 +51,7 @@ impl NonEmptyArray { } pub fn len(&self) -> NonZeroLen { - NonZeroLen::new(self.0.len()).expect("`FixedArray` must have at least one element") + NonZeroLen::new(self.0.len()).expect("`NonEmptyArray` must have at least one element") } pub fn into_vec(self) -> Vec { @@ -82,17 +81,3 @@ impl AsRef<[u8]> for NonEmptyArray { &self.0 } } - -#[cfg(feature = "arbitrary")] -impl<'a, T> arbitrary::Arbitrary<'a> for NonEmptyArray -where - T: arbitrary::Arbitrary<'a>, -{ - fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result { - let arr: Vec = Vec::arbitrary(u)?; - if arr.is_empty() { - return Err(arbitrary::Error::EmptyChoose); - } - Ok(NonEmptyArray(arr)) - } -} diff --git a/rust/tw_evm/src/abi/param.rs b/rust/tw_evm/src/abi/param.rs index 92b2f19e2da..93ca5dcb3eb 100644 --- a/rust/tw_evm/src/abi/param.rs +++ b/rust/tw_evm/src/abi/param.rs @@ -10,7 +10,6 @@ use serde::{de::Error as DeError, Deserialize, Deserializer}; use std::fmt::Formatter; #[derive(Clone, Debug, PartialEq)] -#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))] pub struct Param { /// Param name. pub name: Option, diff --git a/rust/tw_evm/src/abi/param_token.rs b/rust/tw_evm/src/abi/param_token.rs index 724a615bbde..420b8e6f217 100644 --- a/rust/tw_evm/src/abi/param_token.rs +++ b/rust/tw_evm/src/abi/param_token.rs @@ -10,7 +10,6 @@ use serde::ser::SerializeMap; use serde::{Serialize, Serializer}; #[derive(Clone, Debug, PartialEq)] -#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))] pub struct NamedToken { /// Optional param name. pub name: Option, diff --git a/rust/tw_evm/src/abi/param_type/mod.rs b/rust/tw_evm/src/abi/param_type/mod.rs index 1e3ff5ec2a6..1ca70655574 100644 --- a/rust/tw_evm/src/abi/param_type/mod.rs +++ b/rust/tw_evm/src/abi/param_type/mod.rs @@ -18,7 +18,6 @@ use reader::Reader; use writer::Writer; #[derive(Clone, Debug, PartialEq)] -#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))] pub enum ParamType { /// Address. /// diff --git a/rust/tw_evm/src/abi/token.rs b/rust/tw_evm/src/abi/token.rs index ec68897ed51..3d52d080d98 100644 --- a/rust/tw_evm/src/abi/token.rs +++ b/rust/tw_evm/src/abi/token.rs @@ -17,7 +17,6 @@ use tw_memory::Data; use tw_number::{I256, U256}; #[derive(Debug, Clone, PartialEq)] -#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))] pub enum Token { /// Address. /// diff --git a/rust/tw_evm/src/abi/uint.rs b/rust/tw_evm/src/abi/uint.rs index c0a125c2d76..3b5f6021432 100644 --- a/rust/tw_evm/src/abi/uint.rs +++ b/rust/tw_evm/src/abi/uint.rs @@ -11,17 +11,6 @@ use tw_number::U256; #[derive(Clone, Copy, PartialEq)] pub struct UintBits(usize); -#[cfg(feature = "arbitrary")] -impl<'a> arbitrary::Arbitrary<'a> for UintBits { - fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result { - let bits = usize::arbitrary(u)? % U256::BITS % 8; - if bits == 0 { - return Ok(UintBits(8)); - } - Ok(UintBits(bits)) - } -} - impl Default for UintBits { fn default() -> Self { UintBits::new(U256::BITS).expect("U256::BITS must be a valid number of bits") diff --git a/rust/tw_evm/src/address.rs b/rust/tw_evm/src/address.rs index fa04f1b59e4..c53887b5662 100644 --- a/rust/tw_evm/src/address.rs +++ b/rust/tw_evm/src/address.rs @@ -31,7 +31,6 @@ pub trait EvmAddress: FromStr + Into
{ /// Represents an Ethereum address. #[derive(Clone, Copy, Debug, Default, PartialEq)] -#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))] pub struct Address { bytes: H160, } diff --git a/rust/tw_evm/src/modules/abi_encoder.rs b/rust/tw_evm/src/modules/abi_encoder.rs index 11394cbfa3e..6879a1ab2e4 100644 --- a/rust/tw_evm/src/modules/abi_encoder.rs +++ b/rust/tw_evm/src/modules/abi_encoder.rs @@ -256,7 +256,7 @@ impl AbiEncoder { }, TokenEnum::byte_array(bytes) => Ok(Token::Bytes(bytes.to_vec())), TokenEnum::byte_array_fix(bytes) => { - let checked_bytes = NonEmptyBytes::new(bytes.iter().copied().collect())?; + let checked_bytes = NonEmptyBytes::new(bytes.to_vec())?; Ok(Token::FixedBytes(checked_bytes)) }, TokenEnum::array(arr) => {