Skip to content

Commit

Permalink
[eth]: Minor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
satoshiotomakan committed Oct 5, 2023
1 parent 0daa10c commit dc4fa1d
Show file tree
Hide file tree
Showing 14 changed files with 9 additions and 89 deletions.
1 change: 0 additions & 1 deletion rust/Cargo.lock

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

4 changes: 0 additions & 4 deletions rust/tw_evm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 0 additions & 1 deletion rust/tw_evm/fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ tw_proto = { path = "../../tw_proto", features = ["fuzz"] }

[dependencies.tw_evm]
path = ".."
features = ["arbitrary"]

# Prevent this from interfering with workspaces
[workspace]
Expand Down
14 changes: 2 additions & 12 deletions rust/tw_evm/fuzz/fuzz_targets/abi_decode_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<StandardEvmContext>::decode_value(input);
});
21 changes: 2 additions & 19 deletions rust/tw_evm/fuzz/fuzz_targets/abi_encode_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Token>,
}

fuzz_target!(|data: FunctionEncodingInput<'_>| {
let tokens = data
.tokens
.into_iter()
.map(AbiEncoder::<StandardEvmContext>::token_to_proto)
.collect();

let input = Proto::FunctionEncodingInput {
function_name: data.function_name.into(),
tokens,
};
fuzz_target!(|input: Proto::FunctionEncodingInput<'_>| {
let _ = AbiEncoder::<StandardEvmContext>::encode_contract_call(input);
});
22 changes: 3 additions & 19 deletions rust/tw_evm/fuzz/fuzz_targets/abi_function_decode_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Param>,
}

fuzz_target!(|data: ContractCallDecodingInput<'_>| {
let params = data
.inputs
.into_iter()
.map(AbiEncoder::<StandardEvmContext>::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::<StandardEvmContext>::decode_params(decoding_input);
fuzz_target!(|input: Proto::ParamsDecodingInput<'_>| {
let _ = AbiEncoder::<StandardEvmContext>::decode_params(input);
});
17 changes: 1 addition & 16 deletions rust/tw_evm/src/abi/non_empty_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ pub type NonEmptyBytes = NonEmptyArray<u8>;

/// A convenient wrapper over `NonZeroUsize`.
#[derive(Copy, Clone, PartialEq)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub struct NonZeroLen(NonZeroUsize);

impl NonZeroLen {
Expand Down Expand Up @@ -52,7 +51,7 @@ impl<T> NonEmptyArray<T> {
}

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<T> {
Expand Down Expand Up @@ -82,17 +81,3 @@ impl AsRef<[u8]> for NonEmptyArray<u8> {
&self.0
}
}

#[cfg(feature = "arbitrary")]
impl<'a, T> arbitrary::Arbitrary<'a> for NonEmptyArray<T>
where
T: arbitrary::Arbitrary<'a>,
{
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
let arr: Vec<T> = Vec::arbitrary(u)?;
if arr.is_empty() {
return Err(arbitrary::Error::EmptyChoose);
}
Ok(NonEmptyArray(arr))
}
}
1 change: 0 additions & 1 deletion rust/tw_evm/src/abi/param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
Expand Down
1 change: 0 additions & 1 deletion rust/tw_evm/src/abi/param_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
Expand Down
1 change: 0 additions & 1 deletion rust/tw_evm/src/abi/param_type/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down
1 change: 0 additions & 1 deletion rust/tw_evm/src/abi/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down
11 changes: 0 additions & 11 deletions rust/tw_evm/src/abi/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self> {
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")
Expand Down
1 change: 0 additions & 1 deletion rust/tw_evm/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ pub trait EvmAddress: FromStr<Err = AddressError> + Into<Address> {

/// Represents an Ethereum address.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub struct Address {
bytes: H160,
}
Expand Down
2 changes: 1 addition & 1 deletion rust/tw_evm/src/modules/abi_encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ impl<Context: EvmContext> AbiEncoder<Context> {
},
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) => {
Expand Down

0 comments on commit dc4fa1d

Please sign in to comment.