git.delta.rocks / unique-network / refs/commits / 7bd00bbda0e0

difftreelog

source

crates/evm-coder/src/abi/impls.rs6.8 KiBsourcehistory
1use 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_readable {14	($ty:ty, $method:ident, $dynamic:literal) => {15		impl sealed::CanBePlacedInVec for $ty {}1617		impl AbiType for $ty {18			const SIGNATURE: SignatureUnit = make_signature!(new fixed(stringify!($ty)));1920			fn is_dynamic() -> bool {21				$dynamic22			}2324			fn size() -> usize {25				ABI_ALIGNMENT26			}27		}2829		impl AbiRead for $ty {30			fn abi_read(reader: &mut AbiReader) -> Result<$ty> {31				reader.$method()32			}33		}34	};35}3637impl_abi_readable!(uint32, uint32, false);38impl_abi_readable!(uint64, uint64, false);39impl_abi_readable!(uint128, uint128, false);40impl_abi_readable!(uint256, uint256, false);41impl_abi_readable!(bytes4, bytes4, false);42impl_abi_readable!(address, address, false);43impl_abi_readable!(string, string, true);4445impl sealed::CanBePlacedInVec for bool {}4647impl AbiType for bool {48	const SIGNATURE: SignatureUnit = make_signature!(new fixed("bool"));4950	fn is_dynamic() -> bool {51		false52	}53	fn size() -> usize {54		ABI_ALIGNMENT55	}56}57impl AbiRead for bool {58	fn abi_read(reader: &mut AbiReader) -> Result<bool> {59		reader.bool()60	}61}6263impl AbiType for uint8 {64	const SIGNATURE: SignatureUnit = make_signature!(new fixed("uint8"));6566	fn is_dynamic() -> bool {67		false68	}69	fn size() -> usize {70		ABI_ALIGNMENT71	}72}73impl AbiRead for uint8 {74	fn abi_read(reader: &mut AbiReader) -> Result<uint8> {75		reader.uint8()76	}77}7879impl AbiType for bytes {80	const SIGNATURE: SignatureUnit = make_signature!(new fixed("bytes"));8182	fn is_dynamic() -> bool {83		true84	}85	fn size() -> usize {86		ABI_ALIGNMENT87	}88}89impl AbiRead for bytes {90	fn abi_read(reader: &mut AbiReader) -> Result<bytes> {91		Ok(bytes(reader.bytes()?))92	}93}9495impl<R: AbiRead + sealed::CanBePlacedInVec> AbiRead for Vec<R> {96	fn abi_read(reader: &mut AbiReader) -> Result<Vec<R>> {97		let mut sub = reader.subresult(None)?;98		let size = sub.uint32()? as usize;99		sub.subresult_offset = sub.offset;100		let mut out = Vec::with_capacity(size);101		for _ in 0..size {102			out.push(<R>::abi_read(&mut sub)?);103		}104		Ok(out)105	}106}107108impl<R: AbiType> AbiType for Vec<R> {109	const SIGNATURE: SignatureUnit = make_signature!(new nameof(R::SIGNATURE) fixed("[]"));110111	fn is_dynamic() -> bool {112		true113	}114115	fn size() -> usize {116		ABI_ALIGNMENT117	}118}119120impl sealed::CanBePlacedInVec for EthCrossAccount {}121122impl AbiType for EthCrossAccount {123	const SIGNATURE: SignatureUnit = make_signature!(new fixed("(address,uint256)"));124125	fn is_dynamic() -> bool {126		address::is_dynamic() || uint256::is_dynamic()127	}128129	fn size() -> usize {130		<address as AbiType>::size() + <uint256 as AbiType>::size()131	}132}133134impl AbiRead for EthCrossAccount {135	fn abi_read(reader: &mut AbiReader) -> Result<EthCrossAccount> {136		let size = if !EthCrossAccount::is_dynamic() {137			Some(<EthCrossAccount as AbiType>::size())138		} else {139			None140		};141		let mut subresult = reader.subresult(size)?;142		let eth = <address>::abi_read(&mut subresult)?;143		let sub = <uint256>::abi_read(&mut subresult)?;144145		Ok(EthCrossAccount { eth, sub })146	}147}148149impl AbiWrite for EthCrossAccount {150	fn abi_write(&self, writer: &mut AbiWriter) {151		self.eth.abi_write(writer);152		self.sub.abi_write(writer);153	}154}155156macro_rules! impl_abi_writeable {157	($ty:ty, $method:ident) => {158		impl AbiWrite for $ty {159			fn abi_write(&self, writer: &mut AbiWriter) {160				writer.$method(&self)161			}162		}163	};164}165166impl_abi_writeable!(u8, uint8);167impl_abi_writeable!(u32, uint32);168impl_abi_writeable!(u128, uint128);169impl_abi_writeable!(U256, uint256);170impl_abi_writeable!(H160, address);171impl_abi_writeable!(bool, bool);172impl_abi_writeable!(&str, string);173174impl AbiWrite for string {175	fn abi_write(&self, writer: &mut AbiWriter) {176		writer.string(self)177	}178}179180impl AbiWrite for bytes {181	fn abi_write(&self, writer: &mut AbiWriter) {182		writer.bytes(self.0.as_slice())183	}184}185186impl<T: AbiWrite + AbiType> AbiWrite for Vec<T> {187	fn abi_write(&self, writer: &mut AbiWriter) {188		let is_dynamic = T::is_dynamic();189		let mut sub = if is_dynamic {190			AbiWriter::new_dynamic(is_dynamic)191		} else {192			AbiWriter::new()193		};194195		// Write items count196		(self.len() as u32).abi_write(&mut sub);197198		for item in self {199			item.abi_write(&mut sub);200		}201		writer.write_subresult(sub);202	}203}204205impl AbiWrite for () {206	fn abi_write(&self, _writer: &mut AbiWriter) {}207}208209/// This particular AbiWrite implementation should be split to another trait,210/// which only implements `to_result`, but due to lack of specialization feature211/// in stable Rust, we can't have blanket impl of this trait `for T where T: AbiWrite`,212/// so here we abusing default trait methods for it213impl<T: AbiWrite> AbiWrite for ResultWithPostInfo<T> {214	fn abi_write(&self, _writer: &mut AbiWriter) {215		debug_assert!(false, "shouldn't be called, see comment")216	}217	fn to_result(&self) -> ResultWithPostInfo<AbiWriter> {218		match self {219			Ok(v) => Ok(WithPostDispatchInfo {220				post_info: v.post_info.clone(),221				data: {222					let mut out = AbiWriter::new();223					v.data.abi_write(&mut out);224					out225				},226			}),227			Err(e) => Err(e.clone()),228		}229	}230}231232macro_rules! impl_tuples {233	($($ident:ident)+) => {234		impl<$($ident: AbiType,)+> AbiType for ($($ident,)+)235		where236        $(237            $ident: AbiType,238        )+239		{240            const SIGNATURE: SignatureUnit = make_signature!(241                new fixed("(")242                $(nameof(<$ident>::SIGNATURE) fixed(","))+243                shift_left(1)244                fixed(")")245            );246247			fn is_dynamic() -> bool {248				false249				$(250					|| <$ident>::is_dynamic()251				)*252			}253254			fn size() -> usize {255				0 $(+ <$ident>::size())+256			}257		}258259		impl<$($ident),+> sealed::CanBePlacedInVec for ($($ident,)+) {}260261		impl<$($ident),+> AbiRead for ($($ident,)+)262		where263			$($ident: AbiRead,)+264			($($ident,)+): AbiType,265		{266			fn abi_read(reader: &mut AbiReader) -> Result<($($ident,)+)> {267				let size = if !<($($ident,)+)>::is_dynamic() { Some(<($($ident,)+)>::size()) } else { None };268				let mut subresult = reader.subresult(size)?;269				Ok((270					$(<$ident>::abi_read(&mut subresult)?,)+271				))272			}273		}274275		#[allow(non_snake_case)]276		impl<$($ident),+> AbiWrite for ($($ident,)+)277		where278			$($ident: AbiWrite,)+279		{280			fn abi_write(&self, writer: &mut AbiWriter) {281				let ($($ident,)+) = self;282				if writer.is_dynamic {283					let mut sub = AbiWriter::new();284					$($ident.abi_write(&mut sub);)+285					writer.write_subresult(sub);286				} else {287					$($ident.abi_write(writer);)+288				}289			}290		}291	};292}293294impl_tuples! {A}295impl_tuples! {A B}296impl_tuples! {A B C}297impl_tuples! {A B C D}298impl_tuples! {A B C D E}299impl_tuples! {A B C D E F}300impl_tuples! {A B C D E F G}301impl_tuples! {A B C D E F G H}302impl_tuples! {A B C D E F G H I}303impl_tuples! {A B C D E F G H I J}