Skip to content

Commit

Permalink
add migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
brenzi committed Jan 17, 2024
1 parent 78ee997 commit 42ddb41
Show file tree
Hide file tree
Showing 5 changed files with 241 additions and 11 deletions.
48 changes: 44 additions & 4 deletions polkadot-parachains/integritee-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ pub use pallet_teerex::Call as TeerexCall;
mod helpers;
mod weights;

// todo: temporary. remove after fixing
mod migrations_fix;

pub mod xcm_config;

pub type SessionHandlers = ();
Expand Down Expand Up @@ -802,17 +805,54 @@ pub type UncheckedExtrinsic =
/// Extrinsic type that has already been checked.
pub type CheckedExtrinsic = generic::CheckedExtrinsic<AccountId, RuntimeCall, SignedExtra>;

/// Migrations to apply on runtime upgrade.
pub type Migrations = (
// Scheduler
// fixing the scheduler with a local migration is necessary because we have missed intermediate
// migrations. mainnet at V0, jumping to V4 here
// future: v1.6.0 is still at V4.
migrations_fix::scheduler::v4::MigrateToV4<Runtime>,
// XcmpQueue
// code says it's V2, but we have V3 onchain. how come?
// v1.0.0: V3 (can migrate V1/V2 to V3) from here onwards we should be consistent
// v1.4.0: V4
//cumulus_pallet_xcmp_queue::migration::migrate_to_v3<Runtime>,

// DmpQueue
// code says it's V1 but we have V2 onchain. how come?
// at spec_version 29 it was at 1. (release https://github.com/integritee-network/parachain/releases/tag/1.5.33) (polkadot-v0.9.36)
// next spec_version was v35 where it went to 2
// v35 is https://github.com/integritee-network/parachain/releases/tag/1.5.40 (polkadot-v0.9.42)
// v1.0.0: V2 (can migrate V0 and V1 to V2) from here onwards we should be consistent
// v1.6.0 is still V2

// PolkadotXcm
// mainnet is at V0
pallet_xcm::migration::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.
// as we have no issues with collectives, we won't change a running system !
// migrations_fix::collective::v4::MigrateToV4<Runtime, Instance1>,
//
// Democracy
pallet_democracy::migrations::v1::Migration<Runtime>,
// Multisig
pallet_multisig::migrations::v1::MigrateToV1<Runtime>,
// Balances: mainnet at V0. this here brings us to V1
// future: v1.6.0 is still at V1
pallet_balances::migration::MigrateToTrackInactive<Runtime, xcm_config::CheckingAccount>,
);

/// Executive: handles dispatch to the various modules.
pub type Executive = frame_executive::Executive<
Runtime,
Block,
frame_system::ChainContext<Runtime>,
Runtime,
AllPalletsWithSystem, // Solochain: AllPalletsReversedWithSystemFirst, Statemint: AllPallets. Which one to take?
(
pallet_teerex::migrations::v1::MigrateV0toV1<Runtime>,
pallet_teerex::migrations::v2::MigrateV1toV2<Runtime>,
),
Migrations,
>;

#[cfg(feature = "runtime-benchmarks")]
Expand Down
189 changes: 189 additions & 0 deletions polkadot-parachains/integritee-runtime/src/migrations_fix.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
// Copyright (c) 2023 Encointer Association
// This file is part of Encointer
//
// Encointer is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Encointer is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Encointer. If not, see <http://www.gnu.org/licenses/>.

// the following are temporary local migration fixes to solve inconsistencies caused by not
// migrating Storage at the time of migrating runtime code

pub mod scheduler {
// 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 frame_system::pallet_prelude::BlockNumberFor;
use pallet_scheduler::*;
use sp_std::vec::Vec;

/// The log target.
const TARGET: &'static str = "runtime::fix::scheduler::migration";

pub mod v1 {
use super::*;
use frame_support::{pallet_prelude::*, traits::schedule};

#[cfg_attr(any(feature = "std", test), derive(PartialEq, Eq))]
#[derive(Clone, RuntimeDebug, Encode, Decode)]
pub(crate) struct ScheduledV1<Call, BlockNumber> {
maybe_id: Option<Vec<u8>>,
priority: schedule::Priority,
call: Call,
maybe_periodic: Option<schedule::Period<BlockNumber>>,
}

#[frame_support::storage_alias]
pub(crate) type Agenda<T: Config> = StorageMap<
Pallet<T>,
Twox64Concat,
BlockNumberFor<T>,
Vec<Option<ScheduledV1<<T as Config>::RuntimeCall, BlockNumberFor<T>>>>,
ValueQuery,
>;

#[frame_support::storage_alias]
pub(crate) type Lookup<T: Config> =
StorageMap<Pallet<T>, Twox64Concat, Vec<u8>, TaskAddress<BlockNumberFor<T>>>;
}

pub mod v3 {
use super::*;
use frame_support::pallet_prelude::*;

#[frame_support::storage_alias]
pub(crate) type Agenda<T: Config> = StorageMap<
Pallet<T>,
Twox64Concat,
BlockNumberFor<T>,
Vec<Option<ScheduledV3Of<T>>>,
ValueQuery,
>;

#[frame_support::storage_alias]
pub(crate) type Lookup<T: Config> =
StorageMap<Pallet<T>, Twox64Concat, Vec<u8>, TaskAddress<BlockNumberFor<T>>>;
}

pub mod v4 {
use super::*;
use frame_support::pallet_prelude::*;

#[frame_support::storage_alias]
pub type Agenda<T: Config> = StorageMap<
Pallet<T>,
Twox64Concat,
BlockNumberFor<T>,
BoundedVec<
Option<ScheduledOf<T>>,
<T as pallet_scheduler::Config>::MaxScheduledPerBlock,
>,
ValueQuery,
>;

pub(crate) type TaskName = [u8; 32];

#[frame_support::storage_alias]
pub(crate) type Lookup<T: Config> =
StorageMap<Pallet<T>, Twox64Concat, TaskName, TaskAddress<BlockNumberFor<T>>>;

/// 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> {
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, &'static str> {
let agendas = v1::Agenda::<T>::iter_keys().count() as u32;
let lookups = v1::Lookup::<T>::iter_keys().count() as u32;
log::info!(target: TARGET, "agendas present which will be left untouched: {}/{}...", agendas, lookups);
Ok((agendas, lookups).encode())
}

fn on_runtime_upgrade() -> Weight {
let onchain_version = Pallet::<T>::on_chain_storage_version();
if onchain_version >= 3 {
log::warn!(
target: TARGET,
"skipping v0 to v4 migration: executed on wrong storage version.\
Expected version < 3, found {:?}",
onchain_version,
);
return T::DbWeight::get().reads(1)
}
log::info!(target: TARGET, "migrating from {:?} to 4", onchain_version);
StorageVersion::new(4).put::<Pallet<T>>();

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

#[cfg(feature = "try-runtime")]
fn post_upgrade(_state: Vec<u8>) -> Result<(), &'static str> {
ensure!(StorageVersion::get::<Pallet<T>>() == 4, "Must upgrade");

let agendas = Agenda::<T>::iter_keys().count() as u32;
let lookups = Lookup::<T>::iter_keys().count() as u32;
log::info!(target: TARGET, "agendas present a posteriori: {}/{}...", agendas, lookups);
Ok(())
}
}
}
}

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::*;
use sp_std::vec::Vec;

/// The log target.
const TARGET: &'static str = "runtime::fix::collective::migration";

pub mod v4 {
use super::*;
use frame_support::pallet_prelude::*;

/// 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> {
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, &'static str> {
Ok((0u32).encode())
}

fn on_runtime_upgrade() -> Weight {
let onchain_version = Pallet::<T>::on_chain_storage_version();
if onchain_version >= 3 {
log::warn!(
target: TARGET,
"skipping v0 to v4 migration: executed on wrong storage version.\
Expected version < 3, found {:?}",
onchain_version,
);
return T::DbWeight::get().reads(1)
}
log::info!(target: TARGET, "migrating from {:?} to 4", onchain_version);
StorageVersion::new(4).put::<Pallet<T>>();

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

#[cfg(feature = "try-runtime")]
fn post_upgrade(_state: Vec<u8>) -> Result<(), &'static str> {
ensure!(StorageVersion::get::<Pallet<T>>() == 4, "Must upgrade");
Ok(())
}
}
}
}
1 change: 1 addition & 0 deletions polkadot-parachains/integritee-runtime/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ parameter_types! {
parents:0,
interior: Junctions::X1(TEER_GENERAL_KEY)
};
pub CheckingAccount: AccountId = PolkadotXcm::check_account();
}

// Supported Currencies.
Expand Down
4 changes: 2 additions & 2 deletions zombienet/rococo-local-with-integritee.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ chain = "integritee-rococo-local-dev"

[[parachains.collators]]
name = "integritee-collator01"
command = "./target/release/integritee-collator"
command = "~/bin/integritee-collator-v1.6.4"
ws_port = 9944

[[parachains.collators]]
name = "integritee-collator02"
command = "./target/release/integritee-collator"
command = "~/bin/integritee-collator-v1.6.4"
ws_port = 9945

10 changes: 5 additions & 5 deletions zombienet/rococo-local-with-privacy-sidechain-demo-setup.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ timeout = 10000

[relaychain]
default_command = "~/bin/polkadot-v0.9.42"
default_args = [ "-lparachain=debug" ]
default_args = [ "-lparachain=debug --state-pruning=archive --blocks-pruning=archive" ]
chain = "rococo-local"

[[relaychain.nodes]]
Expand All @@ -29,12 +29,12 @@ chain = "integritee-rococo-local-dev"

[[parachains.collators]]
name = "integritee-collator-1"
command = "./target/release/integritee-collator"
command = "~/bin/integritee-collator-v1.6.4"
ws_port = 9944

[[parachains.collators]]
name = "integritee-collator-2"
command = "./target/release/integritee-collator"
command = "~/bin/integritee-collator-v1.6.4"
ws_port = 9945

[[parachains]]
Expand All @@ -46,13 +46,13 @@ chain = "integritee-rococo-local-dev"
[[parachains.collators]]
name = "asset-hub-collator-1"
validator = true
command = "./target/release/integritee-collator"
command = "~/bin/integritee-collator-v1.6.4"
ws_port = 9954

[[parachains.collators]]
name = "asset-hub-collator-2"
validator = true
command = "./target/release/integritee-collator"
command = "~/bin/integritee-collator-v1.6.4"
ws_port = 9955

[[hrmp_channels]]
Expand Down

0 comments on commit 42ddb41

Please sign in to comment.