pallet_session_validator_management/migrations/
v1.rs

1//! Implements storage migration of the `session-validator-management` pallet from v0 to v1.
2#[cfg(feature = "try-runtime")]
3extern crate alloc;
4use frame_support::traits::UncheckedOnRuntimeUpgrade;
5#[cfg(feature = "try-runtime")]
6use {alloc::vec::Vec, parity_scale_codec::Encode};
7
8use super::v0;
9
10/// [frame_support::migrations::VersionedMigration] parametrized for v0 to v1 migration.
11pub type LegacyToV1Migration<T> = frame_support::migrations::VersionedMigration<
12	0, // The migration will only execute when the on-chain storage version is 0
13	1, // The on-chain storage version will be set to 1 after the migration is complete
14	InnerMigrateV0ToV1<T>,
15	crate::pallet::Pallet<T>,
16	<T as frame_system::Config>::DbWeight,
17>;
18
19/// Helper type used internally for migration. Use [LegacyToV1Migration] in your runtime instead.
20pub struct InnerMigrateV0ToV1<T: crate::Config>(core::marker::PhantomData<T>);
21
22impl<T: crate::pallet::Config> UncheckedOnRuntimeUpgrade for InnerMigrateV0ToV1<T> {
23	fn on_runtime_upgrade() -> sp_runtime::Weight {
24		use sp_core::Get;
25		use sp_runtime::BoundedVec;
26
27		let current_committee_v0 = v0::CurrentCommittee::<T>::get();
28		let current_committee_v1 = crate::pallet::CommitteeInfoOf::<T> {
29			epoch: current_committee_v0.epoch,
30			committee: BoundedVec::truncate_from(
31				current_committee_v0.committee.into_iter().map(From::from).collect(),
32			),
33		};
34
35		crate::CurrentCommittee::<T>::put(current_committee_v1);
36
37		let Some(next_committee_v0) = v0::NextCommittee::<T>::get() else {
38			return T::DbWeight::get().reads_writes(2, 1);
39		};
40		let next_committee_v1 = crate::pallet::CommitteeInfoOf::<T> {
41			epoch: next_committee_v0.epoch,
42			committee: BoundedVec::truncate_from(
43				next_committee_v0.committee.into_iter().map(From::from).collect(),
44			),
45		};
46
47		crate::NextCommittee::<T>::put(next_committee_v1);
48
49		T::DbWeight::get().reads_writes(2, 2)
50	}
51
52	#[cfg(feature = "try-runtime")]
53	fn pre_upgrade() -> Result<Vec<u8>, sp_runtime::TryRuntimeError> {
54		let current_committee_v0 = v0::CurrentCommittee::<T>::get();
55		let next_committee_v0 = v0::NextCommittee::<T>::get();
56		Ok((current_committee_v0, next_committee_v0).encode())
57	}
58
59	#[cfg(feature = "try-runtime")]
60	fn post_upgrade(state: Vec<u8>) -> Result<(), sp_runtime::TryRuntimeError> {
61		use frame_support::ensure;
62		use parity_scale_codec::Decode;
63		use sidechain_domain::ScEpochNumber;
64		use v0::LegacyCommitteeInfo;
65
66		let (current_committee_v0, next_committee_v0): (
67			LegacyCommitteeInfo<ScEpochNumber, T::AuthorityId, T::AuthorityKeys, T::MaxValidators>,
68			Option<
69				LegacyCommitteeInfo<
70					ScEpochNumber,
71					T::AuthorityId,
72					T::AuthorityKeys,
73					T::MaxValidators,
74				>,
75			>,
76		) = Decode::decode(&mut state.as_slice())
77			.expect("Previously encoded state should be decodable");
78
79		let current_committee_v1 = crate::CurrentCommittee::<T>::get();
80		let next_committee_v1 = crate::NextCommittee::<T>::get();
81
82		ensure!(
83			current_committee_v0.epoch == current_committee_v1.epoch,
84			"current epoch should be preserved"
85		);
86
87		ensure!(
88			current_committee_v0.committee.to_vec()
89				== (current_committee_v1.committee.iter())
90					.map(|member| (member.authority_id(), member.authority_keys()))
91					.collect::<Vec<_>>(),
92			"current committee membership should be preserved"
93		);
94
95		if next_committee_v0.is_none() && next_committee_v0.is_none() {
96			return Ok(());
97		}
98
99		ensure!(next_committee_v0.is_some(), "V0 next committee should be Some if V1 is");
100		ensure!(next_committee_v1.is_some(), "V1 next committee should be Some if V0 is");
101
102		let next_committee_v0 = next_committee_v0.unwrap();
103		let next_committee_v1 = next_committee_v1.unwrap();
104
105		ensure!(
106			next_committee_v0.epoch == next_committee_v1.epoch,
107			"next epoch should be preserved"
108		);
109
110		ensure!(
111			next_committee_v0.committee.to_vec()
112				== (next_committee_v1.committee.iter())
113					.map(|member| (member.authority_id(), member.authority_keys()))
114					.collect::<Vec<_>>(),
115			"next committee membership should be preserved"
116		);
117
118		Ok(())
119	}
120}