From c0be8e818a2b478d29c107b852fd584fb3d2ab2d Mon Sep 17 00:00:00 2001 From: Oliver Tale-Yazdi Date: Tue, 28 Jan 2025 22:44:35 +0100 Subject: [PATCH 01/10] AHM Fast unstake Signed-off-by: Oliver Tale-Yazdi --- Cargo.lock | 3 + pallets/ah-migrator/Cargo.toml | 1 + pallets/ah-migrator/src/lib.rs | 31 ++- .../ah-migrator/src/staking/fast_unstake.rs | 68 +++++++ pallets/ah-migrator/src/staking/mod.rs | 1 + pallets/rc-migrator/Cargo.toml | 1 + pallets/rc-migrator/src/lib.rs | 44 +++- .../rc-migrator/src/staking/fast_unstake.rs | 192 ++++++++++++++++++ pallets/rc-migrator/src/staking/mod.rs | 1 + pallets/rc-migrator/src/types.rs | 2 + .../asset-hubs/asset-hub-polkadot/Cargo.toml | 1 + .../asset-hubs/asset-hub-polkadot/src/lib.rs | 1 + .../asset-hub-polkadot/src/staking/mod.rs | 13 ++ 13 files changed, 357 insertions(+), 2 deletions(-) create mode 100644 pallets/ah-migrator/src/staking/fast_unstake.rs create mode 100644 pallets/rc-migrator/src/staking/fast_unstake.rs diff --git a/Cargo.lock b/Cargo.lock index 0353bf239a..d87728ad87 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -790,6 +790,7 @@ dependencies = [ "pallet-authorship", "pallet-balances", "pallet-collator-selection", + "pallet-fast-unstake", "pallet-message-queue", "pallet-multisig", "pallet-nfts", @@ -7637,6 +7638,7 @@ dependencies = [ "hex-literal", "log", "pallet-balances", + "pallet-fast-unstake", "pallet-multisig", "pallet-nomination-pools", "pallet-preimage", @@ -8866,6 +8868,7 @@ dependencies = [ "hex-literal", "log", "pallet-balances", + "pallet-fast-unstake", "pallet-multisig", "pallet-nomination-pools", "pallet-preimage", diff --git a/pallets/ah-migrator/Cargo.toml b/pallets/ah-migrator/Cargo.toml index 0629a6cd4e..62fe5accec 100644 --- a/pallets/ah-migrator/Cargo.toml +++ b/pallets/ah-migrator/Cargo.toml @@ -14,6 +14,7 @@ frame-support = { workspace = true } frame-system = { workspace = true } log = { workspace = true } pallet-balances = { workspace = true } +pallet-fast-unstake = { workspace = true } pallet-multisig = { workspace = true } pallet-nomination-pools = { workspace = true } pallet-preimage = { workspace = true } diff --git a/pallets/ah-migrator/src/lib.rs b/pallets/ah-migrator/src/lib.rs index 4839178610..0fd6048120 100644 --- a/pallets/ah-migrator/src/lib.rs +++ b/pallets/ah-migrator/src/lib.rs @@ -51,7 +51,14 @@ use frame_support::{ use frame_system::pallet_prelude::*; use pallet_balances::{AccountData, Reasons as LockReasons}; use pallet_rc_migrator::{ - accounts::Account as RcAccount, multisig::*, preimage::*, proxy::*, staking::nom_pools::*, + accounts::Account as RcAccount, + multisig::*, + preimage::*, + proxy::*, + staking::{ + fast_unstake::{FastUnstakeMigrator, FastUnstakeStorageValues, RcFastUnstakeMessage}, + nom_pools::*, + }, }; use sp_application_crypto::Ss58Codec; use sp_core::H256; @@ -71,6 +78,11 @@ type RcAccountFor = RcAccount< ::RcFreezeReason, >; +#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)] +pub enum PalletEventName { + FastUnstake, +} + #[frame_support::pallet(dev_mode)] pub mod pallet { use super::*; @@ -85,6 +97,7 @@ pub mod pallet { + pallet_proxy::Config + pallet_preimage::Config + pallet_nomination_pools::Config + + pallet_fast_unstake::Config { /// The overarching event type. type RuntimeEvent: From> + IsType<::RuntimeEvent>; @@ -132,6 +145,8 @@ pub mod pallet { FailedToUnreserveDeposit, /// Failed to process an account data from RC. FailedToProcessAccount, + /// Some item could not be inserted because it already exists. + InsertConflict, } #[pallet::event] @@ -234,6 +249,10 @@ pub mod pallet { /// How many nom pools messages failed to integrate. count_bad: u32, }, + /// We received a batch of messages that will be integrated into a pallet. + BatchReceived { pallet: PalletEventName, count: u32 }, + /// We processed a batch of messages for this pallet. + BatchProcessed { pallet: PalletEventName, count_good: u32, count_bad: u32 }, } #[pallet::pallet] @@ -335,6 +354,16 @@ pub mod pallet { Self::do_receive_nom_pools_messages(messages).map_err(Into::into) } + + #[pallet::call_index(8)] + pub fn receive_fast_unstake_messages( + origin: OriginFor, + messages: Vec>, + ) -> DispatchResult { + ensure_root(origin)?; + + Self::do_receive_fast_unstake_messages(messages).map_err(Into::into) + } } #[pallet::hooks] diff --git a/pallets/ah-migrator/src/staking/fast_unstake.rs b/pallets/ah-migrator/src/staking/fast_unstake.rs new file mode 100644 index 0000000000..ad2f21ae78 --- /dev/null +++ b/pallets/ah-migrator/src/staking/fast_unstake.rs @@ -0,0 +1,68 @@ +// This file is part of Substrate. + +// Copyright (C) Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: Apache-2.0 + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! Fast unstake migration logic. + +use crate::{types::*, *}; + +impl Pallet { + pub fn do_receive_fast_unstake_messages( + messages: Vec>, + ) -> Result<(), Error> { + let (mut good, mut bad) = (0, 0); + log::info!("Integrating {} FastUnstakeMessages", messages.len()); + Self::deposit_event(Event::BatchReceived { + pallet: PalletEventName::FastUnstake, + count: messages.len() as u32, + }); + + for message in messages { + match Self::do_receive_fast_unstake_message(message) { + Ok(_) => good += 1, + Err(_) => bad += 1, + } + } + + Self::deposit_event(Event::BatchProcessed { + pallet: PalletEventName::FastUnstake, + count_good: good as u32, + count_bad: bad as u32, + }); + + Ok(()) + } + + pub fn do_receive_fast_unstake_message( + message: RcFastUnstakeMessage, + ) -> Result<(), Error> { + match message { + RcFastUnstakeMessage::StorageValues { values } => { + FastUnstakeMigrator::::put_values(values); + log::debug!("Integrating FastUnstakeStorageValues"); + }, + RcFastUnstakeMessage::Queue { member } => { + if pallet_fast_unstake::Queue::::contains_key(&member.0) { + return Err(Error::::InsertConflict); + } + log::debug!("Integrating FastUnstakeQueueMember: {:?}", &member.0); + pallet_fast_unstake::Queue::::insert(member.0, member.1); + }, + } + + Ok(()) + } +} diff --git a/pallets/ah-migrator/src/staking/mod.rs b/pallets/ah-migrator/src/staking/mod.rs index 1c369f3be7..7fc26e3012 100644 --- a/pallets/ah-migrator/src/staking/mod.rs +++ b/pallets/ah-migrator/src/staking/mod.rs @@ -17,4 +17,5 @@ //! Staking migration logic. +pub mod fast_unstake; pub mod nom_pools; diff --git a/pallets/rc-migrator/Cargo.toml b/pallets/rc-migrator/Cargo.toml index 71c0df6393..b310a08216 100644 --- a/pallets/rc-migrator/Cargo.toml +++ b/pallets/rc-migrator/Cargo.toml @@ -24,6 +24,7 @@ pallet-staking = { workspace = true } pallet-proxy = { workspace = true } pallet-multisig = { workspace = true } pallet-preimage = { workspace = true } +pallet-fast-unstake = { workspace = true } pallet-nomination-pools = { workspace = true } polkadot-runtime-common = { workspace = true } runtime-parachains = { workspace = true } diff --git a/pallets/rc-migrator/src/lib.rs b/pallets/rc-migrator/src/lib.rs index ea81c7c432..2a746455df 100644 --- a/pallets/rc-migrator/src/lib.rs +++ b/pallets/rc-migrator/src/lib.rs @@ -70,7 +70,10 @@ use preimage::{ PreimageChunkMigrator, PreimageLegacyRequestStatusMigrator, PreimageRequestStatusMigrator, }; use proxy::*; -use staking::nom_pools::{NomPoolsMigrator, NomPoolsStage}; +use staking::{ + fast_unstake::{FastUnstakeMigrator, FastUnstakeStage}, + nom_pools::{NomPoolsMigrator, NomPoolsStage}, +}; use types::PalletMigration; /// The log target of this pallet. @@ -153,6 +156,11 @@ pub enum MigrationStage { next_key: Option>, }, NomPoolsMigrationDone, + FastUnstakeMigrationInit, + FastUnstakeMigrationOngoing { + next_key: Option>, + }, + FastUnstakeMigrationDone, MigrationDone, } @@ -194,6 +202,7 @@ pub mod pallet { + pallet_proxy::Config + pallet_preimage::Config + pallet_nomination_pools::Config + + pallet_fast_unstake::Config { /// The overarching event type. type RuntimeEvent: From> + IsType<::RuntimeEvent>; @@ -569,6 +578,39 @@ pub mod pallet { } }, MigrationStage::NomPoolsMigrationDone => { + Self::transition(MigrationStage::FastUnstakeMigrationInit); + }, + MigrationStage::FastUnstakeMigrationInit => { + Self::transition(MigrationStage::FastUnstakeMigrationOngoing { + next_key: None, + }); + }, + MigrationStage::FastUnstakeMigrationOngoing { next_key } => { + let res = with_transaction_opaque_err::, Error, _>(|| { + match FastUnstakeMigrator::::migrate_many(next_key, &mut weight_counter) + { + Ok(last_key) => TransactionOutcome::Commit(Ok(last_key)), + Err(e) => TransactionOutcome::Rollback(Err(e)), + } + }) + .expect("Always returning Ok; qed"); + + match res { + Ok(None) => { + Self::transition(MigrationStage::FastUnstakeMigrationDone); + }, + Ok(Some(next_key)) => { + Self::transition(MigrationStage::FastUnstakeMigrationOngoing { + next_key: Some(next_key), + }); + }, + e => { + log::error!(target: LOG_TARGET, "Error while migrating fast unstake: {:?}", e); + defensive!("Error while migrating fast unstake"); + }, + } + }, + MigrationStage::FastUnstakeMigrationDone => { Self::transition(MigrationStage::MigrationDone); }, MigrationStage::MigrationDone => (), diff --git a/pallets/rc-migrator/src/staking/fast_unstake.rs b/pallets/rc-migrator/src/staking/fast_unstake.rs new file mode 100644 index 0000000000..de9a718a8b --- /dev/null +++ b/pallets/rc-migrator/src/staking/fast_unstake.rs @@ -0,0 +1,192 @@ +// Copyright (C) Parity Technologies (UK) Ltd. +// This file is part of Polkadot. + +// Polkadot is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Polkadot is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Polkadot. If not, see . + +//! Nomination pools data migrator module. + +use crate::{types::*, *}; +use alias::UnstakeRequest; + +#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)] +pub enum FastUnstakeStage { + StorageValues, + Queue(Option), + Finished, +} + +#[derive( + Encode, + Decode, + MaxEncodedLen, + TypeInfo, + RuntimeDebugNoBound, + CloneNoBound, + PartialEqNoBound, + EqNoBound, +)] +#[codec(mel_bound(T: Config))] +#[scale_info(skip_type_params(T))] +pub enum RcFastUnstakeMessage { + StorageValues { values: FastUnstakeStorageValues }, + Queue { member: (T::AccountId, alias::BalanceOf) }, +} + +/// All the `StorageValues` from the fast unstake pallet. +#[derive( + Encode, + Decode, + MaxEncodedLen, + TypeInfo, + CloneNoBound, + PartialEqNoBound, + EqNoBound, + RuntimeDebugNoBound, +)] +#[codec(mel_bound(T: Config))] +#[scale_info(skip_type_params(T))] +pub struct FastUnstakeStorageValues { + pub head: Option>, + pub eras_to_check_per_block: u32, +} + +impl FastUnstakeMigrator { + pub fn take_values() -> FastUnstakeStorageValues { + FastUnstakeStorageValues { + head: alias::Head::::take(), + eras_to_check_per_block: pallet_fast_unstake::ErasToCheckPerBlock::::take(), + } + } + + pub fn put_values(values: FastUnstakeStorageValues) { + alias::Head::::set(values.head); + pallet_fast_unstake::ErasToCheckPerBlock::::put(values.eras_to_check_per_block); + } +} + +pub struct FastUnstakeMigrator { + _phantom: PhantomData, +} + +impl PalletMigration for FastUnstakeMigrator { + type Key = FastUnstakeStage; + type Error = Error; + + fn migrate_many( + mut current_key: Option, + weight_counter: &mut WeightMeter, + ) -> Result, Self::Error> { + let mut inner_key = current_key.unwrap_or(FastUnstakeStage::StorageValues); + let mut messages = Vec::new(); + + loop { + if weight_counter + .try_consume(::DbWeight::get().reads_writes(1, 1)) + .is_err() + { + if messages.is_empty() { + return Err(Error::OutOfWeight); + } else { + break; + } + } + if messages.len() > 10_000 { + log::warn!("Weight allowed very big batch, stopping"); + break; + } + + inner_key = match inner_key { + FastUnstakeStage::StorageValues => { + let values = Self::take_values(); + messages.push(RcFastUnstakeMessage::StorageValues { values }); + FastUnstakeStage::Queue(None) + }, + FastUnstakeStage::Queue(queue_iter) => { + let mut new_queue_iter = match queue_iter.clone() { + Some(queue_iter) => pallet_fast_unstake::Queue::::iter_from( + pallet_fast_unstake::Queue::::hashed_key_for(queue_iter), + ), + None => pallet_fast_unstake::Queue::::iter(), + }; + + match new_queue_iter.next() { + Some((key, member)) => { + pallet_fast_unstake::Queue::::remove(&key); + messages.push(RcFastUnstakeMessage::Queue { + member: (key.clone(), member), + }); + FastUnstakeStage::Queue(Some(key)) + }, + None => FastUnstakeStage::Finished, + } + }, + FastUnstakeStage::Finished => { + break; + }, + } + } + + if !messages.is_empty() { + Pallet::::send_chunked_xcm(messages, |messages| { + types::AhMigratorCall::::ReceiveFastUnstakeMessages { messages } + })?; + } + + if inner_key == FastUnstakeStage::Finished { + Ok(None) + } else { + Ok(Some(inner_key)) + } + } +} + +pub mod alias { + use super::*; + use frame_support::traits::Currency; + use pallet_fast_unstake::types::*; + use sp_staking::EraIndex; + + pub type BalanceOf = <::Currency as Currency< + ::AccountId, + >>::Balance; + + /// An unstake request. + /// + /// This is stored in [`crate::Head`] storage item and points to the current unstake request + /// that is being processed. + // From https://github.com/paritytech/polkadot-sdk/blob/7ecf3f757a5d6f622309cea7f788e8a547a5dce8/substrate/frame/fast-unstake/src/types.rs#L48-L57 + #[derive( + Encode, + Decode, + EqNoBound, + PartialEqNoBound, + CloneNoBound, + TypeInfo, + RuntimeDebugNoBound, + MaxEncodedLen, + )] + #[scale_info(skip_type_params(T))] + pub struct UnstakeRequest { + /// This list of stashes are being processed in this request, and their corresponding + /// deposit. + pub stashes: BoundedVec<(T::AccountId, BalanceOf), T::BatchSize>, + /// The list of eras for which they have been checked. + pub checked: BoundedVec>, + } + + // From https://github.com/paritytech/polkadot-sdk/blob/7ecf3f757a5d6f622309cea7f788e8a547a5dce8/substrate/frame/fast-unstake/src/lib.rs#L213-L214 + #[frame_support::storage_alias(pallet_name)] + pub type Head = + StorageValue, UnstakeRequest, OptionQuery>; +} diff --git a/pallets/rc-migrator/src/staking/mod.rs b/pallets/rc-migrator/src/staking/mod.rs index 0d90cfafc7..4fe042f9da 100644 --- a/pallets/rc-migrator/src/staking/mod.rs +++ b/pallets/rc-migrator/src/staking/mod.rs @@ -14,5 +14,6 @@ // You should have received a copy of the GNU General Public License // along with Polkadot. If not, see . +pub mod fast_unstake; pub mod nom_pools; pub mod nom_pools_alias; diff --git a/pallets/rc-migrator/src/types.rs b/pallets/rc-migrator/src/types.rs index ddbd6a8135..4e933249df 100644 --- a/pallets/rc-migrator/src/types.rs +++ b/pallets/rc-migrator/src/types.rs @@ -46,6 +46,8 @@ pub enum AhMigratorCall { ReceivePreimageLegacyStatus { legacy_status: Vec> }, #[codec(index = 7)] ReceiveNomPoolsMessages { messages: Vec> }, + #[codec(index = 8)] + ReceiveFastUnstakeMessages { messages: Vec> }, } /// Copy of `ParaInfo` type from `paras_registrar` pallet. diff --git a/system-parachains/asset-hubs/asset-hub-polkadot/Cargo.toml b/system-parachains/asset-hubs/asset-hub-polkadot/Cargo.toml index 4d8050a7ee..f322b48741 100644 --- a/system-parachains/asset-hubs/asset-hub-polkadot/Cargo.toml +++ b/system-parachains/asset-hubs/asset-hub-polkadot/Cargo.toml @@ -43,6 +43,7 @@ pallet-assets = { workspace = true } pallet-aura = { workspace = true } pallet-authorship = { workspace = true } pallet-balances = { workspace = true } +pallet-fast-unstake = { workspace = true } pallet-message-queue = { workspace = true } pallet-multisig = { workspace = true } pallet-nfts = { workspace = true } diff --git a/system-parachains/asset-hubs/asset-hub-polkadot/src/lib.rs b/system-parachains/asset-hubs/asset-hub-polkadot/src/lib.rs index e7fbb2d86d..f7d6f5db6b 100644 --- a/system-parachains/asset-hubs/asset-hub-polkadot/src/lib.rs +++ b/system-parachains/asset-hubs/asset-hub-polkadot/src/lib.rs @@ -1092,6 +1092,7 @@ construct_runtime!( // Staking in the 70s NominationPools: pallet_nomination_pools = 70, + FastUnstake: pallet_fast_unstake = 71, // Asset Hub Migrator AhMigrator: pallet_ah_migrator = 255, diff --git a/system-parachains/asset-hubs/asset-hub-polkadot/src/staking/mod.rs b/system-parachains/asset-hubs/asset-hub-polkadot/src/staking/mod.rs index 74d5e0dbf1..2629b48514 100644 --- a/system-parachains/asset-hubs/asset-hub-polkadot/src/staking/mod.rs +++ b/system-parachains/asset-hubs/asset-hub-polkadot/src/staking/mod.rs @@ -17,3 +17,16 @@ //! Staking related config of the Asset Hub. pub mod nom_pools; + +use crate::*; + +impl pallet_fast_unstake::Config for Runtime { + type RuntimeEvent = RuntimeEvent; + type Currency = Balances; + type BatchSize = frame_support::traits::ConstU32<16>; + type Deposit = frame_support::traits::ConstU128<{ UNITS }>; + type ControlOrigin = EnsureRoot; + type Staking = nom_pools::StakingMock; + type MaxErasToCheckPerBlock = ConstU32<1>; + type WeightInfo = (); // TODO weights::pallet_fast_unstake::WeightInfo; +} From 167dec5bccd61926f33ce6a6c2c8fc0e7006ea62 Mon Sep 17 00:00:00 2001 From: Oliver Tale-Yazdi Date: Wed, 29 Jan 2025 20:33:42 +0100 Subject: [PATCH 02/10] wip Signed-off-by: Oliver Tale-Yazdi --- Cargo.lock | 2 +- pallets/ah-migrator/src/lib.rs | 2 +- pallets/ah-migrator/src/staking/fast_unstake.rs | 2 +- pallets/rc-migrator/src/lib.rs | 1 - pallets/rc-migrator/src/staking/fast_unstake.rs | 2 +- 5 files changed, 4 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index babeaaac2e..2b6cbd1cc5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -792,8 +792,8 @@ dependencies = [ "pallet-bounties", "pallet-child-bounties", "pallet-collator-selection", - "pallet-fast-unstake", "pallet-conviction-voting", + "pallet-fast-unstake", "pallet-message-queue", "pallet-multisig", "pallet-nfts", diff --git a/pallets/ah-migrator/src/lib.rs b/pallets/ah-migrator/src/lib.rs index 8b0633257e..d315a10841 100644 --- a/pallets/ah-migrator/src/lib.rs +++ b/pallets/ah-migrator/src/lib.rs @@ -58,7 +58,7 @@ use pallet_rc_migrator::{ preimage::*, proxy::*, staking::{ - fast_unstake::{FastUnstakeMigrator, FastUnstakeStorageValues, RcFastUnstakeMessage}, + fast_unstake::{FastUnstakeMigrator, RcFastUnstakeMessage}, nom_pools::*, }, }; diff --git a/pallets/ah-migrator/src/staking/fast_unstake.rs b/pallets/ah-migrator/src/staking/fast_unstake.rs index ad2f21ae78..bc53191ebd 100644 --- a/pallets/ah-migrator/src/staking/fast_unstake.rs +++ b/pallets/ah-migrator/src/staking/fast_unstake.rs @@ -17,7 +17,7 @@ //! Fast unstake migration logic. -use crate::{types::*, *}; +use crate::*; impl Pallet { pub fn do_receive_fast_unstake_messages( diff --git a/pallets/rc-migrator/src/lib.rs b/pallets/rc-migrator/src/lib.rs index f9d19380fd..56414f1d24 100644 --- a/pallets/rc-migrator/src/lib.rs +++ b/pallets/rc-migrator/src/lib.rs @@ -76,7 +76,6 @@ use staking::{ nom_pools::{NomPoolsMigrator, NomPoolsStage}, }; use referenda::ReferendaStage; -use staking::nom_pools::{NomPoolsMigrator, NomPoolsStage}; use types::PalletMigration; /// The log target of this pallet. diff --git a/pallets/rc-migrator/src/staking/fast_unstake.rs b/pallets/rc-migrator/src/staking/fast_unstake.rs index de9a718a8b..801f524e70 100644 --- a/pallets/rc-migrator/src/staking/fast_unstake.rs +++ b/pallets/rc-migrator/src/staking/fast_unstake.rs @@ -84,7 +84,7 @@ impl PalletMigration for FastUnstakeMigrator { type Error = Error; fn migrate_many( - mut current_key: Option, + current_key: Option, weight_counter: &mut WeightMeter, ) -> Result, Self::Error> { let mut inner_key = current_key.unwrap_or(FastUnstakeStage::StorageValues); From ad1952bcacd366094ff9f3e4a49d74509dd91406 Mon Sep 17 00:00:00 2001 From: Oliver Tale-Yazdi Date: Wed, 29 Jan 2025 20:57:27 +0100 Subject: [PATCH 03/10] dafak is this? Signed-off-by: Oliver Tale-Yazdi --- pallets/rc-migrator/src/staking/bags_list.rs | 160 +++++++++++++++++++ 1 file changed, 160 insertions(+) create mode 100644 pallets/rc-migrator/src/staking/bags_list.rs diff --git a/pallets/rc-migrator/src/staking/bags_list.rs b/pallets/rc-migrator/src/staking/bags_list.rs new file mode 100644 index 0000000000..1492297877 --- /dev/null +++ b/pallets/rc-migrator/src/staking/bags_list.rs @@ -0,0 +1,160 @@ +// Copyright (C) Parity Technologies (UK) Ltd. +// This file is part of Polkadot. + +// Polkadot is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Polkadot is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Polkadot. If not, see . + +//! Nomination pools data migrator module. + +use crate::{types::*, *}; + +#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)] +pub enum BagsListStage { + ListNodes(Option), + ListBags(Option), + Finished, +} + +pub type BagsListStageOf = BagsListStage<::AccountId, ::Score>; + +#[derive( + Encode, + Decode, + MaxEncodedLen, + TypeInfo, + RuntimeDebugNoBound, + CloneNoBound, + PartialEqNoBound, + EqNoBound, +)] +#[codec(mel_bound(T: Config))] +#[scale_info(skip_type_params(T))] +pub enum RcBagsListMessage { + Node { node: alias::NodeOf }, + Bag { bag: alias::BagOf }, +} + +pub struct BagsListMigrator { + _phantom: PhantomData, +} + +impl PalletMigration for BagsListMigrator { + type Key = BagsListStageOf; + type Error = Error; + + fn migrate_many( + current_key: Option, + weight_counter: &mut WeightMeter, + ) -> Result, Self::Error> { + let mut inner_key = current_key.unwrap_or(BagsListStage::ListNodes(None)); + let mut messages = Vec::new(); + + loop { + if weight_counter + .try_consume(::DbWeight::get().reads_writes(1, 1)) + .is_err() + { + if messages.is_empty() { + return Err(Error::OutOfWeight); + } else { + break; + } + } + if messages.len() > 10_000 { + log::warn!("Weight allowed very big batch, stopping"); + break; + } + + inner_key = match inner_key { + BagsListStage::ListNodes(next) => { + let mut iter = match next { + Some(next) => alias::ListNodes::::iter_from(alias::ListNodes::::hashed_key_for(next)), + None => alias::ListNodes::::iter(), + }; + + match iter.next() { + Some((key, node)) => { + alias::ListNodes::::remove(&key); + messages.push(RcBagsListMessage::Node { node }); + BagsListStage::ListNodes(Some(key)) + }, + None => BagsListStage::ListBags(None), + } + }, + BagsListStage::ListBags(next) => { + let mut iter = match next { + Some(next) => alias::ListBags::::iter_from(alias::ListBags::::hashed_key_for(next)), + None => alias::ListBags::::iter(), + }; + + match iter.next() { + Some((key, bag)) => { + alias::ListBags::::remove(&key); + messages.push(RcBagsListMessage::Bag { bag }); + BagsListStage::ListBags(Some(key)) + }, + None => BagsListStage::Finished, + } + }, + BagsListStage::Finished => { + break; + }, + } + } + + if !messages.is_empty() { + Pallet::::send_chunked_xcm(messages, |messages| { + types::AhMigratorCall::::ReceiveBagsListMessages { messages } + })?; + } + + if inner_key == BagsListStage::Finished { + Ok(None) + } else { + Ok(Some(inner_key)) + } + } +} + +pub mod alias { + use super::*; + + // From https://github.com/paritytech/polkadot-sdk/blob/7ecf3f757a5d6f622309cea7f788e8a547a5dce8/substrate/frame/bags-list/src/list/mod.rs#L818-L830 minus all the stuff that we don't need + #[derive(Encode, Decode, MaxEncodedLen, TypeInfo, Clone, PartialEq, Eq, RuntimeDebug)] + pub struct Node { + pub id: AccountId, + pub prev: Option, + pub next: Option, + pub bag_upper: Score, + pub score: Score, + } + pub type NodeOf = Node<::AccountId, ::Score>; + + // From https://github.com/paritytech/polkadot-sdk/blob/7ecf3f757a5d6f622309cea7f788e8a547a5dce8/substrate/frame/bags-list/src/list/mod.rs#L622-L630 + #[derive(Encode, Decode, MaxEncodedLen, TypeInfo, Clone, PartialEq, Eq, RuntimeDebug)] + pub struct Bag { + pub head: Option, + pub tail: Option, + } + pub type BagOf = Bag<::AccountId>; + + // From https://github.com/paritytech/polkadot-sdk/blob/6c3219ebe9231a0305f53c7b33cb558d46058062/substrate/frame/bags-list/src/lib.rs#L255-L257 + #[frame_support::storage_alias(pallet_name)] + pub type ListNodes = + CountedStorageMap, Twox64Concat, ::AccountId, NodeOf>; + + // From https://github.com/paritytech/polkadot-sdk/blob/6c3219ebe9231a0305f53c7b33cb558d46058062/substrate/frame/bags-list/src/lib.rs#L262-L264 + #[frame_support::storage_alias(pallet_name)] + pub type ListBags = + StorageMap, Twox64Concat, ::Score, BagOf>; +} From 5787427c977aa963264c6c4ca0e9a5acb3f30980 Mon Sep 17 00:00:00 2001 From: Oliver Tale-Yazdi Date: Wed, 29 Jan 2025 22:47:30 +0100 Subject: [PATCH 04/10] Bags list Signed-off-by: Oliver Tale-Yazdi --- Cargo.lock | 5 + pallets/ah-migrator/Cargo.toml | 1 + pallets/ah-migrator/src/lib.rs | 24 +- pallets/ah-migrator/src/staking/bags_list.rs | 72 ++++++ pallets/ah-migrator/src/staking/mod.rs | 1 + pallets/rc-migrator/Cargo.toml | 1 + pallets/rc-migrator/src/lib.rs | 64 ++++- pallets/rc-migrator/src/staking/bags_list.rs | 59 +++-- pallets/rc-migrator/src/staking/mod.rs | 1 + pallets/rc-migrator/src/types.rs | 2 + .../asset-hubs/asset-hub-polkadot/Cargo.toml | 3 + .../asset-hubs/asset-hub-polkadot/src/lib.rs | 2 + .../src/staking/bags_thresholds.rs | 234 ++++++++++++++++++ .../asset-hub-polkadot/src/staking/mod.rs | 16 ++ .../src/staking/nom_pools.rs | 14 ++ 15 files changed, 466 insertions(+), 33 deletions(-) create mode 100644 pallets/ah-migrator/src/staking/bags_list.rs create mode 100644 system-parachains/asset-hubs/asset-hub-polkadot/src/staking/bags_thresholds.rs diff --git a/Cargo.lock b/Cargo.lock index 2b6cbd1cc5..046e8119e0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -772,6 +772,7 @@ dependencies = [ "cumulus-primitives-core", "cumulus-primitives-utility", "frame-benchmarking", + "frame-election-provider-support", "frame-executive", "frame-metadata-hash-extension", "frame-support", @@ -788,6 +789,7 @@ dependencies = [ "pallet-assets", "pallet-aura", "pallet-authorship", + "pallet-bags-list", "pallet-balances", "pallet-bounties", "pallet-child-bounties", @@ -834,6 +836,7 @@ dependencies = [ "sp-genesis-builder", "sp-inherents", "sp-io 38.0.0", + "sp-npos-elections", "sp-offchain", "sp-runtime 39.0.5", "sp-session", @@ -7647,6 +7650,7 @@ dependencies = [ "hex", "hex-literal", "log", + "pallet-bags-list", "pallet-balances", "pallet-fast-unstake", "pallet-multisig", @@ -8879,6 +8883,7 @@ dependencies = [ "frame-system", "hex-literal", "log", + "pallet-bags-list", "pallet-balances", "pallet-fast-unstake", "pallet-multisig", diff --git a/pallets/ah-migrator/Cargo.toml b/pallets/ah-migrator/Cargo.toml index efd26fb282..4c9fb8f799 100644 --- a/pallets/ah-migrator/Cargo.toml +++ b/pallets/ah-migrator/Cargo.toml @@ -14,6 +14,7 @@ frame-support = { workspace = true } frame-system = { workspace = true } log = { workspace = true } pallet-balances = { workspace = true } +pallet-bags-list = { workspace = true } pallet-fast-unstake = { workspace = true } pallet-multisig = { workspace = true } pallet-nomination-pools = { workspace = true } diff --git a/pallets/ah-migrator/src/lib.rs b/pallets/ah-migrator/src/lib.rs index d315a10841..0f22af89fc 100644 --- a/pallets/ah-migrator/src/lib.rs +++ b/pallets/ah-migrator/src/lib.rs @@ -58,6 +58,7 @@ use pallet_rc_migrator::{ preimage::*, proxy::*, staking::{ + bags_list::RcBagsListMessage, fast_unstake::{FastUnstakeMigrator, RcFastUnstakeMessage}, nom_pools::*, }, @@ -85,6 +86,7 @@ type RcAccountFor = RcAccount< #[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)] pub enum PalletEventName { FastUnstake, + BagsList, } #[frame_support::pallet(dev_mode)] @@ -103,6 +105,7 @@ pub mod pallet { + pallet_referenda::Config + pallet_nomination_pools::Config + pallet_fast_unstake::Config + + pallet_bags_list::Config { /// The overarching event type. type RuntimeEvent: From> + IsType<::RuntimeEvent>; @@ -270,9 +273,16 @@ pub mod pallet { count_bad: u32, }, /// We received a batch of messages that will be integrated into a pallet. - BatchReceived { pallet: PalletEventName, count: u32 }, + BatchReceived { + pallet: PalletEventName, + count: u32, + }, /// We processed a batch of messages for this pallet. - BatchProcessed { pallet: PalletEventName, count_good: u32, count_bad: u32 }, + BatchProcessed { + pallet: PalletEventName, + count_good: u32, + count_bad: u32, + }, /// We received a batch of referendums that we are going to integrate. ReferendumsBatchReceived { /// How many referendums are in the batch. @@ -424,6 +434,16 @@ pub mod pallet { Self::do_receive_referendums(referendums).map_err(Into::into) } + + #[pallet::call_index(11)] + pub fn receive_bags_list_messages( + origin: OriginFor, + messages: Vec>, + ) -> DispatchResult { + ensure_root(origin)?; + + Self::do_receive_bags_list_messages(messages).map_err(Into::into) + } } #[pallet::hooks] diff --git a/pallets/ah-migrator/src/staking/bags_list.rs b/pallets/ah-migrator/src/staking/bags_list.rs new file mode 100644 index 0000000000..0c06dc87e5 --- /dev/null +++ b/pallets/ah-migrator/src/staking/bags_list.rs @@ -0,0 +1,72 @@ +// This file is part of Substrate. + +// Copyright (C) Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: Apache-2.0 + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! Fast unstake migration logic. + +use crate::*; +use pallet_rc_migrator::staking::bags_list::alias; + +impl Pallet { + pub fn do_receive_bags_list_messages( + messages: Vec>, + ) -> Result<(), Error> { + let (mut good, mut bad) = (0, 0); + log::info!("Integrating {} BagsListMessages", messages.len()); + Self::deposit_event(Event::BatchReceived { + pallet: PalletEventName::BagsList, + count: messages.len() as u32, + }); + + for message in messages { + match Self::do_receive_bags_list_message(message) { + Ok(_) => good += 1, + Err(_) => bad += 1, + } + } + + Self::deposit_event(Event::BatchProcessed { + pallet: PalletEventName::BagsList, + count_good: good as u32, + count_bad: bad as u32, + }); + + Ok(()) + } + + pub fn do_receive_bags_list_message(message: RcBagsListMessage) -> Result<(), Error> { + match message { + RcBagsListMessage::Node { id, node } => { + if alias::ListNodes::::contains_key(&id) { + return Err(Error::::InsertConflict); + } + + alias::ListNodes::::insert(&id, &node); + log::debug!("Integrating BagsListNode: {:?}", &id); + }, + RcBagsListMessage::Bag { score, bag } => { + if alias::ListBags::::contains_key(&score) { + return Err(Error::::InsertConflict); + } + + alias::ListBags::::insert(&score, &bag); + log::debug!("Integrating BagsListBag: {:?}", &score); + }, + } + + Ok(()) + } +} diff --git a/pallets/ah-migrator/src/staking/mod.rs b/pallets/ah-migrator/src/staking/mod.rs index 7fc26e3012..fe11d85fc0 100644 --- a/pallets/ah-migrator/src/staking/mod.rs +++ b/pallets/ah-migrator/src/staking/mod.rs @@ -17,5 +17,6 @@ //! Staking migration logic. +pub mod bags_list; pub mod fast_unstake; pub mod nom_pools; diff --git a/pallets/rc-migrator/Cargo.toml b/pallets/rc-migrator/Cargo.toml index 3a0b665503..0c4a49a9c1 100644 --- a/pallets/rc-migrator/Cargo.toml +++ b/pallets/rc-migrator/Cargo.toml @@ -20,6 +20,7 @@ sp-runtime = { workspace = true } sp-std = { workspace = true } sp-io = { workspace = true } pallet-balances = { workspace = true } +pallet-bags-list = { workspace = true } pallet-staking = { workspace = true } pallet-proxy = { workspace = true } pallet-multisig = { workspace = true } diff --git a/pallets/rc-migrator/src/lib.rs b/pallets/rc-migrator/src/lib.rs index 56414f1d24..8996206110 100644 --- a/pallets/rc-migrator/src/lib.rs +++ b/pallets/rc-migrator/src/lib.rs @@ -71,11 +71,12 @@ use preimage::{ PreimageChunkMigrator, PreimageLegacyRequestStatusMigrator, PreimageRequestStatusMigrator, }; use proxy::*; +use referenda::ReferendaStage; use staking::{ + bags_list::{BagsListMigrator, BagsListStage}, fast_unstake::{FastUnstakeMigrator, FastUnstakeStage}, nom_pools::{NomPoolsMigrator, NomPoolsStage}, }; -use referenda::ReferendaStage; use types::PalletMigration; /// The log target of this pallet. @@ -97,10 +98,13 @@ impl From for Error { } } -pub type MigrationStageOf = MigrationStage<::AccountId>; +pub type MigrationStageOf = MigrationStage< + ::AccountId, + >::Score, +>; #[derive(Encode, Decode, Clone, Default, RuntimeDebug, TypeInfo, MaxEncodedLen, PartialEq, Eq)] -pub enum MigrationStage { +pub enum MigrationStage { /// The migration has not yet started but will start in the next block. #[default] Pending, @@ -164,12 +168,20 @@ pub enum MigrationStage { last_key: Option, }, ReferendaMigrationDone, + BagsListMigrationInit, + BagsListMigrationOngoing { + next_key: Option>, + }, + BagsListMigrationDone, MigrationDone, } -pub type MigrationStageFor = MigrationStage<::AccountId>; +pub type MigrationStageFor = MigrationStage< + ::AccountId, + >::Score, +>; -impl MigrationStage { +impl MigrationStage { /// Whether the migration is finished. /// /// This is **not** the same as `!self.is_ongoing()`. @@ -209,6 +221,7 @@ pub mod pallet { + pallet_referenda::Config + pallet_nomination_pools::Config + pallet_fast_unstake::Config + + pallet_bags_list::Config { /// The overarching event type. type RuntimeEvent: From> + IsType<::RuntimeEvent>; @@ -260,16 +273,15 @@ pub mod pallet { /// A stage transition has occurred. StageTransition { /// The old stage before the transition. - old: MigrationStage, + old: MigrationStageFor, /// The new stage after the transition. - new: MigrationStage, + new: MigrationStageFor, }, } /// The Relay Chain migration state. #[pallet::storage] - pub type RcMigrationStage = - StorageValue<_, MigrationStage, ValueQuery>; + pub type RcMigrationStage = StorageValue<_, MigrationStageFor, ValueQuery>; /// Helper storage item to obtain and store the known accounts that should be kept partially on /// fully on Relay Chain. @@ -308,7 +320,7 @@ pub mod pallet { Self::transition(MigrationStage::AccountsMigrationInit); // toggle for testing - Self::transition(MigrationStage::ProxyMigrationInit); + //Self::transition(MigrationStage::BagsListMigrationInit); }, MigrationStage::AccountsMigrationInit => { // TODO: weights @@ -653,6 +665,36 @@ pub mod pallet { } }, MigrationStage::ReferendaMigrationDone => { + Self::transition(MigrationStage::BagsListMigrationInit); + }, + MigrationStage::BagsListMigrationInit => { + Self::transition(MigrationStage::BagsListMigrationOngoing { next_key: None }); + }, + MigrationStage::BagsListMigrationOngoing { next_key } => { + let res = with_transaction_opaque_err::, Error, _>(|| { + match BagsListMigrator::::migrate_many(next_key, &mut weight_counter) { + Ok(last_key) => TransactionOutcome::Commit(Ok(last_key)), + Err(e) => TransactionOutcome::Rollback(Err(e)), + } + }) + .expect("Always returning Ok; qed"); + + match res { + Ok(None) => { + Self::transition(MigrationStage::BagsListMigrationDone); + }, + Ok(Some(next_key)) => { + Self::transition(MigrationStage::BagsListMigrationOngoing { + next_key: Some(next_key), + }); + }, + e => { + log::error!(target: LOG_TARGET, "Error while migrating bags list: {:?}", e); + defensive!("Error while migrating bags list"); + }, + } + }, + MigrationStage::BagsListMigrationDone => { Self::transition(MigrationStage::MigrationDone); }, MigrationStage::MigrationDone => (), @@ -664,7 +706,7 @@ pub mod pallet { impl Pallet { /// Execute a stage transition and log it. - fn transition(new: MigrationStage) { + fn transition(new: MigrationStageFor) { let old = RcMigrationStage::::get(); RcMigrationStage::::put(&new); log::info!(target: LOG_TARGET, "[Block {:?}] Stage transition: {:?} -> {:?}", frame_system::Pallet::::block_number(), &old, &new); diff --git a/pallets/rc-migrator/src/staking/bags_list.rs b/pallets/rc-migrator/src/staking/bags_list.rs index 1492297877..9ab41121b8 100644 --- a/pallets/rc-migrator/src/staking/bags_list.rs +++ b/pallets/rc-migrator/src/staking/bags_list.rs @@ -25,7 +25,10 @@ pub enum BagsListStage { Finished, } -pub type BagsListStageOf = BagsListStage<::AccountId, ::Score>; +pub type BagsListStageOf = BagsListStage< + ::AccountId, + >::Score, +>; #[derive( Encode, @@ -39,9 +42,9 @@ pub type BagsListStageOf = BagsListStage<::Account )] #[codec(mel_bound(T: Config))] #[scale_info(skip_type_params(T))] -pub enum RcBagsListMessage { - Node { node: alias::NodeOf }, - Bag { bag: alias::BagOf }, +pub enum RcBagsListMessage> { + Node { id: T::AccountId, node: alias::NodeOf }, + Bag { score: T::Score, bag: alias::BagOf }, } pub struct BagsListMigrator { @@ -78,30 +81,34 @@ impl PalletMigration for BagsListMigrator { inner_key = match inner_key { BagsListStage::ListNodes(next) => { let mut iter = match next { - Some(next) => alias::ListNodes::::iter_from(alias::ListNodes::::hashed_key_for(next)), + Some(next) => alias::ListNodes::::iter_from( + alias::ListNodes::::hashed_key_for(next), + ), None => alias::ListNodes::::iter(), }; - + match iter.next() { - Some((key, node)) => { - alias::ListNodes::::remove(&key); - messages.push(RcBagsListMessage::Node { node }); - BagsListStage::ListNodes(Some(key)) + Some((id, node)) => { + alias::ListNodes::::remove(&id); + messages.push(RcBagsListMessage::Node { id: id.clone(), node }); + BagsListStage::ListNodes(Some(id)) }, None => BagsListStage::ListBags(None), } }, BagsListStage::ListBags(next) => { let mut iter = match next { - Some(next) => alias::ListBags::::iter_from(alias::ListBags::::hashed_key_for(next)), + Some(next) => alias::ListBags::::iter_from( + alias::ListBags::::hashed_key_for(next), + ), None => alias::ListBags::::iter(), }; match iter.next() { - Some((key, bag)) => { - alias::ListBags::::remove(&key); - messages.push(RcBagsListMessage::Bag { bag }); - BagsListStage::ListBags(Some(key)) + Some((score, bag)) => { + alias::ListBags::::remove(&score); + messages.push(RcBagsListMessage::Bag { score: score.clone(), bag }); + BagsListStage::ListBags(Some(score)) }, None => BagsListStage::Finished, } @@ -138,7 +145,10 @@ pub mod alias { pub bag_upper: Score, pub score: Score, } - pub type NodeOf = Node<::AccountId, ::Score>; + pub type NodeOf = Node< + ::AccountId, + >::Score, + >; // From https://github.com/paritytech/polkadot-sdk/blob/7ecf3f757a5d6f622309cea7f788e8a547a5dce8/substrate/frame/bags-list/src/list/mod.rs#L622-L630 #[derive(Encode, Decode, MaxEncodedLen, TypeInfo, Clone, PartialEq, Eq, RuntimeDebug)] @@ -150,11 +160,20 @@ pub mod alias { // From https://github.com/paritytech/polkadot-sdk/blob/6c3219ebe9231a0305f53c7b33cb558d46058062/substrate/frame/bags-list/src/lib.rs#L255-L257 #[frame_support::storage_alias(pallet_name)] - pub type ListNodes = - CountedStorageMap, Twox64Concat, ::AccountId, NodeOf>; + pub type ListNodes> = + CountedStorageMap< + pallet_bags_list::Pallet, + Twox64Concat, + ::AccountId, + NodeOf, + >; // From https://github.com/paritytech/polkadot-sdk/blob/6c3219ebe9231a0305f53c7b33cb558d46058062/substrate/frame/bags-list/src/lib.rs#L262-L264 #[frame_support::storage_alias(pallet_name)] - pub type ListBags = - StorageMap, Twox64Concat, ::Score, BagOf>; + pub type ListBags> = StorageMap< + pallet_bags_list::Pallet, + Twox64Concat, + >::Score, + BagOf, + >; } diff --git a/pallets/rc-migrator/src/staking/mod.rs b/pallets/rc-migrator/src/staking/mod.rs index 4fe042f9da..260b80acd0 100644 --- a/pallets/rc-migrator/src/staking/mod.rs +++ b/pallets/rc-migrator/src/staking/mod.rs @@ -14,6 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Polkadot. If not, see . +pub mod bags_list; pub mod fast_unstake; pub mod nom_pools; pub mod nom_pools_alias; diff --git a/pallets/rc-migrator/src/types.rs b/pallets/rc-migrator/src/types.rs index 595c69ea8e..7a82e78462 100644 --- a/pallets/rc-migrator/src/types.rs +++ b/pallets/rc-migrator/src/types.rs @@ -57,6 +57,8 @@ pub enum AhMigratorCall { }, #[codec(index = 10)] ReceiveReferendums { referendums: Vec<(u32, ReferendumInfoOf)> }, + #[codec(index = 11)] + ReceiveBagsListMessages { messages: Vec> }, } /// Copy of `ParaInfo` type from `paras_registrar` pallet. diff --git a/system-parachains/asset-hubs/asset-hub-polkadot/Cargo.toml b/system-parachains/asset-hubs/asset-hub-polkadot/Cargo.toml index 666fd8e5db..c66d1c4c6d 100644 --- a/system-parachains/asset-hubs/asset-hub-polkadot/Cargo.toml +++ b/system-parachains/asset-hubs/asset-hub-polkadot/Cargo.toml @@ -29,6 +29,7 @@ system-parachains-constants = { workspace = true } # Substrate frame-benchmarking = { optional = true, workspace = true } +frame-election-provider-support = { workspace = true } frame-executive = { workspace = true } frame-metadata-hash-extension = { workspace = true } frame-support = { workspace = true } @@ -45,6 +46,7 @@ pallet-aura = { workspace = true } pallet-authorship = { workspace = true } pallet-balances = { workspace = true } pallet-fast-unstake = { workspace = true } +pallet-bags-list = { workspace = true } pallet-bounties = { workspace = true } pallet-child-bounties = { workspace = true } pallet-conviction-voting = { workspace = true } @@ -73,6 +75,7 @@ sp-core = { workspace = true } sp-genesis-builder = { workspace = true } sp-io = { workspace = true } sp-inherents = { workspace = true } +sp-npos-elections = { workspace = true } sp-offchain = { workspace = true } sp-runtime = { workspace = true } sp-session = { workspace = true } diff --git a/system-parachains/asset-hubs/asset-hub-polkadot/src/lib.rs b/system-parachains/asset-hubs/asset-hub-polkadot/src/lib.rs index 8267d98c48..1e4d3cf511 100644 --- a/system-parachains/asset-hubs/asset-hub-polkadot/src/lib.rs +++ b/system-parachains/asset-hubs/asset-hub-polkadot/src/lib.rs @@ -1161,6 +1161,8 @@ construct_runtime!( // Staking in the 70s NominationPools: pallet_nomination_pools = 70, FastUnstake: pallet_fast_unstake = 71, + VoterList: pallet_bags_list:: = 72, + // Asset Hub Migrator AhMigrator: pallet_ah_migrator = 255, diff --git a/system-parachains/asset-hubs/asset-hub-polkadot/src/staking/bags_thresholds.rs b/system-parachains/asset-hubs/asset-hub-polkadot/src/staking/bags_thresholds.rs new file mode 100644 index 0000000000..56c764f7a6 --- /dev/null +++ b/system-parachains/asset-hubs/asset-hub-polkadot/src/staking/bags_thresholds.rs @@ -0,0 +1,234 @@ +// Copyright (C) Parity Technologies (UK) Ltd. +// This file is part of Polkadot. + +// Polkadot is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Polkadot is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Polkadot. If not, see . + +//! Autogenerated bag thresholds. +//! +//! Generated on 2021-10-14T08:36:33.156699497+00:00 +//! for the polkadot runtime. + +/// Existential weight for this runtime. +#[cfg(any(test, feature = "std"))] +#[allow(unused)] +pub const EXISTENTIAL_WEIGHT: u64 = 10_000_000_000; + +/// Constant ratio between bags for this runtime. +#[cfg(any(test, feature = "std"))] +#[allow(unused)] +pub const CONSTANT_RATIO: f64 = 1.1131723507077667; + +/// Upper thresholds delimiting the bag list. +pub const THRESHOLDS: [u64; 200] = [ + 10_000_000_000, + 11_131_723_507, + 12_391_526_824, + 13_793_905_044, + 15_354_993_703, + 17_092_754_435, + 19_027_181_634, + 21_180_532_507, + 23_577_583_160, + 26_245_913_670, + 29_216_225_417, + 32_522_694_326, + 36_203_364_094, + 40_300_583_912, + 44_861_495_728, + 49_938_576_656, + 55_590_242_767, + 61_881_521_217, + 68_884_798_439, + 76_680_653_006, + 85_358_782_760, + 95_019_036_859, + 105_772_564_622, + 117_743_094_401, + 131_068_357_174, + 145_901_671_259, + 162_413_706_368, + 180_794_447_305, + 201_255_379_901, + 224_031_924_337, + 249_386_143_848, + 277_609_759_981, + 309_027_509_097, + 344_000_878_735, + 382_932_266_827, + 426_269_611_626, + 474_511_545_609, + 528_213_132_664, + 587_992_254_562, + 654_536_720_209, + 728_612_179_460, + 811_070_932_564, + 902_861_736_593, + 1_005_040_721_687, + 1_118_783_542_717, + 1_245_398_906_179, + 1_386_343_627_960, + 1_543_239_395_225, + 1_717_891_425_287, + 1_912_309_236_147, + 2_128_729_767_682, + 2_369_643_119_512, + 2_637_821_201_686, + 2_936_349_627_828, + 3_268_663_217_709, + 3_638_585_517_729, + 4_050_372_794_022, + 4_508_763_004_364, + 5_019_030_312_352, + 5_587_045_771_074, + 6_219_344_874_498, + 6_923_202_753_807, + 7_706_717_883_882, + 8_578_905_263_043, + 9_549_800_138_161, + 10_630_573_468_586, + 11_833_660_457_397, + 13_172_903_628_838, + 14_663_712_098_160, + 16_323_238_866_411, + 18_170_578_180_087, + 20_226_985_226_447, + 22_516_120_692_255, + 25_064_322_999_817, + 27_900_911_352_605, + 31_058_523_077_268, + 34_573_489_143_434, + 38_486_252_181_966, + 42_841_831_811_331, + 47_690_342_626_046, + 53_087_570_807_094, + 59_095_615_988_698, + 65_783_605_766_662, + 73_228_491_069_308, + 81_515_931_542_404, + 90_741_281_135_191, + 101_010_685_227_495, + 112_442_301_921_293, + 125_167_661_548_718, + 139_333_180_038_781, + 155_101_843_555_358, + 172_655_083_789_626, + 192_194_865_483_744, + 213_946_010_204_502, + 238_158_783_103_893, + 265_111_772_429_462, + 295_115_094_915_607, + 328_513_963_936_552, + 365_692_661_475_578, + 407_078_959_611_349, + 453_149_042_394_237, + 504_432_984_742_966, + 561_520_851_400_862, + 625_069_486_125_324, + 695_810_069_225_823, + 774_556_530_406_243, + 862_214_913_708_369, + 959_793_802_308_039, + 1_068_415_923_109_985, + 1_189_331_064_661_951, + 1_323_930_457_019_515, + 1_473_762_779_014_021, + 1_640_551_977_100_649, + 1_826_217_100_807_404, + 2_032_894_383_008_501, + 2_262_961_819_074_188, + 2_519_066_527_700_738, + 2_804_155_208_229_882, + 3_121_508_044_894_685, + 3_474_776_448_088_622, + 3_868_025_066_902_796, + 4_305_778_556_320_752, + 4_793_073_637_166_665, + 5_335_517_047_800_242, + 5_939_350_054_341_159, + 6_611_520_261_667_250, + 7_359_761_551_432_161, + 8_192_683_066_856_378, + 9_119_868_268_136_230, + 10_151_985_198_186_376, + 11_300_909_227_415_580, + 12_579_859_689_817_292, + 14_003_551_982_487_792, + 15_588_366_878_604_342, + 17_352_539_001_951_086, + 19_316_366_631_550_092, + 21_502_445_250_375_680, + 23_935_927_525_325_748, + 26_644_812_709_737_600, + 29_660_268_798_266_784, + 33_016_991_140_790_860, + 36_753_601_641_491_664, + 40_913_093_136_236_104, + 45_543_324_061_189_736, + 50_697_569_104_240_168, + 56_435_132_174_936_472, + 62_822_028_745_677_552, + 69_931_745_415_056_768, + 77_846_085_432_775_824, + 86_656_109_914_600_688, + 96_463_185_576_826_656, + 107_380_151_045_315_664, + 119_532_615_158_469_088, + 133_060_402_202_199_856, + 148_119_160_705_543_712, + 164_882_154_307_451_552, + 183_542_255_300_186_560, + 204_314_163_786_713_728, + 227_436_877_985_347_776, + 253_176_444_104_585_088, + 281_829_017_427_734_464, + 313_724_269_827_691_328, + 349_229_182_918_168_832, + 388_752_270_484_770_624, + 432_748_278_778_513_664, + 481_723_418_752_617_984, + 536_241_190_443_833_600, + 596_928_866_512_693_376, + 664_484_709_541_257_600, + 739_686_006_129_409_280, + 823_398_010_228_713_984, + 916_583_898_614_395_264, + 1_020_315_853_041_475_584, + 1_135_787_396_594_579_584, + 1_264_327_126_171_442_688, + 1_407_413_999_103_859_968, + 1_566_694_349_801_462_272, + 1_744_000_832_209_069_824, + 1_941_373_506_026_471_680, + 2_161_083_309_305_266_176, + 2_405_658_187_494_662_656, + 2_677_912_179_572_818_944, + 2_980_977_795_924_034_048, + 3_318_342_060_496_414_208, + 3_693_886_631_935_247_360, + 4_111_932_465_319_354_368, + 4_577_289_528_371_127_808, + 5_095_312_144_166_932_480, + 5_671_960_597_112_134_656, + 6_313_869_711_009_142_784, + 7_028_425_188_266_614_784, + 7_823_848_588_596_424_704, + 8_709_291_924_949_524_480, + 9_694_942_965_096_232_960, + 10_792_142_450_433_898_496, + 12_013_514_580_722_579_456, + 13_373_112_266_084_982_784, + 14_886_578_817_516_689_408, + 16_571_327_936_291_497_984, + 18_446_744_073_709_551_615, +]; diff --git a/system-parachains/asset-hubs/asset-hub-polkadot/src/staking/mod.rs b/system-parachains/asset-hubs/asset-hub-polkadot/src/staking/mod.rs index 2629b48514..ba76aecc2e 100644 --- a/system-parachains/asset-hubs/asset-hub-polkadot/src/staking/mod.rs +++ b/system-parachains/asset-hubs/asset-hub-polkadot/src/staking/mod.rs @@ -15,7 +15,10 @@ // along with Polkadot. If not, see . //! Staking related config of the Asset Hub. +//! +//! The large pallets have their config in a sub-module, the smaller ones are defined here. +pub mod bags_thresholds; pub mod nom_pools; use crate::*; @@ -30,3 +33,16 @@ impl pallet_fast_unstake::Config for Runtime { type MaxErasToCheckPerBlock = ConstU32<1>; type WeightInfo = (); // TODO weights::pallet_fast_unstake::WeightInfo; } + +parameter_types! { + pub const BagThresholds: &'static [u64] = &bags_thresholds::THRESHOLDS; +} + +type VoterBagsListInstance = pallet_bags_list::Instance1; +impl pallet_bags_list::Config for Runtime { + type RuntimeEvent = RuntimeEvent; + type ScoreProvider = nom_pools::StakingMock; + type WeightInfo = (); // TODO weights::pallet_bags_list::WeightInfo; + type BagThresholds = BagThresholds; + type Score = sp_npos_elections::VoteWeight; +} diff --git a/system-parachains/asset-hubs/asset-hub-polkadot/src/staking/nom_pools.rs b/system-parachains/asset-hubs/asset-hub-polkadot/src/staking/nom_pools.rs index 1d42e204b1..754134b542 100644 --- a/system-parachains/asset-hubs/asset-hub-polkadot/src/staking/nom_pools.rs +++ b/system-parachains/asset-hubs/asset-hub-polkadot/src/staking/nom_pools.rs @@ -243,3 +243,17 @@ impl sp_staking::StakingInterface for StakingMock { unimplemented!() } } + +impl frame_election_provider_support::ScoreProvider for StakingMock { + type Score = sp_npos_elections::VoteWeight; + + fn score(_id: &AccountId) -> Self::Score { + unimplemented!() + } + + /* TODO frame_election_provider_support::runtime_benchmarks_or_std_enabled! { + fn set_score_of(id: &AccountId, weight: Self::Score) { + unimplemented!() + } + }*/ +} From 5388bf5fd9c26478a09c2926fab892be6f9efdbb Mon Sep 17 00:00:00 2001 From: Oliver Tale-Yazdi Date: Wed, 29 Jan 2025 22:49:49 +0100 Subject: [PATCH 05/10] fmt Signed-off-by: Oliver Tale-Yazdi --- pallets/ah-migrator/Cargo.toml | 6 ++++++ pallets/rc-migrator/Cargo.toml | 6 ++++++ .../asset-hubs/asset-hub-polkadot/Cargo.toml | 10 ++++++++++ 3 files changed, 22 insertions(+) diff --git a/pallets/ah-migrator/Cargo.toml b/pallets/ah-migrator/Cargo.toml index 4c9fb8f799..c7260084ba 100644 --- a/pallets/ah-migrator/Cargo.toml +++ b/pallets/ah-migrator/Cargo.toml @@ -47,7 +47,9 @@ std = [ "frame-system/std", "hex/std", "log/std", + "pallet-bags-list/std", "pallet-balances/std", + "pallet-fast-unstake/std", "pallet-multisig/std", "pallet-nomination-pools/std", "pallet-preimage/std", @@ -72,7 +74,9 @@ runtime-benchmarks = [ "frame-benchmarking/runtime-benchmarks", "frame-support/runtime-benchmarks", "frame-system/runtime-benchmarks", + "pallet-bags-list/runtime-benchmarks", "pallet-balances/runtime-benchmarks", + "pallet-fast-unstake/runtime-benchmarks", "pallet-multisig/runtime-benchmarks", "pallet-nomination-pools/runtime-benchmarks", "pallet-preimage/runtime-benchmarks", @@ -90,7 +94,9 @@ runtime-benchmarks = [ try-runtime = [ "frame-support/try-runtime", "frame-system/try-runtime", + "pallet-bags-list/try-runtime", "pallet-balances/try-runtime", + "pallet-fast-unstake/try-runtime", "pallet-multisig/try-runtime", "pallet-nomination-pools/try-runtime", "pallet-preimage/try-runtime", diff --git a/pallets/rc-migrator/Cargo.toml b/pallets/rc-migrator/Cargo.toml index 0c4a49a9c1..795c1f5c0d 100644 --- a/pallets/rc-migrator/Cargo.toml +++ b/pallets/rc-migrator/Cargo.toml @@ -43,7 +43,9 @@ std = [ "frame-support/std", "frame-system/std", "log/std", + "pallet-bags-list/std", "pallet-balances/std", + "pallet-fast-unstake/std", "pallet-multisig/std", "pallet-nomination-pools/std", "pallet-preimage/std", @@ -66,7 +68,9 @@ runtime-benchmarks = [ "frame-benchmarking/runtime-benchmarks", "frame-support/runtime-benchmarks", "frame-system/runtime-benchmarks", + "pallet-bags-list/runtime-benchmarks", "pallet-balances/runtime-benchmarks", + "pallet-fast-unstake/runtime-benchmarks", "pallet-multisig/runtime-benchmarks", "pallet-nomination-pools/runtime-benchmarks", "pallet-preimage/runtime-benchmarks", @@ -82,7 +86,9 @@ runtime-benchmarks = [ try-runtime = [ "frame-support/try-runtime", "frame-system/try-runtime", + "pallet-bags-list/try-runtime", "pallet-balances/try-runtime", + "pallet-fast-unstake/try-runtime", "pallet-multisig/try-runtime", "pallet-nomination-pools/try-runtime", "pallet-preimage/try-runtime", diff --git a/system-parachains/asset-hubs/asset-hub-polkadot/Cargo.toml b/system-parachains/asset-hubs/asset-hub-polkadot/Cargo.toml index c66d1c4c6d..8b22e93cec 100644 --- a/system-parachains/asset-hubs/asset-hub-polkadot/Cargo.toml +++ b/system-parachains/asset-hubs/asset-hub-polkadot/Cargo.toml @@ -141,17 +141,20 @@ runtime-benchmarks = [ "cumulus-primitives-core/runtime-benchmarks", "cumulus-primitives-utility/runtime-benchmarks", "frame-benchmarking/runtime-benchmarks", + "frame-election-provider-support/runtime-benchmarks", "frame-support/runtime-benchmarks", "frame-system-benchmarking/runtime-benchmarks", "frame-system/runtime-benchmarks", "hex-literal", "pallet-asset-conversion/runtime-benchmarks", "pallet-assets/runtime-benchmarks", + "pallet-bags-list/runtime-benchmarks", "pallet-balances/runtime-benchmarks", "pallet-bounties/runtime-benchmarks", "pallet-child-bounties/runtime-benchmarks", "pallet-collator-selection/runtime-benchmarks", "pallet-conviction-voting/runtime-benchmarks", + "pallet-fast-unstake/runtime-benchmarks", "pallet-message-queue/runtime-benchmarks", "pallet-multisig/runtime-benchmarks", "pallet-nfts/runtime-benchmarks", @@ -187,6 +190,7 @@ try-runtime = [ "cumulus-pallet-parachain-system/try-runtime", "cumulus-pallet-xcm/try-runtime", "cumulus-pallet-xcmp-queue/try-runtime", + "frame-election-provider-support/try-runtime", "frame-executive/try-runtime", "frame-support/try-runtime", "frame-system/try-runtime", @@ -196,11 +200,13 @@ try-runtime = [ "pallet-assets/try-runtime", "pallet-aura/try-runtime", "pallet-authorship/try-runtime", + "pallet-bags-list/try-runtime", "pallet-balances/try-runtime", "pallet-bounties/try-runtime", "pallet-child-bounties/try-runtime", "pallet-collator-selection/try-runtime", "pallet-conviction-voting/try-runtime", + "pallet-fast-unstake/try-runtime", "pallet-message-queue/try-runtime", "pallet-multisig/try-runtime", "pallet-nfts/try-runtime", @@ -242,6 +248,7 @@ std = [ "cumulus-primitives-core/std", "cumulus-primitives-utility/std", "frame-benchmarking?/std", + "frame-election-provider-support/std", "frame-executive/std", "frame-metadata-hash-extension/std", "frame-support/std", @@ -256,11 +263,13 @@ std = [ "pallet-assets/std", "pallet-aura/std", "pallet-authorship/std", + "pallet-bags-list/std", "pallet-balances/std", "pallet-bounties/std", "pallet-child-bounties/std", "pallet-collator-selection/std", "pallet-conviction-voting/std", + "pallet-fast-unstake/std", "pallet-message-queue/std", "pallet-multisig/std", "pallet-nfts-runtime-api/std", @@ -300,6 +309,7 @@ std = [ "sp-genesis-builder/std", "sp-inherents/std", "sp-io/std", + "sp-npos-elections/std", "sp-offchain/std", "sp-runtime/std", "sp-session/std", From 43256bb696b705affe519658e96a2e18871b2429 Mon Sep 17 00:00:00 2001 From: Oliver Tale-Yazdi Date: Fri, 31 Jan 2025 13:36:20 +0100 Subject: [PATCH 06/10] Deposit Signed-off-by: Oliver Tale-Yazdi --- .../asset-hubs/asset-hub-polkadot/src/staking/mod.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/system-parachains/asset-hubs/asset-hub-polkadot/src/staking/mod.rs b/system-parachains/asset-hubs/asset-hub-polkadot/src/staking/mod.rs index ba76aecc2e..c58b184a9c 100644 --- a/system-parachains/asset-hubs/asset-hub-polkadot/src/staking/mod.rs +++ b/system-parachains/asset-hubs/asset-hub-polkadot/src/staking/mod.rs @@ -23,11 +23,16 @@ pub mod nom_pools; use crate::*; +parameter_types! { + // 1% of the Relay Chain's deposit + pub const FastUnstakeDeposit: Balance = UNITS / 100; +} + impl pallet_fast_unstake::Config for Runtime { type RuntimeEvent = RuntimeEvent; type Currency = Balances; type BatchSize = frame_support::traits::ConstU32<16>; - type Deposit = frame_support::traits::ConstU128<{ UNITS }>; + type Deposit = FastUnstakeDeposit; type ControlOrigin = EnsureRoot; type Staking = nom_pools::StakingMock; type MaxErasToCheckPerBlock = ConstU32<1>; From 651f5c1861baa1e92256ec955082a57a42da494c Mon Sep 17 00:00:00 2001 From: Oliver Tale-Yazdi Date: Fri, 31 Jan 2025 13:53:49 +0100 Subject: [PATCH 07/10] cleanup Signed-off-by: Oliver Tale-Yazdi --- pallets/ah-migrator/src/lib.rs | 2 +- pallets/ah-migrator/src/scheduler.rs | 2 +- pallets/rc-migrator/src/lib.rs | 22 ++++++++++++++-------- pallets/rc-migrator/src/scheduler.rs | 3 +-- 4 files changed, 17 insertions(+), 12 deletions(-) diff --git a/pallets/ah-migrator/src/lib.rs b/pallets/ah-migrator/src/lib.rs index 281e2549ca..7e5082fa91 100644 --- a/pallets/ah-migrator/src/lib.rs +++ b/pallets/ah-migrator/src/lib.rs @@ -461,7 +461,7 @@ pub mod pallet { Self::do_receive_bags_list_messages(messages).map_err(Into::into) } - + #[pallet::call_index(12)] pub fn receive_scheduler_messages( origin: OriginFor, diff --git a/pallets/ah-migrator/src/scheduler.rs b/pallets/ah-migrator/src/scheduler.rs index b16c0454c2..1a5933426d 100644 --- a/pallets/ah-migrator/src/scheduler.rs +++ b/pallets/ah-migrator/src/scheduler.rs @@ -75,7 +75,7 @@ impl Pallet { } else { log::error!( target: LOG_TARGET, - "Failed to convert RC call to AH call for task at block number {}", + "Failed to convert RC call to AH call for task at block number {:?}", block_number ); continue; diff --git a/pallets/rc-migrator/src/lib.rs b/pallets/rc-migrator/src/lib.rs index 668c29c560..f5d4bdf497 100644 --- a/pallets/rc-migrator/src/lib.rs +++ b/pallets/rc-migrator/src/lib.rs @@ -99,8 +99,11 @@ impl From for Error { } } -pub type MigrationStageOf = - MigrationStage<::AccountId, BlockNumberFor, >::Score>; +pub type MigrationStageOf = MigrationStage< + ::AccountId, + BlockNumberFor, + >::Score, +>; #[derive(Encode, Decode, Clone, Default, RuntimeDebug, TypeInfo, MaxEncodedLen, PartialEq, Eq)] pub enum MigrationStage { @@ -171,6 +174,7 @@ pub enum MigrationStage { BagsListMigrationOngoing { next_key: Option>, }, + BagsListMigrationDone, SchedulerMigrationInit, SchedulerMigrationOngoing { last_key: Option>, @@ -179,7 +183,7 @@ pub enum MigrationStage { MigrationDone, } -impl MigrationStage { +impl MigrationStage { /// Whether the migration is finished. /// /// This is **not** the same as `!self.is_ongoing()`. @@ -272,15 +276,15 @@ pub mod pallet { /// A stage transition has occurred. StageTransition { /// The old stage before the transition. - old: MigrationStageFor, + old: MigrationStageOf, /// The new stage after the transition. - new: MigrationStageFor, + new: MigrationStageOf, }, } /// The Relay Chain migration state. #[pallet::storage] - pub type RcMigrationStage = StorageValue<_, MigrationStageFor, ValueQuery>; + pub type RcMigrationStage = StorageValue<_, MigrationStageOf, ValueQuery>; /// Helper storage item to obtain and store the known accounts that should be kept partially on /// fully on Relay Chain. @@ -682,7 +686,9 @@ pub mod pallet { Self::transition(MigrationStage::BagsListMigrationDone); }, Ok(Some(next_key)) => { - Self::transition(MigrationStage::BagsListMigrationOngoing { next_key: Some(next_key) }); + Self::transition(MigrationStage::BagsListMigrationOngoing { + next_key: Some(next_key), + }); }, e => { defensive!("Error while migrating bags list: {:?}", e); @@ -733,7 +739,7 @@ pub mod pallet { impl Pallet { /// Execute a stage transition and log it. - fn transition(new: MigrationStageFor) { + fn transition(new: MigrationStageOf) { let old = RcMigrationStage::::get(); RcMigrationStage::::put(&new); log::info!(target: LOG_TARGET, "[Block {:?}] Stage transition: {:?} -> {:?}", frame_system::Pallet::::block_number(), &old, &new); diff --git a/pallets/rc-migrator/src/scheduler.rs b/pallets/rc-migrator/src/scheduler.rs index 1eec3f7801..c708e6f3c4 100644 --- a/pallets/rc-migrator/src/scheduler.rs +++ b/pallets/rc-migrator/src/scheduler.rs @@ -152,8 +152,7 @@ pub mod alias { /// Information regarding an item to be executed in the future. // FROM: https://github.com/paritytech/polkadot-sdk/blob/f373af0d1c1e296c1b07486dd74710b40089250e/substrate/frame/scheduler/src/lib.rs#L148 - #[cfg_attr(any(feature = "std", test), derive(PartialEq, Eq))] - #[derive(Clone, RuntimeDebug, Encode, Decode, MaxEncodedLen, TypeInfo)] + #[derive(Clone, RuntimeDebug, Encode, Decode, MaxEncodedLen, TypeInfo, PartialEq, Eq)] pub struct Scheduled { /// The unique identity for this task, if there is one. pub maybe_id: Option, From 0839abd9378207ccea258820701ef2fcd9a5749d Mon Sep 17 00:00:00 2001 From: Oliver Tale-Yazdi Date: Fri, 31 Jan 2025 17:46:07 +0100 Subject: [PATCH 08/10] cleanup Signed-off-by: Oliver Tale-Yazdi --- integration-tests/ahm/src/tests.rs | 16 +++--------- pallets/rc-migrator/src/lib.rs | 40 +++++++++++++++++++----------- 2 files changed, 29 insertions(+), 27 deletions(-) diff --git a/integration-tests/ahm/src/tests.rs b/integration-tests/ahm/src/tests.rs index 1beeccf5c0..ea63156c87 100644 --- a/integration-tests/ahm/src/tests.rs +++ b/integration-tests/ahm/src/tests.rs @@ -33,7 +33,8 @@ use cumulus_primitives_core::{AggregateMessageOrigin, ParaId}; use frame_support::traits::*; -use pallet_rc_migrator::{types::PalletMigrationChecks, RcMigrationStage}; +use pallet_rc_migrator::{types::PalletMigrationChecks, MigrationStage, RcMigrationStage}; +use std::str::FromStr; use asset_hub_polkadot_runtime::Runtime as AssetHub; use polkadot_runtime::Runtime as Polkadot; @@ -50,7 +51,7 @@ async fn account_migration_works() { let mut dmps = Vec::new(); if let Ok(stage) = std::env::var("START_STAGE") { - let stage = state_from_str::(&stage); + let stage = MigrationStage::from_str(&stage).expect("Invalid start stage"); RcMigrationStage::::put(stage); } @@ -107,14 +108,3 @@ async fn account_migration_works() { // some overweight ones. }); } - -pub fn state_from_str( - s: &str, -) -> pallet_rc_migrator::MigrationStageOf { - use pallet_rc_migrator::MigrationStage; - match s { - "preimage" => MigrationStage::PreimageMigrationInit, - "referenda" => MigrationStage::ReferendaMigrationInit, - _ => MigrationStage::Pending, - } -} diff --git a/pallets/rc-migrator/src/lib.rs b/pallets/rc-migrator/src/lib.rs index f5d4bdf497..335628c4e3 100644 --- a/pallets/rc-migrator/src/lib.rs +++ b/pallets/rc-migrator/src/lib.rs @@ -42,6 +42,7 @@ mod weights; pub use pallet::*; pub mod scheduler; +use accounts::AccountsMigrator; use frame_support::{ pallet_prelude::*, sp_runtime::traits::AccountIdConversion, @@ -54,31 +55,28 @@ use frame_support::{ weights::WeightMeter, }; use frame_system::{pallet_prelude::*, AccountInfo}; +use multisig::MultisigMigrator; use pallet_balances::AccountData; use polkadot_parachain_primitives::primitives::Id as ParaId; use polkadot_runtime_common::paras_registrar; -use runtime_parachains::hrmp; -use sp_core::{crypto::Ss58Codec, H256}; -use sp_runtime::AccountId32; -use sp_std::prelude::*; -use storage::TransactionOutcome; -use types::AhWeightInfo; -use weights::WeightInfo; -use xcm::prelude::*; - -use accounts::AccountsMigrator; -use multisig::MultisigMigrator; use preimage::{ PreimageChunkMigrator, PreimageLegacyRequestStatusMigrator, PreimageRequestStatusMigrator, }; use proxy::*; use referenda::ReferendaStage; +use runtime_parachains::hrmp; +use sp_core::{crypto::Ss58Codec, H256}; +use sp_runtime::AccountId32; +use sp_std::prelude::*; use staking::{ bags_list::{BagsListMigrator, BagsListStage}, fast_unstake::{FastUnstakeMigrator, FastUnstakeStage}, nom_pools::{NomPoolsMigrator, NomPoolsStage}, }; -use types::PalletMigration; +use storage::TransactionOutcome; +use types::{AhWeightInfo, PalletMigration}; +use weights::WeightInfo; +use xcm::prelude::*; /// The log target of this pallet. pub const LOG_TARGET: &str = "runtime::rc-migrator"; @@ -199,6 +197,22 @@ impl MigrationStage std::str::FromStr + for MigrationStage +{ + type Err = std::string::String; + + fn from_str(s: &str) -> Result { + Ok(match s { + "preimage" => MigrationStage::PreimageMigrationInit, + "referenda" => MigrationStage::ReferendaMigrationInit, + "multisig" => MigrationStage::MultisigMigrationInit, + other => return Err(format!("Unknown migration stage: {}", other)), + }) + } +} + type AccountInfoFor = AccountInfo<::Nonce, ::AccountData>; @@ -322,8 +336,6 @@ pub mod pallet { // TODO: not complete Self::transition(MigrationStage::AccountsMigrationInit); - // toggle for testing - //Self::transition(MigrationStage::BagsListMigrationInit); }, MigrationStage::AccountsMigrationInit => { // TODO: weights From d479de3f8740fe2e403d7c0da8c2befaf9f7e466 Mon Sep 17 00:00:00 2001 From: Oliver Tale-Yazdi Date: Fri, 31 Jan 2025 17:58:22 +0100 Subject: [PATCH 09/10] fix Signed-off-by: Oliver Tale-Yazdi --- integration-tests/ahm/src/tests.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/integration-tests/ahm/src/tests.rs b/integration-tests/ahm/src/tests.rs index ea63156c87..3831ea1e93 100644 --- a/integration-tests/ahm/src/tests.rs +++ b/integration-tests/ahm/src/tests.rs @@ -98,11 +98,16 @@ async fn account_migration_works() { log::debug!("AH DMP messages left: {}", fp.storage.count); next_block_ah(); + + if RcMigrationStage::::get() == + pallet_rc_migrator::MigrationStage::PreimageMigrationDone + { + pallet_rc_migrator::preimage::PreimageChunkMigrator::::post_check( + pre_check_payload.clone(), + ); + } } - pallet_rc_migrator::preimage::PreimageChunkMigrator::::post_check( - pre_check_payload, - ); pallet_ah_migrator::preimage::PreimageMigrationCheck::::post_check(()); // NOTE that the DMP queue is probably not empty because the snapshot that we use contains // some overweight ones. From 5fd498ae5cef4deb25bfe4e0bca8b488cc0a9439 Mon Sep 17 00:00:00 2001 From: Oliver Tale-Yazdi Date: Fri, 31 Jan 2025 18:14:29 +0100 Subject: [PATCH 10/10] fix Signed-off-by: Oliver Tale-Yazdi --- pallets/rc-migrator/src/lib.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pallets/rc-migrator/src/lib.rs b/pallets/rc-migrator/src/lib.rs index 335628c4e3..c0076880b9 100644 --- a/pallets/rc-migrator/src/lib.rs +++ b/pallets/rc-migrator/src/lib.rs @@ -572,8 +572,10 @@ pub mod pallet { ); }, e => { - log::error!(target: LOG_TARGET, "Error while migrating legacy preimage request status: {:?}", e); - defensive!("Error while migrating legacy preimage request status"); + defensive!( + "Error while migrating legacy preimage request status: {:?}", + e + ); }, } }, @@ -605,8 +607,7 @@ pub mod pallet { }); }, e => { - log::error!(target: LOG_TARGET, "Error while migrating nom pools: {:?}", e); - defensive!("Error while migrating nom pools"); + defensive!("Error while migrating nom pools: {:?}", e); }, } }, @@ -638,8 +639,7 @@ pub mod pallet { }); }, e => { - log::error!(target: LOG_TARGET, "Error while migrating fast unstake: {:?}", e); - defensive!("Error while migrating fast unstake"); + defensive!("Error while migrating fast unstake: {:?}", e); }, } },