From 8c1acf340d63c2f5e7776c8adcf76b6b003e271a Mon Sep 17 00:00:00 2001 From: wangjj9219 <183318287@qq.com> Date: Mon, 26 Sep 2022 18:53:28 +0800 Subject: [PATCH 1/4] reset incentives.PendingMultiRewards and rewards.SharesAndWithdrawnRewards for Pool aUSD-IBTC --- modules/incentives/src/lib.rs | 8 ---- modules/incentives/src/migration.rs | 61 +++++++++++++++++++++++++---- runtime/acala/src/lib.rs | 15 +++++-- runtime/karura/src/lib.rs | 10 +---- runtime/mandala/src/lib.rs | 10 +---- 5 files changed, 69 insertions(+), 35 deletions(-) diff --git a/modules/incentives/src/lib.rs b/modules/incentives/src/lib.rs index 42f3c23fc3..0f32075d6d 100644 --- a/modules/incentives/src/lib.rs +++ b/modules/incentives/src/lib.rs @@ -165,14 +165,6 @@ pub mod module { pub type IncentiveRewardAmounts = StorageDoubleMap<_, Twox64Concat, PoolId, Twox64Concat, CurrencyId, Balance, ValueQuery>; - /// NOTE: already deprecated, need remove it after next runtime upgrade - /// Mapping from pool to its fixed reward rate per period. - /// - /// DexSavingRewardRates: map Pool => SavingRatePerPeriod - #[pallet::storage] - #[pallet::getter(fn dex_saving_reward_rates)] - pub type DexSavingRewardRates = StorageMap<_, Twox64Concat, PoolId, Rate, ValueQuery>; - /// Mapping from pool to its claim reward deduction rate. /// /// ClaimRewardDeductionRates: map Pool => DeductionRate diff --git a/modules/incentives/src/migration.rs b/modules/incentives/src/migration.rs index 47d533417e..32dfd0082d 100644 --- a/modules/incentives/src/migration.rs +++ b/modules/incentives/src/migration.rs @@ -19,29 +19,74 @@ use super::*; use crate::log; use frame_support::traits::OnRuntimeUpgrade; +use sp_std::marker::PhantomData; -/// Clear all DexSavingRewardRates storage -pub struct ClearDexSavingRewardRates(sp_std::marker::PhantomData); -impl OnRuntimeUpgrade for ClearDexSavingRewardRates { +type WithdrawnRewards = BTreeMap; + +/// Clear all PendingMultiRewards for specific Pool +pub struct ClearPendingMultiRewards(PhantomData, PhantomData); +impl> OnRuntimeUpgrade for ClearPendingMultiRewards { fn on_runtime_upgrade() -> Weight { + let pool_id = GetPoolId::get(); log::info!( target: "incentives", - "ClearDexSavingRewardRates::on_runtime_upgrade execute, will clear Storage DexSavingRewardRates", + "ClearPendingMultiRewards::on_runtime_upgrade execute, will clear Storage PendingMultiRewards for Pool {:?}", + pool_id, ); - // clear storage DexSavingRewardRates, - let _ = DexSavingRewardRates::::clear(u32::max_value(), None); + // clear all PendingMultiRewards for specific pool + let _ = PendingMultiRewards::::clear_prefix(pool_id, u32::max_value(), None); 0 } #[cfg(feature = "try-runtime")] fn post_upgrade() -> Result<(), &'static str> { - assert_eq!(DexSavingRewardRates::::iter().count(), 0); + let pool_id = GetPoolId::get(); + assert_eq!(PendingMultiRewards::::iter_prefix(pool_id).count(), 0); log::info!( target: "incentives", - "ClearDexSavingRewardRates done!", + "ClearPendingMultiRewards for Pool {:?} done!", + pool_id, + ); + + Ok(()) + } +} + +/// Reset WithdrawnRewards records for rewards.SharesAndWithdrawnRewards at specific PoolId +pub struct ResetSharesAndWithdrawnRewards(PhantomData, PhantomData); +impl> OnRuntimeUpgrade for ResetSharesAndWithdrawnRewards { + fn on_runtime_upgrade() -> Weight { + let pool_id = GetPoolId::get(); + log::info!( + target: "rewards", + "ResetSharesAndWithdrawnRewards::on_runtime_upgrade execute, will reset Storage SharesAndWithdrawnRewards for Pool {:?}", + pool_id + ); + + for (who, (_, _)) in orml_rewards::SharesAndWithdrawnRewards::::iter_prefix(&pool_id) { + orml_rewards::SharesAndWithdrawnRewards::::mutate(&pool_id, &who, |(_, withdrawn_rewards)| { + *withdrawn_rewards = WithdrawnRewards::new(); + }); + } + + 0 + } + + #[cfg(feature = "try-runtime")] + fn post_upgrade() -> Result<(), &'static str> { + let pool_id = GetPoolId::get(); + + for (_, (_, withdrawn_rewards)) in orml_rewards::SharesAndWithdrawnRewards::::iter_prefix(&pool_id) { + assert_eq!(withdrawn_rewards, WithdrawnRewards::new()); + } + + log::info!( + target: "rewards", + "ResetSharesAndWithdrawnRewards for Pool {:?} done!", + pool_id, ); Ok(()) diff --git a/runtime/acala/src/lib.rs b/runtime/acala/src/lib.rs index a7368503ec..73fb2476ef 100644 --- a/runtime/acala/src/lib.rs +++ b/runtime/acala/src/lib.rs @@ -94,8 +94,8 @@ pub use primitives::{ task::TaskResult, unchecked_extrinsic::AcalaUncheckedExtrinsic, AccountId, AccountIndex, Address, Amount, AuctionId, AuthoritysOriginId, Balance, BlockNumber, CurrencyId, - DataProviderId, EraIndex, Hash, Lease, Moment, Multiplier, Nonce, ReserveIdentifier, Share, Signature, TokenSymbol, - TradingPair, + DataProviderId, DexShare, EraIndex, Hash, Lease, Moment, Multiplier, Nonce, ReserveIdentifier, Share, Signature, + TokenSymbol, TradingPair, }; pub use runtime_common::{ cent, dollar, microcent, millicent, AcalaDropAssets, AllPrecompiles, CheckRelayNumber, @@ -1244,6 +1244,12 @@ impl orml_rewards::Config for Runtime { parameter_types! { pub const AccumulatePeriod: BlockNumber = MINUTES; pub const EarnShareBooster: Permill = Permill::from_percent(30); + pub const GetAusdIbtcPoolId: PoolId = PoolId::Dex( + CurrencyId::DexShare( + DexShare::Token(TokenSymbol::AUSD), + DexShare::ForeignAsset(3) + ) + ); } impl module_incentives::Config for Runtime { @@ -1772,7 +1778,10 @@ pub type Executive = frame_executive::Executive< frame_system::ChainContext, Runtime, AllPalletsWithSystem, - module_incentives::migration::ClearDexSavingRewardRates, + ( + module_incentives::migration::ResetSharesAndWithdrawnRewards, + module_incentives::migration::ClearPendingMultiRewards, + ), >; #[cfg(feature = "runtime-benchmarks")] diff --git a/runtime/karura/src/lib.rs b/runtime/karura/src/lib.rs index 0d6fea5366..e8186c1d24 100644 --- a/runtime/karura/src/lib.rs +++ b/runtime/karura/src/lib.rs @@ -1799,14 +1799,8 @@ pub type SignedPayload = generic::SignedPayload; /// Extrinsic type that has already been checked. pub type CheckedExtrinsic = generic::CheckedExtrinsic; /// Executive: handles dispatch to the various modules. -pub type Executive = frame_executive::Executive< - Runtime, - Block, - frame_system::ChainContext, - Runtime, - AllPalletsWithSystem, - module_incentives::migration::ClearDexSavingRewardRates, ->; +pub type Executive = + frame_executive::Executive, Runtime, AllPalletsWithSystem, ()>; #[cfg(feature = "runtime-benchmarks")] #[macro_use] diff --git a/runtime/mandala/src/lib.rs b/runtime/mandala/src/lib.rs index aa9697e21a..2af3a97250 100644 --- a/runtime/mandala/src/lib.rs +++ b/runtime/mandala/src/lib.rs @@ -1919,14 +1919,8 @@ pub type SignedPayload = generic::SignedPayload; /// Extrinsic type that has already been checked. pub type CheckedExtrinsic = generic::CheckedExtrinsic; /// Executive: handles dispatch to the various modules. -pub type Executive = frame_executive::Executive< - Runtime, - Block, - frame_system::ChainContext, - Runtime, - AllPalletsWithSystem, - module_incentives::migration::ClearDexSavingRewardRates, ->; +pub type Executive = + frame_executive::Executive, Runtime, AllPalletsWithSystem, ()>; construct_runtime!( pub enum Runtime where From e4422620225d2ef5d4b5becfb7636c80815e9a34 Mon Sep 17 00:00:00 2001 From: wangjj9219 <183318287@qq.com> Date: Tue, 27 Sep 2022 11:01:41 +0800 Subject: [PATCH 2/4] reset rewards.PoolInfos for Pool aUSD-IBTC --- modules/incentives/src/migration.rs | 29 ++++++++++++++++++++++------- runtime/acala/src/lib.rs | 2 +- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/modules/incentives/src/migration.rs b/modules/incentives/src/migration.rs index 32dfd0082d..c3cc117e53 100644 --- a/modules/incentives/src/migration.rs +++ b/modules/incentives/src/migration.rs @@ -55,37 +55,52 @@ impl> OnRuntimeUpgrade for ClearPendingMultiRe } } -/// Reset WithdrawnRewards records for rewards.SharesAndWithdrawnRewards at specific PoolId -pub struct ResetSharesAndWithdrawnRewards(PhantomData, PhantomData); -impl> OnRuntimeUpgrade for ResetSharesAndWithdrawnRewards { +/// Reset rewards record for storage rewards.SharesAndWithdrawnRewards and rewards.PoolInfos at +/// specific PoolId +pub struct ResetRewardsRecord(PhantomData, PhantomData); +impl> OnRuntimeUpgrade for ResetRewardsRecord { fn on_runtime_upgrade() -> Weight { let pool_id = GetPoolId::get(); log::info!( target: "rewards", - "ResetSharesAndWithdrawnRewards::on_runtime_upgrade execute, will reset Storage SharesAndWithdrawnRewards for Pool {:?}", + "ResetRewardsRecord::on_runtime_upgrade execute, will reset Storage SharesAndWithdrawnRewards and PoolInfos for Pool {:?}", pool_id ); - for (who, (_, _)) in orml_rewards::SharesAndWithdrawnRewards::::iter_prefix(&pool_id) { + let mut total_share: Balance = Default::default(); + + // reset SharesAndWithdrawnRewards + for (who, (share, _)) in orml_rewards::SharesAndWithdrawnRewards::::iter_prefix(&pool_id) { orml_rewards::SharesAndWithdrawnRewards::::mutate(&pool_id, &who, |(_, withdrawn_rewards)| { *withdrawn_rewards = WithdrawnRewards::new(); }); + + total_share = total_share.saturating_add(share); } + // reset PoolInfos + let mut pool_info = orml_rewards::PoolInfo::::default(); + pool_info.total_shares = total_share; + orml_rewards::PoolInfos::::insert(&pool_id, pool_info); + 0 } #[cfg(feature = "try-runtime")] fn post_upgrade() -> Result<(), &'static str> { let pool_id = GetPoolId::get(); + let mut total_share = Balance::default(); - for (_, (_, withdrawn_rewards)) in orml_rewards::SharesAndWithdrawnRewards::::iter_prefix(&pool_id) { + for (_, (share, withdrawn_rewards)) in orml_rewards::SharesAndWithdrawnRewards::::iter_prefix(&pool_id) { assert_eq!(withdrawn_rewards, WithdrawnRewards::new()); + total_share = total_share.saturating_add(share); } + assert_eq!(orml_rewards::PoolInfos::::get(&pool_id).total_shares, total_share); + log::info!( target: "rewards", - "ResetSharesAndWithdrawnRewards for Pool {:?} done!", + "ResetRewardsRecord for Pool {:?} done!", pool_id, ); diff --git a/runtime/acala/src/lib.rs b/runtime/acala/src/lib.rs index 73fb2476ef..ea6adc3581 100644 --- a/runtime/acala/src/lib.rs +++ b/runtime/acala/src/lib.rs @@ -1779,7 +1779,7 @@ pub type Executive = frame_executive::Executive< Runtime, AllPalletsWithSystem, ( - module_incentives::migration::ResetSharesAndWithdrawnRewards, + module_incentives::migration::ResetRewardsRecord, module_incentives::migration::ClearPendingMultiRewards, ), >; From 02ae3f6487b5197ff469ba1ec01be29f571b15f2 Mon Sep 17 00:00:00 2001 From: wangjj9219 <183318287@qq.com> Date: Tue, 27 Sep 2022 13:07:46 +0800 Subject: [PATCH 3/4] fix clippy --- modules/incentives/src/migration.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/modules/incentives/src/migration.rs b/modules/incentives/src/migration.rs index c3cc117e53..0dfe92da5e 100644 --- a/modules/incentives/src/migration.rs +++ b/modules/incentives/src/migration.rs @@ -79,8 +79,10 @@ impl> OnRuntimeUpgrade for ResetRewardsRecord< } // reset PoolInfos - let mut pool_info = orml_rewards::PoolInfo::::default(); - pool_info.total_shares = total_share; + let pool_info = orml_rewards::PoolInfo:: { + total_shares: total_share, + ..Default::default() + }; orml_rewards::PoolInfos::::insert(&pool_id, pool_info); 0 From 9e60c959e69a8c3533b6fbbba4d4e0dcf87aa148 Mon Sep 17 00:00:00 2001 From: wangjj9219 <183318287@qq.com> Date: Fri, 30 Sep 2022 10:06:29 +0800 Subject: [PATCH 4/4] update --- modules/incentives/src/lib.rs | 8 +++++++ modules/incentives/src/migration.rs | 36 ++++++++++++++++++++++++++++- runtime/karura/src/lib.rs | 10 ++++++-- 3 files changed, 51 insertions(+), 3 deletions(-) diff --git a/modules/incentives/src/lib.rs b/modules/incentives/src/lib.rs index 0f32075d6d..42f3c23fc3 100644 --- a/modules/incentives/src/lib.rs +++ b/modules/incentives/src/lib.rs @@ -165,6 +165,14 @@ pub mod module { pub type IncentiveRewardAmounts = StorageDoubleMap<_, Twox64Concat, PoolId, Twox64Concat, CurrencyId, Balance, ValueQuery>; + /// NOTE: already deprecated, need remove it after next runtime upgrade + /// Mapping from pool to its fixed reward rate per period. + /// + /// DexSavingRewardRates: map Pool => SavingRatePerPeriod + #[pallet::storage] + #[pallet::getter(fn dex_saving_reward_rates)] + pub type DexSavingRewardRates = StorageMap<_, Twox64Concat, PoolId, Rate, ValueQuery>; + /// Mapping from pool to its claim reward deduction rate. /// /// ClaimRewardDeductionRates: map Pool => DeductionRate diff --git a/modules/incentives/src/migration.rs b/modules/incentives/src/migration.rs index 0dfe92da5e..15de85a825 100644 --- a/modules/incentives/src/migration.rs +++ b/modules/incentives/src/migration.rs @@ -21,6 +21,34 @@ use crate::log; use frame_support::traits::OnRuntimeUpgrade; use sp_std::marker::PhantomData; +/// Clear all DexSavingRewardRates storage +pub struct ClearDexSavingRewardRates(PhantomData); +impl OnRuntimeUpgrade for ClearDexSavingRewardRates { + fn on_runtime_upgrade() -> Weight { + log::info!( + target: "incentives", + "ClearDexSavingRewardRates::on_runtime_upgrade execute, will clear Storage DexSavingRewardRates", + ); + + // clear storage DexSavingRewardRates, + let _ = DexSavingRewardRates::::clear(u32::max_value(), None); + + 0 + } + + #[cfg(feature = "try-runtime")] + fn post_upgrade() -> Result<(), &'static str> { + assert_eq!(DexSavingRewardRates::::iter().count(), 0); + + log::info!( + target: "incentives", + "ClearDexSavingRewardRates done!", + ); + + Ok(()) + } +} + type WithdrawnRewards = BTreeMap; /// Clear all PendingMultiRewards for specific Pool @@ -98,7 +126,13 @@ impl> OnRuntimeUpgrade for ResetRewardsRecord< total_share = total_share.saturating_add(share); } - assert_eq!(orml_rewards::PoolInfos::::get(&pool_id).total_shares, total_share); + assert_eq!( + orml_rewards::PoolInfos::::get(&pool_id), + orml_rewards::PoolInfo:: { + total_shares: total_share, + ..Default::default() + } + ); log::info!( target: "rewards", diff --git a/runtime/karura/src/lib.rs b/runtime/karura/src/lib.rs index e8186c1d24..0d6fea5366 100644 --- a/runtime/karura/src/lib.rs +++ b/runtime/karura/src/lib.rs @@ -1799,8 +1799,14 @@ pub type SignedPayload = generic::SignedPayload; /// Extrinsic type that has already been checked. pub type CheckedExtrinsic = generic::CheckedExtrinsic; /// Executive: handles dispatch to the various modules. -pub type Executive = - frame_executive::Executive, Runtime, AllPalletsWithSystem, ()>; +pub type Executive = frame_executive::Executive< + Runtime, + Block, + frame_system::ChainContext, + Runtime, + AllPalletsWithSystem, + module_incentives::migration::ClearDexSavingRewardRates, +>; #[cfg(feature = "runtime-benchmarks")] #[macro_use]