From 6d1d288e16aef652f53248df6dca42f649b9af7e Mon Sep 17 00:00:00 2001 From: kianenigma Date: Tue, 2 Mar 2021 13:17:27 +0100 Subject: [PATCH 1/2] Add logs to proc macro pallet. --- .../procedural/src/pallet/expand/hooks.rs | 35 +++++++++++++++++- .../procedural/src/pallet/parse/hooks.rs | 8 ++++ frame/support/src/dispatch.rs | 37 +++++++++---------- 3 files changed, 58 insertions(+), 22 deletions(-) diff --git a/frame/support/procedural/src/pallet/expand/hooks.rs b/frame/support/procedural/src/pallet/expand/hooks.rs index b1eee507fdf50..52054820107af 100644 --- a/frame/support/procedural/src/pallet/expand/hooks.rs +++ b/frame/support/procedural/src/pallet/expand/hooks.rs @@ -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; + + let log_runtime_upgrade = if has_runtime_upgrade { + // a migration is defined here. + quote::quote! { + #frame_support::log::info!( + target: #frame_support::LOG_TARGET, + "⚠️ running migration for {} and setting new storage version to {:?}", + pallet_name, + new_storage_version, + ); + } + } else { + // default. + quote::quote! { + #frame_support::log::info!( + target: #frame_support::LOG_TARGET, + "✅ no migration for '{}' and setting new storage version to {:?}", + pallet_name, + new_storage_version, + ); + } + }; quote::quote_spanned!(def.hooks.attr_span => impl<#type_impl_gen> @@ -60,14 +83,22 @@ pub fn expand_hooks(def: &mut Def) -> proc_macro2::TokenStream { for #pallet_ident<#type_use_gen> #where_clause { fn on_runtime_upgrade() -> #frame_support::weights::Weight { + // log info about the upgrade. + let new_storage_version = #frame_support::crate_to_pallet_version!(); + let pallet_name = < + ::PalletInfo + as + #frame_support::traits::PalletInfo + >::name::().unwrap_or(""); + #log_runtime_upgrade + let result = < Self as #frame_support::traits::Hooks< ::BlockNumber > >::on_runtime_upgrade(); - #frame_support::crate_to_pallet_version!() - .put_into_storage::<::PalletInfo, Self>(); + new_storage_version.put_into_storage::<::PalletInfo, Self>(); let additional_write = < ::DbWeight as #frame_support::traits::Get<_> diff --git a/frame/support/procedural/src/pallet/parse/hooks.rs b/frame/support/procedural/src/pallet/parse/hooks.rs index 585222060e5f4..99ae3ed625414 100644 --- a/frame/support/procedural/src/pallet/parse/hooks.rs +++ b/frame/support/procedural/src/pallet/parse/hooks.rs @@ -28,6 +28,8 @@ pub struct HooksDef { pub where_clause: Option, /// 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", + _ => false, + }); + Ok(Self { attr_span, index, instances, + has_runtime_upgrade, where_clause: item.generics.where_clause.clone(), }) } diff --git a/frame/support/src/dispatch.rs b/frame/support/src/dispatch.rs index ab9feae3c2495..22a5cc0c946c4 100644 --- a/frame/support/src/dispatch.rs +++ b/frame/support/src/dispatch.rs @@ -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 )* })(); - - 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::().expect("pallet will have name in the runtime; qed"); + >::PalletInfo as $crate::traits::PalletInfo>::name::().unwrap_or(""); + let new_storage_version = $crate::crate_to_pallet_version!(); $crate::log::info!( target: $crate::LOG_TARGET, @@ -1352,6 +1343,15 @@ macro_rules! decl_module { 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,16 +1378,12 @@ 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::().expect("pallet will have name in the runtime; qed"); + >::PalletInfo as $crate::traits::PalletInfo>::name::().unwrap_or(""); + let new_storage_version = $crate::crate_to_pallet_version!(); $crate::log::info!( target: $crate::LOG_TARGET, @@ -1396,9 +1392,10 @@ macro_rules! decl_module { 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")] From 0e168feadd6dea802ea54bc10925923d7555e832 Mon Sep 17 00:00:00 2001 From: kianenigma Date: Wed, 3 Mar 2021 13:56:11 +0100 Subject: [PATCH 2/2] update logs. --- frame/support/procedural/src/pallet/expand/hooks.rs | 4 ++-- frame/support/src/dispatch.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/frame/support/procedural/src/pallet/expand/hooks.rs b/frame/support/procedural/src/pallet/expand/hooks.rs index 52054820107af..d55a74209d053 100644 --- a/frame/support/procedural/src/pallet/expand/hooks.rs +++ b/frame/support/procedural/src/pallet/expand/hooks.rs @@ -32,7 +32,7 @@ pub fn expand_hooks(def: &mut Def) -> proc_macro2::TokenStream { quote::quote! { #frame_support::log::info!( target: #frame_support::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, ); @@ -42,7 +42,7 @@ pub fn expand_hooks(def: &mut Def) -> proc_macro2::TokenStream { quote::quote! { #frame_support::log::info!( target: #frame_support::LOG_TARGET, - "✅ no migration for '{}' and setting new storage version to {:?}", + "✅ no migration for {}, setting storage version to {:?}", pallet_name, new_storage_version, ); diff --git a/frame/support/src/dispatch.rs b/frame/support/src/dispatch.rs index 22a5cc0c946c4..4dd2c83f1578a 100644 --- a/frame/support/src/dispatch.rs +++ b/frame/support/src/dispatch.rs @@ -1338,7 +1338,7 @@ macro_rules! decl_module { $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, ); @@ -1387,7 +1387,7 @@ macro_rules! decl_module { $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, );