Skip to content

Commit

Permalink
add benchmark to pallet-evm withdraw (polkadot-evm#1046)
Browse files Browse the repository at this point in the history
* add benchmark for withdraw

* fix benchmarking template

* use newly generated weight

* tabs

* tabs

* tabs

* fix build
  • Loading branch information
nbaztec authored and ashutoshvarma committed Jun 8, 2023
1 parent a1d26a4 commit 01751b9
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 5 deletions.
8 changes: 4 additions & 4 deletions benchmarking/frame-weight-template.hbs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// This file is part of Substrate.

// Copyright (C) 2022 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// This file is part of Frontier.
//
// Copyright (c) 2020-2022 Parity Technologies (UK) Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand Down
1 change: 1 addition & 0 deletions frame/ethereum/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ impl pallet_evm::Config for Test {
type OnChargeTransaction = ();
type OnCreate = ();
type FindAuthor = FindAuthorTruncated;
type WeightInfo = ();
}

parameter_types! {
Expand Down
1 change: 1 addition & 0 deletions frame/evm/precompile/dispatch/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ impl pallet_evm::Config for Test {
type OnChargeTransaction = ();
type OnCreate = ();
type FindAuthor = FindAuthorTruncated;
type WeightInfo = ();
}

pub(crate) struct MockHandle {
Expand Down
14 changes: 14 additions & 0 deletions frame/evm/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
use super::*;
use frame_benchmarking::{benchmarks, impl_benchmark_test_suite};

type CurrencyOf<T> = <T as Config>::Currency;

benchmarks! {

runner_execute {
Expand Down Expand Up @@ -126,6 +128,18 @@ benchmarks! {
);
assert!(call_runner_results.is_ok(), "call() failed");
}

withdraw {
let caller = frame_benchmarking::whitelisted_caller::<T::AccountId>();
let from = H160::from_low_u64_le(0);
let from_account_id = T::AddressMapping::into_account_id(from);
CurrencyOf::<T>::make_free_balance_be(&from_account_id, 100_000u32.into());
}: {
// Withdraw should always fail with `EnsureAddressNever` WithdrawOrigin.
let result = Pallet::<T>::withdraw(RawOrigin::Signed(caller.clone()).into(), from, 100_000u32.into());
assert!(result.is_err());
assert_eq!(result.unwrap_err(), sp_runtime::DispatchError::BadOrigin);
}
}

impl_benchmark_test_suite!(Pallet, crate::tests::new_test_ext(), crate::mock::Test);
7 changes: 6 additions & 1 deletion frame/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ mod mock;
pub mod runner;
#[cfg(test)]
mod tests;
pub mod weights;

use frame_support::{
dispatch::{DispatchResultWithPostInfo, Pays, PostDispatchInfo},
Expand Down Expand Up @@ -97,6 +98,7 @@ pub use fp_evm::{
pub use self::{
pallet::*,
runner::{Runner, RunnerError},
weights::WeightInfo,
};

#[frame_support::pallet]
Expand Down Expand Up @@ -156,6 +158,9 @@ pub mod pallet {
/// Find author for the current block.
type FindAuthor: FindAuthor<H160>;

/// Weight information for extrinsics in this pallet.
type WeightInfo: WeightInfo;

/// EVM config used in the module.
fn config() -> &'static EvmConfig {
&LONDON_CONFIG
Expand All @@ -166,7 +171,7 @@ pub mod pallet {
impl<T: Config> Pallet<T> {
/// Withdraw balance from EVM into currency/balances pallet.
#[pallet::call_index(0)]
#[pallet::weight(0)]
#[pallet::weight(<T as pallet::Config>::WeightInfo::withdraw())]
pub fn withdraw(
origin: OriginFor<T>,
address: H160,
Expand Down
1 change: 1 addition & 0 deletions frame/evm/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ impl crate::Config for Test {
type OnChargeTransaction = ();
type OnCreate = ();
type FindAuthor = FindAuthorTruncated;
type WeightInfo = ();
}

/// Exemple PrecompileSet with only Identity precompile.
Expand Down
65 changes: 65 additions & 0 deletions frame/evm/src/weights.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// SPDX-License-Identifier: Apache-2.0
// This file is part of Frontier.
//
// Copyright (c) 2020-2022 Parity Technologies (UK) Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Autogenerated weights for pallet_evm
//!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
//! DATE: 2023-04-25, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! HOSTNAME: `benchmarker`, CPU: `Intel(R) Core(TM) i7-7700K CPU @ 4.20GHz`
//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024
// Executed Command:
// ./target/release/frontier-template-node
// benchmark
// pallet
// --chain=dev
// --steps=50
// --repeat=20
// --pallet=pallet_evm
// --extrinsic=withdraw
// --execution=wasm
// --wasm-execution=compiled
// --output=weights.rs
// --template=./benchmarking/frame-weight-template.hbs

#![cfg_attr(rustfmt, rustfmt_skip)]
#![allow(unused_parens)]
#![allow(unused_imports)]
#![allow(clippy::unnecessary_cast)]

use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}};
use sp_std::marker::PhantomData;

/// Weight functions needed for pallet_evm.
pub trait WeightInfo {
fn withdraw() -> Weight;
}

/// Weights for pallet_evm using the Substrate node and recommended hardware.
pub struct SubstrateWeight<T>(PhantomData<T>);
impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
fn withdraw() -> Weight {
Weight::from_ref_time(4_532_000 as u64)
}
}

// For backwards compatibility and tests
impl WeightInfo for () {
fn withdraw() -> Weight {
Weight::from_ref_time(4_532_000 as u64)
}
}
1 change: 1 addition & 0 deletions template/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ impl pallet_evm::Config for Runtime {
type OnChargeTransaction = ();
type OnCreate = ();
type FindAuthor = FindAuthorTruncated<Aura>;
type WeightInfo = pallet_evm::weights::SubstrateWeight<Runtime>;
}

parameter_types! {
Expand Down

0 comments on commit 01751b9

Please sign in to comment.