difftreelog
fix AbiRead implementations
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 // dbg!(ast);5 let name = &ast.ident;6 let (is_named_fields, field_names, field_types, params_count) = match &ast.data {7 syn::Data::Struct(ds) => match ds.fields {8 syn::Fields::Named(ref fields) => Ok((9 true,10 fields.named.iter().enumerate().map(map_field_to_name),11 fields.named.iter().map(map_field_to_type),12 fields.named.len(),13 )),14 syn::Fields::Unnamed(ref fields) => Ok((15 false,16 fields.unnamed.iter().enumerate().map(map_field_to_name),17 fields.unnamed.iter().map(map_field_to_type),18 fields.unnamed.len(),19 )),20 syn::Fields::Unit => Err(syn::Error::new(name.span(), "Unit structs not supported")),21 },22 syn::Data::Enum(_) => Err(syn::Error::new(name.span(), "Enums not supported")),23 syn::Data::Union(_) => Err(syn::Error::new(name.span(), "Unions not supported")),24 }?;2526 if params_count == 0 {27 return Err(syn::Error::new(name.span(), "Empty structs not supported"));28 };2930 let can_be_plcaed_in_vec = impl_can_be_placed_in_vec(name);31 let abi_type = impl_abi_type(name, field_types.clone());32 let abi_read = impl_abi_read(name, is_named_fields, field_names.clone(), field_types);33 let abi_write = impl_abi_write(name, is_named_fields, params_count, field_names);34 println!("{}", abi_write);35 Ok(quote! {36 #can_be_plcaed_in_vec37 #abi_type38 #abi_read39 #abi_write40 })41}4243fn impl_can_be_placed_in_vec(ident: &syn::Ident) -> proc_macro2::TokenStream {44 quote! {45 impl ::evm_coder::abi::sealed::CanBePlacedInVec for #ident {}46 }47}4849fn map_field_to_name(field: (usize, &syn::Field)) -> syn::Ident {50 match field.1.ident.as_ref() {51 Some(name) => name.clone(),52 None => {53 let mut name = "field".to_string();54 name.push_str(field.0.to_string().as_str());55 syn::Ident::new(name.as_str(), proc_macro2::Span::call_site())56 }57 }58}5960fn map_field_to_type<'a>(field: &'a syn::Field) -> &'a syn::Type {61 &field.ty62}6364fn impl_abi_type<'a>(65 name: &syn::Ident,66 field_types: impl Iterator<Item = &'a syn::Type> + Clone,67) -> proc_macro2::TokenStream {68 let mut params_signature = {69 let types = field_types.clone();70 quote!(71 #(nameof(<#types as ::evm_coder::abi::AbiType>::SIGNATURE) fixed(","))*72 )73 };7475 params_signature.extend(quote!(shift_left(1)));7677 let fields_for_dynamic = field_types.clone();7879 quote! {80 impl ::evm_coder::abi::AbiType for #name {81 const SIGNATURE: ::evm_coder::custom_signature::SignatureUnit = ::evm_coder::make_signature!(82 new fixed("(")83 #params_signature84 fixed(")")85 );86 fn is_dynamic() -> bool {87 false88 #(89 || <#fields_for_dynamic as ::evm_coder::abi::AbiType>::is_dynamic()90 )*91 }92 fn size() -> usize {93 0 #(+ <#field_types as ::evm_coder::abi::AbiType>::size())*94 }95 }96 }97}9899fn impl_abi_read<'a>(100 name: &syn::Ident,101 is_named_fields: bool,102 field_names: impl Iterator<Item = proc_macro2::Ident> + Clone,103 field_types: impl Iterator<Item = &'a syn::Type> + Clone,104) -> proc_macro2::TokenStream {105 let field_names1 = field_names.clone();106107 let struct_constructor = if is_named_fields {108 quote!(Ok(Self { #(#field_names1),* }))109 } else {110 quote!(Ok(Self ( #(#field_names1),* )))111 };112 quote!(113 impl ::evm_coder::abi::AbiRead for #name {114 fn abi_read(reader: &mut ::evm_coder::abi::AbiReader) -> ::evm_coder::execution::Result<Self> {115 let size = if !<Self as ::evm_coder::abi::AbiType>::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 = <#field_types as ::evm_coder::abi::AbiRead>::abi_read(&mut subresult)?;123 )*124125 #struct_constructor126 }127 }128 )129}130131fn impl_abi_write<'a>(132 name: &syn::Ident,133 is_named_fields: bool,134 params_count: usize,135 field_names: impl Iterator<Item = proc_macro2::Ident> + Clone,136) -> proc_macro2::TokenStream {137 let abi_write = if is_named_fields {138 quote!(139 #(140 self.#field_names.abi_write(sub);141 )*142 )143 } else {144 let field_names = (0..params_count)145 .into_iter()146 .map(proc_macro2::Literal::usize_unsuffixed);147 quote!(148 #(149 self.#field_names.abi_write(sub);150 )*151 )152 };153 quote!(154 impl ::evm_coder::abi::AbiWrite for #name {155 fn abi_write(&self, writer: &mut ::evm_coder::abi::AbiWriter) {156 if <Self as ::evm_coder::abi::AbiType>::is_dynamic() {157 let mut sub = ::evm_coder::abi::AbiWriter::new();158 {159 let sub = &mut sub;160 #abi_write161 }162 writer.write_subresult(sub);163 } else {164 let sub = writer;165 #abi_write166 }167 }168 }169 )170}1use 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 self.#field_names.abi_write(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 self.#field_names.abi_write(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/src/abi/impls.rsdiffbeforeafterboth--- a/crates/evm-coder/src/abi/impls.rs
+++ b/crates/evm-coder/src/abi/impls.rs
@@ -92,7 +92,7 @@
}
}
-impl<R: AbiRead + sealed::CanBePlacedInVec> AbiRead for Vec<R> {
+impl<R: AbiType + AbiRead + sealed::CanBePlacedInVec> AbiRead for Vec<R> {
fn abi_read(reader: &mut AbiReader) -> Result<Vec<R>> {
let mut sub = reader.subresult(None)?;
let size = sub.uint32()? as usize;
@@ -100,6 +100,9 @@
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()
+ };
}
Ok(out)
}
@@ -295,14 +298,19 @@
impl<$($ident),+> AbiRead for ($($ident,)+)
where
- $($ident: AbiRead,)+
+ $($ident: AbiRead + AbiType,)+
($($ident,)+): AbiType,
{
fn abi_read(reader: &mut AbiReader) -> Result<($($ident,)+)> {
- let size = if !<($($ident,)+)>::is_dynamic() { Some(<($($ident,)+)>::size()) } else { None };
+ let is_dynamic = <($($ident,)+)>::is_dynamic();
+ let size = if !is_dynamic { Some(<($($ident,)+)>::size()) } else { None };
let mut subresult = reader.subresult(size)?;
Ok((
- $(<$ident>::abi_read(&mut subresult)?,)+
+ $({
+ let value = <$ident>::abi_read(&mut subresult)?;
+ if !is_dynamic {subresult.seek(<$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
@@ -75,19 +75,19 @@
buf: &[u8],
offset: usize,
pad_start: usize,
- pad_size: usize,
+ pad_end: usize,
block_start: usize,
- block_size: usize,
+ block_end: usize,
) -> Result<[u8; S]> {
if buf.len() - offset < ABI_ALIGNMENT {
return Err(Error::Error(ExitError::OutOfOffset));
}
let mut block = [0; S];
- let is_pad_zeroed = buf[pad_start..pad_size].iter().all(|&v| v == 0);
+ let is_pad_zeroed = buf[pad_start..pad_end].iter().all(|&v| v == 0);
if !is_pad_zeroed {
return Err(Error::Error(ExitError::InvalidRange));
}
- block.copy_from_slice(&buf[block_start..block_size]);
+ block.copy_from_slice(&buf[block_start..block_end]);
Ok(block)
}
@@ -190,7 +190,6 @@
let subresult_offset = self.subresult_offset;
let offset = if let Some(size) = size {
self.offset += size;
- self.subresult_offset += size;
0
} else {
self.uint32()? as usize
@@ -208,6 +207,11 @@
})
}
+ /// Notify about readed data portion.
+ pub fn seek(&mut self, size: usize) {
+ self.subresult_offset += size;
+ }
+
/// Is this parser reached end of buffer?
pub fn is_finished(&self) -> bool {
self.buf.len() == self.offset
crates/evm-coder/src/abi/test.rsdiffbeforeafterboth--- a/crates/evm-coder/src/abi/test.rs
+++ b/crates/evm-coder/src/abi/test.rs
@@ -137,6 +137,27 @@
}
#[test]
+fn encode_decode_vec_tuple_uint8_uint8() {
+ test_impl::<Vec<(u8, u8)>>(
+ 0xdeadbeef,
+ vec![(0x0A, 0x0B), (0x0C, 0x0D), (0x0E, 0x0F)],
+ &hex!(
+ "
+ deadbeef
+ 0000000000000000000000000000000000000000000000000000000000000020
+ 0000000000000000000000000000000000000000000000000000000000000003
+ 000000000000000000000000000000000000000000000000000000000000000a
+ 000000000000000000000000000000000000000000000000000000000000000b
+ 000000000000000000000000000000000000000000000000000000000000000c
+ 000000000000000000000000000000000000000000000000000000000000000d
+ 000000000000000000000000000000000000000000000000000000000000000e
+ 000000000000000000000000000000000000000000000000000000000000000f
+ "
+ ),
+ );
+}
+
+#[test]
fn encode_decode_vec_tuple_uint256_string() {
test_impl::<Vec<(uint256, string)>>(
0xdeadbeef,
@@ -332,3 +353,175 @@
),
);
}
+
+#[test]
+// #[ignore = "reason"]
+fn encode_decode_tuple0_tuple1_uint8_tuple1_string_bytes_tuple1_uint8_bytes() {
+ let int = 0xff;
+ let by = bytes(vec![0x11, 0x22, 0x33]);
+ let string = "some string".to_string();
+
+ test_impl::<((u8,), (String, bytes), (u8, bytes))>(
+ 0xdeadbeef,
+ ((int,), (string.clone(), by.clone()), (int, by)),
+ &hex!(
+ "
+ deadbeef
+ 0000000000000000000000000000000000000000000000000000000000000020
+ 00000000000000000000000000000000000000000000000000000000000000ff
+ 0000000000000000000000000000000000000000000000000000000000000060
+ 0000000000000000000000000000000000000000000000000000000000000120
+ 0000000000000000000000000000000000000000000000000000000000000040
+ 0000000000000000000000000000000000000000000000000000000000000080
+ 000000000000000000000000000000000000000000000000000000000000000b
+ 736f6d6520737472696e67000000000000000000000000000000000000000000
+ 0000000000000000000000000000000000000000000000000000000000000003
+ 1122330000000000000000000000000000000000000000000000000000000000
+ 00000000000000000000000000000000000000000000000000000000000000ff
+ 0000000000000000000000000000000000000000000000000000000000000040
+ 0000000000000000000000000000000000000000000000000000000000000003
+ 1122330000000000000000000000000000000000000000000000000000000000
+ "
+ ),
+ );
+}
+
+#[test]
+fn encode_decode_tuple0_tuple1_uint8_tuple1_uint8_uint8_tuple1_uint8_uint8() {
+ test_impl::<((u8,), (u8, u8), (u8, u8))>(
+ 0xdeadbeef,
+ ((43,), (44, 45), (46, 47)),
+ &hex!(
+ "
+ deadbeef
+ 000000000000000000000000000000000000000000000000000000000000002b
+ 000000000000000000000000000000000000000000000000000000000000002c
+ 000000000000000000000000000000000000000000000000000000000000002d
+ 000000000000000000000000000000000000000000000000000000000000002e
+ 000000000000000000000000000000000000000000000000000000000000002f
+ "
+ ),
+ );
+}
+
+#[test]
+fn encode_decode_tuple0_tuple1_uint8_tuple1_uint8() {
+ test_impl::<((u8,), (u8,))>(
+ 0xdeadbeef,
+ ((43,), (44,)),
+ &hex!(
+ "
+ deadbeef
+ 000000000000000000000000000000000000000000000000000000000000002b
+ 000000000000000000000000000000000000000000000000000000000000002c
+ "
+ ),
+ );
+}
+
+#[test]
+fn encode_decode_tuple0_tuple1_uint8_uint8() {
+ test_impl::<((u8, u8),)>(
+ 0xdeadbeef,
+ ((43, 44),),
+ &hex!(
+ "
+ deadbeef
+ 000000000000000000000000000000000000000000000000000000000000002b
+ 000000000000000000000000000000000000000000000000000000000000002c
+ "
+ ),
+ );
+}
+
+#[test]
+fn encode_decode_tuple_uint8_uint8() {
+ test_impl::<(u8, u8)>(
+ 0xdeadbeef,
+ (43, 44),
+ &hex!(
+ "
+ deadbeef
+ 000000000000000000000000000000000000000000000000000000000000002b
+ 000000000000000000000000000000000000000000000000000000000000002c
+ "
+ ),
+ );
+}
+
+#[test]
+fn encode_decode_tuple0_tuple1_string() {
+ test_impl::<((String,),)>(
+ 0xdeadbeef,
+ (("some string".to_string(),),),
+ &hex!(
+ "
+ deadbeef
+ 0000000000000000000000000000000000000000000000000000000000000020
+ 0000000000000000000000000000000000000000000000000000000000000020
+ 0000000000000000000000000000000000000000000000000000000000000020
+ 000000000000000000000000000000000000000000000000000000000000000b
+ 736f6d6520737472696e67000000000000000000000000000000000000000000
+ "
+ ),
+ );
+}
+
+#[test]
+fn encode_decode_tuple0_tuple1_uint8_string() {
+ test_impl::<((u8, String),)>(
+ 0xdeadbeef,
+ ((0xff, "some string".to_string()),),
+ &hex!(
+ "
+ deadbeef
+ 0000000000000000000000000000000000000000000000000000000000000020
+ 0000000000000000000000000000000000000000000000000000000000000020
+ 00000000000000000000000000000000000000000000000000000000000000ff
+ 0000000000000000000000000000000000000000000000000000000000000040
+ 000000000000000000000000000000000000000000000000000000000000000b
+ 736f6d6520737472696e67000000000000000000000000000000000000000000
+ "
+ ),
+ );
+}
+
+#[test]
+fn encode_decode_tuple0_tuple1_string_bytes() {
+ test_impl::<((String, bytes),)>(
+ 0xdeadbeef,
+ (("some string".to_string(), bytes(vec![1, 2, 3])),),
+ &hex!(
+ "
+ deadbeef
+ 0000000000000000000000000000000000000000000000000000000000000020
+ 0000000000000000000000000000000000000000000000000000000000000020
+ 0000000000000000000000000000000000000000000000000000000000000040
+ 0000000000000000000000000000000000000000000000000000000000000080
+ 000000000000000000000000000000000000000000000000000000000000000b
+ 736f6d6520737472696e67000000000000000000000000000000000000000000
+ 0000000000000000000000000000000000000000000000000000000000000003
+ 0102030000000000000000000000000000000000000000000000000000000000
+ "
+ ),
+ );
+}
+
+#[test]
+fn encode_decode_tuple0_tuple1_uint8_tuple1_string() {
+ test_impl::<((u8,), (String,))>(
+ 0xdeadbeef,
+ ((0xff,), ("some string".to_string(),)),
+ &hex!(
+ "
+ deadbeef
+ 0000000000000000000000000000000000000000000000000000000000000020
+ 00000000000000000000000000000000000000000000000000000000000000ff
+ 0000000000000000000000000000000000000000000000000000000000000040
+ 0000000000000000000000000000000000000000000000000000000000000020
+ 000000000000000000000000000000000000000000000000000000000000000b
+ 736f6d6520737472696e67000000000000000000000000000000000000000000
+ "
+ ),
+ );
+}
crates/evm-coder/tests/abi_derive_generation.rsdiffbeforeafterboth--- a/crates/evm-coder/tests/abi_derive_generation.rs
+++ b/crates/evm-coder/tests/abi_derive_generation.rs
@@ -1,7 +1,7 @@
use evm_coder_procedural::AbiCoder;
use evm_coder::{
types::*,
- abi::{AbiType, AbiRead, AbiWrite},
+ abi::{AbiType, AbiRead, AbiWrite, AbiReader, AbiWriter},
};
// TODO: move to build_failed tests
@@ -406,66 +406,199 @@
);
}
-fn test_impl<TypeStruct, TupleStruct, Tuple>(
- type_struct_data: TypeStruct,
- tuple_struct_data: TupleStruct,
+const FUNCTION_IDENTIFIER: u32 = 0xdeadbeef;
+
+fn test_impl<Tuple, TupleStruct, TypeStruct>(
tuple_data: Tuple,
+ tuple_struct_data: TupleStruct,
+ type_struct_data: TypeStruct,
) where
TypeStruct: AbiWrite + AbiRead + std::cmp::PartialEq + std::fmt::Debug,
+ TupleStruct: AbiWrite + AbiRead + std::cmp::PartialEq + std::fmt::Debug,
Tuple: AbiWrite + AbiRead + std::cmp::PartialEq + std::fmt::Debug,
{
- use evm_coder::abi::{AbiReader, AbiWriter};
- const FUNCTION_IDENTIFIER: u32 = 0xdeadbeef;
+ let encoded_type_struct = test_abi_write_impl(&type_struct_data);
+ let encoded_tuple_struct = test_abi_write_impl(&tuple_struct_data);
+ let encoded_tuple = test_abi_write_impl(&tuple_data);
- let mut writer = AbiWriter::new_call(FUNCTION_IDENTIFIER);
- tuple_data.abi_write(&mut writer);
- let encoded_tuple = writer.finish();
+ similar_asserts::assert_eq!(encoded_tuple, encoded_type_struct);
+ similar_asserts::assert_eq!(encoded_tuple, encoded_tuple_struct);
- let mut writer = AbiWriter::new_call(FUNCTION_IDENTIFIER);
- type_struct_data.abi_write(&mut writer);
- let encoded_struct = writer.finish();
+ // dbg!(&encoded_tuple);
+ // dbg!(&encoded_tuple_struct);
+ // dbg!(&encoded_type_struct);
- similar_asserts::assert_eq!(encoded_tuple, encoded_struct);
+ {
+ let (_, mut decoder) = AbiReader::new_call(&encoded_tuple).unwrap();
+ let restored_struct_data = <TypeStruct>::abi_read(&mut decoder).unwrap();
+ assert_eq!(restored_struct_data, type_struct_data);
+ }
+ {
+ let (_, mut decoder) = AbiReader::new_call(&encoded_tuple).unwrap();
+ let restored_struct_data = <TupleStruct>::abi_read(&mut decoder).unwrap();
+ assert_eq!(restored_struct_data, tuple_struct_data);
+ }
- // let (_, mut decoder) = AbiReader::new_call(&encoded_tuple).unwrap();
- // let restored_struct_data = <TypeStruct>::abi_read(&mut decoder).unwrap();
- // assert_eq!(restored_struct_data, type_struct_data);
+ {
+ let (_, mut decoder) = AbiReader::new_call(&encoded_type_struct).unwrap();
+ let restored_tuple_data = <Tuple>::abi_read(&mut decoder).unwrap();
+ assert_eq!(restored_tuple_data, tuple_data);
+ }
+ {
+ let (_, mut decoder) = AbiReader::new_call(&encoded_tuple_struct).unwrap();
+ let restored_tuple_data = <Tuple>::abi_read(&mut decoder).unwrap();
+ assert_eq!(restored_tuple_data, tuple_data);
+ }
+}
- // let (_, mut decoder) = AbiReader::new_call(&encoded_struct).unwrap();
- // let restored_tuple_data = <Tuple>::abi_read(&mut decoder).unwrap();
- // assert_eq!(restored_tuple_data, tuple_data);
+fn test_abi_write_impl<A>(data: &A) -> Vec<u8>
+where
+ A: AbiWrite + AbiRead + std::cmp::PartialEq + std::fmt::Debug,
+{
+ let mut writer = AbiWriter::new_call(FUNCTION_IDENTIFIER);
+ data.abi_write(&mut writer);
+ let encoded_tuple = writer.finish();
+ encoded_tuple
}
#[test]
fn codec_struct_1_simple() {
let _a = 0xff;
- test_impl::<TypeStruct1SimpleParam, TupleStruct1SimpleParam, (uint8,)>(
- TypeStruct1SimpleParam { _a },
+ test_impl::<(uint8,), TupleStruct1SimpleParam, TypeStruct1SimpleParam>(
+ (_a,),
TupleStruct1SimpleParam(_a),
- (_a,),
+ TypeStruct1SimpleParam { _a },
);
}
#[test]
fn codec_struct_1_dynamic() {
let _a: String = "some string".into();
- test_impl::<TypeStruct1DynamicParam, TupleStruct1DynamicParam, (String,)>(
- TypeStruct1DynamicParam { _a: _a.clone() },
+ test_impl::<(String,), TupleStruct1DynamicParam, TypeStruct1DynamicParam>(
+ (_a.clone(),),
TupleStruct1DynamicParam(_a.clone()),
- (_a,),
+ TypeStruct1DynamicParam { _a },
+ );
+}
+
+#[test]
+fn codec_struct_1_derived_simple() {
+ let _a: u8 = 0xff;
+ test_impl::<((u8,),), TupleStruct1DerivedSimpleParam, TypeStruct1DerivedSimpleParam>(
+ ((_a,),),
+ TupleStruct1DerivedSimpleParam(TupleStruct1SimpleParam(_a)),
+ TypeStruct1DerivedSimpleParam {
+ _a: TypeStruct1SimpleParam { _a },
+ },
);
}
#[test]
+fn codec_struct_1_derived_dynamic() {
+ let _a: String = "some string".into();
+ test_impl::<((String,),), TupleStruct1DerivedDynamicParam, TypeStruct1DerivedDynamicParam>(
+ ((_a.clone(),),),
+ TupleStruct1DerivedDynamicParam(TupleStruct1DynamicParam(_a.clone())),
+ TypeStruct1DerivedDynamicParam {
+ _a: TypeStruct1DynamicParam { _a },
+ },
+ );
+}
+
+#[test]
+fn codec_struct_2_simple() {
+ let _a = 0xff;
+ let _b = 0xbeefbaba;
+ test_impl::<(u8, u32), TupleStruct2SimpleParam, TypeStruct2SimpleParam>(
+ (_a, _b),
+ TupleStruct2SimpleParam(_a, _b),
+ TypeStruct2SimpleParam { _a, _b },
+ );
+}
+
+#[test]
fn codec_struct_2_dynamic() {
let _a: String = "some string".into();
let _b: bytes = bytes(vec![0x11, 0x22, 0x33]);
- test_impl::<TypeStruct2DynamicParam, TupleStruct2DynamicParam, (String, bytes)>(
- TypeStruct2DynamicParam {
- _a: _a.clone(),
- _b: _b.clone(),
- },
+ test_impl::<(String, bytes), TupleStruct2DynamicParam, TypeStruct2DynamicParam>(
+ (_a.clone(), _b.clone()),
TupleStruct2DynamicParam(_a.clone(), _b.clone()),
- (_a, _b),
+ TypeStruct2DynamicParam { _a, _b },
+ );
+}
+
+#[test]
+fn codec_struct_2_mixed() {
+ let _a: u8 = 0xff;
+ let _b: bytes = bytes(vec![0x11, 0x22, 0x33]);
+ test_impl::<(u8, bytes), TupleStruct2MixedParam, TypeStruct2MixedParam>(
+ (_a.clone(), _b.clone()),
+ TupleStruct2MixedParam(_a.clone(), _b.clone()),
+ TypeStruct2MixedParam { _a, _b },
+ );
+}
+
+#[test]
+fn codec_struct_2_derived_simple() {
+ let _a = 0xff;
+ let _b = 0xbeefbaba;
+ test_impl::<((u8,), (u8, u32)), TupleStruct2DerivedSimpleParam, TypeStruct2DerivedSimpleParam>(
+ ((_a,), (_a, _b)),
+ TupleStruct2DerivedSimpleParam(
+ TupleStruct1SimpleParam(_a),
+ TupleStruct2SimpleParam(_a, _b),
+ ),
+ TypeStruct2DerivedSimpleParam {
+ _a: TypeStruct1SimpleParam { _a },
+ _b: TypeStruct2SimpleParam { _a, _b },
+ },
+ );
+}
+
+#[test]
+fn codec_struct_2_derived_dynamic() {
+ let _a = "some string".to_string();
+ let _b = bytes(vec![0x11, 0x22, 0x33]);
+ test_impl::<
+ ((String,), (String, bytes)),
+ TupleStruct2DerivedDynamicParam,
+ TypeStruct2DerivedDynamicParam,
+ >(
+ ((_a.clone(),), (_a.clone(), _b.clone())),
+ TupleStruct2DerivedDynamicParam(
+ TupleStruct1DynamicParam(_a.clone()),
+ TupleStruct2DynamicParam(_a.clone(), _b.clone()),
+ ),
+ TypeStruct2DerivedDynamicParam {
+ _a: TypeStruct1DynamicParam { _a: _a.clone() },
+ _b: TypeStruct2DynamicParam { _a, _b },
+ },
+ );
+}
+
+#[test]
+fn codec_struct_3_derived_mixed() {
+ let int = 0xff;
+ let by = bytes(vec![0x11, 0x22, 0x33]);
+ let string = "some string".to_string();
+ test_impl::<
+ ((u8,), (String, bytes), (u8, bytes)),
+ TupleStruct3DerivedMixedParam,
+ TypeStruct3DerivedMixedParam,
+ >(
+ ((int,), (string.clone(), by.clone()), (int, by.clone())),
+ TupleStruct3DerivedMixedParam(
+ TupleStruct1SimpleParam(int),
+ TupleStruct2DynamicParam(string.clone(), by.clone()),
+ TupleStruct2MixedParam(int, by.clone()),
+ ),
+ TypeStruct3DerivedMixedParam {
+ _a: TypeStruct1SimpleParam { _a: int },
+ _b: TypeStruct2DynamicParam {
+ _a: string.clone(),
+ _b: by.clone(),
+ },
+ _c: TypeStruct2MixedParam { _a: int, _b: by },
+ },
);
}