Skip to content

A0-4581: Storage migration for CommitteeManagement pallet #1892

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ members = [
"pallets/elections",
"pallets/committee-management",
"pallets/operations",
"pallets/support",
"primitives",
"rate-limiter",
]
Expand Down Expand Up @@ -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 }
2 changes: 2 additions & 0 deletions bin/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion pallets/aleph/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
2 changes: 0 additions & 2 deletions pallets/committee-management/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ sp-std = { workspace = true }

pallet-aleph = { workspace = true }
pallet-elections = { workspace = true }
pallets-support = { workspace = true }
primitives = { workspace = true }

[features]
Expand All @@ -52,7 +51,6 @@ std = [
"sp-staking/std",
"sp-std/std",
"primitives/std",
"pallets-support/std",
"pallet-aleph/std",
"pallet-elections/std",
]
Expand Down
3 changes: 2 additions & 1 deletion pallets/committee-management/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ extern crate core;

mod impls;
mod manager;
pub mod migration;
#[cfg(test)]
mod mock;
#[cfg(test)]
Expand Down Expand Up @@ -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]
Expand Down
114 changes: 114 additions & 0 deletions pallets/committee-management/src/migration.rs
Original file line number Diff line number Diff line change
@@ -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)?)
}
}
}
2 changes: 0 additions & 2 deletions pallets/elections/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ sp-runtime = { workspace = true }
sp-staking = { workspace = true }
sp-std = { workspace = true }

pallets-support = { workspace = true }
primitives = { workspace = true }

[features]
Expand All @@ -47,7 +46,6 @@ std = [
"sp-staking/std",
"primitives/std",
"sp-io/std",
"pallets-support/std",
]
try-runtime = [
"frame-support/try-runtime",
Expand Down
21 changes: 0 additions & 21 deletions pallets/support/Cargo.toml

This file was deleted.

5 changes: 0 additions & 5 deletions pallets/support/src/lib.rs

This file was deleted.

34 changes: 0 additions & 34 deletions pallets/support/src/migration.rs

This file was deleted.