Skip to content

Commit

Permalink
Loans: Support for benchmarking prices (#1372)
Browse files Browse the repository at this point in the history
* Add associated type to handle max collection sizes

* support for data-collector
  • Loading branch information
lemunozm committed May 29, 2023
1 parent 63ac712 commit ea4af8c
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 2 deletions.
4 changes: 4 additions & 0 deletions libs/mocks/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ pub mod pallet {
type CollectionId;
type Collection: DataCollection<Self::DataId>;
type Data;
#[cfg(feature = "runtime-benchmarks")]
type MaxCollectionSize: Get<u32>;
}

#[pallet::pallet]
Expand Down Expand Up @@ -49,6 +51,8 @@ pub mod pallet {
impl<T: Config> DataRegistry<T::DataId, T::CollectionId> for Pallet<T> {
type Collection = T::Collection;
type Data = T::Data;
#[cfg(feature = "runtime-benchmarks")]
type MaxCollectionSize = T::MaxCollectionSize;

fn get(a: &T::DataId) -> T::Data {
execute_call!(a)
Expand Down
4 changes: 4 additions & 0 deletions libs/traits/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ pub trait DataRegistry<DataId, CollectionId> {
/// Represents a data
type Data;

/// Identify the max number a collection can reach.
#[cfg(feature = "runtime-benchmarks")]
type MaxCollectionSize: sp_runtime::traits::Get<u32>;

/// Return the last data value for a data id
fn get(data_id: &DataId) -> Self::Data;

Expand Down
3 changes: 3 additions & 0 deletions pallets/data-collector/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,6 @@ std = [
"cfg-traits/std",
"orml-traits/std",
]
runtime-benchmarks = [
"cfg-traits/runtime-benchmarks",
]
2 changes: 2 additions & 0 deletions pallets/data-collector/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ pub mod pallet {
impl<T: Config<I>, I: 'static> DataRegistry<T::DataId, T::CollectionId> for Pallet<T, I> {
type Collection = CachedCollection<T, I>;
type Data = Result<DataValueOf<T, I>, DispatchError>;
#[cfg(feature = "runtime-benchmarks")]
type MaxCollectionSize = T::MaxCollectionSize;

fn get(data_id: &T::DataId) -> Self::Data {
T::DataProvider::get_no_op(data_id)
Expand Down
20 changes: 18 additions & 2 deletions pallets/loans-ref/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
// GNU General Public License for more details.

use cfg_primitives::CFG;
use cfg_traits::{data::DataCollection, InterestAccrual, Permissions, PoolBenchmarkHelper};
use cfg_traits::{
data::{DataCollection, DataRegistry},
InterestAccrual, Permissions, PoolBenchmarkHelper,
};
use cfg_types::{
adjustments::Adjustment,
permissions::{PermissionScope, PoolRole, Role},
Expand Down Expand Up @@ -56,13 +59,19 @@ type MaxRateCountOf<T> = <<T as Config>::InterestAccrual as InterestAccrual<
Adjustment<<T as Config>::Balance>,
>>::MaxRateCount;

type MaxCollectionSizeOf<T> = <<T as Config>::PriceRegistry as DataRegistry<
<T as Config>::PriceId,
PoolIdOf<T>,
>>::MaxCollectionSize;

struct Helper<T>(sp_std::marker::PhantomData<T>);
impl<T: Config> Helper<T>
where
T::Balance: From<u128>,
T::NonFungible: Create<T::AccountId> + Mutate<T::AccountId>,
T::CollectionId: From<u16>,
T::ItemId: From<u16>,
T::PriceId: From<u32>,
T::Pool:
PoolBenchmarkHelper<PoolId = PoolIdOf<T>, AccountId = T::AccountId, Balance = T::Balance>,
PriceCollectionOf<T>: DataCollection<T::PriceId, Data = PriceResultOf<T>>,
Expand All @@ -81,6 +90,7 @@ where
MockPools::mock_deposit(|_, _, _| Ok(()));
MockPools::mock_benchmark_create_pool(|_, _| {});
MockPools::mock_benchmark_give_ausd(|_, _| {});
MockPrices::mock_register_id(|_, _| Ok(()));
MockPrices::mock_collection(|_| MockDataCollection::new(|_| Ok((0, 0))));
}

Expand Down Expand Up @@ -209,13 +219,18 @@ where
}

fn initialize_active_state(n: u32) -> PoolIdOf<T> {
let pool_id = Self::prepare_benchmark();

for i in 1..MaxRateCountOf::<T>::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();
}

let pool_id = Self::prepare_benchmark();
for i in 0..MaxCollectionSizeOf::<T>::get() {
let price_id = T::PriceId::from(i + 1);
T::PriceRegistry::register_id(&price_id, &pool_id).unwrap();
}

for i in 0..n {
let item_id = (i as u16).into();
Expand All @@ -234,6 +249,7 @@ benchmarks! {
T::NonFungible: Create<T::AccountId> + Mutate<T::AccountId>,
T::CollectionId: From<u16>,
T::ItemId: From<u16>,
T::PriceId: From<u32>,
T::Pool: PoolBenchmarkHelper<PoolId = PoolIdOf<T>, AccountId = T::AccountId, Balance = T::Balance>,
PriceCollectionOf<T>: DataCollection<T::PriceId, Data = PriceResultOf<T>>,
}
Expand Down
2 changes: 2 additions & 0 deletions pallets/loans-ref/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ impl pallet_mock_data::Config for Runtime {
type CollectionId = PoolId;
type Data = Result<(Balance, Moment), DispatchError>;
type DataId = PriceId;
#[cfg(feature = "runtime-benchmarks")]
type MaxCollectionSize = MaxActiveLoansPerPool;
}

impl pallet_loans::Config for Runtime {
Expand Down
2 changes: 2 additions & 0 deletions pallets/loans-ref/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ pub struct NoPriceRegistry<T>(PhantomData<T>);
impl<T: Config> DataRegistry<T::PriceId, PoolIdOf<T>> for NoPriceRegistry<T> {
type Collection = NoPriceCollection<T>;
type Data = PriceResultOf<T>;
#[cfg(feature = "runtime-benchmarks")]
type MaxCollectionSize = sp_runtime::traits::ConstU32<0>;

fn get(_: &T::PriceId) -> Self::Data {
Err(DEFAULT_ERR)
Expand Down

0 comments on commit ea4af8c

Please sign in to comment.