difftreelog
refactor Abi impls
in: master
5 files changed
crates/evm-coder/procedural/src/abi_derive.rsdiffbeforeafterboth--- a/crates/evm-coder/procedural/src/abi_derive.rs
+++ b/crates/evm-coder/procedural/src/abi_derive.rs
@@ -121,7 +121,7 @@
#(
let #field_names = {
let value = <#field_types as ::evm_coder::abi::AbiRead>::abi_read(&mut subresult)?;
- if !is_dynamic {subresult.seek(<#field_types as ::evm_coder::abi::AbiType>::size())};
+ if !is_dynamic {subresult.bytes_read(<#field_types as ::evm_coder::abi::AbiType>::size())};
value
};
)*
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.rsdiffbeforeafterboth1use crate::{2 execution::{Result, ResultWithPostInfo, WithPostDispatchInfo},3 types::*,4 make_signature,5 custom_signature::SignatureUnit,6};7use super::{traits::*, ABI_ALIGNMENT, AbiReader, AbiWriter};8use primitive_types::{U256, H160};910#[cfg(not(feature = "std"))]11use alloc::vec::Vec;1213macro_rules! impl_abi_type {14 ($ty:ty, $name:ident, $dynamic:literal) => {15 impl sealed::CanBePlacedInVec for $ty {}1617 impl AbiType for $ty {18 const SIGNATURE: SignatureUnit = make_signature!(new fixed(stringify!($name)));1920 fn is_dynamic() -> bool {21 $dynamic22 }2324 fn size() -> usize {25 ABI_ALIGNMENT26 }27 }28 };29}3031macro_rules! impl_abi_readable {32 ($ty:ty, $method:ident) => {33 impl AbiRead for $ty {34 fn abi_read(reader: &mut AbiReader) -> Result<$ty> {35 reader.$method()36 }37 }38 };39}4041macro_rules! impl_abi_writeable {42 ($ty:ty, $method:ident) => {43 impl AbiWrite for $ty {44 fn abi_write(&self, writer: &mut AbiWriter) {45 writer.$method(&self)46 }47 }48 };49}5051macro_rules! impl_abi {52 ($ty:ty, $method:ident, $dynamic:literal) => {53 impl_abi_type!($ty, $method, $dynamic);54 impl_abi_readable!($ty, $method);55 impl_abi_writeable!($ty, $method);56 };57}5859impl_abi!(bool, bool, false);60impl_abi!(u8, uint8, false);61impl_abi!(u32, uint32, false);62impl_abi!(u64, uint64, false);63impl_abi!(u128, uint128, false);64impl_abi!(U256, uint256, false);65impl_abi!(H160, address, false);66impl_abi!(string, string, true);6768impl_abi_writeable!(&str, string);6970impl_abi_type!(bytes, bytes, true);7172impl AbiRead for bytes {73 fn abi_read(reader: &mut AbiReader) -> Result<bytes> {74 Ok(bytes(reader.bytes()?))75 }76}7778impl AbiWrite for bytes {79 fn abi_write(&self, writer: &mut AbiWriter) {80 writer.bytes(self.0.as_slice())81 }82}8384impl_abi_type!(bytes4, bytes4, false);85impl AbiRead for bytes4 {86 fn abi_read(reader: &mut AbiReader) -> Result<bytes4> {87 reader.bytes4()88 }89}9091impl<T: AbiType + AbiRead + sealed::CanBePlacedInVec> AbiRead for Vec<T> {92 fn abi_read(reader: &mut AbiReader) -> Result<Vec<T>> {93 let mut sub = reader.subresult(None)?;94 let size = sub.uint32()? as usize;95 sub.subresult_offset = sub.offset;96 let is_dynamic = <T as AbiType>::is_dynamic();97 let mut out = Vec::with_capacity(size);98 for _ in 0..size {99 out.push(<T as AbiRead>::abi_read(&mut sub)?);100 if !is_dynamic {101 sub.bytes_read(<T as AbiType>::size());102 };103 }104 Ok(out)105 }106}107108impl<T: AbiType> AbiType for Vec<T> {109 const SIGNATURE: SignatureUnit = make_signature!(new nameof(T::SIGNATURE) fixed("[]"));110111 fn is_dynamic() -> bool {112 true113 }114115 fn size() -> usize {116 ABI_ALIGNMENT117 }118}119120impl sealed::CanBePlacedInVec for Property {}121122impl AbiType for Property {123 const SIGNATURE: SignatureUnit = make_signature!(new fixed("(string,bytes)"));124125 fn is_dynamic() -> bool {126 string::is_dynamic() || bytes::is_dynamic()127 }128129 fn size() -> usize {130 <string as AbiType>::size() + <bytes as AbiType>::size()131 }132}133134impl AbiRead for Property {135 fn abi_read(reader: &mut AbiReader) -> Result<Property> {136 let size = if !Property::is_dynamic() {137 Some(<Property as AbiType>::size())138 } else {139 None140 };141 let mut subresult = reader.subresult(size)?;142 let key = <string>::abi_read(&mut subresult)?;143 let value = <bytes>::abi_read(&mut subresult)?;144145 Ok(Property { key, value })146 }147}148149impl AbiWrite for Property {150 fn abi_write(&self, writer: &mut AbiWriter) {151 (&self.key, &self.value).abi_write(writer);152 }153}154155impl<T: AbiWrite + AbiType> AbiWrite for Vec<T> {156 fn abi_write(&self, writer: &mut AbiWriter) {157 let is_dynamic = T::is_dynamic();158 let mut sub = if is_dynamic {159 AbiWriter::new_dynamic(is_dynamic)160 } else {161 AbiWriter::new()162 };163164 // Write items count165 (self.len() as u32).abi_write(&mut sub);166167 for item in self {168 item.abi_write(&mut sub);169 }170 writer.write_subresult(sub);171 }172}173174impl AbiWrite for () {175 fn abi_write(&self, _writer: &mut AbiWriter) {}176}177178/// This particular AbiWrite implementation should be split to another trait,179/// which only implements `to_result`, but due to lack of specialization feature180/// in stable Rust, we can't have blanket impl of this trait `for T where T: AbiWrite`,181/// so here we abusing default trait methods for it182impl<T: AbiWrite> AbiWrite for ResultWithPostInfo<T> {183 fn abi_write(&self, _writer: &mut AbiWriter) {184 debug_assert!(false, "shouldn't be called, see comment")185 }186 fn to_result(&self) -> ResultWithPostInfo<AbiWriter> {187 match self {188 Ok(v) => Ok(WithPostDispatchInfo {189 post_info: v.post_info.clone(),190 data: {191 let mut out = AbiWriter::new();192 v.data.abi_write(&mut out);193 out194 },195 }),196 Err(e) => Err(e.clone()),197 }198 }199}200201macro_rules! impl_tuples {202 ($($ident:ident)+) => {203 impl<$($ident: AbiType,)+> AbiType for ($($ident,)+)204 where205 $(206 $ident: AbiType,207 )+208 {209 const SIGNATURE: SignatureUnit = make_signature!(210 new fixed("(")211 $(nameof(<$ident>::SIGNATURE) fixed(","))+212 shift_left(1)213 fixed(")")214 );215216 fn is_dynamic() -> bool {217 false218 $(219 || <$ident>::is_dynamic()220 )*221 }222223 fn size() -> usize {224 0 $(+ <$ident>::size())+225 }226 }227228 impl<$($ident),+> sealed::CanBePlacedInVec for ($($ident,)+) {}229230 impl<$($ident),+> AbiRead for ($($ident,)+)231 where232 Self: AbiType,233 $($ident: AbiRead + AbiType,)+234 {235 fn abi_read(reader: &mut AbiReader) -> Result<($($ident,)+)> {236 let is_dynamic = <Self>::is_dynamic();237 let size = if !is_dynamic { Some(<Self>::size()) } else { None };238 let mut subresult = reader.subresult(size)?;239 Ok((240 $({241 let value = <$ident>::abi_read(&mut subresult)?;242 if !is_dynamic {subresult.bytes_read(<$ident as AbiType>::size())};243 value244 },)+245 ))246 }247 }248249 #[allow(non_snake_case)]250 impl<$($ident),+> AbiWrite for ($($ident,)+)251 where252 $($ident: AbiWrite + AbiType,)+253 {254 fn abi_write(&self, writer: &mut AbiWriter) {255 let ($($ident,)+) = self;256 if <Self as AbiType>::is_dynamic() {257 let mut sub = AbiWriter::new();258 $($ident.abi_write(&mut sub);)+259 writer.write_subresult(sub);260 } else {261 $($ident.abi_write(writer);)+262 }263 }264 }265 };266}267268impl_tuples! {A}269impl_tuples! {A B}270impl_tuples! {A B C}271impl_tuples! {A B C D}272impl_tuples! {A B C D E}273impl_tuples! {A B C D E F}274impl_tuples! {A B C D E F G}275impl_tuples! {A B C D E F G H}276impl_tuples! {A B C D E F G H I}277impl_tuples! {A B C D E F G H I J}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,