git.delta.rocks / unique-network / refs/commits / ed0719fab73d

difftreelog

source

runtime/common/ethereum/precompiles/utils/mod.rs3.1 KiBsourcehistory
1// Copyright 2019-2022 PureStake Inc.2// Copyright 2022      Stake Technologies3// This file is part of Utils package, originally developed by Purestake Inc.4// Utils package used in Astar Network in terms of GPLv3.5//6// Utils is free software: you can redistribute it and/or modify7// it under the terms of the GNU General Public License as published by8// the Free Software Foundation, either version 3 of the License, or9// (at your option) any later version.1011// Utils is distributed in the hope that it will be useful,12// but WITHOUT ANY WARRANTY; without even the implied warranty of13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the14// GNU General Public License for more details.1516// You should have received a copy of the GNU General Public License17// along with Utils.  If not, see <http://www.gnu.org/licenses/>.1819use fp_evm::{Context, ExitRevert, PrecompileFailure};20use sp_core::U256;21use sp_std::{borrow::ToOwned, marker::PhantomData};2223mod data;2425pub use data::{Bytes, EvmDataReader, EvmDataWriter};2627/// Alias for Result returning an EVM precompile error.28pub type EvmResult<T = ()> = Result<T, PrecompileFailure>;2930/// Helper functions requiring a Runtime.31/// This runtime must of course implement `pallet_evm::Config`.32#[derive(Clone, Copy, Debug)]33pub struct RuntimeHelper<Runtime>(PhantomData<Runtime>);3435/// Represents modifiers a Solidity function can be annotated with.36#[derive(Copy, Clone, PartialEq, Eq)]37pub enum FunctionModifier {38	/// Function that doesn't modify the state.39	View,40	/// Function that modifies the state and accept funds.41	Payable,42}4344/// Custom Gasometer to record costs in precompiles.45/// It is advised to record known costs as early as possible to46/// avoid unecessary computations if there is an Out of Gas.47///48/// Provides functions related to reverts, as reverts takes the recorded amount49/// of gas into account.50#[derive(Clone, Copy, Debug)]51pub struct Gasometer();5253impl Gasometer {54	/// Create a new Gasometer with provided gas limit.55	/// None is no limit.56	pub fn new() -> Self {57		Self()58	}5960	/// Revert the execution, making the user pay for the the currently61	/// recorded cost. It is better to **revert** instead of **error** as62	/// erroring consumes the entire gas limit, and **revert** returns an error63	/// message to the calling contract.64	///65	/// TODO : Record cost of the input based on its size and handle Out of Gas ?66	/// This might be required if we format revert messages using user data.67	#[must_use]68	pub fn revert(&self, output: impl AsRef<[u8]>) -> PrecompileFailure {69		PrecompileFailure::Revert {70			exit_status: ExitRevert::Reverted,71			output: output.as_ref().to_owned(),72		}73	}7475	/// Check that a function call is compatible with the context it is76	/// called into.77	pub fn check_function_modifier(78		&self,79		context: &Context,80		is_static: bool,81		modifier: FunctionModifier,82	) -> EvmResult {83		if is_static && modifier != FunctionModifier::View {84			return Err(self.revert("can't call non-static function in static context"));85		}8687		if modifier != FunctionModifier::Payable && context.apparent_value > U256::zero() {88			return Err(self.revert("function is not payable"));89		}9091		Ok(())92	}93}