diff --git a/pallets/loans-ref/src/benchmarking.rs b/pallets/loans-ref/src/benchmarking.rs index e04fbe1b57..2f8baf8f7b 100644 --- a/pallets/loans-ref/src/benchmarking.rs +++ b/pallets/loans-ref/src/benchmarking.rs @@ -224,12 +224,14 @@ where fn initialize_active_state(n: u32) -> PoolIdOf { let pool_id = Self::prepare_benchmark(); + // TODO: ideally should this be computed by a parameter? for i in 1..MaxRateCountOf::::get() { // First `i` (i=0) used by the loan's interest rate. let rate = T::Rate::saturating_from_rational(i + 1, 5000); T::InterestAccrual::reference_rate(rate).unwrap(); } + // TODO: ideally should this be computed by a parameter? for i in 0..MaxCollectionSizeOf::::get() { let price_id = i.into(); // This account is different in each iteration because of how oracles works. @@ -248,15 +250,6 @@ where pool_id } - - fn max_benchmark_loans() -> u32 { - // Maximum loans used for benchmaks. - // This can be smaller than `MaxActiveLoansPerPool`, - // to avoid calculate weights during hours if the loan numbers is too - // large. It should be large enough to be able to represent a consistent weight - // function for any number of loans. - T::MaxActiveLoansPerPool::get().min(50) - } } benchmarks! { @@ -283,7 +276,7 @@ benchmarks! { }: _(RawOrigin::Signed(borrower), pool_id, loan_info) borrow { - let n in 1..Helper::::max_benchmark_loans() - 1; + let n in 1..T::MaxActiveLoansPerPool::get() - 1; let borrower = account("borrower", 0, 0); let pool_id = Helper::::initialize_active_state(n); @@ -292,7 +285,7 @@ benchmarks! { }: _(RawOrigin::Signed(borrower), pool_id, loan_id, 10.into()) repay { - let n in 1..Helper::::max_benchmark_loans() - 1; + let n in 1..T::MaxActiveLoansPerPool::get() - 1; let borrower = account("borrower", 0, 0); let pool_id = Helper::::initialize_active_state(n); @@ -302,7 +295,7 @@ benchmarks! { }: _(RawOrigin::Signed(borrower), pool_id, loan_id, 10.into(), 0.into()) write_off { - let n in 1..Helper::::max_benchmark_loans() - 1; + let n in 1..T::MaxActiveLoansPerPool::get() - 1; let borrower = account("borrower", 0, 0); let pool_id = Helper::::initialize_active_state(n); @@ -314,7 +307,7 @@ benchmarks! { }: _(RawOrigin::Signed(borrower), pool_id, loan_id) admin_write_off { - let n in 1..Helper::::max_benchmark_loans() - 1; + let n in 1..T::MaxActiveLoansPerPool::get() - 1; let loan_admin = account("loan_admin", 0, 0); let pool_id = Helper::::initialize_active_state(n); @@ -325,7 +318,7 @@ benchmarks! { }: _(RawOrigin::Signed(loan_admin), pool_id, loan_id, T::Rate::zero(), T::Rate::zero()) close { - let n in 1..Helper::::max_benchmark_loans() - 1; + let n in 1..T::MaxActiveLoansPerPool::get() - 1; let borrower = account("borrower", 0, 0); let pool_id = Helper::::initialize_active_state(n); @@ -343,7 +336,7 @@ benchmarks! { }: _(RawOrigin::Signed(pool_admin), pool_id, policy) update_portfolio_valuation { - let n in 1..Helper::::max_benchmark_loans(); + let n in 1..T::MaxActiveLoansPerPool::get(); let borrower = account("borrower", 0, 0); let pool_id = Helper::::initialize_active_state(n); diff --git a/runtime/common/src/lib.rs b/runtime/common/src/lib.rs index 212ae783fd..31dad96c49 100644 --- a/runtime/common/src/lib.rs +++ b/runtime/common/src/lib.rs @@ -22,6 +22,17 @@ pub mod account_conversion; pub mod apis; pub mod evm; +#[macro_export] +macro_rules! production_or_benchmark { + ($production:expr, $benchmark:expr) => {{ + if cfg!(feature = "runtime-benchmarks") { + $benchmark + } else { + $production + } + }}; +} + pub mod xcm_fees { use cfg_primitives::{constants::currency_decimals, types::Balance}; use frame_support::weights::constants::{ExtrinsicBaseWeight, WEIGHT_REF_TIME_PER_SECOND}; diff --git a/runtime/development/src/lib.rs b/runtime/development/src/lib.rs index a12a0488e4..777c8926f8 100644 --- a/runtime/development/src/lib.rs +++ b/runtime/development/src/lib.rs @@ -1296,16 +1296,20 @@ impl pallet_xcm_transactor::Config for Runtime { parameter_types! { pub const MaxActiveLoansPerPool: u32 = 1000; + /// We do not need so many iterations for benchmarking, + /// which also increase A LOT the time of calculating weights + pub const MaxActiveLoansPerPoolBenchmark: u32 = production_or_benchmark!( + MaxActiveLoansPerPool::get(), + 50 + ); pub const MaxWriteOffPolicySize: u32 = 10; pub const MaxPriceOracleMembers: u32 = 10; - pub const MaxHasDispatchedSize: u32 = if cfg!(feature = "runtime-benchmarks") { - // This can be removed once - // be merged. + // The benchmark distintion can be removed once + // be merged. + pub const MaxHasDispatchedSize: u32 = production_or_benchmark!( + MaxPriceOracleMembers::get(), MaxActiveLoansPerPool::get() - } else { - MaxPriceOracleMembers::get() - }; - + ); pub const MaxPoolsWithExternalPrices: u32 = 50; pub RootOperatorOraclePrice: AccountId = PRICE_ORACLE_PALLET_ID.into_account_truncating(); } @@ -1351,6 +1355,17 @@ impl pallet_data_collector::Config for Runtime { type Moment = Moment; } +impl pallet_interest_accrual::Config for Runtime { + type Balance = Balance; + type InterestRate = Rate; + // TODO: This is a stopgap value until we can calculate it correctly with + // updated benchmarks. See #1024 + type MaxRateCount = MaxActiveLoansPerPool; + type RuntimeEvent = RuntimeEvent; + type Time = Timestamp; + type Weights = (); +} + impl pallet_loans_ref::Config for Runtime { type Balance = Balance; type CollectionId = CollectionId; @@ -1358,7 +1373,7 @@ impl pallet_loans_ref::Config for Runtime { type InterestAccrual = InterestAccrual; type ItemId = ItemId; type LoanId = LoanId; - type MaxActiveLoansPerPool = MaxActiveLoansPerPool; + type MaxActiveLoansPerPool = MaxActiveLoansPerPoolBenchmark; type MaxWriteOffPolicySize = MaxWriteOffPolicySize; type NonFungible = Uniques; type Permissions = Permissions; @@ -1546,17 +1561,6 @@ impl orml_asset_registry::Config for Runtime { type WeightInfo = (); } -impl pallet_interest_accrual::Config for Runtime { - type Balance = Balance; - type InterestRate = Rate; - // TODO: This is a stopgap value until we can calculate it correctly with - // updated benchmarks. See #1024 - type MaxRateCount = MaxActiveLoansPerPool; - type RuntimeEvent = RuntimeEvent; - type Time = Timestamp; - type Weights = (); -} - impl pallet_connectors::Config for Runtime { type AccountConverter = AccountConverter; type AdminOrigin = EnsureRoot;