diff --git a/Cargo.lock b/Cargo.lock index 1fad450f95..6b5107f3f2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5125,7 +5125,6 @@ dependencies = [ "pallet-session", "pallet-staking", "pallet-timestamp", - "pallets-support", "parity-scale-codec", "primitives", "rand", @@ -5202,7 +5201,6 @@ dependencies = [ "pallet-authorship", "pallet-balances", "pallet-staking", - "pallets-support", "parity-scale-codec", "primitives", "rand", @@ -5571,14 +5569,6 @@ dependencies = [ "sp-std", ] -[[package]] -name = "pallets-support" -version = "0.1.4" -dependencies = [ - "frame-support", - "sp-std", -] - [[package]] name = "parity-db" version = "0.4.13" diff --git a/Cargo.toml b/Cargo.toml index 70b9ddbfbb..c76d1712d0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,7 +14,6 @@ members = [ "pallets/elections", "pallets/committee-management", "pallets/operations", - "pallets/support", "primitives", "rate-limiter", ] @@ -185,5 +184,4 @@ pallet-aleph-runtime-api = { path = "pallets/aleph-runtime-api", default-feature pallet-committee-management = { path = "pallets/committee-management", default-features = false } pallet-elections = { path = "pallets/elections", default-features = false } pallet-operations = { path = "pallets/operations", default-features = false } -pallets-support = { path = "pallets/support", default-features = false } primitives = { path = "primitives", default-features = false } diff --git a/bin/runtime/src/lib.rs b/bin/runtime/src/lib.rs index 6fa013166c..4a58d13918 100644 --- a/bin/runtime/src/lib.rs +++ b/bin/runtime/src/lib.rs @@ -1033,6 +1033,8 @@ pub type SignedBlock = generic::SignedBlock<Block>; /// BlockId type as expected by this runtime. pub type BlockId = generic::BlockId<Block>; +pub type Migration = pallet_committee_management::migration::v2::Migration<Runtime>; + /// Executive: handles dispatch to the various modules. pub type Executive = frame_executive::Executive< Runtime, diff --git a/pallets/aleph/src/lib.rs b/pallets/aleph/src/lib.rs index ba117c1d7b..382226333f 100644 --- a/pallets/aleph/src/lib.rs +++ b/pallets/aleph/src/lib.rs @@ -104,7 +104,7 @@ pub mod pallet { #[pallet::storage] #[pallet::getter(fn authorities)] - pub(super) type Authorities<T: Config> = StorageValue<_, Vec<T::AuthorityId>, ValueQuery>; + pub type Authorities<T: Config> = StorageValue<_, Vec<T::AuthorityId>, ValueQuery>; #[pallet::storage] #[pallet::getter(fn next_authorities)] diff --git a/pallets/committee-management/Cargo.toml b/pallets/committee-management/Cargo.toml index 065ab57316..072f1edd88 100644 --- a/pallets/committee-management/Cargo.toml +++ b/pallets/committee-management/Cargo.toml @@ -28,7 +28,6 @@ sp-std = { workspace = true } pallet-aleph = { workspace = true } pallet-elections = { workspace = true } -pallets-support = { workspace = true } primitives = { workspace = true } [features] @@ -52,7 +51,6 @@ std = [ "sp-staking/std", "sp-std/std", "primitives/std", - "pallets-support/std", "pallet-aleph/std", "pallet-elections/std", ] diff --git a/pallets/committee-management/src/lib.rs b/pallets/committee-management/src/lib.rs index 3f7ddc215c..8477062d7c 100644 --- a/pallets/committee-management/src/lib.rs +++ b/pallets/committee-management/src/lib.rs @@ -5,6 +5,7 @@ extern crate core; mod impls; mod manager; +pub mod migration; #[cfg(test)] mod mock; #[cfg(test)] @@ -51,7 +52,7 @@ impl Get<Perquintill> for DefaultLenientThreshold { } } -const STORAGE_VERSION: StorageVersion = StorageVersion::new(1); +const STORAGE_VERSION: StorageVersion = StorageVersion::new(2); pub(crate) const LOG_TARGET: &str = "pallet-committee-management"; #[frame_support::pallet] diff --git a/pallets/committee-management/src/migration.rs b/pallets/committee-management/src/migration.rs new file mode 100644 index 0000000000..e284aa8de8 --- /dev/null +++ b/pallets/committee-management/src/migration.rs @@ -0,0 +1,114 @@ +use frame_support::{ + pallet_prelude::{PalletInfoAccess, StorageVersion, ValueQuery, Weight}, + storage_alias, + traits::OnRuntimeUpgrade, +}; +use log::info; +use parity_scale_codec::Decode; +use primitives::{ProductionBanConfig as ProductionBanConfigStruct, SessionValidators}; +use sp_std::vec::Vec; + +use crate::{CurrentAndNextSessionValidators, CurrentAndNextSessionValidatorsStorage}; + +/// Ensure that the current pallet storage version matches `version`. +pub fn ensure_storage_version<P: PalletInfoAccess>(version: u16) -> Result<(), &'static str> { + if StorageVersion::get::<P>() == StorageVersion::new(version) { + Ok(()) + } else { + Err("Bad storage version") + } +} + +pub mod v2 { + use frame_support::traits::Get; + + use super::*; + use crate::{Config, Pallet, ProductionBanConfig, LOG_TARGET}; + + const OLD_VERSION: u16 = 1; + const NEW_VERSION: u16 = 2; + + #[derive(Decode)] + pub struct SessionValidatorsLegacy<T> { + pub committee: Vec<T>, + pub non_committee: Vec<T>, + } + + #[derive(Decode)] + pub struct CurrentAndNextSessionValidatorsLegacy<T> { + pub next: SessionValidatorsLegacy<T>, + pub current: SessionValidatorsLegacy<T>, + } + + #[storage_alias] + type BanConfig<T: Config> = StorageValue<Pallet<T>, ProductionBanConfigStruct, ValueQuery>; + + pub struct Migration<T>(sp_std::marker::PhantomData<T>); + + impl<T: Config + pallet_aleph::Config> OnRuntimeUpgrade for Migration<T> { + fn on_runtime_upgrade() -> Weight { + if StorageVersion::get::<Pallet<T>>() != StorageVersion::new(OLD_VERSION) { + log::info!( + target: LOG_TARGET, + "Skipping migrations from STORAGE_VERSION 1 to 2 for pallet committee-management." + ); + return T::DbWeight::get().reads(1); + }; + + let reads = 4; // StorageVersion, CurrentAndNextSessionValidatorsStorage, NextFinalityCommittee, BanConfig + let mut writes = 2; // StorageVersion, ProductionBanConfig + info!(target: LOG_TARGET, "Running migration from STORAGE_VERSION 1 to 2 for pallet committee-management."); + + let res = CurrentAndNextSessionValidatorsStorage::<T>::translate::< + CurrentAndNextSessionValidatorsLegacy<T::AccountId>, + _, + >(|current_validators_legacy| { + let current_validators_legacy = + current_validators_legacy.expect("This storage exists"); + + let finalizers = pallet_aleph::NextFinalityCommittee::<T>::get(); + let current_validators = SessionValidators { + producers: current_validators_legacy.current.committee, + finalizers: finalizers.clone(), // we use next finalizers as it's hard to get current but we won't need them in current session. + non_committee: current_validators_legacy.current.non_committee, + }; + let next_validators = SessionValidators { + producers: current_validators_legacy.next.committee, + finalizers, + non_committee: current_validators_legacy.next.non_committee, + }; + + Some(CurrentAndNextSessionValidators { + current: current_validators, + next: next_validators, + }) + }); + if res.is_ok() { + writes += 1; + } else { + log::error!(target: LOG_TARGET, "Could not migrate CurrentAndNextSessionValidatorsStorage."); + }; + + let ban_config = BanConfig::<T>::get(); + ProductionBanConfig::<T>::put(ban_config); + BanConfig::<T>::kill(); + + StorageVersion::new(NEW_VERSION).put::<Pallet<T>>(); + info!(target: LOG_TARGET, "Finished migration from STORAGE_VERSION 1 to 2 for pallet committee-management."); + + T::DbWeight::get().reads(reads) + T::DbWeight::get().writes(writes) + } + + #[cfg(feature = "try-runtime")] + fn pre_upgrade() -> Result<Vec<u8>, sp_runtime::DispatchError> { + ensure_storage_version::<Pallet<T>>(OLD_VERSION)?; + + Ok(Vec::new()) + } + + #[cfg(feature = "try-runtime")] + fn post_upgrade(_: Vec<u8>) -> Result<(), sp_runtime::DispatchError> { + Ok(ensure_storage_version::<Pallet<T>>(NEW_VERSION)?) + } + } +} diff --git a/pallets/elections/Cargo.toml b/pallets/elections/Cargo.toml index 94323face7..cc08fcb640 100644 --- a/pallets/elections/Cargo.toml +++ b/pallets/elections/Cargo.toml @@ -25,7 +25,6 @@ sp-runtime = { workspace = true } sp-staking = { workspace = true } sp-std = { workspace = true } -pallets-support = { workspace = true } primitives = { workspace = true } [features] @@ -47,7 +46,6 @@ std = [ "sp-staking/std", "primitives/std", "sp-io/std", - "pallets-support/std", ] try-runtime = [ "frame-support/try-runtime", diff --git a/pallets/support/Cargo.toml b/pallets/support/Cargo.toml deleted file mode 100644 index 48bea5e802..0000000000 --- a/pallets/support/Cargo.toml +++ /dev/null @@ -1,21 +0,0 @@ -[package] -name = "pallets-support" -version = "0.1.4" -authors.workspace = true -edition.workspace = true -homepage.workspace = true -repository.workspace = true - -[dependencies] -frame-support = { workspace = true } -sp-std = { workspace = true } - -[features] -default = ["std"] -std = [ - "frame-support/std", - "sp-std/std" -] -try-runtime = [ - "frame-support/try-runtime", -] diff --git a/pallets/support/src/lib.rs b/pallets/support/src/lib.rs deleted file mode 100644 index c033adad87..0000000000 --- a/pallets/support/src/lib.rs +++ /dev/null @@ -1,5 +0,0 @@ -#![cfg_attr(not(feature = "std"), no_std)] - -mod migration; - -pub use migration::{ensure_storage_version, StorageMigration}; diff --git a/pallets/support/src/migration.rs b/pallets/support/src/migration.rs deleted file mode 100644 index d43a8d5a3e..0000000000 --- a/pallets/support/src/migration.rs +++ /dev/null @@ -1,34 +0,0 @@ -use frame_support::{ - pallet_prelude::{PalletInfoAccess, StorageVersion, Weight}, - traits::OnRuntimeUpgrade, -}; - -/// In order to run both pre- and post- checks around every migration, we entangle methods of -/// `OnRuntimeUpgrade` into the desired flow and expose it with `migrate` method. -/// -/// This way, `try-runtime` no longer triggers checks. We do it by hand. -pub trait StorageMigration: OnRuntimeUpgrade { - #[allow(clippy::let_and_return)] - fn migrate() -> Weight { - #[cfg(feature = "try-runtime")] - let state = Self::pre_upgrade().expect("Pre upgrade should succeed"); - - let weight = Self::on_runtime_upgrade(); - - #[cfg(feature = "try-runtime")] - Self::post_upgrade(state).expect("Post upgrade should succeed"); - - weight - } -} - -impl<T: OnRuntimeUpgrade> StorageMigration for T {} - -/// Ensure that the current pallet storage version matches `version`. -pub fn ensure_storage_version<P: PalletInfoAccess>(version: u16) -> Result<(), &'static str> { - if StorageVersion::get::<P>() == StorageVersion::new(version) { - Ok(()) - } else { - Err("Bad storage version") - } -}