Switch to relay_dispatch_queue_remaining_capacity (#2608)

* Switch to `relay_dispatch_queue_remaining_capacity`

This switches the parachain runtimes to use `relay_dispatch_queue_remaining_capacity` when possible.
If the data is not yet available on the relay chain it falls back to `relay_dispatch_queue_size`. It
will require that all parachains migrate to `relay_dispatch_queue_remaining_capacity` before we can
start removing the call to `relay_dipatch_queue_size`.

Besides that the pr adapts the xcm exumulator to make it work with the message queue.

* Fix test and use correct types

* ".git/.scripts/commands/fmt/fmt.sh"

---------

Co-authored-by: command-bot <>
This commit is contained in:
Bastian Köcher
2023-05-21 22:23:18 +02:00
committed by GitHub
parent 5ca30a3c4f
commit 3a20b3d702
9 changed files with 424 additions and 327 deletions
+5 -5
View File
@@ -233,13 +233,13 @@ pub mod pallet {
};
<PendingUpwardMessages<T>>::mutate(|up| {
let (count, size) = relevant_messaging_state.relay_dispatch_queue_size;
let queue_size = relevant_messaging_state.relay_dispatch_queue_size;
let available_capacity = cmp::min(
host_config.max_upward_queue_count.saturating_sub(count),
host_config.max_upward_message_num_per_candidate,
queue_size.remaining_count,
host_config.max_upward_message_num_per_candidate.into(),
);
let available_size = host_config.max_upward_queue_size.saturating_sub(size);
let available_size = queue_size.remaining_size;
// Count the number of messages we can possibly fit in the given constraints, i.e.
// available_capacity and available_size.
@@ -431,7 +431,7 @@ pub mod pallet {
.read_abridged_host_configuration()
.expect("Invalid host configuration in relay chain state proof");
let relevant_messaging_state = relay_state_proof
.read_messaging_state_snapshot()
.read_messaging_state_snapshot(&host_config)
.expect("Invalid messaging state in relay chain state proof");
<ValidationData<T>>::put(&vfp);
@@ -24,6 +24,17 @@ use sp_state_machine::{Backend, TrieBackend, TrieBackendBuilder};
use sp_std::vec::Vec;
use sp_trie::{HashDBT, MemoryDB, StorageProof, EMPTY_PREFIX};
/// The capacity of the upward message queue of a parachain on the relay chain.
// The field order should stay the same as the data can be found in the proof to ensure both are
// have the same encoded representation.
#[derive(Clone, Encode, Decode, TypeInfo, Default)]
pub struct RelayDispachQueueSize {
/// The number of additional messages that can be enqueued.
pub remaining_count: u32,
/// The total size of additional messages that can be enqueued.
pub remaining_size: u32,
}
/// A snapshot of some messaging related state of relay chain pertaining to the current parachain.
///
/// This data is essential for making sure that the parachain is aware of current resource use on
@@ -37,10 +48,7 @@ pub struct MessagingStateSnapshot {
pub dmq_mqc_head: relay_chain::Hash,
/// The current capacity of the upward message queue of the current parachain on the relay chain.
///
/// The capacity is represented by a tuple that consist of the `count` of the messages and the
/// `total_size` expressed as the sum of byte sizes of all messages in the queue.
pub relay_dispatch_queue_size: (u32, u32),
pub relay_dispatch_queue_size: RelayDispachQueueSize,
/// Information about all the inbound HRMP channels.
///
@@ -164,7 +172,10 @@ impl RelayChainStateProof {
/// Read the [`MessagingStateSnapshot`] from the relay chain state proof.
///
/// Returns an error if anything failed at reading or decoding.
pub fn read_messaging_state_snapshot(&self) -> Result<MessagingStateSnapshot, Error> {
pub fn read_messaging_state_snapshot(
&self,
host_config: &AbridgedHostConfiguration,
) -> Result<MessagingStateSnapshot, Error> {
let dmq_mqc_head: relay_chain::Hash = read_entry(
&self.trie_backend,
&relay_chain::well_known_keys::dmq_mqc_head(self.para_id),
@@ -172,12 +183,35 @@ impl RelayChainStateProof {
)
.map_err(Error::DmqMqcHead)?;
let relay_dispatch_queue_size: (u32, u32) = read_entry(
let relay_dispatch_queue_size = read_optional_entry::<RelayDispachQueueSize, _>(
&self.trie_backend,
&relay_chain::well_known_keys::relay_dispatch_queue_size(self.para_id),
Some((0, 0)),
)
.map_err(Error::RelayDispatchQueueSize)?;
&relay_chain::well_known_keys::relay_dispatch_queue_remaining_capacity(self.para_id)
.key,
);
// TODO paritytech/polkadot#6283: Remove all usages of `relay_dispatch_queue_size`
//
// When the relay chain and all parachains support `relay_dispatch_queue_remaining_capacity`,
// this code here needs to be removed and above needs to be changed to `read_entry` that
// returns an error if `relay_dispatch_queue_remaining_capacity` can not be found/decoded.
//
// For now we just fallback to the old dispatch queue size if there is an error.
let relay_dispatch_queue_size = match relay_dispatch_queue_size {
Ok(Some(r)) => r,
_ => {
let res = read_entry::<(u32, u32), _>(
&self.trie_backend,
#[allow(deprecated)]
&relay_chain::well_known_keys::relay_dispatch_queue_size(self.para_id),
Some((0, 0)),
)
.map_err(Error::RelayDispatchQueueSize)?;
let remaining_count = host_config.max_upward_queue_count.saturating_sub(res.0);
let remaining_size = host_config.max_upward_queue_size.saturating_sub(res.1);
RelayDispachQueueSize { remaining_count, remaining_size }
},
};
let ingress_channel_index: Vec<ParaId> = read_entry(
&self.trie_backend,
@@ -513,7 +513,7 @@ fn send_upward_message_num_per_candidate() {
BlockTests::new()
.with_relay_sproof_builder(|_, _, sproof| {
sproof.host_config.max_upward_message_num_per_candidate = 1;
sproof.relay_dispatch_queue_size = None;
sproof.relay_dispatch_queue_remaining_capacity = None;
})
.add_with_post_test(
1,
@@ -544,8 +544,8 @@ fn send_upward_message_relay_bottleneck() {
sproof.host_config.max_upward_queue_count = 5;
match relay_block_num {
1 => sproof.relay_dispatch_queue_size = Some((5, 0)),
2 => sproof.relay_dispatch_queue_size = Some((4, 0)),
1 => sproof.relay_dispatch_queue_remaining_capacity = Some((0, 2048)),
2 => sproof.relay_dispatch_queue_remaining_capacity = Some((1, 2048)),
_ => unreachable!(),
}
})