diff --git a/pallets/interest-accrual/Cargo.toml b/pallets/interest-accrual/Cargo.toml index deca59e00f..0f2636e1ff 100644 --- a/pallets/interest-accrual/Cargo.toml +++ b/pallets/interest-accrual/Cargo.toml @@ -32,6 +32,7 @@ cfg-types = { workspace = true } [dev-dependencies] bitflags = { workspace = true } +cfg-mocks = { workspace = true } [features] default = ["std"] @@ -60,6 +61,7 @@ runtime-benchmarks = [ "frame-system/runtime-benchmarks", "pallet-timestamp/runtime-benchmarks", "sp-runtime/runtime-benchmarks", + "cfg-mocks/runtime-benchmarks", ] try-runtime = [ "cfg-primitives/try-runtime", @@ -69,4 +71,5 @@ try-runtime = [ "frame-system/try-runtime", "pallet-timestamp/try-runtime", "sp-runtime/try-runtime", + "cfg-mocks/try-runtime", ] diff --git a/pallets/interest-accrual/src/benchmarking.rs b/pallets/interest-accrual/src/benchmarking.rs index 65018e8668..76237d395f 100644 --- a/pallets/interest-accrual/src/benchmarking.rs +++ b/pallets/interest-accrual/src/benchmarking.rs @@ -31,9 +31,9 @@ benchmarks! { // and returns a reasonably-precise weight for the pow. calculate_accumulated_rate { let n in 1..25; - let now: Seconds = (1 << n) - 1; + let now = Seconds::from((1 << n) - 1); let rate = interest_rate_per_sec(T::Rate::saturating_from_rational(10, 100)).unwrap(); - }: { Pallet::::calculate_accumulated_rate(rate, One::one(), 0, now).unwrap() } + }: { Pallet::::calculate_accumulated_rate(rate, One::one(), Zero::zero(), now).unwrap() } verify { } } diff --git a/pallets/interest-accrual/src/lib.rs b/pallets/interest-accrual/src/lib.rs index 42147bed22..5f956c94f3 100644 --- a/pallets/interest-accrual/src/lib.rs +++ b/pallets/interest-accrual/src/lib.rs @@ -122,17 +122,17 @@ #![cfg_attr(not(feature = "std"), no_std)] -use cfg_primitives::SECONDS_PER_YEAR; +use cfg_primitives::{Seconds, SECONDS_PER_YEAR}; use cfg_traits::{ interest::{InterestAccrual, InterestRate, RateCollection}, - Seconds, TimeAsSecs, + time::UnixTimeSecs, }; use cfg_types::adjustments::Adjustment; use frame_support::{pallet_prelude::RuntimeDebug, BoundedVec}; use frame_system::pallet_prelude::BlockNumberFor; use parity_scale_codec::{Decode, Encode, MaxEncodedLen}; use scale_info::TypeInfo; -use sp_arithmetic::traits::{checked_pow, One, Zero}; +use sp_arithmetic::traits::{ensure_pow, One, Zero}; use sp_runtime::{ traits::{ AtLeast32BitUnsigned, CheckedAdd, CheckedSub, EnsureAdd, EnsureAddAssign, EnsureDiv, @@ -211,7 +211,7 @@ pub mod pallet { + FixedPointNumber + MaxEncodedLen; - type Time: TimeAsSecs; + type Time: UnixTimeSecs; type MaxRateCount: Get; @@ -315,8 +315,7 @@ pub mod pallet { Ordering::Less => { let delta = now.ensure_sub(when)?; let rate_adjustment = - checked_pow(rate.interest_rate_per_sec, delta.ensure_into()?) - .ok_or(ArithmeticError::Overflow)?; + ensure_pow(rate.interest_rate_per_sec, delta.ensure_into()?)?; rate.accumulated_rate.ensure_div(rate_adjustment)? } Ordering::Greater => { @@ -389,8 +388,7 @@ pub mod pallet { ) -> Result { // accumulated_rate * interest_rate_per_sec ^ (now - last_updated) let time_difference_secs = now.ensure_sub(last_updated)?; - checked_pow(interest_rate_per_sec, time_difference_secs as usize) - .ok_or(ArithmeticError::Overflow)? // TODO: This line can be remove once #1241 be merged + ensure_pow(interest_rate_per_sec, time_difference_secs.ensure_into()?)? .ensure_mul(accumulated_rate) } diff --git a/pallets/interest-accrual/src/mock.rs b/pallets/interest-accrual/src/mock.rs index e19324e0bf..f3c42e2b7d 100644 --- a/pallets/interest-accrual/src/mock.rs +++ b/pallets/interest-accrual/src/mock.rs @@ -1,17 +1,16 @@ -use cfg_traits::Millis; -use frame_support::{derive_impl, parameter_types, traits::Hooks}; -use sp_io::TestExternalities; -use sp_runtime::BuildStorage; +use frame_support::{derive_impl, parameter_types}; use crate::*; +pub const START_DATE: Seconds = Seconds::from(1640995200); + pub type Balance = u128; pub type Rate = sp_arithmetic::fixed_point::FixedU128; frame_support::construct_runtime!( pub enum Runtime { System: frame_system, - Timestamp: pallet_timestamp, + Timer: cfg_mocks::time::pallet, InterestAccrual: crate, } ); @@ -21,11 +20,8 @@ impl frame_system::Config for Runtime { type Block = frame_system::mocking::MockBlock; } -impl pallet_timestamp::Config for Runtime { - type MinimumPeriod = (); - type Moment = Millis; - type OnTimestampSet = (); - type WeightInfo = (); +impl cfg_mocks::time::pallet::Config for Runtime { + type Moment = Seconds; } parameter_types! { @@ -37,25 +33,15 @@ impl Config for Runtime { type MaxRateCount = MaxRateCount; type Rate = Rate; type RuntimeEvent = RuntimeEvent; - type Time = Timestamp; + type Time = Timer; type Weights = (); } #[allow(unused)] pub fn new_test_ext() -> sp_io::TestExternalities { - const SECONDS: u64 = 1000; - const START_DATE: u64 = 1640995200; - - let storage = frame_system::GenesisConfig::::default() - .build_storage() - .unwrap(); - - let mut externalities = TestExternalities::new(storage); - externalities.execute_with(|| { - System::set_block_number(1); - System::on_initialize(System::block_number()); - Timestamp::on_initialize(System::block_number()); - Timestamp::set(RuntimeOrigin::none(), START_DATE * SECONDS).unwrap(); + let mut ext = System::externalities(); + ext.execute_with(|| { + Timer::mock_now(|| START_DATE); }); - externalities + ext }