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

[HRMP] Dont partially modify pages #4710

Merged
merged 5 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
13 changes: 7 additions & 6 deletions cumulus/pallets/xcmp-queue/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ impl<T: Config> Pallet<T> {
let channel_info =
T::ChannelInfo::get_channel_info(recipient).ok_or(MessageSendError::NoChannel)?;
// Max message size refers to aggregates, or pages. Not to individual fragments.
let max_message_size = channel_info.max_message_size as usize;
let max_message_size = channel_info.max_message_size.min(T::MaxPageSize::get()) as usize;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let max_message_size = channel_info.max_message_size.min(T::MaxPageSize::get()) as usize;
let max_message_size = channel_info.max_message_size.min.saturating_add(XcmpMessageFormat::max_encoded_len())(T::MaxPageSize::get()) as usize;

The XcmpMessageFormat is part of the page. So, if we don't do this, we will never be able to storage a message of max_message_size (when this is smaller than T::MaxPageSize).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but i think this is a hard limit from the relay chain that we cannot go over. So the "max message size" is for a HRMP message - not for the XCM inside of it.

let format_size = format.encoded_size();
// We check the encoded fragment length plus the format size against the max message size
// because the format is concatenated if a new page is needed.
Expand Down Expand Up @@ -522,7 +522,7 @@ impl<T: Config> Pallet<T> {
// We return the size of the last page inside of the option, to not calculate it again.
let appended_to_last_page = have_active
.then(|| {
<OutboundXcmpMessages<T>>::mutate(
<OutboundXcmpMessages<T>>::try_mutate(
recipient,
channel_details.last_index - 1,
|page| {
Expand All @@ -532,17 +532,18 @@ impl<T: Config> Pallet<T> {
) != Ok(format)
{
defensive!("Bad format in outbound queue; dropping message");
return None
return Err(())
}
if page.len() + encoded_fragment.len() > max_message_size {
return None
return Err(())
}
for frag in encoded_fragment.iter() {
page.try_push(*frag).ok()?;
page.try_push(*frag)?;
}
Some(page.len())
Ok(page.len())
},
)
.ok()
})
.flatten();

Expand Down
31 changes: 30 additions & 1 deletion cumulus/pallets/xcmp-queue/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use frame_support::{
use mock::{new_test_ext, ParachainSystem, RuntimeOrigin as Origin, Test, XcmpQueue};
use sp_runtime::traits::{BadOrigin, Zero};
use std::iter::{once, repeat};
use xcm_builder::InspectMessageQueues;

#[test]
fn empty_concatenated_works() {
Expand Down Expand Up @@ -854,7 +855,6 @@ fn verify_fee_factor_increase_and_decrease() {
#[test]
fn get_messages_works() {
new_test_ext().execute_with(|| {
use xcm_builder::InspectMessageQueues;
let sibling_para_id = ParaId::from(2001);
ParachainSystem::open_outbound_hrmp_channel_for_benchmarks_or_tests(sibling_para_id);
let destination: Location = (Parent, Parachain(sibling_para_id.into())).into();
Expand Down Expand Up @@ -890,3 +890,32 @@ fn get_messages_works() {
);
});
}

/// We try to send a fragment that will not fit into the currently active page. This should
/// therefore not modify the current page but instead create a new one.
#[test]
fn page_not_modified_when_fragment_does_not_fit() {
new_test_ext().execute_with(|| {
let sibling = ParaId::from(2001);
ParachainSystem::open_outbound_hrmp_channel_for_benchmarks_or_tests(sibling);

let destination: Location = (Parent, Parachain(sibling.into())).into();
let message = Xcm(vec![ClearOrigin; 600]);

loop {
let old_page_zero = OutboundXcmpMessages::<Test>::get(sibling, 0);
assert_ok!(send_xcm::<XcmpQueue>(destination.clone(), message.clone()));

// If a new page was created by this send_xcm call, then page_zero was not also
// modified:
let num_pages = OutboundXcmpMessages::<Test>::iter_prefix(sibling).count();
if num_pages == 2 {
let new_page_zero = OutboundXcmpMessages::<Test>::get(sibling, 0);
assert_eq!(old_page_zero, new_page_zero);
break
} else if num_pages > 2 {
panic!("Too many pages created");
}
}
});
}
11 changes: 11 additions & 0 deletions prdoc/pr_4710.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
title: "Dont partially modify HRMP pages"

doc:
- audience: Runtime Dev
description: |
The xcmp-queue pallet now does not partially modify a page anymore when the next message does
not fully fit into it but instead cleanly creates a new one.

crates:
- name: cumulus-pallet-xcmp-queue
bump: patch
Loading