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

Add migration logs to pallet v2 #8243

Merged
2 commits merged into from
Mar 3, 2021
Merged

Add migration logs to pallet v2 #8243

2 commits merged into from
Mar 3, 2021

Conversation

kianenigma
Copy link
Contributor

@kianenigma kianenigma 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. labels Mar 2, 2021
@kianenigma kianenigma requested review from bkchr and gui1117 March 2, 2021 12:18
@@ -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",
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

@@ -1329,21 +1329,12 @@ 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 )* })();
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

@@ -25,6 +25,29 @@ pub fn expand_hooks(def: &mut Def) -> proc_macro2::TokenStream {
let pallet_ident = &def.pallet_struct.pallet;
let where_clause = &def.hooks.where_clause;
let frame_system = &def.frame_system;
let has_runtime_upgrade = def.hooks.has_runtime_upgrade;
Copy link
Member

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 OnRuntimeUpgrade hook or only when the storage version is lower than the pallet version?

Copy link
Contributor Author

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_upgrade in your #[pallet_hooks].

Perhaps I can name it has_custom_, because even if you don't have this, the storage version is upgraded.

Copy link
Member

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

"⚠️ running migration for {} and setting new storage version to {:?}",

forever?

Even if the current upgrade did not require a migration because no changes to the data structures were made?

Copy link
Contributor Author

@kianenigma kianenigma Mar 2, 2021

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_upgrade in 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.

Copy link
Contributor

@gui1117 gui1117 Mar 2, 2021

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 OnRuntimeUpgrade hook or only when the storage version is lower than the pallet version?

it is true when a pallet has a OnRuntimeUpgrade implementation (even if it is {}).

EDIT: It was an answer to the first message

Copy link
Contributor

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 in on_runtime_upgrade.

Copy link
Member

@athei athei Mar 2, 2021

Choose a reason for hiding this comment

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

if version <= 10 { panic!("version too old not supported") }
if version <= 11 { /* migrate from 10 to 11 */ }
if version <= 12 { /* migrate from 11 to 12 */ }
if version <= 13 { /* migrate from 12 to 13 */ }

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.

Copy link
Contributor Author

@kianenigma kianenigma Mar 3, 2021

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:

⚠️ Pallet-xxx is declaring internal migrations (which may or may not execute) and updates storage version to xxx

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.

Copy link
Member

@athei athei Mar 3, 2021

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.

Copy link
Contributor Author

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

@kianenigma
Copy link
Contributor Author

bot merge

@ghost
Copy link

ghost commented Mar 3, 2021

Waiting for commit status.

@ghost ghost merged commit e5e8197 into master Mar 3, 2021
@ghost ghost deleted the kiz-add-migration-log branch March 3, 2021 13:27
@Lohann Lohann mentioned this pull request Mar 22, 2021
6 tasks
This pull request was closed.
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. 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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants