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

migrations: VersionedRuntimeUpgrade #14311

Merged
merged 46 commits into from
Jul 2, 2023

Conversation

liamaharon
Copy link
Contributor

@liamaharon liamaharon commented Jun 6, 2023

Closes #13107

Description

Make it easier to write versioned runtime upgrades.

VersionedRuntimeUpgrade allows developers to write migrations without worrying about checking and setting storage versions. Instead, the developer wraps their migration in this struct which takes care of version handling using best practices.

It takes 5 type parameters:

  • From: The version being upgraded from
  • To: The version being upgraded to
  • Inner: An implementation of OnRuntimeUpgrade
  • Pallet: The Pallet being upgraded
  • Weight: The runtime's RuntimeDbWeight implementation

When a VersionedRuntimeUpgrades on_runtime_upgrade method is called, the on-chain version of the pallet is compared to From. If they match, Inner::on_runtime_upgrade is called and the pallets on-chain version is set to To. Otherwise, a warning is logged notifying the developer that the upgrade was a noop and should probably be removed.

Example

Example using this for a migration in the Polkadot repo: https://github.com/paritytech/polkadot/compare/liam-versioned-runtime-upgrade-example

Decision not to remove #[pallet::storage_version]

The possibility of removing #[pallet::storage_version] leaving only an on-chain version has been discussed. Although this would be nice, I think it is necessary to keep so that we may correctly set the initial on-chain version of a pallet when it is first initialized.

See paritytech/polkadot-sdk#109 for more which I'm planning to work on soon, which appears to require #[pallet::storage_version].

Decision to use From and To rather than just Version (From)

See #14311 (comment)

TODOs

@liamaharon liamaharon added A0-please_review Pull request needs code review. B0-silent Changes should not be mentioned in any release notes C1-low PR touches the given topic and has a low impact on builders. D3-trivial 🧸 PR contains trivial changes in a runtime directory that do not require an audit T1-runtime This PR/Issue is related to the topic “runtime”. labels Jun 6, 2023
@liamaharon liamaharon requested review from a team June 6, 2023 21:22
Copy link
Member

@ggwpez ggwpez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for starting this!

frame/support/src/migrations.rs Outdated Show resolved Hide resolved
frame/support/src/migrations.rs Show resolved Hide resolved
frame/support/src/migrations.rs Outdated Show resolved Hide resolved
@paritytech-ci paritytech-ci requested a review from a team June 7, 2023 11:15
@gui1117
Copy link
Contributor

gui1117 commented Jun 11, 2023

Looks good,

I saw the example in https://github.com/paritytech/polkadot/compare/liam-versioned-runtime-upgrade-example
I wonder about usage: instead of using the type in the kusama runtime it should be used to declare the migration with version checked inside the pallet directly, no?
the pallet configuration would expose both VersionedCheckedMigrateToV6 and VersionedUncheckedMigrateToV6:

type VersionCheckedMigrateToV6 = frame_support::migrations::VersionedRuntimeUpgrade<
			V5ToV6,
			parachains_configuration::migration::v6::VersionedUncheckedMigrateToV6<Runtime>,
			Configuration,
			RocksDbWeight,
		>,

@liamaharon
Copy link
Contributor Author

liamaharon commented Jun 11, 2023

Thanks @ggwpez and @thiolliere for your comments. I've addressed them:

and also written some tests.

Ready to review again when you have some time.

@liamaharon
Copy link
Contributor Author

Question: after merging, should we apply this to applicable Unreleased migrations?

@kianenigma
Copy link
Contributor

kianenigma commented Jun 12, 2023

The main reason I suggested removing #[pallet::storage_version] is that I envision a single system-like migration pallet (possibly #14275) to handle all the things that @thiolliere and @ggwpez discussed, like jumping migrations and so on.

Moreover, we have always considered a new hook for when a pallet is added to the runtime. All in all

I think it is necessary to keep so that we may correctly set the initial on-chain version of a pallet when it is first initialized.

can be solved in various other ways.

--

From a DevEx perspective, I still want to suggest finding a way to not do this, as having both the onchain version and the fixed pallet version is not optimal.

All in all, I do agree that this is a clear improvement over the existing mechanism. My comment is that the existing mechanism is itself somewhat clunky/broken and can be reconsidered.

/// that the upgrade was a noop and should probably be removed.
///
/// Example:
/// ```ignore
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

possibly not to difficult to make this not be ignore and actually compile.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are you thinking I'd use the dummy pallet and upgrades from my test file? I'd rather not use a migration from a real pallet since it creates a dependency that could break. I'm also curious what the benefit/s are of making the comment compile

@ggwpez ggwpez added C1-low PR touches the given topic and has a low impact on builders. and removed C3-medium PR touches the given topic and has a medium impact on builders. labels Jun 23, 2023
@ggwpez
Copy link
Member

ggwpez commented Jun 23, 2023

I think we still use the C* labels for release priority. So anything that is not a hot/security fix is "low".

frame/support/src/migrations.rs Outdated Show resolved Hide resolved
frame/support/src/migrations.rs Outdated Show resolved Hide resolved
frame/support/src/migrations.rs Outdated Show resolved Hide resolved
frame/support/src/migrations.rs Outdated Show resolved Hide resolved
frame/support/src/migrations.rs Outdated Show resolved Hide resolved
frame/support/tests/versioned_runtime_upgrade.rs Outdated Show resolved Hide resolved
frame/support/src/migrations.rs Outdated Show resolved Hide resolved
liamaharon and others added 11 commits June 30, 2023 00:20
Co-authored-by: Gonçalo Pestana <g6pestana@gmail.com>
Co-authored-by: Gonçalo Pestana <g6pestana@gmail.com>
Co-authored-by: Gonçalo Pestana <g6pestana@gmail.com>
Co-authored-by: Gonçalo Pestana <g6pestana@gmail.com>
Co-authored-by: Gonçalo Pestana <g6pestana@gmail.com>
@liamaharon
Copy link
Contributor Author

bot merge

@paritytech-processbot paritytech-processbot bot merged commit 9e8b2f6 into master Jul 2, 2023
@paritytech-processbot paritytech-processbot bot deleted the liam-versioned-runtime-upgrade branch July 2, 2023 08:36
nathanwhit pushed a commit to nathanwhit/substrate that referenced this pull request Jul 19, 2023
* VersionedRuntimeUpgrade

* only require one version and add a pre-upgrade check

* add docs

* improve warning log

* improve comments

* fix log

* use associated constants

* allow passing from and to versions

* test versioned_runtime_upgrade

* fix typo

* improve docs

* docs

* docs

* remove event from dummy pallet

* remove pre_upgrade current storage version check

* derive_impl

* skip pre/post checks if the actual migration will not run

* improve variable naming

* docs

* fix post_upgrade 'should run' logic

* fix comments

* pre and post hook tests

* feature gate try-runtime stuff

* remove deprecated macro

* Update frame/support/src/migrations.rs

Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* decode_all

* make experimental

* use rust generics

* add info log when running

* simplify tests

* improve log

* improve log

* cleaner pre_upgrade encoding

* Update frame/support/src/migrations.rs

Co-authored-by: Gonçalo Pestana <g6pestana@gmail.com>

* Update frame/support/src/migrations.rs

Co-authored-by: Gonçalo Pestana <g6pestana@gmail.com>

* Update frame/support/src/migrations.rs

Co-authored-by: Gonçalo Pestana <g6pestana@gmail.com>

* Update frame/support/src/migrations.rs

Co-authored-by: Gonçalo Pestana <g6pestana@gmail.com>

* Update frame/support/src/migrations.rs

Co-authored-by: Gonçalo Pestana <g6pestana@gmail.com>

* VersionedPostUpgradeData enum

* move versioned runtime upgrade tests to test/tests

* fix rust doc

* clarify comment

---------

Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Co-authored-by: Gonçalo Pestana <g6pestana@gmail.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A0-please_review Pull request needs code review. B1-note_worthy Changes should be noted in the release notes C1-low PR touches the given topic and has a low impact on builders. D3-trivial 🧸 PR contains trivial changes in a runtime directory that do not require an audit T1-runtime This PR/Issue is related to the topic “runtime”.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Versioned OnRuntimeUpgrade
7 participants