diff --git a/pallets/pool-registry/src/benchmarking.rs b/pallets/pool-registry/src/benchmarking.rs index c60bbad1e5..7059dbeacc 100644 --- a/pallets/pool-registry/src/benchmarking.rs +++ b/pallets/pool-registry/src/benchmarking.rs @@ -86,9 +86,7 @@ mod benchmarks { } #[benchmark] - fn update_no_execution( - n: Linear<1, { min(MaxTranches::::get(), 10) }>, - ) -> Result<(), BenchmarkError> { + fn update(n: Linear<1, { min(MaxTranches::::get(), 10) }>) -> Result<(), BenchmarkError> { #[cfg(test)] init_mocks(); @@ -100,35 +98,14 @@ mod benchmarks { ); #[extrinsic_call] - update( - RawOrigin::Signed(whitelisted_caller()), - T::PoolId::default(), - T::ModifyPool::worst_pool_changes(None), - ); - - Ok(()) - } - - /* - #[benchmark] - fn update_and_execute( - n: Linear<1, { min(MaxTranches::::get(), 10) }>, - ) -> Result<(), BenchmarkError> { - #[cfg(test)] - init_mocks(); - - T::ModifyPool::create_heaviest_pool(T::PoolId::default(), whitelisted_caller(), 0.into()); - - #[extrinsic_call] - update( + _( RawOrigin::Signed(whitelisted_caller()), T::PoolId::default(), - T::ModifyPool::changes(Some(n)), + T::ModifyPool::worst_pool_changes(Some(n)), ); Ok(()) } - */ #[benchmark] fn execute_update( @@ -146,6 +123,8 @@ mod benchmarks { assert_eq!(update_state, UpdateState::Stored(n)); + // TODO overpass T::MinUpdateDelay to be able to execute it correctly + #[extrinsic_call] _( RawOrigin::Signed(whitelisted_caller()), @@ -182,238 +161,3 @@ mod benchmarks { crate::mock::Runtime ); } - -/* - -const CURRENCY: u128 = 1_000_000_000_000_000; -const MAX_RESERVE: u128 = 10_000 * CURRENCY; - -const SECS_PER_HOUR: u64 = 60 * 60; -const SECS_PER_DAY: u64 = 24 * SECS_PER_HOUR; -const SECS_PER_YEAR: u64 = 365 * SECS_PER_DAY; - -const TRANCHE: TrancheIndex = 0; -const POOL: u64 = 0; - -const AUSD_CURRENCY_ID: CurrencyId = CurrencyId::ForeignAsset(1); - -benchmarks! { - where_clause { - where - T: Config - + pallet_investments::Config, - T: pallet_pool_system::Config::InterestRate, - MaxTranches = ::MaxTranches>, - T: pallet_pool_fees::Config, - ::PoolFees: PoolFeesBenchmarkHelper< - PoolId = ::PoolId, - PoolFeeInfo = PoolFeeInfo::Balance, ::Rate>, - >, - ::Tokens: Inspect, - <::Lookup as sp_runtime::traits::StaticLookup>::Source: - From<::AccountId>, - ::Permission: Permissions, - ::ModifyPool: PoolMutate< - ::AccountId, - ::PoolId, - TrancheInput = TrancheInput< - ::Rate, - ::StringLimit - >, - PoolChanges = PoolChanges< - ::Rate, - ::StringLimit, - ::MaxTranches - >, - PoolFeeInput = (PoolFeeBucket, <::PoolFees as PoolFeesBenchmarkHelper>::PoolFeeInfo), - >, - Vec<(PoolFeeBucket, PoolFeeInfo<::AccountId, u128, ::InterestRate>)>: FromIterator<(PoolFeeBucket, PoolFeeInfo<::AccountId, u128, ::Rate>)> - } - register { - let n in 1..::MaxTranches::get(); - let m in 0..::MaxPoolFeesPerBucket::get(); - let caller: ::AccountId = create_admin::(0); - let tranches = build_bench_input_tranches::(n); - let pool_fee_input = as PoolFeesBenchmarkHelper>::get_pool_fee_infos(m).into_iter().map(|fee| (PoolFeeBucket::Top, fee)).collect(); - let origin = if ::PoolCreateOrigin::try_origin(RawOrigin::Signed(caller.clone()).into()).is_ok() { - RawOrigin::Signed(caller.clone()) - } else { - RawOrigin::Root - }; - prepare_asset_registry::(); - - #[cfg(test)] - mock::MockWriteOffPolicy::mock_worst_case_policy(|| ()); - - let policy = T::ModifyWriteOffPolicy::worst_case_policy(); - }: register(origin, caller, POOL, tranches.clone(), AUSD_CURRENCY_ID, MAX_RESERVE, None, policy, pool_fee_input) - verify { - let pool = get_pool::(); - assert_input_tranches_match::(pool.tranches.residual_top_slice(), &tranches); - assert_eq!(pool.reserve.available, Zero::zero()); - assert_eq!(pool.reserve.total, Zero::zero()); - assert_eq!(pool.parameters.min_epoch_time, T::DefaultMinEpochTime::get()); - assert_eq!(pool.parameters.max_nav_age, T::DefaultMaxNAVAge::get()); - } - - update_no_execution { - // Execution of updates is blocked as no epoch has passed - // since we submitted the update - let admin: ::AccountId = create_admin::(0); - let n in 1..::MaxTranches::get(); - let m in 0..::MaxPoolFeesPerBucket::get(); - let tranches = build_update_tranches::(n); - prepare_asset_registry::(); - create_pool::(n, m, admin.clone())?; - - - // Submit redemption order so the update isn't executed - let amount = MAX_RESERVE / 2; - let investor = create_investor::(0, TRANCHE, Some(amount))?; - let locator = get_tranche_id::(TRANCHE); - pallet_investments::Pallet::::update_redeem_order(RawOrigin::Signed(investor.clone()).into(), (POOL, locator), amount)?; - - - let changes = PoolChanges { - tranches: Change::NoChange, - min_epoch_time: Change::NewValue(SECS_PER_DAY), - max_nav_age: Change::NewValue(SECS_PER_HOUR), - tranche_metadata: Change::NoChange, - }; - }: update(RawOrigin::Signed(admin), POOL, changes.clone()) - verify { - // Should be the old values - let pool = get_pool::(); - assert_eq!(pool.parameters.min_epoch_time, T::DefaultMinEpochTime::get()); - assert_eq!(pool.parameters.max_nav_age, T::DefaultMaxNAVAge::get()); - - let actual_update = get_scheduled_update::(); - assert_eq!(actual_update.changes, changes); - } - - update_and_execute { - let admin: T::AccountId = create_admin::(0); - let n in 1..::MaxTranches::get(); - let m in 0..::MaxPoolFeesPerBucket::get(); - let tranches = build_update_tranches::(n); - prepare_asset_registry::(); - create_pool::(n, m, admin.clone())?; - - let changes = PoolChanges { - tranches: Change::NewValue(tranches.clone()), - min_epoch_time: Change::NewValue(SECS_PER_DAY), - max_nav_age: Change::NewValue(SECS_PER_HOUR), - tranche_metadata: Change::NewValue(build_update_tranche_token_metadata::()), - }; - }: update(RawOrigin::Signed(admin), POOL, changes) - verify { - // No redemption order was submitted and the MinUpdateDelay is 0 for benchmarks, - // so the update should have been executed immediately. - let pool = get_pool::(); - assert_update_tranches_match::(pool.tranches.residual_top_slice(), &tranches); - assert_eq!(pool.parameters.min_epoch_time, SECS_PER_DAY); - assert_eq!(pool.parameters.max_nav_age, SECS_PER_HOUR); - } - execute_update { - let admin: T::AccountId = create_admin::(0); - let n in 1..::MaxTranches::get(); - let m in 0..::MaxPoolFeesPerBucket::get(); - let tranches = build_update_tranches::(n); - prepare_asset_registry::(); - create_pool::(n, m, admin.clone())?; - - let pool = get_pool::(); - let default_min_epoch_time = pool.parameters.min_epoch_time; - let default_max_nav_age = pool.parameters.max_nav_age; - - let changes = PoolChanges { - tranches: Change::NewValue(build_update_tranches::(n)), - min_epoch_time: Change::NewValue(SECS_PER_DAY), - max_nav_age: Change::NewValue(SECS_PER_HOUR), - tranche_metadata: Change::NewValue(build_update_tranche_token_metadata::()), - }; - - // Invest so we can redeem later - let investor = create_investor::(0, TRANCHE, Some(1))?; - let locator = get_tranche_id::(TRANCHE); - // Submit redemption order so the update isn't immediately executed - pallet_investments::Pallet::::update_redeem_order(RawOrigin::Signed(investor.clone()).into(), (POOL, locator), 1)?; - - update_pool::(changes.clone())?; - - // Withdraw redeem order so the update can be executed after that - pallet_investments::Pallet::::update_redeem_order(RawOrigin::Signed(investor.clone()).into(), (POOL, locator), 0)?; - }: execute_update(RawOrigin::Signed(admin), POOL) - verify { - let pool = get_pool::(); - assert_update_tranches_match::(pool.tranches.residual_top_slice(), &tranches); - assert_eq!(pool.parameters.min_epoch_time, SECS_PER_DAY); - assert_eq!(pool.parameters.max_nav_age, SECS_PER_HOUR); - } - - set_metadata { - let n in 0..::MaxSizeMetadata::get(); - let m in 0..::MaxPoolFeesPerBucket::get(); - let caller: ::AccountId = create_admin::(0); - prepare_asset_registry::(); - create_pool::(2, m, caller.clone())?; - let metadata = vec![0u8; n as usize]; - }: set_metadata(RawOrigin::Signed(caller), POOL, metadata.clone()) - verify { - let metadata: BoundedVec::MaxSizeMetadata> = metadata.try_into().unwrap(); - assert_eq!(Pallet::::get_pool_metadata(POOL).unwrap(), metadata); - } -} - -fn build_update_tranche_token_metadata( -) -> BoundedVec, T::MaxTranches> { - vec![TrancheMetadata { - token_name: BoundedVec::default(), - token_symbol: BoundedVec::default(), - }] - .try_into() - .expect("T::MaxTranches > 0") -} - -fn build_update_tranches( - num_tranches: u32, -) -> BoundedVec, T::MaxTranches> { - let senior_interest_rate = T::InterestRate::saturating_from_rational(5, 100) - / T::InterestRate::saturating_from_integer(SECS_PER_YEAR); - let mut tranches: Vec<_> = (1..num_tranches) - .map(|tranche_id| TrancheUpdate { - tranche_type: TrancheType::NonResidual { - interest_rate_per_sec: senior_interest_rate - / T::InterestRate::saturating_from_integer(tranche_id * 2) - + One::one(), - min_risk_buffer: Perquintill::from_percent((tranche_id * 2).into()), - }, - seniority: None, - }) - .collect(); - tranches.insert( - 0, - TrancheUpdate { - tranche_type: TrancheType::Residual, - seniority: None, - }, - ); - - tranches.try_into().expect("num_tranches <= T::MaxTranches") -} - -// TODO: Enable once ModifyPool is not fully mocked -// impl_benchmark_test_suite!( -// Pallet, -// crate::mock::TestExternalitiesBuilder::default().build(), -// crate::mock::Test, -// ); -*/ diff --git a/pallets/pool-system/src/benchmarking.rs b/pallets/pool-system/src/benchmarking.rs index 4689725e9f..a60ea87e46 100644 --- a/pallets/pool-system/src/benchmarking.rs +++ b/pallets/pool-system/src/benchmarking.rs @@ -13,7 +13,7 @@ //! Module provides benchmarking for Loan Pallet use cfg_primitives::PoolEpochId; -use cfg_traits::{fee::PoolFeesInspect, UpdateState}; +use cfg_traits::fee::PoolFeesInspect; use cfg_types::tokens::{CurrencyId, CustomMetadata}; use frame_benchmarking::{account, benchmarks, impl_benchmark_test_suite}; use frame_support::traits::Currency; @@ -22,7 +22,7 @@ use parity_scale_codec::EncodeLike; use sp_std::vec; use super::*; -use crate::tranches::{TrancheIndex, TrancheInput, TrancheLoc}; +use crate::tranches::{TrancheIndex, TrancheLoc}; const CURRENCY: u128 = 1_000_000_000_000_000; const MAX_RESERVE: u128 = 10_000 * CURRENCY; @@ -301,35 +301,4 @@ where set_liquidity_admin::(caller) } -pub fn update_pool>( - changes: PoolChanges, -) -> Result { - Pallet::::update(POOL, changes) -} - -pub fn get_scheduled_update>( -) -> ScheduledUpdateDetails { - Pallet::::scheduled_update(POOL).unwrap() -} - -pub fn assert_input_tranches_match( - chain: &[TrancheOf], - target: &[TrancheInput], -) { - assert_eq!(chain.len(), target.len()); - for (chain, target) in chain.iter().zip(target.iter()) { - assert_eq!(chain.tranche_type, target.tranche_type); - } -} - -pub fn assert_update_tranches_match( - chain: &[TrancheOf], - target: &[TrancheUpdate], -) { - assert_eq!(chain.len(), target.len()); - for (chain, target) in chain.iter().zip(target.iter()) { - assert_eq!(chain.tranche_type, target.tranche_type); - } -} - impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::Runtime,);