Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions prdoc/pr_9451.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
title: 'Call `SingleBlockMigrations` from `frame_system::Config` on `try_on_runtime_upgrade`.'
doc:
- audience: Runtime Dev
description: |-
Fixes a small bug in `try-runtime` code, where `SingleBlockMigrations` from `frame_system::Config` was not called in `try_on_runtime_upgrade`.
crates:
- name: frame-executive
bump: patch
11 changes: 8 additions & 3 deletions substrate/frame/executive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,10 +424,15 @@ where
pub fn try_runtime_upgrade(checks: UpgradeCheckSelect) -> Result<Weight, TryRuntimeError> {
let before_all_weight =
<AllPalletsWithSystem as BeforeAllRuntimeMigrations>::before_all_runtime_migrations();

let try_on_runtime_upgrade_weight =
<(COnRuntimeUpgrade, AllPalletsWithSystem) as OnRuntimeUpgrade>::try_on_runtime_upgrade(
checks.pre_and_post(),
)?;
<(
COnRuntimeUpgrade,
<System as frame_system::Config>::SingleBlockMigrations,
Comment thread
RomarQ marked this conversation as resolved.
// We want to run the migrations before we call into the pallets as they may
// access any state that would then not be migrated.
AllPalletsWithSystem,
) as OnRuntimeUpgrade>::try_on_runtime_upgrade(checks.pre_and_post())?;

frame_system::LastRuntimeUpgrade::<System>::put(
frame_system::LastRuntimeUpgradeInfo::from(
Expand Down
49 changes: 41 additions & 8 deletions substrate/frame/executive/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ use sp_runtime::{
};

const TEST_KEY: &[u8] = b":test:key:";
const TEST_KEY_2: &[u8] = b":test:key_2:";

#[frame_support::pallet(dev_mode)]
mod custom {
Expand Down Expand Up @@ -354,6 +355,7 @@ impl frame_system::Config for Runtime {
type PostTransactions = MockedSystemCallbacks;
type MultiBlockMigrator = MockedModeGetter;
type ExtensionsWeightInfo = MockExtensionsWeights;
type SingleBlockMigrations = CustomOnRuntimeUpgrade;
}

#[derive(
Expand Down Expand Up @@ -479,17 +481,24 @@ type TestBlock = Block<UncheckedXt>;
// Will contain `true` when the custom runtime logic was called.
const CUSTOM_ON_RUNTIME_KEY: &[u8] = b":custom:on_runtime";

struct CustomOnRuntimeUpgrade;
pub struct CustomOnRuntimeUpgrade;
impl OnRuntimeUpgrade for CustomOnRuntimeUpgrade {
fn on_runtime_upgrade() -> Weight {
sp_io::storage::set(TEST_KEY, "custom_upgrade".as_bytes());
sp_io::storage::set(TEST_KEY_2, "try_runtime_upgrade_works".as_bytes());
sp_io::storage::set(CUSTOM_ON_RUNTIME_KEY, &true.encode());
System::deposit_event(frame_system::Event::CodeUpdated);

assert_eq!(0, System::last_runtime_upgrade_spec_version());

Weight::from_parts(100, 0)
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(_state: Vec<u8>) -> Result<(), TryRuntimeError> {
assert_eq!(&sp_io::storage::get(TEST_KEY_2).unwrap()[..], *b"try_runtime_upgrade_works");
Ok(())
}
}

type Executive = super::Executive<
Expand Down Expand Up @@ -1074,20 +1083,15 @@ fn all_weights_are_recorded_correctly() {
MockedSystemCallbacks::reset();

// All weights that show up in the `initialize_block_impl`
let custom_runtime_upgrade_weight = CustomOnRuntimeUpgrade::on_runtime_upgrade();
let runtime_upgrade_weight =
<AllPalletsWithSystem as OnRuntimeUpgrade>::on_runtime_upgrade();
let runtime_upgrade_weight = Executive::execute_on_runtime_upgrade();
let on_initialize_weight =
<AllPalletsWithSystem as OnInitialize<u64>>::on_initialize(block_number);
let base_block_weight = <Runtime as frame_system::Config>::BlockWeights::get().base_block;

// Weights are recorded correctly
assert_eq!(
frame_system::Pallet::<Runtime>::block_weight().total(),
custom_runtime_upgrade_weight +
runtime_upgrade_weight +
on_initialize_weight +
base_block_weight,
runtime_upgrade_weight + on_initialize_weight + base_block_weight,
);
});
}
Expand Down Expand Up @@ -1294,6 +1298,35 @@ fn try_execute_block_works() {
});
}

#[test]
#[cfg(feature = "try-runtime")]
fn try_runtime_upgrade_works() {
Comment thread
RomarQ marked this conversation as resolved.
use frame_support::traits::OnGenesis;

sp_tracing::init_for_tests();

type ExecutiveWithoutMigrations = super::Executive<
Runtime,
Block<UncheckedXt>,
ChainContext<Runtime>,
Runtime,
AllPalletsWithSystem,
>;

new_test_ext(1).execute_with(|| {
// Call `on_genesis` to reset the storage version of all pallets.
AllPalletsWithSystem::on_genesis();

// Make sure the test storages are un-set
assert!(&sp_io::storage::get(TEST_KEY_2).is_none());

ExecutiveWithoutMigrations::try_runtime_upgrade(UpgradeCheckSelect::All).unwrap();

// Make sure the test storages were set
assert_eq!(&sp_io::storage::get(TEST_KEY_2).unwrap()[..], *b"try_runtime_upgrade_works");
});
}

/// Same as `extrinsic_while_exts_forbidden_errors` but using the try-runtime function.
#[test]
#[cfg(feature = "try-runtime")]
Expand Down
7 changes: 7 additions & 0 deletions templates/parachain/runtime/src/configs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ parameter_types! {
pub const SS58Prefix: u16 = 42;
}

/// All migrations of the runtime, aside from the ones declared in the pallets.
///
/// This can be a tuple of types, each implementing `OnRuntimeUpgrade`.
#[allow(unused_parens)]
type SingleBlockMigrations = ();

/// The default types are being injected by [`derive_impl`](`frame_support::derive_impl`) from
/// [`ParaChainDefaultConfig`](`struct@frame_system::config_preludes::ParaChainDefaultConfig`),
/// but overridden as needed.
Expand Down Expand Up @@ -127,6 +133,7 @@ impl frame_system::Config for Runtime {
/// The action to take on a Runtime Upgrade
type OnSetCode = cumulus_pallet_parachain_system::ParachainSetCode<Self>;
type MaxConsumers = frame_support::traits::ConstU32<16>;
type SingleBlockMigrations = SingleBlockMigrations;
}

/// Configure the palelt weight reclaim tx.
Expand Down
7 changes: 0 additions & 7 deletions templates/parachain/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,20 +95,13 @@ pub type TxExtension = cumulus_pallet_weight_reclaim::StorageWeightReclaim<
pub type UncheckedExtrinsic =
generic::UncheckedExtrinsic<Address, RuntimeCall, Signature, TxExtension>;

/// All migrations of the runtime, aside from the ones declared in the pallets.
///
/// This can be a tuple of types, each implementing `OnRuntimeUpgrade`.
#[allow(unused_parens)]
type Migrations = ();

/// Executive: handles dispatch to the various modules.
pub type Executive = frame_executive::Executive<
Runtime,
Block,
frame_system::ChainContext<Runtime>,
Runtime,
AllPalletsWithSystem,
Migrations,
>;

/// Handles converting a weight scalar to a fee value, based on the scale and granularity of the
Expand Down
7 changes: 7 additions & 0 deletions templates/solochain/runtime/src/configs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ parameter_types! {
pub const SS58Prefix: u8 = 42;
}

/// All migrations of the runtime, aside from the ones declared in the pallets.
///
/// This can be a tuple of types, each implementing `OnRuntimeUpgrade`.
#[allow(unused_parens)]
type SingleBlockMigrations = ();

/// The default types are being injected by [`derive_impl`](`frame_support::derive_impl`) from
/// [`SoloChainDefaultConfig`](`struct@frame_system::config_preludes::SolochainDefaultConfig`),
/// but overridden as needed.
Expand Down Expand Up @@ -88,6 +94,7 @@ impl frame_system::Config for Runtime {
/// This is used as an identifier of the chain. 42 is the generic substrate prefix.
type SS58Prefix = SS58Prefix;
type MaxConsumers = frame_support::traits::ConstU32<16>;
type SingleBlockMigrations = SingleBlockMigrations;
}

impl pallet_aura::Config for Runtime {
Expand Down
7 changes: 0 additions & 7 deletions templates/solochain/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,20 +168,13 @@ pub type UncheckedExtrinsic =
/// The payload being signed in transactions.
pub type SignedPayload = generic::SignedPayload<RuntimeCall, TxExtension>;

/// All migrations of the runtime, aside from the ones declared in the pallets.
///
/// This can be a tuple of types, each implementing `OnRuntimeUpgrade`.
#[allow(unused_parens)]
type Migrations = ();

/// Executive: handles dispatch to the various modules.
pub type Executive = frame_executive::Executive<
Runtime,
Block,
frame_system::ChainContext<Runtime>,
Runtime,
AllPalletsWithSystem,
Migrations,
>;

// Create the runtime by composing the FRAME pallets that were previously configured.
Expand Down
Loading