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

Simplify event assertion with predicate-based check #7734

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
cdcb88d
Simplify event assertion with predicate-based check
raymondkfcheung Feb 26, 2025
ea79034
Merge branch 'master' into ray-add-events-contains
raymondkfcheung Feb 26, 2025
6e22c8d
Remove unused import
raymondkfcheung Feb 26, 2025
7608e91
Update from raymondkfcheung running command 'prdoc --audience runtime…
github-actions[bot] Feb 26, 2025
4a39cbf
Fix tests
raymondkfcheung Feb 26, 2025
019ab2d
Merge remote-tracking branch 'origin/ray-add-events-contains' into ra…
raymondkfcheung Feb 26, 2025
be54e6c
Merge branch 'master' into ray-add-events-contains
raymondkfcheung Feb 26, 2025
a49f95a
Update PR Doc
raymondkfcheung Feb 26, 2025
7f548a8
Update PR Doc
raymondkfcheung Feb 26, 2025
0c4211f
Update PR Doc
raymondkfcheung Feb 26, 2025
37b4c8d
Update from raymondkfcheung running command 'fmt'
github-actions[bot] Feb 26, 2025
9d62059
Update substrate/frame/system/src/lib.rs
raymondkfcheung Feb 27, 2025
ba65697
Merge branch 'master' into ray-add-events-contains
raymondkfcheung Feb 27, 2025
50bcfa8
Update PR Doc
raymondkfcheung Feb 27, 2025
2da26e1
Merge branch 'master' into ray-add-events-contains
raymondkfcheung Feb 27, 2025
3633bd4
Delete contains_event from frame
raymondkfcheung Feb 27, 2025
71a290a
Merge branch 'master' into ray-add-events-contains
raymondkfcheung Feb 27, 2025
8012fab
Rename to relay_chain_has_xcm_event
raymondkfcheung Feb 27, 2025
89d89f8
Change to macro
raymondkfcheung Feb 27, 2025
ab9a3d5
Simplify macro
raymondkfcheung Feb 27, 2025
3120460
Merge branch 'master' into ray-add-events-contains
raymondkfcheung Feb 27, 2025
f485e89
Rename to runtime
raymondkfcheung Feb 27, 2025
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
70 changes: 26 additions & 44 deletions polkadot/xcm/xcm-simulator/example/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,21 @@ fn buy_execution<C>(fees: impl Into<Asset>) -> Instruction<C> {
BuyExecution { fees: fees.into(), weight_limit: Unlimited }
}

/// Helper macro to check if a system event exists in the event list.
///
/// Example usage:
/// ```ignore
/// assert!(system_contains_event!(parachain, System(frame_system::Event::Remarked { .. })));
/// assert!(system_contains_event!(relay_chain, XcmPallet(pallet_xcm::Event::Attempted { .. })));
/// ```
macro_rules! system_contains_event {
($runtime:ident, $variant:ident($($pattern:tt)*)) => {
$runtime::System::events().iter().any(|e| {
matches!(e.event, $runtime::RuntimeEvent::$variant($($pattern)*))
})
};
}

#[test]
fn remote_account_ids_work() {
child_account_account_id(1, ALICE);
Expand Down Expand Up @@ -53,11 +68,7 @@ fn dmp() {
});

ParaA::execute_with(|| {
use parachain::{RuntimeEvent, System};
assert!(System::events().iter().any(|r| matches!(
r.event,
RuntimeEvent::System(frame_system::Event::Remarked { .. })
)));
assert!(system_contains_event!(parachain, System(frame_system::Event::Remarked { .. })));
});
}

Expand All @@ -81,11 +92,7 @@ fn ump() {
});

Relay::execute_with(|| {
use relay_chain::{RuntimeEvent, System};
assert!(System::events().iter().any(|r| matches!(
r.event,
RuntimeEvent::System(frame_system::Event::Remarked { .. })
)));
assert!(system_contains_event!(relay_chain, System(frame_system::Event::Remarked { .. })));
});
}

Expand All @@ -109,11 +116,7 @@ fn xcmp() {
});

ParaB::execute_with(|| {
use parachain::{RuntimeEvent, System};
assert!(System::events().iter().any(|r| matches!(
r.event,
RuntimeEvent::System(frame_system::Event::Remarked { .. })
)));
assert!(system_contains_event!(parachain, System(frame_system::Event::Remarked { .. })));
});
}

Expand All @@ -137,18 +140,12 @@ fn reserve_transfer() {
INITIAL_BALANCE + withdraw_amount
);
// Ensure expected events were emitted
let events = relay_chain::System::events();
let attempted_count = count_relay_chain_events(&events, |event| {
matches!(
event,
relay_chain::RuntimeEvent::XcmPallet(pallet_xcm::Event::Attempted { .. })
)
});
let sent_count = count_relay_chain_events(&events, |event| {
matches!(event, relay_chain::RuntimeEvent::XcmPallet(pallet_xcm::Event::Sent { .. }))
});
assert_eq!(attempted_count, 1, "Expected one XcmPallet::Attempted event");
assert_eq!(sent_count, 1, "Expected one XcmPallet::Sent event");
let attempted_emitted =
system_contains_event!(relay_chain, XcmPallet(pallet_xcm::Event::Attempted { .. }));
let sent_emitted =
system_contains_event!(relay_chain, XcmPallet(pallet_xcm::Event::Sent { .. }));
assert!(attempted_emitted, "Expected XcmPallet::Attempted event emitted");
assert!(sent_emitted, "Expected XcmPallet::Sent event emitted");
});

ParaA::execute_with(|| {
Expand Down Expand Up @@ -193,13 +190,8 @@ fn reserve_transfer_with_error() {
assert!(log_capture.contains("XCM validate_send failed"));

// Verify that XcmPallet::Attempted was NOT emitted (rollback happened)
let events = relay_chain::System::events();
let xcm_attempted_emitted = events.iter().any(|e| {
matches!(
e.event,
relay_chain::RuntimeEvent::XcmPallet(pallet_xcm::Event::Attempted { .. })
)
});
let xcm_attempted_emitted =
system_contains_event!(relay_chain, XcmPallet(pallet_xcm::Event::Attempted { .. }));
assert!(
!xcm_attempted_emitted,
"Expected no XcmPallet::Attempted event due to rollback, but it was emitted"
Expand Down Expand Up @@ -580,13 +572,3 @@ fn query_holding() {
);
});
}

fn count_relay_chain_events<F>(
events: &[frame_system::EventRecord<relay_chain::RuntimeEvent, sp_core::H256>],
predicate: F,
) -> usize
where
F: Fn(&relay_chain::RuntimeEvent) -> bool,
{
events.iter().filter(|e| predicate(&e.event)).count()
}
8 changes: 8 additions & 0 deletions prdoc/pr_7734.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
title: Simplify event assertion with predicate-based check
doc:
- audience: Runtime Dev
description: |-
Simplify event assertions by introducing `contains_event`, reducing duplicated code.
crates:
- name: xcm-simulator-example
bump: patch
Loading