pallet_session_validator_management/migrations/
authority_keys.rs1extern crate alloc;
83
84use crate::*;
85use alloc::vec::Vec;
86use core::marker::PhantomData;
87use frame_support::traits::OnRuntimeUpgrade;
88use sp_core::Get;
89use sp_runtime::BoundedVec;
90use sp_runtime::traits::{Member, OpaqueKeys};
91
92pub trait UpgradeAuthorityKeys<NewAuthorityKeys> {
94 fn upgrade(self) -> NewAuthorityKeys;
96}
97
98pub struct AuthorityKeysMigration<
107 T,
108 OldAuthorityKeys,
109 const FROM_VERSION: u32,
110 const TO_VERSION: u32,
111> where
112 T: crate::Config,
113 OldAuthorityKeys: UpgradeAuthorityKeys<T::AuthorityKeys> + Member + Decode + Clone,
114{
115 _phantom: PhantomData<(T, OldAuthorityKeys)>,
116}
117
118impl<T: crate::Config, OldAuthorityKeys, const FROM_VERSION: u32, const TO_VERSION: u32>
119 AuthorityKeysMigration<T, OldAuthorityKeys, FROM_VERSION, TO_VERSION>
120where
121 OldAuthorityKeys: UpgradeAuthorityKeys<T::AuthorityKeys> + Member + Decode + Clone,
122{
123 fn upgrade_bounded_vec(
125 old: BoundedVec<CommitteeMember<T::AuthorityId, OldAuthorityKeys>, T::MaxValidators>,
126 ) -> BoundedVec<CommitteeMemberOf<T>, T::MaxValidators> {
127 BoundedVec::truncate_from(
128 old.into_iter()
129 .map(|old| old.map_authority_keys(OldAuthorityKeys::upgrade))
130 .collect::<Vec<_>>(),
131 )
132 }
133
134 fn upgrade_committee_info(
136 old: CommitteeInfo<T::AuthorityId, OldAuthorityKeys, T::MaxValidators>,
137 ) -> CommitteeInfoOf<T> {
138 CommitteeInfo { epoch: old.epoch, committee: Self::upgrade_bounded_vec(old.committee) }
139 }
140}
141
142impl<T, OldAuthorityKeys, const FROM_VERSION: u32, const TO_VERSION: u32> OnRuntimeUpgrade
143 for AuthorityKeysMigration<T, OldAuthorityKeys, FROM_VERSION, TO_VERSION>
144where
145 T: crate::Config + pallet_session::Config<Keys = <T as crate::Config>::AuthorityKeys>,
146 OldAuthorityKeys: UpgradeAuthorityKeys<T::AuthorityKeys> + OpaqueKeys + Member + Decode + Clone,
147{
148 fn on_runtime_upgrade() -> sp_runtime::Weight {
149 let current_version = crate::AuthorityKeysVersion::<T>::get();
150
151 let mut weight = T::DbWeight::get().reads_writes(1, 0);
152
153 if TO_VERSION <= current_version {
154 log::info!(
155 "🚚 AuthorityKeysMigration {FROM_VERSION}->{TO_VERSION} can be removed; authority keys storage is already at version {current_version}."
156 );
157 return weight;
158 }
159 if current_version != FROM_VERSION {
160 log::warn!(
161 "🚚 AuthorityKeysMigration {FROM_VERSION}->{TO_VERSION} can not be applied to authority keys storage at version {current_version}."
162 );
163 return weight;
164 }
165
166 if let Some(new) = CurrentCommittee::<T>::translate::<
167 CommitteeInfo<T::AuthorityId, OldAuthorityKeys, T::MaxValidators>,
168 _,
169 >(|old| old.map(Self::upgrade_committee_info))
170 .expect("Decoding of the old value must succeed")
171 {
172 CurrentCommittee::<T>::put(new);
173 log::info!("🚚️ Migrated current committee storage to version {TO_VERSION}");
174 weight = weight.saturating_add(T::DbWeight::get().reads_writes(1, 1));
175 }
176
177 if let Some(new) = NextCommittee::<T>::translate::<
178 CommitteeInfo<T::AuthorityId, OldAuthorityKeys, T::MaxValidators>,
179 _,
180 >(|old| old.map(Self::upgrade_committee_info))
181 .expect("Decoding of the old value must succeed")
182 {
183 NextCommittee::<T>::put(new);
184 log::info!("🚚️ Migrated new committee storage to version {TO_VERSION}");
185 weight = weight.saturating_add(T::DbWeight::get().reads_writes(1, 1));
186 }
187
188 pallet_session::Pallet::<T>::upgrade_keys(|_id, old_keys| {
189 OldAuthorityKeys::upgrade(old_keys)
190 });
191 weight.saturating_add(T::DbWeight::get().reads_writes(2, 2));
192 log::info!("🚚️ Migrated keys in pallet_session to version {TO_VERSION}");
193
194 crate::AuthorityKeysVersion::<T>::set(TO_VERSION);
195 weight.saturating_add(T::DbWeight::get().reads_writes(0, 1));
196
197 weight
198 }
199}