Skip to content

Commit

Permalink
pallet_inclusion: allow using generic AggregateMessageOrigin
Browse files Browse the repository at this point in the history
  • Loading branch information
tmpolaczyk authored and Aleksandar Brayanov committed Nov 14, 2024
1 parent 8d25f84 commit 51b247a
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 14 deletions.
25 changes: 14 additions & 11 deletions polkadot/runtime/parachains/src/inclusion/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
//! It is responsible for carrying candidates from being backable to being backed, and then from
//! backed to included.
use codec::FullCodec;
use sp_runtime::traits::{Convert, Debug};
use crate::{
configuration::{self, HostConfiguration},
disputes, dmp, hrmp,
Expand Down Expand Up @@ -268,7 +270,7 @@ impl From<u32> for AggregateMessageOrigin {

/// The maximal length of a UMP message.
pub type MaxUmpMessageLenOf<T> =
<<T as Config>::MessageQueue as EnqueueMessage<AggregateMessageOrigin>>::MaxMessageLen;
<<T as Config>::MessageQueue as EnqueueMessage<<T as Config>::AggregateMessageOrigin>>::MaxMessageLen;

#[frame_support::pallet]
pub mod pallet {
Expand All @@ -294,12 +296,15 @@ pub mod pallet {
type DisputesHandler: disputes::DisputesHandler<BlockNumberFor<Self>>;
type RewardValidators: RewardValidators;

type AggregateMessageOrigin: FullCodec + MaxEncodedLen + Clone + Eq + PartialEq + TypeInfo + Debug;
type GetAggregateMessageOrigin: Convert<UmpQueueId, Self::AggregateMessageOrigin>;
type GetParaFromAggregateMessageOrigin: Convert<Self::AggregateMessageOrigin, ParaId>;
/// The system message queue.
///
/// The message queue provides general queueing and processing functionality. Currently it
/// replaces the old `UMP` dispatch queue. Other use-cases can be implemented as well by
/// adding new variants to `AggregateMessageOrigin`.
type MessageQueue: EnqueueMessage<AggregateMessageOrigin>;
type MessageQueue: EnqueueMessage<Self::AggregateMessageOrigin>;

/// Weight info for the calls of this pallet.
type WeightInfo: WeightInfo;
Expand Down Expand Up @@ -496,7 +501,7 @@ impl<T: Config> Pallet<T> {
}

pub(crate) fn cleanup_outgoing_ump_dispatch_queue(para: ParaId) {
T::MessageQueue::sweep_queue(AggregateMessageOrigin::Ump(UmpQueueId::Para(para)));
T::MessageQueue::sweep_queue(T::GetAggregateMessageOrigin::convert(UmpQueueId::Para(para)));
}

/// Extract the freed cores based on cores that became available.
Expand Down Expand Up @@ -921,7 +926,7 @@ impl<T: Config> Pallet<T> {
}

pub(crate) fn relay_dispatch_queue_size(para_id: ParaId) -> (u32, u32) {
let fp = T::MessageQueue::footprint(AggregateMessageOrigin::Ump(UmpQueueId::Para(para_id)));
let fp = T::MessageQueue::footprint(T::GetAggregateMessageOrigin::convert(UmpQueueId::Para(para_id)));
(fp.storage.count as u32, fp.storage.size as u32)
}

Expand Down Expand Up @@ -1009,7 +1014,7 @@ impl<T: Config> Pallet<T> {

T::MessageQueue::enqueue_messages(
messages.into_iter(),
AggregateMessageOrigin::Ump(UmpQueueId::Para(para)),
T::GetAggregateMessageOrigin::convert(UmpQueueId::Para(para)),
);
Self::deposit_event(Event::UpwardMessagesReceived { from: para, count });
}
Expand Down Expand Up @@ -1205,12 +1210,10 @@ impl AcceptanceCheckErr {
}
}

impl<T: Config> OnQueueChanged<AggregateMessageOrigin> for Pallet<T> {
impl<T: Config> OnQueueChanged<T::AggregateMessageOrigin> for Pallet<T> {
// Write back the remaining queue capacity into `relay_dispatch_queue_remaining_capacity`.
fn on_queue_changed(origin: AggregateMessageOrigin, fp: QueueFootprint) {
let para = match origin {
AggregateMessageOrigin::Ump(UmpQueueId::Para(p)) => p,
};
fn on_queue_changed(origin: T::AggregateMessageOrigin, fp: QueueFootprint) {
let para = T::GetParaFromAggregateMessageOrigin::convert(origin);
let QueueFootprint { storage: Footprint { count, size }, .. } = fp;
let (count, size) = (count.saturated_into(), size.saturated_into());
// TODO paritytech/polkadot#6283: Remove all usages of `relay_dispatch_queue_size`
Expand Down Expand Up @@ -1413,6 +1416,6 @@ impl<T: Config> QueueFootprinter for Pallet<T> {
type Origin = UmpQueueId;

fn message_count(origin: Self::Origin) -> u64 {
T::MessageQueue::footprint(AggregateMessageOrigin::Ump(origin)).storage.count
T::MessageQueue::footprint(T::GetAggregateMessageOrigin::convert(origin)).storage.count
}
}
24 changes: 23 additions & 1 deletion polkadot/runtime/rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ use sp_runtime::{
traits::{
AccountIdConversion, BlakeTwo256, Block as BlockT, ConstU32, ConvertInto,
Extrinsic as ExtrinsicT, IdentityLookup, Keccak256, OpaqueKeys, SaturatedConversion,
Verify,
Verify, Convert
},
transaction_validity::{TransactionPriority, TransactionSource, TransactionValidity},
ApplyExtrinsicResult, FixedU128, KeyTypeId, Perbill, Percent, Permill, RuntimeDebug,
Expand Down Expand Up @@ -958,10 +958,32 @@ impl polkadot_runtime_parachains::inclusion::RewardValidators for RewardValidato
fn reward_bitfields(_: impl IntoIterator<Item = ValidatorIndex>) {}
}


pub struct GetAggregateMessageOrigin;

impl Convert<UmpQueueId, AggregateMessageOrigin> for GetAggregateMessageOrigin {
fn convert(queue_id: UmpQueueId) -> AggregateMessageOrigin {
AggregateMessageOrigin::Ump(queue_id)
}
}

pub struct GetParaFromAggregateMessageOrigin;

impl Convert<AggregateMessageOrigin, ParaId> for GetParaFromAggregateMessageOrigin {
fn convert(x: AggregateMessageOrigin) -> ParaId {
match x {
AggregateMessageOrigin::Ump(UmpQueueId::Para(para_id)) => para_id,
}
}
}

impl parachains_inclusion::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type DisputesHandler = ParasDisputes;
type RewardValidators = RewardValidators;
type AggregateMessageOrigin = AggregateMessageOrigin;
type GetAggregateMessageOrigin = GetAggregateMessageOrigin;
type GetParaFromAggregateMessageOrigin = GetParaFromAggregateMessageOrigin;
type MessageQueue = MessageQueue;
type WeightInfo = weights::polkadot_runtime_parachains_inclusion::WeightInfo<Runtime>;
}
Expand Down
25 changes: 24 additions & 1 deletion polkadot/runtime/test-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ use alloc::{
use codec::Encode;
use pallet_transaction_payment::FungibleAdapter;

use polkadot_runtime_parachains::inclusion::AggregateMessageOrigin;
use polkadot_runtime_parachains::inclusion::UmpQueueId;
use polkadot_runtime_parachains::{
assigner_parachains as parachains_assigner_parachains,
configuration as parachains_configuration,
Expand Down Expand Up @@ -81,7 +83,7 @@ use sp_runtime::{
generic, impl_opaque_keys,
traits::{
BlakeTwo256, Block as BlockT, ConvertInto, Extrinsic as ExtrinsicT, OpaqueKeys,
SaturatedConversion, StaticLookup, Verify,
SaturatedConversion, StaticLookup, Verify, Convert
},
transaction_validity::{TransactionPriority, TransactionSource, TransactionValidity},
ApplyExtrinsicResult, FixedU128, KeyTypeId, Perbill, Percent,
Expand Down Expand Up @@ -491,10 +493,31 @@ impl parachains_shared::Config for Runtime {
type DisabledValidators = Session;
}

pub struct GetAggregateMessageOrigin;

impl Convert<UmpQueueId, AggregateMessageOrigin> for GetAggregateMessageOrigin {
fn convert(queue_id: UmpQueueId) -> AggregateMessageOrigin {
AggregateMessageOrigin::Ump(queue_id)
}
}

pub struct GetParaFromAggregateMessageOrigin;

impl Convert<AggregateMessageOrigin, ParaId> for GetParaFromAggregateMessageOrigin {
fn convert(x: AggregateMessageOrigin) -> ParaId {
match x {
AggregateMessageOrigin::Ump(UmpQueueId::Para(para_id)) => para_id,
}
}
}

impl parachains_inclusion::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type DisputesHandler = ParasDisputes;
type RewardValidators = RewardValidatorsWithEraPoints<Runtime>;
type AggregateMessageOrigin = AggregateMessageOrigin;
type GetAggregateMessageOrigin = GetAggregateMessageOrigin;
type GetParaFromAggregateMessageOrigin = GetParaFromAggregateMessageOrigin;
type MessageQueue = ();
type WeightInfo = ();
}
Expand Down
23 changes: 22 additions & 1 deletion polkadot/runtime/westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ use sp_runtime::{
create_runtime_str, generic, impl_opaque_keys,
traits::{
AccountIdConversion, BlakeTwo256, Block as BlockT, ConvertInto, Extrinsic as ExtrinsicT,
IdentityLookup, Keccak256, OpaqueKeys, SaturatedConversion, Verify,
IdentityLookup, Keccak256, OpaqueKeys, SaturatedConversion, Verify, Convert
},
transaction_validity::{TransactionPriority, TransactionSource, TransactionValidity},
ApplyExtrinsicResult, FixedU128, KeyTypeId, Percent, Permill,
Expand Down Expand Up @@ -1188,10 +1188,31 @@ impl parachains_session_info::Config for Runtime {
type ValidatorSet = Historical;
}

pub struct GetAggregateMessageOrigin;

impl Convert<UmpQueueId, AggregateMessageOrigin> for GetAggregateMessageOrigin {
fn convert(queue_id: UmpQueueId) -> AggregateMessageOrigin {
AggregateMessageOrigin::Ump(queue_id)
}
}

pub struct GetParaFromAggregateMessageOrigin;

impl Convert<AggregateMessageOrigin, ParaId> for GetParaFromAggregateMessageOrigin {
fn convert(x: AggregateMessageOrigin) -> ParaId {
match x {
AggregateMessageOrigin::Ump(UmpQueueId::Para(para_id)) => para_id,
}
}
}

impl parachains_inclusion::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type DisputesHandler = ParasDisputes;
type RewardValidators = parachains_reward_points::RewardValidatorsWithEraPoints<Runtime>;
type AggregateMessageOrigin = AggregateMessageOrigin;
type GetAggregateMessageOrigin = GetAggregateMessageOrigin;
type GetParaFromAggregateMessageOrigin = GetParaFromAggregateMessageOrigin;
type MessageQueue = MessageQueue;
type WeightInfo = weights::polkadot_runtime_parachains_inclusion::WeightInfo<Runtime>;
}
Expand Down

0 comments on commit 51b247a

Please sign in to comment.