This repository was 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.7k
Add migration logs to pallet v2 #8243
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,6 +28,8 @@ pub struct HooksDef { | |
| pub where_clause: Option<syn::WhereClause>, | ||
| /// The span of the pallet::hooks attribute. | ||
| pub attr_span: proc_macro2::Span, | ||
| /// Boolean flag, set to true if the `on_runtime_upgrade` method of hooks was implemented. | ||
| pub has_runtime_upgrade: bool, | ||
| } | ||
|
|
||
| impl HooksDef { | ||
|
|
@@ -66,10 +68,16 @@ impl HooksDef { | |
| return Err(syn::Error::new(item_trait.span(), msg)); | ||
| } | ||
|
|
||
| let has_runtime_upgrade = item.items.iter().any(|i| match i { | ||
| syn::ImplItem::Method(method) => method.sig.ident == "on_runtime_upgrade", | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Happy to write a test for this, if someone can point out where, or find another way to avoid hardcoding the name here. |
||
| _ => false, | ||
| }); | ||
|
|
||
| Ok(Self { | ||
| attr_span, | ||
| index, | ||
| instances, | ||
| has_runtime_upgrade, | ||
| where_clause: item.generics.where_clause.clone(), | ||
| }) | ||
| } | ||
|
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1329,29 +1329,29 @@ macro_rules! decl_module { | |
| { | ||
| fn on_runtime_upgrade() -> $return { | ||
| $crate::sp_tracing::enter_span!($crate::sp_tracing::trace_span!("on_runtime_upgrade")); | ||
| let result: $return = (|| { $( $impl )* })(); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here I just re-ordered stuff: first log that the migration is about to happen, and then do it. |
||
|
|
||
| let new_storage_version = $crate::crate_to_pallet_version!(); | ||
| new_storage_version | ||
| .put_into_storage::<<$trait_instance as $system::Config>::PalletInfo, Self>(); | ||
|
|
||
| let additional_write = < | ||
| <$trait_instance as $system::Config>::DbWeight as $crate::traits::Get<_> | ||
| >::get().writes(1); | ||
|
|
||
| let pallet_name = << | ||
| $trait_instance | ||
| as | ||
| $system::Config | ||
| >::PalletInfo as $crate::traits::PalletInfo>::name::<Self>().expect("pallet will have name in the runtime; qed"); | ||
| >::PalletInfo as $crate::traits::PalletInfo>::name::<Self>().unwrap_or("<unknown pallet name>"); | ||
| let new_storage_version = $crate::crate_to_pallet_version!(); | ||
|
|
||
| $crate::log::info!( | ||
| target: $crate::LOG_TARGET, | ||
| "⚠️ running migration for {} and setting new storage version to {:?}", | ||
| "⚠️ {} declares internal migrations (which *might* execute), setting storage version to {:?}", | ||
| pallet_name, | ||
| new_storage_version, | ||
| ); | ||
|
|
||
| let result: $return = (|| { $( $impl )* })(); | ||
|
|
||
| new_storage_version | ||
| .put_into_storage::<<$trait_instance as $system::Config>::PalletInfo, Self>(); | ||
|
|
||
| let additional_write = < | ||
| <$trait_instance as $system::Config>::DbWeight as $crate::traits::Get<_> | ||
| >::get().writes(1); | ||
|
|
||
| result.saturating_add(additional_write) | ||
| } | ||
|
|
||
|
|
@@ -1378,27 +1378,24 @@ macro_rules! decl_module { | |
| { | ||
| fn on_runtime_upgrade() -> $crate::dispatch::Weight { | ||
| $crate::sp_tracing::enter_span!($crate::sp_tracing::trace_span!("on_runtime_upgrade")); | ||
|
|
||
| let new_storage_version = $crate::crate_to_pallet_version!(); | ||
| new_storage_version | ||
| .put_into_storage::<<$trait_instance as $system::Config>::PalletInfo, Self>(); | ||
|
|
||
| let pallet_name = << | ||
| $trait_instance | ||
| as | ||
| $system::Config | ||
| >::PalletInfo as $crate::traits::PalletInfo>::name::<Self>().expect("pallet will have name in the runtime; qed"); | ||
| >::PalletInfo as $crate::traits::PalletInfo>::name::<Self>().unwrap_or("<unknown pallet name>"); | ||
| let new_storage_version = $crate::crate_to_pallet_version!(); | ||
|
|
||
| $crate::log::info!( | ||
| target: $crate::LOG_TARGET, | ||
| "✅ no migration for '{}' and setting new storage version to {:?}", | ||
| "✅ no migration for {}, setting storage version to {:?}", | ||
| pallet_name, | ||
| new_storage_version, | ||
| ); | ||
|
|
||
| < | ||
| <$trait_instance as $system::Config>::DbWeight as $crate::traits::Get<_> | ||
| >::get().writes(1) | ||
| new_storage_version | ||
| .put_into_storage::<<$trait_instance as $system::Config>::PalletInfo, Self>(); | ||
|
|
||
| <<$trait_instance as $system::Config>::DbWeight as $crate::traits::Get<_>>::get().writes(1) | ||
| } | ||
|
|
||
| #[cfg(feature = "try-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.
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.
Is this true when a pallet has a
OnRuntimeUpgradehook or only when the storage version is lower than the pallet version?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.
only when you have a function with name
on_runime_upgradein your#[pallet_hooks].Perhaps I can name it
has_custom_, because even if you don't have this, the storage version is upgraded.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.
So once we add the hook we will get
forever?
Even if the current upgrade did not require a migration because no changes to the data structures were made?
Uh oh!
There was an error while loading. Please reload this page.
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.
Yes, if you don't have the migration you should not have
fn on_runtime_upgradein your code, as it can be accidentally re-applied.You might say I am opinionated here, but this is exactly the scenario of how we almost burned polkadot's balance structure once, by applying a migration twice, due to forgetting to remove it from
on_runtime_upgrade. While we have now storage versions to also prevent that, my opinion is due to such incidents and catching them early.Uh oh!
There was an error while loading. Please reload this page.
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 is true when a pallet has a
OnRuntimeUpgradeimplementation (even if it is{}).EDIT: It was an answer to the first message
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.
Yeah I'm unsure how to resolve the conflict between keeping very long migration functions around vs not supporting upgrades from old versions. My current preferred compromise is providing this sort of full migration in the migration mod or crate and then calling whatever the pallet author thinks is best in
on_runtime_upgrade. Then downstream users still have access to all the migrations, but you don't have to keep them around inon_runtime_upgrade.Uh oh!
There was an error while loading. Please reload this page.
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.
This is actually what I was planning to do. Apart from being annoying its the only thing that I can come up with that "just works". Maybe we can build some macros or functions around this pattern to make it more bearable. Because for users this is the best case.
Uh oh!
There was an error while loading. Please reload this page.
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.
For now I will change the log to something that reflects it, so that we can move on:
This is because what you are doing, is neat, but what I am building is mostly for pallets that make it into polkadot and current workflow there is that before each release we want to delete all the old migrations, and such warnings can help with that.
Uh oh!
There was an error while loading. Please reload this page.
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.
I understand. I am fine with that. Contracts should be easily upgradeable for all runtimes. Therefore I need to go down this route.
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.
updated, then can you approve? :D