diff --git a/runtime/common/src/precompile/dex.rs b/runtime/common/src/precompile/dex.rs index 5640ef87c..1f94099c6 100644 --- a/runtime/common/src/precompile/dex.rs +++ b/runtime/common/src/precompile/dex.rs @@ -16,8 +16,10 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use super::input::{Input, InputT, Output}; -use crate::precompile::input::InputPricer; +use super::{ + input::{Input, InputPricer, InputT, Output}, + target_gas_limit, +}; use crate::WeightToGas; use frame_support::{log, traits::Get}; use module_dex::WeightInfo; @@ -68,7 +70,7 @@ where Runtime::AccountId, Runtime::AddressMapping, ::Erc20InfoMapping, - >::new(input, target_gas); + >::new(input, target_gas_limit(target_gas)); let gas_cost = Pricer::::cost(&input)?; @@ -119,7 +121,7 @@ where PrecompileFailure::Revert { exit_status: ExitRevert::Reverted, output: "Dex get_liquidity_token_address failed".into(), - cost: target_gas.unwrap_or_default(), + cost: target_gas_limit(target_gas).unwrap_or_default(), })?; Ok(PrecompileOutput { @@ -149,7 +151,7 @@ where PrecompileFailure::Revert { exit_status: ExitRevert::Reverted, output: "Dex get_swap_target_amount failed".into(), - cost: target_gas.unwrap_or_default(), + cost: target_gas_limit(target_gas).unwrap_or_default(), })?; Ok(PrecompileOutput { @@ -179,7 +181,7 @@ where PrecompileFailure::Revert { exit_status: ExitRevert::Reverted, output: "Dex get_swap_supply_amount failed".into(), - cost: target_gas.unwrap_or_default(), + cost: target_gas_limit(target_gas).unwrap_or_default(), })?; Ok(PrecompileOutput { @@ -211,7 +213,7 @@ where PrecompileFailure::Revert { exit_status: ExitRevert::Reverted, output: Into::<&str>::into(e).as_bytes().to_vec(), - cost: target_gas.unwrap_or_default(), + cost: target_gas_limit(target_gas).unwrap_or_default(), })?; Ok(PrecompileOutput { @@ -243,7 +245,7 @@ where PrecompileFailure::Revert { exit_status: ExitRevert::Reverted, output: Into::<&str>::into(e).as_bytes().to_vec(), - cost: target_gas.unwrap_or_default(), + cost: target_gas_limit(target_gas).unwrap_or_default(), })?; Ok(PrecompileOutput { @@ -279,7 +281,7 @@ where .map_err(|e| PrecompileFailure::Revert { exit_status: ExitRevert::Reverted, output: Into::<&str>::into(e).as_bytes().to_vec(), - cost: target_gas.unwrap_or_default(), + cost: target_gas_limit(target_gas).unwrap_or_default(), })?; Ok(PrecompileOutput { @@ -315,7 +317,7 @@ where .map_err(|e| PrecompileFailure::Revert { exit_status: ExitRevert::Reverted, output: Into::<&str>::into(e).as_bytes().to_vec(), - cost: target_gas.unwrap_or_default(), + cost: target_gas_limit(target_gas).unwrap_or_default(), })?; Ok(PrecompileOutput { @@ -595,7 +597,7 @@ mod tests { PrecompileFailure::Revert { exit_status: ExitRevert::Reverted, output: "invalid currency id".into(), - cost: 10000, + cost: target_gas_limit(Some(10_000)).unwrap(), } ); }); diff --git a/runtime/common/src/precompile/evm.rs b/runtime/common/src/precompile/evm.rs index d73327f47..3b300c100 100644 --- a/runtime/common/src/precompile/evm.rs +++ b/runtime/common/src/precompile/evm.rs @@ -16,25 +16,23 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . +use super::{ + input::{Input, InputPricer, InputT, Output}, + target_gas_limit, + weights::PrecompileWeights, +}; use crate::WeightToGas; use module_evm::{ precompiles::Precompile, runner::state::{PrecompileFailure, PrecompileOutput, PrecompileResult}, Context, ExitError, ExitRevert, ExitSucceed, WeightInfo, }; +use module_support::EVMManager; use num_enum::{IntoPrimitive, TryFromPrimitive}; +use primitives::Balance; use sp_runtime::{traits::Convert, RuntimeDebug}; use sp_std::{marker::PhantomData, prelude::*}; -use module_support::EVMManager; - -use super::{ - input::{Input, InputT, Output}, - weights::PrecompileWeights, -}; -use crate::precompile::input::InputPricer; -use primitives::Balance; - /// The `EVM` impl precompile. /// /// `input` data starts with `action`. @@ -71,7 +69,8 @@ where { fn execute(input: &[u8], target_gas: Option, _context: &Context, _is_static: bool) -> PrecompileResult { let input = Input::::new( - input, target_gas, + input, + target_gas_limit(target_gas), ); let gas_cost = Pricer::::cost(&input)?; @@ -112,7 +111,7 @@ where PrecompileFailure::Revert { exit_status: ExitRevert::Reverted, output: Into::<&str>::into(e).as_bytes().to_vec(), - cost: target_gas.unwrap_or_default(), + cost: target_gas_limit(target_gas).unwrap_or_default(), } })?; @@ -160,7 +159,7 @@ where .map_err(|e| PrecompileFailure::Revert { exit_status: ExitRevert::Reverted, output: Into::<&str>::into(e).as_bytes().to_vec(), - cost: target_gas.unwrap_or_default(), + cost: target_gas_limit(target_gas).unwrap_or_default(), })?; Ok(PrecompileOutput { @@ -177,7 +176,7 @@ where PrecompileFailure::Revert { exit_status: ExitRevert::Reverted, output: Into::<&str>::into(e).as_bytes().to_vec(), - cost: target_gas.unwrap_or_default(), + cost: target_gas_limit(target_gas).unwrap_or_default(), } })?; @@ -194,7 +193,7 @@ where PrecompileFailure::Revert { exit_status: ExitRevert::Reverted, output: Into::<&str>::into(e).as_bytes().to_vec(), - cost: target_gas.unwrap_or_default(), + cost: target_gas_limit(target_gas).unwrap_or_default(), } })?; @@ -211,7 +210,7 @@ where PrecompileFailure::Revert { exit_status: ExitRevert::Reverted, output: Into::<&str>::into(e).as_bytes().to_vec(), - cost: target_gas.unwrap_or_default(), + cost: target_gas_limit(target_gas).unwrap_or_default(), } })?; diff --git a/runtime/common/src/precompile/mod.rs b/runtime/common/src/precompile/mod.rs index 0ebc9319f..c5fbb7934 100644 --- a/runtime/common/src/precompile/mod.rs +++ b/runtime/common/src/precompile/mod.rs @@ -76,6 +76,10 @@ pub const ORACLE: H160 = H160(hex!("0000000000000000000000000000000000000403")); pub const SCHEDULER: H160 = H160(hex!("0000000000000000000000000000000000000404")); pub const DEX: H160 = H160(hex!("0000000000000000000000000000000000000405")); +pub fn target_gas_limit(target_gas: Option) -> Option { + target_gas.map(|x| x.saturating_div(10).saturating_mul(9)) // 90% +} + pub struct AllPrecompiles { active: BTreeSet, _marker: PhantomData, diff --git a/runtime/common/src/precompile/multicurrency.rs b/runtime/common/src/precompile/multicurrency.rs index c8dcb9686..99f9fb523 100644 --- a/runtime/common/src/precompile/multicurrency.rs +++ b/runtime/common/src/precompile/multicurrency.rs @@ -16,8 +16,11 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use super::input::{Input, InputT, Output}; -use crate::{precompile::input::InputPricer, WeightToGas}; +use super::{ + input::{Input, InputPricer, InputT, Output}, + target_gas_limit, +}; +use crate::WeightToGas; use frame_support::{ log, traits::{Currency, Get}, @@ -70,13 +73,13 @@ where Runtime::AccountId, ::AddressMapping, Runtime::Erc20InfoMapping, - >::new(input, target_gas); + >::new(input, target_gas_limit(target_gas)); let currency_id = Runtime::Erc20InfoMapping::decode_evm_address(context.caller).ok_or_else(|| PrecompileFailure::Revert { exit_status: ExitRevert::Reverted, output: "invalid currency id".into(), - cost: target_gas.unwrap_or_default(), + cost: target_gas_limit(target_gas).unwrap_or_default(), })?; let gas_cost = Pricer::::cost(&input, currency_id)?; @@ -98,7 +101,7 @@ where let name = Runtime::Erc20InfoMapping::name(currency_id).ok_or_else(|| PrecompileFailure::Revert { exit_status: ExitRevert::Reverted, output: "Get name failed".into(), - cost: target_gas.unwrap_or_default(), + cost: target_gas_limit(target_gas).unwrap_or_default(), })?; log::debug!(target: "evm", "multicurrency: name: {:?}", name); @@ -114,7 +117,7 @@ where Runtime::Erc20InfoMapping::symbol(currency_id).ok_or_else(|| PrecompileFailure::Revert { exit_status: ExitRevert::Reverted, output: "Get symbol failed".into(), - cost: target_gas.unwrap_or_default(), + cost: target_gas_limit(target_gas).unwrap_or_default(), })?; log::debug!(target: "evm", "multicurrency: symbol: {:?}", symbol); @@ -130,7 +133,7 @@ where Runtime::Erc20InfoMapping::decimals(currency_id).ok_or_else(|| PrecompileFailure::Revert { exit_status: ExitRevert::Reverted, output: "Get decimals failed".into(), - cost: target_gas.unwrap_or_default(), + cost: target_gas_limit(target_gas).unwrap_or_default(), })?; log::debug!(target: "evm", "multicurrency: decimals: {:?}", decimals); @@ -185,7 +188,7 @@ where .map_err(|e| PrecompileFailure::Revert { exit_status: ExitRevert::Reverted, output: Into::<&str>::into(e).as_bytes().to_vec(), - cost: target_gas.unwrap_or_default(), + cost: target_gas_limit(target_gas).unwrap_or_default(), })?; Ok(PrecompileOutput { @@ -306,7 +309,7 @@ mod tests { PrecompileFailure::Revert { exit_status: ExitRevert::Reverted, output: "invalid currency id".into(), - cost: 10_000, + cost: target_gas_limit(Some(10_000)).unwrap(), } ); }); @@ -550,7 +553,7 @@ mod tests { PrecompileFailure::Revert { exit_status: ExitRevert::Reverted, output: "BalanceTooLow".into(), - cost: 100_000, + cost: target_gas_limit(Some(100_000)).unwrap(), } ); }) diff --git a/runtime/common/src/precompile/nft.rs b/runtime/common/src/precompile/nft.rs index b9f245c51..a885d01d1 100644 --- a/runtime/common/src/precompile/nft.rs +++ b/runtime/common/src/precompile/nft.rs @@ -16,6 +16,10 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . +use super::{ + input::{Input, InputT, Output}, + target_gas_limit, +}; use frame_support::{ log, traits::tokens::nonfungibles::{Inspect, Transfer}, @@ -26,16 +30,13 @@ use module_evm::{ Context, ExitError, ExitRevert, ExitSucceed, }; use module_support::AddressMapping; +use num_enum::{IntoPrimitive, TryFromPrimitive}; +use orml_traits::InspectExtended; +use primitives::nft::NFTBalance; use sp_core::H160; use sp_runtime::RuntimeDebug; use sp_std::{marker::PhantomData, prelude::*}; -use orml_traits::InspectExtended; - -use super::input::{Input, InputT, Output}; -use num_enum::{IntoPrimitive, TryFromPrimitive}; -use primitives::nft::NFTBalance; - /// The `NFT` impl precompile. /// /// `input` data starts with `action`. @@ -64,7 +65,8 @@ where { fn execute(input: &[u8], target_gas: Option, _context: &Context, _is_static: bool) -> PrecompileResult { let input = Input::::new( - input, target_gas, + input, + target_gas_limit(target_gas), ); let gas_cost = Pricer::::cost(&input)?; @@ -127,7 +129,7 @@ where .map_err(|e| PrecompileFailure::Revert { exit_status: ExitRevert::Reverted, output: Into::<&str>::into(e).as_bytes().to_vec(), - cost: target_gas.unwrap_or_default(), + cost: target_gas_limit(target_gas).unwrap_or_default(), })?; Ok(PrecompileOutput { diff --git a/runtime/common/src/precompile/oracle.rs b/runtime/common/src/precompile/oracle.rs index 8248ee9b2..59d27bc1c 100644 --- a/runtime/common/src/precompile/oracle.rs +++ b/runtime/common/src/precompile/oracle.rs @@ -16,23 +16,23 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use crate::{precompile::input::InputPricer, WeightToGas}; +use super::{ + input::{Input, InputPricer, InputT, Output}, + target_gas_limit, + weights::PrecompileWeights, +}; +use crate::WeightToGas; use frame_support::{log, sp_runtime::FixedPointNumber}; use module_evm::{ precompiles::Precompile, runner::state::{PrecompileFailure, PrecompileOutput, PrecompileResult}, Context, ExitError, ExitSucceed, }; +use module_support::{Erc20InfoMapping as Erc20InfoMappingT, PriceProvider as PriceProviderT}; use num_enum::{IntoPrimitive, TryFromPrimitive}; use sp_runtime::{traits::Convert, RuntimeDebug}; use sp_std::{marker::PhantomData, prelude::*}; -use super::{ - input::{Input, InputT, Output}, - weights::PrecompileWeights, -}; -use module_support::{Erc20InfoMapping as Erc20InfoMappingT, PriceProvider as PriceProviderT}; - /// The `Oracle` impl precompile. /// /// @@ -55,7 +55,8 @@ where { fn execute(input: &[u8], target_gas: Option, _context: &Context, _is_static: bool) -> PrecompileResult { let input = Input::::new( - input, target_gas, + input, + target_gas_limit(target_gas), ); let gas_cost = Pricer::::cost(&input)?; @@ -202,7 +203,7 @@ mod tests { assert_noop!( OraclePrecompile::execute( &[0u8; 0], - Some(10), + Some(1000), &Context { address: Default::default(), caller: alice_evm_addr(), @@ -213,14 +214,14 @@ mod tests { PrecompileFailure::Revert { exit_status: ExitRevert::Reverted, output: "invalid input".into(), - cost: 10, + cost: target_gas_limit(Some(1000)).unwrap(), } ); assert_noop!( OraclePrecompile::execute( &[0u8; 3], - Some(10), + Some(1000), &Context { address: Default::default(), caller: alice_evm_addr(), @@ -231,14 +232,14 @@ mod tests { PrecompileFailure::Revert { exit_status: ExitRevert::Reverted, output: "invalid input".into(), - cost: 10, + cost: target_gas_limit(Some(1000)).unwrap(), } ); assert_noop!( OraclePrecompile::execute( &[1u8; 32], - Some(10), + Some(1000), &Context { address: Default::default(), caller: alice_evm_addr(), @@ -249,7 +250,7 @@ mod tests { PrecompileFailure::Revert { exit_status: ExitRevert::Reverted, output: "invalid action".into(), - cost: 10, + cost: target_gas_limit(Some(1000)).unwrap(), } ); }); diff --git a/runtime/common/src/precompile/schedule.rs b/runtime/common/src/precompile/schedule.rs index c9d059222..a40b918fb 100644 --- a/runtime/common/src/precompile/schedule.rs +++ b/runtime/common/src/precompile/schedule.rs @@ -19,6 +19,11 @@ // Disable the following lints #![allow(clippy::type_complexity)] +use super::{ + input::{Input, InputT, Output}, + target_gas_limit, +}; +use codec::{Decode, Encode}; use frame_support::{ dispatch::Dispatchable, ensure, log, parameter_types, @@ -33,16 +38,13 @@ use module_evm::{ Context, ExitError, ExitRevert, ExitSucceed, }; use module_support::{AddressMapping, TransactionPayment}; +use num_enum::{IntoPrimitive, TryFromPrimitive}; +use pallet_scheduler::TaskAddress; use primitives::{Balance, BlockNumber}; use sp_core::H160; use sp_runtime::RuntimeDebug; use sp_std::{fmt::Debug, marker::PhantomData, prelude::*}; -use super::input::{Input, InputT, Output}; -use codec::{Decode, Encode}; -use num_enum::{IntoPrimitive, TryFromPrimitive}; -use pallet_scheduler::TaskAddress; - parameter_types! { pub storage EvmSchedulerNextID: u32 = 0u32; } @@ -106,7 +108,8 @@ where { fn execute(input: &[u8], target_gas: Option, _context: &Context, _is_static: bool) -> PrecompileResult { let input = Input::::new( - input, target_gas, + input, + target_gas_limit(target_gas), ); let gas_cost = Pricer::::cost(&input)?; @@ -163,7 +166,7 @@ where .map_err(|e| PrecompileFailure::Revert { exit_status: ExitRevert::Reverted, output: Into::<&str>::into(e).as_bytes().to_vec(), - cost: target_gas.unwrap_or_default(), + cost: target_gas_limit(target_gas).unwrap_or_default(), })?; } @@ -182,7 +185,7 @@ where let next_id = current_id.checked_add(1).ok_or_else(|| PrecompileFailure::Revert { exit_status: ExitRevert::Reverted, output: "Scheduler next id overflow".into(), - cost: target_gas.unwrap_or_default(), + cost: target_gas_limit(target_gas).unwrap_or_default(), })?; EvmSchedulerNextID::set(&next_id); @@ -217,7 +220,7 @@ where .map_err(|_| PrecompileFailure::Revert { exit_status: ExitRevert::Reverted, output: "Schedule failed".into(), - cost: target_gas.unwrap_or_default(), + cost: target_gas_limit(target_gas).unwrap_or_default(), })?; Ok(PrecompileOutput { @@ -243,14 +246,14 @@ where let task_info = TaskInfo::decode(&mut &task_id[..]).map_err(|_| PrecompileFailure::Revert { exit_status: ExitRevert::Reverted, output: "Decode task_id failed".into(), - cost: target_gas.unwrap_or_default(), + cost: target_gas_limit(target_gas).unwrap_or_default(), })?; ensure!( task_info.sender == from, PrecompileFailure::Revert { exit_status: ExitRevert::Reverted, output: "NoPermission".into(), - cost: target_gas.unwrap_or_default(), + cost: target_gas_limit(target_gas).unwrap_or_default(), } ); @@ -262,7 +265,7 @@ where .map_err(|_| PrecompileFailure::Revert { exit_status: ExitRevert::Reverted, output: "Cancel schedule failed".into(), - cost: target_gas.unwrap_or_default(), + cost: target_gas_limit(target_gas).unwrap_or_default(), })?; #[cfg(not(feature = "with-ethereum-compatibility"))] @@ -300,14 +303,14 @@ where let task_info = TaskInfo::decode(&mut &task_id[..]).map_err(|_| PrecompileFailure::Revert { exit_status: ExitRevert::Reverted, output: "Decode task_id failed".into(), - cost: target_gas.unwrap_or_default(), + cost: target_gas_limit(target_gas).unwrap_or_default(), })?; ensure!( task_info.sender == from, PrecompileFailure::Revert { exit_status: ExitRevert::Reverted, output: "NoPermission".into(), - cost: target_gas.unwrap_or_default(), + cost: target_gas_limit(target_gas).unwrap_or_default(), } ); @@ -319,7 +322,7 @@ where .map_err(|e| PrecompileFailure::Revert { exit_status: ExitRevert::Reverted, output: Into::<&str>::into(e).as_bytes().to_vec(), - cost: target_gas.unwrap_or_default(), + cost: target_gas_limit(target_gas).unwrap_or_default(), })?; Ok(PrecompileOutput { @@ -568,7 +571,7 @@ mod tests { Err(PrecompileFailure::Revert { exit_status: ExitRevert::Reverted, output: "NoPermission".into(), - cost: 10_000, + cost: target_gas_limit(Some(10_000)).unwrap() }) );