Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Contracts: Use RuntimeUpgrade hooks instead of Hooks::on_runtime_upgr…
Browse files Browse the repository at this point in the history
…ade (#2570)

* Fixes

* Remove on_runtime_upgrade hook

* remove upgrade_fn / add doc to Migration struct

* Add cumulus_pallet_*::migration to Migrations type

* add docstring

* fix

* Update parachain-template/runtime/src/lib.rs

Co-authored-by: Bastian Köcher <[email protected]>

* Update parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/lib.rs

* Update pallets/parachain-system/src/migration.rs

---------

Co-authored-by: Bastian Köcher <[email protected]>
  • Loading branch information
pgherveou and bkchr authored Jun 5, 2023
1 parent 58454da commit 1208a9d
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 64 deletions.
4 changes: 0 additions & 4 deletions pallets/dmp-queue/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,6 @@ pub mod pallet {

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_runtime_upgrade() -> Weight {
migration::migrate_to_latest::<T>()
}

fn on_idle(_now: T::BlockNumber, max_weight: Weight) -> Weight {
// on_idle processes additional messages with any remaining block weight.
Self::service_queue(max_weight)
Expand Down
37 changes: 20 additions & 17 deletions pallets/dmp-queue/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,34 @@
use crate::{Config, Configuration, Overweight, Pallet, DEFAULT_POV_SIZE};
use frame_support::{
pallet_prelude::*,
traits::StorageVersion,
traits::{OnRuntimeUpgrade, StorageVersion},
weights::{constants::WEIGHT_REF_TIME_PER_MILLIS, Weight},
};

/// The current storage version.
pub const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);
pub const STORAGE_VERSION: StorageVersion = StorageVersion::new(2);

/// Migrates the pallet storage to the most recent version, checking and setting the
/// `StorageVersion`.
pub fn migrate_to_latest<T: Config>() -> Weight {
let mut weight = T::DbWeight::get().reads(1);
/// Migrates the pallet storage to the most recent version.
pub struct Migration<T: Config>(PhantomData<T>);

if StorageVersion::get::<Pallet<T>>() == 0 {
weight.saturating_accrue(migrate_to_v1::<T>());
StorageVersion::new(1).put::<Pallet<T>>();
weight.saturating_accrue(T::DbWeight::get().writes(1));
}
impl<T: Config> OnRuntimeUpgrade for Migration<T> {
fn on_runtime_upgrade() -> Weight {
let mut weight = T::DbWeight::get().reads(1);

if StorageVersion::get::<Pallet<T>>() == 1 {
weight.saturating_accrue(migrate_to_v2::<T>());
StorageVersion::new(2).put::<Pallet<T>>();
weight.saturating_accrue(T::DbWeight::get().writes(1));
}
if StorageVersion::get::<Pallet<T>>() == 0 {
weight.saturating_accrue(migrate_to_v1::<T>());
StorageVersion::new(1).put::<Pallet<T>>();
weight.saturating_accrue(T::DbWeight::get().writes(1));
}

weight
if StorageVersion::get::<Pallet<T>>() == 1 {
weight.saturating_accrue(migrate_to_v2::<T>());
StorageVersion::new(2).put::<Pallet<T>>();
weight.saturating_accrue(T::DbWeight::get().writes(1));
}

weight
}
}

mod v0 {
Expand Down
6 changes: 1 addition & 5 deletions pallets/parachain-system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ use sp_runtime::{
use sp_std::{cmp, collections::btree_map::BTreeMap, prelude::*};
use xcm::latest::XcmHash;

mod migration;
pub mod migration;
mod relay_state_snapshot;
#[macro_use]
pub mod validate_block;
Expand Down Expand Up @@ -197,10 +197,6 @@ pub mod pallet {

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_runtime_upgrade() -> Weight {
migration::on_runtime_upgrade::<T>()
}

fn on_finalize(_: T::BlockNumber) {
<DidSetValidationCode<T>>::kill();
<UpgradeRestrictionSignal<T>>::kill();
Expand Down
39 changes: 22 additions & 17 deletions pallets/parachain-system/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,37 @@

use crate::{Config, Pallet, ReservedDmpWeightOverride, ReservedXcmpWeightOverride};
use frame_support::{
traits::{Get, StorageVersion},
pallet_prelude::*,
traits::{Get, OnRuntimeUpgrade, StorageVersion},
weights::Weight,
};

/// The current storage version.
pub const STORAGE_VERSION: StorageVersion = StorageVersion::new(2);

/// Call this during the next runtime upgrade for this module.
pub fn on_runtime_upgrade<T: Config>() -> Weight {
let mut weight: Weight = T::DbWeight::get().reads(2);
/// Migrates the pallet storage to the most recent version.
pub struct Migration<T: Config>(PhantomData<T>);

if StorageVersion::get::<Pallet<T>>() == 0 {
weight = weight
.saturating_add(v1::migrate::<T>())
.saturating_add(T::DbWeight::get().writes(1));
StorageVersion::new(1).put::<Pallet<T>>();
}
impl<T: Config> OnRuntimeUpgrade for Migration<T> {
fn on_runtime_upgrade() -> Weight {
let mut weight: Weight = T::DbWeight::get().reads(2);

if StorageVersion::get::<Pallet<T>>() == 1 {
weight = weight
.saturating_add(v2::migrate::<T>())
.saturating_add(T::DbWeight::get().writes(1));
STORAGE_VERSION.put::<Pallet<T>>();
}
if StorageVersion::get::<Pallet<T>>() == 0 {
weight = weight
.saturating_add(v1::migrate::<T>())
.saturating_add(T::DbWeight::get().writes(1));
StorageVersion::new(1).put::<Pallet<T>>();
}

weight
if StorageVersion::get::<Pallet<T>>() == 1 {
weight = weight
.saturating_add(v2::migrate::<T>())
.saturating_add(T::DbWeight::get().writes(1));
StorageVersion::new(2).put::<Pallet<T>>();
}

weight
}
}

/// V2: Migrate to 2D weights for ReservedXcmpWeightOverride and ReservedDmpWeightOverride.
Expand Down
4 changes: 0 additions & 4 deletions pallets/xcmp-queue/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,6 @@ pub mod pallet {

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_runtime_upgrade() -> Weight {
migration::migrate_to_latest::<T>()
}

fn on_idle(_now: T::BlockNumber, max_weight: Weight) -> Weight {
// on_idle processes additional messages with any remaining block weight.
Self::service_xcmp_queue(max_weight)
Expand Down
37 changes: 20 additions & 17 deletions pallets/xcmp-queue/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,34 @@
use crate::{Config, Overweight, Pallet, QueueConfig, DEFAULT_POV_SIZE};
use frame_support::{
pallet_prelude::*,
traits::StorageVersion,
traits::{OnRuntimeUpgrade, StorageVersion},
weights::{constants::WEIGHT_REF_TIME_PER_MILLIS, Weight},
};

/// The current storage version.
pub const STORAGE_VERSION: StorageVersion = StorageVersion::new(2);
pub const STORAGE_VERSION: StorageVersion = StorageVersion::new(3);

/// Migrates the pallet storage to the most recent version, checking and setting the
/// `StorageVersion`.
pub fn migrate_to_latest<T: Config>() -> Weight {
let mut weight = T::DbWeight::get().reads(1);
/// Migrates the pallet storage to the most recent version.
pub struct Migration<T: Config>(PhantomData<T>);

if StorageVersion::get::<Pallet<T>>() == 1 {
weight.saturating_accrue(migrate_to_v2::<T>());
StorageVersion::new(2).put::<Pallet<T>>();
weight.saturating_accrue(T::DbWeight::get().writes(1));
}
impl<T: Config> OnRuntimeUpgrade for Migration<T> {
fn on_runtime_upgrade() -> Weight {
let mut weight = T::DbWeight::get().reads(1);

if StorageVersion::get::<Pallet<T>>() == 2 {
weight.saturating_accrue(migrate_to_v3::<T>());
StorageVersion::new(3).put::<Pallet<T>>();
weight.saturating_accrue(T::DbWeight::get().writes(1));
}
if StorageVersion::get::<Pallet<T>>() == 1 {
weight.saturating_accrue(migrate_to_v2::<T>());
StorageVersion::new(2).put::<Pallet<T>>();
weight.saturating_accrue(T::DbWeight::get().writes(1));
}

weight
if StorageVersion::get::<Pallet<T>>() == 2 {
weight.saturating_accrue(migrate_to_v3::<T>());
StorageVersion::new(3).put::<Pallet<T>>();
weight.saturating_accrue(T::DbWeight::get().writes(1));
}

weight
}
}

mod v1 {
Expand Down
1 change: 1 addition & 0 deletions parachains/runtimes/contracts/contracts-rococo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ std = [
"frame-support/std",
"frame-system-rpc-runtime-api/std",
"frame-system/std",
"frame-try-runtime/std",
"kusama-runtime-constants/std",
"pallet-aura/std",
"pallet-authorship/std",
Expand Down
7 changes: 7 additions & 0 deletions parachains/runtimes/contracts/contracts-rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,16 @@ pub type SignedExtra = (
pub type UncheckedExtrinsic =
generic::UncheckedExtrinsic<Address, RuntimeCall, Signature, SignedExtra>;

/// Migrations to apply on runtime upgrade.
pub type Migrations = (
cumulus_pallet_dmp_queue::migration::Migration<Runtime>,
cumulus_pallet_parachain_system::migration::Migration<Runtime>,
cumulus_pallet_xcmp_queue::migration::Migration<Runtime>,
pallet_balances::migration::MigrateToTrackInactive<Runtime, xcm_config::CheckingAccount>,
pallet_contracts::Migration<Runtime>,
pallet_multisig::migrations::v1::MigrateToV1<Runtime>,
pallet_collator_selection::migration::v1::MigrateToV1<Runtime>,
pallet_xcm::migration::v1::MigrateToV1<Runtime>,
);

type EventRecord = frame_system::EventRecord<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ parameter_types! {
pub RelayChainOrigin: RuntimeOrigin = cumulus_pallet_xcm::Origin::Relay.into();
pub UniversalLocation: InteriorMultiLocation = Parachain(ParachainInfo::parachain_id().into()).into();
pub const ExecutiveBody: BodyId = BodyId::Executive;
pub CheckingAccount: AccountId = PolkadotXcm::check_account();
}

/// We allow root and the Relay Chain council to execute privileged collator selection operations.
Expand Down

0 comments on commit 1208a9d

Please sign in to comment.