Skip to content

Commit

Permalink
WIP: (DNM): check Oliver's migration fix from paritytech/polkadot-sdk…
Browse files Browse the repository at this point in the history
  • Loading branch information
bkontur committed Jan 16, 2024
1 parent 00909ef commit 78ec75d
Showing 1 changed file with 118 additions and 1 deletion.
119 changes: 118 additions & 1 deletion relay/kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1687,7 +1687,8 @@ pub mod migrations {
pallet_nomination_pools::migration::versioned::V6ToV7<Runtime>,
pallet_staking::migrations::v14::MigrateToV14<Runtime>,
parachains_configuration::migration::v10::MigrateToV10<Runtime>,
pallet_nomination_pools::migration::versioned::V7ToV8<Runtime>,
// TODO:(PR#137) - replace with fixed/released version
crate::test_oliverfix_migration::V7ToV8<Runtime>,
);
}

Expand Down Expand Up @@ -2775,3 +2776,119 @@ mod init_state_migration {
}
}
}

mod test_oliverfix_migration {
use super::*;
use frame_support::{
traits::OnRuntimeUpgrade, DebugNoBound, RuntimeDebugNoBound, Twox64Concat,
};
use frame_system::pallet_prelude::BlockNumberFor;
use pallet_nomination_pools::*;
use sp_runtime::Saturating;

pub type V7ToV8<T> = frame_support::migrations::VersionedMigration<
7,
8,
v8::VersionUncheckedMigrateV7ToV8<T>,
pallet_nomination_pools::pallet::Pallet<T>,
<T as frame_system::Config>::DbWeight,
>;

pub mod v8 {
use super::*;

use super::v7::BondedPoolInner as OldBondedPoolInner;

impl<T: Config> OldBondedPoolInner<T> {
fn migrate_to_v8(self) -> BondedPoolInner<T> {
BondedPoolInner {
commission: Commission {
current: self.commission.current,
max: self.commission.max,
change_rate: self.commission.change_rate,
throttle_from: self.commission.throttle_from,
// `claim_permission` is a new field.
claim_permission: None,
},
member_counter: self.member_counter,
points: self.points,
roles: self.roles,
state: self.state,
}
}
}

pub struct VersionUncheckedMigrateV7ToV8<T>(sp_std::marker::PhantomData<T>);

impl<T: Config> OnRuntimeUpgrade for VersionUncheckedMigrateV7ToV8<T> {
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, TryRuntimeError> {
Ok(Vec::new())
}

fn on_runtime_upgrade() -> Weight {
let mut translated = 0u64;
BondedPools::<T>::translate::<OldBondedPoolInner<T>, _>(|_key, old_value| {
translated.saturating_inc();
Some(old_value.migrate_to_v8())
});
T::DbWeight::get().reads_writes(translated, translated + 1)
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(_: Vec<u8>) -> Result<(), TryRuntimeError> {
// Check new `claim_permission` field is present.
ensure!(
BondedPools::<T>::iter()
.all(|(_, inner)| inner.commission.claim_permission.is_none()),
"`claim_permission` value has not been set correctly."
);
Ok(())
}
}
}

mod v7 {
use super::*;

#[derive(Encode, Decode, MaxEncodedLen, TypeInfo, DebugNoBound, PartialEq, Clone)]
#[codec(mel_bound(T: Config))]
#[scale_info(skip_type_params(T))]
pub struct Commission<T: Config> {
pub current: Option<(Perbill, T::AccountId)>,
pub max: Option<Perbill>,
pub change_rate: Option<CommissionChangeRate<BlockNumberFor<T>>>,
pub throttle_from: Option<BlockNumberFor<T>>,
}

#[derive(Encode, Decode, MaxEncodedLen, TypeInfo, DebugNoBound, PartialEq, Clone)]
#[codec(mel_bound(T: Config))]
#[scale_info(skip_type_params(T))]
pub struct BondedPoolInner<T: Config> {
pub commission: Commission<T>,
pub member_counter: u32,
pub points: BalanceOf<T>,
pub roles: PoolRoles<T::AccountId>,
pub state: PoolState,
}

#[derive(RuntimeDebugNoBound)]
#[cfg_attr(feature = "std", derive(Clone, PartialEq))]
pub struct BondedPool<T: Config> {
/// The identifier of the pool.
id: PoolId,
/// The inner fields.
inner: BondedPoolInner<T>,
}

impl<T: Config> BondedPool<T> {
fn bonded_account(&self) -> T::AccountId {
Pallet::<T>::create_bonded_account(self.id)
}
}

#[frame_support::storage_alias]
pub type BondedPools<T: Config> =
CountedStorageMap<Pallet<T>, Twox64Concat, PoolId, BondedPoolInner<T>>;
}
}

0 comments on commit 78ec75d

Please sign in to comment.