Skip to content

Commit

Permalink
do not use new feature, use macro in frame-support instead
Browse files Browse the repository at this point in the history
  • Loading branch information
gui1117 committed Jul 23, 2024
1 parent 514e714 commit 61fa26d
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 99 deletions.
1 change: 0 additions & 1 deletion substrate/frame/support/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ runtime-benchmarks = [
"sp-staking/runtime-benchmarks",
]
try-runtime = [
"frame-support-procedural/try-runtime",
"frame-system/try-runtime",
"sp-debug-derive/force-debug",
"sp-runtime/try-runtime",
Expand Down
3 changes: 0 additions & 3 deletions substrate/frame/support/procedural/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ regex = { workspace = true }
[features]
default = ["std"]
std = ["sp-crypto-hashing/std"]
# Generates the implementation for traits and methods available when frame-support is compiled with
# `try-runtime` feature.
try-runtime = []
no-metadata-docs = []
experimental = []
# Generate impl-trait for tuples with the given number of tuples. Will be needed as the number of
Expand Down
112 changes: 50 additions & 62 deletions substrate/frame/support/procedural/src/pallet/expand/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,66 +140,6 @@ pub fn expand_hooks(def: &mut Def) -> proc_macro2::TokenStream {
}
};

let pre_upgrade_post_upgrade = if cfg!(feature = "try-runtime") {
quote::quote_spanned!(span =>
fn pre_upgrade() -> Result<#frame_support::__private::Vec<u8>, #frame_support::sp_runtime::TryRuntimeError> {
<
Self
as
#frame_support::traits::Hooks<#frame_system::pallet_prelude::BlockNumberFor::<T>>
>::pre_upgrade()
}

fn post_upgrade(state: #frame_support::__private::Vec<u8>) -> Result<(), #frame_support::sp_runtime::TryRuntimeError> {
#post_storage_version_check

<
Self
as
#frame_support::traits::Hooks<#frame_system::pallet_prelude::BlockNumberFor::<T>>
>::post_upgrade(state)
}
)
} else {
proc_macro2::TokenStream::new()
};

let try_state = if cfg!(feature = "try-runtime") {
quote::quote_spanned!(span =>
impl<#type_impl_gen>
#frame_support::traits::TryState<#frame_system::pallet_prelude::BlockNumberFor::<T>>
for #pallet_ident<#type_use_gen> #where_clause
{
fn try_state(
n: #frame_system::pallet_prelude::BlockNumberFor::<T>,
_s: #frame_support::traits::TryStateSelect
) -> Result<(), #frame_support::sp_runtime::TryRuntimeError> {
#frame_support::__private::log::info!(
target: #frame_support::LOG_TARGET,
"🩺 Running {:?} try-state checks",
#pallet_name,
);
<
Self as #frame_support::traits::Hooks<
#frame_system::pallet_prelude::BlockNumberFor::<T>
>
>::try_state(n).map_err(|err| {
#frame_support::__private::log::error!(
target: #frame_support::LOG_TARGET,
"❌ {:?} try_state checks failed: {:?}",
#pallet_name,
err
);

err
})
}
}
)
} else {
proc_macro2::TokenStream::new()
};

quote::quote_spanned!(span =>
#hooks_impl

Expand Down Expand Up @@ -314,7 +254,25 @@ pub fn expand_hooks(def: &mut Def) -> proc_macro2::TokenStream {
>::on_runtime_upgrade()
}

#pre_upgrade_post_upgrade
#frame_support::try_runtime_enabled! {
fn pre_upgrade() -> Result<#frame_support::__private::Vec<u8>, #frame_support::sp_runtime::TryRuntimeError> {
<
Self
as
#frame_support::traits::Hooks<#frame_system::pallet_prelude::BlockNumberFor::<T>>
>::pre_upgrade()
}

fn post_upgrade(state: #frame_support::__private::Vec<u8>) -> Result<(), #frame_support::sp_runtime::TryRuntimeError> {
#post_storage_version_check

<
Self
as
#frame_support::traits::Hooks<#frame_system::pallet_prelude::BlockNumberFor::<T>>
>::post_upgrade(state)
}
}
}

impl<#type_impl_gen>
Expand Down Expand Up @@ -348,6 +306,36 @@ pub fn expand_hooks(def: &mut Def) -> proc_macro2::TokenStream {
}
}

#try_state
#frame_support::try_runtime_enabled! {
impl<#type_impl_gen>
#frame_support::traits::TryState<#frame_system::pallet_prelude::BlockNumberFor::<T>>
for #pallet_ident<#type_use_gen> #where_clause
{
fn try_state(
n: #frame_system::pallet_prelude::BlockNumberFor::<T>,
_s: #frame_support::traits::TryStateSelect
) -> Result<(), #frame_support::sp_runtime::TryRuntimeError> {
#frame_support::__private::log::info!(
target: #frame_support::LOG_TARGET,
"🩺 Running {:?} try-state checks",
#pallet_name,
);
<
Self as #frame_support::traits::Hooks<
#frame_system::pallet_prelude::BlockNumberFor::<T>
>
>::try_state(n).map_err(|err| {
#frame_support::__private::log::error!(
target: #frame_support::LOG_TARGET,
"❌ {:?} try_state checks failed: {:?}",
#pallet_name,
err
);

err
})
}
}
}
)
}
66 changes: 33 additions & 33 deletions substrate/frame/support/procedural/src/pallet/expand/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,7 @@ pub fn expand_storages(def: &mut Def) -> proc_macro2::TokenStream {
let type_impl_gen = &def.type_impl_generics(proc_macro2::Span::call_site());
let type_use_gen = &def.type_use_generics(proc_macro2::Span::call_site());

let try_decode_entire_state = if cfg!(feature = "try-runtime") {
let try_decode_entire_state = {
let mut storage_names = def
.storages
.iter()
Expand All @@ -849,44 +849,44 @@ pub fn expand_storages(def: &mut Def) -> proc_macro2::TokenStream {
storage_names.sort_by_cached_key(|ident| ident.to_string());

quote::quote!(
impl<#type_impl_gen> #frame_support::traits::TryDecodeEntireStorage
for #pallet_ident<#type_use_gen> #completed_where_clause
{
fn try_decode_entire_state() -> Result<usize, #frame_support::__private::Vec<#frame_support::traits::TryDecodeEntireStorageError>> {
let pallet_name = <<T as #frame_system::Config>::PalletInfo as #frame_support::traits::PalletInfo>
::name::<#pallet_ident<#type_use_gen>>()
.expect("Every active pallet has a name in the runtime; qed");

#frame_support::__private::log::debug!(target: "runtime::try-decode-state", "trying to decode pallet: {pallet_name}");

// NOTE: for now, we have to exclude storage items that are feature gated.
let mut errors = #frame_support::__private::Vec::new();
let mut decoded = 0usize;

#(
#frame_support::__private::log::debug!(target: "runtime::try-decode-state", "trying to decode storage: \
{pallet_name}::{}", stringify!(#storage_names));
#frame_support::try_runtime_enabled! {
impl<#type_impl_gen> #frame_support::traits::TryDecodeEntireStorage
for #pallet_ident<#type_use_gen> #completed_where_clause
{
fn try_decode_entire_state() -> Result<usize, #frame_support::__private::Vec<#frame_support::traits::TryDecodeEntireStorageError>> {
let pallet_name = <<T as #frame_system::Config>::PalletInfo as #frame_support::traits::PalletInfo>
::name::<#pallet_ident<#type_use_gen>>()
.expect("Every active pallet has a name in the runtime; qed");

#frame_support::__private::log::debug!(target: "runtime::try-decode-state", "trying to decode pallet: {pallet_name}");

// NOTE: for now, we have to exclude storage items that are feature gated.
let mut errors = #frame_support::__private::Vec::new();
let mut decoded = 0usize;

#(
#frame_support::__private::log::debug!(target: "runtime::try-decode-state", "trying to decode storage: \
{pallet_name}::{}", stringify!(#storage_names));

match <#storage_names as #frame_support::traits::TryDecodeEntireStorage>::try_decode_entire_state() {
Ok(count) => {
decoded += count;
},
Err(err) => {
errors.extend(err);
},
}
)*

match <#storage_names as #frame_support::traits::TryDecodeEntireStorage>::try_decode_entire_state() {
Ok(count) => {
decoded += count;
},
Err(err) => {
errors.extend(err);
},
if errors.is_empty() {
Ok(decoded)
} else {
Err(errors)
}
)*

if errors.is_empty() {
Ok(decoded)
} else {
Err(errors)
}
}
}
)
} else {
proc_macro2::TokenStream::new()
};

quote::quote!(
Expand Down
2 changes: 2 additions & 0 deletions substrate/frame/support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2587,6 +2587,8 @@ pub use frame_support_procedural::register_default_impl;

// Generate a macro that will enable/disable code based on `std` feature being active.
sp_core::generate_feature_enabled_macro!(std_enabled, feature = "std", $);
// Generate a macro that will enable/disable code based on `try-runtime` feature being active.
sp_core::generate_feature_enabled_macro!(try_runtime_enabled, feature = "try-runtime", $);

// Helper for implementing GenesisBuilder runtime API
pub mod genesis_builder_helper;
Expand Down

0 comments on commit 61fa26d

Please sign in to comment.