Skip to content

Commit

Permalink
complete migration setup. trouble with collective instances
Browse files Browse the repository at this point in the history
  • Loading branch information
brenzi committed Jan 23, 2024
1 parent 3ec4e12 commit 7f18dea
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 16 deletions.
13 changes: 11 additions & 2 deletions polkadot-parachains/integritee-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));

use codec::{Decode, Encode, MaxEncodedLen};
use cumulus_pallet_parachain_system::RelayNumberStrictlyIncreases;
use frame_support::traits::{
ConstBool, EqualPrivilegeOnly, Imbalance, InstanceFilter, OnUnbalanced,
use frame_support::{
instances::{Instance1, Instance2},
traits::{ConstBool, EqualPrivilegeOnly, Imbalance, InstanceFilter, OnUnbalanced},
};
pub use opaque::*;
use pallet_collective;
Expand Down Expand Up @@ -825,6 +826,14 @@ pub type CheckedExtrinsic = generic::CheckedExtrinsic<AccountId, RuntimeCall, Si
pub type Migrations = (
migrations_fix::preimage::v1::MigrateToV1<Runtime>,
migrations_fix::bounties::v4::MigrateToV4<Runtime>,
// Multisig
pallet_multisig::migrations::v1::MigrateToV1<Runtime>,
// Collective
// migration changes the pallet name prefix (back in 2021). no need to touch this. I guess this has been left untouched when we migrated solo to para
// for consistency, we will bruteforce to V4
// future: v1.6.0 is still at V4.
migrations_fix::collective::v4::MigrateToV4<Runtime, Instance1>,
migrations_fix::collective::v4::MigrateToV4<Runtime, Instance2>,
);

/// Executive: handles dispatch to the various modules.
Expand Down
41 changes: 27 additions & 14 deletions polkadot-parachains/integritee-runtime/src/migrations_fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,6 @@ pub mod scheduler {
}

pub mod collective {
// this is necessary because migrations from v0 to v3 are no longer available in the scheduler
// pallet code and migrating is only possible from v3. The strategy here is to empty the agenda
// (has been empty since genesis)
use frame_support::traits::OnRuntimeUpgrade;
use pallet_collective::*;

Expand All @@ -151,20 +148,18 @@ pub mod collective {

pub mod v4 {
use super::*;
use frame_support::pallet_prelude::*;
use frame_support::{pallet_prelude::*, traits::Instance};
use sp_std::vec::Vec;

/// Migrate the scheduler pallet from V0 to V4 without changing storage. the only active schedule has been submitted already in V4
pub struct MigrateToV4<T>(sp_std::marker::PhantomData<T>);

impl<T: Config> OnRuntimeUpgrade for MigrateToV4<T> {
pub struct MigrateToV4<T: Config<I>, I: 'static>(sp_std::marker::PhantomData<(T, I)>);
impl<T: Config<I>, I: Instance + 'static> OnRuntimeUpgrade for MigrateToV4<T, I> {
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, sp_runtime::DispatchError> {
Ok((0u32).encode())
}

fn on_runtime_upgrade() -> Weight {
let onchain_version = Pallet::<T>::on_chain_storage_version();
let onchain_version = Pallet::<T, I>::on_chain_storage_version();
if onchain_version >= 3 {
log::warn!(
target: TARGET,
Expand All @@ -175,14 +170,14 @@ pub mod collective {
return T::DbWeight::get().reads(1)
}
log::info!(target: TARGET, "migrating from {:?} to 4", onchain_version);
StorageVersion::new(4).put::<Pallet<T>>();
StorageVersion::new(4).put::<Pallet<T, I>>();

T::DbWeight::get().reads_writes(1, 1)
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(_state: Vec<u8>) -> Result<(), sp_runtime::DispatchError> {
ensure!(StorageVersion::get::<Pallet<T>>() == 4, "Must upgrade");
ensure!(StorageVersion::get::<Pallet<T, I>>() == 4, "Must upgrade");
Ok(())
}
}
Expand Down Expand Up @@ -304,7 +299,7 @@ pub mod bounties {
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(state: Vec<u8>) -> Result<(), sp_runtime::DispatchError> {
fn post_upgrade(_state: Vec<u8>) -> Result<(), sp_runtime::DispatchError> {
ensure!(StorageVersion::get::<Pallet<T>>() == 4, "Must upgrade");
Ok(())
}
Expand All @@ -326,16 +321,34 @@ pub mod preimage {

pub mod v1 {
use super::*;
use frame_support::pallet_prelude::*;
use frame_support::{pallet_prelude::*, traits::Currency};
use sp_std::vec::Vec;

const MAX_SIZE: u32 = 4 * 1024 * 1024;
type BalanceOf<T> =
<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;

//these are actually the same types as in the current version of the pallet.
#[frame_support::storage_alias]
pub(super) type StatusFor<T: Config> = StorageMap<
Pallet<T>,
Identity,
crate::Hash,
RequestStatus<crate::AccountId, BalanceOf<T>>,
>;

#[frame_support::storage_alias]
pub(super) type PreimageFor<T: Config> =
StorageMap<Pallet<T>, Identity, (crate::Hash, u32), BoundedVec<u8, ConstU32<MAX_SIZE>>>;

pub struct MigrateToV1<T>(sp_std::marker::PhantomData<T>);

impl<T: Config> OnRuntimeUpgrade for MigrateToV1<T> {
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, sp_runtime::DispatchError> {
let images = PreimageFor::<T>::iter_values().count() as u32;
let status = StatusFor::<T>::iter_values().count() as u32;
log::info!(target: TARGET, "PreImageFor decoded: {}, StatusFor decoded {}", images, status);
assert_eq!(images, status);
Ok(0u32.encode())
}
Expand All @@ -358,7 +371,7 @@ pub mod preimage {
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(state: Vec<u8>) -> Result<(), sp_runtime::DispatchError> {
fn post_upgrade(_state: Vec<u8>) -> Result<(), sp_runtime::DispatchError> {
ensure!(StorageVersion::get::<Pallet<T>>() == 1, "Must upgrade");
Ok(())
}
Expand Down

0 comments on commit 7f18dea

Please sign in to comment.