Skip to content

Commit

Permalink
migration
Browse files Browse the repository at this point in the history
  • Loading branch information
AmbientTea committed Feb 20, 2025
1 parent 679a35e commit dd780c1
Showing 1 changed file with 67 additions and 2 deletions.
69 changes: 67 additions & 2 deletions toolkit/pallets/session-validator-management/src/migrations/v1.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#[cfg(feature = "try-runtime")]
extern crate alloc;
use crate::CommitteeMemberInfo;
#[cfg(feature = "try-runtime")]
use alloc::vec::Vec;
use frame_support::traits::UncheckedOnRuntimeUpgrade;
use parity_scale_codec::Encode;

mod v0 {
use frame_support::pallet_prelude::OptionQuery;
Expand Down Expand Up @@ -110,11 +112,74 @@ where

#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, sp_runtime::TryRuntimeError> {
Ok(vec![])
let current_committee_v0 = v0::CurrentCommittee::<T>::get();
let next_committee_v0 = v0::NextCommittee::<T>::get();
Ok((current_committee_v0, next_committee_v0).encode())
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(_state: Vec<u8>) -> Result<(), sp_runtime::TryRuntimeError> {
fn post_upgrade(state: Vec<u8>) -> Result<(), sp_runtime::TryRuntimeError> {
use frame_support::ensure;
use parity_scale_codec::Decode;
use v0::LegacyCommitteeInfo;

let (current_committee_v0, next_committee_v0): (
LegacyCommitteeInfo<
T::ScEpochNumber,
T::AuthorityId,
T::AuthorityKeys,
T::MaxValidators,
>,
Option<
LegacyCommitteeInfo<
T::ScEpochNumber,
T::AuthorityId,
T::AuthorityKeys,
T::MaxValidators,
>,
>,
) = Decode::decode(&mut state.as_slice())
.expect("Previously encoded state should be decodable");

let current_committee_v1 = crate::CurrentCommittee::<T>::get();
let next_committee_v1 = crate::NextCommittee::<T>::get();

ensure!(
current_committee_v0.epoch == current_committee_v1.epoch,
"current epoch should be preserved"
);

ensure!(
current_committee_v0.committee.to_vec()
== (current_committee_v1.committee.iter())
.map(|member| (member.authority_id(), member.authority_keys()))
.collect::<Vec<_>>(),
"current committee membership should be preserved"
);

if next_committee_v0.is_none() && next_committee_v0.is_none() {
return Ok(());
}

ensure!(next_committee_v0.is_some(), "V0 next committee should be Some if V1 is");
ensure!(next_committee_v1.is_some(), "V1 next committee should be Some if V0 is");

let next_committee_v0 = next_committee_v0.unwrap();
let next_committee_v1 = next_committee_v1.unwrap();

ensure!(
next_committee_v0.epoch == next_committee_v1.epoch,
"next epoch should be preserved"
);

ensure!(
next_committee_v0.committee.to_vec()
== (next_committee_v1.committee.iter())
.map(|member| (member.authority_id(), member.authority_keys()))
.collect::<Vec<_>>(),
"next committee membership should be preserved"
);

Ok(())
}
}
Expand Down

0 comments on commit dd780c1

Please sign in to comment.