This repository has been 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.6k
Storage migration to remove founder role #12766
Merged
joepetrowski
merged 4 commits into
joe-alliance-foundingfellows
from
muharem-alliance-migration-founders-role-2
Nov 23, 2022
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 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 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 |
---|---|---|
|
@@ -20,7 +20,7 @@ use frame_support::{pallet_prelude::*, storage::migration, traits::OnRuntimeUpgr | |
use log; | ||
|
||
/// The current storage version. | ||
pub const STORAGE_VERSION: StorageVersion = StorageVersion::new(1); | ||
pub const STORAGE_VERSION: StorageVersion = StorageVersion::new(2); | ||
|
||
/// Wrapper for all migrations of this pallet. | ||
pub fn migrate<T: Config<I>, I: 'static>() -> Weight { | ||
|
@@ -31,6 +31,10 @@ pub fn migrate<T: Config<I>, I: 'static>() -> Weight { | |
weight = weight.saturating_add(v0_to_v1::migrate::<T, I>()); | ||
} | ||
|
||
if onchain_version < 2 { | ||
weight = weight.saturating_add(v1_to_v2::migrate::<T, I>()); | ||
} | ||
|
||
STORAGE_VERSION.put::<Pallet<T, I>>(); | ||
weight = weight.saturating_add(T::DbWeight::get().writes(1)); | ||
|
||
|
@@ -77,3 +81,99 @@ mod v0_to_v1 { | |
T::DbWeight::get().writes(res.unique.into()) | ||
} | ||
} | ||
|
||
/// v1_to_v2: `Members` storage map collapses `Founder` and `Fellow` keys into one `Fellow`. | ||
/// Total number of `Founder`s and `Fellow`s must not be higher than `T::MaxMembersCount`. | ||
pub(crate) mod v1_to_v2 { | ||
use super::*; | ||
use crate::{MemberRole, Members}; | ||
|
||
/// V1 Role set. | ||
#[derive(Copy, Clone, PartialEq, Eq, Encode, Decode, TypeInfo, MaxEncodedLen)] | ||
pub enum MemberRoleV1 { | ||
Founder, | ||
Fellow, | ||
Ally, | ||
Retiring, | ||
} | ||
|
||
pub fn migrate<T: Config<I>, I: 'static>() -> Weight { | ||
log::info!(target: LOG_TARGET, "Running migration v1_to_v2: `Members` storage map collapses `Founder` and `Fellow` keys into one `Fellow`."); | ||
// fetch into the scope all members. | ||
let founders_vec = take_members::<T, I>(MemberRoleV1::Founder).into_inner(); | ||
let mut fellows_vec = take_members::<T, I>(MemberRoleV1::Fellow).into_inner(); | ||
let allies = take_members::<T, I>(MemberRoleV1::Ally); | ||
let retiring = take_members::<T, I>(MemberRoleV1::Retiring); | ||
Comment on lines
+105
to
+106
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. Is there any need to include these? |
||
if founders_vec | ||
.len() | ||
.saturating_add(fellows_vec.len()) | ||
.saturating_add(allies.len()) | ||
.saturating_add(retiring.len()) == | ||
0 | ||
{ | ||
return T::DbWeight::get().reads(4) | ||
} | ||
log::info!( | ||
target: LOG_TARGET, | ||
"Members storage v1 contains, '{}' founders, '{}' fellows, '{}' allies, '{}' retiring members.", | ||
founders_vec.len(), | ||
fellows_vec.len(), | ||
allies.len(), | ||
retiring.len(), | ||
); | ||
// merge founders with fellows and sort. | ||
fellows_vec.extend(founders_vec); | ||
fellows_vec.sort(); | ||
if fellows_vec.len() as u32 > T::MaxMembersCount::get() { | ||
log::error!( | ||
target: LOG_TARGET, | ||
"Merged list of founders and fellows do not fit into `T::MaxMembersCount` bound. Truncating the merged set into max members count." | ||
); | ||
fellows_vec.truncate(T::MaxMembersCount::get() as usize); | ||
} | ||
let fellows: BoundedVec<T::AccountId, T::MaxMembersCount> = | ||
fellows_vec.try_into().unwrap_or_default(); | ||
// insert members with new storage map key. | ||
Members::<T, I>::insert(&MemberRole::Fellow, fellows.clone()); | ||
Members::<T, I>::insert(&MemberRole::Ally, allies.clone()); | ||
Members::<T, I>::insert(&MemberRole::Retiring, retiring.clone()); | ||
log::info!( | ||
target: LOG_TARGET, | ||
"Members storage updated with, '{}' fellows, '{}' allies, '{}' retiring members.", | ||
fellows.len(), | ||
allies.len(), | ||
retiring.len(), | ||
); | ||
T::DbWeight::get().reads_writes(4, 4) | ||
} | ||
|
||
fn take_members<T: Config<I>, I: 'static>( | ||
role: MemberRoleV1, | ||
) -> BoundedVec<T::AccountId, T::MaxMembersCount> { | ||
migration::take_storage_item::< | ||
MemberRoleV1, | ||
BoundedVec<T::AccountId, T::MaxMembersCount>, | ||
Twox64Concat, | ||
>(<Pallet<T, I>>::name().as_bytes(), b"Members", role) | ||
.unwrap_or_default() | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
use crate::{mock::*, MemberRole}; | ||
|
||
#[test] | ||
fn migration_v1_to_v2_works() { | ||
new_test_ext().execute_with(|| { | ||
assert_ok!(Alliance::join_alliance(RuntimeOrigin::signed(4))); | ||
assert_eq!(Alliance::members(MemberRole::Ally), vec![4]); | ||
assert_eq!(Alliance::members(MemberRole::Fellow), vec![1, 2, 3]); | ||
v1_to_v2::migrate::<Test, ()>(); | ||
assert_eq!(Alliance::members(MemberRole::Fellow), vec![1, 2, 3, 4]); | ||
assert_eq!(Alliance::members(MemberRole::Ally), vec![]); | ||
assert_eq!(Alliance::members(MemberRole::Retiring), vec![]); | ||
}); | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is used to be MaxFellows and MaxFounders, now its just per role