Skip to content

Commit

Permalink
Better handling of error in inherents logic. (paritytech#14521)
Browse files Browse the repository at this point in the history
* impl

* trigger CI

* Revert "trigger CI"

This reverts commit 9426361.

* Fix

* fix

* fix

* fix
  • Loading branch information
gui1117 authored and nathanwhit committed Jul 19, 2023
1 parent c488973 commit 1317a78
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
40 changes: 34 additions & 6 deletions frame/support/procedural/src/construct_runtime/expand/inherent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,37 @@ pub fn expand_outer_inherent(
use #scrate::inherent::{ProvideInherent, IsFatalError};
use #scrate::traits::{IsSubType, ExtrinsicCall};
use #scrate::sp_runtime::traits::Block as _;
use #scrate::_private::sp_inherents::Error;
use #scrate::log;

let mut result = #scrate::inherent::CheckInherentsResult::new();

// This handle assume we abort on the first fatal error.
fn handle_put_error_result(res: Result<(), Error>) {
const LOG_TARGET: &str = "runtime::inherent";
match res {
Ok(()) => (),
Err(Error::InherentDataExists(id)) =>
log::debug!(
target: LOG_TARGET,
"Some error already reported for inherent {:?}, new non fatal \
error is ignored",
id
),
Err(Error::FatalErrorReported) =>
log::error!(
target: LOG_TARGET,
"Fatal error already reported, unexpected considering there is \
only one fatal error",
),
Err(_) =>
log::error!(
target: LOG_TARGET,
"Unexpected error from `put_error` operation",
),
}
}

for xt in block.extrinsics() {
// Inherents are before any other extrinsics.
// And signed extrinsics are not inherents.
Expand All @@ -110,9 +138,9 @@ pub fn expand_outer_inherent(
if #pallet_names::is_inherent(call) {
is_inherent = true;
if let Err(e) = #pallet_names::check_inherent(call, self) {
result.put_error(
handle_put_error_result(result.put_error(
#pallet_names::INHERENT_IDENTIFIER, &e
).expect("There is only one fatal error; qed");
));
if e.is_fatal_error() {
return result;
}
Expand Down Expand Up @@ -153,19 +181,19 @@ pub fn expand_outer_inherent(
});

if !found {
result.put_error(
handle_put_error_result(result.put_error(
#pallet_names::INHERENT_IDENTIFIER, &e
).expect("There is only one fatal error; qed");
));
if e.is_fatal_error() {
return result;
}
}
},
Ok(None) => (),
Err(e) => {
result.put_error(
handle_put_error_result(result.put_error(
#pallet_names::INHERENT_IDENTIFIER, &e
).expect("There is only one fatal error; qed");
));
if e.is_fatal_error() {
return result;
}
Expand Down
6 changes: 6 additions & 0 deletions frame/support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1521,6 +1521,12 @@ pub mod tests {
}
}

/// Private module re-exporting items used by frame support macros.
#[doc(hidden)]
pub mod _private {
pub use sp_inherents;
}

/// Prelude to be used for pallet testing, for ease of use.
#[cfg(feature = "std")]
pub mod testing_prelude {
Expand Down

0 comments on commit 1317a78

Please sign in to comment.