git.delta.rocks / unique-network / refs/commits / 99f6558100fe

difftreelog

source

pallets/evm-coder-substrate/src/execution.rs5.3 KiBsourcehistory
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617//! Contract execution related types1819#[cfg(not(feature = "std"))]20use alloc::string::{String, ToString};21#[cfg(feature = "std")]22use std::string::{String, ToString};2324use evm_coder::ERC165Call;25pub use evm_coder_substrate_procedural::PreDispatch;26use evm_core::{ExitError, ExitFatal};27pub use frame_support::weights::Weight;2829/// Execution error, should be convertible between EVM and Substrate.30#[derive(Debug, Clone)]31pub enum Error {32	/// Non-fatal contract error occured33	Revert(String),34	/// EVM fatal error35	Fatal(ExitFatal),36	/// EVM normal error37	Error(ExitError),38}3940impl<E> From<E> for Error41where42	E: ToString,43{44	fn from(e: E) -> Self {45		Self::Revert(e.to_string())46	}47}4849/// To be used in [`crate::solidity_interface`] implementation.50pub type Result<T> = core::result::Result<T, Error>;51/// Return type of items in [`crate::solidity_interface`] definition52pub type ResultWithPostInfo<T> =53	core::result::Result<WithPostDispatchInfo<T>, WithPostDispatchInfo<Error>>;5455pub trait PreDispatch {56	fn dispatch_info(&self) -> DispatchInfo;57}5859impl PreDispatch for ERC165Call {60	fn dispatch_info(&self) -> DispatchInfo {61		DispatchInfo {62			// ERC165 impl should be cheap63			weight: Weight::from_parts(200, 0),64		}65	}66}6768/// Static information collected from [`crate::weight`].69pub struct DispatchInfo {70	/// Statically predicted call weight71	pub weight: Weight,72}7374impl From<Weight> for DispatchInfo {75	fn from(weight: Weight) -> Self {76		Self { weight }77	}78}79// TODO: use 2-dimensional weight after frontier upgrade80impl From<u64> for DispatchInfo {81	fn from(weight: u64) -> Self {82		Self {83			weight: Weight::from_parts(weight, 0),84		}85	}86}87impl From<()> for DispatchInfo {88	fn from(_: ()) -> Self {89		Self {90			weight: Weight::zero(),91		}92	}93}9495/// Weight information that is only available post dispatch.96/// Note: This can only be used to reduce the weight or fee, not increase it.97#[derive(Default, Clone)]98pub struct PostDispatchInfo {99	/// Actual weight consumed by call100	pub actual_weight: Option<Weight>,101}102103impl PostDispatchInfo {104	/// Calculate amount to be returned back to user105	pub fn calc_unspent(&self, info: &DispatchInfo) -> Weight {106		info.weight - self.calc_actual_weight(info)107	}108109	/// Calculate actual consumed weight, saturating to weight reported110	/// pre-dispatch111	pub fn calc_actual_weight(&self, info: &DispatchInfo) -> Weight {112		if let Some(actual_weight) = self.actual_weight {113			actual_weight.min(info.weight)114		} else {115			info.weight116		}117	}118}119120/// Wrapper for PostDispatchInfo and any user-provided data121#[derive(Clone)]122pub struct WithPostDispatchInfo<T> {123	/// User provided data124	pub data: T,125	/// Info known after dispatch126	pub post_info: PostDispatchInfo,127}128129impl<T> From<T> for WithPostDispatchInfo<T> {130	fn from(data: T) -> Self {131		Self {132			data,133			post_info: Default::default(),134		}135	}136}137#[allow(clippy::crate_in_macro_def)]138#[macro_export]139macro_rules! frontier_contract {140	(141		macro_rules! $res:ident {...}142		impl$(<$($gen:ident $(: $($(+)? $bound:ty)*)?),+ $(,)?>)? Contract for $ty:ty {...}143	) => {144		/// Generate macro to convert function return value into Contract result145		macro_rules! $res {146			($i:expr) => {{147				pallet_evm_coder_substrate::spez! {148					for res = $i;149					match<T> ::pallet_evm_coder_substrate::execution::ResultWithPostInfo<T> -> ::pallet_evm_coder_substrate::execution::ResultWithPostInfo<T> {150						res151					}152					match<T> ::pallet_evm_coder_substrate::execution::Result<T> -> ::pallet_evm_coder_substrate::execution::ResultWithPostInfo<T> {153						res154							.map(pallet_evm_coder_substrate::execution::WithPostDispatchInfo::from)155							.map_err(pallet_evm_coder_substrate::execution::WithPostDispatchInfo::from)156					}157					match<T> T -> ::pallet_evm_coder_substrate::execution::ResultWithPostInfo<T> {158						Ok(pallet_evm_coder_substrate::execution::WithPostDispatchInfo::from(res))159					}160				}161			}};162		}163		impl $(<$($gen),+>)? $crate::Contract for $ty where T: crate::Config {164			type Error = ::pallet_evm_coder_substrate::execution::Error;165			type WithPostInfo<R> = ::pallet_evm_coder_substrate::execution::WithPostDispatchInfo<R>;166			type Result<R, E> = core::result::Result<R, E>;167			fn map_post<I, O>(168				v: Self::WithPostInfo<I>,169				mapper: impl FnOnce(I) -> O,170			) -> Self::WithPostInfo<O> {171				::pallet_evm_coder_substrate::execution::WithPostDispatchInfo {172					data: mapper(v.data),173					post_info: v.post_info,174				}175			}176			fn with_default_post<V>(v: V) -> Self::WithPostInfo<V> {177				::pallet_evm_coder_substrate::execution::WithPostDispatchInfo {178					data: v,179					post_info: ::pallet_evm_coder_substrate::execution::PostDispatchInfo {180						actual_weight: None,181					},182				}183			}184		}185	};186}