Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/github_actions/google-github-acti…
Browse files Browse the repository at this point in the history
…ons/setup-gcloud-2.0.1
  • Loading branch information
wischli authored Jan 3, 2024
2 parents 1f44df4 + f967ff6 commit 1a9a7f0
Show file tree
Hide file tree
Showing 16 changed files with 402 additions and 49 deletions.
6 changes: 4 additions & 2 deletions libs/mocks/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ pub mod pallet {
register_call!(move |(a, b)| f(a, b));
}

pub fn mock_collection(f: impl Fn(&T::CollectionId) -> T::Collection + 'static) {
pub fn mock_collection(
f: impl Fn(&T::CollectionId) -> Result<T::Collection, DispatchError> + 'static,
) {
register_call!(f);
}

Expand All @@ -56,7 +58,7 @@ pub mod pallet {
execute_call!((a, b))
}

fn collection(a: &T::CollectionId) -> T::Collection {
fn collection(a: &T::CollectionId) -> Result<T::Collection, DispatchError> {
execute_call!(a)
}

Expand Down
2 changes: 1 addition & 1 deletion libs/traits/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub trait DataRegistry<DataId, CollectionId> {

/// Retrives a collection of data with all data associated to a collection
/// id
fn collection(collection_id: &CollectionId) -> Self::Collection;
fn collection(collection_id: &CollectionId) -> Result<Self::Collection, DispatchError>;

/// Start listening data changes for a data id in a collection id
fn register_id(data_id: &DataId, collection_id: &CollectionId) -> DispatchResult;
Expand Down
2 changes: 1 addition & 1 deletion pallets/loans/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ fn config_mocks() {
MockPools::mock_withdraw(|_, _, _| Ok(()));
MockPools::mock_deposit(|_, _, _| Ok(()));
MockPrices::mock_register_id(|_, _| Ok(()));
MockPrices::mock_collection(|_| MockDataCollection::new(|_| Ok(Default::default())));
MockPrices::mock_collection(|_| Ok(MockDataCollection::new(|_| Ok(Default::default()))));
MockChangeGuard::mock_note(|_, change| {
MockChangeGuard::mock_released(move |_, _| Ok(change.clone()));
Ok(sp_core::H256::default())
Expand Down
2 changes: 1 addition & 1 deletion pallets/loans/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1013,7 +1013,7 @@ pub mod pallet {
pool_id: T::PoolId,
) -> Result<(T::Balance, u32), DispatchError> {
let rates = T::InterestAccrual::rates();
let prices = T::PriceRegistry::collection(&pool_id);
let prices = T::PriceRegistry::collection(&pool_id)?;
let loans = ActiveLoans::<T>::get(pool_id);
let values = loans
.iter()
Expand Down
8 changes: 5 additions & 3 deletions pallets/loans/src/tests/portfolio_valuation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ fn config_mocks() {
});
MockPrices::mock_collection(|pool_id| {
assert_eq!(*pool_id, POOL_A);
MockDataCollection::new(|id| match *id {
Ok(MockDataCollection::new(|id| match *id {
REGISTER_PRICE_ID => Ok((PRICE_VALUE, BLOCK_TIME_MS)),
_ => Err(PRICE_ID_NO_FOUND),
})
}))
});
}

Expand Down Expand Up @@ -195,7 +195,9 @@ fn with_unregister_price_id_and_oracle_not_required() {

// Suddenty, the oracle set a value
MockPrices::mock_collection(|_| {
MockDataCollection::new(|_| Ok((PRICE_VALUE * 8, BLOCK_TIME_MS)))
Ok(MockDataCollection::new(|_| {
Ok((PRICE_VALUE * 8, BLOCK_TIME_MS))
}))
});

update_portfolio();
Expand Down
36 changes: 33 additions & 3 deletions pallets/oracle-data-collection/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@ use frame_system::RawOrigin;

use crate::{
pallet::{Call, Collection, Config, Pallet},
types::Change,
types::{Change, CollectionInfo},
};

#[cfg(test)]
fn init_mocks() {
use crate::mock::{MockChangeGuard, MockIsAdmin, MockProvider};
use crate::mock::{MockChangeGuard, MockIsAdmin, MockProvider, MockTime};

MockIsAdmin::mock_check(|_| true);
MockProvider::mock_get(|_, _| Ok(Some((Default::default(), Default::default()))));
MockChangeGuard::mock_note(|_, change| {
MockChangeGuard::mock_released(move |_, _| Ok(change.clone()));
Ok(Default::default())
});
MockTime::mock_now(|| 0);
}

mod util {
Expand Down Expand Up @@ -131,16 +132,45 @@ mod benchmarks {
)?;
}

// Worst case expect to read the max age
Pallet::<T>::set_collection_info(
RawOrigin::Signed(admin.clone()).into(),
T::CollectionId::default(),
CollectionInfo::default(),
)
.unwrap();

#[extrinsic_call]
update_collection(RawOrigin::Signed(admin), T::CollectionId::default());

assert_eq!(
Collection::<T>::get(T::CollectionId::default()).len() as u32,
Collection::<T>::get(T::CollectionId::default())
.content
.len() as u32,
m
);

Ok(())
}

#[benchmark]
fn set_collection_info() -> Result<(), BenchmarkError> {
#[cfg(test)]
init_mocks();

let admin: T::AccountId = whitelisted_caller();

T::ChangeGuard::bench_create_pool(T::CollectionId::default(), &admin);

#[extrinsic_call]
set_collection_info(
RawOrigin::Signed(admin),
T::CollectionId::default(),
CollectionInfo::default(),
);

Ok(())
}

impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::Runtime);
}
Loading

0 comments on commit 1a9a7f0

Please sign in to comment.