Skip to content

Commit 64fb9a6

Browse files
committed
Emit UMP signal from parachain runtime
1 parent 1c29484 commit 64fb9a6

File tree

2 files changed

+115
-23
lines changed

2 files changed

+115
-23
lines changed

cumulus/pallets/parachain-system/src/lib.rs

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@ use alloc::{collections::btree_map::BTreeMap, vec, vec::Vec};
3333
use codec::{Decode, DecodeLimit, Encode};
3434
use core::cmp;
3535
use cumulus_primitives_core::{
36-
relay_chain, AbridgedHostConfiguration, ChannelInfo, ChannelStatus, CollationInfo,
37-
CumulusDigestItem, GetChannelInfo, ListChannelInfos, MessageSendError, OutboundHrmpMessage,
38-
ParaId, PersistedValidationData, UpwardMessage, UpwardMessageSender, XcmpMessageHandler,
36+
relay_chain::{self, UMPSignal, UMP_SEPARATOR},
37+
AbridgedHostConfiguration, ChannelInfo, ChannelStatus, CollationInfo, CumulusDigestItem,
38+
GetChannelInfo, ListChannelInfos, MessageSendError, OutboundHrmpMessage, ParaId,
39+
PersistedValidationData, UpwardMessage, UpwardMessageSender, XcmpMessageHandler,
3940
XcmpMessageSource,
4041
};
4142
use cumulus_primitives_parachain_inherent::{v0, MessageQueueChain, ParachainInherentData};
@@ -361,8 +362,8 @@ pub mod pallet {
361362
UpwardMessages::<T>::put(&up[..num as usize]);
362363
*up = up.split_off(num as usize);
363364

364-
// Send the core selector UMP signal.
365-
Self::send_ump_signal();
365+
// Send the pending UMP signals + the core selector UMP signal.
366+
Self::send_ump_signals();
366367

367368
// If the total size of the pending messages is less than the threshold,
368369
// we decrease the fee factor, since the queue is less congested.
@@ -585,7 +586,7 @@ pub mod pallet {
585586
validation_data: vfp,
586587
relay_chain_state,
587588
relay_parent_descendants,
588-
collator_peer_id: _,
589+
collator_peer_id,
589590
} = data;
590591

591592
// Check that the associated relay chain block number is as expected.
@@ -693,6 +694,12 @@ pub mod pallet {
693694

694695
<T::OnSystemEvent as OnSystemEvent>::on_validation_data(&vfp);
695696

697+
if let Some(collator_peer_id) = collator_peer_id {
698+
PendingUpwardSignals::<T>::mutate(|signals| {
699+
signals.push(UMPSignal::ApprovedPeer(collator_peer_id).encode());
700+
});
701+
}
702+
696703
total_weight.saturating_accrue(Self::enqueue_inbound_downward_messages(
697704
relevant_messaging_state.dmq_mqc_head,
698705
inbound_messages_data.downward_messages,
@@ -905,14 +912,20 @@ pub mod pallet {
905912

906913
/// Upward messages that were sent in a block.
907914
///
908-
/// This will be cleared in `on_initialize` of each new block.
915+
/// This will be cleared in `on_initialize` for each new block.
909916
#[pallet::storage]
910917
pub type UpwardMessages<T: Config> = StorageValue<_, Vec<UpwardMessage>, ValueQuery>;
911918

912-
/// Upward messages that are still pending and not yet send to the relay chain.
919+
/// Upward messages that are still pending and not yet sent to the relay chain.
913920
#[pallet::storage]
914921
pub type PendingUpwardMessages<T: Config> = StorageValue<_, Vec<UpwardMessage>, ValueQuery>;
915922

923+
/// Upward signals that are still pending and not yet sent to the relay chain.
924+
///
925+
/// This will be cleared in `on_finalize` for each block.
926+
#[pallet::storage]
927+
pub type PendingUpwardSignals<T: Config> = StorageValue<_, Vec<UpwardMessage>, ValueQuery>;
928+
916929
/// The factor to multiply the base delivery fee by for UMP.
917930
#[pallet::storage]
918931
pub type UpwardDeliveryFeeFactor<T: Config> =
@@ -1508,22 +1521,25 @@ impl<T: Config> Pallet<T> {
15081521
}
15091522

15101523
/// Send the ump signals
1511-
fn send_ump_signal() {
1512-
use cumulus_primitives_core::relay_chain::{UMPSignal, UMP_SEPARATOR};
1524+
fn send_ump_signals() {
1525+
// Take the pending UMP signals.
1526+
let mut ump_signals = PendingUpwardSignals::<T>::take();
1527+
// Append the core selector signal.
1528+
if let Some(core_info) =
1529+
CumulusDigestItem::find_core_info(&frame_system::Pallet::<T>::digest())
1530+
{
1531+
ump_signals.push(
1532+
UMPSignal::SelectCore(core_info.selector, core_info.claim_queue_offset).encode(),
1533+
);
1534+
}
15131535

1514-
UpwardMessages::<T>::mutate(|up| {
1515-
if let Some(core_info) =
1516-
CumulusDigestItem::find_core_info(&frame_system::Pallet::<T>::digest())
1517-
{
1536+
// Send all the UMP signals.
1537+
if !ump_signals.is_empty() {
1538+
UpwardMessages::<T>::mutate(|up| {
15181539
up.push(UMP_SEPARATOR);
1519-
1520-
// Send the core selector signal.
1521-
up.push(
1522-
UMPSignal::SelectCore(core_info.selector, core_info.claim_queue_offset)
1523-
.encode(),
1524-
);
1525-
}
1526-
});
1540+
up.append(&mut ump_signals);
1541+
});
1542+
}
15271543
}
15281544

15291545
/// Open HRMP channel for using it in benchmarks or tests.

cumulus/pallets/parachain-system/src/tests.rs

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@
1919
use super::*;
2020
use crate::mock::*;
2121

22+
use alloc::collections::BTreeMap;
2223
use core::num::NonZeroU32;
23-
use cumulus_primitives_core::{AbridgedHrmpChannel, InboundDownwardMessage, InboundHrmpMessage};
24+
use cumulus_primitives_core::{
25+
relay_chain::ApprovedPeerId, AbridgedHrmpChannel, ClaimQueueOffset, CoreInfo, CoreSelector,
26+
InboundDownwardMessage, InboundHrmpMessage, CUMULUS_CONSENSUS_ID,
27+
};
2428
use cumulus_primitives_parachain_inherent::{
2529
v0, INHERENT_IDENTIFIER, PARACHAIN_INHERENT_IDENTIFIER_V0,
2630
};
@@ -31,6 +35,7 @@ use rand::Rng;
3135
use relay_chain::HrmpChannelId;
3236
use sp_core::H256;
3337
use sp_inherents::InherentDataProvider;
38+
use sp_runtime::DigestItem;
3439
use sp_trie::StorageProof;
3540

3641
#[test]
@@ -1655,3 +1660,74 @@ fn ump_fee_factor_increases_and_decreases() {
16551660
},
16561661
);
16571662
}
1663+
1664+
#[test]
1665+
fn ump_signals_are_sent_correctly() {
1666+
let core_info = CoreInfo {
1667+
selector: CoreSelector(1),
1668+
claim_queue_offset: ClaimQueueOffset(1),
1669+
number_of_cores: codec::Compact(1),
1670+
};
1671+
1672+
// Test cases list with the following format:
1673+
// `((expect_approved_peer, expect_select_core), expected_upward_messages)`
1674+
let test_cases = BTreeMap::from([
1675+
((false, false), vec![b"Test".to_vec()]),
1676+
(
1677+
(true, false),
1678+
vec![
1679+
b"Test".to_vec(),
1680+
UMP_SEPARATOR,
1681+
UMPSignal::ApprovedPeer(ApprovedPeerId::try_from(b"12345".to_vec()).unwrap())
1682+
.encode(),
1683+
],
1684+
),
1685+
(
1686+
(false, true),
1687+
vec![
1688+
b"Test".to_vec(),
1689+
UMP_SEPARATOR,
1690+
UMPSignal::SelectCore(core_info.selector, core_info.claim_queue_offset).encode(),
1691+
],
1692+
),
1693+
(
1694+
(true, true),
1695+
vec![
1696+
b"Test".to_vec(),
1697+
UMP_SEPARATOR,
1698+
UMPSignal::ApprovedPeer(ApprovedPeerId::try_from(b"12345".to_vec()).unwrap())
1699+
.encode(),
1700+
UMPSignal::SelectCore(core_info.selector, core_info.claim_queue_offset).encode(),
1701+
],
1702+
),
1703+
]);
1704+
1705+
for ((expect_approved_peer, expect_select_core), expected_upward_messages) in test_cases {
1706+
let core_info_digest = CumulusDigestItem::CoreInfo(core_info.clone()).encode();
1707+
1708+
BlockTests::new()
1709+
.with_inherent_data(move |_, _, data| {
1710+
if expect_approved_peer {
1711+
data.collator_peer_id =
1712+
Some(ApprovedPeerId::try_from(b"12345".to_vec()).unwrap());
1713+
}
1714+
})
1715+
.add_with_post_test(
1716+
1,
1717+
move || {
1718+
ParachainSystem::send_upward_message(b"Test".to_vec()).unwrap();
1719+
1720+
if expect_select_core {
1721+
System::deposit_log(DigestItem::PreRuntime(
1722+
CUMULUS_CONSENSUS_ID,
1723+
core_info_digest.clone(),
1724+
));
1725+
}
1726+
},
1727+
move || {
1728+
assert_eq!(PendingUpwardSignals::<Test>::get(), Vec::<Vec<u8>>::new());
1729+
assert_eq!(UpwardMessages::<Test>::get(), expected_upward_messages);
1730+
},
1731+
);
1732+
}
1733+
}

0 commit comments

Comments
 (0)