Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrated benchmarking of token-issuer from v1 to v2. #1577

Merged
merged 3 commits into from
Jan 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
200 changes: 151 additions & 49 deletions pallets/token-issuer/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,90 +18,192 @@

#![cfg(feature = "runtime-benchmarks")]
use bifrost_primitives::{CurrencyId, TokenSymbol};
use frame_benchmarking::{
account, benchmarks, impl_benchmark_test_suite, v1::BenchmarkError, whitelisted_caller,
use frame_benchmarking::v2::*;
use frame_support::{
assert_ok, sp_runtime::traits::UniqueSaturatedFrom, traits::UnfilteredDispatchable,
};
use frame_support::{sp_runtime::traits::UniqueSaturatedFrom, traits::UnfilteredDispatchable};
use frame_system::RawOrigin;

use super::*;
#[allow(unused_imports)]
use crate::Pallet as TokenIssuer;

benchmarks! {
add_to_issue_whitelist {
let origin = T::ControlOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;
#[benchmarks(where T: Config)]
mod benchmarks {
use super::*;

#[benchmark]
fn add_to_issue_whitelist() -> Result<(), BenchmarkError> {
let origin =
T::ControlOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;
let account: T::AccountId = whitelisted_caller();
let currency_id = CurrencyId::Token(TokenSymbol::KSM);
let call = Call::<T>::add_to_issue_whitelist { currency_id, account };
}: {call.dispatch_bypass_filter(origin)?}
let call = Call::<T>::add_to_issue_whitelist {
currency_id,
account,
};

#[block]
{
call.dispatch_bypass_filter(origin)?;
}

remove_from_issue_whitelist {
let origin = T::ControlOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;
Ok(())
}

#[benchmark]
fn remove_from_issue_whitelist() -> Result<(), BenchmarkError> {
let origin =
T::ControlOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;
let account: T::AccountId = whitelisted_caller();
let currency_id = CurrencyId::Token(TokenSymbol::KSM);
let add_call = Call::<T>::add_to_issue_whitelist { currency_id, account: account.clone() };

let add_call = Call::<T>::add_to_issue_whitelist {
currency_id,
account: account.clone(),
};
add_call.dispatch_bypass_filter(origin.clone())?;

let remove_call = Call::<T>::remove_from_issue_whitelist { currency_id, account };
}: {remove_call.dispatch_bypass_filter(origin)?}
let remove_call = Call::<T>::remove_from_issue_whitelist {
currency_id,
account,
};

add_to_transfer_whitelist {
let origin = T::ControlOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;
#[block]
{
remove_call.dispatch_bypass_filter(origin)?;
}

Ok(())
}

#[benchmark]
fn add_to_transfer_whitelist() -> Result<(), BenchmarkError> {
let origin =
T::ControlOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;
let account: T::AccountId = whitelisted_caller();
let currency_id = CurrencyId::Token(TokenSymbol::KSM);
let call = Call::<T>::add_to_transfer_whitelist { currency_id, account };
}: {call.dispatch_bypass_filter(origin)?}
let call = Call::<T>::add_to_transfer_whitelist {
currency_id,
account,
};

remove_from_transfer_whitelist {
let origin = T::ControlOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;
#[block]
{
call.dispatch_bypass_filter(origin)?;
}

Ok(())
}

#[benchmark]
fn remove_from_transfer_whitelist() -> Result<(), BenchmarkError> {
let origin =
T::ControlOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;
let account: T::AccountId = whitelisted_caller();
let currency_id = CurrencyId::Token(TokenSymbol::KSM);
let add_call = Call::<T>::add_to_transfer_whitelist { currency_id, account: account.clone() };

let add_call = Call::<T>::add_to_transfer_whitelist {
currency_id,
account: account.clone(),
};
add_call.dispatch_bypass_filter(origin.clone())?;

let remove_call = Call::<T>::remove_from_transfer_whitelist { currency_id, account };
}: {remove_call.dispatch_bypass_filter(origin)?}
let remove_call = Call::<T>::remove_from_transfer_whitelist {
currency_id,
account,
};

#[block]
{
remove_call.dispatch_bypass_filter(origin)?;
}

Ok(())
}

issue {
let origin = T::ControlOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;
#[benchmark]
fn issue() -> Result<(), BenchmarkError> {
let origin =
T::ControlOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;
let caller: T::AccountId = whitelisted_caller();
let currency_id = CurrencyId::Token(TokenSymbol::KSM);
let add_call = Call::<T>::add_to_issue_whitelist { currency_id: currency_id, account: caller.clone() };

let add_call = Call::<T>::add_to_issue_whitelist {
currency_id,
account: caller.clone(),
};
add_call.dispatch_bypass_filter(origin.clone())?;

let original_balance = T::MultiCurrency::free_balance(currency_id, &caller);
let token_amount = BalanceOf::<T>::unique_saturated_from(1000u32 as u128);
}: _(RawOrigin::Signed(caller.clone()), caller.clone(), currency_id, token_amount)
verify {
assert_eq!(T::MultiCurrency::free_balance(currency_id, &caller), token_amount + original_balance);
let token_amount = BalanceOf::<T>::unique_saturated_from(1000000000000000u128);

#[block]
{
Pallet::<T>::issue(
RawOrigin::Signed(caller.clone()).into(),
caller.clone(),
currency_id,
token_amount,
)?;
}

assert_eq!(
T::MultiCurrency::free_balance(currency_id, &caller),
token_amount + original_balance
);

Ok(())
}

transfer {
let origin = T::ControlOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;
#[benchmark]
fn transfer() -> Result<(), BenchmarkError> {
let origin =
T::ControlOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;
let caller: T::AccountId = whitelisted_caller();
let currency_id = CurrencyId::Token(TokenSymbol::KSM);

// add caller to the transfer whitelist
let add_transfer_call = Call::<T>::add_to_transfer_whitelist { currency_id: currency_id, account: caller.clone() };
let initial_amount = BalanceOf::<T>::unique_saturated_from(1_000_000_000_000_000u128);
assert_ok!(T::MultiCurrency::deposit(
currency_id,
&caller,
initial_amount
));

let add_transfer_call = Call::<T>::add_to_transfer_whitelist {
currency_id,
account: caller.clone(),
};
add_transfer_call.dispatch_bypass_filter(origin.clone())?;

// transfer some ksm from caller account to receiver account
let receiver: T::AccountId = account("bechmarking_account_1", 0, 0);
let transfer_token_amount = BalanceOf::<T>::unique_saturated_from(800u32 as u128);
let transfer_token_amount = BalanceOf::<T>::unique_saturated_from(800_000_000_000_000u128);
let caller_original_balance = T::MultiCurrency::free_balance(currency_id, &caller);
let receiver_original_balance = T::MultiCurrency::free_balance(currency_id, &receiver);
}: _(RawOrigin::Signed(caller.clone()), receiver.clone(), currency_id, transfer_token_amount)
verify {
assert_eq!(T::MultiCurrency::free_balance(currency_id, &caller), caller_original_balance - transfer_token_amount);
assert_eq!(T::MultiCurrency::free_balance(currency_id, &receiver), transfer_token_amount+ receiver_original_balance);

#[block]
{
Pallet::<T>::transfer(
RawOrigin::Signed(caller.clone()).into(),
receiver.clone(),
currency_id,
transfer_token_amount,
)?;
}

assert_eq!(
T::MultiCurrency::free_balance(currency_id, &caller),
caller_original_balance - transfer_token_amount
);
assert_eq!(
T::MultiCurrency::free_balance(currency_id, &receiver),
transfer_token_amount + receiver_original_balance
);

Ok(())
}
}

impl_benchmark_test_suite!(
TokenIssuer,
crate::mock::ExtBuilder::default()
.one_hundred_precision_for_each_currency_type_for_whitelist_account()
.build(),
crate::mock::Runtime
);
impl_benchmark_test_suite!(
Pallet,
crate::mock::new_test_ext_benchmark(),
crate::mock::Runtime
);
}
5 changes: 5 additions & 0 deletions pallets/token-issuer/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,8 @@ impl ExtBuilder {
t.into()
}
}

#[cfg(feature = "runtime-benchmarks")]
pub fn new_test_ext_benchmark() -> sp_io::TestExternalities {
ExtBuilder::default().build()
}
88 changes: 45 additions & 43 deletions pallets/token-issuer/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@

//! Autogenerated weights for bifrost_token_issuer
//!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
//! DATE: 2023-09-14, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! HOSTNAME: `bifrost-jenkins`, CPU: `Intel(R) Xeon(R) CPU E5-26xx v4`
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 42.0.1
//! DATE: 2024-12-30, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! HOSTNAME: `mjl-legion`, CPU: `12th Gen Intel(R) Core(TM) i9-12900H`
//! WASM-EXECUTION: Compiled, CHAIN: Some("bifrost-kusama-local"), DB CACHE: 1024

// Executed Command:
Expand Down Expand Up @@ -63,80 +63,82 @@ pub trait WeightInfo {

// For backwards compatibility and tests
impl WeightInfo for () {
/// Storage: TokenIssuer IssueWhiteList (r:1 w:1)
/// Proof Skipped: TokenIssuer IssueWhiteList (max_values: None, max_size: None, mode: Measured)
/// Storage: `TokenIssuer::IssueWhiteList` (r:1 w:1)
/// Proof: `TokenIssuer::IssueWhiteList` (`max_values`: None, `max_size`: None, mode: `Measured`)
fn add_to_issue_whitelist() -> Weight {
// Proof Size summary in bytes:
// Measured: `142`
// Estimated: `3607`
// Minimum execution time: 32_693_000 picoseconds.
Weight::from_parts(33_651_000, 3607)
// Minimum execution time: 6_203_000 picoseconds.
Weight::from_parts(6_858_000, 3607)
.saturating_add(RocksDbWeight::get().reads(1_u64))
.saturating_add(RocksDbWeight::get().writes(1_u64))
}
/// Storage: TokenIssuer IssueWhiteList (r:1 w:1)
/// Proof Skipped: TokenIssuer IssueWhiteList (max_values: None, max_size: None, mode: Measured)
/// Storage: `TokenIssuer::IssueWhiteList` (r:1 w:1)
/// Proof: `TokenIssuer::IssueWhiteList` (`max_values`: None, `max_size`: None, mode: `Measured`)
fn remove_from_issue_whitelist() -> Weight {
// Proof Size summary in bytes:
// Measured: `220`
// Estimated: `3685`
// Minimum execution time: 34_136_000 picoseconds.
Weight::from_parts(35_196_000, 3685)
// Minimum execution time: 6_672_000 picoseconds.
Weight::from_parts(7_119_000, 3685)
.saturating_add(RocksDbWeight::get().reads(1_u64))
.saturating_add(RocksDbWeight::get().writes(1_u64))
}
/// Storage: TokenIssuer TransferWhiteList (r:1 w:1)
/// Proof Skipped: TokenIssuer TransferWhiteList (max_values: None, max_size: None, mode: Measured)
/// Storage: `TokenIssuer::TransferWhiteList` (r:1 w:1)
/// Proof: `TokenIssuer::TransferWhiteList` (`max_values`: None, `max_size`: None, mode: `Measured`)
fn add_to_transfer_whitelist() -> Weight {
// Proof Size summary in bytes:
// Measured: `142`
// Estimated: `3607`
// Minimum execution time: 32_485_000 picoseconds.
Weight::from_parts(33_735_000, 3607)
// Minimum execution time: 6_326_000 picoseconds.
Weight::from_parts(6_852_000, 3607)
.saturating_add(RocksDbWeight::get().reads(1_u64))
.saturating_add(RocksDbWeight::get().writes(1_u64))
}
/// Storage: TokenIssuer TransferWhiteList (r:1 w:1)
/// Proof Skipped: TokenIssuer TransferWhiteList (max_values: None, max_size: None, mode: Measured)
/// Storage: `TokenIssuer::TransferWhiteList` (r:1 w:1)
/// Proof: `TokenIssuer::TransferWhiteList` (`max_values`: None, `max_size`: None, mode: `Measured`)
fn remove_from_transfer_whitelist() -> Weight {
// Proof Size summary in bytes:
// Measured: `220`
// Estimated: `3685`
// Minimum execution time: 33_573_000 picoseconds.
Weight::from_parts(34_609_000, 3685)
// Minimum execution time: 7_016_000 picoseconds.
Weight::from_parts(7_358_000, 3685)
.saturating_add(RocksDbWeight::get().reads(1_u64))
.saturating_add(RocksDbWeight::get().writes(1_u64))
}
/// Storage: TokenIssuer IssueWhiteList (r:1 w:0)
/// Proof Skipped: TokenIssuer IssueWhiteList (max_values: None, max_size: None, mode: Measured)
/// Storage: Tokens Accounts (r:1 w:1)
/// Proof: Tokens Accounts (max_values: None, max_size: Some(118), added: 2593, mode: MaxEncodedLen)
/// Storage: AssetRegistry CurrencyMetadatas (r:1 w:0)
/// Proof Skipped: AssetRegistry CurrencyMetadatas (max_values: None, max_size: None, mode: Measured)
/// Storage: Tokens TotalIssuance (r:1 w:1)
/// Proof: Tokens TotalIssuance (max_values: None, max_size: Some(38), added: 2513, mode: MaxEncodedLen)
/// Storage: `TokenIssuer::IssueWhiteList` (r:1 w:0)
/// Proof: `TokenIssuer::IssueWhiteList` (`max_values`: None, `max_size`: None, mode: `Measured`)
/// Storage: `Tokens::Accounts` (r:1 w:1)
/// Proof: `Tokens::Accounts` (`max_values`: None, `max_size`: Some(118), added: 2593, mode: `MaxEncodedLen`)
/// Storage: `AssetRegistry::CurrencyMetadatas` (r:1 w:0)
/// Proof: `AssetRegistry::CurrencyMetadatas` (`max_values`: None, `max_size`: None, mode: `Measured`)
/// Storage: `Tokens::TotalIssuance` (r:1 w:1)
/// Proof: `Tokens::TotalIssuance` (`max_values`: None, `max_size`: Some(38), added: 2513, mode: `MaxEncodedLen`)
fn issue() -> Weight {
// Proof Size summary in bytes:
// Measured: `1870`
// Estimated: `5335`
// Minimum execution time: 99_796_000 picoseconds.
Weight::from_parts(100_707_000, 5335)
// Measured: `679`
// Estimated: `4144`
// Minimum execution time: 21_440_000 picoseconds.
Weight::from_parts(23_100_000, 4144)
.saturating_add(RocksDbWeight::get().reads(4_u64))
.saturating_add(RocksDbWeight::get().writes(2_u64))
}
/// Storage: TokenIssuer TransferWhiteList (r:1 w:0)
/// Proof Skipped: TokenIssuer TransferWhiteList (max_values: None, max_size: None, mode: Measured)
/// Storage: Tokens Accounts (r:2 w:2)
/// Proof: Tokens Accounts (max_values: None, max_size: Some(118), added: 2593, mode: MaxEncodedLen)
/// Storage: AssetRegistry CurrencyMetadatas (r:1 w:0)
/// Proof Skipped: AssetRegistry CurrencyMetadatas (max_values: None, max_size: None, mode: Measured)
/// Storage: `TokenIssuer::TransferWhiteList` (r:1 w:0)
/// Proof: `TokenIssuer::TransferWhiteList` (`max_values`: None, `max_size`: None, mode: `Measured`)
/// Storage: `Tokens::Accounts` (r:2 w:2)
/// Proof: `Tokens::Accounts` (`max_values`: None, `max_size`: Some(118), added: 2593, mode: `MaxEncodedLen`)
/// Storage: `AssetRegistry::CurrencyMetadatas` (r:1 w:0)
/// Proof: `AssetRegistry::CurrencyMetadatas` (`max_values`: None, `max_size`: None, mode: `Measured`)
/// Storage: `System::Account` (r:1 w:1)
/// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`)
fn transfer() -> Weight {
// Proof Size summary in bytes:
// Measured: `1989`
// Measured: `950`
// Estimated: `6176`
// Minimum execution time: 114_723_000 picoseconds.
Weight::from_parts(115_788_000, 6176)
.saturating_add(RocksDbWeight::get().reads(4_u64))
.saturating_add(RocksDbWeight::get().writes(2_u64))
// Minimum execution time: 32_395_000 picoseconds.
Weight::from_parts(33_244_000, 6176)
.saturating_add(RocksDbWeight::get().reads(5_u64))
.saturating_add(RocksDbWeight::get().writes(3_u64))
}
}
}