Skip to content

Commit

Permalink
feat(dyn-abi): clean up and improve performance (#174)
Browse files Browse the repository at this point in the history
* feat(dyn-abi): clean up and improve performance

* chore: clippy
  • Loading branch information
DaniPopes authored Jul 4, 2023
1 parent 96b150d commit 9c85a28
Show file tree
Hide file tree
Showing 22 changed files with 1,040 additions and 992 deletions.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,13 @@ thiserror = "1.0"

# misc
arbitrary = "1.3"
criterion = "0.5"
derive_arbitrary = "1.3"
arrayvec = { version = "0.7", default-features = false }
bytes = { version = "1.4", default-features = false }
criterion = "0.5"
derive_arbitrary = "1.3"
getrandom = "0.2"
hex = { package = "const-hex", version = ">=1.5", default-features = false, features = ["alloc"] }
itoa = "1"
once_cell = "1"
proptest = "1"
proptest-derive = "0.3"
Expand Down
1 change: 1 addition & 0 deletions crates/dyn-abi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ alloy-sol-types = { workspace = true, features = ["eip712-serde"] }
alloy-primitives.workspace = true

hex.workspace = true
itoa.workspace = true
serde.workspace = true
serde_json = { workspace = true, features = ["alloc"] }

Expand Down
10 changes: 8 additions & 2 deletions crates/dyn-abi/src/eip712/coerce.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
use crate::{no_std_prelude::*, DynAbiError, DynSolType, DynSolValue, Word};
use crate::{DynAbiError, DynSolType, DynSolValue, Word};
use alloc::{
borrow::ToOwned,
boxed::Box,
string::{String, ToString},
vec::Vec,
};
use alloy_primitives::{Address, I256, U256};

/// Coerce a `serde_json::Value` to a `DynSolValue::Address`
Expand Down Expand Up @@ -40,7 +46,7 @@ pub(crate) fn bytes(value: &serde_json::Value) -> Result<DynSolValue, DynAbiErro
pub(crate) fn fixed_bytes(n: usize, value: &serde_json::Value) -> Result<DynSolValue, DynAbiError> {
if let Some(Ok(buf)) = value.as_str().map(hex::decode) {
let mut word: Word = Default::default();
let min = if buf.len() > n { n } else { buf.len() };
let min = n.min(buf.len());
word[..min].copy_from_slice(&buf[..min]);
return Ok(DynSolValue::FixedBytes(word, n))
}
Expand Down
1 change: 0 additions & 1 deletion crates/dyn-abi/src/eip712/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
//!
//! <https://eips.ethereum.org/EIPS/eip-712#specification-of-the-eth_signtypeddata-json-rpc>

/// EIP-712 specific parsing structures.
pub mod parser;

mod typed_data;
Expand Down
4 changes: 3 additions & 1 deletion crates/dyn-abi/src/eip712/parser.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
//! EIP-712 specific parsing structures.

use crate::{
eip712::resolver::{PropertyDef, TypeDef},
no_std_prelude::*,
parser::TypeSpecifier,
DynAbiError,
};
use alloc::vec::Vec;

/// A property is a type and a name. Of the form `type name`. E.g.
/// `uint256 foo` or `(MyStruct[23],bool) bar`.
Expand Down
11 changes: 8 additions & 3 deletions crates/dyn-abi/src/eip712/resolver.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
use crate::{
eip712::typed_data::Eip712Types,
eip712_parser::EncodeType,
no_std_prelude::*,
parser::{RootType, TypeSpecifier, TypeStem},
DynAbiError, DynSolType, DynSolValue,
};
use alloc::collections::{BTreeMap, BTreeSet};
use alloc::{
borrow::ToOwned,
boxed::Box,
collections::{BTreeMap, BTreeSet},
string::{String, ToString},
vec::Vec,
};
use alloy_primitives::{keccak256, B256};
use alloy_sol_types::SolStruct;
use core::{cmp::Ordering, fmt};
Expand Down Expand Up @@ -400,7 +405,7 @@ impl Resolver {
};

let ty = type_spec.sizes.iter().fold(ty, |acc, item| match item {
Some(size) => DynSolType::FixedArray(Box::new(acc), *size),
Some(size) => DynSolType::FixedArray(Box::new(acc), size.get()),
None => DynSolType::Array(Box::new(acc)),
});

Expand Down
7 changes: 5 additions & 2 deletions crates/dyn-abi/src/eip712/typed_data.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use crate::{
eip712::{PropertyDef, Resolver},
no_std_prelude::*,
parser::TypeSpecifier,
DynAbiError, DynSolType, DynSolValue,
};
use alloc::collections::BTreeMap;
use alloc::{
collections::BTreeMap,
string::{String, ToString},
vec::Vec,
};
use alloy_primitives::{keccak256, B256};
use alloy_sol_types::{Eip712Domain, SolStruct};
use serde::{Deserialize, Serialize};
Expand Down
16 changes: 14 additions & 2 deletions crates/dyn-abi/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use crate::{no_std_prelude::*, DynSolType};
use crate::DynSolType;
use alloc::string::{String, ToString};
use core::fmt;

/// Dynamic ABI result type.
pub type DynAbiResult<T, E = DynAbiError> = core::result::Result<T, E>;

/// Error when parsing EIP-712 `encodeType` strings
///
/// <https://eips.ethereum.org/EIPS/eip-712#definition-of-encodetype>
Expand Down Expand Up @@ -28,7 +32,15 @@ pub enum DynAbiError {
}

#[cfg(feature = "std")]
impl std::error::Error for DynAbiError {}
impl std::error::Error for DynAbiError {
#[inline]
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::HexError(e) => Some(e),
_ => None,
}
}
}

impl fmt::Display for DynAbiError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand Down
11 changes: 1 addition & 10 deletions crates/dyn-abi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,8 @@
#[macro_use]
extern crate alloc;

mod no_std_prelude {
pub(crate) use alloc::{
borrow::{Borrow, Cow, ToOwned},
boxed::Box,
string::{String, ToString},
vec::Vec,
};
}

mod error;
pub use error::DynAbiError;
pub use error::{DynAbiError, DynAbiResult};

pub use alloy_sol_types::{Decoder, Eip712Domain, Encoder, Error, Result, SolType, Word};

Expand Down
Loading

0 comments on commit 9c85a28

Please sign in to comment.