diff --git a/toolkit/pallets/session-validator-management/src/migrations/v1.rs b/toolkit/pallets/session-validator-management/src/migrations/v1.rs index c25f1053d..0c056d686 100644 --- a/toolkit/pallets/session-validator-management/src/migrations/v1.rs +++ b/toolkit/pallets/session-validator-management/src/migrations/v1.rs @@ -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; @@ -110,11 +112,74 @@ where #[cfg(feature = "try-runtime")] fn pre_upgrade() -> Result, sp_runtime::TryRuntimeError> { - Ok(vec![]) + let current_committee_v0 = v0::CurrentCommittee::::get(); + let next_committee_v0 = v0::NextCommittee::::get(); + Ok((current_committee_v0, next_committee_v0).encode()) } #[cfg(feature = "try-runtime")] - fn post_upgrade(_state: Vec) -> Result<(), sp_runtime::TryRuntimeError> { + fn post_upgrade(state: Vec) -> 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::::get(); + let next_committee_v1 = crate::NextCommittee::::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::>(), + "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::>(), + "next committee membership should be preserved" + ); + Ok(()) } }