diff --git a/Cargo.lock b/Cargo.lock index b3b9b126..de910e60 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3725,6 +3725,7 @@ dependencies = [ "pallet-proxy", "pallet-scheduler", "pallet-sidechain", + "pallet-sudo", "pallet-teeracle", "pallet-teerex", "pallet-timestamp", diff --git a/polkadot-parachains/integritee-runtime/Cargo.toml b/polkadot-parachains/integritee-runtime/Cargo.toml index 03779c00..1f74059c 100644 --- a/polkadot-parachains/integritee-runtime/Cargo.toml +++ b/polkadot-parachains/integritee-runtime/Cargo.toml @@ -46,6 +46,7 @@ pallet-multisig = { default-features = false, git = "https://github.com/parityte pallet-preimage = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" } pallet-proxy = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" } pallet-scheduler = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" } +pallet-sudo = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" } pallet-timestamp = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v1.0.0" } pallet-transaction-payment = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v1.0.0" } pallet-transaction-payment-rpc-runtime-api = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v1.0.0" } @@ -139,6 +140,7 @@ std = [ "pallet-proxy/std", "pallet-scheduler/std", "pallet-sidechain/std", + "pallet-sudo/std", "pallet-teeracle/std", "pallet-teerex/std", "pallet-timestamp/std", @@ -194,6 +196,7 @@ runtime-benchmarks = [ "pallet-preimage/runtime-benchmarks", "pallet-scheduler/runtime-benchmarks", "pallet-sidechain/runtime-benchmarks", + "pallet-sudo/runtime-benchmarks", "pallet-teeracle/runtime-benchmarks", "pallet-teerex/runtime-benchmarks", "pallet-timestamp/runtime-benchmarks", @@ -228,6 +231,7 @@ try-runtime = [ "pallet-preimage/try-runtime", "pallet-scheduler/try-runtime", "pallet-sidechain/try-runtime", + "pallet-sudo/try-runtime", "pallet-teeracle/try-runtime", "pallet-teerex/try-runtime", "pallet-timestamp/try-runtime", diff --git a/polkadot-parachains/integritee-runtime/src/lib.rs b/polkadot-parachains/integritee-runtime/src/lib.rs index 5bf25aad..2048f3e6 100644 --- a/polkadot-parachains/integritee-runtime/src/lib.rs +++ b/polkadot-parachains/integritee-runtime/src/lib.rs @@ -28,6 +28,7 @@ use cumulus_pallet_parachain_system::RelayNumberStrictlyIncreases; use frame_support::traits::{ ConstBool, EqualPrivilegeOnly, Imbalance, InstanceFilter, OnUnbalanced, }; +mod migrations_fix; pub use opaque::*; use pallet_collective; use pallet_transaction_payment::{FeeDetails, RuntimeDispatchInfo}; @@ -740,6 +741,12 @@ impl orml_xcm::Config for Runtime { type SovereignOrigin = EnsureRoot; } +impl pallet_sudo::Config for Runtime { + type RuntimeCall = RuntimeCall; + type RuntimeEvent = RuntimeEvent; + type WeightInfo = (); +} + construct_runtime!( pub enum Runtime { @@ -768,6 +775,8 @@ construct_runtime!( Council: pallet_collective::::{Pallet, Call, Storage, Origin, Event, Config} = 15, TechnicalCommittee: pallet_collective::::{Pallet, Call, Storage, Event, Origin, Config} = 16, + // was at index 5 before, but storage prefix goes by name only + Sudo: pallet_sudo::{Pallet, Call, Storage, Config, Event} = 17, Bounties: pallet_bounties::{Pallet, Call, Storage, Event} = 18, ChildBounties: pallet_child_bounties = 19, @@ -819,7 +828,22 @@ pub type UncheckedExtrinsic = pub type CheckedExtrinsic = generic::CheckedExtrinsic; /// Migrations to apply on runtime upgrade. -pub type Migrations = (); +pub type Migrations = ( + migrations_fix::scheduler::v4::MigrateToV4, + migrations_fix::xcm::v1::MigrateToV1, + pallet_balances::migration::MigrateToTrackInactive, + migrations_fix::preimage::v1::MigrateToV1, + migrations_fix::bounties::v4::MigrateToV4, + pallet_multisig::migrations::v1::MigrateToV1, + migrations_fix::collective::v4::MigrateToV4, + migrations_fix::collective::v4::MigrateToV4, + pallet_teerex::migrations::v1::MigrateV0toV1, + pallet_teerex::migrations::v2::MigrateV1toV2, + migrations_fix::dmp_queue::v2::MigrateToV2, + migrations_fix::xcmp_queue::v3::MigrateToV3, + migrations_fix::democracy::v1::MigrateToV1, + cumulus_pallet_parachain_system::migration::Migration, +); /// Executive: handles dispatch to the various modules. pub type Executive = frame_executive::Executive< diff --git a/polkadot-parachains/integritee-runtime/src/migrations_fix.rs b/polkadot-parachains/integritee-runtime/src/migrations_fix.rs new file mode 100644 index 00000000..0b6750cd --- /dev/null +++ b/polkadot-parachains/integritee-runtime/src/migrations_fix.rs @@ -0,0 +1,518 @@ +// 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 . + +// 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 { + maybe_id: Option>, + priority: schedule::Priority, + call: Call, + maybe_periodic: Option>, + } + + #[frame_support::storage_alias] + pub(crate) type Agenda = StorageMap< + Pallet, + Twox64Concat, + BlockNumberFor, + Vec::RuntimeCall, BlockNumberFor>>>, + ValueQuery, + >; + + #[frame_support::storage_alias] + pub(crate) type Lookup = + StorageMap, Twox64Concat, Vec, TaskAddress>>; + } + + pub mod v3 { + use super::*; + use frame_support::pallet_prelude::*; + + #[frame_support::storage_alias] + pub(crate) type Agenda = StorageMap< + Pallet, + Twox64Concat, + BlockNumberFor, + Vec>>, + ValueQuery, + >; + + #[frame_support::storage_alias] + pub(crate) type Lookup = + StorageMap, Twox64Concat, Vec, TaskAddress>>; + } + + pub mod v4 { + use super::*; + use frame_support::pallet_prelude::*; + + #[frame_support::storage_alias] + pub type Agenda = StorageMap< + Pallet, + Twox64Concat, + BlockNumberFor, + BoundedVec< + Option>, + ::MaxScheduledPerBlock, + >, + ValueQuery, + >; + + #[allow(dead_code)] + type TaskName = [u8; 32]; + + #[frame_support::storage_alias] + pub(crate) type Lookup = + StorageMap, Twox64Concat, TaskName, TaskAddress>>; + + /// Migrate the scheduler pallet from V0 to V4 without changing storage. the only active schedule has been submitted already in V4 + pub struct MigrateToV4(sp_std::marker::PhantomData); + + impl OnRuntimeUpgrade for MigrateToV4 { + #[cfg(feature = "try-runtime")] + fn pre_upgrade() -> Result, sp_runtime::DispatchError> { + let agendas = v1::Agenda::::iter_keys().count() as u32; + let lookups = v1::Lookup::::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::::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::>(); + + T::DbWeight::get().reads_writes(1, 1) + } + + #[cfg(feature = "try-runtime")] + fn post_upgrade(_state: Vec) -> Result<(), sp_runtime::DispatchError> { + ensure!(StorageVersion::get::>() == 4, "Must upgrade"); + + let agendas = Agenda::::iter_keys().count() as u32; + let lookups = Lookup::::iter_keys().count() as u32; + log::info!(target: TARGET, "agendas present a posteriori: {}/{}...", agendas, lookups); + Ok(()) + } + } + } +} + +pub mod collective { + use frame_support::traits::OnRuntimeUpgrade; + use pallet_collective::*; + + /// The log target. + const TARGET: &'static str = "runtime::fix::collective::migration"; + + pub mod v4 { + use super::*; + use frame_support::{pallet_prelude::*, traits::Instance}; + use sp_std::vec::Vec; + + pub struct MigrateToV4, I: 'static>(PhantomData<(T, I)>); + impl, I: 'static> OnRuntimeUpgrade for MigrateToV4 { + #[cfg(feature = "try-runtime")] + fn pre_upgrade() -> Result, DispatchError> { + Ok((0u32).encode()) + } + + fn on_runtime_upgrade() -> Weight { + let onchain_version = Pallet::::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::>(); + + T::DbWeight::get().reads_writes(1, 1) + } + + #[cfg(feature = "try-runtime")] + fn post_upgrade(_state: Vec) -> Result<(), DispatchError> { + ensure!(StorageVersion::get::>() == 4, "Must upgrade"); + Ok(()) + } + } + } +} + +//PolkadotXcm pallet +pub mod xcm { + // 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_xcm::*; + + /// The log target. + const TARGET: &'static str = "runtime::fix::xcm::migration"; + + pub mod v1 { + use super::*; + use frame_support::pallet_prelude::*; + use sp_std::vec::Vec; + use xcm::{prelude::XcmVersion, v3::QueryId, VersionedMultiLocation}; + + #[frame_support::storage_alias] + pub(super) type VersionNotifyTargets = StorageDoubleMap< + Pallet, + Twox64Concat, + XcmVersion, + Blake2_128Concat, + VersionedMultiLocation, + (QueryId, Weight, XcmVersion), + OptionQuery, + >; + + pub struct MigrateToV1(sp_std::marker::PhantomData); + + impl OnRuntimeUpgrade for MigrateToV1 { + #[cfg(feature = "try-runtime")] + fn pre_upgrade() -> Result, sp_runtime::DispatchError> { + let targets = VersionNotifyTargets::::iter_prefix_values(3).count() as u32; + log::info!(target: TARGET, "found {} VersionNotifyTargets", targets); + Ok(targets.encode()) + } + + fn on_runtime_upgrade() -> Weight { + let onchain_version = Pallet::::on_chain_storage_version(); + if onchain_version > 0 { + log::warn!( + target: TARGET, + "skipping v0 to v1 migration: executed on wrong storage version.\ + Expected version 0, found {:?}", + onchain_version, + ); + return T::DbWeight::get().reads(1) + } + log::info!(target: TARGET, "migrating from {:?} to 1", onchain_version); + StorageVersion::new(1).put::>(); + + T::DbWeight::get().reads_writes(1, 1) + } + + #[cfg(feature = "try-runtime")] + fn post_upgrade(state: Vec) -> Result<(), sp_runtime::DispatchError> { + ensure!(StorageVersion::get::>() == 1, "Must upgrade"); + let old_targets: u32 = Decode::decode(&mut &state[..]) + .expect("pre_upgrade provides a valid state; qed"); + let targets = VersionNotifyTargets::::iter_prefix_values(3); + assert_eq!( + old_targets, + targets.count() as u32, + "must preserve all targets and be able to decode storage" + ); + Ok(()) + } + } + } +} + +/// the bounties pallet experienced a manual version fix which we didn't implement. this bruteforces v4 +/// https://github.com/paritytech/substrate/commit/9957da3cbb027f9b754c453a4d58a62665e532ef +pub mod bounties { + // 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_bounties::*; + + /// The log target. + const TARGET: &'static str = "runtime::fix::bounties::migration"; + + pub mod v4 { + use super::*; + use frame_support::pallet_prelude::*; + use sp_std::vec::Vec; + + pub struct MigrateToV4(sp_std::marker::PhantomData); + + impl OnRuntimeUpgrade for MigrateToV4 { + #[cfg(feature = "try-runtime")] + fn pre_upgrade() -> Result, sp_runtime::DispatchError> { + Ok(0u32.encode()) + } + + fn on_runtime_upgrade() -> Weight { + let onchain_version = Pallet::::on_chain_storage_version(); + if onchain_version >= 4 { + log::warn!( + target: TARGET, + "skipping v0 to v4 migration: executed on wrong storage version.\ + Expected version 0, found {:?}", + onchain_version, + ); + return T::DbWeight::get().reads(1) + } + log::info!(target: TARGET, "migrating from {:?} to 4", onchain_version); + StorageVersion::new(4).put::>(); + + T::DbWeight::get().reads_writes(1, 1) + } + + #[cfg(feature = "try-runtime")] + fn post_upgrade(_state: Vec) -> Result<(), sp_runtime::DispatchError> { + ensure!(StorageVersion::get::>() == 4, "Must upgrade"); + Ok(()) + } + } + } +} + +/// the bounties pallet experienced a manual version fix which we didn't implement. this bruteforces v4 +/// https://github.com/paritytech/substrate/commit/9957da3cbb027f9b754c453a4d58a62665e532ef +pub mod preimage { + // 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_preimage::*; + + /// The log target. + const TARGET: &'static str = "runtime::fix::preimage::migration"; + + pub mod v1 { + use super::*; + use frame_support::{pallet_prelude::*, traits::Currency}; + use sp_std::vec::Vec; + + const MAX_SIZE: u32 = 4 * 1024 * 1024; + type BalanceOf = + <::Currency as Currency<::AccountId>>::Balance; + + //these are actually the same types as in the current version of the pallet. + #[frame_support::storage_alias] + pub(super) type StatusFor = StorageMap< + Pallet, + Identity, + crate::Hash, + RequestStatus>, + >; + + #[frame_support::storage_alias] + pub(super) type PreimageFor = + StorageMap, Identity, (crate::Hash, u32), BoundedVec>>; + + pub struct MigrateToV1(sp_std::marker::PhantomData); + + impl OnRuntimeUpgrade for MigrateToV1 { + #[cfg(feature = "try-runtime")] + fn pre_upgrade() -> Result, sp_runtime::DispatchError> { + let images = PreimageFor::::iter_values().count() as u32; + let status = StatusFor::::iter_values().count() as u32; + log::info!(target: TARGET, "PreImageFor decoded: {}, StatusFor decoded {}", images, status); + assert_eq!(images, status); + Ok(0u32.encode()) + } + + fn on_runtime_upgrade() -> Weight { + let onchain_version = Pallet::::on_chain_storage_version(); + if onchain_version >= 1 { + log::warn!( + target: TARGET, + "skipping v0 to v1 migration: executed on wrong storage version.\ + Expected version 0, found {:?}", + onchain_version, + ); + return T::DbWeight::get().reads(1) + } + log::info!(target: TARGET, "migrating from {:?} to 1", onchain_version); + StorageVersion::new(1).put::>(); + + T::DbWeight::get().reads_writes(1, 1) + } + + #[cfg(feature = "try-runtime")] + fn post_upgrade(_state: Vec) -> Result<(), sp_runtime::DispatchError> { + ensure!(StorageVersion::get::>() == 1, "Must upgrade"); + Ok(()) + } + } + } +} + +pub mod democracy { + use frame_support::traits::OnRuntimeUpgrade; + use pallet_democracy::*; + + /// The log target. + const TARGET: &'static str = "runtime::fix::democracy::migration"; + + pub mod v1 { + use super::*; + use frame_support::pallet_prelude::*; + use sp_std::vec::Vec; + + pub struct MigrateToV1(sp_std::marker::PhantomData); + + impl OnRuntimeUpgrade for MigrateToV1 { + #[cfg(feature = "try-runtime")] + fn pre_upgrade() -> Result, sp_runtime::DispatchError> { + Ok(0u32.encode()) + } + + fn on_runtime_upgrade() -> Weight { + let onchain_version = Pallet::::on_chain_storage_version(); + if onchain_version >= 4 { + log::warn!( + target: TARGET, + "skipping v0 to v1 migration: executed on wrong storage version.\ + Expected version 0, found {:?}", + onchain_version, + ); + return T::DbWeight::get().reads(1) + } + log::info!(target: TARGET, "migrating from {:?} to 1", onchain_version); + StorageVersion::new(1).put::>(); + + T::DbWeight::get().reads_writes(1, 1) + } + + #[cfg(feature = "try-runtime")] + fn post_upgrade(_state: Vec) -> Result<(), sp_runtime::DispatchError> { + ensure!(StorageVersion::get::>() == 1, "Must upgrade"); + Ok(()) + } + } + } +} + +pub mod dmp_queue { + use cumulus_pallet_dmp_queue::*; + use frame_support::traits::OnRuntimeUpgrade; + + /// The log target. + const TARGET: &'static str = "runtime::fix::dmp_queue::migration"; + + pub mod v2 { + use super::*; + use frame_support::pallet_prelude::*; + use sp_std::vec::Vec; + + pub struct MigrateToV2(sp_std::marker::PhantomData); + + impl OnRuntimeUpgrade for MigrateToV2 { + #[cfg(feature = "try-runtime")] + fn pre_upgrade() -> Result, sp_runtime::DispatchError> { + Ok(0u32.encode()) + } + + fn on_runtime_upgrade() -> Weight { + let onchain_version = Pallet::::on_chain_storage_version(); + if onchain_version >= 2 { + log::warn!( + target: TARGET, + "skipping v0 to v2 migration: executed on wrong storage version.\ + Expected version 0, found {:?}", + onchain_version, + ); + return T::DbWeight::get().reads(1) + } + log::info!(target: TARGET, "migrating from {:?} to 2", onchain_version); + StorageVersion::new(2).put::>(); + + T::DbWeight::get().reads_writes(1, 1) + } + + #[cfg(feature = "try-runtime")] + fn post_upgrade(_state: Vec) -> Result<(), sp_runtime::DispatchError> { + ensure!(StorageVersion::get::>() == 2, "Must upgrade"); + Ok(()) + } + } + } +} + +pub mod xcmp_queue { + use cumulus_pallet_xcmp_queue::*; + use frame_support::traits::OnRuntimeUpgrade; + + /// The log target. + const TARGET: &'static str = "runtime::fix::xcmp_queue::migration"; + + pub mod v3 { + use super::*; + use frame_support::pallet_prelude::*; + use sp_std::vec::Vec; + + pub struct MigrateToV3(sp_std::marker::PhantomData); + + impl OnRuntimeUpgrade for MigrateToV3 { + #[cfg(feature = "try-runtime")] + fn pre_upgrade() -> Result, sp_runtime::DispatchError> { + Ok(0u32.encode()) + } + + fn on_runtime_upgrade() -> Weight { + let onchain_version = Pallet::::on_chain_storage_version(); + if onchain_version >= 3 { + log::warn!( + target: TARGET, + "skipping v0 to v3 migration: executed on wrong storage version.\ + Expected version 0, found {:?}", + onchain_version, + ); + return T::DbWeight::get().reads(1) + } + log::info!(target: TARGET, "migrating from {:?} to 3", onchain_version); + StorageVersion::new(3).put::>(); + + T::DbWeight::get().reads_writes(1, 1) + } + + #[cfg(feature = "try-runtime")] + fn post_upgrade(_state: Vec) -> Result<(), sp_runtime::DispatchError> { + ensure!(StorageVersion::get::>() == 3, "Must upgrade"); + Ok(()) + } + } + } +} diff --git a/polkadot-parachains/src/chain_spec.rs b/polkadot-parachains/src/chain_spec.rs index ba1cc105..15625efb 100644 --- a/polkadot-parachains/src/chain_spec.rs +++ b/polkadot-parachains/src/chain_spec.rs @@ -225,6 +225,7 @@ fn integritee_genesis_config( .to_vec(), _config: Default::default(), }, + sudo: parachain_runtime::SudoConfig { key: Some(root_key.clone()) }, balances: parachain_runtime::BalancesConfig { balances: endowed_accounts.iter().cloned().map(|k| (k, 1_000 * TEER)).collect(), },