Skip to content

Commit

Permalink
feat: add benchmarking
Browse files Browse the repository at this point in the history
  • Loading branch information
chungquantin committed Aug 2, 2024
1 parent 955547b commit e042435
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 4 deletions.
69 changes: 65 additions & 4 deletions pallets/api/src/fungibles/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,42 @@
//! Benchmarking setup for pallet-api::fungibles
#![cfg(feature = "runtime-benchmarks")]

use super::{AccountIdOf, AssetIdOf, AssetsInstanceOf, AssetsOf, BalanceOf, Call, Config, Pallet};
use codec::Encode;
use frame_benchmarking::{account, v2::*};
use frame_support::{
assert_ok,
traits::{
fungibles::{
approvals::{Inspect as ApprovalInspect, Mutate},
Create, Inspect,
approvals::{Inspect as ApprovalInspect, Mutate as ApprovalMutate},
Create, Inspect, Mutate,
},
Currency,
},
};
use frame_system::RawOrigin;
use sp_core::crypto::FromEntropy;

Check failure on line 19 in pallets/api/src/fungibles/benchmarking.rs

View workflow job for this annotation

GitHub Actions / clippy

failed to resolve: use of undeclared crate or module `sp_core`

error[E0433]: failed to resolve: use of undeclared crate or module `sp_core` --> pallets/api/src/fungibles/benchmarking.rs:19:5 | 19 | use sp_core::crypto::FromEntropy; | ^^^^^^^ use of undeclared crate or module `sp_core`
use sp_runtime::traits::Zero;

const SEED: u32 = 1;

/// Trait describing factory functions for dispatchables' parameters.
pub trait ArgumentsFactory<AssetKind> {
/// Factory function for an asset kind.
fn create_asset_kind(seed: u32) -> AssetKind;
}

/// Implementation that expects the parameters implement the [`FromEntropy`] trait.
impl<AssetKind> ArgumentsFactory<AssetKind> for ()
where
AssetKind: FromEntropy,
{
fn create_asset_kind(seed: u32) -> AssetKind {
AssetKind::from_entropy(&mut seed.encode().as_slice()).unwrap()
}
}

// See if `generic_event` has been emitted.
fn assert_has_event<T: Config>(
generic_event: <T as pallet_assets::Config<AssetsInstanceOf<T>>>::RuntimeEvent,
Expand All @@ -31,12 +51,53 @@ fn assert_has_event<T: Config>(
mod benchmarks {
use super::*;

// Parameter:
// - 'a': wethere `transfer` accepts native token fungible or asset.
#[benchmark]
fn transfer(a: Linear<0, 1>) -> Result<(), BenchmarkError> {
let from: AccountIdOf<T> = account("Alice", 0, SEED);
let to: AccountIdOf<T> = account("Bob", 0, SEED);
let min_balance = <BalanceOf<T>>::from(1u32);
let asset_id = AssetIdOf::<T>::zero();
let asset_kind = <T as Config>::BenchmarkHelper::create_asset_kind(a);
let amount = <BalanceOf<T>>::from(u32::MAX / 2);
// Initiate the native balance for the `from` account.
T::Currency::make_free_balance_be(&from, u32::MAX.into());
if a == 1 {
assert_ok!(<AssetsOf<T> as Create<AccountIdOf<T>>>::create(
asset_id.clone(),
from.clone(),
true,
min_balance
));
assert!(<AssetsOf<T> as Mutate<AccountIdOf<T>>>::mint_into(
asset_id.clone(),
&from,
<BalanceOf<T>>::from(u32::MAX),
)
.is_ok());
};

#[extrinsic_call]
_(RawOrigin::Signed(from.clone()), asset_kind, to.clone(), amount);
if a == 1 {
assert_eq!(
<AssetsOf<T> as Inspect<AccountIdOf<T>>>::total_balance(asset_id, &to),
<BalanceOf<T>>::from(u32::MAX / 2)
);
} else {
assert_eq!(T::Currency::free_balance(&to), (u32::MAX / 2).into());
}
Ok(())
}

// Parameter:
// - 'a': whether `approve_transfer` is required.
// - 'c': whether `cancel_approval` is required.
#[benchmark]
fn approve(a: Linear<0, 1>, c: Linear<0, 1>) -> Result<(), BenchmarkError> {
let asset_id = AssetIdOf::<T>::zero();
let asset_kind = <T as Config>::BenchmarkHelper::create_asset_kind(SEED);
let min_balance = <BalanceOf<T>>::from(1u32);
let owner: AccountIdOf<T> = account("Alice", 0, SEED);
let spender: AccountIdOf<T> = account("Bob", 0, SEED);
Expand All @@ -49,7 +110,7 @@ mod benchmarks {
true,
min_balance
));
assert_ok!(<AssetsOf<T> as Mutate<AccountIdOf<T>>>::approve(
assert_ok!(<AssetsOf<T> as ApprovalMutate<AccountIdOf<T>>>::approve(
asset_id.clone(),
&owner,
&spender,
Expand All @@ -68,7 +129,7 @@ mod benchmarks {
};

#[extrinsic_call]
_(RawOrigin::Signed(owner.clone()), asset_id.clone(), spender.clone(), approval_value);
_(RawOrigin::Signed(owner.clone()), asset_kind, spender.clone(), approval_value);

assert_eq!(AssetsOf::<T>::allowance(asset_id.clone(), &owner, &spender), approval_value);
if c == 1 {
Expand Down
7 changes: 7 additions & 0 deletions pallets/api/src/fungibles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ mod benchmarking;
mod tests;
pub mod weights;

#[cfg(feature = "runtime-benchmarks")]
pub use benchmarking::ArgumentsFactory;

use frame_support::traits::{
fungible::{Inspect as NativeInspect, Mutate as NativeMutate},
fungibles::{
Expand Down Expand Up @@ -112,6 +115,10 @@ pub mod pallet {

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

/// Helper type for benchmarks.
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper: ArgumentsFactory<Self::AssetKind>;
}

#[pallet::pallet]
Expand Down

0 comments on commit e042435

Please sign in to comment.