difftreelog
refactor Abi impls
in: master
5 files changed
crates/evm-coder/procedural/src/abi_derive.rsdiffbeforeafterboth1use quote::quote;23pub(crate) fn impl_abi_macro(ast: &syn::DeriveInput) -> syn::Result<proc_macro2::TokenStream> {4 let name = &ast.ident;5 let (is_named_fields, field_names, field_types, params_count) = match &ast.data {6 syn::Data::Struct(ds) => match ds.fields {7 syn::Fields::Named(ref fields) => Ok((8 true,9 fields.named.iter().enumerate().map(map_field_to_name),10 fields.named.iter().map(map_field_to_type),11 fields.named.len(),12 )),13 syn::Fields::Unnamed(ref fields) => Ok((14 false,15 fields.unnamed.iter().enumerate().map(map_field_to_name),16 fields.unnamed.iter().map(map_field_to_type),17 fields.unnamed.len(),18 )),19 syn::Fields::Unit => Err(syn::Error::new(name.span(), "Unit structs not supported")),20 },21 syn::Data::Enum(_) => Err(syn::Error::new(name.span(), "Enums not supported")),22 syn::Data::Union(_) => Err(syn::Error::new(name.span(), "Unions not supported")),23 }?;2425 if params_count == 0 {26 return Err(syn::Error::new(name.span(), "Empty structs not supported"));27 };2829 let can_be_plcaed_in_vec = impl_can_be_placed_in_vec(name);30 let abi_type = impl_abi_type(name, field_types.clone());31 let abi_read = impl_abi_read(name, is_named_fields, field_names.clone(), field_types);32 let abi_write = impl_abi_write(name, is_named_fields, params_count, field_names);3334 Ok(quote! {35 #can_be_plcaed_in_vec36 #abi_type37 #abi_read38 #abi_write39 })40}4142fn impl_can_be_placed_in_vec(ident: &syn::Ident) -> proc_macro2::TokenStream {43 quote! {44 impl ::evm_coder::abi::sealed::CanBePlacedInVec for #ident {}45 }46}4748fn map_field_to_name(field: (usize, &syn::Field)) -> syn::Ident {49 match field.1.ident.as_ref() {50 Some(name) => name.clone(),51 None => {52 let mut name = "field".to_string();53 name.push_str(field.0.to_string().as_str());54 syn::Ident::new(name.as_str(), proc_macro2::Span::call_site())55 }56 }57}5859fn map_field_to_type<'a>(field: &'a syn::Field) -> &'a syn::Type {60 &field.ty61}6263fn impl_abi_type<'a>(64 name: &syn::Ident,65 field_types: impl Iterator<Item = &'a syn::Type> + Clone,66) -> proc_macro2::TokenStream {67 let mut params_signature = {68 let types = field_types.clone();69 quote!(70 #(nameof(<#types as ::evm_coder::abi::AbiType>::SIGNATURE) fixed(","))*71 )72 };7374 params_signature.extend(quote!(shift_left(1)));7576 let fields_for_dynamic = field_types.clone();7778 quote! {79 impl ::evm_coder::abi::AbiType for #name {80 const SIGNATURE: ::evm_coder::custom_signature::SignatureUnit = ::evm_coder::make_signature!(81 new fixed("(")82 #params_signature83 fixed(")")84 );85 fn is_dynamic() -> bool {86 false87 #(88 || <#fields_for_dynamic as ::evm_coder::abi::AbiType>::is_dynamic()89 )*90 }91 fn size() -> usize {92 0 #(+ <#field_types as ::evm_coder::abi::AbiType>::size())*93 }94 }95 }96}9798fn impl_abi_read<'a>(99 name: &syn::Ident,100 is_named_fields: bool,101 field_names: impl Iterator<Item = proc_macro2::Ident> + Clone,102 field_types: impl Iterator<Item = &'a syn::Type> + Clone,103) -> proc_macro2::TokenStream {104 let field_names1 = field_names.clone();105106 let struct_constructor = if is_named_fields {107 quote!(Ok(Self { #(#field_names1),* }))108 } else {109 quote!(Ok(Self ( #(#field_names1),* )))110 };111 quote!(112 impl ::evm_coder::abi::AbiRead for #name {113 fn abi_read(reader: &mut ::evm_coder::abi::AbiReader) -> ::evm_coder::execution::Result<Self> {114 let is_dynamic = <Self as ::evm_coder::abi::AbiType>::is_dynamic();115 let size = if !is_dynamic {116 Some(<Self as ::evm_coder::abi::AbiType>::size())117 } else {118 None119 };120 let mut subresult = reader.subresult(size)?;121 #(122 let #field_names = {123 let value = <#field_types as ::evm_coder::abi::AbiRead>::abi_read(&mut subresult)?;124 if !is_dynamic {subresult.seek(<#field_types as ::evm_coder::abi::AbiType>::size())};125 value126 };127 )*128129 #struct_constructor130 }131 }132 )133}134135fn impl_abi_write<'a>(136 name: &syn::Ident,137 is_named_fields: bool,138 params_count: usize,139 field_names: impl Iterator<Item = proc_macro2::Ident> + Clone,140) -> proc_macro2::TokenStream {141 let abi_write = if is_named_fields {142 quote!(143 #(144 ::evm_coder::abi::AbiWrite::abi_write(&self.#field_names, sub);145 )*146 )147 } else {148 let field_names = (0..params_count)149 .into_iter()150 .map(proc_macro2::Literal::usize_unsuffixed);151 quote!(152 #(153 ::evm_coder::abi::AbiWrite::abi_write(&self.#field_names, sub);154 )*155 )156 };157 quote!(158 impl ::evm_coder::abi::AbiWrite for #name {159 fn abi_write(&self, writer: &mut ::evm_coder::abi::AbiWriter) {160 if <Self as ::evm_coder::abi::AbiType>::is_dynamic() {161 let mut sub = ::evm_coder::abi::AbiWriter::new();162 {163 let sub = &mut sub;164 #abi_write165 }166 writer.write_subresult(sub);167 } else {168 let sub = writer;169 #abi_write170 }171 }172 }173 )174}crates/evm-coder/procedural/src/solidity_interface.rsdiffbeforeafterboth--- a/crates/evm-coder/procedural/src/solidity_interface.rs
+++ b/crates/evm-coder/procedural/src/solidity_interface.rs
@@ -406,7 +406,7 @@
quote! {
#name: {
let value = <#ty as ::evm_coder::abi::AbiRead>::abi_read(reader)?;
- if !is_dynamic {reader.seek(<#ty as ::evm_coder::abi::AbiType>::size())};
+ if !is_dynamic {reader.bytes_read(<#ty as ::evm_coder::abi::AbiType>::size())};
value
}
}
crates/evm-coder/src/abi/impls.rsdiffbeforeafterboth--- a/crates/evm-coder/src/abi/impls.rs
+++ b/crates/evm-coder/src/abi/impls.rs
@@ -10,12 +10,12 @@
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
-macro_rules! impl_abi_readable {
- ($ty:ty, $method:ident, $dynamic:literal) => {
+macro_rules! impl_abi_type {
+ ($ty:ty, $name:ident, $dynamic:literal) => {
impl sealed::CanBePlacedInVec for $ty {}
impl AbiType for $ty {
- const SIGNATURE: SignatureUnit = make_signature!(new fixed(stringify!($ty)));
+ const SIGNATURE: SignatureUnit = make_signature!(new fixed(stringify!($name)));
fn is_dynamic() -> bool {
$dynamic
@@ -25,7 +25,11 @@
ABI_ALIGNMENT
}
}
+ };
+}
+macro_rules! impl_abi_readable {
+ ($ty:ty, $method:ident) => {
impl AbiRead for $ty {
fn abi_read(reader: &mut AbiReader) -> Result<$ty> {
reader.$method()
@@ -34,82 +38,75 @@
};
}
-impl_abi_readable!(uint32, uint32, false);
-impl_abi_readable!(uint64, uint64, false);
-impl_abi_readable!(uint128, uint128, false);
-impl_abi_readable!(uint256, uint256, false);
-impl_abi_readable!(bytes4, bytes4, false);
-impl_abi_readable!(address, address, false);
-impl_abi_readable!(string, string, true);
+macro_rules! impl_abi_writeable {
+ ($ty:ty, $method:ident) => {
+ impl AbiWrite for $ty {
+ fn abi_write(&self, writer: &mut AbiWriter) {
+ writer.$method(&self)
+ }
+ }
+ };
+}
-impl sealed::CanBePlacedInVec for bool {}
+macro_rules! impl_abi {
+ ($ty:ty, $method:ident, $dynamic:literal) => {
+ impl_abi_type!($ty, $method, $dynamic);
+ impl_abi_readable!($ty, $method);
+ impl_abi_writeable!($ty, $method);
+ };
+}
-impl AbiType for bool {
- const SIGNATURE: SignatureUnit = make_signature!(new fixed("bool"));
+impl_abi!(bool, bool, false);
+impl_abi!(u8, uint8, false);
+impl_abi!(u32, uint32, false);
+impl_abi!(u64, uint64, false);
+impl_abi!(u128, uint128, false);
+impl_abi!(U256, uint256, false);
+impl_abi!(H160, address, false);
+impl_abi!(string, string, true);
- fn is_dynamic() -> bool {
- false
- }
- fn size() -> usize {
- ABI_ALIGNMENT
- }
-}
-impl AbiRead for bool {
- fn abi_read(reader: &mut AbiReader) -> Result<bool> {
- reader.bool()
- }
-}
+impl_abi_writeable!(&str, string);
-impl AbiType for uint8 {
- const SIGNATURE: SignatureUnit = make_signature!(new fixed("uint8"));
+impl_abi_type!(bytes, bytes, true);
- fn is_dynamic() -> bool {
- false
- }
- fn size() -> usize {
- ABI_ALIGNMENT
- }
-}
-impl AbiRead for uint8 {
- fn abi_read(reader: &mut AbiReader) -> Result<uint8> {
- reader.uint8()
+impl AbiRead for bytes {
+ fn abi_read(reader: &mut AbiReader) -> Result<bytes> {
+ Ok(bytes(reader.bytes()?))
}
}
-impl AbiType for bytes {
- const SIGNATURE: SignatureUnit = make_signature!(new fixed("bytes"));
-
- fn is_dynamic() -> bool {
- true
- }
- fn size() -> usize {
- ABI_ALIGNMENT
+impl AbiWrite for bytes {
+ fn abi_write(&self, writer: &mut AbiWriter) {
+ writer.bytes(self.0.as_slice())
}
}
-impl AbiRead for bytes {
- fn abi_read(reader: &mut AbiReader) -> Result<bytes> {
- Ok(bytes(reader.bytes()?))
+
+impl_abi_type!(bytes4, bytes4, false);
+impl AbiRead for bytes4 {
+ fn abi_read(reader: &mut AbiReader) -> Result<bytes4> {
+ reader.bytes4()
}
}
-impl<R: AbiType + AbiRead + sealed::CanBePlacedInVec> AbiRead for Vec<R> {
- fn abi_read(reader: &mut AbiReader) -> Result<Vec<R>> {
+impl<T: AbiType + AbiRead + sealed::CanBePlacedInVec> AbiRead for Vec<T> {
+ fn abi_read(reader: &mut AbiReader) -> Result<Vec<T>> {
let mut sub = reader.subresult(None)?;
let size = sub.uint32()? as usize;
sub.subresult_offset = sub.offset;
+ let is_dynamic = <T as AbiType>::is_dynamic();
let mut out = Vec::with_capacity(size);
for _ in 0..size {
- out.push(<R>::abi_read(&mut sub)?);
- if !<R>::is_dynamic() {
- sub.subresult_offset += <R>::size()
+ out.push(<T as AbiRead>::abi_read(&mut sub)?);
+ if !is_dynamic {
+ sub.bytes_read(<T as AbiType>::size());
};
}
Ok(out)
}
}
-impl<R: AbiType> AbiType for Vec<R> {
- const SIGNATURE: SignatureUnit = make_signature!(new nameof(R::SIGNATURE) fixed("[]"));
+impl<T: AbiType> AbiType for Vec<T> {
+ const SIGNATURE: SignatureUnit = make_signature!(new nameof(T::SIGNATURE) fixed("[]"));
fn is_dynamic() -> bool {
true
@@ -152,39 +149,9 @@
impl AbiWrite for Property {
fn abi_write(&self, writer: &mut AbiWriter) {
(&self.key, &self.value).abi_write(writer);
- }
-}
-
-macro_rules! impl_abi_writeable {
- ($ty:ty, $method:ident) => {
- impl AbiWrite for $ty {
- fn abi_write(&self, writer: &mut AbiWriter) {
- writer.$method(&self)
- }
- }
- };
-}
-
-impl_abi_writeable!(u8, uint8);
-impl_abi_writeable!(u32, uint32);
-impl_abi_writeable!(u128, uint128);
-impl_abi_writeable!(U256, uint256);
-impl_abi_writeable!(H160, address);
-impl_abi_writeable!(bool, bool);
-impl_abi_writeable!(&str, string);
-
-impl AbiWrite for string {
- fn abi_write(&self, writer: &mut AbiWriter) {
- writer.string(self)
}
}
-impl AbiWrite for bytes {
- fn abi_write(&self, writer: &mut AbiWriter) {
- writer.bytes(self.0.as_slice())
- }
-}
-
impl<T: AbiWrite + AbiType> AbiWrite for Vec<T> {
fn abi_write(&self, writer: &mut AbiWriter) {
let is_dynamic = T::is_dynamic();
@@ -272,7 +239,7 @@
Ok((
$({
let value = <$ident>::abi_read(&mut subresult)?;
- if !is_dynamic {subresult.seek(<$ident as AbiType>::size())};
+ if !is_dynamic {subresult.bytes_read(<$ident as AbiType>::size())};
value
},)+
))
crates/evm-coder/src/abi/mod.rsdiffbeforeafterboth--- a/crates/evm-coder/src/abi/mod.rs
+++ b/crates/evm-coder/src/abi/mod.rs
@@ -208,7 +208,7 @@
}
/// Notify about readed data portion.
- pub fn seek(&mut self, size: usize) {
+ pub fn bytes_read(&mut self, size: usize) {
self.subresult_offset += size;
}
@@ -281,6 +281,11 @@
self.write_padleft(&u32::to_be_bytes(*value))
}
+ /// Write [`u64`] to end of buffer
+ pub fn uint64(&mut self, value: &u64) {
+ self.write_padleft(&u64::to_be_bytes(*value))
+ }
+
/// Write [`u128`] to end of buffer
pub fn uint128(&mut self, value: &u128) {
self.write_padleft(&u128::to_be_bytes(*value))
crates/evm-coder/src/abi/test.rsdiffbeforeafterboth--- a/crates/evm-coder/src/abi/test.rs
+++ b/crates/evm-coder/src/abi/test.rs
@@ -48,11 +48,44 @@
}
#[test]
+fn encode_decode_uint64() {
+ test_impl_uint!(uint64);
+}
+
+#[test]
fn encode_decode_uint128() {
test_impl_uint!(uint128);
}
#[test]
+fn encode_decode_bool_true() {
+ test_impl::<bool>(
+ 0xdeadbeef,
+ true,
+ &hex!(
+ "
+ deadbeef
+ 0000000000000000000000000000000000000000000000000000000000000001
+ "
+ ),
+ );
+}
+
+#[test]
+fn encode_decode_bool_false() {
+ test_impl::<bool>(
+ 0xdeadbeef,
+ false,
+ &hex!(
+ "
+ deadbeef
+ 0000000000000000000000000000000000000000000000000000000000000000
+ "
+ ),
+ );
+}
+
+#[test]
fn encode_decode_uint256() {
test_impl::<uint256>(
0xdeadbeef,