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

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
slumber committed Jul 31, 2023
1 parent d70f655 commit 341d799
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 10 deletions.
54 changes: 54 additions & 0 deletions pallets/parachain-system/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,60 @@ fn inherent_processed_messages_are_ignored() {
});
}

#[test]
fn hrmp_outbound_respects_used_bandwidth() {
let recipient = ParaId::from(400);

CONSENSUS_HOOK.with(|c| {
*c.borrow_mut() = Box::new(|_| (Weight::zero(), NonZeroU32::new(3).unwrap().into()))
});

BlockTests::new()
.with_inclusion_delay(2)
.with_relay_sproof_builder(move |_, _, sproof| {
sproof.host_config.hrmp_max_message_num_per_candidate = 10;
let channel = sproof.upsert_outbound_channel(recipient);
channel.max_capacity = 2;
channel.max_total_size = 4;

channel.max_message_size = 10;
})
.add(1, || {})
.add_with_post_test(
2,
move || {
send_message(recipient, b"22".to_vec());
},
move || {
let v = HrmpOutboundMessages::<Test>::get();
assert_eq!(v, vec![OutboundHrmpMessage { recipient, data: b"22".to_vec() }]);
},
)
.add_with_post_test(
3,
move || {
send_message(recipient, b"333".to_vec());
},
move || {
// Parent has not been included, new message would've exceeded capacity.
let v = HrmpOutboundMessages::<Test>::get();
assert!(v.is_empty());
},
)
.add_with_post_test(
4,
move || {
send_message(recipient, b"a".to_vec());
send_message(recipient, b"b".to_vec());
},
move || {
let v = HrmpOutboundMessages::<Test>::get();
// One small message fits. This line relies on test implementation of [`XcmpMessageSource`].
assert_eq!(v, vec![OutboundHrmpMessage { recipient, data: b"a".to_vec() }]);
},
);
}

#[test]
fn events() {
BlockTests::new()
Expand Down
38 changes: 28 additions & 10 deletions test/relay-sproof-builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,34 @@ impl RelayStateSproofBuilder {
in_index.insert(idx, sender);
}

self.hrmp_channels
.entry(relay_chain::HrmpChannelId { sender, recipient: self.para_id })
.or_insert_with(|| AbridgedHrmpChannel {
max_capacity: 0,
max_total_size: 0,
max_message_size: 0,
msg_count: 0,
total_size: 0,
mqc_head: None,
})
self.upsert_channel(relay_chain::HrmpChannelId { sender, recipient: self.para_id })
}

/// Returns a mutable reference to HRMP channel metadata for a channel (`self.para_id`, `recipient`).
///
/// If there is no channel, a new default one is created.
///
/// It also updates the `hrmp_egress_channel_index`, creating it if needed.
pub fn upsert_outbound_channel(&mut self, recipient: ParaId) -> &mut AbridgedHrmpChannel {
let in_index = self.hrmp_egress_channel_index.get_or_insert_with(Vec::new);
if let Err(idx) = in_index.binary_search(&recipient) {
in_index.insert(idx, recipient);
}

self.upsert_channel(relay_chain::HrmpChannelId { sender: self.para_id, recipient })
}

/// Creates a new default entry in the hrmp channels mapping if not exists, and returns mutable
/// reference to it.
fn upsert_channel(&mut self, id: relay_chain::HrmpChannelId) -> &mut AbridgedHrmpChannel {
self.hrmp_channels.entry(id).or_insert_with(|| AbridgedHrmpChannel {
max_capacity: 0,
max_total_size: 0,
max_message_size: 0,
msg_count: 0,
total_size: 0,
mqc_head: None,
})
}

pub fn into_state_root_and_proof(
Expand Down

0 comments on commit 341d799

Please sign in to comment.