From d4785ea31402d9a7f17d9d71197e5d9c7a95fb13 Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Tue, 3 Dec 2019 18:04:17 +0100 Subject: [PATCH] pallet-evm: weight calculation for call/create based on gas provided (#4261) * pallet-evm: weight calculation for call/create based on gas provided * Update frame/evm/src/lib.rs Co-Authored-By: Niklas Adolfsson --- substrate/frame/evm/src/lib.rs | 41 ++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/substrate/frame/evm/src/lib.rs b/substrate/frame/evm/src/lib.rs index 13be83dccf..c02739885e 100644 --- a/substrate/frame/evm/src/lib.rs +++ b/substrate/frame/evm/src/lib.rs @@ -23,13 +23,14 @@ mod backend; pub use crate::backend::{Account, Log, Vicinity, Backend}; -use rstd::vec::Vec; +use rstd::{vec::Vec, marker::PhantomData}; use support::{dispatch::Result, decl_module, decl_storage, decl_event}; +use support::weights::{Weight, WeighData, ClassifyDispatch, DispatchClass, PaysFee}; use support::traits::{Currency, WithdrawReason, ExistenceRequirement}; use system::ensure_signed; use sp_runtime::ModuleId; use support::weights::SimpleDispatchInfo; -use sp_runtime::traits::{UniqueSaturatedInto, AccountIdConversion}; +use sp_runtime::traits::{UniqueSaturatedInto, AccountIdConversion, SaturatedConversion}; use primitives::{U256, H256, H160}; use evm::{ExitReason, ExitSucceed, ExitError}; use evm::executor::StackExecutor; @@ -82,6 +83,38 @@ impl Precompiles for () { } } +struct WeightForCallCreate(PhantomData); + +impl Default for WeightForCallCreate { + fn default() -> Self { + Self(PhantomData) + } +} + +impl WeighData<(&H160, &Vec, &U256, &u32)> for WeightForCallCreate { + fn weigh_data(&self, (_, _, _, gas_provided): (&H160, &Vec, &U256, &u32)) -> Weight { + F::gas_price().saturated_into::().saturating_mul(*gas_provided) + } +} + +impl WeighData<(&Vec, &U256, &u32)> for WeightForCallCreate { + fn weigh_data(&self, (_, _, gas_provided): (&Vec, &U256, &u32)) -> Weight { + F::gas_price().saturated_into::().saturating_mul(*gas_provided) + } +} + +impl ClassifyDispatch for WeightForCallCreate { + fn classify_dispatch(&self, _: T) -> DispatchClass { + DispatchClass::Normal + } +} + +impl PaysFee for WeightForCallCreate { + fn pays_fee(&self) -> bool { + true + } +} + /// EVM module trait pub trait Trait: system::Trait + timestamp::Trait { /// Calculator for current gas price. @@ -161,7 +194,7 @@ decl_module! { Ok(()) } - #[weight = SimpleDispatchInfo::FixedNormal(10_000)] + #[weight = WeightForCallCreate::::default()] fn call(origin, target: H160, input: Vec, value: U256, gas_limit: u32) -> Result { let sender = ensure_signed(origin)?; let source = T::ConvertAccountId::convert_account_id(&sender); @@ -212,7 +245,7 @@ decl_module! { ret } - #[weight = SimpleDispatchInfo::FixedNormal(10_000)] + #[weight = WeightForCallCreate::::default()] fn create(origin, init: Vec, value: U256, gas_limit: u32) -> Result { let sender = ensure_signed(origin)?; let source = T::ConvertAccountId::convert_account_id(&sender);