This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Initialize on-chain StorageVersion for pallets added after genesis#14641
Open
liamaharon wants to merge 12 commits intomasterfrom
Open
Initialize on-chain StorageVersion for pallets added after genesis#14641liamaharon wants to merge 12 commits intomasterfrom
StorageVersion for pallets added after genesis#14641liamaharon wants to merge 12 commits intomasterfrom
Conversation
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. |
Contributor
Author
There was a problem hiding this comment.
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>(); |
Contributor
Author
There was a problem hiding this comment.
otherwise the new before_all hook will init it to the correct value
…it-pallet-on-chain-version-after-genesis
7 tasks
KiChjang
reviewed
Aug 18, 2023
Co-authored-by: Keith Yeung <kungfukeith11@gmail.com>
This file contains hidden or 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
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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
existsmethod toStorageVersion, which can be used to check whether the on-chain storage version for a pallet has been initializedbefore_allmethod to theOnRuntimeUpgradetrait with a noop default implementationExecutiveto callbefore_allfor all migrations before running any other hooksbefore_allin the pallet proc macro impl ofOnRuntimeUpgradeto 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_namein the pallet expand proc macro.Some discussion about the placement of
before_allIt was decided in paritytech/polkadot-sdk#109 to add
before_alldirectly 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
OnRuntimeUpgradecalled something likeOnPalletRuntimeUpgradeand puttingbefore_allthere. 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?Executivecurrently runsCOnRuntimeUpgrade(custom migrations) beforeAllPalletsWithSystemmigrations. We need versions to be initialized before theCOnRuntimeUpgrademigrations are run, becauseCOnRuntimeUpgrademigrations may use the on-chain version for critical logic. e.g.VersionedRuntimeUpgradeuses it to decide whether or not to execute.We cannot reorder
COnRuntimeUpgradeandAllPalletsWithSystemsoAllPalletsWithSystemruns first, becauseAllPalletsWithSystemhave some logic in theirpost_upgradehooks to verify that the on-chain version and current pallet version match. A common use case ofCOnRuntimeUpgrademigrations is to perform a migration which will result in the versions matching, so if they were reordered thesepost_upgradechecks 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_allhook initializes the on-chain version to 1c.
on_runtime_upgradeof 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