Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve SolType tuples #115

Merged
merged 2 commits into from
Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions crates/sol-types/src/types/data_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,8 @@ where
}

macro_rules! tuple_impls {
(@one $ty:ident) => { 1usize };

// compile time `join(",")` format string
(@fmt $other:ident) => { ",{}" };
(@fmt $first:ident, $($other:ident,)*) => {
Expand Down Expand Up @@ -501,20 +503,20 @@ macro_rules! tuple_impls {
fn sol_type_name() -> Cow<'static, str> {
format!(
concat!(
"tuple(",
"(",
tuple_impls! { @fmt $($ty,)+ },
")",
),
$(<$ty as SolType>::sol_type_name(),)+
).into()
}

fn encoded_size<B_: Borrow<Self::RustType>>(rust: B_) -> usize {
fn encoded_size<B: Borrow<Self::RustType>>(rust: B) -> usize {
if let Some(size) = Self::ENCODED_SIZE {
return size
}

let ($(ref $ty,)+) = *rust.borrow();
let ($($ty,)+) = rust.borrow();
0 $(
+ <$ty as SolType>::encoded_size($ty)
)+
Expand All @@ -538,22 +540,27 @@ macro_rules! tuple_impls {
)+)
}

fn tokenize<B_: Borrow<Self::RustType>>(rust: B_) -> Self::TokenType {
fn tokenize<B: Borrow<Self::RustType>>(rust: B) -> Self::TokenType {
let ($($ty,)+) = rust.borrow();
($(
<$ty as SolType>::tokenize($ty),
)+)
}

fn eip712_data_word<B_: Borrow<Self::RustType>>(rust: B_) -> Word {
fn eip712_data_word<B: Borrow<Self::RustType>>(rust: B) -> Word {
const COUNT: usize = 0usize $(+ tuple_impls!(@one $ty))+;
let ($($ty,)+) = rust.borrow();
let encoding: Vec<u8> = [$(
let encoding: [[u8; 32]; COUNT] = [$(
<$ty as SolType>::eip712_data_word($ty).0,
)+].concat();
keccak256(&encoding).into()
)+];
// SAFETY: Flattening [[u8; 32]; COUNT] to [u8; COUNT * 32] is valid
let ptr = encoding.as_ptr() as *const u8;
let len = COUNT * 32;
let encoding: &[u8] = unsafe { core::slice::from_raw_parts(ptr, len) };
keccak256(encoding).into()
}

fn encode_packed_to<B_: Borrow<Self::RustType>>(rust: B_, out: &mut Vec<u8>) {
fn encode_packed_to<B: Borrow<Self::RustType>>(rust: B, out: &mut Vec<u8>) {
let ($($ty,)+) = rust.borrow();
// TODO: Reserve
$(
Expand All @@ -572,12 +579,12 @@ impl SolType for () {

#[inline]
fn sol_type_name() -> Cow<'static, str> {
"tuple()".into()
"()".into()
}

#[inline]
fn type_check(_token: &Self::TokenType) -> Result<()> {
Err(crate::Error::type_check_fail(b"", "tuple()"))
Ok(())
}

#[inline]
Expand Down
4 changes: 2 additions & 2 deletions crates/sol-types/src/types/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ use crate::{no_std_prelude::*, token::TokenSeq, Result, TokenType, Word};
/// assert_eq!(&DynUint256Array::sol_type_name(), "uint256[]");
///
/// type Erc20FunctionArgs = (Address, Uint<256>);
/// assert_eq!(&Erc20FunctionArgs::sol_type_name(), "tuple(address,uint256)");
/// assert_eq!(&Erc20FunctionArgs::sol_type_name(), "(address,uint256)");
///
/// type LargeComplexType = (FixedArray<Array<Bool>, 2>, (FixedBytes<13>, String));
/// assert_eq!(&LargeComplexType::sol_type_name(), "tuple(bool[][2],tuple(bytes13,string))");
/// assert_eq!(&LargeComplexType::sol_type_name(), "(bool[][2],(bytes13,string))");
/// ```
///
/// These types are zero cost representations of Solidity types. They do not
Expand Down