Skip to content
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

A0-4581: Storage migration for CommitteeManagement pallet #1892

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
3 changes: 3 additions & 0 deletions bin/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1034,13 +1034,16 @@ 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::v1::Migration<Runtime>;

/// Executive: handles dispatch to the various modules.
pub type Executive = frame_executive::Executive<
Runtime,
Block,
frame_system::ChainContext<Runtime>,
Runtime,
AllPalletsWithSystem,
Migration,
>;

#[cfg(feature = "runtime-benchmarks")]
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
1 change: 1 addition & 0 deletions 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
102 changes: 102 additions & 0 deletions pallets/committee-management/src/migration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
use frame_support::{
pallet_prelude::{StorageVersion, ValueQuery, Weight},
storage_alias,
traits::OnRuntimeUpgrade,
};
use log::info;
use parity_scale_codec::Decode;
use primitives::{ProductionBanConfig as ProductionBanConfigStruct, SessionValidators};

use crate::{CurrentAndNextSessionValidators, CurrentAndNextSessionValidatorsStorage};

pub mod v1 {
use frame_support::traits::Get;
use parity_scale_codec::Encode;
use primitives::KEY_TYPE;
use sp_runtime::traits::OpaqueKeys;

use super::*;
use crate::{Config, Pallet, ProductionBanConfig, LOG_TARGET};

#[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 + pallet_session::Config> OnRuntimeUpgrade for Migration<T> {
fn on_runtime_upgrade() -> Weight {
if StorageVersion::get::<Pallet<T>>() != StorageVersion::new(3) {
log::info!(
target: LOG_TARGET,
"Skipping migrations from STORAGE_VERSION 0 to 1 for pallet committee-management."
);
return T::DbWeight::get().reads(1);
};

let reads = 5; // StorageVersion, CurrentAndNextSessionValidatorsStorage, Authorities, NextFinalityCommittee, BanConfig
let mut writes = 2; // StorageVersion, ProductionBanConfig
info!(target: LOG_TARGET, "Running migration from STORAGE_VERSION 0 to 1 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 aleph_authorities = pallet_aleph::Authorities::<T>::get();
let mut finalizers = Vec::new();
for (v, keys) in pallet_session::QueuedKeys::<T>::get().into_iter() {
let aleph_key = keys
.get::<<T as pallet_aleph::Config>::AuthorityId>(KEY_TYPE)
.unwrap();

if aleph_authorities.iter().any(|aa| aa.eq(&aleph_key)) {
let v = T::AccountId::decode(&mut v.encode().as_ref()).unwrap();
finalizers.push(v);
}
}

let current_validators = SessionValidators {
producers: current_validators_legacy.current.committee,
finalizers,
non_committee: current_validators_legacy.current.non_committee,
};
let next_validators = SessionValidators {
producers: current_validators_legacy.next.committee,
finalizers: pallet_aleph::NextFinalityCommittee::<T>::get(),
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(1).put::<Pallet<T>>();
T::DbWeight::get().reads(reads) + T::DbWeight::get().writes(writes)
}
}
}
Loading