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

Commit 24082a5

Browse files
slumberrphmeier
authored andcommitted
add test
1 parent 34499d5 commit 24082a5

File tree

2 files changed

+82
-10
lines changed
  • pallets/parachain-system/src
  • test/relay-sproof-builder/src

2 files changed

+82
-10
lines changed

pallets/parachain-system/src/tests.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,60 @@ fn inherent_processed_messages_are_ignored() {
687687
});
688688
}
689689

690+
#[test]
691+
fn hrmp_outbound_respects_used_bandwidth() {
692+
let recipient = ParaId::from(400);
693+
694+
CONSENSUS_HOOK.with(|c| {
695+
*c.borrow_mut() = Box::new(|_| (Weight::zero(), NonZeroU32::new(3).unwrap().into()))
696+
});
697+
698+
BlockTests::new()
699+
.with_inclusion_delay(2)
700+
.with_relay_sproof_builder(move |_, _, sproof| {
701+
sproof.host_config.hrmp_max_message_num_per_candidate = 10;
702+
let channel = sproof.upsert_outbound_channel(recipient);
703+
channel.max_capacity = 2;
704+
channel.max_total_size = 4;
705+
706+
channel.max_message_size = 10;
707+
})
708+
.add(1, || {})
709+
.add_with_post_test(
710+
2,
711+
move || {
712+
send_message(recipient, b"22".to_vec());
713+
},
714+
move || {
715+
let v = HrmpOutboundMessages::<Test>::get();
716+
assert_eq!(v, vec![OutboundHrmpMessage { recipient, data: b"22".to_vec() }]);
717+
},
718+
)
719+
.add_with_post_test(
720+
3,
721+
move || {
722+
send_message(recipient, b"333".to_vec());
723+
},
724+
move || {
725+
// Parent has not been included, new message would've exceeded capacity.
726+
let v = HrmpOutboundMessages::<Test>::get();
727+
assert!(v.is_empty());
728+
},
729+
)
730+
.add_with_post_test(
731+
4,
732+
move || {
733+
send_message(recipient, b"a".to_vec());
734+
send_message(recipient, b"b".to_vec());
735+
},
736+
move || {
737+
let v = HrmpOutboundMessages::<Test>::get();
738+
// One small message fits. This line relies on test implementation of [`XcmpMessageSource`].
739+
assert_eq!(v, vec![OutboundHrmpMessage { recipient, data: b"a".to_vec() }]);
740+
},
741+
);
742+
}
743+
690744
#[test]
691745
fn events() {
692746
BlockTests::new()

test/relay-sproof-builder/src/lib.rs

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,34 @@ impl RelayStateSproofBuilder {
9191
in_index.insert(idx, sender);
9292
}
9393

94-
self.hrmp_channels
95-
.entry(relay_chain::HrmpChannelId { sender, recipient: self.para_id })
96-
.or_insert_with(|| AbridgedHrmpChannel {
97-
max_capacity: 0,
98-
max_total_size: 0,
99-
max_message_size: 0,
100-
msg_count: 0,
101-
total_size: 0,
102-
mqc_head: None,
103-
})
94+
self.upsert_channel(relay_chain::HrmpChannelId { sender, recipient: self.para_id })
95+
}
96+
97+
/// Returns a mutable reference to HRMP channel metadata for a channel (`self.para_id`, `recipient`).
98+
///
99+
/// If there is no channel, a new default one is created.
100+
///
101+
/// It also updates the `hrmp_egress_channel_index`, creating it if needed.
102+
pub fn upsert_outbound_channel(&mut self, recipient: ParaId) -> &mut AbridgedHrmpChannel {
103+
let in_index = self.hrmp_egress_channel_index.get_or_insert_with(Vec::new);
104+
if let Err(idx) = in_index.binary_search(&recipient) {
105+
in_index.insert(idx, recipient);
106+
}
107+
108+
self.upsert_channel(relay_chain::HrmpChannelId { sender: self.para_id, recipient })
109+
}
110+
111+
/// Creates a new default entry in the hrmp channels mapping if not exists, and returns mutable
112+
/// reference to it.
113+
fn upsert_channel(&mut self, id: relay_chain::HrmpChannelId) -> &mut AbridgedHrmpChannel {
114+
self.hrmp_channels.entry(id).or_insert_with(|| AbridgedHrmpChannel {
115+
max_capacity: 0,
116+
max_total_size: 0,
117+
max_message_size: 0,
118+
msg_count: 0,
119+
total_size: 0,
120+
mqc_head: None,
121+
})
104122
}
105123

106124
pub fn into_state_root_and_proof(

0 commit comments

Comments
 (0)