From f108616c65f2c357b3f87dc1d251e7e92d00c4aa Mon Sep 17 00:00:00 2001 From: Roy Yang Date: Wed, 27 Oct 2021 15:51:18 +1300 Subject: [PATCH 01/25] Added on_initialize hook that yields interest for total_staking_balances --- modules/homa-lite/src/benchmarking.rs | 12 ++++ modules/homa-lite/src/lib.rs | 22 +++++++ modules/homa-lite/src/mock.rs | 4 ++ modules/homa-lite/src/tests.rs | 23 +++++++ modules/homa-lite/src/weights.rs | 65 +++++++++++-------- runtime/acala/src/lib.rs | 4 ++ runtime/acala/src/weights/module_homa_lite.rs | 45 +++++++------ runtime/karura/src/lib.rs | 4 ++ .../karura/src/weights/module_homa_lite.rs | 31 +++++---- runtime/mandala/src/lib.rs | 4 ++ .../mandala/src/weights/module_homa_lite.rs | 19 ++++-- 11 files changed, 166 insertions(+), 67 deletions(-) diff --git a/modules/homa-lite/src/benchmarking.rs b/modules/homa-lite/src/benchmarking.rs index 3bb41fbcc..1ddeb92da 100644 --- a/modules/homa-lite/src/benchmarking.rs +++ b/modules/homa-lite/src/benchmarking.rs @@ -47,6 +47,12 @@ benchmarks! { let _ = crate::Pallet::::on_idle(::BlockNumber::default(), 1_000_000_000); } + on_initialize { + let _ = crate::Pallet::::set_total_staking_currency(RawOrigin::Root.into(), 1_000_000_000_000_000_000); + }: { + let _ = crate::Pallet::::on_initialize(::BlockNumber::default()); + } + mint { let amount = 1_000_000_000_000; let caller: T::AccountId = account("caller", 0, SEED); @@ -104,6 +110,12 @@ mod tests { assert_ok!(Pallet::::test_benchmark_on_idle()); }); } + #[test] + fn test_on_initialize() { + ExtBuilder::default().build().execute_with(|| { + assert_ok!(Pallet::::test_benchmark_on_initialize()); + }); + } #[test] fn test_mint() { diff --git a/modules/homa-lite/src/lib.rs b/modules/homa-lite/src/lib.rs index e80bdba46..1d9a4887c 100644 --- a/modules/homa-lite/src/lib.rs +++ b/modules/homa-lite/src/lib.rs @@ -138,6 +138,14 @@ pub mod module { /// Maximum number of scheduled unbonds allowed #[pallet::constant] type MaxScheduledUnbonds: Get; + + /// The number of blocks to pass before TotalStakingCurrency is updated. + #[pallet::constant] + type StakingUpdateFrequency: Get; + + /// Interest rate per update + #[pallet::constant] + type StakingInterestRatePerUpdate: Get; } #[pallet::error] @@ -267,6 +275,20 @@ pub mod module { } current_weight } + + fn on_initialize(n: T::BlockNumber) -> Weight { + // Update the total amount of Staking balance by acrueing the interest periodically. + if !T::StakingInterestRatePerUpdate::get().is_zero() && n % T::StakingUpdateFrequency::get() == Zero::zero() + { + let new_total = TotalStakingCurrency::::mutate(|current| { + *current = current.saturating_add(T::StakingInterestRatePerUpdate::get().mul(*current)); + *current + }); + Self::deposit_event(Event::::TotalStakingCurrencySet(new_total)); + return ::WeightInfo::on_initialize(); + } + 0 + } } #[pallet::call] diff --git a/modules/homa-lite/src/mock.rs b/modules/homa-lite/src/mock.rs index 51c77a26c..b69115636 100644 --- a/modules/homa-lite/src/mock.rs +++ b/modules/homa-lite/src/mock.rs @@ -270,6 +270,8 @@ parameter_types! { pub const MaxScheduledUnbonds: u32 = 14; pub const SubAccountIndex: u16 = 0; pub ParachainId: ParaId = ParaId::from(PARACHAIN_ID); + pub const StakingUpdateFrequency: BlockNumber = 100; + pub StakingInterestRatePerUpdate: Permill = Permill::from_rational(1u32, 100u32); } ord_parameter_types! { pub const Root: AccountId = ROOT; @@ -306,6 +308,8 @@ impl Config for Runtime { type MaximumRedeemRequestMatchesForMint = MaximumRedeemRequestMatchesForMint; type RelayChainUnbondingSlashingSpans = RelayChainUnbondingSlashingSpans; type MaxScheduledUnbonds = MaxScheduledUnbonds; + type StakingUpdateFrequency = StakingUpdateFrequency; + type StakingInterestRatePerUpdate = StakingInterestRatePerUpdate; } type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; diff --git a/modules/homa-lite/src/tests.rs b/modules/homa-lite/src/tests.rs index 568ef91b0..7986d2b6e 100644 --- a/modules/homa-lite/src/tests.rs +++ b/modules/homa-lite/src/tests.rs @@ -824,3 +824,26 @@ fn redeem_can_handle_dust_available_staking_currency() { ))); }); } + +#[test] +fn total_staking_currency_update_periodically() { + ExtBuilder::default().build().execute_with(|| { + assert_ok!(HomaLite::set_total_staking_currency(Origin::root(), dollar(1_000_000))); + + HomaLite::on_initialize(0); + // Inflate by 1%: 1_000_000 * 1.01 + assert_eq!(TotalStakingCurrency::::get(), dollar(1_010_000)); + + for i in 1..101 { + HomaLite::on_initialize(i); + } + // 1_010_000 * 1.01 + assert_eq!(TotalStakingCurrency::::get(), dollar(1_020_100)); + + for i in 101..201 { + HomaLite::on_initialize(i); + } + //1_020_100 * 1.01 + assert_eq!(TotalStakingCurrency::::get(), dollar(1_030_301)); + }); +} diff --git a/modules/homa-lite/src/weights.rs b/modules/homa-lite/src/weights.rs index 2ded7545d..e3e01f076 100644 --- a/modules/homa-lite/src/weights.rs +++ b/modules/homa-lite/src/weights.rs @@ -19,7 +19,7 @@ //! Autogenerated weights for module_homa_lite //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2021-10-03, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! DATE: 2021-10-27, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` //! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 128 // Executed Command: @@ -48,6 +48,7 @@ use sp_std::marker::PhantomData; /// Weight functions needed for module_homa_lite. pub trait WeightInfo { fn on_idle() -> Weight; + fn on_initialize() -> Weight; fn mint() -> Weight; fn mint_for_requests() -> Weight; fn set_total_staking_currency() -> Weight; @@ -63,49 +64,54 @@ pub trait WeightInfo { pub struct AcalaWeight(PhantomData); impl WeightInfo for AcalaWeight { fn on_idle() -> Weight { - (34_000_000 as Weight) - .saturating_add(T::DbWeight::get().reads(8 as Weight)) - .saturating_add(T::DbWeight::get().writes(3 as Weight)) + (254_000_000 as Weight) + .saturating_add(T::DbWeight::get().reads(23 as Weight)) + .saturating_add(T::DbWeight::get().writes(17 as Weight)) + } + fn on_initialize() -> Weight { + (12_000_000 as Weight) + .saturating_add(T::DbWeight::get().reads(1 as Weight)) + .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn mint() -> Weight { - (128_000_000 as Weight) + (139_000_000 as Weight) .saturating_add(T::DbWeight::get().reads(14 as Weight)) .saturating_add(T::DbWeight::get().writes(7 as Weight)) } fn mint_for_requests() -> Weight { - (338_000_000 as Weight) + (351_000_000 as Weight) .saturating_add(T::DbWeight::get().reads(30 as Weight)) .saturating_add(T::DbWeight::get().writes(21 as Weight)) } fn set_total_staking_currency() -> Weight { - (10_000_000 as Weight) + (11_000_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn adjust_total_staking_currency() -> Weight { - (12_000_000 as Weight) + (13_000_000 as Weight) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn set_minting_cap() -> Weight { - (10_000_000 as Weight) + (11_000_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn set_xcm_dest_weight() -> Weight { - (10_000_000 as Weight) + (11_000_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn request_redeem() -> Weight { - (76_000_000 as Weight) + (84_000_000 as Weight) .saturating_add(T::DbWeight::get().reads(7 as Weight)) - .saturating_add(T::DbWeight::get().writes(3 as Weight)) + .saturating_add(T::DbWeight::get().writes(4 as Weight)) } fn schedule_unbond() -> Weight { - (12_000_000 as Weight) + (14_000_000 as Weight) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn replace_schedule_unbond() -> Weight { - (10_000_000 as Weight) + (11_000_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } } @@ -113,49 +119,54 @@ impl WeightInfo for AcalaWeight { // For backwards compatibility and tests impl WeightInfo for () { fn on_idle() -> Weight { - (34_000_000 as Weight) - .saturating_add(RocksDbWeight::get().reads(8 as Weight)) - .saturating_add(RocksDbWeight::get().writes(3 as Weight)) + (254_000_000 as Weight) + .saturating_add(RocksDbWeight::get().reads(23 as Weight)) + .saturating_add(RocksDbWeight::get().writes(17 as Weight)) + } + fn on_initialize() -> Weight { + (12_000_000 as Weight) + .saturating_add(RocksDbWeight::get().reads(1 as Weight)) + .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } fn mint() -> Weight { - (128_000_000 as Weight) + (139_000_000 as Weight) .saturating_add(RocksDbWeight::get().reads(14 as Weight)) .saturating_add(RocksDbWeight::get().writes(7 as Weight)) } fn mint_for_requests() -> Weight { - (338_000_000 as Weight) + (351_000_000 as Weight) .saturating_add(RocksDbWeight::get().reads(30 as Weight)) .saturating_add(RocksDbWeight::get().writes(21 as Weight)) } fn set_total_staking_currency() -> Weight { - (10_000_000 as Weight) + (11_000_000 as Weight) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } fn adjust_total_staking_currency() -> Weight { - (12_000_000 as Weight) + (13_000_000 as Weight) .saturating_add(RocksDbWeight::get().reads(1 as Weight)) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } fn set_minting_cap() -> Weight { - (10_000_000 as Weight) + (11_000_000 as Weight) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } fn set_xcm_dest_weight() -> Weight { - (10_000_000 as Weight) + (11_000_000 as Weight) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } fn request_redeem() -> Weight { - (76_000_000 as Weight) + (84_000_000 as Weight) .saturating_add(RocksDbWeight::get().reads(7 as Weight)) - .saturating_add(RocksDbWeight::get().writes(3 as Weight)) + .saturating_add(RocksDbWeight::get().writes(4 as Weight)) } fn schedule_unbond() -> Weight { - (12_000_000 as Weight) + (14_000_000 as Weight) .saturating_add(RocksDbWeight::get().reads(1 as Weight)) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } fn replace_schedule_unbond() -> Weight { - (10_000_000 as Weight) + (11_000_000 as Weight) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } } diff --git a/runtime/acala/src/lib.rs b/runtime/acala/src/lib.rs index 030a29f35..944bcb913 100644 --- a/runtime/acala/src/lib.rs +++ b/runtime/acala/src/lib.rs @@ -1533,6 +1533,8 @@ parameter_types! { pub ParachainAccount: AccountId = ParachainInfo::get().into_account(); pub SubAccountIndex: u16 = RelayChainSubAccountId::HomaLite as u16; pub const XcmUnbondFee: Balance = 600_000_000; // TODO identify unbon fee + pub const StakingUpdateFrequency: BlockNumber = 14_400; // Update staking total Once per day + pub StakingInterestRatePerUpdate: Permill = Permill::from_rational(383u32, 1_000_000u32); // 1.15^(1/365) = 1.000383 } impl module_homa_lite::Config for Runtime { type Event = Event; @@ -1557,6 +1559,8 @@ impl module_homa_lite::Config for Runtime { type MaximumRedeemRequestMatchesForMint = MaximumRedeemRequestMatchesForMint; type RelayChainUnbondingSlashingSpans = RelayChainUnbondingSlashingSpans; type MaxScheduledUnbonds = MaxScheduledUnbonds; + type StakingUpdateFrequency = StakingUpdateFrequency; + type StakingInterestRatePerUpdate = StakingInterestRatePerUpdate; } pub type LocalAssetTransactor = MultiCurrencyAdapter< diff --git a/runtime/acala/src/weights/module_homa_lite.rs b/runtime/acala/src/weights/module_homa_lite.rs index abd23f1f5..75f367505 100644 --- a/runtime/acala/src/weights/module_homa_lite.rs +++ b/runtime/acala/src/weights/module_homa_lite.rs @@ -19,22 +19,22 @@ //! Autogenerated weights for module_homa_lite //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2021-10-04, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` -//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("karura-dev"), DB CACHE: 128 +//! DATE: 2021-10-27, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("acala-dev"), DB CACHE: 128 // Executed Command: // target/release/acala // benchmark -// --chain=karura-dev +// --chain=acala-dev // --steps=50 // --repeat=20 -// --pallet=module_homa_lite +// --pallet=module-homa-lite // --extrinsic=* // --execution=wasm // --wasm-execution=compiled // --heap-pages=4096 // --template=./templates/runtime-weight-template.hbs -// --output=./runtime/karura/src/weights/ +// --output=./runtime/acala/src/weights/ #![cfg_attr(rustfmt, rustfmt_skip)] @@ -48,49 +48,54 @@ use sp_std::marker::PhantomData; pub struct WeightInfo(PhantomData); impl module_homa_lite::WeightInfo for WeightInfo { fn on_idle() -> Weight { - (63_233_000 as Weight) - .saturating_add(T::DbWeight::get().reads(8 as Weight)) - .saturating_add(T::DbWeight::get().writes(3 as Weight)) + (249_000_000 as Weight) + .saturating_add(T::DbWeight::get().reads(23 as Weight)) + .saturating_add(T::DbWeight::get().writes(17 as Weight)) + } + fn on_initialize() -> Weight { + (11_000_000 as Weight) + .saturating_add(T::DbWeight::get().reads(1 as Weight)) + .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn mint() -> Weight { - (238_339_000 as Weight) + (134_000_000 as Weight) .saturating_add(T::DbWeight::get().reads(14 as Weight)) .saturating_add(T::DbWeight::get().writes(7 as Weight)) } fn mint_for_requests() -> Weight { - (254_671_000 as Weight) - .saturating_add(T::DbWeight::get().reads(16 as Weight)) - .saturating_add(T::DbWeight::get().writes(7 as Weight)) + (342_000_000 as Weight) + .saturating_add(T::DbWeight::get().reads(30 as Weight)) + .saturating_add(T::DbWeight::get().writes(21 as Weight)) } fn set_total_staking_currency() -> Weight { - (18_892_000 as Weight) + (11_000_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn adjust_total_staking_currency() -> Weight { - (21_634_000 as Weight) + (12_000_000 as Weight) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn set_minting_cap() -> Weight { - (19_501_000 as Weight) + (11_000_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn set_xcm_dest_weight() -> Weight { - (18_739_000 as Weight) + (11_000_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn request_redeem() -> Weight { - (136_967_000 as Weight) + (80_000_000 as Weight) .saturating_add(T::DbWeight::get().reads(7 as Weight)) - .saturating_add(T::DbWeight::get().writes(3 as Weight)) + .saturating_add(T::DbWeight::get().writes(4 as Weight)) } fn schedule_unbond() -> Weight { - (21_197_000 as Weight) + (13_000_000 as Weight) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn replace_schedule_unbond() -> Weight { - (19_289_000 as Weight) + (11_000_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } } diff --git a/runtime/karura/src/lib.rs b/runtime/karura/src/lib.rs index cc1f74a54..3cc0461f9 100644 --- a/runtime/karura/src/lib.rs +++ b/runtime/karura/src/lib.rs @@ -1552,6 +1552,8 @@ parameter_types! { // Calculated from polkadot/xcm/xcm-builder: fn buy_weight // We must charge higher than what Kusama required (533_333_300, obtained from integration test) pub XcmUnbondFee: Balance = 60 * millicent(KSM); + pub const StakingUpdateFrequency: BlockNumber = 14_400; // Update staking total Once per day + pub StakingInterestRatePerUpdate: Permill = Permill::from_rational(383u32, 1_000_000u32); // 1.15^(1/365) = 1.000383 } impl module_homa_lite::Config for Runtime { type Event = Event; @@ -1576,6 +1578,8 @@ impl module_homa_lite::Config for Runtime { type MaximumRedeemRequestMatchesForMint = MaximumRedeemRequestMatchesForMint; type RelayChainUnbondingSlashingSpans = RelayChainUnbondingSlashingSpans; type MaxScheduledUnbonds = MaxScheduledUnbonds; + type StakingUpdateFrequency = StakingUpdateFrequency; + type StakingInterestRatePerUpdate = StakingInterestRatePerUpdate; } pub type LocalAssetTransactor = MultiCurrencyAdapter< diff --git a/runtime/karura/src/weights/module_homa_lite.rs b/runtime/karura/src/weights/module_homa_lite.rs index abd23f1f5..ee8e823fe 100644 --- a/runtime/karura/src/weights/module_homa_lite.rs +++ b/runtime/karura/src/weights/module_homa_lite.rs @@ -19,7 +19,7 @@ //! Autogenerated weights for module_homa_lite //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2021-10-04, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! DATE: 2021-10-27, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` //! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("karura-dev"), DB CACHE: 128 // Executed Command: @@ -28,7 +28,7 @@ // --chain=karura-dev // --steps=50 // --repeat=20 -// --pallet=module_homa_lite +// --pallet=module-homa-lite // --extrinsic=* // --execution=wasm // --wasm-execution=compiled @@ -48,49 +48,54 @@ use sp_std::marker::PhantomData; pub struct WeightInfo(PhantomData); impl module_homa_lite::WeightInfo for WeightInfo { fn on_idle() -> Weight { - (63_233_000 as Weight) + (33_000_000 as Weight) .saturating_add(T::DbWeight::get().reads(8 as Weight)) .saturating_add(T::DbWeight::get().writes(3 as Weight)) } + fn on_initialize() -> Weight { + (12_000_000 as Weight) + .saturating_add(T::DbWeight::get().reads(1 as Weight)) + .saturating_add(T::DbWeight::get().writes(1 as Weight)) + } fn mint() -> Weight { - (238_339_000 as Weight) + (136_000_000 as Weight) .saturating_add(T::DbWeight::get().reads(14 as Weight)) .saturating_add(T::DbWeight::get().writes(7 as Weight)) } fn mint_for_requests() -> Weight { - (254_671_000 as Weight) + (142_000_000 as Weight) .saturating_add(T::DbWeight::get().reads(16 as Weight)) .saturating_add(T::DbWeight::get().writes(7 as Weight)) } fn set_total_staking_currency() -> Weight { - (18_892_000 as Weight) + (11_000_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn adjust_total_staking_currency() -> Weight { - (21_634_000 as Weight) + (12_000_000 as Weight) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn set_minting_cap() -> Weight { - (19_501_000 as Weight) + (11_000_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn set_xcm_dest_weight() -> Weight { - (18_739_000 as Weight) + (11_000_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn request_redeem() -> Weight { - (136_967_000 as Weight) + (82_000_000 as Weight) .saturating_add(T::DbWeight::get().reads(7 as Weight)) - .saturating_add(T::DbWeight::get().writes(3 as Weight)) + .saturating_add(T::DbWeight::get().writes(4 as Weight)) } fn schedule_unbond() -> Weight { - (21_197_000 as Weight) + (14_000_000 as Weight) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn replace_schedule_unbond() -> Weight { - (19_289_000 as Weight) + (11_000_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } } diff --git a/runtime/mandala/src/lib.rs b/runtime/mandala/src/lib.rs index e5127b4ae..43688e6eb 100644 --- a/runtime/mandala/src/lib.rs +++ b/runtime/mandala/src/lib.rs @@ -1294,6 +1294,8 @@ parameter_types! { // Calculated from polkadot/xcm/xcm-builder: fn buy_weight // This is a place holder value since XCM is not tested for Mandala yet. pub XcmUnbondFee: Balance = 60 * millicent(DOT); + pub const StakingUpdateFrequency: BlockNumber = 14_400; // Update staking total Once per day + pub StakingInterestRatePerUpdate: Permill = Permill::from_rational(383u32, 1_000_000u32); // 1.15^(1/365) = 1.000383 } impl module_homa_lite::Config for Runtime { type Event = Event; @@ -1318,6 +1320,8 @@ impl module_homa_lite::Config for Runtime { type MaximumRedeemRequestMatchesForMint = MaximumRedeemRequestMatchesForMint; type RelayChainUnbondingSlashingSpans = RelayChainUnbondingSlashingSpans; type MaxScheduledUnbonds = MaxScheduledUnbonds; + type StakingUpdateFrequency = StakingUpdateFrequency; + type StakingInterestRatePerUpdate = StakingInterestRatePerUpdate; } parameter_types! { diff --git a/runtime/mandala/src/weights/module_homa_lite.rs b/runtime/mandala/src/weights/module_homa_lite.rs index db4bcf179..d8aaa37b9 100644 --- a/runtime/mandala/src/weights/module_homa_lite.rs +++ b/runtime/mandala/src/weights/module_homa_lite.rs @@ -19,7 +19,7 @@ //! Autogenerated weights for module_homa_lite //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2021-10-03, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! DATE: 2021-10-27, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` //! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 128 // Executed Command: @@ -50,13 +50,18 @@ impl module_homa_lite::WeightInfo for WeightInfo { fn on_idle() -> Weight { (0 as Weight) } + fn on_initialize() -> Weight { + (12_000_000 as Weight) + .saturating_add(T::DbWeight::get().reads(1 as Weight)) + .saturating_add(T::DbWeight::get().writes(1 as Weight)) + } fn mint() -> Weight { - (131_000_000 as Weight) + (137_000_000 as Weight) .saturating_add(T::DbWeight::get().reads(14 as Weight)) .saturating_add(T::DbWeight::get().writes(7 as Weight)) } fn mint_for_requests() -> Weight { - (339_000_000 as Weight) + (348_000_000 as Weight) .saturating_add(T::DbWeight::get().reads(30 as Weight)) .saturating_add(T::DbWeight::get().writes(21 as Weight)) } @@ -74,16 +79,16 @@ impl module_homa_lite::WeightInfo for WeightInfo { .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn set_xcm_dest_weight() -> Weight { - (11_000_000 as Weight) + (10_000_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn request_redeem() -> Weight { - (76_000_000 as Weight) + (81_000_000 as Weight) .saturating_add(T::DbWeight::get().reads(7 as Weight)) - .saturating_add(T::DbWeight::get().writes(3 as Weight)) + .saturating_add(T::DbWeight::get().writes(4 as Weight)) } fn schedule_unbond() -> Weight { - (12_000_000 as Weight) + (13_000_000 as Weight) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } From 3b1ecf75ad88a1a35b398fafa7d66ac2a08ac039 Mon Sep 17 00:00:00 2001 From: Roy Yang Date: Wed, 27 Oct 2021 19:07:47 +1300 Subject: [PATCH 02/25] made minor improvement --- modules/homa-lite/src/lib.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/modules/homa-lite/src/lib.rs b/modules/homa-lite/src/lib.rs index b3d21ecf0..ba3aaf06d 100644 --- a/modules/homa-lite/src/lib.rs +++ b/modules/homa-lite/src/lib.rs @@ -280,14 +280,14 @@ pub mod module { // Update the total amount of Staking balance by acrueing the interest periodically. if !T::StakingInterestRatePerUpdate::get().is_zero() && n % T::StakingUpdateFrequency::get() == Zero::zero() { - let new_total = TotalStakingCurrency::::mutate(|current| { + TotalStakingCurrency::::mutate(|current| { *current = current.saturating_add(T::StakingInterestRatePerUpdate::get().mul(*current)); - *current + Self::deposit_event(Event::::TotalStakingCurrencySet(*current)); }); - Self::deposit_event(Event::::TotalStakingCurrencySet(new_total)); - return ::WeightInfo::on_initialize(); + ::WeightInfo::on_initialize() + } else { + 0 } - 0 } } From a91409e0450c9fff54c0befa4e074644e1952331 Mon Sep 17 00:00:00 2001 From: Roy Yang Date: Thu, 28 Oct 2021 11:49:43 +1300 Subject: [PATCH 03/25] Added an integration test for yielding interest on total_staking_currency --- runtime/integration-tests/src/homa_lite.rs | 51 +++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/runtime/integration-tests/src/homa_lite.rs b/runtime/integration-tests/src/homa_lite.rs index 440c6f20f..7e6ddf52d 100644 --- a/runtime/integration-tests/src/homa_lite.rs +++ b/runtime/integration-tests/src/homa_lite.rs @@ -19,7 +19,7 @@ //! Tests the Homa-lite module, and its cross-chain functionalities. use crate::setup::*; -use frame_support::{assert_noop, assert_ok}; +use frame_support::{assert_noop, assert_ok, traits::Hooks}; use module_support::ExchangeRateProvider; use orml_traits::{MultiCurrency, MultiReservableCurrency}; @@ -305,6 +305,55 @@ fn homa_lite_mint_and_redeem() { }); } +#[test] +fn liquid_value_goes_up_periodically() { + ExtBuilder::default() + .balances(vec![(alice(), LIQUID_CURRENCY, 10_000_000 * dollar(LIQUID_CURRENCY))]) + .build() + .execute_with(|| { + assert_ok!(HomaLite::set_total_staking_currency( + Origin::root(), + 1_000_000 * dollar(RELAY_CHAIN_CURRENCY) + )); + + let rate1 = HomaLite::get_exchange_rate(); + + HomaLite::on_initialize(0); + // Inflate by 1.000383 every 1 day (14400 blocks) + // 1_000_000 * 1.000383 = 1_000_383 + assert_eq!( + HomaLite::total_staking_currency(), + 1_000_383 * dollar(RELAY_CHAIN_CURRENCY) + ); + let rate2 = HomaLite::get_exchange_rate(); + assert!(rate2 > rate1); + + for i in 1..14401 { + HomaLite::on_initialize(i); + } + // 1_000_383 * 1.000383 = 1000766.14669 (with rounding error) + #[cfg(any(feature = "with-mandala-runtime", feature = "with-acala-runtime"))] + assert_eq!(HomaLite::total_staking_currency(), 10_007_661_466_890_000); + #[cfg(feature = "with-karura-runtime")] + assert_eq!(HomaLite::total_staking_currency(), 1_000_766_146_689_000_000); + + let rate3 = HomaLite::get_exchange_rate(); + assert!(rate3 > rate2); + + for i in 14401..28802 { + HomaLite::on_initialize(i); + } + // 1000766.146689 * 1.000383 = 1.001149440123181887 + #[cfg(any(feature = "with-mandala-runtime", feature = "with-acala-runtime"))] + assert_eq!(HomaLite::total_staking_currency(), 10_011_494_401_231_819); + #[cfg(feature = "with-karura-runtime")] + assert_eq!(HomaLite::total_staking_currency(), 1_001_149_440_123_181_887); + + let rate4 = HomaLite::get_exchange_rate(); + assert!(rate4 > rate3); + }); +} + #[cfg(feature = "with-karura-runtime")] mod karura_only_tests { use crate::relaychain::kusama_test_net::*; From e0db3996fa090fa279ef178a1919d5a5c97de882 Mon Sep 17 00:00:00 2001 From: Acala Benchmarking Bot Date: Thu, 28 Oct 2021 00:32:41 +0000 Subject: [PATCH 04/25] cargo run --release --color=never --bin=acala --features=runtime-benchmarks --features=with-mandala-runtime -- benchmark --chain=dev --steps=50 --repeat=20 --pallet=module_homa_lite --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./modules/homa-lite/src/weights.rs --template=./templates/module-weight-template.hbs --- modules/homa-lite/src/weights.rs | 54 ++++++++++++++++---------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/modules/homa-lite/src/weights.rs b/modules/homa-lite/src/weights.rs index e3e01f076..d8f7bb8aa 100644 --- a/modules/homa-lite/src/weights.rs +++ b/modules/homa-lite/src/weights.rs @@ -19,7 +19,7 @@ //! Autogenerated weights for module_homa_lite //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2021-10-27, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! DATE: 2021-10-28, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` //! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 128 // Executed Command: @@ -28,13 +28,13 @@ // --chain=dev // --steps=50 // --repeat=20 -// --pallet=module-homa-lite +// --pallet=module_homa_lite // --extrinsic=* // --execution=wasm // --wasm-execution=compiled // --heap-pages=4096 -// --template=./templates/module-weight-template.hbs // --output=./modules/homa-lite/src/weights.rs +// --template=./templates/module-weight-template.hbs #![cfg_attr(rustfmt, rustfmt_skip)] @@ -64,54 +64,54 @@ pub trait WeightInfo { pub struct AcalaWeight(PhantomData); impl WeightInfo for AcalaWeight { fn on_idle() -> Weight { - (254_000_000 as Weight) + (468_466_000 as Weight) .saturating_add(T::DbWeight::get().reads(23 as Weight)) - .saturating_add(T::DbWeight::get().writes(17 as Weight)) + .saturating_add(T::DbWeight::get().writes(18 as Weight)) } fn on_initialize() -> Weight { - (12_000_000 as Weight) + (20_473_000 as Weight) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn mint() -> Weight { - (139_000_000 as Weight) + (251_043_000 as Weight) .saturating_add(T::DbWeight::get().reads(14 as Weight)) .saturating_add(T::DbWeight::get().writes(7 as Weight)) } fn mint_for_requests() -> Weight { - (351_000_000 as Weight) + (633_220_000 as Weight) .saturating_add(T::DbWeight::get().reads(30 as Weight)) .saturating_add(T::DbWeight::get().writes(21 as Weight)) } fn set_total_staking_currency() -> Weight { - (11_000_000 as Weight) + (19_001_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn adjust_total_staking_currency() -> Weight { - (13_000_000 as Weight) + (21_490_000 as Weight) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn set_minting_cap() -> Weight { - (11_000_000 as Weight) + (19_226_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn set_xcm_dest_weight() -> Weight { - (11_000_000 as Weight) + (18_840_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn request_redeem() -> Weight { - (84_000_000 as Weight) + (155_885_000 as Weight) .saturating_add(T::DbWeight::get().reads(7 as Weight)) .saturating_add(T::DbWeight::get().writes(4 as Weight)) } fn schedule_unbond() -> Weight { - (14_000_000 as Weight) + (42_839_000 as Weight) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn replace_schedule_unbond() -> Weight { - (11_000_000 as Weight) + (20_012_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } } @@ -119,54 +119,54 @@ impl WeightInfo for AcalaWeight { // For backwards compatibility and tests impl WeightInfo for () { fn on_idle() -> Weight { - (254_000_000 as Weight) + (468_466_000 as Weight) .saturating_add(RocksDbWeight::get().reads(23 as Weight)) - .saturating_add(RocksDbWeight::get().writes(17 as Weight)) + .saturating_add(RocksDbWeight::get().writes(18 as Weight)) } fn on_initialize() -> Weight { - (12_000_000 as Weight) + (20_473_000 as Weight) .saturating_add(RocksDbWeight::get().reads(1 as Weight)) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } fn mint() -> Weight { - (139_000_000 as Weight) + (251_043_000 as Weight) .saturating_add(RocksDbWeight::get().reads(14 as Weight)) .saturating_add(RocksDbWeight::get().writes(7 as Weight)) } fn mint_for_requests() -> Weight { - (351_000_000 as Weight) + (633_220_000 as Weight) .saturating_add(RocksDbWeight::get().reads(30 as Weight)) .saturating_add(RocksDbWeight::get().writes(21 as Weight)) } fn set_total_staking_currency() -> Weight { - (11_000_000 as Weight) + (19_001_000 as Weight) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } fn adjust_total_staking_currency() -> Weight { - (13_000_000 as Weight) + (21_490_000 as Weight) .saturating_add(RocksDbWeight::get().reads(1 as Weight)) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } fn set_minting_cap() -> Weight { - (11_000_000 as Weight) + (19_226_000 as Weight) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } fn set_xcm_dest_weight() -> Weight { - (11_000_000 as Weight) + (18_840_000 as Weight) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } fn request_redeem() -> Weight { - (84_000_000 as Weight) + (155_885_000 as Weight) .saturating_add(RocksDbWeight::get().reads(7 as Weight)) .saturating_add(RocksDbWeight::get().writes(4 as Weight)) } fn schedule_unbond() -> Weight { - (14_000_000 as Weight) + (42_839_000 as Weight) .saturating_add(RocksDbWeight::get().reads(1 as Weight)) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } fn replace_schedule_unbond() -> Weight { - (11_000_000 as Weight) + (20_012_000 as Weight) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } } From d797e8386909972aea49cf8d32ee35ed733f2153 Mon Sep 17 00:00:00 2001 From: Acala Benchmarking Bot Date: Thu, 28 Oct 2021 00:40:26 +0000 Subject: [PATCH 05/25] cargo run --release --color=never --bin=acala --features=runtime-benchmarks --features=with-mandala-runtime -- benchmark --chain=dev --steps=50 --repeat=20 --pallet=module_homa_lite --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./modules/homa-lite/src/weights.rs --template=./templates/module-weight-template.hbs --- modules/homa-lite/src/weights.rs | 44 ++++++++++++++++---------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/modules/homa-lite/src/weights.rs b/modules/homa-lite/src/weights.rs index d8f7bb8aa..a427ecd54 100644 --- a/modules/homa-lite/src/weights.rs +++ b/modules/homa-lite/src/weights.rs @@ -64,54 +64,54 @@ pub trait WeightInfo { pub struct AcalaWeight(PhantomData); impl WeightInfo for AcalaWeight { fn on_idle() -> Weight { - (468_466_000 as Weight) + (469_486_000 as Weight) .saturating_add(T::DbWeight::get().reads(23 as Weight)) .saturating_add(T::DbWeight::get().writes(18 as Weight)) } fn on_initialize() -> Weight { - (20_473_000 as Weight) + (20_904_000 as Weight) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn mint() -> Weight { - (251_043_000 as Weight) + (252_381_000 as Weight) .saturating_add(T::DbWeight::get().reads(14 as Weight)) .saturating_add(T::DbWeight::get().writes(7 as Weight)) } fn mint_for_requests() -> Weight { - (633_220_000 as Weight) + (631_470_000 as Weight) .saturating_add(T::DbWeight::get().reads(30 as Weight)) .saturating_add(T::DbWeight::get().writes(21 as Weight)) } fn set_total_staking_currency() -> Weight { - (19_001_000 as Weight) + (19_327_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn adjust_total_staking_currency() -> Weight { - (21_490_000 as Weight) + (22_052_000 as Weight) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn set_minting_cap() -> Weight { - (19_226_000 as Weight) + (19_810_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn set_xcm_dest_weight() -> Weight { - (18_840_000 as Weight) + (19_007_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn request_redeem() -> Weight { - (155_885_000 as Weight) + (154_379_000 as Weight) .saturating_add(T::DbWeight::get().reads(7 as Weight)) .saturating_add(T::DbWeight::get().writes(4 as Weight)) } fn schedule_unbond() -> Weight { - (42_839_000 as Weight) + (23_721_000 as Weight) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn replace_schedule_unbond() -> Weight { - (20_012_000 as Weight) + (20_407_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } } @@ -119,54 +119,54 @@ impl WeightInfo for AcalaWeight { // For backwards compatibility and tests impl WeightInfo for () { fn on_idle() -> Weight { - (468_466_000 as Weight) + (469_486_000 as Weight) .saturating_add(RocksDbWeight::get().reads(23 as Weight)) .saturating_add(RocksDbWeight::get().writes(18 as Weight)) } fn on_initialize() -> Weight { - (20_473_000 as Weight) + (20_904_000 as Weight) .saturating_add(RocksDbWeight::get().reads(1 as Weight)) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } fn mint() -> Weight { - (251_043_000 as Weight) + (252_381_000 as Weight) .saturating_add(RocksDbWeight::get().reads(14 as Weight)) .saturating_add(RocksDbWeight::get().writes(7 as Weight)) } fn mint_for_requests() -> Weight { - (633_220_000 as Weight) + (631_470_000 as Weight) .saturating_add(RocksDbWeight::get().reads(30 as Weight)) .saturating_add(RocksDbWeight::get().writes(21 as Weight)) } fn set_total_staking_currency() -> Weight { - (19_001_000 as Weight) + (19_327_000 as Weight) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } fn adjust_total_staking_currency() -> Weight { - (21_490_000 as Weight) + (22_052_000 as Weight) .saturating_add(RocksDbWeight::get().reads(1 as Weight)) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } fn set_minting_cap() -> Weight { - (19_226_000 as Weight) + (19_810_000 as Weight) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } fn set_xcm_dest_weight() -> Weight { - (18_840_000 as Weight) + (19_007_000 as Weight) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } fn request_redeem() -> Weight { - (155_885_000 as Weight) + (154_379_000 as Weight) .saturating_add(RocksDbWeight::get().reads(7 as Weight)) .saturating_add(RocksDbWeight::get().writes(4 as Weight)) } fn schedule_unbond() -> Weight { - (42_839_000 as Weight) + (23_721_000 as Weight) .saturating_add(RocksDbWeight::get().reads(1 as Weight)) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } fn replace_schedule_unbond() -> Weight { - (20_012_000 as Weight) + (20_407_000 as Weight) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } } From 1b2ab1bddddc32653d77f794591ae7076b053d46 Mon Sep 17 00:00:00 2001 From: Acala Benchmarking Bot Date: Thu, 28 Oct 2021 00:48:14 +0000 Subject: [PATCH 06/25] cargo run --release --color=never --bin=acala --features=runtime-benchmarks --features=with-mandala-runtime -- benchmark --chain=dev --steps=50 --repeat=20 --pallet=module_homa_lite --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./modules/homa-lite/src/weights.rs --template=./templates/module-weight-template.hbs --- modules/homa-lite/src/weights.rs | 44 ++++++++++++++++---------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/modules/homa-lite/src/weights.rs b/modules/homa-lite/src/weights.rs index a427ecd54..dac8aa7c3 100644 --- a/modules/homa-lite/src/weights.rs +++ b/modules/homa-lite/src/weights.rs @@ -64,54 +64,54 @@ pub trait WeightInfo { pub struct AcalaWeight(PhantomData); impl WeightInfo for AcalaWeight { fn on_idle() -> Weight { - (469_486_000 as Weight) + (463_757_000 as Weight) .saturating_add(T::DbWeight::get().reads(23 as Weight)) .saturating_add(T::DbWeight::get().writes(18 as Weight)) } fn on_initialize() -> Weight { - (20_904_000 as Weight) + (20_641_000 as Weight) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn mint() -> Weight { - (252_381_000 as Weight) + (249_560_000 as Weight) .saturating_add(T::DbWeight::get().reads(14 as Weight)) .saturating_add(T::DbWeight::get().writes(7 as Weight)) } fn mint_for_requests() -> Weight { - (631_470_000 as Weight) + (628_871_000 as Weight) .saturating_add(T::DbWeight::get().reads(30 as Weight)) .saturating_add(T::DbWeight::get().writes(21 as Weight)) } fn set_total_staking_currency() -> Weight { - (19_327_000 as Weight) + (19_059_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn adjust_total_staking_currency() -> Weight { - (22_052_000 as Weight) + (21_797_000 as Weight) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn set_minting_cap() -> Weight { - (19_810_000 as Weight) + (19_578_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn set_xcm_dest_weight() -> Weight { - (19_007_000 as Weight) + (18_928_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn request_redeem() -> Weight { - (154_379_000 as Weight) + (152_142_000 as Weight) .saturating_add(T::DbWeight::get().reads(7 as Weight)) .saturating_add(T::DbWeight::get().writes(4 as Weight)) } fn schedule_unbond() -> Weight { - (23_721_000 as Weight) + (23_344_000 as Weight) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn replace_schedule_unbond() -> Weight { - (20_407_000 as Weight) + (20_300_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } } @@ -119,54 +119,54 @@ impl WeightInfo for AcalaWeight { // For backwards compatibility and tests impl WeightInfo for () { fn on_idle() -> Weight { - (469_486_000 as Weight) + (463_757_000 as Weight) .saturating_add(RocksDbWeight::get().reads(23 as Weight)) .saturating_add(RocksDbWeight::get().writes(18 as Weight)) } fn on_initialize() -> Weight { - (20_904_000 as Weight) + (20_641_000 as Weight) .saturating_add(RocksDbWeight::get().reads(1 as Weight)) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } fn mint() -> Weight { - (252_381_000 as Weight) + (249_560_000 as Weight) .saturating_add(RocksDbWeight::get().reads(14 as Weight)) .saturating_add(RocksDbWeight::get().writes(7 as Weight)) } fn mint_for_requests() -> Weight { - (631_470_000 as Weight) + (628_871_000 as Weight) .saturating_add(RocksDbWeight::get().reads(30 as Weight)) .saturating_add(RocksDbWeight::get().writes(21 as Weight)) } fn set_total_staking_currency() -> Weight { - (19_327_000 as Weight) + (19_059_000 as Weight) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } fn adjust_total_staking_currency() -> Weight { - (22_052_000 as Weight) + (21_797_000 as Weight) .saturating_add(RocksDbWeight::get().reads(1 as Weight)) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } fn set_minting_cap() -> Weight { - (19_810_000 as Weight) + (19_578_000 as Weight) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } fn set_xcm_dest_weight() -> Weight { - (19_007_000 as Weight) + (18_928_000 as Weight) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } fn request_redeem() -> Weight { - (154_379_000 as Weight) + (152_142_000 as Weight) .saturating_add(RocksDbWeight::get().reads(7 as Weight)) .saturating_add(RocksDbWeight::get().writes(4 as Weight)) } fn schedule_unbond() -> Weight { - (23_721_000 as Weight) + (23_344_000 as Weight) .saturating_add(RocksDbWeight::get().reads(1 as Weight)) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } fn replace_schedule_unbond() -> Weight { - (20_407_000 as Weight) + (20_300_000 as Weight) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } } From c0ad7b2334f94756714e70c9a87102a5306e767a Mon Sep 17 00:00:00 2001 From: Acala Benchmarking Bot Date: Thu, 28 Oct 2021 00:56:34 +0000 Subject: [PATCH 07/25] cargo run --release --color=never --bin=acala --features=runtime-benchmarks --features=with-karura-runtime -- benchmark --chain=karura-dev --steps=50 --repeat=20 --pallet=module_homa_lite --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --template=./templates/runtime-weight-template.hbs --output=./runtime/karura/src/weights/ --- .../karura/src/weights/module_homa_lite.rs | 38 +++++++++---------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/runtime/karura/src/weights/module_homa_lite.rs b/runtime/karura/src/weights/module_homa_lite.rs index ee8e823fe..487169c08 100644 --- a/runtime/karura/src/weights/module_homa_lite.rs +++ b/runtime/karura/src/weights/module_homa_lite.rs @@ -19,7 +19,7 @@ //! Autogenerated weights for module_homa_lite //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2021-10-27, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! DATE: 2021-10-28, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` //! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("karura-dev"), DB CACHE: 128 // Executed Command: @@ -28,7 +28,7 @@ // --chain=karura-dev // --steps=50 // --repeat=20 -// --pallet=module-homa-lite +// --pallet=module_homa_lite // --extrinsic=* // --execution=wasm // --wasm-execution=compiled @@ -48,54 +48,54 @@ use sp_std::marker::PhantomData; pub struct WeightInfo(PhantomData); impl module_homa_lite::WeightInfo for WeightInfo { fn on_idle() -> Weight { - (33_000_000 as Weight) - .saturating_add(T::DbWeight::get().reads(8 as Weight)) - .saturating_add(T::DbWeight::get().writes(3 as Weight)) + (82_914_000 as Weight) + .saturating_add(T::DbWeight::get().reads(11 as Weight)) + .saturating_add(T::DbWeight::get().writes(4 as Weight)) } fn on_initialize() -> Weight { - (12_000_000 as Weight) + (21_231_000 as Weight) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn mint() -> Weight { - (136_000_000 as Weight) - .saturating_add(T::DbWeight::get().reads(14 as Weight)) - .saturating_add(T::DbWeight::get().writes(7 as Weight)) + (304_380_000 as Weight) + .saturating_add(T::DbWeight::get().reads(17 as Weight)) + .saturating_add(T::DbWeight::get().writes(8 as Weight)) } fn mint_for_requests() -> Weight { - (142_000_000 as Weight) - .saturating_add(T::DbWeight::get().reads(16 as Weight)) - .saturating_add(T::DbWeight::get().writes(7 as Weight)) + (319_431_000 as Weight) + .saturating_add(T::DbWeight::get().reads(19 as Weight)) + .saturating_add(T::DbWeight::get().writes(8 as Weight)) } fn set_total_staking_currency() -> Weight { - (11_000_000 as Weight) + (19_514_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn adjust_total_staking_currency() -> Weight { - (12_000_000 as Weight) + (22_034_000 as Weight) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn set_minting_cap() -> Weight { - (11_000_000 as Weight) + (20_202_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn set_xcm_dest_weight() -> Weight { - (11_000_000 as Weight) + (19_264_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn request_redeem() -> Weight { - (82_000_000 as Weight) + (152_610_000 as Weight) .saturating_add(T::DbWeight::get().reads(7 as Weight)) .saturating_add(T::DbWeight::get().writes(4 as Weight)) } fn schedule_unbond() -> Weight { - (14_000_000 as Weight) + (24_186_000 as Weight) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn replace_schedule_unbond() -> Weight { - (11_000_000 as Weight) + (20_937_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } } From ed1cdc262a828a1bae58bfd8fd2b67692d9406c4 Mon Sep 17 00:00:00 2001 From: Roy Yang Date: Fri, 29 Oct 2021 16:25:58 +1300 Subject: [PATCH 08/25] Added helper function to ensure event is emited when TotalStakingCurrency is updated Update unit tests Changed StakingInterestRatePerUpdateSet from Config to storage, changeable via governance extrinsic calls. --- modules/homa-lite/src/benchmarking.rs | 9 +++ modules/homa-lite/src/lib.rs | 73 ++++++++++++----- modules/homa-lite/src/mock.rs | 2 - modules/homa-lite/src/tests.rs | 81 +++++++++++++++---- modules/homa-lite/src/weights.rs | 9 +++ runtime/acala/src/lib.rs | 2 - runtime/acala/src/weights/module_homa_lite.rs | 4 + runtime/integration-tests/src/homa_lite.rs | 4 + runtime/karura/src/lib.rs | 2 - .../karura/src/weights/module_homa_lite.rs | 4 + runtime/mandala/src/lib.rs | 2 - .../mandala/src/weights/module_homa_lite.rs | 4 + 12 files changed, 150 insertions(+), 46 deletions(-) diff --git a/modules/homa-lite/src/benchmarking.rs b/modules/homa-lite/src/benchmarking.rs index 1ddeb92da..948cfac32 100644 --- a/modules/homa-lite/src/benchmarking.rs +++ b/modules/homa-lite/src/benchmarking.rs @@ -96,6 +96,8 @@ benchmarks! { schedule_unbond {}: _(RawOrigin::Root, 1_000_000_000_000, ::BlockNumber::default()) replace_schedule_unbond {}: _(RawOrigin::Root, vec![(1_000_000, ::BlockNumber::default()), (1_000_000_000, ::BlockNumber::default())]) + + set_staking_interest_rate_per_update {}: _(RawOrigin::Root, Permill::default()) } #[cfg(test)] @@ -171,4 +173,11 @@ mod tests { assert_ok!(Pallet::::test_benchmark_replace_schedule_unbond()); }); } + + #[test] + fn test_set_staking_interest_rate_per_update() { + ExtBuilder::default().build().execute_with(|| { + assert_ok!(Pallet::::test_benchmark_set_staking_interest_rate_per_update()); + }); + } } diff --git a/modules/homa-lite/src/lib.rs b/modules/homa-lite/src/lib.rs index ba3aaf06d..81a3a6ae4 100644 --- a/modules/homa-lite/src/lib.rs +++ b/modules/homa-lite/src/lib.rs @@ -142,10 +142,6 @@ pub mod module { /// The number of blocks to pass before TotalStakingCurrency is updated. #[pallet::constant] type StakingUpdateFrequency: Get; - - /// Interest rate per update - #[pallet::constant] - type StakingInterestRatePerUpdate: Get; } #[pallet::error] @@ -207,6 +203,9 @@ pub mod module { /// The scheduled Unbond has been withdrew from the RelayChain. ///\[staking_amount_added\] ScheduledUnbondWithdrew(Balance), + + /// Interest rate for TotalStakingCurrency is set + StakingInterestRatePerUpdateSet(Permill), } /// The total amount of the staking currency on the relaychain. @@ -247,6 +246,12 @@ pub mod module { pub type ScheduledUnbond = StorageValue<_, BoundedVec<(Balance, RelayChainBlockNumberOf), T::MaxScheduledUnbonds>, ValueQuery>; + /// Every T::StakingUpdateFrequency blocks, TotalStakingCurrency gain interest by this rate. + /// StakingInterestRatePerUpdate: Value: Permill + #[pallet::storage] + #[pallet::getter(fn staking_interest_rate_per_update)] + pub type StakingInterestRatePerUpdate = StorageValue<_, Permill, ValueQuery>; + #[pallet::pallet] pub struct Pallet(_); @@ -278,12 +283,13 @@ pub mod module { fn on_initialize(n: T::BlockNumber) -> Weight { // Update the total amount of Staking balance by acrueing the interest periodically. - if !T::StakingInterestRatePerUpdate::get().is_zero() && n % T::StakingUpdateFrequency::get() == Zero::zero() + if !Self::staking_interest_rate_per_update().is_zero() + && !T::StakingUpdateFrequency::get().is_zero() + && n % T::StakingUpdateFrequency::get() == Zero::zero() { - TotalStakingCurrency::::mutate(|current| { - *current = current.saturating_add(T::StakingInterestRatePerUpdate::get().mul(*current)); - Self::deposit_event(Event::::TotalStakingCurrencySet(*current)); - }); + let current = Self::total_staking_currency(); + let new_total = current.saturating_add(Self::staking_interest_rate_per_update().mul(current)); + let _ = Self::update_total_staking_currency_storage(new_total); ::WeightInfo::on_initialize() } else { 0 @@ -322,12 +328,7 @@ pub mod module { #[transactional] pub fn set_total_staking_currency(origin: OriginFor, staking_total: Balance) -> DispatchResult { T::GovernanceOrigin::ensure_origin(origin)?; - ensure!(!staking_total.is_zero(), Error::::InvalidTotalStakingCurrency); - - TotalStakingCurrency::::put(staking_total); - Self::deposit_event(Event::::TotalStakingCurrencySet(staking_total)); - - Ok(()) + Self::update_total_staking_currency_storage(staking_total) } /// Adjusts the total_staking_currency by the given difference. @@ -362,10 +363,7 @@ pub mod module { .ok_or(ArithmeticError::Underflow)?; } - TotalStakingCurrency::::put(current_staking_total); - Self::deposit_event(Event::::TotalStakingCurrencySet(current_staking_total)); - - Ok(()) + Self::update_total_staking_currency_storage(current_staking_total) } /// Updates the cap for how much Staking currency can be used to Mint liquid currency. @@ -589,6 +587,25 @@ pub mod module { Ok(()) } + + /// Set the interest rate for TotalStakingCurrency. + /// TotakStakingCurrency is incremented every `T::StakingUpdateFrequency` blocks + /// + /// Requires `T::GovernanceOrigin` + /// + /// Parameters: + /// - `interest_rate`: the new interest rate for TotalStakingCurrency. + #[pallet::weight(< T as Config >::WeightInfo::set_staking_interest_rate_per_update())] + #[transactional] + pub fn set_staking_interest_rate_per_update(origin: OriginFor, interest_rate: Permill) -> DispatchResult { + T::GovernanceOrigin::ensure_origin(origin)?; + + StakingInterestRatePerUpdate::::put(interest_rate); + + Self::deposit_event(Event::::StakingInterestRatePerUpdateSet(interest_rate)); + + Ok(()) + } } impl Pallet { @@ -773,7 +790,7 @@ pub mod module { Error::::ExceededStakingCurrencyMintCap ); - TotalStakingCurrency::::put(new_total_staking_currency); + Self::update_total_staking_currency_storage(new_total_staking_currency)?; // All checks pass. Proceed with Xcm transfer. T::XcmTransfer::transfer( @@ -811,6 +828,7 @@ pub mod module { let mut available_staking_balance = Self::available_staking_balance() .checked_add(staking_amount) .ok_or(ArithmeticError::Overflow)?; + let mut total_staking_currency = Self::total_staking_currency(); for (redeemer, (request_amount, extra_fee)) in RedeemRequests::::iter() { // If all the currencies are minted, return. if available_staking_balance.is_zero() { @@ -822,7 +840,8 @@ pub mod module { ); let actual_staking_amount = Self::convert_liquid_to_staking(actual_liquid_amount)?; - TotalStakingCurrency::::mutate(|x| *x = x.saturating_sub(actual_staking_amount)); + total_staking_currency = total_staking_currency.saturating_sub(actual_staking_amount); + Self::update_total_staking_currency_storage(total_staking_currency)?; // Redeem from the available_staking_balances costs only the xcm unbond fee. T::Currency::deposit( @@ -890,6 +909,18 @@ pub mod module { Self::xcm_dest_weight(), ) } + + /// Helper function that update the storage with the new + fn update_total_staking_currency_storage(new_total: Balance) -> DispatchResult { + ensure!(!new_total.is_zero(), Error::::InvalidTotalStakingCurrency); + + // Update storage and emit event. + TotalStakingCurrency::::mutate(|current| { + *current = new_total; + Self::deposit_event(Event::::TotalStakingCurrencySet(*current)); + }); + Ok(()) + } } impl ExchangeRateProvider for Pallet { diff --git a/modules/homa-lite/src/mock.rs b/modules/homa-lite/src/mock.rs index 350910130..16c11dc26 100644 --- a/modules/homa-lite/src/mock.rs +++ b/modules/homa-lite/src/mock.rs @@ -271,7 +271,6 @@ parameter_types! { pub const SubAccountIndex: u16 = 0; pub ParachainId: ParaId = ParaId::from(PARACHAIN_ID); pub const StakingUpdateFrequency: BlockNumber = 100; - pub StakingInterestRatePerUpdate: Permill = Permill::from_rational(1u32, 100u32); } ord_parameter_types! { pub const Root: AccountId = ROOT; @@ -309,7 +308,6 @@ impl Config for Runtime { type RelayChainUnbondingSlashingSpans = RelayChainUnbondingSlashingSpans; type MaxScheduledUnbonds = MaxScheduledUnbonds; type StakingUpdateFrequency = StakingUpdateFrequency; - type StakingInterestRatePerUpdate = StakingInterestRatePerUpdate; } type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; diff --git a/modules/homa-lite/src/tests.rs b/modules/homa-lite/src/tests.rs index 1089d678c..e7178f9f7 100644 --- a/modules/homa-lite/src/tests.rs +++ b/modules/homa-lite/src/tests.rs @@ -374,6 +374,11 @@ fn on_idle_can_process_xcm_to_increase_available_staking_balance() { #[test] fn new_available_staking_currency_can_handle_redeem_requests() { ExtBuilder::default().build().execute_with(|| { + assert_ok!(HomaLite::set_total_staking_currency( + Origin::root(), + Currencies::total_issuance(LKSM) / 10 + )); + assert_ok!(HomaLite::replace_schedule_unbond( Origin::root(), vec![(dollar(1_000), 1)], @@ -398,13 +403,13 @@ fn new_available_staking_currency_can_handle_redeem_requests() { HomaLite::on_idle(MockRelayBlockNumberProvider::get(), 5_000_000_000); // All available staking currency should be redeemed, paying the `XcmUnbondFee` - assert_eq!(AvailableStakingBalance::::get(), 0); - assert_eq!(Currencies::free_balance(KSM, &ROOT), dollar(999)); + assert_eq!(AvailableStakingBalance::::get(), 1); // rounding error + assert_eq!(Currencies::free_balance(KSM, &ROOT), dollar(999) - 1); // rounding error assert_eq!(Currencies::free_balance(LKSM, &ROOT), dollar(989_000)); - assert_eq!(Currencies::reserved_balance(LKSM, &ROOT), dollar(989)); + assert_eq!(Currencies::reserved_balance(LKSM, &ROOT), dollar(98911) / 100); assert_eq!( RedeemRequests::::get(&ROOT), - Some((dollar(989), Permill::zero())) + Some((dollar(98911) / 100, Permill::zero())) ); // Add more staking currency to fully satify the last redeem request @@ -416,8 +421,8 @@ fn new_available_staking_currency_can_handle_redeem_requests() { HomaLite::on_idle(MockRelayBlockNumberProvider::get(), 5_000_000_000); // The last request is redeemed, the leftover is stored. - assert_eq!(AvailableStakingBalance::::get(), 51_100_000_000_000); - assert_eq!(Currencies::free_balance(KSM, &ROOT), 1_096_900_000_000_000); + assert_eq!(AvailableStakingBalance::::get(), 51_087_911_967_033); + assert_eq!(Currencies::free_balance(KSM, &ROOT), 1_096_912_088_032_967); assert_eq!(Currencies::free_balance(LKSM, &ROOT), dollar(989_000)); assert_eq!(Currencies::reserved_balance(LKSM, &ROOT), dollar(0)); assert_eq!(RedeemRequests::::get(&ROOT), None); @@ -429,6 +434,11 @@ fn new_available_staking_currency_can_handle_redeem_requests() { #[test] fn on_idle_can_handle_changes_in_exchange_rate() { ExtBuilder::default().build().execute_with(|| { + assert_ok!(HomaLite::set_total_staking_currency( + Origin::root(), + Currencies::total_issuance(LKSM) / 10 + )); + // When redeem was requested, 100_000 is redeemed to 10_000 staking currency assert_ok!(HomaLite::request_redeem( Origin::signed(ROOT), @@ -551,13 +561,25 @@ fn request_redeem_can_handle_dust_redeem_requests() { // on_idle can handle dust redeem requests #[test] fn on_idle_can_handle_dust_redeem_requests() { - ExtBuilder::default().build().execute_with(|| { - // Test that on_idle doesn't add dust redeem requests into the queue. + ExtBuilder::empty().build().execute_with(|| { + assert_ok!(Currencies::update_balance( + Origin::root(), + ALICE, + LKSM, + dollar(500_501) as i128 + )); + + // This amount will leave a dust after redeem assert_ok!(HomaLite::request_redeem( - Origin::signed(ROOT), - dollar(500_010), + Origin::signed(ALICE), + dollar(500_501), Permill::zero() )); + assert_ok!(HomaLite::set_total_staking_currency( + Origin::root(), + Currencies::total_issuance(LKSM) / 10 + )); + assert_ok!(HomaLite::replace_schedule_unbond( Origin::root(), vec![(dollar(50_000), 2)], @@ -565,11 +587,12 @@ fn on_idle_can_handle_dust_redeem_requests() { MockRelayBlockNumberProvider::set(2); HomaLite::on_idle(MockRelayBlockNumberProvider::get(), 5_000_000_000); - assert_eq!(AvailableStakingBalance::::get(), 49_001_000_000_000); - assert_eq!(Currencies::free_balance(KSM, &ROOT), 49_949_999_000_000_000); - assert_eq!(Currencies::free_balance(LKSM, &ROOT), dollar(499_990)); - assert_eq!(Currencies::reserved_balance(LKSM, &ROOT), 0); - assert_eq!(RedeemRequests::::get(&ROOT), None); + assert_eq!(AvailableStakingBalance::::get(), 0); + assert_eq!(Currencies::free_balance(KSM, &ALICE), dollar(49_999)); + // Dust amount is un-reserved and returned to the user + assert_eq!(Currencies::free_balance(LKSM, &ALICE), 499_000_000_000); + assert_eq!(Currencies::reserved_balance(LKSM, &ALICE), 0); + assert_eq!(RedeemRequests::::get(&ALICE), None); }); } @@ -827,16 +850,40 @@ fn total_staking_currency_update_periodically() { assert_ok!(HomaLite::set_total_staking_currency(Origin::root(), dollar(1_000_000))); HomaLite::on_initialize(0); + // Default inflation rate is 0% + assert_eq!(TotalStakingCurrency::::get(), dollar(1_000_000)); + + for i in 1..101 { + HomaLite::on_initialize(i); + } + assert_eq!(TotalStakingCurrency::::get(), dollar(1_000_000)); + + // Interest rate can only be set by governance + assert_noop!( + HomaLite::set_staking_interest_rate_per_update(Origin::signed(ALICE), Permill::from_percent(1)), + BadOrigin + ); + assert_ok!(HomaLite::set_staking_interest_rate_per_update( + Origin::root(), + Permill::from_percent(1) + )); + System::assert_last_event(Event::HomaLite(crate::Event::StakingInterestRatePerUpdateSet( + Permill::from_percent(1), + ))); + + for i in 101..201 { + HomaLite::on_initialize(i); + } // Inflate by 1%: 1_000_000 * 1.01 assert_eq!(TotalStakingCurrency::::get(), dollar(1_010_000)); - for i in 1..101 { + for i in 201..301 { HomaLite::on_initialize(i); } // 1_010_000 * 1.01 assert_eq!(TotalStakingCurrency::::get(), dollar(1_020_100)); - for i in 101..201 { + for i in 301..401 { HomaLite::on_initialize(i); } //1_020_100 * 1.01 diff --git a/modules/homa-lite/src/weights.rs b/modules/homa-lite/src/weights.rs index dac8aa7c3..a96d690e3 100644 --- a/modules/homa-lite/src/weights.rs +++ b/modules/homa-lite/src/weights.rs @@ -58,6 +58,7 @@ pub trait WeightInfo { fn request_redeem() -> Weight; fn schedule_unbond() -> Weight; fn replace_schedule_unbond() -> Weight; + fn set_staking_interest_rate_per_update() -> Weight; } /// Weights for module_homa_lite using the Acala node and recommended hardware. @@ -114,6 +115,10 @@ impl WeightInfo for AcalaWeight { (20_300_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } + fn set_staking_interest_rate_per_update() -> Weight { + (19_059_000 as Weight) + .saturating_add(T::DbWeight::get().writes(1 as Weight)) + } } // For backwards compatibility and tests @@ -169,4 +174,8 @@ impl WeightInfo for () { (20_300_000 as Weight) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } + fn set_staking_interest_rate_per_update() -> Weight { + (19_059_000 as Weight) + .saturating_add(RocksDbWeight::get().writes(1 as Weight)) + } } diff --git a/runtime/acala/src/lib.rs b/runtime/acala/src/lib.rs index 7dc17cef1..6adba5ea9 100644 --- a/runtime/acala/src/lib.rs +++ b/runtime/acala/src/lib.rs @@ -1534,7 +1534,6 @@ parameter_types! { pub SubAccountIndex: u16 = RelayChainSubAccountId::HomaLite as u16; pub const XcmUnbondFee: Balance = 600_000_000; // TODO identify unbon fee pub const StakingUpdateFrequency: BlockNumber = 14_400; // Update staking total Once per day - pub StakingInterestRatePerUpdate: Permill = Permill::from_rational(383u32, 1_000_000u32); // 1.15^(1/365) = 1.000383 } impl module_homa_lite::Config for Runtime { type Event = Event; @@ -1560,7 +1559,6 @@ impl module_homa_lite::Config for Runtime { type RelayChainUnbondingSlashingSpans = RelayChainUnbondingSlashingSpans; type MaxScheduledUnbonds = MaxScheduledUnbonds; type StakingUpdateFrequency = StakingUpdateFrequency; - type StakingInterestRatePerUpdate = StakingInterestRatePerUpdate; } pub type LocalAssetTransactor = MultiCurrencyAdapter< diff --git a/runtime/acala/src/weights/module_homa_lite.rs b/runtime/acala/src/weights/module_homa_lite.rs index 75f367505..f0de71c35 100644 --- a/runtime/acala/src/weights/module_homa_lite.rs +++ b/runtime/acala/src/weights/module_homa_lite.rs @@ -98,4 +98,8 @@ impl module_homa_lite::WeightInfo for WeightInfo { (11_000_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } + fn set_staking_interest_rate_per_update() -> Weight { + (11_000_000 as Weight) + .saturating_add(T::DbWeight::get().writes(1 as Weight)) + } } diff --git a/runtime/integration-tests/src/homa_lite.rs b/runtime/integration-tests/src/homa_lite.rs index 7e6ddf52d..f2a40e7a3 100644 --- a/runtime/integration-tests/src/homa_lite.rs +++ b/runtime/integration-tests/src/homa_lite.rs @@ -315,6 +315,10 @@ fn liquid_value_goes_up_periodically() { Origin::root(), 1_000_000 * dollar(RELAY_CHAIN_CURRENCY) )); + assert_ok!(HomaLite::set_staking_interest_rate_per_update( + Origin::root(), + Permill::from_rational(383u32, 1_000_000u32) + )); let rate1 = HomaLite::get_exchange_rate(); diff --git a/runtime/karura/src/lib.rs b/runtime/karura/src/lib.rs index f9ad9f43f..aa3059d42 100644 --- a/runtime/karura/src/lib.rs +++ b/runtime/karura/src/lib.rs @@ -1558,7 +1558,6 @@ parameter_types! { // We must charge higher than what Kusama required (533_333_300, obtained from integration test) pub XcmUnbondFee: Balance = 60 * millicent(KSM); pub const StakingUpdateFrequency: BlockNumber = 14_400; // Update staking total Once per day - pub StakingInterestRatePerUpdate: Permill = Permill::from_rational(383u32, 1_000_000u32); // 1.15^(1/365) = 1.000383 } impl module_homa_lite::Config for Runtime { type Event = Event; @@ -1584,7 +1583,6 @@ impl module_homa_lite::Config for Runtime { type RelayChainUnbondingSlashingSpans = RelayChainUnbondingSlashingSpans; type MaxScheduledUnbonds = MaxScheduledUnbonds; type StakingUpdateFrequency = StakingUpdateFrequency; - type StakingInterestRatePerUpdate = StakingInterestRatePerUpdate; } pub type LocalAssetTransactor = MultiCurrencyAdapter< diff --git a/runtime/karura/src/weights/module_homa_lite.rs b/runtime/karura/src/weights/module_homa_lite.rs index 487169c08..4862572f6 100644 --- a/runtime/karura/src/weights/module_homa_lite.rs +++ b/runtime/karura/src/weights/module_homa_lite.rs @@ -98,4 +98,8 @@ impl module_homa_lite::WeightInfo for WeightInfo { (20_937_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } + fn set_staking_interest_rate_per_update() -> Weight { + (19_514_000 as Weight) + .saturating_add(T::DbWeight::get().writes(1 as Weight)) + } } diff --git a/runtime/mandala/src/lib.rs b/runtime/mandala/src/lib.rs index de8ba1ba6..5a1058bdf 100644 --- a/runtime/mandala/src/lib.rs +++ b/runtime/mandala/src/lib.rs @@ -1296,7 +1296,6 @@ parameter_types! { // This is a place holder value since XCM is not tested for Mandala yet. pub XcmUnbondFee: Balance = 60 * millicent(DOT); pub const StakingUpdateFrequency: BlockNumber = 14_400; // Update staking total Once per day - pub StakingInterestRatePerUpdate: Permill = Permill::from_rational(383u32, 1_000_000u32); // 1.15^(1/365) = 1.000383 } impl module_homa_lite::Config for Runtime { type Event = Event; @@ -1322,7 +1321,6 @@ impl module_homa_lite::Config for Runtime { type RelayChainUnbondingSlashingSpans = RelayChainUnbondingSlashingSpans; type MaxScheduledUnbonds = MaxScheduledUnbonds; type StakingUpdateFrequency = StakingUpdateFrequency; - type StakingInterestRatePerUpdate = StakingInterestRatePerUpdate; } parameter_types! { diff --git a/runtime/mandala/src/weights/module_homa_lite.rs b/runtime/mandala/src/weights/module_homa_lite.rs index d8aaa37b9..26c391e82 100644 --- a/runtime/mandala/src/weights/module_homa_lite.rs +++ b/runtime/mandala/src/weights/module_homa_lite.rs @@ -96,4 +96,8 @@ impl module_homa_lite::WeightInfo for WeightInfo { (11_000_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } + fn set_staking_interest_rate_per_update() -> Weight { + (11_000_000 as Weight) + .saturating_add(T::DbWeight::get().writes(1 as Weight)) + } } From 4543d0d161b4cc416645851ec147a5738a7799aa Mon Sep 17 00:00:00 2001 From: Roy Yang Date: Fri, 29 Oct 2021 16:50:13 +1300 Subject: [PATCH 09/25] Add assert to ensure the correct event is emited --- modules/homa-lite/src/tests.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/modules/homa-lite/src/tests.rs b/modules/homa-lite/src/tests.rs index e7178f9f7..8cb3fcae0 100644 --- a/modules/homa-lite/src/tests.rs +++ b/modules/homa-lite/src/tests.rs @@ -876,18 +876,27 @@ fn total_staking_currency_update_periodically() { } // Inflate by 1%: 1_000_000 * 1.01 assert_eq!(TotalStakingCurrency::::get(), dollar(1_010_000)); + System::assert_last_event(Event::HomaLite(crate::Event::TotalStakingCurrencySet(dollar( + 1_010_000, + )))); for i in 201..301 { HomaLite::on_initialize(i); } // 1_010_000 * 1.01 assert_eq!(TotalStakingCurrency::::get(), dollar(1_020_100)); + System::assert_last_event(Event::HomaLite(crate::Event::TotalStakingCurrencySet(dollar( + 1_020_100, + )))); for i in 301..401 { HomaLite::on_initialize(i); } //1_020_100 * 1.01 assert_eq!(TotalStakingCurrency::::get(), dollar(1_030_301)); + System::assert_last_event(Event::HomaLite(crate::Event::TotalStakingCurrencySet(dollar( + 1_030_301, + )))); }); } From 99b15096db11d10a5d5b96409d02917c91c214fe Mon Sep 17 00:00:00 2001 From: Roy Yang Date: Fri, 29 Oct 2021 18:08:41 +1300 Subject: [PATCH 10/25] Fixed benchmark test --- modules/homa-lite/src/benchmarking.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/homa-lite/src/benchmarking.rs b/modules/homa-lite/src/benchmarking.rs index 948cfac32..906d0ae8e 100644 --- a/modules/homa-lite/src/benchmarking.rs +++ b/modules/homa-lite/src/benchmarking.rs @@ -79,7 +79,7 @@ benchmarks! { set_total_staking_currency {}: _(RawOrigin::Root, 1_000_000_000_000) - adjust_total_staking_currency {}: _(RawOrigin::Root, AmountOf::::default()) + adjust_total_staking_currency {}: _(RawOrigin::Root, AmountOf::::max_value()) set_minting_cap { }: _(RawOrigin::Root, 1_000_000_000_000_000_000) From 20053564d73e80475cc58c66d477b2f89eeee545 Mon Sep 17 00:00:00 2001 From: Acala Benchmarking Bot Date: Fri, 29 Oct 2021 05:38:54 +0000 Subject: [PATCH 11/25] cargo run --release --color=never --bin=acala --features=runtime-benchmarks --features=with-mandala-runtime -- benchmark --chain=dev --steps=50 --repeat=20 --pallet=module_homa_lite --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./modules/homa-lite/src/weights.rs --template=./templates/module-weight-template.hbs --- modules/homa-lite/src/weights.rs | 60 +++++++++++++++----------------- 1 file changed, 29 insertions(+), 31 deletions(-) diff --git a/modules/homa-lite/src/weights.rs b/modules/homa-lite/src/weights.rs index a96d690e3..de069c62b 100644 --- a/modules/homa-lite/src/weights.rs +++ b/modules/homa-lite/src/weights.rs @@ -19,7 +19,7 @@ //! Autogenerated weights for module_homa_lite //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2021-10-28, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! DATE: 2021-10-29, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` //! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 128 // Executed Command: @@ -65,58 +65,57 @@ pub trait WeightInfo { pub struct AcalaWeight(PhantomData); impl WeightInfo for AcalaWeight { fn on_idle() -> Weight { - (463_757_000 as Weight) - .saturating_add(T::DbWeight::get().reads(23 as Weight)) - .saturating_add(T::DbWeight::get().writes(18 as Weight)) + (61_325_000 as Weight) + .saturating_add(T::DbWeight::get().reads(11 as Weight)) } fn on_initialize() -> Weight { - (20_641_000 as Weight) + (3_340_000 as Weight) .saturating_add(T::DbWeight::get().reads(1 as Weight)) - .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn mint() -> Weight { - (249_560_000 as Weight) + (245_257_000 as Weight) .saturating_add(T::DbWeight::get().reads(14 as Weight)) .saturating_add(T::DbWeight::get().writes(7 as Weight)) } fn mint_for_requests() -> Weight { - (628_871_000 as Weight) + (602_614_000 as Weight) .saturating_add(T::DbWeight::get().reads(30 as Weight)) .saturating_add(T::DbWeight::get().writes(21 as Weight)) } fn set_total_staking_currency() -> Weight { - (19_059_000 as Weight) + (21_429_000 as Weight) + .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn adjust_total_staking_currency() -> Weight { - (21_797_000 as Weight) + (22_079_000 as Weight) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn set_minting_cap() -> Weight { - (19_578_000 as Weight) + (18_989_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn set_xcm_dest_weight() -> Weight { - (18_928_000 as Weight) + (18_245_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn request_redeem() -> Weight { - (152_142_000 as Weight) + (144_193_000 as Weight) .saturating_add(T::DbWeight::get().reads(7 as Weight)) .saturating_add(T::DbWeight::get().writes(4 as Weight)) } fn schedule_unbond() -> Weight { - (23_344_000 as Weight) + (22_671_000 as Weight) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn replace_schedule_unbond() -> Weight { - (20_300_000 as Weight) + (19_369_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn set_staking_interest_rate_per_update() -> Weight { - (19_059_000 as Weight) + (17_812_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } } @@ -124,58 +123,57 @@ impl WeightInfo for AcalaWeight { // For backwards compatibility and tests impl WeightInfo for () { fn on_idle() -> Weight { - (463_757_000 as Weight) - .saturating_add(RocksDbWeight::get().reads(23 as Weight)) - .saturating_add(RocksDbWeight::get().writes(18 as Weight)) + (61_325_000 as Weight) + .saturating_add(RocksDbWeight::get().reads(11 as Weight)) } fn on_initialize() -> Weight { - (20_641_000 as Weight) + (3_340_000 as Weight) .saturating_add(RocksDbWeight::get().reads(1 as Weight)) - .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } fn mint() -> Weight { - (249_560_000 as Weight) + (245_257_000 as Weight) .saturating_add(RocksDbWeight::get().reads(14 as Weight)) .saturating_add(RocksDbWeight::get().writes(7 as Weight)) } fn mint_for_requests() -> Weight { - (628_871_000 as Weight) + (602_614_000 as Weight) .saturating_add(RocksDbWeight::get().reads(30 as Weight)) .saturating_add(RocksDbWeight::get().writes(21 as Weight)) } fn set_total_staking_currency() -> Weight { - (19_059_000 as Weight) + (21_429_000 as Weight) + .saturating_add(RocksDbWeight::get().reads(1 as Weight)) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } fn adjust_total_staking_currency() -> Weight { - (21_797_000 as Weight) + (22_079_000 as Weight) .saturating_add(RocksDbWeight::get().reads(1 as Weight)) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } fn set_minting_cap() -> Weight { - (19_578_000 as Weight) + (18_989_000 as Weight) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } fn set_xcm_dest_weight() -> Weight { - (18_928_000 as Weight) + (18_245_000 as Weight) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } fn request_redeem() -> Weight { - (152_142_000 as Weight) + (144_193_000 as Weight) .saturating_add(RocksDbWeight::get().reads(7 as Weight)) .saturating_add(RocksDbWeight::get().writes(4 as Weight)) } fn schedule_unbond() -> Weight { - (23_344_000 as Weight) + (22_671_000 as Weight) .saturating_add(RocksDbWeight::get().reads(1 as Weight)) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } fn replace_schedule_unbond() -> Weight { - (20_300_000 as Weight) + (19_369_000 as Weight) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } fn set_staking_interest_rate_per_update() -> Weight { - (19_059_000 as Weight) + (17_812_000 as Weight) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } } From 3c078113fdaf6073d62bcd8ed8f6678138741879 Mon Sep 17 00:00:00 2001 From: Acala Benchmarking Bot Date: Fri, 29 Oct 2021 05:59:12 +0000 Subject: [PATCH 12/25] cargo run --release --color=never --bin=acala --features=runtime-benchmarks --features=with-karura-runtime -- benchmark --chain=karura-dev --steps=50 --repeat=20 --pallet=module_homa_lite --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --template=./templates/runtime-weight-template.hbs --output=./runtime/karura/src/weights/ --- .../karura/src/weights/module_homa_lite.rs | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/runtime/karura/src/weights/module_homa_lite.rs b/runtime/karura/src/weights/module_homa_lite.rs index 4862572f6..b26fde992 100644 --- a/runtime/karura/src/weights/module_homa_lite.rs +++ b/runtime/karura/src/weights/module_homa_lite.rs @@ -19,7 +19,7 @@ //! Autogenerated weights for module_homa_lite //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2021-10-28, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! DATE: 2021-10-29, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` //! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("karura-dev"), DB CACHE: 128 // Executed Command: @@ -48,58 +48,58 @@ use sp_std::marker::PhantomData; pub struct WeightInfo(PhantomData); impl module_homa_lite::WeightInfo for WeightInfo { fn on_idle() -> Weight { - (82_914_000 as Weight) - .saturating_add(T::DbWeight::get().reads(11 as Weight)) + (75_782_000 as Weight) + .saturating_add(T::DbWeight::get().reads(12 as Weight)) .saturating_add(T::DbWeight::get().writes(4 as Weight)) } fn on_initialize() -> Weight { - (21_231_000 as Weight) + (5_959_000 as Weight) .saturating_add(T::DbWeight::get().reads(1 as Weight)) - .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn mint() -> Weight { - (304_380_000 as Weight) + (423_446_000 as Weight) .saturating_add(T::DbWeight::get().reads(17 as Weight)) .saturating_add(T::DbWeight::get().writes(8 as Weight)) } fn mint_for_requests() -> Weight { - (319_431_000 as Weight) + (270_971_000 as Weight) .saturating_add(T::DbWeight::get().reads(19 as Weight)) .saturating_add(T::DbWeight::get().writes(8 as Weight)) } fn set_total_staking_currency() -> Weight { - (19_514_000 as Weight) + (20_863_000 as Weight) + .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn adjust_total_staking_currency() -> Weight { - (22_034_000 as Weight) + (21_761_000 as Weight) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn set_minting_cap() -> Weight { - (20_202_000 as Weight) + (18_646_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn set_xcm_dest_weight() -> Weight { - (19_264_000 as Weight) + (18_046_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn request_redeem() -> Weight { - (152_610_000 as Weight) + (137_950_000 as Weight) .saturating_add(T::DbWeight::get().reads(7 as Weight)) .saturating_add(T::DbWeight::get().writes(4 as Weight)) } fn schedule_unbond() -> Weight { - (24_186_000 as Weight) + (22_342_000 as Weight) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn replace_schedule_unbond() -> Weight { - (20_937_000 as Weight) + (19_468_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn set_staking_interest_rate_per_update() -> Weight { - (19_514_000 as Weight) + (17_718_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } } From 6e3ff15cf35683781a4b219c993c9e5a316d0208 Mon Sep 17 00:00:00 2001 From: Roy Yang Date: Mon, 1 Nov 2021 10:35:54 +1300 Subject: [PATCH 13/25] Made minor improvement to address PR comments --- modules/homa-lite/src/lib.rs | 5 +++-- runtime/acala/src/lib.rs | 4 ++-- runtime/karura/src/lib.rs | 3 +-- runtime/mandala/src/lib.rs | 3 +-- 4 files changed, 7 insertions(+), 8 deletions(-) diff --git a/modules/homa-lite/src/lib.rs b/modules/homa-lite/src/lib.rs index 81a3a6ae4..8ab74b7e1 100644 --- a/modules/homa-lite/src/lib.rs +++ b/modules/homa-lite/src/lib.rs @@ -282,10 +282,11 @@ pub mod module { } fn on_initialize(n: T::BlockNumber) -> Weight { + let update_frequency = T::StakingUpdateFrequency::get(); // Update the total amount of Staking balance by acrueing the interest periodically. if !Self::staking_interest_rate_per_update().is_zero() - && !T::StakingUpdateFrequency::get().is_zero() - && n % T::StakingUpdateFrequency::get() == Zero::zero() + && !update_frequency.is_zero() + && n % update_frequency == Zero::zero() { let current = Self::total_staking_currency(); let new_total = current.saturating_add(Self::staking_interest_rate_per_update().mul(current)); diff --git a/runtime/acala/src/lib.rs b/runtime/acala/src/lib.rs index 7d27bf1b1..156bc9797 100644 --- a/runtime/acala/src/lib.rs +++ b/runtime/acala/src/lib.rs @@ -1532,8 +1532,8 @@ parameter_types! { pub ParachainAccount: AccountId = ParachainInfo::get().into_account(); pub SubAccountIndex: u16 = RelayChainSubAccountId::HomaLite as u16; pub const XcmUnbondFee: Balance = 600_000_000; // TODO identify unbon fee - pub const StakingUpdateFrequency: BlockNumber = 14_400; // Update staking total Once per day } + impl module_homa_lite::Config for Runtime { type Event = Event; type WeightInfo = weights::module_homa_lite::WeightInfo; @@ -1557,7 +1557,7 @@ impl module_homa_lite::Config for Runtime { type MaximumRedeemRequestMatchesForMint = MaximumRedeemRequestMatchesForMint; type RelayChainUnbondingSlashingSpans = RelayChainUnbondingSlashingSpans; type MaxScheduledUnbonds = MaxScheduledUnbonds; - type StakingUpdateFrequency = StakingUpdateFrequency; + type StakingUpdateFrequency = OneDay; } pub type LocalAssetTransactor = MultiCurrencyAdapter< diff --git a/runtime/karura/src/lib.rs b/runtime/karura/src/lib.rs index e14eb3cd2..588b7c742 100644 --- a/runtime/karura/src/lib.rs +++ b/runtime/karura/src/lib.rs @@ -1556,7 +1556,6 @@ parameter_types! { // Calculated from polkadot/xcm/xcm-builder: fn buy_weight // We must charge higher than what Kusama required (533_333_300, obtained from integration test) pub XcmUnbondFee: Balance = 60 * millicent(KSM); - pub const StakingUpdateFrequency: BlockNumber = 14_400; // Update staking total Once per day } impl module_homa_lite::Config for Runtime { type Event = Event; @@ -1581,7 +1580,7 @@ impl module_homa_lite::Config for Runtime { type MaximumRedeemRequestMatchesForMint = MaximumRedeemRequestMatchesForMint; type RelayChainUnbondingSlashingSpans = RelayChainUnbondingSlashingSpans; type MaxScheduledUnbonds = MaxScheduledUnbonds; - type StakingUpdateFrequency = StakingUpdateFrequency; + type StakingUpdateFrequency = OneDay; } pub type LocalAssetTransactor = MultiCurrencyAdapter< diff --git a/runtime/mandala/src/lib.rs b/runtime/mandala/src/lib.rs index bae4784b0..280599c7f 100644 --- a/runtime/mandala/src/lib.rs +++ b/runtime/mandala/src/lib.rs @@ -1294,7 +1294,6 @@ parameter_types! { // Calculated from polkadot/xcm/xcm-builder: fn buy_weight // This is a place holder value since XCM is not tested for Mandala yet. pub XcmUnbondFee: Balance = 60 * millicent(DOT); - pub const StakingUpdateFrequency: BlockNumber = 14_400; // Update staking total Once per day } impl module_homa_lite::Config for Runtime { type Event = Event; @@ -1319,7 +1318,7 @@ impl module_homa_lite::Config for Runtime { type MaximumRedeemRequestMatchesForMint = MaximumRedeemRequestMatchesForMint; type RelayChainUnbondingSlashingSpans = RelayChainUnbondingSlashingSpans; type MaxScheduledUnbonds = MaxScheduledUnbonds; - type StakingUpdateFrequency = StakingUpdateFrequency; + type StakingUpdateFrequency = OneDay; } parameter_types! { From e9d5154be73952d875bf51fea228da1c29dce5fa Mon Sep 17 00:00:00 2001 From: Roy Yang Date: Mon, 1 Nov 2021 11:02:53 +1300 Subject: [PATCH 14/25] Fixed integration test --- runtime/integration-tests/src/homa_lite.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/runtime/integration-tests/src/homa_lite.rs b/runtime/integration-tests/src/homa_lite.rs index e0db0a7fe..6f72792a7 100644 --- a/runtime/integration-tests/src/homa_lite.rs +++ b/runtime/integration-tests/src/homa_lite.rs @@ -346,8 +346,11 @@ fn liquid_value_goes_up_periodically() { // 1_000_383 * 1.000383 = 1000766.14669 (with rounding error) #[cfg(any(feature = "with-mandala-runtime", feature = "with-acala-runtime"))] assert_eq!(HomaLite::total_staking_currency(), 10_007_661_466_890_000); + + // Karura ias 12 sec block time + // 1_000_383 * 1.000383 * 1.000383 = 1001149.440123181887 #[cfg(feature = "with-karura-runtime")] - assert_eq!(HomaLite::total_staking_currency(), 1_000_766_146_689_000_000); + assert_eq!(HomaLite::total_staking_currency(), 1_001_149_440_123_181_887); let rate3 = HomaLite::get_exchange_rate(); assert!(rate3 > rate2); @@ -358,8 +361,10 @@ fn liquid_value_goes_up_periodically() { // 1000766.146689 * 1.000383 = 1.001149440123181887 #[cfg(any(feature = "with-mandala-runtime", feature = "with-acala-runtime"))] assert_eq!(HomaLite::total_staking_currency(), 10_011_494_401_231_819); + + // 1001149.440123181887 * 1.000383 * 1.000383 = 1001916.46745192646655 #[cfg(feature = "with-karura-runtime")] - assert_eq!(HomaLite::total_staking_currency(), 1_001_149_440_123_181_887); + assert_eq!(HomaLite::total_staking_currency(), 1_001_916_467_451_926_467); let rate4 = HomaLite::get_exchange_rate(); assert!(rate4 > rate3); From 70d3014a8ceac1109ef1077c77ab75dbd7940165 Mon Sep 17 00:00:00 2001 From: Roy Yang Date: Mon, 1 Nov 2021 18:35:08 +1300 Subject: [PATCH 15/25] Used checked_rem instead of % for better safety --- Cargo.lock | 1 + modules/homa-lite/Cargo.toml | 2 ++ modules/homa-lite/src/lib.rs | 9 +++++---- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5357a479e..4827b6b2a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5567,6 +5567,7 @@ dependencies = [ "pallet-xcm", "parity-scale-codec", "scale-info", + "sp-arithmetic", "sp-core", "sp-io", "sp-runtime", diff --git a/modules/homa-lite/Cargo.toml b/modules/homa-lite/Cargo.toml index 28fe58c6e..463835ecb 100644 --- a/modules/homa-lite/Cargo.toml +++ b/modules/homa-lite/Cargo.toml @@ -10,6 +10,7 @@ scale-info = { version = "1.0", default-features = false, features = ["derive"] frame-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.12", default-features = false, optional = true} frame-support = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.12", default-features = false } frame-system = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.12", default-features = false } +sp-arithmetic = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.12", default-features = false } sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.12", default-features = false } sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.12", default-features = false } sp-std = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.12", default-features = false } @@ -36,6 +37,7 @@ std = [ "frame-support/std", "frame-system/std", "scale-info/std", + "sp-arithmetic/std", "sp-runtime/std", "sp-core/std", "sp-std/std", diff --git a/modules/homa-lite/src/lib.rs b/modules/homa-lite/src/lib.rs index 8ab74b7e1..300ba04d7 100644 --- a/modules/homa-lite/src/lib.rs +++ b/modules/homa-lite/src/lib.rs @@ -32,8 +32,9 @@ use orml_traits::{ arithmetic::Signed, BalanceStatus, MultiCurrency, MultiCurrencyExtended, MultiReservableCurrency, XcmTransfer, }; use primitives::{Balance, CurrencyId}; +use sp_arithmetic::traits::CheckedRem; use sp_runtime::{ - traits::{BlockNumberProvider, Bounded, Saturating, Zero}, + traits::{BlockNumberProvider, Bounded, One, Saturating, Zero}, ArithmeticError, FixedPointNumber, Permill, }; use sp_std::{ @@ -282,11 +283,11 @@ pub mod module { } fn on_initialize(n: T::BlockNumber) -> Weight { - let update_frequency = T::StakingUpdateFrequency::get(); // Update the total amount of Staking balance by acrueing the interest periodically. if !Self::staking_interest_rate_per_update().is_zero() - && !update_frequency.is_zero() - && n % update_frequency == Zero::zero() + && n.checked_rem(&T::StakingUpdateFrequency::get()) + .unwrap_or(One::one()) + .is_zero() { let current = Self::total_staking_currency(); let new_total = current.saturating_add(Self::staking_interest_rate_per_update().mul(current)); From 604fc16cb2a93f6469619125370463475e5ff1b0 Mon Sep 17 00:00:00 2001 From: Roy Yang Date: Mon, 1 Nov 2021 19:26:48 +1300 Subject: [PATCH 16/25] Fixed a clippy build error --- modules/homa-lite/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/homa-lite/src/lib.rs b/modules/homa-lite/src/lib.rs index 300ba04d7..314075b10 100644 --- a/modules/homa-lite/src/lib.rs +++ b/modules/homa-lite/src/lib.rs @@ -286,7 +286,7 @@ pub mod module { // Update the total amount of Staking balance by acrueing the interest periodically. if !Self::staking_interest_rate_per_update().is_zero() && n.checked_rem(&T::StakingUpdateFrequency::get()) - .unwrap_or(One::one()) + .unwrap_or_else(One::one) .is_zero() { let current = Self::total_staking_currency(); From b9b59cd31c3881637c2bdc230403c4ae31d715ec Mon Sep 17 00:00:00 2001 From: Roy Yang Date: Mon, 1 Nov 2021 20:38:54 +1300 Subject: [PATCH 17/25] Fixed integration tests for acala runtime --- runtime/integration-tests/src/homa_lite.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/runtime/integration-tests/src/homa_lite.rs b/runtime/integration-tests/src/homa_lite.rs index 3e38358ed..0c3c8850e 100644 --- a/runtime/integration-tests/src/homa_lite.rs +++ b/runtime/integration-tests/src/homa_lite.rs @@ -344,13 +344,15 @@ fn liquid_value_goes_up_periodically() { HomaLite::on_initialize(i); } // 1_000_383 * 1.000383 = 1000766.14669 (with rounding error) - #[cfg(any(feature = "with-mandala-runtime", feature = "with-acala-runtime"))] + #[cfg(feature = "with-mandala-runtime")] assert_eq!(HomaLite::total_staking_currency(), 10_007_661_466_890_000); // Karura ias 12 sec block time // 1_000_383 * 1.000383 * 1.000383 = 1001149.440123181887 #[cfg(feature = "with-karura-runtime")] assert_eq!(HomaLite::total_staking_currency(), 1_001_149_440_123_181_887); + #[cfg(feature = "with-acala-runtime")] + assert_eq!(HomaLite::total_staking_currency(), 10_011_494_401_231_819); let rate3 = HomaLite::get_exchange_rate(); assert!(rate3 > rate2); @@ -359,12 +361,14 @@ fn liquid_value_goes_up_periodically() { HomaLite::on_initialize(i); } // 1000766.146689 * 1.000383 = 1.001149440123181887 - #[cfg(any(feature = "with-mandala-runtime", feature = "with-acala-runtime"))] + #[cfg(feature = "with-mandala-runtime")] assert_eq!(HomaLite::total_staking_currency(), 10_011_494_401_231_819); // 1001149.440123181887 * 1.000383 * 1.000383 = 1001916.46745192646655 #[cfg(feature = "with-karura-runtime")] assert_eq!(HomaLite::total_staking_currency(), 1_001_916_467_451_926_467); + #[cfg(feature = "with-acala-runtime")] + assert_eq!(HomaLite::total_staking_currency(), 10_019_164_674_519_265); let rate4 = HomaLite::get_exchange_rate(); assert!(rate4 > rate3); From e43b30b65fcc6f4c98136d79e5a8153b1ad6c140 Mon Sep 17 00:00:00 2001 From: Roy Yang Date: Tue, 2 Nov 2021 10:16:51 +1300 Subject: [PATCH 18/25] cached a variable locally to avoid duplicate storage read --- modules/homa-lite/src/lib.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/modules/homa-lite/src/lib.rs b/modules/homa-lite/src/lib.rs index 314075b10..271c047ee 100644 --- a/modules/homa-lite/src/lib.rs +++ b/modules/homa-lite/src/lib.rs @@ -284,13 +284,14 @@ pub mod module { fn on_initialize(n: T::BlockNumber) -> Weight { // Update the total amount of Staking balance by acrueing the interest periodically. - if !Self::staking_interest_rate_per_update().is_zero() + let interest_rate = Self::staking_interest_rate_per_update(); + if !interest_rate.is_zero() && n.checked_rem(&T::StakingUpdateFrequency::get()) .unwrap_or_else(One::one) .is_zero() { let current = Self::total_staking_currency(); - let new_total = current.saturating_add(Self::staking_interest_rate_per_update().mul(current)); + let new_total = current.saturating_add(interest_rate.mul(current)); let _ = Self::update_total_staking_currency_storage(new_total); ::WeightInfo::on_initialize() } else { From a86cb55a7d98eaeae30386a44ef9e7eb6b6b5ff0 Mon Sep 17 00:00:00 2001 From: Roy Yang Date: Tue, 2 Nov 2021 13:16:30 +1300 Subject: [PATCH 19/25] Made improvement to address PR comments. --- modules/homa-lite/src/lib.rs | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/modules/homa-lite/src/lib.rs b/modules/homa-lite/src/lib.rs index 271c047ee..1ec5e244a 100644 --- a/modules/homa-lite/src/lib.rs +++ b/modules/homa-lite/src/lib.rs @@ -34,7 +34,7 @@ use orml_traits::{ use primitives::{Balance, CurrencyId}; use sp_arithmetic::traits::CheckedRem; use sp_runtime::{ - traits::{BlockNumberProvider, Bounded, One, Saturating, Zero}, + traits::{BlockNumberProvider, Bounded, Saturating, Zero}, ArithmeticError, FixedPointNumber, Permill, }; use sp_std::{ @@ -287,12 +287,13 @@ pub mod module { let interest_rate = Self::staking_interest_rate_per_update(); if !interest_rate.is_zero() && n.checked_rem(&T::StakingUpdateFrequency::get()) - .unwrap_or_else(One::one) - .is_zero() + .map_or(false, |n| n.is_zero()) { - let current = Self::total_staking_currency(); - let new_total = current.saturating_add(interest_rate.mul(current)); - let _ = Self::update_total_staking_currency_storage(new_total); + // Inflate the staking total by the interest rate. + // This will only fail when current TotalStakingCurrency is 0. In this case it is OK to fail. + let _ = Self::update_total_staking_currency_storage(|current| { + current.saturating_add(interest_rate.mul(current)) + }); ::WeightInfo::on_initialize() } else { 0 @@ -331,7 +332,7 @@ pub mod module { #[transactional] pub fn set_total_staking_currency(origin: OriginFor, staking_total: Balance) -> DispatchResult { T::GovernanceOrigin::ensure_origin(origin)?; - Self::update_total_staking_currency_storage(staking_total) + Self::update_total_staking_currency_storage(|_n| staking_total) } /// Adjusts the total_staking_currency by the given difference. @@ -366,7 +367,7 @@ pub mod module { .ok_or(ArithmeticError::Underflow)?; } - Self::update_total_staking_currency_storage(current_staking_total) + Self::update_total_staking_currency_storage(|_n| current_staking_total) } /// Updates the cap for how much Staking currency can be used to Mint liquid currency. @@ -793,7 +794,7 @@ pub mod module { Error::::ExceededStakingCurrencyMintCap ); - Self::update_total_staking_currency_storage(new_total_staking_currency)?; + Self::update_total_staking_currency_storage(|_n| new_total_staking_currency)?; // All checks pass. Proceed with Xcm transfer. T::XcmTransfer::transfer( @@ -844,7 +845,7 @@ pub mod module { let actual_staking_amount = Self::convert_liquid_to_staking(actual_liquid_amount)?; total_staking_currency = total_staking_currency.saturating_sub(actual_staking_amount); - Self::update_total_staking_currency_storage(total_staking_currency)?; + Self::update_total_staking_currency_storage(|_n| total_staking_currency)?; // Redeem from the available_staking_balances costs only the xcm unbond fee. T::Currency::deposit( @@ -914,14 +915,14 @@ pub mod module { } /// Helper function that update the storage with the new - fn update_total_staking_currency_storage(new_total: Balance) -> DispatchResult { - ensure!(!new_total.is_zero(), Error::::InvalidTotalStakingCurrency); - - // Update storage and emit event. - TotalStakingCurrency::::mutate(|current| { - *current = new_total; - Self::deposit_event(Event::::TotalStakingCurrencySet(*current)); + fn update_total_staking_currency_storage(f: impl FnOnce(Balance) -> Balance) -> DispatchResult { + // Update storage with given function and emit event. + let new_total = TotalStakingCurrency::::mutate(|current| { + *current = f(*current); + *current }); + ensure!(!new_total.is_zero(), Error::::InvalidTotalStakingCurrency); + Self::deposit_event(Event::::TotalStakingCurrencySet(new_total)); Ok(()) } } From 0e89ba48827588a3e328c92a7db1ce3ade91a800 Mon Sep 17 00:00:00 2001 From: Roy Yang Date: Tue, 2 Nov 2021 13:50:46 +1300 Subject: [PATCH 20/25] minor improvements --- modules/homa-lite/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/homa-lite/src/lib.rs b/modules/homa-lite/src/lib.rs index 1ec5e244a..687abd538 100644 --- a/modules/homa-lite/src/lib.rs +++ b/modules/homa-lite/src/lib.rs @@ -914,9 +914,9 @@ pub mod module { ) } - /// Helper function that update the storage with the new + /// Helper function that update the storage of total_staking_currency and emit event. + #[transactional] fn update_total_staking_currency_storage(f: impl FnOnce(Balance) -> Balance) -> DispatchResult { - // Update storage with given function and emit event. let new_total = TotalStakingCurrency::::mutate(|current| { *current = f(*current); *current From e2c7d6a541a109c74d8e70cc2e3d348f1744b1c4 Mon Sep 17 00:00:00 2001 From: Roy Yang Date: Tue, 2 Nov 2021 18:25:15 +1300 Subject: [PATCH 21/25] improved how `update_total_staking_currency_storage` is implemented --- modules/homa-lite/src/lib.rs | 68 +++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 33 deletions(-) diff --git a/modules/homa-lite/src/lib.rs b/modules/homa-lite/src/lib.rs index 687abd538..b11dae3d4 100644 --- a/modules/homa-lite/src/lib.rs +++ b/modules/homa-lite/src/lib.rs @@ -292,7 +292,7 @@ pub mod module { // Inflate the staking total by the interest rate. // This will only fail when current TotalStakingCurrency is 0. In this case it is OK to fail. let _ = Self::update_total_staking_currency_storage(|current| { - current.saturating_add(interest_rate.mul(current)) + Ok(current.saturating_add(interest_rate.mul(current))) }); ::WeightInfo::on_initialize() } else { @@ -332,7 +332,7 @@ pub mod module { #[transactional] pub fn set_total_staking_currency(origin: OriginFor, staking_total: Balance) -> DispatchResult { T::GovernanceOrigin::ensure_origin(origin)?; - Self::update_total_staking_currency_storage(|_n| staking_total) + Self::update_total_staking_currency_storage(|_n| Ok(staking_total)) } /// Adjusts the total_staking_currency by the given difference. @@ -345,7 +345,6 @@ pub mod module { #[transactional] pub fn adjust_total_staking_currency(origin: OriginFor, by_amount: AmountOf) -> DispatchResult { T::GovernanceOrigin::ensure_origin(origin)?; - let mut current_staking_total = Self::total_staking_currency(); // Convert AmountOf into Balance safely. let by_amount_abs = if by_amount == AmountOf::::min_value() { @@ -357,17 +356,18 @@ pub mod module { let by_balance = TryInto::::try_into(by_amount_abs).map_err(|_| ArithmeticError::Overflow)?; // Adjust the current total. - if by_amount.is_positive() { - current_staking_total = current_staking_total - .checked_add(by_balance) - .ok_or(ArithmeticError::Overflow)?; - } else { - current_staking_total = current_staking_total - .checked_sub(by_balance) - .ok_or(ArithmeticError::Underflow)?; - } - - Self::update_total_staking_currency_storage(|_n| current_staking_total) + Self::update_total_staking_currency_storage(|current_staking_total| { + let new_total = if by_amount.is_positive() { + current_staking_total + .checked_add(by_balance) + .ok_or(ArithmeticError::Overflow) + } else { + current_staking_total + .checked_sub(by_balance) + .ok_or(ArithmeticError::Underflow) + }?; + Ok(new_total) + }) } /// Updates the cap for how much Staking currency can be used to Mint liquid currency. @@ -785,16 +785,17 @@ pub mod module { liquid_to_mint = (Permill::one().saturating_sub(T::MaxRewardPerEra::get())).mul(liquid_to_mint); liquid_to_mint = Self::convert_staking_to_liquid(liquid_to_mint)?; - // Ensure the total amount staked doesn't exceed the cap. - let new_total_staking_currency = Self::total_staking_currency() - .checked_add(staking_remaining) - .ok_or(ArithmeticError::Overflow)?; - ensure!( - new_total_staking_currency <= Self::staking_currency_mint_cap(), - Error::::ExceededStakingCurrencyMintCap - ); - - Self::update_total_staking_currency_storage(|_n| new_total_staking_currency)?; + // Update staking total and ensure the new total doesn't exceed the cap. + Self::update_total_staking_currency_storage(|total_staking_currency| { + let new_total_staking_currency = total_staking_currency + .checked_add(staking_remaining) + .ok_or(ArithmeticError::Overflow)?; + ensure!( + new_total_staking_currency <= Self::staking_currency_mint_cap(), + Error::::ExceededStakingCurrencyMintCap + ); + Ok(new_total_staking_currency) + })?; // All checks pass. Proceed with Xcm transfer. T::XcmTransfer::transfer( @@ -845,7 +846,7 @@ pub mod module { let actual_staking_amount = Self::convert_liquid_to_staking(actual_liquid_amount)?; total_staking_currency = total_staking_currency.saturating_sub(actual_staking_amount); - Self::update_total_staking_currency_storage(|_n| total_staking_currency)?; + Self::update_total_staking_currency_storage(|_n| Ok(total_staking_currency))?; // Redeem from the available_staking_balances costs only the xcm unbond fee. T::Currency::deposit( @@ -916,14 +917,15 @@ pub mod module { /// Helper function that update the storage of total_staking_currency and emit event. #[transactional] - fn update_total_staking_currency_storage(f: impl FnOnce(Balance) -> Balance) -> DispatchResult { - let new_total = TotalStakingCurrency::::mutate(|current| { - *current = f(*current); - *current - }); - ensure!(!new_total.is_zero(), Error::::InvalidTotalStakingCurrency); - Self::deposit_event(Event::::TotalStakingCurrencySet(new_total)); - Ok(()) + fn update_total_staking_currency_storage( + f: impl FnOnce(Balance) -> Result, + ) -> DispatchResult { + TotalStakingCurrency::::try_mutate(|current| { + *current = f(*current)?; + ensure!(!current.is_zero(), Error::::InvalidTotalStakingCurrency); + Self::deposit_event(Event::::TotalStakingCurrencySet(*current)); + Ok(()) + }) } } From 7936c71286521c5f12b703ad57a3ac39ac5478fe Mon Sep 17 00:00:00 2001 From: Xiliang Chen Date: Tue, 2 Nov 2021 21:44:20 +1300 Subject: [PATCH 22/25] Update modules/homa-lite/src/lib.rs --- modules/homa-lite/src/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/homa-lite/src/lib.rs b/modules/homa-lite/src/lib.rs index b11dae3d4..071d93ce0 100644 --- a/modules/homa-lite/src/lib.rs +++ b/modules/homa-lite/src/lib.rs @@ -916,7 +916,6 @@ pub mod module { } /// Helper function that update the storage of total_staking_currency and emit event. - #[transactional] fn update_total_staking_currency_storage( f: impl FnOnce(Balance) -> Result, ) -> DispatchResult { From 9d8bff3208ac98928e7f962881492a7f31a1d32c Mon Sep 17 00:00:00 2001 From: Roy Yang Date: Wed, 3 Nov 2021 01:31:07 +1300 Subject: [PATCH 23/25] Made on_intialize more accurate via benchmarking 2 cases separately. Fixed benchmarking for on_initialize. benchmarked the module and with all runtimes. Updated unit test to test the right weight is returned from on_initialize --- modules/homa-lite/Cargo.toml | 1 + modules/homa-lite/src/benchmarking.rs | 16 +++- modules/homa-lite/src/lib.rs | 2 +- modules/homa-lite/src/tests.rs | 25 +++-- modules/homa-lite/src/weights.rs | 91 +++++++++++-------- runtime/acala/src/weights/module_homa_lite.rs | 41 +++++---- .../karura/src/weights/module_homa_lite.rs | 33 ++++--- .../mandala/src/weights/module_homa_lite.rs | 28 +++--- 8 files changed, 143 insertions(+), 94 deletions(-) diff --git a/modules/homa-lite/Cargo.toml b/modules/homa-lite/Cargo.toml index e841fda69..52390ae56 100644 --- a/modules/homa-lite/Cargo.toml +++ b/modules/homa-lite/Cargo.toml @@ -51,5 +51,6 @@ runtime-benchmarks = [ "frame-benchmarking", "frame-support/runtime-benchmarks", "frame-system/runtime-benchmarks", + "pallet-xcm/runtime-benchmarks", ] try-runtime = ["frame-support/try-runtime"] diff --git a/modules/homa-lite/src/benchmarking.rs b/modules/homa-lite/src/benchmarking.rs index 906d0ae8e..eb37eae1c 100644 --- a/modules/homa-lite/src/benchmarking.rs +++ b/modules/homa-lite/src/benchmarking.rs @@ -48,11 +48,20 @@ benchmarks! { } on_initialize { + let _ = crate::Pallet::::set_staking_interest_rate_per_update( + RawOrigin::Root.into(), + Permill::from_percent(1) + ); let _ = crate::Pallet::::set_total_staking_currency(RawOrigin::Root.into(), 1_000_000_000_000_000_000); }: { let _ = crate::Pallet::::on_initialize(::BlockNumber::default()); } + on_initialize_without_work {}: { + // interest rate is not calculated becasue `set_staking_interest_rate_per_update` is not called. + let _ = crate::Pallet::::on_initialize(::BlockNumber::default()); + } + mint { let amount = 1_000_000_000_000; let caller: T::AccountId = account("caller", 0, SEED); @@ -118,7 +127,12 @@ mod tests { assert_ok!(Pallet::::test_benchmark_on_initialize()); }); } - + #[test] + fn test_on_initialize_without_work() { + ExtBuilder::default().build().execute_with(|| { + assert_ok!(Pallet::::test_benchmark_on_initialize_without_work()); + }); + } #[test] fn test_mint() { ExtBuilder::default().build().execute_with(|| { diff --git a/modules/homa-lite/src/lib.rs b/modules/homa-lite/src/lib.rs index b11dae3d4..431d7efe6 100644 --- a/modules/homa-lite/src/lib.rs +++ b/modules/homa-lite/src/lib.rs @@ -296,7 +296,7 @@ pub mod module { }); ::WeightInfo::on_initialize() } else { - 0 + ::WeightInfo::on_initialize_without_work() } } } diff --git a/modules/homa-lite/src/tests.rs b/modules/homa-lite/src/tests.rs index 8cb3fcae0..f4a9ccc5c 100644 --- a/modules/homa-lite/src/tests.rs +++ b/modules/homa-lite/src/tests.rs @@ -849,13 +849,17 @@ fn total_staking_currency_update_periodically() { ExtBuilder::default().build().execute_with(|| { assert_ok!(HomaLite::set_total_staking_currency(Origin::root(), dollar(1_000_000))); - HomaLite::on_initialize(0); + let on_initialize_weight = ::WeightInfo::on_initialize(); + let on_initialize_without_work_weight = ::WeightInfo::on_initialize_without_work(); + + assert_eq!(HomaLite::on_initialize(0), on_initialize_weight); // Default inflation rate is 0% assert_eq!(TotalStakingCurrency::::get(), dollar(1_000_000)); - for i in 1..101 { - HomaLite::on_initialize(i); + for i in 1..100 { + assert_eq!(HomaLite::on_initialize(i), on_initialize_without_work_weight); } + assert_eq!(HomaLite::on_initialize(100), on_initialize_weight); assert_eq!(TotalStakingCurrency::::get(), dollar(1_000_000)); // Interest rate can only be set by governance @@ -871,27 +875,30 @@ fn total_staking_currency_update_periodically() { Permill::from_percent(1), ))); - for i in 101..201 { - HomaLite::on_initialize(i); + for i in 101..200 { + assert_eq!(HomaLite::on_initialize(i), on_initialize_without_work_weight); } + assert_eq!(HomaLite::on_initialize(200), on_initialize_weight); // Inflate by 1%: 1_000_000 * 1.01 assert_eq!(TotalStakingCurrency::::get(), dollar(1_010_000)); System::assert_last_event(Event::HomaLite(crate::Event::TotalStakingCurrencySet(dollar( 1_010_000, )))); - for i in 201..301 { - HomaLite::on_initialize(i); + for i in 201..300 { + assert_eq!(HomaLite::on_initialize(i), on_initialize_without_work_weight); } + assert_eq!(HomaLite::on_initialize(300), on_initialize_weight); // 1_010_000 * 1.01 assert_eq!(TotalStakingCurrency::::get(), dollar(1_020_100)); System::assert_last_event(Event::HomaLite(crate::Event::TotalStakingCurrencySet(dollar( 1_020_100, )))); - for i in 301..401 { - HomaLite::on_initialize(i); + for i in 301..400 { + assert_eq!(HomaLite::on_initialize(i), on_initialize_without_work_weight); } + assert_eq!(HomaLite::on_initialize(400), on_initialize_weight); //1_020_100 * 1.01 assert_eq!(TotalStakingCurrency::::get(), dollar(1_030_301)); System::assert_last_event(Event::HomaLite(crate::Event::TotalStakingCurrencySet(dollar( diff --git a/modules/homa-lite/src/weights.rs b/modules/homa-lite/src/weights.rs index de069c62b..9d7c4b84c 100644 --- a/modules/homa-lite/src/weights.rs +++ b/modules/homa-lite/src/weights.rs @@ -19,22 +19,22 @@ //! Autogenerated weights for module_homa_lite //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2021-10-29, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` -//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 128 +//! DATE: 2021-11-02, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("karura-dev"), DB CACHE: 128 // Executed Command: // target/release/acala // benchmark -// --chain=dev +// --chain=karura-dev // --steps=50 // --repeat=20 -// --pallet=module_homa_lite +// --pallet=module-homa-lite // --extrinsic=* // --execution=wasm // --wasm-execution=compiled // --heap-pages=4096 -// --output=./modules/homa-lite/src/weights.rs // --template=./templates/module-weight-template.hbs +// --output=./modules/homa-lite/src/weights.rs #![cfg_attr(rustfmt, rustfmt_skip)] @@ -49,6 +49,7 @@ use sp_std::marker::PhantomData; pub trait WeightInfo { fn on_idle() -> Weight; fn on_initialize() -> Weight; + fn on_initialize_without_work() -> Weight; fn mint() -> Weight; fn mint_for_requests() -> Weight; fn set_total_staking_currency() -> Weight; @@ -65,57 +66,63 @@ pub trait WeightInfo { pub struct AcalaWeight(PhantomData); impl WeightInfo for AcalaWeight { fn on_idle() -> Weight { - (61_325_000 as Weight) - .saturating_add(T::DbWeight::get().reads(11 as Weight)) + (41_000_000 as Weight) + .saturating_add(T::DbWeight::get().reads(12 as Weight)) + .saturating_add(T::DbWeight::get().writes(4 as Weight)) } fn on_initialize() -> Weight { - (3_340_000 as Weight) + (12_000_000 as Weight) + .saturating_add(T::DbWeight::get().reads(2 as Weight)) + .saturating_add(T::DbWeight::get().writes(1 as Weight)) + } + fn on_initialize_without_work() -> Weight { + (2_000_000 as Weight) .saturating_add(T::DbWeight::get().reads(1 as Weight)) } fn mint() -> Weight { - (245_257_000 as Weight) - .saturating_add(T::DbWeight::get().reads(14 as Weight)) - .saturating_add(T::DbWeight::get().writes(7 as Weight)) + (138_000_000 as Weight) + .saturating_add(T::DbWeight::get().reads(17 as Weight)) + .saturating_add(T::DbWeight::get().writes(8 as Weight)) } fn mint_for_requests() -> Weight { - (602_614_000 as Weight) - .saturating_add(T::DbWeight::get().reads(30 as Weight)) - .saturating_add(T::DbWeight::get().writes(21 as Weight)) + (145_000_000 as Weight) + .saturating_add(T::DbWeight::get().reads(19 as Weight)) + .saturating_add(T::DbWeight::get().writes(8 as Weight)) } fn set_total_staking_currency() -> Weight { - (21_429_000 as Weight) + (12_000_000 as Weight) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn adjust_total_staking_currency() -> Weight { - (22_079_000 as Weight) + (12_000_000 as Weight) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn set_minting_cap() -> Weight { - (18_989_000 as Weight) + (10_000_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn set_xcm_dest_weight() -> Weight { - (18_245_000 as Weight) + (10_000_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn request_redeem() -> Weight { - (144_193_000 as Weight) + (72_000_000 as Weight) .saturating_add(T::DbWeight::get().reads(7 as Weight)) .saturating_add(T::DbWeight::get().writes(4 as Weight)) } fn schedule_unbond() -> Weight { - (22_671_000 as Weight) + (12_000_000 as Weight) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn replace_schedule_unbond() -> Weight { - (19_369_000 as Weight) + (10_000_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn set_staking_interest_rate_per_update() -> Weight { - (17_812_000 as Weight) + (10_000_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } } @@ -123,57 +130,63 @@ impl WeightInfo for AcalaWeight { // For backwards compatibility and tests impl WeightInfo for () { fn on_idle() -> Weight { - (61_325_000 as Weight) - .saturating_add(RocksDbWeight::get().reads(11 as Weight)) + (41_000_000 as Weight) + .saturating_add(RocksDbWeight::get().reads(12 as Weight)) + .saturating_add(RocksDbWeight::get().writes(4 as Weight)) } fn on_initialize() -> Weight { - (3_340_000 as Weight) + (12_000_000 as Weight) + .saturating_add(RocksDbWeight::get().reads(2 as Weight)) + .saturating_add(RocksDbWeight::get().writes(1 as Weight)) + } + fn on_initialize_without_work() -> Weight { + (2_000_000 as Weight) .saturating_add(RocksDbWeight::get().reads(1 as Weight)) } fn mint() -> Weight { - (245_257_000 as Weight) - .saturating_add(RocksDbWeight::get().reads(14 as Weight)) - .saturating_add(RocksDbWeight::get().writes(7 as Weight)) + (138_000_000 as Weight) + .saturating_add(RocksDbWeight::get().reads(17 as Weight)) + .saturating_add(RocksDbWeight::get().writes(8 as Weight)) } fn mint_for_requests() -> Weight { - (602_614_000 as Weight) - .saturating_add(RocksDbWeight::get().reads(30 as Weight)) - .saturating_add(RocksDbWeight::get().writes(21 as Weight)) + (145_000_000 as Weight) + .saturating_add(RocksDbWeight::get().reads(19 as Weight)) + .saturating_add(RocksDbWeight::get().writes(8 as Weight)) } fn set_total_staking_currency() -> Weight { - (21_429_000 as Weight) + (12_000_000 as Weight) .saturating_add(RocksDbWeight::get().reads(1 as Weight)) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } fn adjust_total_staking_currency() -> Weight { - (22_079_000 as Weight) + (12_000_000 as Weight) .saturating_add(RocksDbWeight::get().reads(1 as Weight)) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } fn set_minting_cap() -> Weight { - (18_989_000 as Weight) + (10_000_000 as Weight) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } fn set_xcm_dest_weight() -> Weight { - (18_245_000 as Weight) + (10_000_000 as Weight) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } fn request_redeem() -> Weight { - (144_193_000 as Weight) + (72_000_000 as Weight) .saturating_add(RocksDbWeight::get().reads(7 as Weight)) .saturating_add(RocksDbWeight::get().writes(4 as Weight)) } fn schedule_unbond() -> Weight { - (22_671_000 as Weight) + (12_000_000 as Weight) .saturating_add(RocksDbWeight::get().reads(1 as Weight)) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } fn replace_schedule_unbond() -> Weight { - (19_369_000 as Weight) + (10_000_000 as Weight) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } fn set_staking_interest_rate_per_update() -> Weight { - (17_812_000 as Weight) + (10_000_000 as Weight) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } } diff --git a/runtime/acala/src/weights/module_homa_lite.rs b/runtime/acala/src/weights/module_homa_lite.rs index f0de71c35..bf514527b 100644 --- a/runtime/acala/src/weights/module_homa_lite.rs +++ b/runtime/acala/src/weights/module_homa_lite.rs @@ -19,7 +19,7 @@ //! Autogenerated weights for module_homa_lite //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2021-10-27, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! DATE: 2021-11-02, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` //! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("acala-dev"), DB CACHE: 128 // Executed Command: @@ -48,27 +48,30 @@ use sp_std::marker::PhantomData; pub struct WeightInfo(PhantomData); impl module_homa_lite::WeightInfo for WeightInfo { fn on_idle() -> Weight { - (249_000_000 as Weight) - .saturating_add(T::DbWeight::get().reads(23 as Weight)) - .saturating_add(T::DbWeight::get().writes(17 as Weight)) + (0 as Weight) } fn on_initialize() -> Weight { - (11_000_000 as Weight) - .saturating_add(T::DbWeight::get().reads(1 as Weight)) + (13_000_000 as Weight) + .saturating_add(T::DbWeight::get().reads(2 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } + fn on_initialize_without_work() -> Weight { + (2_000_000 as Weight) + .saturating_add(T::DbWeight::get().reads(1 as Weight)) + } fn mint() -> Weight { - (134_000_000 as Weight) - .saturating_add(T::DbWeight::get().reads(14 as Weight)) - .saturating_add(T::DbWeight::get().writes(7 as Weight)) + (137_000_000 as Weight) + .saturating_add(T::DbWeight::get().reads(17 as Weight)) + .saturating_add(T::DbWeight::get().writes(8 as Weight)) } fn mint_for_requests() -> Weight { - (342_000_000 as Weight) - .saturating_add(T::DbWeight::get().reads(30 as Weight)) - .saturating_add(T::DbWeight::get().writes(21 as Weight)) + (326_000_000 as Weight) + .saturating_add(T::DbWeight::get().reads(33 as Weight)) + .saturating_add(T::DbWeight::get().writes(22 as Weight)) } fn set_total_staking_currency() -> Weight { - (11_000_000 as Weight) + (12_000_000 as Weight) + .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn adjust_total_staking_currency() -> Weight { @@ -77,29 +80,29 @@ impl module_homa_lite::WeightInfo for WeightInfo { .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn set_minting_cap() -> Weight { - (11_000_000 as Weight) + (10_000_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn set_xcm_dest_weight() -> Weight { - (11_000_000 as Weight) + (10_000_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn request_redeem() -> Weight { - (80_000_000 as Weight) + (74_000_000 as Weight) .saturating_add(T::DbWeight::get().reads(7 as Weight)) .saturating_add(T::DbWeight::get().writes(4 as Weight)) } fn schedule_unbond() -> Weight { - (13_000_000 as Weight) + (12_000_000 as Weight) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn replace_schedule_unbond() -> Weight { - (11_000_000 as Weight) + (10_000_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn set_staking_interest_rate_per_update() -> Weight { - (11_000_000 as Weight) + (10_000_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } } diff --git a/runtime/karura/src/weights/module_homa_lite.rs b/runtime/karura/src/weights/module_homa_lite.rs index b26fde992..e4dcb5669 100644 --- a/runtime/karura/src/weights/module_homa_lite.rs +++ b/runtime/karura/src/weights/module_homa_lite.rs @@ -19,7 +19,7 @@ //! Autogenerated weights for module_homa_lite //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2021-10-29, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! DATE: 2021-11-02, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` //! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("karura-dev"), DB CACHE: 128 // Executed Command: @@ -28,7 +28,7 @@ // --chain=karura-dev // --steps=50 // --repeat=20 -// --pallet=module_homa_lite +// --pallet=module-homa-lite // --extrinsic=* // --execution=wasm // --wasm-execution=compiled @@ -48,58 +48,63 @@ use sp_std::marker::PhantomData; pub struct WeightInfo(PhantomData); impl module_homa_lite::WeightInfo for WeightInfo { fn on_idle() -> Weight { - (75_782_000 as Weight) + (41_000_000 as Weight) .saturating_add(T::DbWeight::get().reads(12 as Weight)) .saturating_add(T::DbWeight::get().writes(4 as Weight)) } fn on_initialize() -> Weight { - (5_959_000 as Weight) + (13_000_000 as Weight) + .saturating_add(T::DbWeight::get().reads(2 as Weight)) + .saturating_add(T::DbWeight::get().writes(1 as Weight)) + } + fn on_initialize_without_work() -> Weight { + (2_000_000 as Weight) .saturating_add(T::DbWeight::get().reads(1 as Weight)) } fn mint() -> Weight { - (423_446_000 as Weight) + (137_000_000 as Weight) .saturating_add(T::DbWeight::get().reads(17 as Weight)) .saturating_add(T::DbWeight::get().writes(8 as Weight)) } fn mint_for_requests() -> Weight { - (270_971_000 as Weight) + (147_000_000 as Weight) .saturating_add(T::DbWeight::get().reads(19 as Weight)) .saturating_add(T::DbWeight::get().writes(8 as Weight)) } fn set_total_staking_currency() -> Weight { - (20_863_000 as Weight) + (12_000_000 as Weight) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn adjust_total_staking_currency() -> Weight { - (21_761_000 as Weight) + (12_000_000 as Weight) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn set_minting_cap() -> Weight { - (18_646_000 as Weight) + (10_000_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn set_xcm_dest_weight() -> Weight { - (18_046_000 as Weight) + (10_000_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn request_redeem() -> Weight { - (137_950_000 as Weight) + (73_000_000 as Weight) .saturating_add(T::DbWeight::get().reads(7 as Weight)) .saturating_add(T::DbWeight::get().writes(4 as Weight)) } fn schedule_unbond() -> Weight { - (22_342_000 as Weight) + (12_000_000 as Weight) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn replace_schedule_unbond() -> Weight { - (19_468_000 as Weight) + (10_000_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn set_staking_interest_rate_per_update() -> Weight { - (17_718_000 as Weight) + (10_000_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } } diff --git a/runtime/mandala/src/weights/module_homa_lite.rs b/runtime/mandala/src/weights/module_homa_lite.rs index 26c391e82..1685ec659 100644 --- a/runtime/mandala/src/weights/module_homa_lite.rs +++ b/runtime/mandala/src/weights/module_homa_lite.rs @@ -19,7 +19,7 @@ //! Autogenerated weights for module_homa_lite //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2021-10-27, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! DATE: 2021-11-02, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` //! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 128 // Executed Command: @@ -48,25 +48,31 @@ use sp_std::marker::PhantomData; pub struct WeightInfo(PhantomData); impl module_homa_lite::WeightInfo for WeightInfo { fn on_idle() -> Weight { - (0 as Weight) + (34_000_000 as Weight) + .saturating_add(T::DbWeight::get().reads(11 as Weight)) } fn on_initialize() -> Weight { - (12_000_000 as Weight) - .saturating_add(T::DbWeight::get().reads(1 as Weight)) + (13_000_000 as Weight) + .saturating_add(T::DbWeight::get().reads(2 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } + fn on_initialize_without_work() -> Weight { + (2_000_000 as Weight) + .saturating_add(T::DbWeight::get().reads(1 as Weight)) + } fn mint() -> Weight { - (137_000_000 as Weight) + (132_000_000 as Weight) .saturating_add(T::DbWeight::get().reads(14 as Weight)) .saturating_add(T::DbWeight::get().writes(7 as Weight)) } fn mint_for_requests() -> Weight { - (348_000_000 as Weight) + (324_000_000 as Weight) .saturating_add(T::DbWeight::get().reads(30 as Weight)) .saturating_add(T::DbWeight::get().writes(21 as Weight)) } fn set_total_staking_currency() -> Weight { - (11_000_000 as Weight) + (12_000_000 as Weight) + .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn adjust_total_staking_currency() -> Weight { @@ -75,7 +81,7 @@ impl module_homa_lite::WeightInfo for WeightInfo { .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn set_minting_cap() -> Weight { - (11_000_000 as Weight) + (10_000_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn set_xcm_dest_weight() -> Weight { @@ -83,12 +89,12 @@ impl module_homa_lite::WeightInfo for WeightInfo { .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn request_redeem() -> Weight { - (81_000_000 as Weight) + (75_000_000 as Weight) .saturating_add(T::DbWeight::get().reads(7 as Weight)) .saturating_add(T::DbWeight::get().writes(4 as Weight)) } fn schedule_unbond() -> Weight { - (13_000_000 as Weight) + (12_000_000 as Weight) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } @@ -97,7 +103,7 @@ impl module_homa_lite::WeightInfo for WeightInfo { .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn set_staking_interest_rate_per_update() -> Weight { - (11_000_000 as Weight) + (10_000_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } } From 6a678cca1c58f72ab2f4476694d920a1d5c39f37 Mon Sep 17 00:00:00 2001 From: Roy Yang Date: Wed, 3 Nov 2021 10:19:02 +1300 Subject: [PATCH 24/25] Fixed a broken unit test --- modules/homa-lite/src/tests.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/modules/homa-lite/src/tests.rs b/modules/homa-lite/src/tests.rs index f4a9ccc5c..64abe1f13 100644 --- a/modules/homa-lite/src/tests.rs +++ b/modules/homa-lite/src/tests.rs @@ -852,14 +852,16 @@ fn total_staking_currency_update_periodically() { let on_initialize_weight = ::WeightInfo::on_initialize(); let on_initialize_without_work_weight = ::WeightInfo::on_initialize_without_work(); - assert_eq!(HomaLite::on_initialize(0), on_initialize_weight); + // Interst rate isn't set yet - no interest rate calculation is done. + assert_eq!(HomaLite::on_initialize(0), on_initialize_without_work_weight); // Default inflation rate is 0% assert_eq!(TotalStakingCurrency::::get(), dollar(1_000_000)); for i in 1..100 { assert_eq!(HomaLite::on_initialize(i), on_initialize_without_work_weight); } - assert_eq!(HomaLite::on_initialize(100), on_initialize_weight); + // Interst rate isn't set yet - no interest rate calculation is done. + assert_eq!(HomaLite::on_initialize(0), on_initialize_without_work_weight); assert_eq!(TotalStakingCurrency::::get(), dollar(1_000_000)); // Interest rate can only be set by governance From d803a14fe8425dab80701ef7478765d0a7191959 Mon Sep 17 00:00:00 2001 From: Roy Yang Date: Mon, 8 Nov 2021 12:03:45 +1300 Subject: [PATCH 25/25] Addressed a PR comment --- modules/homa-lite/src/lib.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/modules/homa-lite/src/lib.rs b/modules/homa-lite/src/lib.rs index 3db592bf7..7d6dc9a24 100644 --- a/modules/homa-lite/src/lib.rs +++ b/modules/homa-lite/src/lib.rs @@ -930,7 +930,6 @@ pub mod module { } let mut new_balances: Vec<(T::AccountId, Balance, Permill)> = vec![]; - let mut total_staking_currency = Self::total_staking_currency(); let mut num_matched = 0u32; for (redeemer, (request_amount, extra_fee)) in RedeemRequests::::iter() { let actual_liquid_amount = min( @@ -939,8 +938,7 @@ pub mod module { ); let actual_staking_amount = Self::convert_liquid_to_staking(actual_liquid_amount)?; - total_staking_currency = total_staking_currency.saturating_sub(actual_staking_amount); - Self::update_total_staking_currency_storage(|_n| Ok(total_staking_currency))?; + Self::update_total_staking_currency_storage(|total| Ok(total.saturating_sub(actual_staking_amount)))?; // Redeem from the available_staking_balances costs only the xcm unbond fee. T::Currency::deposit(