Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Commit

Permalink
Benchmarks for dot-, erc20- and eth-app pallets (paritytech#317)
Browse files Browse the repository at this point in the history
* Benchmarks for dot-app pallet

* Benchmarks for erc20-app pallet

* Benchmarks for eth-app pallet

* Generate WeightInfo for dot-app, erc20-app, eth-app
  • Loading branch information
Rizziepit authored Mar 17, 2021
1 parent 985f3c7 commit 92af4bf
Show file tree
Hide file tree
Showing 31 changed files with 596 additions and 10 deletions.
3 changes: 3 additions & 0 deletions parachain/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions parachain/pallets/assets/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use frame_system::RawOrigin;
use frame_benchmarking::{account, benchmarks, whitelisted_caller, impl_benchmark_test_suite};
use sp_core::H160;

#[allow(unused_imports)]
use crate::Module as Assets;

fn set_balance<T: Config>(asset_id: &AssetId, who: &T::AccountId, amount: &U256) {
Expand Down
2 changes: 1 addition & 1 deletion parachain/pallets/dispatch/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ where

#[cfg(feature = "runtime-benchmarks")]
fn successful_origin() -> OuterOrigin {
OuterOrigin::from(Origin(Default::default()))
OuterOrigin::from(Origin(H160::repeat_byte(2)))
}
}

Expand Down
6 changes: 6 additions & 0 deletions parachain/pallets/dot-app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ hex = { package = "rustc-hex", version = "2.1.0", default-features = false }
rlp = { version = "0.5", default-features = false }
hex-literal = { version = "0.3.1", default-features = false }

frame-benchmarking = { git = "https://github.com/paritytech/substrate.git", branch = "rococo-v1", default-features = false, optional = true }
frame-support = { git = "https://github.com/paritytech/substrate.git", branch = "rococo-v1", default-features = false }
frame-system = { git = "https://github.com/paritytech/substrate.git", branch = "rococo-v1", default-features = false }
sp-core = { git = "https://github.com/paritytech/substrate.git", branch = "rococo-v1", default-features = false }
Expand Down Expand Up @@ -50,3 +51,8 @@ std = [
"ethabi/std",
"rlp/std"
]
runtime-benchmarks = [
"frame-benchmarking",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
]
99 changes: 99 additions & 0 deletions parachain/pallets/dot-app/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
//! DotApp pallet benchmarking
#![cfg(feature = "runtime-benchmarks")]

use super::*;

use frame_system::RawOrigin;
use frame_support::traits::UnfilteredDispatchable;
use frame_benchmarking::{account, benchmarks, whitelisted_caller, impl_benchmark_test_suite};
use sp_core::H160;
use sp_runtime::traits::Zero;

use crate::Module as DotApp;

benchmarks! {
// Benchmark `lock` extrinsic under worst case conditions:
// * The amount is successfully locked
// * The sender account is killed
// * The channel executes incentivization logic
lock {
let existential_deposit = T::Currency::minimum_balance();
let caller: T::AccountId = whitelisted_caller();
let lock_account = DotApp::<T>::account_id();
let recipient = H160::zero();

let balance = existential_deposit * 10u32.into();
// The amount is chosen such that balance - amount < existential_deposit
// so that the account is reaped
let amount = existential_deposit * 9u32.into() + 1u32.into();

T::Currency::make_free_balance_be(&caller, balance);

}: _(RawOrigin::Signed(caller.clone()), ChannelId::Incentivized, recipient, amount)
verify {
assert!(!balance.is_zero() && !amount.is_zero());
assert_eq!(T::Currency::free_balance(&caller), Zero::zero());
assert_eq!(T::Currency::free_balance(&lock_account), amount);
}

// Benchmark `lock` extrinsic for the average case:
// * The amount is successfully locked
// * The sender remains alive
// * The channel executes incentivization logic
#[extra]
lock_sender_alive {
let existential_deposit = T::Currency::minimum_balance();
let caller: T::AccountId = whitelisted_caller();
let lock_account = DotApp::<T>::account_id();
let recipient = H160::zero();

let balance = existential_deposit * 10u32.into();
let amount = existential_deposit * 8u32.into();

T::Currency::make_free_balance_be(&caller, balance);

}: lock(RawOrigin::Signed(caller.clone()), ChannelId::Incentivized, recipient, amount)
verify {
assert!(!balance.is_zero() && !amount.is_zero());
assert_eq!(T::Currency::free_balance(&caller), balance - amount);
assert_eq!(T::Currency::free_balance(&lock_account), amount);
}

// Benchmark `unlock` extrinsic under worst case conditions:
// * The amount is successfully unlocked
unlock {
let origin = T::CallOrigin::successful_origin();
if let Ok(caller) = T::CallOrigin::try_origin(origin.clone()) {
Address::put(caller);
} else {
return Err("Failed to extract caller address from origin");
}

let existential_deposit = T::Currency::minimum_balance();
let lock_account = DotApp::<T>::account_id();
let recipient: T::AccountId = account("recipient", 0, 0);
let recipient_lookup: <T::Lookup as StaticLookup>::Source = T::Lookup::unlookup(recipient.clone());
let sender = H160::zero();

let balance = existential_deposit * 10u32.into();
let amount = existential_deposit * 8u32.into();
let amount_wrapped = wrap::<T>(amount, T::Decimals::get()).unwrap();

T::Currency::make_free_balance_be(&lock_account, balance);

let call = Call::<T>::unlock(sender, recipient_lookup, amount_wrapped);

}: { call.dispatch_bypass_filter(origin)? }
verify {
assert!(!balance.is_zero() && !amount.is_zero());
assert_eq!(T::Currency::free_balance(&lock_account), balance - amount);
assert_eq!(T::Currency::free_balance(&recipient), amount);
}
}

impl_benchmark_test_suite!(
DotApp,
crate::mock::new_tester(),
crate::mock::Test,
);
21 changes: 18 additions & 3 deletions parachain/pallets/dot-app/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ use frame_support::{
EnsureOrigin,
Currency,
ExistenceRequirement::{KeepAlive, AllowDeath},
}
},
weights::Weight,
};
use sp_std::{
prelude::*,
Expand All @@ -26,6 +27,7 @@ use primitives::{wrap, unwrap};

use payload::OutboundPayload;

mod benchmarking;
mod payload;
mod primitives;

Expand All @@ -35,6 +37,17 @@ mod mock;
#[cfg(test)]
mod tests;

/// Weight functions needed for this pallet.
pub trait WeightInfo {
fn lock() -> Weight;
fn unlock() -> Weight;
}

impl WeightInfo for () {
fn lock() -> Weight { 0 }
fn unlock() -> Weight { 0 }
}

type BalanceOf<T> = <<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;

pub trait Config: system::Config {
Expand All @@ -49,6 +62,8 @@ pub trait Config: system::Config {
type ModuleId: Get<ModuleId>;

type Decimals: Get<u32>;
/// Weight information for extrinsics in this pallet
type WeightInfo: WeightInfo;
}

decl_storage! {
Expand Down Expand Up @@ -93,7 +108,7 @@ decl_module! {
});
}

#[weight = 0]
#[weight = T::WeightInfo::lock()]
#[transactional]
pub fn lock(origin, channel_id: ChannelId, recipient: H160, amount: BalanceOf<T>) -> DispatchResult {
let who = ensure_signed(origin)?;
Expand All @@ -116,7 +131,7 @@ decl_module! {
Ok(())
}

#[weight = 0]
#[weight = T::WeightInfo::unlock()]
#[transactional]
pub fn unlock(origin, sender: H160, recipient: <T::Lookup as StaticLookup>::Source, amount: U256) -> DispatchResult {
let who = T::CallOrigin::ensure_origin(origin)?;
Expand Down
1 change: 1 addition & 0 deletions parachain/pallets/dot-app/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ impl dot_app::Config for Test {
type CallOrigin = artemis_dispatch::EnsureEthereumAccount;
type ModuleId = DotModuleId;
type Decimals = Decimals;
type WeightInfo = ();
}

pub fn new_tester() -> sp_io::TestExternalities {
Expand Down
6 changes: 6 additions & 0 deletions parachain/pallets/erc20-app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ hex = { package = "rustc-hex", version = "2.1.0", default-features = false }
rlp = { version = "0.5", default-features = false }
hex-literal = { version = "0.3.1", default-features = false }

frame-benchmarking = { git = "https://github.com/paritytech/substrate.git", branch = "rococo-v1", default-features = false, optional = true }
frame-support = { git = "https://github.com/paritytech/substrate.git", branch = "rococo-v1", default-features = false }
frame-system = { git = "https://github.com/paritytech/substrate.git", branch = "rococo-v1", default-features = false }
sp-core = { git = "https://github.com/paritytech/substrate.git", branch = "rococo-v1", default-features = false }
Expand Down Expand Up @@ -48,3 +49,8 @@ std = [
"ethabi/std",
"rlp/std"
]
runtime-benchmarks = [
"frame-benchmarking",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
]
60 changes: 60 additions & 0 deletions parachain/pallets/erc20-app/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//! ERC20App pallet benchmarking
#![cfg(feature = "runtime-benchmarks")]

use super::*;

use frame_system::RawOrigin;
use frame_support::traits::UnfilteredDispatchable;
use frame_benchmarking::{account, benchmarks, whitelisted_caller, impl_benchmark_test_suite};
use sp_core::H160;

#[allow(unused_imports)]
use crate::Module as ERC20App;

benchmarks! {
// Benchmark `burn` extrinsic under worst case conditions:
// * `burn` successfully substracts amount from caller account
// * The channel executes incentivization logic
burn {
let caller: T::AccountId = whitelisted_caller();
let token = H160::repeat_byte(1);
let recipient = H160::repeat_byte(2);
let amount: U256 = 500.into();

T::Assets::deposit(AssetId::Token(token), &caller, amount)?;

}: _(RawOrigin::Signed(caller.clone()), ChannelId::Incentivized, token, recipient, amount)
verify {
assert_eq!(T::Assets::balance(AssetId::Token(token), &caller), U256::zero());
}

// Benchmark `mint` extrinsic under worst case conditions:
// * `mint` successfully adds amount to recipient account
mint {
let origin = T::CallOrigin::successful_origin();
if let Ok(caller) = T::CallOrigin::try_origin(origin.clone()) {
Address::put(caller);
} else {
return Err("Failed to extract caller address from origin");
}

let token = H160::repeat_byte(1);
let recipient: T::AccountId = account("recipient", 0, 0);
let recipient_lookup: <T::Lookup as StaticLookup>::Source = T::Lookup::unlookup(recipient.clone());
let sender = H160::zero();
let amount: U256 = 500.into();

let call = Call::<T>::mint(token, sender, recipient_lookup, amount);

}: { call.dispatch_bypass_filter(origin)? }
verify {
assert_eq!(T::Assets::balance(AssetId::Token(token), &recipient), amount);
}
}

impl_benchmark_test_suite!(
ERC20App,
crate::mock::new_tester(),
crate::mock::Test,
);
20 changes: 18 additions & 2 deletions parachain/pallets/erc20-app/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use frame_support::{
dispatch::{DispatchError, DispatchResult},
traits::EnsureOrigin,
transactional,
weights::Weight,
};
use sp_runtime::traits::StaticLookup;
use sp_std::prelude::*;
Expand All @@ -32,12 +33,25 @@ use artemis_core::{ChannelId, OutboundRouter, AssetId, MultiAsset};
mod payload;
use payload::OutboundPayload;

mod benchmarking;

#[cfg(test)]
mod mock;

#[cfg(test)]
mod tests;

/// Weight functions needed for this pallet.
pub trait WeightInfo {
fn burn() -> Weight;
fn mint() -> Weight;
}

impl WeightInfo for () {
fn burn() -> Weight { 0 }
fn mint() -> Weight { 0 }
}

pub trait Config: system::Config {
type Event: From<Event<Self>> + Into<<Self as system::Config>::Event>;

Expand All @@ -46,6 +60,8 @@ pub trait Config: system::Config {
type OutboundRouter: OutboundRouter<Self::AccountId>;

type CallOrigin: EnsureOrigin<Self::Origin, Success=H160>;

type WeightInfo: WeightInfo;
}

decl_storage! {
Expand Down Expand Up @@ -82,7 +98,7 @@ decl_module! {
fn deposit_event() = default;

/// Burn an ERC20 token balance
#[weight = 0]
#[weight = T::WeightInfo::burn()]
#[transactional]
pub fn burn(origin, channel_id: ChannelId, token: H160, recipient: H160, amount: U256) -> DispatchResult {
let who = ensure_signed(origin)?;
Expand All @@ -102,7 +118,7 @@ decl_module! {
Ok(())
}

#[weight = 0]
#[weight = T::WeightInfo::mint()]
#[transactional]
pub fn mint(origin, token: H160, sender: H160, recipient: <T::Lookup as StaticLookup>::Source, amount: U256) -> DispatchResult {
let who = T::CallOrigin::ensure_origin(origin)?;
Expand Down
1 change: 1 addition & 0 deletions parachain/pallets/erc20-app/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ impl erc20_app::Config for Test {
type Assets = Assets;
type OutboundRouter = MockOutboundRouter<Self::AccountId>;
type CallOrigin = artemis_dispatch::EnsureEthereumAccount;
type WeightInfo = ();
}

pub fn new_tester() -> sp_io::TestExternalities {
Expand Down
6 changes: 6 additions & 0 deletions parachain/pallets/eth-app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ hex = { package = "rustc-hex", version = "2.1.0", default-features = false }
rlp = { version = "0.5", default-features = false }
hex-literal = { version = "0.3.1", default-features = false }

frame-benchmarking = { git = "https://github.com/paritytech/substrate.git", branch = "rococo-v1", default-features = false, optional = true }
frame-support = { git = "https://github.com/paritytech/substrate.git", branch = "rococo-v1", default-features = false }
frame-system = { git = "https://github.com/paritytech/substrate.git", branch = "rococo-v1", default-features = false }
sp-core = { git = "https://github.com/paritytech/substrate.git", branch = "rococo-v1", default-features = false }
Expand Down Expand Up @@ -48,3 +49,8 @@ std = [
"ethabi/std",
"rlp/std"
]
runtime-benchmarks = [
"frame-benchmarking",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
]
Loading

0 comments on commit 92af4bf

Please sign in to comment.