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 debugging by using #[track_caller] in system assert_last_event and assert_has_event #7142

Merged
merged 6 commits into from
Jan 25, 2025
Merged
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
24 changes: 21 additions & 3 deletions substrate/frame/system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2017,23 +2017,41 @@ impl<T: Config> Pallet<T> {
///
/// NOTE: Events not registered at the genesis block and quietly omitted.
#[cfg(any(feature = "std", feature = "runtime-benchmarks", test))]
#[track_caller]
pub fn assert_has_event(event: T::RuntimeEvent) {
let warn = if Self::block_number().is_zero() {
"WARNING: block number is zero, and events are not registered at block number zero.\n"
} else {
""
};

let events = Self::events();
assert!(
events.iter().any(|record| record.event == event),
"expected event {event:?} not found in events {events:?}",
"{warn}expected event {event:?} not found in events {events:?}",
);
}

/// Assert the last event equal to the given `event`.
///
/// NOTE: Events not registered at the genesis block and quietly omitted.
#[cfg(any(feature = "std", feature = "runtime-benchmarks", test))]
#[track_caller]
pub fn assert_last_event(event: T::RuntimeEvent) {
let last_event = Self::events().last().expect("events expected").event.clone();
let warn = if Self::block_number().is_zero() {
"WARNING: block number is zero, and events are not registered at block number zero.\n"
} else {
""
};

let last_event = Self::events()
.last()
.expect(&alloc::format!("{warn}events expected"))
.event
.clone();
assert_eq!(
last_event, event,
"expected event {event:?} is not equal to the last event {last_event:?}",
"{warn}expected event {event:?} is not equal to the last event {last_event:?}",
);
}

Expand Down
Loading