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

fix(sol-types): many ABI coder fixes #434

Merged
merged 4 commits into from
Nov 23, 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
6 changes: 3 additions & 3 deletions crates/sol-macro/src/expand/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ pub(super) fn expand(cx: &ExpCtxt<'_>, function: &ItemFunction) -> Result<TokenS

#[automatically_derived]
impl ::alloy_sol_types::SolCall for #call_name {
type Arguments<'a> = #call_tuple;
type Token<'a> = <Self::Arguments<'a> as ::alloy_sol_types::SolType>::Token<'a>;
type Parameters<'a> = #call_tuple;
type Token<'a> = <Self::Parameters<'a> as ::alloy_sol_types::SolType>::Token<'a>;

type Return = #return_name;

Expand All @@ -125,7 +125,7 @@ pub(super) fn expand(cx: &ExpCtxt<'_>, function: &ItemFunction) -> Result<TokenS
const SIGNATURE: &'static str = #signature;
const SELECTOR: [u8; 4] = #selector;

fn new<'a>(tuple: <Self::Arguments<'a> as ::alloy_sol_types::SolType>::RustType) -> Self {
fn new<'a>(tuple: <Self::Parameters<'a> as ::alloy_sol_types::SolType>::RustType) -> Self {
tuple.into()
}

Expand Down
8 changes: 8 additions & 0 deletions crates/sol-macro/src/expand/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,14 @@ fn expand_from_into_tuples<P>(name: &Ident, fields: &Parameters<P>) -> TokenStre
#[doc(hidden)]
type UnderlyingRustTuple<'a> = #rust_tuple;

#[cfg(test)]
#[allow(dead_code, unreachable_patterns)]
fn _type_assertion(_t: ::alloy_sol_types::private::AssertTypeEq<UnderlyingRustTuple>) {
match _t {
::alloy_sol_types::private::AssertTypeEq::<<UnderlyingSolTuple as ::alloy_sol_types::SolType>::RustType>(_) => {}
}
}

#[automatically_derived]
#[doc(hidden)]
impl ::core::convert::From<#name> for UnderlyingRustTuple<'_> {
Expand Down
2 changes: 1 addition & 1 deletion crates/sol-macro/src/expand/struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ pub(super) fn expand(cx: &ExpCtxt<'_>, s: &ItemStruct) -> Result<TokenStream> {

#eip712_encode_type_fns

fn eip712_encode_data(&self) -> Vec<u8> {
fn eip712_encode_data(&self) -> ::alloy_sol_types::private::Vec<u8> {
#encode_data_impl
}
}
Expand Down
92 changes: 83 additions & 9 deletions crates/sol-types/src/abi/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ pub fn decode_sequence<'de, T: TokenSeq<'de>>(data: &'de [u8], validate: bool) -

#[cfg(test)]
mod tests {
use crate::{sol_data, utils::pad_usize, SolType};
use crate::{sol, sol_data, utils::pad_usize, SolType, SolValue};
use alloc::string::ToString;
use alloy_primitives::{address, hex, Address, B256, U256};

Expand All @@ -326,15 +326,13 @@ mod tests {
"
);

assert_eq!(
MyTy::abi_encode_params(&vec![
vec![Address::repeat_byte(0x11)],
vec![Address::repeat_byte(0x22)],
]),
encoded
);
let ty = vec![vec![Address::repeat_byte(0x11)], vec![Address::repeat_byte(0x22)]];
assert_eq!(MyTy::abi_encode_params(&ty), encoded);

MyTy::abi_decode_params(&encoded, false).unwrap();
let decoded = MyTy::abi_decode_params(&encoded, false).unwrap();
assert_eq!(decoded, ty);
assert_eq!(decoded.abi_encode_params(), encoded);
assert_eq!(decoded.abi_encoded_size(), encoded.len());
}

#[test]
Expand All @@ -354,6 +352,8 @@ mod tests {
let expected = (address1, address2, uint);
let decoded = MyTy::abi_decode_sequence(&encoded, true).unwrap();
assert_eq!(decoded, expected);
assert_eq!(decoded.abi_encode_params(), encoded);
assert_eq!(decoded.abi_encoded_size(), encoded.len());
}

#[test]
Expand All @@ -377,6 +377,8 @@ mod tests {
// this test vector contains a top-level indirect
let decoded = MyTy::abi_decode(&encoded, true).unwrap();
assert_eq!(decoded, expected);
assert_eq!(decoded.abi_encode(), encoded);
assert_eq!(decoded.abi_encoded_size(), encoded.len());
}

#[test]
Expand Down Expand Up @@ -427,6 +429,8 @@ mod tests {

let decoded = MyTy::abi_decode(&encoded, true).unwrap();
assert_eq!(decoded, expected);
assert_eq!(decoded.abi_encode(), encoded);
assert_eq!(decoded.abi_encoded_size(), encoded.len());
}

#[test]
Expand All @@ -452,6 +456,8 @@ mod tests {

let decoded = MyTy::abi_decode(&encoded, true).unwrap();
assert_eq!(decoded, expected);
assert_eq!(decoded.abi_encode(), encoded);
assert_eq!(decoded.abi_encoded_size(), encoded.len());
}

#[test]
Expand Down Expand Up @@ -492,6 +498,8 @@ mod tests {

let decoded = MyTy::abi_decode_params(&encoded, true).unwrap();
assert_eq!(decoded, expected);
assert_eq!(decoded.abi_encode_params(), encoded);
assert_eq!(decoded.abi_encoded_size(), encoded.len() + 32);
}

#[test]
Expand Down Expand Up @@ -686,4 +694,70 @@ mod tests {
"did not match error"
);
}

// https://github.com/alloy-rs/core/issues/433
#[test]
fn fixed_before_dynamic() {
sol! {
#[derive(Debug, PartialEq, Eq)]
struct Ty {
bytes32[3] arr;
bytes dyn;
}
}

let ty = Ty {
arr: [[0x11u8; 32].into(), [0x22u8; 32].into(), [0x33u8; 32].into()],
r#dyn: vec![0x44u8; 4],
};
let encoded = hex!(
"0000000000000000000000000000000000000000000000000000000000000020"
"1111111111111111111111111111111111111111111111111111111111111111"
"2222222222222222222222222222222222222222222222222222222222222222"
"3333333333333333333333333333333333333333333333333333333333333333"
"0000000000000000000000000000000000000000000000000000000000000080"
"0000000000000000000000000000000000000000000000000000000000000004"
"4444444400000000000000000000000000000000000000000000000000000000"
);
assert_eq!(hex::encode(ty.abi_encode()), hex::encode(encoded));
assert_eq!(ty.abi_encoded_size(), encoded.len());

assert_eq!(<Ty as SolType>::abi_decode(&encoded, true).unwrap(), ty);
}

#[test]
fn dynarray_before_dynamic() {
sol! {
#[derive(Debug, PartialEq, Eq)]
struct Ty {
bytes[3] arr;
bytes dyn;
}
}

let ty = Ty {
arr: [vec![0x11u8; 32], vec![0x22u8; 32], vec![0x33u8; 32]],
r#dyn: vec![0x44u8; 4],
};
let encoded = hex!(
"0000000000000000000000000000000000000000000000000000000000000020" // struct offset
"0000000000000000000000000000000000000000000000000000000000000040" // arr offset
"0000000000000000000000000000000000000000000000000000000000000160" // dyn offset
"0000000000000000000000000000000000000000000000000000000000000060" // arr[0] offset
"00000000000000000000000000000000000000000000000000000000000000a0" // arr[1] offset
"00000000000000000000000000000000000000000000000000000000000000e0" // arr[2] offset
"0000000000000000000000000000000000000000000000000000000000000020" // arr[0]
"1111111111111111111111111111111111111111111111111111111111111111"
"0000000000000000000000000000000000000000000000000000000000000020" // arr[1]
"2222222222222222222222222222222222222222222222222222222222222222"
"0000000000000000000000000000000000000000000000000000000000000020" // arr[2]
"3333333333333333333333333333333333333333333333333333333333333333"
"0000000000000000000000000000000000000000000000000000000000000004" // dyn
"4444444400000000000000000000000000000000000000000000000000000000"
);
assert_eq!(hex::encode(ty.abi_encode()), hex::encode(encoded));
assert_eq!(ty.abi_encoded_size(), encoded.len());

assert_eq!(<Ty as SolType>::abi_decode(&encoded, false).unwrap(), ty);
}
}
Loading