-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Fix Society v2 migration #14421
Fix Society v2 migration #14421
Changes from 18 commits
984ac11
d67ae41
93cca30
e7b08e1
315c1f4
54f3108
7ea67b3
039a55b
81725a2
4929c1a
ab2aa97
61af5d9
46913c3
8cce590
88e0836
af97daa
0b4abd8
400304c
afd9e93
c7e4ca3
e1f3c01
1a6f08e
568440b
2897ad5
039d3e1
c1dd3e7
f17bf46
b07294a
a769e38
b039f09
f344dcb
93d7ae4
803d114
e1c2599
1f9e5fe
87b3ee1
9f0e4f9
28c36cc
9878670
3461900
5b6b1a1
b0f08e0
20083ca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,10 @@ | |
|
||
use super::*; | ||
use codec::{Decode, Encode}; | ||
use frame_support::traits::{Instance, OnRuntimeUpgrade}; | ||
use frame_support::{ | ||
migrations::VersionedRuntimeUpgrade, | ||
traits::{Instance, OnRuntimeUpgrade}, | ||
}; | ||
|
||
#[cfg(feature = "try-runtime")] | ||
use sp_runtime::TryRuntimeError; | ||
|
@@ -28,15 +31,15 @@ use sp_runtime::TryRuntimeError; | |
const TARGET: &'static str = "runtime::society::migration"; | ||
|
||
/// This migration moves all the state to v2 of Society. | ||
pub struct MigrateToV2<T: Config<I>, I: 'static, PastPayouts>( | ||
pub struct VersionUncheckedMigrateToV2<T: Config<I>, I: 'static, PastPayouts>( | ||
sp_std::marker::PhantomData<(T, I, PastPayouts)>, | ||
); | ||
|
||
impl< | ||
T: Config<I>, | ||
I: Instance + 'static, | ||
PastPayouts: Get<Vec<(<T as frame_system::Config>::AccountId, BalanceOf<T, I>)>>, | ||
> OnRuntimeUpgrade for MigrateToV2<T, I, PastPayouts> | ||
> OnRuntimeUpgrade for VersionUncheckedMigrateToV2<T, I, PastPayouts> | ||
{ | ||
#[cfg(feature = "try-runtime")] | ||
fn pre_upgrade() -> Result<Vec<u8>, TryRuntimeError> { | ||
|
@@ -50,14 +53,20 @@ impl< | |
} | ||
|
||
fn on_runtime_upgrade() -> Weight { | ||
let current = Pallet::<T, I>::current_storage_version(); | ||
let onchain = Pallet::<T, I>::on_chain_storage_version(); | ||
if current == 2 && onchain == 0 { | ||
from_original::<T, I>(&mut PastPayouts::get()) | ||
} else { | ||
if onchain < 2 { | ||
log::info!( | ||
"Running migration with current storage version {:?} / onchain {:?}", | ||
current, | ||
target: TARGET, | ||
"Running migration against onchain version {:?}", | ||
onchain | ||
); | ||
let weight = from_original::<T, I>(&mut PastPayouts::get()); | ||
crate::STORAGE_VERSION.put::<Pallet<T, I>>(); | ||
weight | ||
} else { | ||
log::warn!( | ||
liamaharon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
target: TARGET, | ||
"Migration is a noop. onchain version: {:?}", | ||
onchain | ||
); | ||
T::DbWeight::get().reads(1) | ||
|
@@ -95,6 +104,14 @@ impl< | |
} | ||
} | ||
|
||
pub type VersionCheckedMigrateToV2<Runtime, Pallet, I, PastPayouts> = VersionedRuntimeUpgrade< | ||
0, | ||
2, | ||
VersionUncheckedMigrateToV2<Runtime, I, PastPayouts>, | ||
Pallet, | ||
<Runtime as frame_system::Config>::DbWeight, | ||
liamaharon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
>; | ||
|
||
pub(crate) mod old { | ||
use super::*; | ||
use frame_support::storage_alias; | ||
|
@@ -181,7 +198,7 @@ pub(crate) mod old { | |
} | ||
|
||
pub fn can_migrate<T: Config<I>, I: Instance + 'static>() -> bool { | ||
old::Members::<T, I>::exists() | ||
Members::<T, I>::iter().next().is_none() | ||
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. Rococo 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. Now you only return true if the list is empty? I'm confused. 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.
|
||
} | ||
|
||
/// Will panic if there are any inconsistencies in the pallet's state or old keys remaining. | ||
|
@@ -236,9 +253,9 @@ pub fn assert_internal_consistency<T: Config<I>, I: Instance + 'static>() { | |
pub fn from_original<T: Config<I>, I: Instance + 'static>( | ||
past_payouts: &mut [(<T as frame_system::Config>::AccountId, BalanceOf<T, I>)], | ||
) -> Weight { | ||
// First check that this is the original state layout. This is easy since the original layout | ||
// contained the Members value, and this value no longer exists in the new layout. | ||
if !old::Members::<T, I>::exists() { | ||
// First check that this is the original state layout. This is easy since the new layout | ||
// contains a new Members value, which did not exist in the old layout. | ||
if Members::<T, I>::iter().next().is_some() { | ||
liamaharon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
log::warn!(target: TARGET, "Skipping MigrateToV2 migration since it appears unapplicable"); | ||
// Already migrated or no data to migrate: Bail. | ||
return T::DbWeight::get().reads(1) | ||
|
@@ -281,6 +298,31 @@ pub fn from_original<T: Config<I>, I: Instance + 'static>( | |
let record = MemberRecord { index: member_count, rank: 0, strikes, vouching }; | ||
Members::<T, I>::insert(&member, record); | ||
MemberByIndex::<T, I>::insert(member_count, &member); | ||
|
||
// The founder must be the first member in Society V2. If we find the founder not in index | ||
// zero, we swap it with the first member. | ||
if member == Founder::<T, I>::get().expect("founder is always set; qed") && member_count > 0 | ||
liamaharon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
let member_to_swap = MemberByIndex::<T, I>::get(0) | ||
.expect("member_count > 0, we must have at least 1 member; qed"); | ||
liamaharon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Swap the founder with the first member in MemberByIndex. | ||
MemberByIndex::<T, I>::swap(0, member_count); | ||
// Update the indicies of the swapped member MemberRecords. | ||
Members::<T, I>::mutate(&member, |m| { | ||
if let Some(member) = m { | ||
member.index = 0; | ||
} else { | ||
log::error!("Member somehow disapeared from storage after it was inserted") | ||
liamaharon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
}); | ||
Members::<T, I>::mutate(&member_to_swap, |m| { | ||
if let Some(member) = m { | ||
member.index = member_count; | ||
} else { | ||
log::error!("Member somehow disapeared from storage after it was queried") | ||
} | ||
}); | ||
} | ||
member_count.saturating_inc(); | ||
} | ||
MemberCount::<T, I>::put(member_count); | ||
|
Uh oh!
There was an error while loading. Please reload this page.