Skip to content

Commit 61801ce

Browse files
bkonturggwpez
andauthored
Backport of fix Pools 6->7 migration (#2942) (#3093)
This should result as a patched `25.0.1` version for https://crates.io/crates/pallet-nomination-pools/25.0.0. Relates to: polkadot-fellows/runtimes#159 Co-authored-by: Oliver Tale-Yazdi <[email protected]>
1 parent 925af80 commit 61801ce

File tree

2 files changed

+60
-22
lines changed

2 files changed

+60
-22
lines changed

prdoc/pr_2942.prdoc

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0
2+
# See doc at https://raw.githubusercontent.com/paritytech/polkadot-sdk/master/prdoc/schema_user.json
3+
4+
title: Fix pallet-nomination-pools v6 to v7 migration
5+
6+
doc:
7+
- audience: Node Dev
8+
description: |
9+
Restores the behaviour of the nomination pools `V6ToV7` migration so that it still works when
10+
the pallet will be upgraded to V8 afterwards.
11+
12+
crates:
13+
- name: "pallet-nomination-pools"

substrate/frame/nomination-pools/src/migration.rs

+47-22
Original file line numberDiff line numberDiff line change
@@ -57,26 +57,9 @@ pub mod versioned {
5757
}
5858

5959
pub mod v8 {
60-
use super::*;
61-
62-
#[derive(Decode)]
63-
pub struct OldCommission<T: Config> {
64-
pub current: Option<(Perbill, T::AccountId)>,
65-
pub max: Option<Perbill>,
66-
pub change_rate: Option<CommissionChangeRate<BlockNumberFor<T>>>,
67-
pub throttle_from: Option<BlockNumberFor<T>>,
68-
}
69-
70-
#[derive(Decode)]
71-
pub struct OldBondedPoolInner<T: Config> {
72-
pub commission: OldCommission<T>,
73-
pub member_counter: u32,
74-
pub points: BalanceOf<T>,
75-
pub roles: PoolRoles<T::AccountId>,
76-
pub state: PoolState,
77-
}
60+
use super::{v7::V7BondedPoolInner, *};
7861

79-
impl<T: Config> OldBondedPoolInner<T> {
62+
impl<T: Config> V7BondedPoolInner<T> {
8063
fn migrate_to_v8(self) -> BondedPoolInner<T> {
8164
BondedPoolInner {
8265
commission: Commission {
@@ -104,7 +87,7 @@ pub mod v8 {
10487

10588
fn on_runtime_upgrade() -> Weight {
10689
let mut translated = 0u64;
107-
BondedPools::<T>::translate::<OldBondedPoolInner<T>, _>(|_key, old_value| {
90+
BondedPools::<T>::translate::<V7BondedPoolInner<T>, _>(|_key, old_value| {
10891
translated.saturating_inc();
10992
Some(old_value.migrate_to_v8())
11093
});
@@ -128,16 +111,58 @@ pub mod v8 {
128111
///
129112
/// WARNING: This migration works under the assumption that the [`BondedPools`] cannot be inflated
130113
/// arbitrarily. Otherwise this migration could fail due to too high weight.
131-
mod v7 {
114+
pub(crate) mod v7 {
132115
use super::*;
133116

117+
#[derive(Encode, Decode, MaxEncodedLen, TypeInfo, DebugNoBound, PartialEq, Clone)]
118+
#[codec(mel_bound(T: Config))]
119+
#[scale_info(skip_type_params(T))]
120+
pub struct V7Commission<T: Config> {
121+
pub current: Option<(Perbill, T::AccountId)>,
122+
pub max: Option<Perbill>,
123+
pub change_rate: Option<CommissionChangeRate<BlockNumberFor<T>>>,
124+
pub throttle_from: Option<BlockNumberFor<T>>,
125+
}
126+
127+
#[derive(Encode, Decode, MaxEncodedLen, TypeInfo, DebugNoBound, PartialEq, Clone)]
128+
#[codec(mel_bound(T: Config))]
129+
#[scale_info(skip_type_params(T))]
130+
pub struct V7BondedPoolInner<T: Config> {
131+
pub commission: V7Commission<T>,
132+
pub member_counter: u32,
133+
pub points: BalanceOf<T>,
134+
pub roles: PoolRoles<T::AccountId>,
135+
pub state: PoolState,
136+
}
137+
138+
#[allow(dead_code)]
139+
#[derive(RuntimeDebugNoBound)]
140+
#[cfg_attr(feature = "std", derive(Clone, PartialEq))]
141+
pub struct V7BondedPool<T: Config> {
142+
/// The identifier of the pool.
143+
id: PoolId,
144+
/// The inner fields.
145+
inner: V7BondedPoolInner<T>,
146+
}
147+
148+
impl<T: Config> V7BondedPool<T> {
149+
fn bonded_account(&self) -> T::AccountId {
150+
Pallet::<T>::create_bonded_account(self.id)
151+
}
152+
}
153+
154+
// NOTE: We cannot put a V7 prefix here since that would change the storage key.
155+
#[frame_support::storage_alias]
156+
pub type BondedPools<T: Config> =
157+
CountedStorageMap<Pallet<T>, Twox64Concat, PoolId, V7BondedPoolInner<T>>;
158+
134159
pub struct VersionUncheckedMigrateV6ToV7<T>(sp_std::marker::PhantomData<T>);
135160
impl<T: Config> VersionUncheckedMigrateV6ToV7<T> {
136161
fn calculate_tvl_by_total_stake() -> BalanceOf<T> {
137162
BondedPools::<T>::iter()
138163
.map(|(id, inner)| {
139164
T::Staking::total_stake(
140-
&BondedPool { id, inner: inner.clone() }.bonded_account(),
165+
&V7BondedPool { id, inner: inner.clone() }.bonded_account(),
141166
)
142167
.unwrap_or_default()
143168
})

0 commit comments

Comments
 (0)