Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
debug assert events at genesis (#13217)
Browse files Browse the repository at this point in the history
  • Loading branch information
muharem authored Jan 23, 2023
1 parent 419b840 commit 52d72b3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
14 changes: 14 additions & 0 deletions frame/system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1247,6 +1247,8 @@ impl<T: Config> Pallet<T> {
}

/// Deposits an event into this block's event record.
///
/// NOTE: Events not registered at the genesis block and quietly omitted.
pub fn deposit_event(event: impl Into<T::RuntimeEvent>) {
Self::deposit_event_indexed(&[], event.into());
}
Expand All @@ -1256,6 +1258,8 @@ impl<T: Config> Pallet<T> {
///
/// This will update storage entries that correspond to the specified topics.
/// It is expected that light-clients could subscribe to this topics.
///
/// NOTE: Events not registered at the genesis block and quietly omitted.
pub fn deposit_event_indexed(topics: &[T::Hash], event: T::RuntimeEvent) {
let block_number = Self::block_number();
// Don't populate events on genesis.
Expand Down Expand Up @@ -1445,8 +1449,14 @@ impl<T: Config> Pallet<T> {
/// NOTE: This should only be used in tests. Reading events from the runtime can have a large
/// impact on the PoV size of a block. Users should use alternative and well bounded storage
/// items for any behavior like this.
///
/// NOTE: Events not registered at the genesis block and quietly omitted.
#[cfg(any(feature = "std", feature = "runtime-benchmarks", test))]
pub fn events() -> Vec<EventRecord<T::RuntimeEvent, T::Hash>> {
debug_assert!(
!Self::block_number().is_zero(),
"events not registered at the genesis block"
);
// Dereferencing the events here is fine since we are not in the
// memory-restricted runtime.
Self::read_events_no_consensus().map(|e| *e).collect()
Expand Down Expand Up @@ -1501,6 +1511,8 @@ impl<T: Config> Pallet<T> {
}

/// Assert the given `event` exists.
///
/// NOTE: Events not registered at the genesis block and quietly omitted.
#[cfg(any(feature = "std", feature = "runtime-benchmarks", test))]
pub fn assert_has_event(event: T::RuntimeEvent) {
let events = Self::events();
Expand All @@ -1511,6 +1523,8 @@ impl<T: Config> Pallet<T> {
}

/// 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))]
pub fn assert_last_event(event: T::RuntimeEvent) {
let last_event = Self::events().last().expect("events expected").event.clone();
Expand Down
3 changes: 2 additions & 1 deletion frame/system/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,8 @@ fn events_not_emitted_during_genesis() {
assert!(System::block_number().is_zero());
let mut account_data = AccountInfo::default();
System::on_created_account(Default::default(), &mut account_data);
assert!(System::events().is_empty());
// No events registered at the genesis block
assert!(!System::read_events_no_consensus().any(|_| true));
// Events will be emitted starting on block 1
System::set_block_number(1);
System::on_created_account(Default::default(), &mut account_data);
Expand Down

0 comments on commit 52d72b3

Please sign in to comment.