From 1a49dd08289caa24e2d6125fc6753ec21d122bce Mon Sep 17 00:00:00 2001 From: William Freudenberger Date: Fri, 22 Sep 2023 14:20:11 +0200 Subject: [PATCH] feat: orderbook bench helper (#1558) * feat: orderbook bench helper * feat: add bench_fill_order_full * refactor: bench_create_pool * refactor: orderbook bench helper * feat: add InvestmentIdBenchmarkHelper * fix: mock_bench_default_investment_id * fix: order book bench --- libs/mocks/src/pools.rs | 33 ++++-- libs/traits/src/benchmarking.rs | 60 ++++++++++ libs/traits/src/lib.rs | 22 ++-- pallets/loans/src/benchmarking.rs | 11 +- pallets/loans/src/tests/mock.rs | 3 +- pallets/order-book/src/benchmarking.rs | 154 +++++++++++-------------- pallets/order-book/src/lib.rs | 57 +++++++++ pallets/pool-system/src/impls.rs | 88 ++++++++------ pallets/pool-system/src/tests/mod.rs | 4 +- 9 files changed, 282 insertions(+), 150 deletions(-) create mode 100644 libs/traits/src/benchmarking.rs diff --git a/libs/mocks/src/pools.rs b/libs/mocks/src/pools.rs index 48c7b13580..5660251a24 100644 --- a/libs/mocks/src/pools.rs +++ b/libs/mocks/src/pools.rs @@ -23,6 +23,7 @@ pub mod pallet { type Balance; type BalanceRatio; type CurrencyId; + type TrancheCurrency; } #[pallet::pallet] @@ -66,12 +67,20 @@ pub mod pallet { register_call!(move |(a, b, c)| f(a, b, c)); } - pub fn mock_benchmark_create_pool(f: impl Fn(T::PoolId, &T::AccountId) + 'static) { + pub fn mock_bench_create_pool(f: impl Fn(T::PoolId, &T::AccountId) + 'static) { register_call!(move |(a, b)| f(a, b)); } - pub fn mock_benchmark_give_ausd(f: impl Fn(&T::AccountId, T::Balance) + 'static) { - register_call!(move |(a, b)| f(a, b)); + pub fn mock_bench_investor_setup( + f: impl Fn(T::PoolId, T::AccountId, T::Balance) + 'static, + ) { + register_call!(move |(a, b, c)| f(a, b, c)); + } + + pub fn mock_bench_default_investment_id( + f: impl Fn(T::PoolId) -> T::TrancheCurrency + 'static + 'static, + ) { + register_call!(f); } } @@ -124,17 +133,27 @@ pub mod pallet { } #[cfg(feature = "runtime-benchmarks")] - impl cfg_traits::PoolBenchmarkHelper for Pallet { + impl cfg_traits::benchmarking::PoolBenchmarkHelper for Pallet { type AccountId = T::AccountId; type Balance = T::Balance; type PoolId = T::PoolId; - fn benchmark_create_pool(a: Self::PoolId, b: &Self::AccountId) { + fn bench_create_pool(a: Self::PoolId, b: &Self::AccountId) { execute_call!((a, b)) } - fn benchmark_give_ausd(a: &Self::AccountId, b: Self::Balance) { - execute_call!((a, b)) + fn bench_investor_setup(a: Self::PoolId, b: Self::AccountId, c: Self::Balance) { + execute_call!((a, b, c)) + } + } + + #[cfg(feature = "runtime-benchmarks")] + impl cfg_traits::benchmarking::InvestmentIdBenchmarkHelper for Pallet { + type InvestmentId = T::TrancheCurrency; + type PoolId = T::PoolId; + + fn bench_default_investment_id(a: Self::PoolId) -> Self::InvestmentId { + execute_call!(a) } } } diff --git a/libs/traits/src/benchmarking.rs b/libs/traits/src/benchmarking.rs new file mode 100644 index 0000000000..cffc831b2b --- /dev/null +++ b/libs/traits/src/benchmarking.rs @@ -0,0 +1,60 @@ +// Copyright 2023 Centrifuge Foundation (centrifuge.io). +// +// This file is part of the Centrifuge chain project. +// Centrifuge is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version (see http://www.gnu.org/licenses). +// Centrifuge is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +/// Benchmark utility to create pools +pub trait PoolBenchmarkHelper { + type PoolId; + type AccountId; + type Balance; + + /// Create a pool for the given the pool id and the admin. + fn bench_create_pool(pool_id: Self::PoolId, admin: &Self::AccountId); + + /// Prepare user to be able to invest, i.e. fund with pool currency and give + /// permissions. + fn bench_investor_setup( + pool_id: Self::PoolId, + account: Self::AccountId, + balance: Self::Balance, + ); +} + +/// Benchmark utility to expose investment identifiers +pub trait InvestmentIdBenchmarkHelper { + type PoolId; + type InvestmentId; + + /// Return the default investment id for the given pool. + fn bench_default_investment_id(pool_id: Self::PoolId) -> Self::InvestmentId; +} + +/// Benchmark utility for adding currency trading pairs +pub trait OrderBookBenchmarkHelper { + type AccountId; + type Balance; + type CurrencyId; + type OrderIdNonce; + + /// Adds the corresponding trading pair, creates trader accounts and mints + /// appropriate amounts of balance into these + fn bench_setup_trading_pair( + asset_in: Self::CurrencyId, + asset_out: Self::CurrencyId, + amount_in: Self::Balance, + amount_out: Self::Balance, + decimals_in: u32, + decimals_out: u32, + ) -> (Self::AccountId, Self::AccountId); + + /// Fulfills the given swap order from the trader account + fn bench_fill_order_full(trader: Self::AccountId, order_id: Self::OrderIdNonce); +} diff --git a/libs/traits/src/lib.rs b/libs/traits/src/lib.rs index fdcc122bfb..d6e04c5b85 100644 --- a/libs/traits/src/lib.rs +++ b/libs/traits/src/lib.rs @@ -49,6 +49,10 @@ pub mod liquidity_pools; /// Traits related to rewards. pub mod rewards; +#[cfg(feature = "runtime-benchmarks")] +/// Traits related to benchmarking tooling. +pub mod benchmarking; + /// A trait used for loosely coupling the claim pallet with a reward mechanism. /// /// ## Overview @@ -128,8 +132,10 @@ pub trait PoolInspect { type TrancheId; type Moment; - /// check if the pool exists + /// Check if the pool exists fn pool_exists(pool_id: Self::PoolId) -> bool; + + /// Check if the tranche exists for the given pool fn tranche_exists(pool_id: Self::PoolId, tranche_id: Self::TrancheId) -> bool; /// Get the account used for the given `pool_id`. @@ -257,20 +263,6 @@ pub trait PoolWriteOffPolicyMutate { fn worst_case_policy() -> Self::Policy; } -/// Utility to benchmark pools easily -#[cfg(feature = "runtime-benchmarks")] -pub trait PoolBenchmarkHelper { - type PoolId; - type AccountId; - type Balance; - - /// Create a benchmark pool giving the id and the admin. - fn benchmark_create_pool(pool_id: Self::PoolId, admin: &Self::AccountId); - - /// Give AUSD to the account - fn benchmark_give_ausd(account: &Self::AccountId, balance: Self::Balance); -} - /// A trait that can be used to retrieve the current price for a currency pub struct CurrencyPair { pub base: CurrencyId, diff --git a/pallets/loans/src/benchmarking.rs b/pallets/loans/src/benchmarking.rs index 5821e54fb3..f131546121 100644 --- a/pallets/loans/src/benchmarking.rs +++ b/pallets/loans/src/benchmarking.rs @@ -13,10 +13,11 @@ use cfg_primitives::CFG; use cfg_traits::{ + benchmarking::PoolBenchmarkHelper, changes::ChangeGuard, data::DataRegistry, interest::{CompoundingSchedule, InterestAccrual, InterestRate}, - Permissions, PoolBenchmarkHelper, PoolWriteOffPolicyMutate, + Permissions, PoolWriteOffPolicyMutate, }; use cfg_types::{ adjustments::Adjustment, @@ -77,8 +78,8 @@ fn config_mocks() { MockPools::mock_account_for(|_| 0); MockPools::mock_withdraw(|_, _, _| Ok(())); MockPools::mock_deposit(|_, _, _| Ok(())); - MockPools::mock_benchmark_create_pool(|_, _| {}); - MockPools::mock_benchmark_give_ausd(|_, _| {}); + MockPools::mock_bench_create_pool(|_, _| {}); + MockPools::mock_bench_investor_setup(|_, _, _| {}); MockPrices::mock_feed_value(|_, _, _| Ok(())); MockPrices::mock_register_id(|_, _| Ok(())); MockPrices::mock_collection(|_| MockDataCollection::new(|_| Ok(Default::default()))); @@ -107,7 +108,7 @@ where let pool_id = Default::default(); let pool_admin = account("pool_admin", 0, 0); - T::Pool::benchmark_create_pool(pool_id, &pool_admin); + T::Pool::bench_create_pool(pool_id, &pool_admin); let loan_admin = account("loan_admin", 0, 0); T::Permissions::add( @@ -118,7 +119,7 @@ where .unwrap(); let borrower = account::("borrower", 0, 0); - T::Pool::benchmark_give_ausd(&borrower, (FUNDS * CFG).into()); + T::Pool::bench_investor_setup(pool_id, borrower.clone(), (FUNDS * CFG).into()); T::NonFungible::create_collection(&COLLECION_ID.into(), &borrower, &borrower).unwrap(); T::Permissions::add( PermissionScope::Pool(pool_id), diff --git a/pallets/loans/src/tests/mock.rs b/pallets/loans/src/tests/mock.rs index 90173b4346..23ecdc634b 100644 --- a/pallets/loans/src/tests/mock.rs +++ b/pallets/loans/src/tests/mock.rs @@ -17,7 +17,7 @@ use cfg_mocks::{ pallet_mock_change_guard, pallet_mock_data, pallet_mock_permissions, pallet_mock_pools, }; use cfg_primitives::Moment; -use cfg_types::permissions::PermissionScope; +use cfg_types::{permissions::PermissionScope, tokens::TrancheCurrency}; use codec::{Decode, Encode, MaxEncodedLen}; use frame_support::traits::{ tokens::nonfungibles::{Create, Mutate}, @@ -199,6 +199,7 @@ impl pallet_mock_pools::Config for Runtime { type BalanceRatio = Quantity; type CurrencyId = CurrencyId; type PoolId = PoolId; + type TrancheCurrency = TrancheCurrency; type TrancheId = TrancheId; } diff --git a/pallets/order-book/src/benchmarking.rs b/pallets/order-book/src/benchmarking.rs index 5469981204..fc088372c7 100644 --- a/pallets/order-book/src/benchmarking.rs +++ b/pallets/order-book/src/benchmarking.rs @@ -12,18 +12,22 @@ #![cfg(feature = "runtime-benchmarks")] +use cfg_traits::benchmarking::OrderBookBenchmarkHelper; use cfg_types::tokens::{CurrencyId, CustomMetadata}; use frame_benchmarking::*; -use frame_support::traits::fungibles::Mutate as FungiblesMutate; use frame_system::RawOrigin; use orml_traits::asset_registry::{Inspect, Mutate}; use sp_runtime::FixedPointNumber; -// use pallet_pool_system::benchmarking::prepare_asset_registry; use super::*; -const CURRENCY_0: u128 = 1_000_000; -const CURRENCY_1: u128 = 1_000_000_000_000; +const AMOUNT_IN: u128 = 1_000_000; +const AMOUNT_OUT: u128 = 1_000_000_000_000; +const BUY_AMOUNT: u128 = 100 * AMOUNT_IN; +const ASSET_IN: CurrencyId = CurrencyId::ForeignAsset(1); +const ASSET_OUT: CurrencyId = CurrencyId::ForeignAsset(2); +const DECIMALS_IN: u32 = 12; +const DECIMALS_OUT: u32 = 6; benchmarks! { where_clause { @@ -33,117 +37,99 @@ benchmarks! { } create_order { - let (account_0, _, asset_0, asset_1) = set_up_users_currencies::()?; - }:create_order(RawOrigin::Signed(account_0.clone()), asset_0, asset_1, 100 * CURRENCY_0, T::SellRatio::saturating_from_integer(2)) + let (account_out, _) = Pallet::::bench_setup_trading_pair(ASSET_IN, ASSET_OUT, 1000 * AMOUNT_IN, 1000 * AMOUNT_OUT, DECIMALS_IN, DECIMALS_OUT); + }:create_order(RawOrigin::Signed(account_out.clone()), ASSET_IN, ASSET_OUT, BUY_AMOUNT, T::SellRatio::saturating_from_integer(2)) user_update_order { - let (account_0, _, asset_0, asset_1) = set_up_users_currencies::()?; + let (account_out, _) = Pallet::::bench_setup_trading_pair(ASSET_IN, ASSET_OUT, 1000 * AMOUNT_IN, 1000 * AMOUNT_OUT, DECIMALS_IN, DECIMALS_OUT); - let order_id = Pallet::::place_order(account_0.clone(), asset_0, asset_1, 100 * CURRENCY_0, T::SellRatio::saturating_from_integer(2).into(), 100 * CURRENCY_0)?; + let order_id = Pallet::::place_order(account_out.clone(), ASSET_IN, ASSET_OUT, BUY_AMOUNT, T::SellRatio::saturating_from_integer(2).into(), BUY_AMOUNT)?; - }:user_update_order(RawOrigin::Signed(account_0.clone()), order_id, 150 * CURRENCY_0, T::SellRatio::saturating_from_integer(1)) + }:user_update_order(RawOrigin::Signed(account_out.clone()), order_id, 10 * BUY_AMOUNT, T::SellRatio::saturating_from_integer(1)) user_cancel_order { - let (account_0, _, asset_0, asset_1) = set_up_users_currencies::()?; + let (account_out, _) = Pallet::::bench_setup_trading_pair(ASSET_IN, ASSET_OUT, 1000 * AMOUNT_IN, 1000 * AMOUNT_OUT, DECIMALS_IN, DECIMALS_OUT); - let order_id = Pallet::::place_order(account_0.clone(), asset_0, asset_1, 100 * CURRENCY_0, T::SellRatio::saturating_from_integer(2).into(), 100 * CURRENCY_0)?; + let order_id = Pallet::::place_order(account_out.clone(), ASSET_IN, ASSET_OUT, BUY_AMOUNT, T::SellRatio::saturating_from_integer(2).into(), BUY_AMOUNT)?; - }:user_cancel_order(RawOrigin::Signed(account_0.clone()), order_id) + }:user_cancel_order(RawOrigin::Signed(account_out.clone()), order_id) fill_order_full { - let (account_0, account_1, asset_0, asset_1) = set_up_users_currencies::()?; + let (account_out, account_in) = Pallet::::bench_setup_trading_pair(ASSET_IN, ASSET_OUT, 1000 * AMOUNT_IN, 1000 * AMOUNT_OUT, DECIMALS_IN, DECIMALS_OUT); - let order_id = Pallet::::place_order(account_0.clone(), asset_0, asset_1, 100 * CURRENCY_0, T::SellRatio::saturating_from_integer(2).into(), 100 * CURRENCY_0)?; + let order_id = Pallet::::place_order(account_out.clone(), ASSET_IN, ASSET_OUT, BUY_AMOUNT, T::SellRatio::saturating_from_integer(2).into(), BUY_AMOUNT)?; - }:fill_order_full(RawOrigin::Signed(account_1.clone()), order_id) + }:fill_order_full(RawOrigin::Signed(account_in.clone()), order_id) fill_order_partial { - let (account_0, account_1, asset_0, asset_1) = set_up_users_currencies::()?; + let (account_out, account_in) = Pallet::::bench_setup_trading_pair(ASSET_IN, ASSET_OUT, 1000 * AMOUNT_IN, 1000 * AMOUNT_OUT, DECIMALS_IN, DECIMALS_OUT); - let order_id = Pallet::::place_order(account_0.clone(), asset_0, asset_1, 100 * CURRENCY_0, T::SellRatio::saturating_from_integer(2).into(), 10 * CURRENCY_0)?; + let order_id = Pallet::::place_order(account_out.clone(), ASSET_IN, ASSET_OUT, BUY_AMOUNT, T::SellRatio::saturating_from_integer(2).into(), BUY_AMOUNT / 10)?; - }:fill_order_partial(RawOrigin::Signed(account_1.clone()), order_id, 40 * CURRENCY_0) + }:fill_order_partial(RawOrigin::Signed(account_in.clone()), order_id, BUY_AMOUNT / 2) add_trading_pair { - let asset_0 = CurrencyId::ForeignAsset(1); - let asset_1 = CurrencyId::ForeignAsset(2); - }:add_trading_pair(RawOrigin::Root, asset_0, asset_1, 100 * CURRENCY_0) + }:add_trading_pair(RawOrigin::Root, ASSET_IN, ASSET_OUT, BUY_AMOUNT) rm_trading_pair { - let (account_0, _, asset_0, asset_1) = set_up_users_currencies::()?; - }:rm_trading_pair(RawOrigin::Root, asset_0, asset_1) + let (account_out, _) = Pallet::::bench_setup_trading_pair(ASSET_IN, ASSET_OUT, 1000 * AMOUNT_IN, 1000 * AMOUNT_OUT, DECIMALS_IN, DECIMALS_OUT); + }:rm_trading_pair(RawOrigin::Root, ASSET_IN, ASSET_OUT) update_min_order { - let (account_0, _, asset_0, asset_1) = set_up_users_currencies::()?; - }:update_min_order(RawOrigin::Root, asset_0, asset_1, 1 * CURRENCY_0) - - + let (account_out, _) = Pallet::::bench_setup_trading_pair(ASSET_IN, ASSET_OUT, 1000 * AMOUNT_IN, 1000 * AMOUNT_OUT, DECIMALS_IN, DECIMALS_OUT); + }:update_min_order(RawOrigin::Root, ASSET_IN, ASSET_OUT, AMOUNT_IN) } -fn set_up_users_currencies>() -> Result< - ( - T::AccountId, - T::AccountId, - T::AssetCurrencyId, - T::AssetCurrencyId, - ), - &'static str, -> -where - ::AssetRegistry: orml_traits::asset_registry::Mutate, -{ - let account_0: T::AccountId = account::("Account0", 1, 0); - let account_1: T::AccountId = account::("Account1", 2, 0); - let asset_0 = CurrencyId::ForeignAsset(1); - let asset_1 = CurrencyId::ForeignAsset(2); - prepare_asset_registry::(); - T::TradeableAsset::mint_into(asset_0, &account_0, 1_000 * CURRENCY_0)?; - T::TradeableAsset::mint_into(asset_1, &account_0, 1_000 * CURRENCY_1)?; - T::TradeableAsset::mint_into(asset_0, &account_1, 1_000 * CURRENCY_0)?; - T::TradeableAsset::mint_into(asset_1, &account_1, 1_000 * CURRENCY_1)?; - TradingPair::::insert(asset_0, asset_1, 1 * CURRENCY_0); - Ok((account_0, account_1, asset_0, asset_1)) -} impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::Runtime,); -pub fn prepare_asset_registry() +pub(crate) struct Helper(sp_std::marker::PhantomData); + +impl Helper where - T::AssetRegistry: Mutate, + T::AssetRegistry: + Mutate, { - match T::AssetRegistry::metadata(&CurrencyId::ForeignAsset(1)) { - Some(_) => (), - None => { - T::AssetRegistry::register_asset( - Some(CurrencyId::ForeignAsset(1)), - orml_asset_registry::AssetMetadata { - decimals: 12, - name: "MOCK TOKEN".as_bytes().to_vec(), - symbol: "MOCK".as_bytes().to_vec(), - existential_deposit: 0, - location: None, - additional: CustomMetadata::default(), - }, - ) - .expect("Registering Pool asset must work"); + pub fn register_trading_assets( + asset_in: T::AssetCurrencyId, + asset_out: T::AssetCurrencyId, + decimals_in: u32, + decimals_out: u32, + ) { + match T::AssetRegistry::metadata(&asset_in) { + Some(_) => (), + None => { + T::AssetRegistry::register_asset( + Some(asset_in), + orml_asset_registry::AssetMetadata { + decimals: decimals_in, + name: "ASSET IN".as_bytes().to_vec(), + symbol: "INC".as_bytes().to_vec(), + existential_deposit: T::Balance::zero(), + location: None, + additional: CustomMetadata::default(), + }, + ) + .expect("Registering Pool asset must work"); + } } - } - match T::AssetRegistry::metadata(&CurrencyId::ForeignAsset(2)) { - Some(_) => (), - None => { - T::AssetRegistry::register_asset( - Some(CurrencyId::ForeignAsset(2)), - orml_asset_registry::AssetMetadata { - decimals: 6, - name: "MOCK TOKEN 1".as_bytes().to_vec(), - symbol: "MOCK1".as_bytes().to_vec(), - existential_deposit: 0, - location: None, - additional: CustomMetadata::default(), - }, - ) - .expect("Registering Pool asset must work"); + match T::AssetRegistry::metadata(&asset_out) { + Some(_) => (), + None => { + T::AssetRegistry::register_asset( + Some(asset_out), + orml_asset_registry::AssetMetadata { + decimals: decimals_out, + name: "ASSET OUT".as_bytes().to_vec(), + symbol: "OUT".as_bytes().to_vec(), + existential_deposit: T::Balance::zero(), + location: None, + additional: CustomMetadata::default(), + }, + ) + .expect("Registering Pool asset must work"); + } } } } diff --git a/pallets/order-book/src/lib.rs b/pallets/order-book/src/lib.rs index b268e85155..30ec3ae549 100644 --- a/pallets/order-book/src/lib.rs +++ b/pallets/order-book/src/lib.rs @@ -982,4 +982,61 @@ pub mod pallet { TradingPair::::get(currency_in, currency_out).is_ok() } } + + #[cfg(feature = "runtime-benchmarks")] + impl cfg_traits::benchmarking::OrderBookBenchmarkHelper for Pallet + where + T::AssetRegistry: orml_traits::asset_registry::Mutate< + AssetId = T::AssetCurrencyId, + Balance = T::Balance, + CustomMetadata = CustomMetadata, + >, + { + type AccountId = T::AccountId; + type Balance = T::Balance; + type CurrencyId = T::AssetCurrencyId; + type OrderIdNonce = T::OrderIdNonce; + + fn bench_setup_trading_pair( + asset_in: Self::CurrencyId, + asset_out: Self::CurrencyId, + amount_in: Self::Balance, + amount_out: Self::Balance, + decimals_in: u32, + decimals_out: u32, + ) -> (Self::AccountId, Self::AccountId) { + let account_out: Self::AccountId = + frame_benchmarking::account::("account_out", 1, 0); + let account_in: Self::AccountId = + frame_benchmarking::account::("account_in", 2, 0); + crate::benchmarking::Helper::::register_trading_assets( + asset_in.into(), + asset_out.into(), + decimals_in, + decimals_out, + ); + + frame_support::assert_ok!(T::TradeableAsset::mint_into( + asset_out, + &account_out, + amount_out + )); + frame_support::assert_ok!(T::TradeableAsset::mint_into( + asset_in, + &account_in, + amount_in, + )); + + TradingPair::::insert(asset_in, asset_out, Self::Balance::one()); + + (account_out, account_in) + } + + fn bench_fill_order_full(trader: Self::AccountId, order_id: Self::OrderIdNonce) { + frame_support::assert_ok!(Self::fill_order_full( + frame_system::RawOrigin::Signed(trader.clone()).into(), + order_id + )); + } + } } diff --git a/pallets/pool-system/src/impls.rs b/pallets/pool-system/src/impls.rs index 413cc37d9b..25587d8fa0 100644 --- a/pallets/pool-system/src/impls.rs +++ b/pallets/pool-system/src/impls.rs @@ -444,7 +444,10 @@ impl ChangeGuard for Pallet { #[cfg(feature = "runtime-benchmarks")] mod benchmarks_utils { - use cfg_traits::{investments::Investment, PoolBenchmarkHelper}; + use cfg_traits::{ + benchmarking::{InvestmentIdBenchmarkHelper, PoolBenchmarkHelper}, + investments::Investment, + }; use cfg_types::{ pools::TrancheMetadata, tokens::{CurrencyId, CustomMetadata}, @@ -456,7 +459,7 @@ mod benchmarks_utils { use super::*; - const AUSD_CURRENCY_ID: CurrencyId = CurrencyId::ForeignAsset(1); + const POOL_CURRENCY: CurrencyId = CurrencyId::ForeignAsset(1); impl> PoolBenchmarkHelper for Pallet where @@ -467,16 +470,16 @@ mod benchmarks_utils { type Balance = T::Balance; type PoolId = T::PoolId; - fn benchmark_create_pool(pool_id: T::PoolId, admin: &T::AccountId) { + fn bench_create_pool(pool_id: T::PoolId, admin: &T::AccountId) { const FUNDS: u32 = u32::max_value(); - if T::AssetRegistry::metadata(&AUSD_CURRENCY_ID).is_none() { - T::AssetRegistry::register_asset( - Some(AUSD_CURRENCY_ID), + if T::AssetRegistry::metadata(&POOL_CURRENCY).is_none() { + frame_support::assert_ok!(T::AssetRegistry::register_asset( + Some(POOL_CURRENCY), orml_asset_registry::AssetMetadata { decimals: 12, - name: "MOCK AUSD".as_bytes().to_vec(), - symbol: "MOCKAUSD".as_bytes().to_vec(), + name: "MOCK POOL CURRENCY".as_bytes().to_vec(), + symbol: "MOCKPCUR".as_bytes().to_vec(), existential_deposit: Zero::zero(), location: None, additional: CustomMetadata { @@ -484,13 +487,12 @@ mod benchmarks_utils { ..CustomMetadata::default() }, }, - ) - .unwrap(); + )); } - T::Currency::make_free_balance_be(admin, T::PoolDeposit::get()); // Pool creation - Pallet::::create( + T::Currency::make_free_balance_be(admin, T::PoolDeposit::get()); + frame_support::assert_ok!(Pallet::::create( admin.clone(), admin.clone(), pool_id, @@ -515,33 +517,21 @@ mod benchmarks_utils { }, }, ], - AUSD_CURRENCY_ID, + POOL_CURRENCY, FUNDS.into(), - ) - .unwrap(); + )); // Investment in pool let investor = account::("investor_benchmark_pool", 0, 0); - let tranche = Pallet::::pool(pool_id) - .unwrap() - .tranches - .tranche_id(TrancheLoc::Index(0)) - .unwrap(); - - T::Permission::add( - PermissionScope::Pool(pool_id), - investor.clone(), - Role::PoolRole(PoolRole::TrancheInvestor(tranche, u64::MAX)), - ) - .unwrap(); - - T::Tokens::mint_into(AUSD_CURRENCY_ID, &investor, FUNDS.into()).unwrap(); - T::Investments::update_investment( + Self::bench_investor_setup(pool_id, investor.clone(), FUNDS.into()); + let tranche = + ::bench_default_investment_id(pool_id) + .of_tranche(); + frame_support::assert_ok!(T::Investments::update_investment( &investor, T::TrancheCurrency::generate(pool_id.into(), tranche), FUNDS.into(), - ) - .unwrap(); + )); // Close epoch Pool::::mutate(pool_id, |pool| { @@ -550,12 +540,38 @@ mod benchmarks_utils { pool.parameters.max_nav_age = 999_999_999_999; }); - Pallet::::close_epoch(RawOrigin::Signed(admin.clone()).into(), pool_id).unwrap(); + frame_support::assert_ok!(Pallet::::close_epoch( + RawOrigin::Signed(admin.clone()).into(), + pool_id + )); } - fn benchmark_give_ausd(account: &T::AccountId, balance: T::Balance) { - T::Tokens::mint_into(AUSD_CURRENCY_ID, account, balance).unwrap(); - T::Currency::make_free_balance_be(account, balance); + fn bench_investor_setup(pool_id: T::PoolId, account: T::AccountId, balance: T::Balance) { + T::Tokens::mint_into(POOL_CURRENCY, &account, balance).unwrap(); + T::Currency::make_free_balance_be(&account, balance); + + let tranche = + ::bench_default_investment_id(pool_id) + .of_tranche(); + frame_support::assert_ok!(T::Permission::add( + PermissionScope::Pool(pool_id), + account, + Role::PoolRole(PoolRole::TrancheInvestor(tranche, u64::MAX)), + )); + } + } + + impl> InvestmentIdBenchmarkHelper for Pallet { + type InvestmentId = T::TrancheCurrency; + type PoolId = T::PoolId; + + fn bench_default_investment_id(pool_id: Self::PoolId) -> T::TrancheCurrency { + let tranche_id = Pallet::::pool(pool_id) + .expect("Pool should exist") + .tranches + .tranche_id(TrancheLoc::Index(0)) + .expect("Tranche at index 0 should exist"); + T::TrancheCurrency::generate(pool_id, tranche_id) } } } diff --git a/pallets/pool-system/src/tests/mod.rs b/pallets/pool-system/src/tests/mod.rs index f9f6bf3941..54e82bf2cf 100644 --- a/pallets/pool-system/src/tests/mod.rs +++ b/pallets/pool-system/src/tests/mod.rs @@ -2662,9 +2662,9 @@ mod changes { #[test] #[cfg(feature = "runtime-benchmarks")] fn benchmark_pool() { - use cfg_traits::PoolBenchmarkHelper; + use cfg_traits::benchmarking::PoolBenchmarkHelper; new_test_ext().execute_with(|| { - PoolSystem::benchmark_create_pool(0, &0); + PoolSystem::bench_create_pool(0, &0); }); }