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

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 sp_std::borrow::ToOwned;20use fp_evm::{Context, ExitRevert, PrecompileFailure};21use sp_core::U256;22use sp_std::marker::PhantomData;2324mod data;2526pub use data::{Bytes, EvmData, EvmDataReader, EvmDataWriter};2728/// Alias for Result returning an EVM precompile error.29pub type EvmResult<T = ()> = Result<T, PrecompileFailure>;3031/// Helper functions requiring a Runtime.32/// This runtime must of course implement `pallet_evm::Config`.33#[derive(Clone, Copy, Debug)]34pub struct RuntimeHelper<Runtime>(PhantomData<Runtime>);3536/// Represents modifiers a Solidity function can be annotated with.37#[derive(Copy, Clone, PartialEq, Eq)]38pub enum FunctionModifier {39	/// Function that doesn't modify the state.40	View,41	/// Function that modifies the state and accept funds.42	Payable,43}4445/// Custom Gasometer to record costs in precompiles.46/// It is advised to record known costs as early as possible to47/// avoid unecessary computations if there is an Out of Gas.48///49/// Provides functions related to reverts, as reverts takes the recorded amount50/// of gas into account.51#[derive(Clone, Copy, Debug)]52pub struct Gasometer();5354impl Gasometer {55	/// Create a new Gasometer with provided gas limit.56	/// None is no limit.57	pub fn new() -> Self {58		Self()59	}6061	/// Revert the execution, making the user pay for the the currently62	/// recorded cost. It is better to **revert** instead of **error** as63	/// erroring consumes the entire gas limit, and **revert** returns an error64	/// message to the calling contract.65	///66	/// TODO : Record cost of the input based on its size and handle Out of Gas ?67	/// This might be required if we format revert messages using user data.68	#[must_use]69	pub fn revert(&self, output: impl AsRef<[u8]>) -> PrecompileFailure {70		PrecompileFailure::Revert {71			exit_status: ExitRevert::Reverted,72			output: output.as_ref().to_owned(),73		}74	}7576	/// Check that a function call is compatible with the context it is77	/// called into.78	pub fn check_function_modifier(79		&self,80		context: &Context,81		is_static: bool,82		modifier: FunctionModifier,83	) -> EvmResult {84		if is_static && modifier != FunctionModifier::View {85			return Err(self.revert("can't call non-static function in static context"));86		}8788		if modifier != FunctionModifier::Payable && context.apparent_value > U256::zero() {89			return Err(self.revert("function is not payable"));90		}9192		Ok(())93	}94}