Skip to content

Commit

Permalink
runtime compiling without std
Browse files Browse the repository at this point in the history
  • Loading branch information
lemunozm committed Aug 5, 2024
1 parent ca0872a commit 431c9dc
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 66 deletions.
72 changes: 61 additions & 11 deletions libs/utils/src/num_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use sp_arithmetic::traits::{
Bounded, CheckedAdd, CheckedDiv, CheckedMul, CheckedNeg, CheckedRem, CheckedShl, CheckedShr,
CheckedSub, IntegerSquareRoot, One, Saturating, Zero,
};
use sp_runtime::traits::Scale;
use sp_std::{
cmp::Ordering,
fmt::{self, Debug},
Expand Down Expand Up @@ -128,21 +129,25 @@ macro_rules! impl_try_from {

macro_rules! impl_into {
($from:ty, $to:ty) => {
impl<I> Into<$to> for NumWrapper<$from, I> {
fn into(self) -> $to {
self.inner.into()
// Implemented as an opposite From to have
// the inverse Into automatically implemented
impl<I> From<NumWrapper<$from, I>> for $to {
fn from(other: NumWrapper<$from, I>) -> Self {
other.inner.into()
}
}
};
}

macro_rules! impl_try_into {
($from:ty, $to:ty) => {
impl<I> TryInto<$to> for NumWrapper<$from, I> {
type Error = <$from as TryInto<$to>>::Error;
// Implemented as an opposite TryFrom to have
// the inverse TryInto automatically implemented
impl<I> TryFrom<NumWrapper<$from, I>> for $to {
type Error = <$to as TryFrom<$from>>::Error;

fn try_into(self) -> Result<$to, Self::Error> {
Ok(self.inner.try_into()?)
fn try_from(other: NumWrapper<$from, I>) -> Result<$to, Self::Error> {
other.inner.try_into()
}
}
};
Expand Down Expand Up @@ -556,6 +561,22 @@ impl<T, I> CompactAs for NumWrapper<T, I> {
}
}

impl<T: Scale<S, Output = T>, S, I> Scale<S> for NumWrapper<T, I> {
type Output = Self;

fn mul(self, other: S) -> Self::Output {
Self::new(self.inner.mul(other))
}

fn div(self, other: S) -> Self::Output {
Self::new(self.inner.div(other))
}

fn rem(self, other: S) -> Self::Output {
Self::new(self.inner.rem(other))
}
}

#[cfg(test)]
mod tests {
use frame_support::Parameter;
Expand All @@ -573,6 +594,7 @@ mod tests {
fn is_type_info<T: TypeInfo>() {}
fn is_encode_like<T: EncodeLike>() {}
fn is_fixed_point_operand<T: FixedPointOperand>() {}
fn is_scale<T: Scale<S>, S>() {}

// Id does not require any implementation
struct Id;
Expand Down Expand Up @@ -600,8 +622,36 @@ mod tests {
}

check_wrapper!(u8_type, u8);
check_wrapper!(u16_type, u8);
check_wrapper!(u32_type, u8);
check_wrapper!(u64_type, u8);
check_wrapper!(u128_type, u8);
check_wrapper!(u16_type, u16);
check_wrapper!(u32_type, u32);
check_wrapper!(u64_type, u64);
check_wrapper!(u128_type, u128);

#[test]
fn check_scale() {
type U8 = NumWrapper<u8, Id>;
is_scale::<U8, u8>();

type U16 = NumWrapper<u16, Id>;
is_scale::<U8, u8>();
is_scale::<U16, u16>();

type U32 = NumWrapper<u32, Id>;
is_scale::<U32, u8>();
is_scale::<U32, u16>();
is_scale::<U32, u32>();

type U64 = NumWrapper<u64, Id>;
is_scale::<U64, u8>();
is_scale::<U64, u16>();
is_scale::<U64, u32>();
is_scale::<U64, u64>();

type U128 = NumWrapper<u128, Id>;
is_scale::<U128, u8>();
is_scale::<U128, u16>();
is_scale::<U128, u32>();
is_scale::<U128, u64>();
is_scale::<U128, u128>();
}
}
8 changes: 3 additions & 5 deletions pallets/pool-fees/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use cfg_mocks::{
pallet_mock_change_guard, pallet_mock_permissions, pallet_mock_pools,
pre_conditions::pallet as pallet_mock_pre_conditions,
};
use cfg_primitives::{Balance, CollectionId, PoolFeeId, PoolId, TrancheId};
use cfg_primitives::{Balance, CollectionId, PoolFeeId, PoolId, Seconds, TrancheId};
use cfg_traits::{fee::PoolFeeBucket, PoolNAV};
use cfg_types::{
fixed_point::{Rate, Ratio},
Expand All @@ -37,8 +37,6 @@ use crate::{
Event::Accrued, FeeIds, FeeIdsToPoolBucket, LastFeeId, PoolFeeInfoOf, PoolFeeOf,
};

pub const SECONDS: u64 = 1000;

pub const ADMIN: AccountId = 1;
pub const EDITOR: AccountId = 2;
pub const DESTINATION: AccountId = 3;
Expand Down Expand Up @@ -137,7 +135,7 @@ impl orml_tokens::Config for Runtime {
}

impl cfg_mocks::pallet_mock_time::Config for Runtime {
type Moment = u64;
type Moment = Seconds;
}

impl cfg_test_utils::mocks::nav::Config for Runtime {
Expand Down Expand Up @@ -373,7 +371,7 @@ pub(crate) fn init_mocks() {
});
MockPools::mock_deposit(|_, _, _| Ok(()));
MockChangeGuard::mock_note(|_, _| Ok(H256::default()));
MockTime::mock_now(|| 0);
MockTime::mock_now(|| Seconds::new(0));
}

pub(crate) fn config_change_mocks(fee: &PoolFeeInfoOf<Runtime>) {
Expand Down
54 changes: 26 additions & 28 deletions pallets/pool-fees/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use cfg_primitives::Balance;
use cfg_primitives::{Balance, Seconds};
use frame_support::{assert_noop, assert_ok};
use rand::Rng;
use sp_arithmetic::FixedPointNumber;
Expand Down Expand Up @@ -520,17 +520,15 @@ mod extrinsics {

mod disbursements {
use cfg_primitives::SECONDS_PER_YEAR;
use cfg_traits::{EpochTransitionHook, PoolNAV, TimeAsSecs};
use cfg_traits::{time::UnixTimeSecs, EpochTransitionHook, PoolNAV};
use cfg_types::{
fixed_point::Rate,
pools::{PoolFeeAmount, PoolFeeType},
};
use frame_support::traits::fungibles::Inspect;

use super::*;
use crate::mock::{
get_disbursements, pay_single_fee_and_assert, MockTime, NAV, POOL_CURRENCY, SECONDS,
};
use crate::mock::{get_disbursements, pay_single_fee_and_assert, MockTime, NAV, POOL_CURRENCY};

mod single_fee {
use super::*;
Expand All @@ -544,7 +542,7 @@ mod disbursements {
#[test]
fn sufficient_reserve_sfs() {
ExtBuilder::default().set_aum(NAV).build().execute_with(|| {
MockTime::mock_now(|| SECONDS_PER_YEAR * SECONDS);
MockTime::mock_now(|| SECONDS_PER_YEAR);

let fee_id = 1;
let res_pre_fees = NAV;
Expand Down Expand Up @@ -585,7 +583,7 @@ mod disbursements {
#[test]
fn insufficient_reserve_sfs() {
ExtBuilder::default().set_aum(NAV).build().execute_with(|| {
MockTime::mock_now(|| SECONDS_PER_YEAR * SECONDS);
MockTime::mock_now(|| SECONDS_PER_YEAR);

let fee_id = 1;
let res_pre_fees = NAV / 100;
Expand Down Expand Up @@ -633,10 +631,10 @@ mod disbursements {
#[test]
fn sufficient_reserve_sfa() {
ExtBuilder::default().set_aum(NAV).build().execute_with(|| {
MockTime::mock_now(|| SECONDS_PER_YEAR * SECONDS);
MockTime::mock_now(|| SECONDS_PER_YEAR);

let fee_id = 1;
let res_pre_fees: Balance = (2 * SECONDS_PER_YEAR).into();
let res_pre_fees: Balance = (SECONDS_PER_YEAR * 2).into();
let res_post_fees = &mut res_pre_fees.clone();
let amount_per_second = 1;
let fee_amount = SECONDS_PER_YEAR.into();
Expand Down Expand Up @@ -674,7 +672,7 @@ mod disbursements {
#[test]
fn insufficient_reserve_sfa() {
ExtBuilder::default().set_aum(NAV).build().execute_with(|| {
MockTime::mock_now(|| SECONDS_PER_YEAR * SECONDS);
MockTime::mock_now(|| SECONDS_PER_YEAR);

let fee_id = 1;
let res_pre_fees: Balance = (SECONDS_PER_YEAR / 2).into();
Expand Down Expand Up @@ -739,7 +737,7 @@ mod disbursements {
#[test]
fn empty_charge_scfs() {
ExtBuilder::default().set_aum(NAV).build().execute_with(|| {
MockTime::mock_now(|| SECONDS_PER_YEAR * SECONDS);
MockTime::mock_now(|| SECONDS_PER_YEAR);

let fee_id = 1;
let res_pre_fees = NAV;
Expand Down Expand Up @@ -770,7 +768,7 @@ mod disbursements {
#[test]
fn below_max_charge_sufficient_reserve_scfs() {
ExtBuilder::default().set_aum(NAV).build().execute_with(|| {
MockTime::mock_now(|| SECONDS_PER_YEAR * SECONDS);
MockTime::mock_now(|| SECONDS_PER_YEAR);

let fee_id = 1;
let res_pre_fees = NAV;
Expand Down Expand Up @@ -808,7 +806,7 @@ mod disbursements {
#[test]
fn max_charge_sufficient_reserve_scfs() {
ExtBuilder::default().set_aum(NAV).build().execute_with(|| {
MockTime::mock_now(|| SECONDS_PER_YEAR * SECONDS);
MockTime::mock_now(|| SECONDS_PER_YEAR);

let fee_id = 1;
let res_pre_fees = NAV;
Expand Down Expand Up @@ -845,7 +843,7 @@ mod disbursements {
#[test]
fn excess_charge_sufficient_reserve_scfs() {
ExtBuilder::default().set_aum(NAV).build().execute_with(|| {
MockTime::mock_now(|| SECONDS_PER_YEAR * SECONDS);
MockTime::mock_now(|| SECONDS_PER_YEAR);

let fee_id = 1;
let res_pre_fees = NAV;
Expand Down Expand Up @@ -884,7 +882,7 @@ mod disbursements {
#[test]
fn insufficient_reserve_scfs() {
ExtBuilder::default().set_aum(NAV).build().execute_with(|| {
MockTime::mock_now(|| SECONDS_PER_YEAR * SECONDS);
MockTime::mock_now(|| SECONDS_PER_YEAR);

let fee_id = 1;
let res_pre_fees = NAV / 100;
Expand Down Expand Up @@ -939,7 +937,7 @@ mod disbursements {
#[test]
fn empty_charge_scfa() {
ExtBuilder::default().set_aum(NAV).build().execute_with(|| {
MockTime::mock_now(|| SECONDS_PER_YEAR * SECONDS);
MockTime::mock_now(|| SECONDS_PER_YEAR);

let fee_id = 1;
let res_pre_fees = NAV;
Expand Down Expand Up @@ -970,7 +968,7 @@ mod disbursements {
#[test]
fn below_max_charge_sufficient_reserve_scfa() {
ExtBuilder::default().set_aum(NAV).build().execute_with(|| {
MockTime::mock_now(|| SECONDS_PER_YEAR * SECONDS);
MockTime::mock_now(|| SECONDS_PER_YEAR);

let fee_id = 1;
let res_pre_fees = NAV;
Expand Down Expand Up @@ -1008,7 +1006,7 @@ mod disbursements {
#[test]
fn max_charge_sufficient_reserve_scfa() {
ExtBuilder::default().set_aum(NAV).build().execute_with(|| {
MockTime::mock_now(|| SECONDS_PER_YEAR * SECONDS);
MockTime::mock_now(|| SECONDS_PER_YEAR);

let fee_id = 1;
let res_pre_fees = NAV;
Expand Down Expand Up @@ -1045,7 +1043,7 @@ mod disbursements {
#[test]
fn excess_charge_sufficient_reserve_scfa() {
ExtBuilder::default().set_aum(NAV).build().execute_with(|| {
MockTime::mock_now(|| SECONDS_PER_YEAR * SECONDS);
MockTime::mock_now(|| SECONDS_PER_YEAR);

let fee_id = 1;
let res_pre_fees = NAV;
Expand Down Expand Up @@ -1084,7 +1082,7 @@ mod disbursements {
#[test]
fn insufficient_reserve_scfa() {
ExtBuilder::default().set_aum(NAV).build().execute_with(|| {
MockTime::mock_now(|| SECONDS_PER_YEAR * SECONDS);
MockTime::mock_now(|| SECONDS_PER_YEAR);

let fee_id = 1;
let amount_per_second = 1;
Expand Down Expand Up @@ -1191,10 +1189,10 @@ mod disbursements {
ExtBuilder::default().set_aum(NAV).build().execute_with(|| {
add_fees(vec![default_fixed_fee()]);

assert_eq!(PoolFees::nav(POOL), Some((0, 0)));
MockTime::mock_now(|| SECONDS_PER_YEAR * SECONDS);
assert_eq!(PoolFees::nav(POOL), Some((0, Seconds::new(0))));
MockTime::mock_now(|| SECONDS_PER_YEAR);

assert_eq!(PoolFees::nav(POOL), Some((0, 0)));
assert_eq!(PoolFees::nav(POOL), Some((0, Seconds::new(0))));
assert_ok!(PoolFees::update_portfolio_valuation(
RuntimeOrigin::signed(ANY),
POOL
Expand All @@ -1215,9 +1213,9 @@ mod disbursements {
fn update_single_charged() {
ExtBuilder::default().set_aum(NAV).build().execute_with(|| {
add_fees(vec![default_chargeable_fee()]);
MockTime::mock_now(|| SECONDS_PER_YEAR * SECONDS);
MockTime::mock_now(|| SECONDS_PER_YEAR);

assert_eq!(PoolFees::nav(POOL), Some((0, 0)));
assert_eq!(PoolFees::nav(POOL), Some((0, Seconds::new(0))));
assert_ok!(PoolFees::update_portfolio_valuation(
RuntimeOrigin::signed(ANY),
POOL
Expand All @@ -1242,15 +1240,15 @@ mod disbursements {
#[test]
fn fixed_charged_charged() {
ExtBuilder::default().set_aum(NAV).build().execute_with(|| {
MockTime::mock_now(|| SECONDS_PER_YEAR * SECONDS);
MockTime::mock_now(|| SECONDS_PER_YEAR);

let charged_fee_ids = vec![2, 3];
let res_pre_fees = NAV;
let res_post_fees = &mut res_pre_fees.clone();
let annual_rate = Rate::saturating_from_rational(1, 100);
let fixed_fee_amount = NAV / 100;
let amount_per_seconds = vec![2, 1];
let payable = vec![(2 * SECONDS_PER_YEAR).into(), SECONDS_PER_YEAR.into()];
let payable = vec![(SECONDS_PER_YEAR * 2).into(), SECONDS_PER_YEAR.into()];
let charged_y1 = vec![1, 2 * payable[1]];
let charged_y2 = vec![payable[0], payable[1]];

Expand Down Expand Up @@ -1347,7 +1345,7 @@ mod disbursements {

// Year 2: Make reserve insufficient to handle all fees (last fee
// falls short
MockTime::mock_now(|| 2 * SECONDS_PER_YEAR * SECONDS);
MockTime::mock_now(|| SECONDS_PER_YEAR * 2);
let res_pre_fees = fixed_fee_amount + charged_y2[0] + 1;
let res_post_fees = &mut res_pre_fees.clone();
assert_ok!(PoolFees::charge_fee(
Expand Down
5 changes: 2 additions & 3 deletions runtime/altair/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@ use cfg_primitives::{
IBalance, InvestmentId, ItemId, LoanId, Nonce, OrderId, PalletIndex, PoolEpochId,
PoolFeeId, PoolId, Signature, TrancheId, TrancheWeight,
},
LPGatewayQueueMessageNonce,
LPGatewayQueueMessageNonce, Millis, Seconds,
};
use cfg_traits::{
investments::OrderManager, Millis, Permissions as PermissionsT, PoolUpdateGuard, PreConditions,
Seconds,
investments::OrderManager, Permissions as PermissionsT, PoolUpdateGuard, PreConditions,
};
use cfg_types::{
fee_keys::{Fee, FeeKey},
Expand Down
5 changes: 2 additions & 3 deletions runtime/centrifuge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@ use cfg_primitives::{
IBalance, InvestmentId, ItemId, LoanId, Nonce, OrderId, PalletIndex, PoolEpochId,
PoolFeeId, PoolId, Signature, TrancheId, TrancheWeight,
},
LPGatewayQueueMessageNonce,
LPGatewayQueueMessageNonce, Millis, Seconds,
};
use cfg_traits::{
investments::OrderManager, Millis, Permissions as PermissionsT, PoolUpdateGuard, PreConditions,
Seconds,
investments::OrderManager, Permissions as PermissionsT, PoolUpdateGuard, PreConditions,
};
use cfg_types::{
fee_keys::{Fee, FeeKey},
Expand Down
Loading

0 comments on commit 431c9dc

Please sign in to comment.