This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Initialize on-chain StorageVersion
for pallets added after genesis
#14641
Open
liamaharon
wants to merge
12
commits into
master
Choose a base branch
from
liam-init-pallet-on-chain-version-after-genesis
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Initialize on-chain StorageVersion
for pallets added after genesis
#14641
liamaharon
wants to merge
12
commits into
master
from
liam-init-pallet-on-chain-version-after-genesis
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
liamaharon
added
A3-in_progress
Pull request is in progress. No review needed at this stage.
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
B1-note_worthy
Changes should be noted in the release notes
T1-runtime
This PR/Issue is related to the topic “runtime”.
labels
Jul 25, 2023
liamaharon
added
A0-please_review
Pull request needs code review.
and removed
A3-in_progress
Pull request is in progress. No review needed at this stage.
labels
Jul 26, 2023
liamaharon
commented
Jul 26, 2023
|
||
// Pallet with no current storage version should have the on-chain version initialized to 0. | ||
TestExternalities::default().execute_with(|| { | ||
// Example4 current_storage_version is NoStorageVersionSet. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be cool if there was some way I could assert this. Any ideas?
liamaharon
commented
Jul 26, 2023
Comment on lines
+2216
to
+2217
// Set the on-chain version to something different to the current version | ||
StorageVersion::new(100).put::<Example>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
otherwise the new before_all hook will init it to the correct value
liamaharon
added
D5-nicetohaveaudit ⚠️
PR contains trivial changes to logic that should be properly reviewed.
and removed
D3-trivial 🧸
PR contains trivial changes in a runtime directory that do not require an audit
labels
Jul 28, 2023
…it-pallet-on-chain-version-after-genesis
7 tasks
KiChjang
reviewed
Aug 18, 2023
Co-authored-by: Keith Yeung <[email protected]>
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.
D5-nicetohaveaudit ⚠️
PR contains trivial changes to logic that should be properly reviewed.
T1-runtime
This PR/Issue is related to the topic “runtime”.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Closes paritytech/polkadot-sdk#109
Problem
Quoting from the above issue:
Solution
exists
method toStorageVersion
, which can be used to check whether the on-chain storage version for a pallet has been initializedbefore_all
method to theOnRuntimeUpgrade
trait with a noop default implementationExecutive
to callbefore_all
for all migrations before running any other hooksbefore_all
in the pallet proc macro impl ofOnRuntimeUpgrade
to initialize the on-chain version to the current pallet version if it does not exist. If no current pallet version exists, the on-chain version is initialized to 0.Other changes in this PR
pallet_name
in the pallet expand proc macro.Some discussion about the placement of
before_all
It was decided in paritytech/polkadot-sdk#109 to add
before_all
directly toOnRuntimeUpgrade
. However, unless there are use cases outside of initializing the storage version of a pallet maybe it is confusing it make it part of that trait and should be put elsewhere?@kianenigma suggested creating a superset of
OnRuntimeUpgrade
called something likeOnPalletRuntimeUpgrade
and puttingbefore_all
there. I like this suggestion because it removes it from view from the average Substrate developer usingOnRuntimeUpgrade
, but maybe there'll be complications having two different migration traits (like couldn't combine into tuples anymore)? Open to suggestions here, @bkchr curious to hear what you think.FAQ
Why create a new hook instead of adding this logic to the pallet
pre_upgrade
?Executive
currently runsCOnRuntimeUpgrade
(custom migrations) beforeAllPalletsWithSystem
migrations. We need versions to be initialized before theCOnRuntimeUpgrade
migrations are run, becauseCOnRuntimeUpgrade
migrations may use the on-chain version for critical logic. e.g.VersionedRuntimeUpgrade
uses it to decide whether or not to execute.We cannot reorder
COnRuntimeUpgrade
andAllPalletsWithSystem
soAllPalletsWithSystem
runs first, becauseAllPalletsWithSystem
have some logic in theirpost_upgrade
hooks to verify that the on-chain version and current pallet version match. A common use case ofCOnRuntimeUpgrade
migrations is to perform a migration which will result in the versions matching, so if they were reordered thesepost_upgrade
checks would fail.Why init the on-chain version for pallets without a current storage version?
We must init the on-chain version for pallets even if they don't have a defined storage version so if there is a future version bump, the on-chain version is not automatically set to that new version without a proper migration.
e.g. bad scenario:
a. Runtime upgrade occurs
b.
before_all
hook initializes the on-chain version to 1c.
on_runtime_upgrade
of the migration executes, and sees the on-chain version is already 1 therefore think storage is already migrated and does not execute the storage migrationNow, on-chain version is 1 but storage is still at version 0.
By always initializing the on-chain version when the pallet is added to the runtime we avoid that scenario.
TODO
before_all