From e97ff797910439c66b86f29b87621c242dc48953 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Tue, 27 Jun 2023 15:16:02 +0200 Subject: [PATCH] fix(sol-types): empty data decode --- crates/json-abi/Cargo.toml | 2 +- crates/rlp/Cargo.toml | 2 +- crates/sol-macro/src/expand/error.rs | 6 +----- crates/sol-macro/src/expand/event.rs | 7 +------ crates/sol-macro/src/expand/function.rs | 7 +------ crates/sol-macro/src/expand/mod.rs | 13 ++++++------- crates/sol-macro/src/expand/struct.rs | 6 +----- crates/sol-types/src/coder/decoder.rs | 12 +++--------- crates/sol-types/src/types/data_type.rs | 9 ++++----- crates/sol-types/tests/sol.rs | 11 +++++++++++ 10 files changed, 30 insertions(+), 45 deletions(-) diff --git a/crates/json-abi/Cargo.toml b/crates/json-abi/Cargo.toml index 5c7152d61..384b0c471 100644 --- a/crates/json-abi/Cargo.toml +++ b/crates/json-abi/Cargo.toml @@ -14,8 +14,8 @@ repository.workspace = true exclude.workspace = true [dependencies] -serde = { workspace = true, features = ["derive"] } alloy-primitives.workspace = true +serde = { workspace = true, features = ["derive"] } [dev-dependencies] serde_json.workspace = true diff --git a/crates/rlp/Cargo.toml b/crates/rlp/Cargo.toml index 8f366037e..9b7962e39 100644 --- a/crates/rlp/Cargo.toml +++ b/crates/rlp/Cargo.toml @@ -27,6 +27,6 @@ alloy-rlp-derive = { workspace = true, optional = true } hex-literal.workspace = true [features] -default = ["std", "derive"] +default = ["std"] std = ["arrayvec/std", "bytes/std"] derive = ["dep:alloy-rlp-derive"] diff --git a/crates/sol-macro/src/expand/error.rs b/crates/sol-macro/src/expand/error.rs index 2f96b2770..73709e978 100644 --- a/crates/sol-macro/src/expand/error.rs +++ b/crates/sol-macro/src/expand/error.rs @@ -26,11 +26,7 @@ pub(super) fn expand(cx: &ExpCtxt<'_>, error: &ItemError) -> Result } = error; cx.assert_resolved(params)?; - let tokenize_impl: TokenStream = if params.is_empty() { - quote! { ::core::convert::From::from([]) } - } else { - expand_tokenize_func(params.iter()) - }; + let tokenize_impl = expand_tokenize_func(params.iter()); let signature = cx.signature(name.as_string(), params); let selector = crate::utils::selector(&signature); diff --git a/crates/sol-macro/src/expand/event.rs b/crates/sol-macro/src/expand/event.rs index 61a207724..1f0bddb74 100644 --- a/crates/sol-macro/src/expand/event.rs +++ b/crates/sol-macro/src/expand/event.rs @@ -90,12 +90,7 @@ pub(super) fn expand(cx: &ExpCtxt<'_>, event: &ItemEvent) -> Result .enumerate() .map(|(i, p)| expand_event_topic_field(i, p, p.name.as_ref())); - let tokenize_body_impl = if event.parameters.iter().all(|p| p.is_indexed()) { - // special case for empty tuple - quote! {::core::convert::From::from([])} - } else { - expand_event_tokenize_func(event.parameters.iter()) - }; + let tokenize_body_impl = expand_event_tokenize_func(event.parameters.iter()); let encode_topics_impl = encode_first_topic .into_iter() diff --git a/crates/sol-macro/src/expand/function.rs b/crates/sol-macro/src/expand/function.rs index dc2127492..790765eed 100644 --- a/crates/sol-macro/src/expand/function.rs +++ b/crates/sol-macro/src/expand/function.rs @@ -108,11 +108,7 @@ fn expand_call(cx: &ExpCtxt<'_>, function: &ItemFunction) -> Result let signature = cx.signature(function.name().as_string(), &function.arguments); let selector = crate::utils::selector(&signature); - let tokenize_impl = if function.arguments.is_empty() { - quote! { ::core::convert::From::from([]) } - } else { - expand_tokenize_func(function.arguments.iter()) - }; + let tokenize_impl = expand_tokenize_func(function.arguments.iter()); let tokens = quote! { #struct_def @@ -147,7 +143,6 @@ fn expand_call(cx: &ExpCtxt<'_>, function: &ItemFunction) -> Result fn decode_returns(data: &[u8], validate: bool) -> ::alloy_sol_types::Result { as ::alloy_sol_types::SolType>::decode(data, validate).map(Into::into) } - } }; }; diff --git a/crates/sol-macro/src/expand/mod.rs b/crates/sol-macro/src/expand/mod.rs index fbb9e4eca..2da20eefd 100644 --- a/crates/sol-macro/src/expand/mod.rs +++ b/crates/sol-macro/src/expand/mod.rs @@ -359,21 +359,20 @@ fn expand_from_into_unit(name: &Ident) -> TokenStream { type UnderlyingRustTuple<'a> = (); impl From<()> for #name { - fn from(_: ()) -> Self { + #[inline] + fn from((): ()) -> Self { Self {} } } impl From<#name> for () { - fn from(_: #name) -> Self { - () - } + #[inline] + fn from(#name {}: #name) {} } impl ::alloy_sol_types::Encodable<()> for #name { - fn to_tokens(&self) -> <() as ::alloy_sol_types::SolType>::TokenType<'_> { - ::alloy_sol_types::token::FixedSeqToken::from([]) - } + #[inline] + fn to_tokens(&self) {} } } } diff --git a/crates/sol-macro/src/expand/struct.rs b/crates/sol-macro/src/expand/struct.rs index aaae914d3..91cea92e4 100644 --- a/crates/sol-macro/src/expand/struct.rs +++ b/crates/sol-macro/src/expand/struct.rs @@ -57,11 +57,7 @@ pub(super) fn expand(_cx: &ExpCtxt<'_>, s: &ItemStruct) -> Result { quote!(#encoded_type) }; - let tokenize_impl: TokenStream = if fields.is_empty() { - quote! { ::core::convert::From::from([]) } - } else { - expand_tokenize_func(fields.iter()) - }; + let tokenize_impl = expand_tokenize_func(fields.iter()); let encode_data_impl = match fields.len() { 0 => unreachable!(), diff --git a/crates/sol-types/src/coder/decoder.rs b/crates/sol-types/src/coder/decoder.rs index f51439da9..e03a4821d 100644 --- a/crates/sol-types/src/coder/decoder.rs +++ b/crates/sol-types/src/coder/decoder.rs @@ -197,19 +197,13 @@ impl<'de> Decoder<'de> { /// Decodes a single token from the underlying buffer. #[inline] - pub fn decode>(&mut self, data: &[u8]) -> Result { - if data.is_empty() { - return Err(Error::Overrun) - } + pub fn decode>(&mut self) -> Result { T::decode_from(self) } /// Decodes a sequence of tokens from the underlying buffer. #[inline] - pub fn decode_sequence + TokenSeq<'de>>(&mut self, data: &[u8]) -> Result { - if data.is_empty() { - return Err(Error::Overrun) - } + pub fn decode_sequence + TokenSeq<'de>>(&mut self) -> Result { T::decode_sequence(self) } } @@ -218,7 +212,7 @@ impl<'de> Decoder<'de> { /// types param. pub fn decode<'de, T: TokenSeq<'de>>(data: &'de [u8], validate: bool) -> Result { let mut decoder = Decoder::new(data, validate); - let res = decoder.decode_sequence::(data)?; + let res = decoder.decode_sequence::()?; if validate && encode(&res) != data { return Err(Error::ReserMismatch) } diff --git a/crates/sol-types/src/types/data_type.rs b/crates/sol-types/src/types/data_type.rs index 727b0c790..f83e97b37 100644 --- a/crates/sol-types/src/types/data_type.rs +++ b/crates/sol-types/src/types/data_type.rs @@ -628,14 +628,14 @@ macro_rules! tuple_impls { impl Encodable<()> for () { #[inline] - fn to_tokens(&self) -> <() as SolType>::TokenType<'_> { - FixedSeqToken([]) - } + fn to_tokens(&self) {} } +all_the_tuples!(@double tuple_encodable_impls); + impl SolType for () { type RustType = (); - type TokenType<'a> = FixedSeqToken<(), 0>; + type TokenType<'a> = (); const ENCODED_SIZE: Option = Some(0); @@ -661,7 +661,6 @@ impl SolType for () { fn encode_packed_to(_rust: &Self::RustType, _out: &mut Vec) {} } -all_the_tuples!(@double tuple_encodable_impls); all_the_tuples!(tuple_impls); mod sealed { diff --git a/crates/sol-types/tests/sol.rs b/crates/sol-types/tests/sol.rs index 8eeb31a7f..63dc9c24f 100644 --- a/crates/sol-types/tests/sol.rs +++ b/crates/sol-types/tests/sol.rs @@ -145,3 +145,14 @@ fn error() { let e = SomeError { a: U256::from(1) }; assert_eq!(e.encoded_size(), 32); } + +sol! { + interface WETH { + function deposit() external payable; + } +} + +#[test] +fn empty_call() { + WETH::depositCall::decode(&WETH::depositCall::SELECTOR, true).expect("it should work"); +}