Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve try-state developer experience & fix bug #2019

Merged
merged 8 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 2 additions & 13 deletions substrate/frame/executive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,23 +349,12 @@ where
Ok(frame_system::Pallet::<System>::block_weight().total())
}

/// Execute all `OnRuntimeUpgrade` of this runtime, including the pre and post migration checks.
/// Execute all `OnRuntimeUpgrade` of this runtime.
///
/// Runs the try-state code both before and after the migration function if `checks` is set to
/// `true`. Also, if set to `true`, it runs the `pre_upgrade` and `post_upgrade` hooks.
/// The `checks` param determines whether to execute `pre/post_upgrade` and `try_state` hooks.
pub fn try_runtime_upgrade(
checks: frame_try_runtime::UpgradeCheckSelect,
) -> Result<Weight, TryRuntimeError> {
if checks.try_state() {
liamaharon marked this conversation as resolved.
Show resolved Hide resolved
let _guard = frame_support::StorageNoopGuard::default();
<AllPalletsWithSystem as frame_support::traits::TryState<
BlockNumberFor<System>,
>>::try_state(
frame_system::Pallet::<System>::block_number(),
frame_try_runtime::TryStateSelect::All,
)?;
}

let weight =
<(COnRuntimeUpgrade, AllPalletsWithSystem) as OnRuntimeUpgrade>::try_on_runtime_upgrade(
checks.pre_and_post(),
Expand Down
36 changes: 21 additions & 15 deletions substrate/frame/support/procedural/src/pallet/expand/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,6 @@ pub fn expand_hooks(def: &mut Def) -> proc_macro2::TokenStream {
}
};

let log_try_state = quote::quote! {
let pallet_name = <
<T as #frame_system::Config>::PalletInfo
as
#frame_support::traits::PalletInfo
>::name::<Self>().expect("No name found for the pallet! This usually means that the pallet wasn't added to `construct_runtime!`.");
#frame_support::__private::log::debug!(
target: #frame_support::LOG_TARGET,
"🩺 try-state pallet {:?}",
pallet_name,
);
};

let hooks_impl = if def.hooks.is_none() {
let frame_system = &def.frame_system;
quote::quote! {
Expand Down Expand Up @@ -271,12 +258,31 @@ pub fn expand_hooks(def: &mut Def) -> proc_macro2::TokenStream {
n: #frame_system::pallet_prelude::BlockNumberFor::<T>,
_s: #frame_support::traits::TryStateSelect
) -> Result<(), #frame_support::sp_runtime::TryRuntimeError> {
#log_try_state
let pallet_name = <
<T as #frame_system::Config>::PalletInfo
as
#frame_support::traits::PalletInfo
>::name::<Self>().expect("No name found for the pallet! This usually means that the pallet wasn't added to `construct_runtime!`.");

#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)
>::try_state(n).map_err(|err| {
#frame_support::__private::log::error!(
target: #frame_support::LOG_TARGET,
"❌ {:?} try_state checks failed: {:?}",
pallet_name,
err
);

err
})
}
}
)
Expand Down
24 changes: 21 additions & 3 deletions substrate/frame/support/src/traits/try_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,27 @@ impl<BlockNumber: Clone + sp_std::fmt::Debug + AtLeast32BitUnsigned> TryState<Bl
match targets {
Select::None => Ok(()),
Select::All => {
let mut result = Ok(());
for_tuples!( #( result = result.and(Tuple::try_state(n.clone(), targets.clone())); )* );
result
let mut error_count = 0;
for_tuples!(#(
if let Err(_) = Tuple::try_state(n.clone(), targets.clone()) {
error_count += 1;
}
)*);

if error_count > 0 {
log::error!(
target: "try-runtime",
"{} pallets exited with errors while executing try_state checks.",
error_count
);

return Err(
"Detected errors while executing try_state checks. See logs for more info."
.into(),
)
}

Ok(())
},
Select::RoundRobin(len) => {
let functions: &[fn(BlockNumber, Select) -> Result<(), TryRuntimeError>] =
Expand Down