This repository was archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Reward pool migration fix #13715
Merged
Merged
Reward pool migration fix #13715
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c82c7ad
reward pool migration fix
fa25d1c
comment
c1d1da8
remove generic
0a35b40
rm max
172c3a9
foramtting
b325b25
Add note to V4 migration
ggwpez ad19222
Add more asserts to the V5 migration
ggwpez 4093158
Make compile
ggwpez 985286c
Update frame/nomination-pools/src/migration.rs
298dacd
Make V4 migration re-usable
ggwpez 9b60539
Add MigrateV3ToV5 wrapper
ggwpez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -478,9 +478,18 @@ pub mod v4 { | |
| } | ||
| } | ||
|
|
||
| /// # Warning | ||
| /// | ||
| /// Please update to V5 immediately afterwards via `MigrateToV5`. See | ||
| /// github.com/paritytech/substrate/pull/13715 | ||
| /// | ||
| /// This migration adds a `commission` field to every `BondedPoolInner`, if | ||
| /// any. | ||
| #[deprecated( | ||
| note = "This migration MUST be followed by `MigrateToV5` to avoid mangled storage. See: github.com/paritytech/substrate/pull/13715" | ||
| )] | ||
| pub struct MigrateToV4<T, U>(sp_std::marker::PhantomData<(T, U)>); | ||
| #[allow(deprecated)] | ||
| impl<T: Config, U: Get<Perbill>> OnRuntimeUpgrade for MigrateToV4<T, U> { | ||
| fn on_runtime_upgrade() -> Weight { | ||
| let current = Pallet::<T>::current_storage_version(); | ||
|
|
@@ -494,6 +503,7 @@ pub mod v4 { | |
| ); | ||
|
|
||
| if current == 4 && onchain == 3 { | ||
| log!(warn, "Please run MigrateToV5 immediately after this migration. See github.com/paritytech/substrate/pull/13715"); | ||
| let initial_global_max_commission = U::get(); | ||
| GlobalMaxCommission::<T>::set(Some(initial_global_max_commission)); | ||
| log!( | ||
|
|
@@ -548,3 +558,147 @@ pub mod v4 { | |
| } | ||
| } | ||
| } | ||
|
|
||
| pub mod v5 { | ||
| use super::*; | ||
|
|
||
| #[derive(Decode)] | ||
| pub struct OldRewardPool<T: Config> { | ||
| last_recorded_reward_counter: T::RewardCounter, | ||
| last_recorded_total_payouts: BalanceOf<T>, | ||
| total_rewards_claimed: BalanceOf<T>, | ||
| } | ||
|
|
||
| impl<T: Config> OldRewardPool<T> { | ||
| fn migrate_to_v5(self) -> RewardPool<T> { | ||
| RewardPool { | ||
| last_recorded_reward_counter: self.last_recorded_reward_counter, | ||
| last_recorded_total_payouts: self.last_recorded_total_payouts, | ||
| total_rewards_claimed: self.total_rewards_claimed, | ||
| total_commission_pending: Zero::zero(), | ||
| total_commission_claimed: Zero::zero(), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// This migration adds`total_commission_pending` and `total_commission_claimed` field to every | ||
| /// `RewardPool`, if any. | ||
| pub struct MigrateToV5<T>(sp_std::marker::PhantomData<T>); | ||
| impl<T: Config> OnRuntimeUpgrade for MigrateToV5<T> { | ||
| fn on_runtime_upgrade() -> Weight { | ||
| let current = Pallet::<T>::current_storage_version(); | ||
| let onchain = Pallet::<T>::on_chain_storage_version(); | ||
|
|
||
| log!( | ||
| info, | ||
| "Running migration with current storage version {:?} / onchain {:?}", | ||
| current, | ||
| onchain | ||
| ); | ||
|
|
||
| if current == 5 && onchain == 4 { | ||
| let mut translated = 0u64; | ||
| RewardPools::<T>::translate::<OldRewardPool<T>, _>(|_id, old_value| { | ||
| translated.saturating_inc(); | ||
| Some(old_value.migrate_to_v5()) | ||
| }); | ||
|
|
||
| current.put::<Pallet<T>>(); | ||
| log!(info, "Upgraded {} pools, storage to version {:?}", translated, current); | ||
|
|
||
| // reads: translated + onchain version. | ||
| // writes: translated + current.put. | ||
| T::DbWeight::get().reads_writes(translated + 1, translated + 1) | ||
| } else { | ||
| log!(info, "Migration did not execute. This probably should be removed"); | ||
| T::DbWeight::get().reads(1) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(feature = "try-runtime")] | ||
| fn pre_upgrade() -> Result<Vec<u8>, &'static str> { | ||
| ensure!( | ||
| Pallet::<T>::current_storage_version() > Pallet::<T>::on_chain_storage_version(), | ||
| "the on_chain version is equal or more than the current one" | ||
| ); | ||
|
|
||
| let rpool_keys = RewardPools::<T>::iter_keys().count(); | ||
| let rpool_values = RewardPools::<T>::iter_values().count(); | ||
| if rpool_keys != rpool_values { | ||
| log!(info, "🔥 There are {} undecodable RewardPools in storage. This migration will try to correct them. keys: {}, values: {}", rpool_keys.saturating_sub(rpool_values), rpool_keys, rpool_values); | ||
| } | ||
|
|
||
| ensure!( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is all fine, but end game is have #13013. |
||
| PoolMembers::<T>::iter_keys().count() == PoolMembers::<T>::iter_values().count(), | ||
| "There are undecodable PoolMembers in storage. This migration will not fix that." | ||
| ); | ||
| ensure!( | ||
| BondedPools::<T>::iter_keys().count() == BondedPools::<T>::iter_values().count(), | ||
| "There are undecodable BondedPools in storage. This migration will not fix that." | ||
| ); | ||
| ensure!( | ||
| SubPoolsStorage::<T>::iter_keys().count() == | ||
| SubPoolsStorage::<T>::iter_values().count(), | ||
| "There are undecodable SubPools in storage. This migration will not fix that." | ||
| ); | ||
| ensure!( | ||
| Metadata::<T>::iter_keys().count() == Metadata::<T>::iter_values().count(), | ||
| "There are undecodable Metadata in storage. This migration will not fix that." | ||
| ); | ||
|
|
||
| Ok((rpool_values as u64).encode()) | ||
| } | ||
|
|
||
| #[cfg(feature = "try-runtime")] | ||
| fn post_upgrade(data: Vec<u8>) -> Result<(), &'static str> { | ||
| let old_rpool_values: u64 = Decode::decode(&mut &data[..]).unwrap(); | ||
| let rpool_keys = RewardPools::<T>::iter_keys().count() as u64; | ||
| let rpool_values = RewardPools::<T>::iter_values().count() as u64; | ||
| ensure!( | ||
| rpool_keys == rpool_values, | ||
| "There are STILL undecodable RewardPools - migration failed" | ||
| ); | ||
|
|
||
| if old_rpool_values != rpool_values { | ||
| log!( | ||
| info, | ||
| "🎉 Fixed {} undecodable RewardPools.", | ||
| rpool_values.saturating_sub(old_rpool_values) | ||
| ); | ||
| } | ||
|
|
||
| // ensure all RewardPools items now contain `total_commission_pending` and | ||
| // `total_commission_claimed` field. | ||
| ensure!( | ||
| RewardPools::<T>::iter().all(|(_, reward_pool)| reward_pool | ||
| .total_commission_pending | ||
| .is_zero() && reward_pool | ||
| .total_commission_claimed | ||
| .is_zero()), | ||
| "a commission value has been incorrectly set" | ||
| ); | ||
| ensure!(Pallet::<T>::on_chain_storage_version() == 5, "wrong storage version"); | ||
|
|
||
| // These should not have been touched - just in case. | ||
| ensure!( | ||
| PoolMembers::<T>::iter_keys().count() == PoolMembers::<T>::iter_values().count(), | ||
| "There are undecodable PoolMembers in storage." | ||
| ); | ||
| ensure!( | ||
| BondedPools::<T>::iter_keys().count() == BondedPools::<T>::iter_values().count(), | ||
| "There are undecodable BondedPools in storage." | ||
| ); | ||
| ensure!( | ||
| SubPoolsStorage::<T>::iter_keys().count() == | ||
| SubPoolsStorage::<T>::iter_values().count(), | ||
| "There are undecodable SubPools in storage." | ||
| ); | ||
| ensure!( | ||
| Metadata::<T>::iter_keys().count() == Metadata::<T>::iter_values().count(), | ||
| "There are undecodable Metadata in storage." | ||
| ); | ||
|
|
||
| Ok(()) | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.