From a43e2aaf985ffd639bf937b9665583ef15dd40ec Mon Sep 17 00:00:00 2001 From: Michael Benfield Date: Mon, 26 Feb 2024 15:39:23 -0800 Subject: [PATCH 1/4] upgrade alloy --- Cargo.toml | 4 +- examples/erc20/Cargo.toml | 4 +- stylus-proc/src/calls/mod.rs | 60 ++++++++++-------- stylus-proc/src/methods/external.rs | 2 +- stylus-proc/src/types.rs | 2 +- stylus-sdk/src/abi/bytes.rs | 56 ++++++++--------- stylus-sdk/src/abi/fixed_bytes.rs | 96 +++++++++++++++-------------- stylus-sdk/src/abi/internal.rs | 14 ++++- stylus-sdk/src/call/error.rs | 2 +- stylus-sdk/src/evm.rs | 2 +- 10 files changed, 132 insertions(+), 110 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b38370b5..a72e9e3a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,8 +12,8 @@ repository = "https://github.com/OffchainLabs/stylus-sdk-rs" rust-version = "1.71.0" [workspace.dependencies] -alloy-primitives = { version = "0.3.1", default-features = false , features = ["native-keccak"] } -alloy-sol-types = { version = "0.3.1", default-features = false } +alloy-primitives = { version = "0.6.3", default-features = false , features = ["native-keccak"] } +alloy-sol-types = { version = "0.6.3", default-features = false } cfg-if = "1.0.0" derivative = { version = "2.2.0", features = ["use_core"] } hex = { version = "0.4.3", default-features = false, features = ["alloc"] } diff --git a/examples/erc20/Cargo.toml b/examples/erc20/Cargo.toml index b55e876d..70c53b80 100644 --- a/examples/erc20/Cargo.toml +++ b/examples/erc20/Cargo.toml @@ -4,8 +4,8 @@ version = "0.1.0" edition = "2021" [dependencies] -alloy-primitives = "0.3.1" -alloy-sol-types = "0.3.1" +alloy-primitives = "0.6.3" +alloy-sol-types = "0.6.3" stylus-sdk = { path = "../../../stylus-sdk-rs/stylus-sdk" } mini-alloc = { path = "../../mini-alloc" } diff --git a/stylus-proc/src/calls/mod.rs b/stylus-proc/src/calls/mod.rs index 844457b9..ecf6ba7f 100644 --- a/stylus-proc/src/calls/mod.rs +++ b/stylus-proc/src/calls/mod.rs @@ -20,7 +20,8 @@ pub fn sol_interface(input: TokenStream) -> TokenStream { let alloy_address = quote!(stylus_sdk::alloy_primitives::Address); let sol_address = quote!(stylus_sdk::alloy_sol_types::sol_data::Address); let sol_type = quote!(stylus_sdk::alloy_sol_types::SolType); - let alloy = quote!(stylus_sdk::alloy_sol_types); + let sol_value = quote!(stylus_sdk::alloy_sol_types::SolValue); + let sol_type_value = quote!(stylus_sdk::alloy_sol_types::private::SolTypeValue); let mut output = quote!(); let mut method_impls = quote!(); @@ -34,7 +35,6 @@ pub fn sol_interface(input: TokenStream) -> TokenStream { } let name = &contract.name; - let sol_name = Ident::new(&format!("{name}SolType"), name.span()); for item in contract.body { let Item::Function(func) = item else { @@ -142,11 +142,11 @@ pub fn sol_interface(input: TokenStream) -> TokenStream { Result<<#return_type as #sol_type>::RustType, stylus_sdk::call::Error> { use alloc::vec; - let args = <(#(#sol_args,)*) as #sol_type>::encode(&(#(#rust_arg_names,)*)); + let args = <(#(#sol_args,)*) as #sol_type>::abi_encode(&(#(#rust_arg_names,)*)); let mut calldata = vec![#selector0, #selector1, #selector2, #selector3]; calldata.extend(args); let returned = #call(context, self.address, &calldata)?; - Ok(<(#return_type,) as #sol_type>::decode(&returned, true)?.0) + Ok(<(#return_type,) as #sol_type>::abi_decode(&returned, true)?.0) } }); } @@ -172,42 +172,52 @@ pub fn sol_interface(input: TokenStream) -> TokenStream { } } - pub struct #sol_name; - - impl #sol_type for #sol_name { - type RustType = #name; - - type TokenType<'a> = <#sol_address as #sol_type>::TokenType<'a>; + impl #sol_value for #name { + type SolType = #name; + } - fn sol_type_name() -> alloc::borrow::Cow<'static, str> { - <#sol_address as #sol_type>::sol_type_name() + impl #sol_type_value for #name { + #[inline] + fn stv_to_tokens(&self) -> ::Token<'_> { + <#sol_address as #sol_type>::tokenize(&self.address) } - fn type_check(token: &Self::TokenType<'_>) -> #alloy::Result<()> { - #sol_address::type_check(token) + #[inline] + fn stv_abi_encoded_size(&self) -> usize { + <#sol_address as #sol_type>::abi_encoded_size(&self.address) } - fn detokenize(token: Self::TokenType<'_>) -> Self::RustType { - #name::new(#sol_address::detokenize(token)) + #[inline] + fn stv_eip712_data_word(&self) -> alloy_sol_types::Word { + <#sol_address as #sol_type>::eip712_data_word(&self.address) } - fn eip712_data_word(rust: &Self::RustType) -> #alloy::Word { - #sol_address::eip712_data_word(&rust.address) + #[inline] + fn stv_abi_encode_packed_to(&self, out: &mut alloy_sol_types::private::Vec) { + <#sol_address as #sol_type>::abi_encode_packed_to(&self.address, out) } + } - fn encode_packed_to(rust: &Self::RustType, out: &mut alloc::vec::Vec) { - #sol_address::encode_packed_to(&rust.address, out) + impl #sol_type for #name { + type RustType = #name; + + type Token<'a> = <#sol_address as #sol_type>::Token<'a>; + + const SOL_NAME: &'static str = <#sol_address as #sol_type>::SOL_NAME; + + const ENCODED_SIZE: Option = <#sol_address as #sol_type>::ENCODED_SIZE; + + fn valid_token(token: &Self::Token<'_>) -> bool { + <#sol_address as #sol_type>::valid_token(token) } - } - impl #alloy::Encodable<#sol_name> for #name { - fn to_tokens(&self) -> <#sol_name as #sol_type>::TokenType<'_> { - <#alloy_address as #alloy::Encodable<#sol_address>>::to_tokens(&self.address) + fn detokenize(token: Self::Token<'_>) -> Self::RustType { + #name::new(#sol_address::detokenize(token)) } } impl stylus_sdk::abi::AbiType for #name { - type SolType = #sol_name; + type SolType = #name; const ABI: stylus_sdk::abi::ConstString = <#alloy_address as stylus_sdk::abi::AbiType>::ABI; } diff --git a/stylus-proc/src/methods/external.rs b/stylus-proc/src/methods/external.rs index 0956b356..610eee8a 100644 --- a/stylus-proc/src/methods/external.rs +++ b/stylus-proc/src/methods/external.rs @@ -148,7 +148,7 @@ pub fn external(_attr: TokenStream, input: TokenStream) -> TokenStream { #[allow(non_upper_case_globals)] #constant => { #deny_value - let args = match <<( #( #arg_types, )* ) as AbiType>::SolType as SolType>::decode(input, true) { + let args = match <<( #( #arg_types, )* ) as AbiType>::SolType as SolType>::abi_decode(input, true) { Ok(args) => args, Err(err) => { internal::failed_to_decode_arguments(err); diff --git a/stylus-proc/src/types.rs b/stylus-proc/src/types.rs index 22006f1f..49279450 100644 --- a/stylus-proc/src/types.rs +++ b/stylus-proc/src/types.rs @@ -73,7 +73,7 @@ pub fn solidity_type_info(ty: &Type) -> (Cow<'static, str>, Cow<'static, str>) { } macro_rules! simple { ($ty:ident) => { - (path!(stringify!($ty)), sol_data::$ty::sol_type_name()) + (path!(stringify!($ty)), sol_data::$ty::SOL_NAME.into()) }; } match ty { diff --git a/stylus-sdk/src/abi/bytes.rs b/stylus-sdk/src/abi/bytes.rs index 923cacd6..0491084d 100644 --- a/stylus-sdk/src/abi/bytes.rs +++ b/stylus-sdk/src/abi/bytes.rs @@ -3,12 +3,10 @@ use crate::{ abi::{AbiType, ConstString}, - crypto, util::evm_padded_length, }; -use alloc::borrow::Cow; use alloc::vec::Vec; -use alloy_sol_types::{token::PackedSeqToken, Encodable, SolType}; +use alloy_sol_types::{abi::token::PackedSeqToken, private::SolTypeValue, SolType, SolValue}; use core::ops::{Deref, DerefMut}; /// Represents a [`bytes`] in Solidity. @@ -60,54 +58,54 @@ impl AsMut<[u8]> for Bytes { /// [`abi`]: crate::abi pub struct BytesSolType; -impl SolType for BytesSolType { - type RustType = Bytes; - - type TokenType<'a> = PackedSeqToken<'a>; - - const ENCODED_SIZE: Option = None; - +impl SolTypeValue for Bytes { #[inline] - fn encoded_size(bytes: &Self::RustType) -> usize { - 32 + evm_padded_length(bytes.len()) + fn stv_to_tokens(&self) -> ::Token<'_> { + self.0.tokenize() } #[inline] - fn sol_type_name() -> Cow<'static, str> { - "bytes".into() + fn stv_abi_encoded_size(&self) -> usize { + 32 + evm_padded_length(self.len()) } #[inline] - fn type_check(_: &Self::TokenType<'_>) -> alloy_sol_types::Result<()> { - Ok(()) // Any PackedSeqToken is valid bytes + fn stv_eip712_data_word(&self) -> alloy_sol_types::Word { + self.0.eip712_data_word() } #[inline] - fn detokenize(token: Self::TokenType<'_>) -> Self::RustType { - Bytes(token.0.into()) + fn stv_abi_encode_packed_to(&self, out: &mut alloy_sol_types::private::Vec) { + self.0.abi_encode_packed_to(out) } +} + +impl SolType for Bytes { + type RustType = Bytes; + + type Token<'a> = PackedSeqToken<'a>; + + const ENCODED_SIZE: Option = None; + + const SOL_NAME: &'static str = "bytes"; #[inline] - fn eip712_data_word(bytes: &Self::RustType) -> alloy_sol_types::Word { - // "The dynamic values bytes and string are encoded as a keccak256 hash of their contents." - // - https://eips.ethereum.org/EIPS/eip-712#definition-of-encodedata - crypto::keccak(bytes) + fn valid_token(_: &Self::Token<'_>) -> bool { + true // Any PackedSeqToken is valid bytes } #[inline] - fn encode_packed_to(bytes: &Self::RustType, out: &mut Vec) { - out.extend_from_slice(bytes); + fn detokenize(token: Self::Token<'_>) -> Self::RustType { + Bytes(token.0.into()) } } -impl Encodable for Bytes { - fn to_tokens(&self) -> PackedSeqToken<'_> { - PackedSeqToken(&self.0) - } +impl SolValue for Bytes { + type SolType = Self; } impl AbiType for Bytes { - type SolType = BytesSolType; + type SolType = Self; const ABI: ConstString = ConstString::new("bytes"); diff --git a/stylus-sdk/src/abi/fixed_bytes.rs b/stylus-sdk/src/abi/fixed_bytes.rs index f5003872..7072b3cf 100644 --- a/stylus-sdk/src/abi/fixed_bytes.rs +++ b/stylus-sdk/src/abi/fixed_bytes.rs @@ -2,30 +2,41 @@ // For licensing, see https://github.com/OffchainLabs/stylus-sdk-rs/blob/stylus/licenses/COPYRIGHT.md use super::{AbiType, ConstString}; -use alloc::borrow::Cow; -use alloc::vec::Vec; use alloy_primitives::FixedBytes; use alloy_sol_types::{ + abi::token::WordToken, + private::SolTypeValue, sol_data::{ByteCount, SupportedFixedBytes}, - token::WordToken, - Encodable, SolType, Word, + SolType, SolValue, Word, }; -/// Provides a corresponding [`SolType`] for [`FixedBytes`]. -/// -/// This type will be removed after an upcoming [`FixedBytes refactor`][refactor] in Alloy. -/// -/// [refactor]: https://github.com/alloy-rs/core/issues/259 +/// SolType for FixedBytes. pub struct FixedBytesSolType; -impl FixedBytesSolType { - const SOL_TYPE_NAME: ConstString = - ConstString::new("bytes").concat(ConstString::from_decimal_number(N)); +impl SolTypeValue> for FixedBytes +where + ByteCount: SupportedFixedBytes, +{ + #[inline] + fn stv_to_tokens(&self) -> as alloy_sol_types::SolType>::Token<'_> { + let mut word = Word::ZERO; + word[..N].copy_from_slice(&self.0); + word.into() + } - fn to_word(bytes: &FixedBytes) -> Word { - let mut out = Word::ZERO; - out[..N].copy_from_slice(&bytes.0); - out + #[inline] + fn stv_abi_encoded_size(&self) -> usize { + self.0.abi_encoded_size() + } + + #[inline] + fn stv_eip712_data_word(&self) -> alloy_sol_types::Word { + SolTypeValue::>::stv_to_tokens(self).0 + } + + #[inline] + fn stv_abi_encode_packed_to(&self, out: &mut alloy_sol_types::private::Vec) { + out.extend_from_slice(&self.0) } } @@ -35,45 +46,30 @@ where { type RustType = FixedBytes; - type TokenType<'a> = WordToken; + type Token<'a> = WordToken; - fn sol_type_name() -> Cow<'static, str> { - Self::SOL_TYPE_NAME.as_str().into() - } + const ENCODED_SIZE: Option = Some(1); + + const SOL_NAME: &'static str = >::NAME; - fn type_check(token: &Self::TokenType<'_>) -> alloy_sol_types::Result<()> { - // Fail if any padding bytes are non-zero - if token.0[N..].iter().any(|b| *b != 0) { - return Err(Self::type_check_fail(token.as_slice())); - } - Ok(()) + fn valid_token(token: &Self::Token<'_>) -> bool { + // Valid if all padding bytes are 0 + token.0[N..].iter().all(|b| *b == 0) } - fn detokenize(token: Self::TokenType<'_>) -> Self::RustType { + fn detokenize(token: Self::Token<'_>) -> Self::RustType { let mut out = FixedBytes([0u8; N]); out.copy_from_slice(&token.0[..N]); out } - - fn eip712_data_word(bytes: &Self::RustType) -> alloy_sol_types::Word { - // Fixed sized values in EIP712 are padded to a word - Self::to_word(bytes) - } - - fn encode_packed_to(bytes: &Self::RustType, out: &mut Vec) { - // Packed encoding doesn't do any padding - out.extend_from_slice(&bytes.0); - } } -impl Encodable> for FixedBytes -where - ByteCount: SupportedFixedBytes, -{ - fn to_tokens(&self) -> WordToken { - FixedBytesSolType::to_word(self).into() - } -} +// impl SolValue for FixedBytes +// where +// ByteCount: SupportedFixedBytes, +// { +// type SolType = Self; +// } impl AbiType for FixedBytes where @@ -83,3 +79,13 @@ where const ABI: ConstString = ConstString::new("bytes").concat(ConstString::from_decimal_number(N)); } + +// XXX +// impl Encodable> for FixedBytes +// where +// ByteCount: SupportedFixedBytes, +// { +// fn to_tokens(&self) -> WordToken { +// FixedBytesSolType::to_word(self).into() +// } +// } diff --git a/stylus-sdk/src/abi/internal.rs b/stylus-sdk/src/abi/internal.rs index 84cb68df..4c8e7ba9 100644 --- a/stylus-sdk/src/abi/internal.rs +++ b/stylus-sdk/src/abi/internal.rs @@ -14,15 +14,23 @@ pub trait EncodableReturnType { fn encode(self) -> ArbResult; } -impl EncodableReturnType for T { +impl EncodableReturnType for T +where + T: AbiType + alloy_sol_types::private::SolTypeValue<::SolType>, +{ #[inline(always)] fn encode(self) -> ArbResult { // coerce types into a tuple of at least 1 element - Ok(<(T,) as AbiType>::SolType::encode(&(self,))) + Ok(<(::SolType,) as SolType>::abi_encode( + &(self,), + )) } } -impl>> EncodableReturnType for Result { +impl>> EncodableReturnType for Result +where + T: AbiType + alloy_sol_types::private::SolTypeValue<::SolType>, +{ #[inline(always)] fn encode(self) -> ArbResult { match self { diff --git a/stylus-sdk/src/call/error.rs b/stylus-sdk/src/call/error.rs index 1956d0e8..56fafc57 100644 --- a/stylus-sdk/src/call/error.rs +++ b/stylus-sdk/src/call/error.rs @@ -26,7 +26,7 @@ impl From for Vec { Error::Revert(data) => data, Error::AbiDecodingFailed(err) => { console!("failed to decode return data from external call: {err}"); - Panic::from(PanicKind::Generic).encode() + Panic::from(PanicKind::Generic).abi_encode() } } } diff --git a/stylus-sdk/src/evm.rs b/stylus-sdk/src/evm.rs index 9ade5816..cdba67b1 100644 --- a/stylus-sdk/src/evm.rs +++ b/stylus-sdk/src/evm.rs @@ -15,7 +15,7 @@ use crate::hostio::{self, wrap_hostio}; use alloc::{vec, vec::Vec}; use alloy_primitives::B256; -use alloy_sol_types::{token::WordToken, SolEvent, TopicList}; +use alloy_sol_types::{abi::token::WordToken, SolEvent, TopicList}; /// Emits an evm log from combined topics and data. fn emit_log(bytes: &[u8], topics: usize) { From acfbf4cbebbde2e9804197bfd1c8454c228dbdb4 Mon Sep 17 00:00:00 2001 From: Rachel Bousfield Date: Tue, 27 Feb 2024 18:02:41 -0700 Subject: [PATCH 2/4] remove FixedBytes type --- Cargo.lock | 105 +++++++++++++++++++++++------- stylus-sdk/src/abi/fixed_bytes.rs | 91 -------------------------- stylus-sdk/src/abi/impls.rs | 20 ++++-- stylus-sdk/src/abi/mod.rs | 4 +- 4 files changed, 94 insertions(+), 126 deletions(-) delete mode 100644 stylus-sdk/src/abi/fixed_bytes.rs diff --git a/Cargo.lock b/Cargo.lock index 6d6fa839..a54d6c60 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -13,9 +13,9 @@ dependencies = [ [[package]] name = "alloy-primitives" -version = "0.3.1" +version = "0.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "66f73f11dcfbf8bb763d88fb1d082fe7cca0a00d3227d9921bdbd52ce5e013e2" +checksum = "ef197eb250c64962003cb08b90b17f0882c192f4a6f2f544809d424fd7cb0e7d" dependencies = [ "bytes", "cfg-if 1.0.0", @@ -29,24 +29,27 @@ dependencies = [ [[package]] name = "alloy-sol-macro" -version = "0.3.1" +version = "0.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a74ceeffdacf9dd0910404d743d07273776fd17b85f9cb17b49a97e5c6055ce9" +checksum = "82e92100dee7fd1e44abbe0ef6607f18758cf0ad4e483f4c65ff5c8d85428a6d" dependencies = [ + "const-hex", "dunce", "heck", + "indexmap", + "proc-macro-error", "proc-macro2", "quote", "syn 2.0.27", - "syn-solidity", + "syn-solidity 0.6.3", "tiny-keccak", ] [[package]] name = "alloy-sol-types" -version = "0.3.1" +version = "0.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d5f347cb6bb307b3802ec455ef43ce00f5e590e0ceca3d2f3b070f5ee367e235" +checksum = "3e7c6a8c492b1d6a4f92a8fc6a13cf39473978dd7d459d7221969ce5a73d97cd" dependencies = [ "alloy-primitives", "alloy-sol-macro", @@ -61,9 +64,9 @@ checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" [[package]] name = "bitflags" -version = "1.3.2" +version = "2.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" +checksum = "ed570934406eb16438a4e976b1b4500774099c13b8cb96eec99f620f05090ddf" [[package]] name = "block-buffer" @@ -80,12 +83,6 @@ version = "3.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f30e7476521f6f8af1a1c4c0b8cc94f0bee37d91763d0ca2665f299b6cd8aec" -[[package]] -name = "byteorder" -version = "1.4.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" - [[package]] name = "bytes" version = "1.4.0" @@ -116,13 +113,14 @@ dependencies = [ [[package]] name = "const-hex" -version = "1.6.1" +version = "1.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "268f52aae268980d03dd9544c1ea591965b2735b038d6998d6e4ab37c8c24445" +checksum = "efbd12d49ab0eaf8193ba9175e45f56bbc2e4b27d57b8cfe62aa47942a46b9a9" dependencies = [ "cfg-if 1.0.0", "cpufeatures", "hex", + "proptest", "serde", ] @@ -206,6 +204,12 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "56ce8c6da7551ec6c462cbaf3bfbc75131ebbfa1c944aeaa9dab51ca1c5f0c3b" +[[package]] +name = "equivalent" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" + [[package]] name = "fnv" version = "1.0.7" @@ -222,6 +226,12 @@ dependencies = [ "version_check", ] +[[package]] +name = "hashbrown" +version = "0.14.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604" + [[package]] name = "heck" version = "0.4.1" @@ -240,6 +250,16 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6fe2267d4ed49bc07b63801559be28c718ea06c4738b7a03c94df7386d2cde46" +[[package]] +name = "indexmap" +version = "2.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "233cf39063f058ea2caae4091bf4a3ef70a653afbc026f5c4a4135d114e3c177" +dependencies = [ + "equivalent", + "hashbrown", +] + [[package]] name = "itoa" version = "1.0.9" @@ -343,6 +363,30 @@ version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" +[[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "syn 1.0.109", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", +] + [[package]] name = "proc-macro2" version = "1.0.66" @@ -354,12 +398,11 @@ dependencies = [ [[package]] name = "proptest" -version = "1.2.0" +version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4e35c06b98bf36aba164cc17cb25f7e232f5c4aeea73baa14b8a9f0d92dbfa65" +checksum = "31b476131c3c86cb68032fdc5cb6d5a1045e3e42d96b69fa599fd77701e1f5bf" dependencies = [ "bitflags", - "byteorder", "num-traits", "rand", "rand_chacha", @@ -441,9 +484,9 @@ checksum = "e5ea92a5b6195c6ef2a0295ea818b312502c6fc94dde986c5553242e18fd4ce2" [[package]] name = "ruint" -version = "1.10.1" +version = "1.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "95294d6e3a6192f3aabf91c38f56505a625aa495533442744185a36d75a790c4" +checksum = "49b1d9521f889713d1221270fdd63370feca7e5c71a18745343402fa86e4f04f" dependencies = [ "proptest", "rand", @@ -455,9 +498,9 @@ dependencies = [ [[package]] name = "ruint-macro" -version = "1.1.0" +version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e666a5496a0b2186dbcd0ff6106e29e093c15591bde62c20d3842007c6978a09" +checksum = "f86854cf50259291520509879a5c294c3c9a4c334e9ff65071c51e42ef1e2343" [[package]] name = "rustc_version" @@ -510,7 +553,7 @@ dependencies = [ "regex", "sha3", "syn 1.0.109", - "syn-solidity", + "syn-solidity 0.3.1", ] [[package]] @@ -564,6 +607,18 @@ dependencies = [ "syn 2.0.27", ] +[[package]] +name = "syn-solidity" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e656cbcef8a77543b5accbd76f60f9e0bc4be364b0aba4263a6f313f8a355511" +dependencies = [ + "paste", + "proc-macro2", + "quote", + "syn 2.0.27", +] + [[package]] name = "tiny-keccak" version = "2.0.2" diff --git a/stylus-sdk/src/abi/fixed_bytes.rs b/stylus-sdk/src/abi/fixed_bytes.rs deleted file mode 100644 index 7072b3cf..00000000 --- a/stylus-sdk/src/abi/fixed_bytes.rs +++ /dev/null @@ -1,91 +0,0 @@ -// Copyright 2023, Offchain Labs, Inc. -// For licensing, see https://github.com/OffchainLabs/stylus-sdk-rs/blob/stylus/licenses/COPYRIGHT.md - -use super::{AbiType, ConstString}; -use alloy_primitives::FixedBytes; -use alloy_sol_types::{ - abi::token::WordToken, - private::SolTypeValue, - sol_data::{ByteCount, SupportedFixedBytes}, - SolType, SolValue, Word, -}; - -/// SolType for FixedBytes. -pub struct FixedBytesSolType; - -impl SolTypeValue> for FixedBytes -where - ByteCount: SupportedFixedBytes, -{ - #[inline] - fn stv_to_tokens(&self) -> as alloy_sol_types::SolType>::Token<'_> { - let mut word = Word::ZERO; - word[..N].copy_from_slice(&self.0); - word.into() - } - - #[inline] - fn stv_abi_encoded_size(&self) -> usize { - self.0.abi_encoded_size() - } - - #[inline] - fn stv_eip712_data_word(&self) -> alloy_sol_types::Word { - SolTypeValue::>::stv_to_tokens(self).0 - } - - #[inline] - fn stv_abi_encode_packed_to(&self, out: &mut alloy_sol_types::private::Vec) { - out.extend_from_slice(&self.0) - } -} - -impl SolType for FixedBytesSolType -where - ByteCount: SupportedFixedBytes, -{ - type RustType = FixedBytes; - - type Token<'a> = WordToken; - - const ENCODED_SIZE: Option = Some(1); - - const SOL_NAME: &'static str = >::NAME; - - fn valid_token(token: &Self::Token<'_>) -> bool { - // Valid if all padding bytes are 0 - token.0[N..].iter().all(|b| *b == 0) - } - - fn detokenize(token: Self::Token<'_>) -> Self::RustType { - let mut out = FixedBytes([0u8; N]); - out.copy_from_slice(&token.0[..N]); - out - } -} - -// impl SolValue for FixedBytes -// where -// ByteCount: SupportedFixedBytes, -// { -// type SolType = Self; -// } - -impl AbiType for FixedBytes -where - ByteCount: SupportedFixedBytes, -{ - type SolType = FixedBytesSolType; - - const ABI: ConstString = ConstString::new("bytes").concat(ConstString::from_decimal_number(N)); -} - -// XXX -// impl Encodable> for FixedBytes -// where -// ByteCount: SupportedFixedBytes, -// { -// fn to_tokens(&self) -> WordToken { -// FixedBytesSolType::to_word(self).into() -// } -// } diff --git a/stylus-sdk/src/abi/impls.rs b/stylus-sdk/src/abi/impls.rs index 368ac412..c98ddb44 100644 --- a/stylus-sdk/src/abi/impls.rs +++ b/stylus-sdk/src/abi/impls.rs @@ -1,13 +1,10 @@ -// Copyright 2023, Offchain Labs, Inc. +// Copyright 2023-2024, Offchain Labs, Inc. // For licensing, see https://github.com/OffchainLabs/stylus-sdk-rs/blob/stylus/licenses/COPYRIGHT.md use super::{AbiType, ConstString}; use alloc::{string::String, vec::Vec}; -use alloy_primitives::{Address, Signed, Uint}; -use alloy_sol_types::sol_data::{self, IntBitCount, SupportedInt}; - -#[cfg(test)] -use alloy_primitives::FixedBytes; +use alloy_primitives::{Address, FixedBytes, Signed, Uint}; +use alloy_sol_types::sol_data::{self, ByteCount, IntBitCount, SupportedFixedBytes, SupportedInt}; /// Generates a test to ensure the two-way relationship between Rust Types and Sol Types is bijective. macro_rules! test_type { @@ -19,7 +16,7 @@ macro_rules! test_type { fn []() { assert_eq!( <$($ty)* as AbiType>::ABI.as_str(), - <<$($ty)* as AbiType>::SolType as alloy_sol_types::SolType>::sol_type_name(), + <<$($ty)* as AbiType>::SolType as alloy_sol_types::SolType>::SOL_NAME, "{}'s ABI didn't match its SolType sol_type_name", stringify!($($ty)*), ); @@ -44,6 +41,15 @@ macro_rules! append_dec { }; } +impl AbiType for FixedBytes +where + ByteCount: SupportedFixedBytes, +{ + type SolType = sol_data::FixedBytes; + + const ABI: ConstString = append_dec!("bytes", N); +} + test_type!(bytes, "bytes calldata", super::Bytes); impl AbiType for Uint diff --git a/stylus-sdk/src/abi/mod.rs b/stylus-sdk/src/abi/mod.rs index 0545ff1a..b5aad86f 100644 --- a/stylus-sdk/src/abi/mod.rs +++ b/stylus-sdk/src/abi/mod.rs @@ -1,4 +1,4 @@ -// Copyright 2023, Offchain Labs, Inc. +// Copyright 2023-2024, Offchain Labs, Inc. // For licensing, see https://github.com/OffchainLabs/stylus-sdk-rs/blob/stylus/licenses/COPYRIGHT.md //! Solidity ABIs for Rust types. @@ -20,7 +20,6 @@ use core::borrow::BorrowMut; pub use bytes::{Bytes, BytesSolType}; pub use const_string::ConstString; -pub use fixed_bytes::FixedBytesSolType; #[cfg(feature = "export-abi")] pub use export::GenerateAbi; @@ -30,7 +29,6 @@ pub mod export; mod bytes; mod const_string; -mod fixed_bytes; mod impls; #[doc(hidden)] From 71955d5ea7b91f538b018e3547747ffbc82f0f80 Mon Sep 17 00:00:00 2001 From: Rory Neithinger Date: Wed, 5 Jun 2024 13:18:53 -0700 Subject: [PATCH 3/4] upgrade syn-solidity version --- Cargo.lock | 74 ++++++++++++++++++++++-------------- Cargo.toml | 6 +-- stylus-proc/src/calls/mod.rs | 4 +- stylus-proc/src/types.rs | 2 +- 4 files changed, 52 insertions(+), 34 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a54d6c60..8490585c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -13,9 +13,9 @@ dependencies = [ [[package]] name = "alloy-primitives" -version = "0.6.3" +version = "0.7.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ef197eb250c64962003cb08b90b17f0882c192f4a6f2f544809d424fd7cb0e7d" +checksum = "5277af0cbcc483ee6ad2c1e818090b5928d27f04fd6580680f31c1cf8068bcc2" dependencies = [ "bytes", "cfg-if 1.0.0", @@ -29,27 +29,56 @@ dependencies = [ [[package]] name = "alloy-sol-macro" -version = "0.6.3" +version = "0.7.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "82e92100dee7fd1e44abbe0ef6607f18758cf0ad4e483f4c65ff5c8d85428a6d" +checksum = "30708a79919b082f2692423c8cc72fc250477e4a2ecb0d4a7244cd3cdb299965" dependencies = [ + "alloy-sol-macro-expander", + "alloy-sol-macro-input", + "proc-macro-error", + "proc-macro2", + "quote", + "syn 2.0.27", +] + +[[package]] +name = "alloy-sol-macro-expander" +version = "0.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c7a679ac01774ab7e00a567a918d4231ae692c5c8cedaf4e16956c3116d7896" +dependencies = [ + "alloy-sol-macro-input", "const-hex", - "dunce", "heck", "indexmap", "proc-macro-error", "proc-macro2", "quote", "syn 2.0.27", - "syn-solidity 0.6.3", + "syn-solidity", "tiny-keccak", ] +[[package]] +name = "alloy-sol-macro-input" +version = "0.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "356da0c2228aa6675a5faaa08a3e4061b967f924753983d72b9a18d9a3fad44e" +dependencies = [ + "const-hex", + "dunce", + "heck", + "proc-macro2", + "quote", + "syn 2.0.27", + "syn-solidity", +] + [[package]] name = "alloy-sol-types" -version = "0.6.3" +version = "0.7.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3e7c6a8c492b1d6a4f92a8fc6a13cf39473978dd7d459d7221969ce5a73d97cd" +checksum = "6eb5e6234c0b62514992589fe1578f64d418dbc8ef5cd1ab2d7f2f568f599698" dependencies = [ "alloy-primitives", "alloy-sol-macro", @@ -234,9 +263,9 @@ checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604" [[package]] name = "heck" -version = "0.4.1" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" [[package]] name = "hex" @@ -484,9 +513,9 @@ checksum = "e5ea92a5b6195c6ef2a0295ea818b312502c6fc94dde986c5553242e18fd4ce2" [[package]] name = "ruint" -version = "1.12.0" +version = "1.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49b1d9521f889713d1221270fdd63370feca7e5c71a18745343402fa86e4f04f" +checksum = "2c3cc4c2511671f327125da14133d0c5c5d137f006a1017a16f557bc85b16286" dependencies = [ "proptest", "rand", @@ -498,9 +527,9 @@ dependencies = [ [[package]] name = "ruint-macro" -version = "1.2.0" +version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f86854cf50259291520509879a5c294c3c9a4c334e9ff65071c51e42ef1e2343" +checksum = "48fd7bd8a6377e15ad9d42a8ec25371b94ddc67abe7c8b9127bec79bebaaae18" [[package]] name = "rustc_version" @@ -553,7 +582,7 @@ dependencies = [ "regex", "sha3", "syn 1.0.109", - "syn-solidity 0.3.1", + "syn-solidity", ] [[package]] @@ -598,20 +627,9 @@ dependencies = [ [[package]] name = "syn-solidity" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e5f995d2140b0f751dbe94365be2591edbf3d1b75dcfaeac14183abbd2ff07bd" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.27", -] - -[[package]] -name = "syn-solidity" -version = "0.6.3" +version = "0.7.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e656cbcef8a77543b5accbd76f60f9e0bc4be364b0aba4263a6f313f8a355511" +checksum = "e6fe08d08d84f2c0a77f1e7c46518789d745c2e87a2721791ed7c3c9bc78df28" dependencies = [ "paste", "proc-macro2", diff --git a/Cargo.toml b/Cargo.toml index a72e9e3a..cfd509b0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,8 +12,8 @@ repository = "https://github.com/OffchainLabs/stylus-sdk-rs" rust-version = "1.71.0" [workspace.dependencies] -alloy-primitives = { version = "0.6.3", default-features = false , features = ["native-keccak"] } -alloy-sol-types = { version = "0.6.3", default-features = false } +alloy-primitives = { version = "0.7.5", default-features = false , features = ["native-keccak"] } +alloy-sol-types = { version = "0.7.5", default-features = false } cfg-if = "1.0.0" derivative = { version = "2.2.0", features = ["use_core"] } hex = { version = "0.4.3", default-features = false, features = ["alloc"] } @@ -30,7 +30,7 @@ paste = "1.0.14" quote = "1.0" regex = "1.9.1" proc-macro2 = "1.0" -syn-solidity = "=0.3.1" +syn-solidity = "0.7.5" convert_case = "0.6.0" # members diff --git a/stylus-proc/src/calls/mod.rs b/stylus-proc/src/calls/mod.rs index ecf6ba7f..e2efbe50 100644 --- a/stylus-proc/src/calls/mod.rs +++ b/stylus-proc/src/calls/mod.rs @@ -8,7 +8,7 @@ use proc_macro2::Ident; use quote::quote; use sha3::{Digest, Keccak256}; use std::borrow::Cow; -use syn_solidity::{FunctionAttribute, Item, Mutability, SolIdent, Visibility}; +use syn_solidity::{FunctionAttribute, Item, Mutability, SolIdent, Spanned, Visibility}; pub fn sol_interface(input: TokenStream) -> TokenStream { let input = match syn_solidity::parse(input) { @@ -107,7 +107,7 @@ pub fn sol_interface(input: TokenStream) -> TokenStream { let mut sol_args = vec![]; let mut rust_args = vec![]; let mut rust_arg_names = vec![]; - for (i, arg) in func.arguments.iter().enumerate() { + for (i, arg) in func.parameters.iter().enumerate() { let (sol_path, abi) = solidity_type_info(&arg.ty); if i > 0 { selector.update(","); diff --git a/stylus-proc/src/types.rs b/stylus-proc/src/types.rs index 49279450..c5715aca 100644 --- a/stylus-proc/src/types.rs +++ b/stylus-proc/src/types.rs @@ -95,7 +95,7 @@ pub fn solidity_type_info(ty: &Type) -> (Cow<'static, str>, Cow<'static, str>) { } Type::Array(ty) => { let (path, abi) = solidity_type_info(&ty.ty); - match ty.size.as_ref().map(|x| x.base10_digits()) { + match ty.size() { Some(size) => (path!("FixedArray<{path}, {size}>"), abi!("{abi}[{size}]")), None => (path!("Array<{path}>"), abi!("{abi}[]")), } From 9b0889a86d88524a34ab8e89e77c663eef422b1d Mon Sep 17 00:00:00 2001 From: Rory Neithinger Date: Wed, 5 Jun 2024 13:54:17 -0700 Subject: [PATCH 4/4] remove cargo stylus --version, bad exit code --- ci/smoke_test.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ci/smoke_test.sh b/ci/smoke_test.sh index fa23fd4b..467bc7cb 100755 --- a/ci/smoke_test.sh +++ b/ci/smoke_test.sh @@ -5,7 +5,7 @@ set -euo pipefail # Print version information rustc -Vv cargo -V -cargo stylus --version +#cargo stylus --version cargo stylus new counter cd counter