Skip to content

Commit

Permalink
restructure migration
Browse files Browse the repository at this point in the history
  • Loading branch information
AmbientTea committed Feb 20, 2025
1 parent 0060729 commit e90921b
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ runtime like this:

``` rust
pub type Migrations = (
pallet_session_validator_management::migrations::v1::Migration<Runtime>,
pallet_session_validator_management::migrations::v1::LegacyToV1Migration<Runtime>,
// ...
);
/// Executive: handles dispatch to the various modules.
Expand Down Expand Up @@ -42,11 +42,10 @@ to select its committee, use the `CommitteeMember` type provided by this crate.
A `CommitteeMember` implementation for the legacy `(T::AuthorityId, T::AuthorityKeys)`
type is also provided and can be used.

### Migration
### Migration from Legacy

Migration logic is provided in the `migrations::v1` module. It assumes that
the types `T::AuthorityId` and `T::AuthorityKeys` do not change as part of
the same runtime upgrade. The only requirement for the new type
`T::CommitteeMember` is to implement the trait
`From<(T::AuthorityId, T::AuthorityKeys)>`.
Migration logic is provided by the `migrations::v1::LegacyToV1Migration` migration.
It assumes that the types `T::AuthorityId` and `T::AuthorityKeys` do not change as
part of the same runtime upgrade. The only requirement for the new type
`T::CommitteeMember` is to implement the trait `From<(T::AuthorityId, T::AuthorityKeys)>`.

Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod v0;
pub mod v1;
52 changes: 52 additions & 0 deletions toolkit/pallets/session-validator-management/src/migrations/v0.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use frame_support::pallet_prelude::{OptionQuery, ValueQuery, Zero};
use frame_support::{storage_alias, BoundedVec, CloneNoBound};
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;

#[derive(CloneNoBound, Encode, Decode, TypeInfo, MaxEncodedLen)]
#[scale_info(skip_type_params(MaxValidators))]
pub struct LegacyCommitteeInfo<
ScEpochNumber: Clone,
AuthorityId: Clone,
AuthorityKeys: Clone,
MaxValidators,
> {
pub epoch: ScEpochNumber,
pub committee: BoundedVec<(AuthorityId, AuthorityKeys), MaxValidators>,
}

impl<ScEpochNumber, AuthorityId, AuthorityKeys, MaxValidators> Default
for LegacyCommitteeInfo<ScEpochNumber, AuthorityId, AuthorityKeys, MaxValidators>
where
AuthorityId: Clone,
AuthorityKeys: Clone,
ScEpochNumber: Clone + Zero,
{
fn default() -> Self {
Self { epoch: ScEpochNumber::zero(), committee: BoundedVec::new() }
}
}

#[storage_alias]
pub type CurrentCommittee<T: crate::pallet::Config> = StorageValue<
crate::Pallet<T>,
LegacyCommitteeInfo<
<T as crate::pallet::Config>::ScEpochNumber,
<T as crate::pallet::Config>::AuthorityId,
<T as crate::pallet::Config>::AuthorityKeys,
<T as crate::pallet::Config>::MaxValidators,
>,
ValueQuery,
>;

#[storage_alias]
pub type NextCommittee<T: crate::pallet::Config> = StorageValue<
crate::Pallet<T>,
LegacyCommitteeInfo<
<T as crate::pallet::Config>::ScEpochNumber,
<T as crate::pallet::Config>::AuthorityId,
<T as crate::pallet::Config>::AuthorityKeys,
<T as crate::pallet::Config>::MaxValidators,
>,
OptionQuery,
>;
64 changes: 6 additions & 58 deletions toolkit/pallets/session-validator-management/src/migrations/v1.rs
Original file line number Diff line number Diff line change
@@ -1,64 +1,12 @@
#[cfg(feature = "try-runtime")]
extern crate alloc;
#[cfg(feature = "try-runtime")]
use alloc::vec::Vec;
use frame_support::pallet_prelude::{OptionQuery, ValueQuery, Zero};
use frame_support::traits::UncheckedOnRuntimeUpgrade;
use frame_support::{storage_alias, BoundedVec, CloneNoBound};
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;

mod v0 {
use super::*;

#[derive(CloneNoBound, Encode, Decode, TypeInfo, MaxEncodedLen)]
#[scale_info(skip_type_params(MaxValidators))]
pub struct LegacyCommitteeInfo<
ScEpochNumber: Clone,
AuthorityId: Clone,
AuthorityKeys: Clone,
MaxValidators,
> {
pub epoch: ScEpochNumber,
pub committee: BoundedVec<(AuthorityId, AuthorityKeys), MaxValidators>,
}

impl<ScEpochNumber, AuthorityId, AuthorityKeys, MaxValidators> Default
for LegacyCommitteeInfo<ScEpochNumber, AuthorityId, AuthorityKeys, MaxValidators>
where
AuthorityId: Clone,
AuthorityKeys: Clone,
ScEpochNumber: Clone + Zero,
{
fn default() -> Self {
Self { epoch: ScEpochNumber::zero(), committee: BoundedVec::new() }
}
}
#[cfg(feature = "try-runtime")]
use {
alloc::vec::Vec, parity_scale_codec::Encode, sp_session_validator_management::CommitteeMember,
};

#[storage_alias]
pub type CurrentCommittee<T: crate::pallet::Config> = StorageValue<
crate::Pallet<T>,
LegacyCommitteeInfo<
<T as crate::pallet::Config>::ScEpochNumber,
<T as crate::pallet::Config>::AuthorityId,
<T as crate::pallet::Config>::AuthorityKeys,
<T as crate::pallet::Config>::MaxValidators,
>,
ValueQuery,
>;

#[storage_alias]
pub type NextCommittee<T: crate::pallet::Config> = StorageValue<
crate::Pallet<T>,
LegacyCommitteeInfo<
<T as crate::pallet::Config>::ScEpochNumber,
<T as crate::pallet::Config>::AuthorityId,
<T as crate::pallet::Config>::AuthorityKeys,
<T as crate::pallet::Config>::MaxValidators,
>,
OptionQuery,
>;
}
use super::v0;

pub struct InnerMigrateV0ToV1<T: crate::Config>(core::marker::PhantomData<T>);

Expand Down Expand Up @@ -177,7 +125,7 @@ where
}
}

pub type Migration<T> = frame_support::migrations::VersionedMigration<
pub type LegacyToV1Migration<T> = frame_support::migrations::VersionedMigration<
0, // The migration will only execute when the on-chain storage version is 0
1, // The on-chain storage version will be set to 1 after the migration is complete
InnerMigrateV0ToV1<T>,
Expand Down

0 comments on commit e90921b

Please sign in to comment.