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

Commit

Permalink
parachain dmp.rs to Frame V2 (#3426)
Browse files Browse the repository at this point in the history
* migration

* fix warning

* change runtimes

* extra line

* another double space lol

* add dmp in test runtime

* test runtime

* Adjust visibility on storage items

Co-authored-by: Keith Yeung <kungfukeith11@gmail.com>
  • Loading branch information
ferrell-code and KiChjang authored Jul 22, 2021
1 parent 887f0a0 commit cc74665
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 34 deletions.
2 changes: 1 addition & 1 deletion runtime/common/src/xcm_sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl<T: configuration::Config + dmp::Config> SendXcm for ChildParachainRouter<T>
MultiLocation::X1(Junction::Parachain(id)) => {
// Downward message passing.
let config = <configuration::Module<T>>::config();
<dmp::Module<T>>::queue_downward_message(
<dmp::Pallet<T>>::queue_downward_message(
&config,
id.into(),
VersionedXcm::from(msg).encode(),
Expand Down
2 changes: 1 addition & 1 deletion runtime/kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1487,7 +1487,7 @@ construct_runtime! {
ParasScheduler: parachains_scheduler::{Pallet, Call, Storage} = 55,
Paras: parachains_paras::{Pallet, Call, Storage, Event, Config} = 56,
Initializer: parachains_initializer::{Pallet, Call, Storage} = 57,
ParasDmp: parachains_dmp::{Pallet, Call, Storage} = 58,
Dmp: parachains_dmp::{Pallet, Call, Storage} = 58,
ParasUmp: parachains_ump::{Pallet, Call, Storage, Event} = 59,
ParasHrmp: parachains_hrmp::{Pallet, Call, Storage, Event} = 60,
ParasSessionInfo: parachains_session_info::{Pallet, Call, Storage} = 61,
Expand Down
64 changes: 42 additions & 22 deletions runtime/parachains/src/dmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ use crate::{
configuration::{self, HostConfiguration},
initializer,
};
use frame_support::{decl_module, decl_storage, StorageMap, weights::Weight, traits::Get};
use frame_support::pallet_prelude::*;
use sp_std::{fmt, prelude::*};
use sp_runtime::traits::{BlakeTwo256, Hash as HashT, SaturatedConversion};
use primitives::v1::{Id as ParaId, DownwardMessage, InboundDownwardMessage, Hash};
use xcm::v0::Error as XcmError;

pub use pallet::*;

/// An error sending a downward message.
#[cfg_attr(test, derive(Debug))]
pub enum QueueDownwardMessageError {
Expand Down Expand Up @@ -71,30 +73,49 @@ impl fmt::Debug for ProcessedDownwardMessagesAcceptanceErr {
}
}

pub trait Config: frame_system::Config + configuration::Config {}

decl_storage! {
trait Store for Module<T: Config> as Dmp {
/// The downward messages addressed for a certain para.
DownwardMessageQueues: map hasher(twox_64_concat) ParaId => Vec<InboundDownwardMessage<T::BlockNumber>>;
/// A mapping that stores the downward message queue MQC head for each para.
///
/// Each link in this chain has a form:
/// `(prev_head, B, H(M))`, where
/// - `prev_head`: is the previous head hash or zero if none.
/// - `B`: is the relay-chain block number in which a message was appended.
/// - `H(M)`: is the hash of the message being appended.
DownwardMessageQueueHeads: map hasher(twox_64_concat) ParaId => Hash;
}
}
#[frame_support::pallet]
pub mod pallet {
use super::*;

decl_module! {
/// The DMP module.
pub struct Module<T: Config> for enum Call where origin: <T as frame_system::Config>::Origin { }
#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
pub struct Pallet<T>(_);

#[pallet::config]
pub trait Config: frame_system::Config + configuration::Config {}

/// The downward messages addressed for a certain para.
#[pallet::storage]
pub(crate) type DownwardMessageQueues<T: Config> = StorageMap<
_,
Twox64Concat,
ParaId,
Vec<InboundDownwardMessage<T::BlockNumber>>,
ValueQuery
>;

/// A mapping that stores the downward message queue MQC head for each para.
///
/// Each link in this chain has a form:
/// `(prev_head, B, H(M))`, where
/// - `prev_head`: is the previous head hash or zero if none.
/// - `B`: is the relay-chain block number in which a message was appended.
/// - `H(M)`: is the hash of the message being appended.
#[pallet::storage]
pub(crate) type DownwardMessageQueueHeads<T: Config> = StorageMap<
_,
Twox64Concat,
ParaId,
Hash,
ValueQuery,
>;

#[pallet::call]
impl<T: Config> Pallet<T> {}
}

/// Routines and getters related to downward message passing.
impl<T: Config> Module<T> {
impl<T: Config> Pallet<T> {
/// Block initialization logic, called by initializer.
pub(crate) fn initializer_initialize(_now: T::BlockNumber) -> Weight {
0
Expand Down Expand Up @@ -226,7 +247,6 @@ mod tests {
use super::*;
use hex_literal::hex;
use primitives::v1::BlockNumber;
use frame_support::traits::{OnFinalize, OnInitialize};
use parity_scale_codec::Encode;
use crate::mock::{Configuration, new_test_ext, System, Dmp, MockGenesisConfig, Paras};

Expand Down
6 changes: 3 additions & 3 deletions runtime/parachains/src/hrmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1051,7 +1051,7 @@ impl<T: Config> Module<T> {
.encode()
};
if let Err(dmp::QueueDownwardMessageError::ExceedsMaxMessageSize) =
<dmp::Module<T>>::queue_downward_message(&config, recipient, notification_bytes)
<dmp::Pallet<T>>::queue_downward_message(&config, recipient, notification_bytes)
{
// this should never happen unless the max downward message size is configured to an
// jokingly small number.
Expand Down Expand Up @@ -1114,7 +1114,7 @@ impl<T: Config> Module<T> {
.encode()
};
if let Err(dmp::QueueDownwardMessageError::ExceedsMaxMessageSize) =
<dmp::Module<T>>::queue_downward_message(&config, sender, notification_bytes)
<dmp::Pallet<T>>::queue_downward_message(&config, sender, notification_bytes)
{
// this should never happen unless the max downward message size is configured to an
// jokingly small number.
Expand Down Expand Up @@ -1164,7 +1164,7 @@ impl<T: Config> Module<T> {
channel_id.sender
};
if let Err(dmp::QueueDownwardMessageError::ExceedsMaxMessageSize) =
<dmp::Module<T>>::queue_downward_message(&config, opposite_party, notification_bytes)
<dmp::Pallet<T>>::queue_downward_message(&config, opposite_party, notification_bytes)
{
// this should never happen unless the max downward message size is configured to an
// jokingly small number.
Expand Down
4 changes: 2 additions & 2 deletions runtime/parachains/src/inclusion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,7 @@ impl<T: Config> Pallet<T> {
}

// enact the messaging facet of the candidate.
weight += <dmp::Module<T>>::prune_dmq(
weight += <dmp::Pallet<T>>::prune_dmq(
receipt.descriptor.para_id,
commitments.processed_downward_messages,
);
Expand Down Expand Up @@ -918,7 +918,7 @@ impl<T: Config> CandidateCheckContext<T> {
}

// check if the candidate passes the messaging acceptance criteria
<dmp::Module<T>>::check_processed_downward_messages(
<dmp::Pallet<T>>::check_processed_downward_messages(
para_id,
processed_downward_messages,
)?;
Expand Down
6 changes: 3 additions & 3 deletions runtime/parachains/src/initializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ pub mod pallet {
inclusion::Pallet::<T>::initializer_initialize(now) +
session_info::Module::<T>::initializer_initialize(now) +
T::DisputesHandler::initializer_initialize(now) +
dmp::Module::<T>::initializer_initialize(now) +
dmp::Pallet::<T>::initializer_initialize(now) +
ump::Module::<T>::initializer_initialize(now) +
hrmp::Module::<T>::initializer_initialize(now);

Expand All @@ -152,7 +152,7 @@ pub mod pallet {
// reverse initialization order.
hrmp::Module::<T>::initializer_finalize();
ump::Module::<T>::initializer_finalize();
dmp::Module::<T>::initializer_finalize();
dmp::Pallet::<T>::initializer_finalize();
T::DisputesHandler::initializer_finalize();
session_info::Module::<T>::initializer_finalize();
inclusion::Pallet::<T>::initializer_finalize();
Expand Down Expand Up @@ -238,7 +238,7 @@ impl<T: Config> Pallet<T> {
inclusion::Pallet::<T>::initializer_on_new_session(&notification);
session_info::Module::<T>::initializer_on_new_session(&notification);
T::DisputesHandler::initializer_on_new_session(&notification);
dmp::Module::<T>::initializer_on_new_session(&notification, &outgoing_paras);
dmp::Pallet::<T>::initializer_on_new_session(&notification, &outgoing_paras);
ump::Module::<T>::initializer_on_new_session(&notification, &outgoing_paras);
hrmp::Module::<T>::initializer_on_new_session(&notification, &outgoing_paras);
}
Expand Down
2 changes: 1 addition & 1 deletion runtime/parachains/src/runtime_api_impl/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ pub fn session_info<T: session_info::Config>(index: SessionIndex) -> Option<Sess
pub fn dmq_contents<T: dmp::Config>(
recipient: ParaId,
) -> Vec<InboundDownwardMessage<T::BlockNumber>> {
<dmp::Module<T>>::dmq_contents(recipient)
<dmp::Pallet<T>>::dmq_contents(recipient)
}

/// Implementation for the `inbound_hrmp_channels_contents` function of the runtime API.
Expand Down
1 change: 1 addition & 0 deletions runtime/test-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,7 @@ construct_runtime! {
SessionInfo: parachains_session_info::{Pallet, Call, Storage},
Hrmp: parachains_hrmp::{Pallet, Call, Storage, Event},
Ump: parachains_ump::{Pallet, Call, Storage, Event},
Dmp: parachains_dmp::{Pallet, Call, Storage},
ParasDisputes: parachains_disputes::{Pallet, Storage, Event<T>},

Sudo: pallet_sudo::{Pallet, Call, Storage, Config<T>, Event<T>},
Expand Down
2 changes: 1 addition & 1 deletion runtime/westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1078,7 +1078,7 @@ construct_runtime! {
ParasScheduler: parachains_scheduler::{Pallet, Call, Storage} = 46,
Paras: parachains_paras::{Pallet, Call, Storage, Event, Config} = 47,
Initializer: parachains_initializer::{Pallet, Call, Storage} = 48,
ParasDmp: parachains_dmp::{Pallet, Call, Storage} = 49,
Dmp: parachains_dmp::{Pallet, Call, Storage} = 49,
ParasUmp: parachains_ump::{Pallet, Call, Storage, Event} = 50,
ParasHrmp: parachains_hrmp::{Pallet, Call, Storage, Event} = 51,
ParasSessionInfo: parachains_session_info::{Pallet, Call, Storage} = 52,
Expand Down

0 comments on commit cc74665

Please sign in to comment.