diff --git a/cumulus/Cargo.lock b/cumulus/Cargo.lock index 60a1052a0c..74e0390b99 100644 --- a/cumulus/Cargo.lock +++ b/cumulus/Cargo.lock @@ -1112,6 +1112,18 @@ dependencies = [ "tokio 0.1.22", ] +[[package]] +name = "cumulus-message-broker" +version = "0.1.0" +dependencies = [ + "cumulus-primitives", + "frame-support", + "frame-system", + "parity-scale-codec", + "sp-inherents", + "sp-std", +] + [[package]] name = "cumulus-network" version = "0.1.0" @@ -1255,6 +1267,7 @@ dependencies = [ name = "cumulus-test-parachain-runtime" version = "0.1.0" dependencies = [ + "cumulus-message-broker", "cumulus-parachain-upgrade", "cumulus-primitives", "cumulus-runtime", diff --git a/cumulus/Cargo.toml b/cumulus/Cargo.toml index f82a96c47f..1606db4a4d 100644 --- a/cumulus/Cargo.toml +++ b/cumulus/Cargo.toml @@ -1,6 +1,7 @@ [workspace] members = [ "consensus", + "message-broker", "network", "parachain-upgrade", "primitives", diff --git a/cumulus/message-broker/Cargo.toml b/cumulus/message-broker/Cargo.toml new file mode 100644 index 0000000000..43bcf7d92c --- /dev/null +++ b/cumulus/message-broker/Cargo.toml @@ -0,0 +1,28 @@ +[package] +name = "cumulus-message-broker" +version = "0.1.0" +authors = ["Parity Technologies "] +edition = "2018" + +[dependencies] +# Substrate dependencies +frame-support = { git = "https://github.com/paritytech/substrate", default-features = false , branch = "master" } +frame-system = { git = "https://github.com/paritytech/substrate", default-features = false , branch = "master" } +sp-inherents = { git = "https://github.com/paritytech/substrate", default-features = false , branch = "master" } +sp-std = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" } + +# Other dependencies +codec = { package = "parity-scale-codec", version = "1.3.0", features = [ "derive" ], default-features = false } + +# Cumulus dependencies +cumulus-primitives = { path = "../primitives", default-features = false } + +[features] +default = [ "std" ] +std = [ + "frame-support/std", + "frame-system/std", + "sp-inherents/std", + "cumulus-primitives/std", + "sp-std/std" +] diff --git a/cumulus/message-broker/src/lib.rs b/cumulus/message-broker/src/lib.rs new file mode 100644 index 0000000000..47733e811a --- /dev/null +++ b/cumulus/message-broker/src/lib.rs @@ -0,0 +1,74 @@ +// Copyright 2020 Parity Technologies (UK) Ltd. +// This file is part of Cumulus. + +// Substrate is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Substrate is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Cumulus. If not, see . + +//! This pallet deals with the low-level details of parachain message passing. +//! +//! Specifically, this pallet serves as a glue layer between cumulus collation pipeline and the +//! runtime that hosts this pallet. + +#![cfg_attr(not(feature = "std"), no_std)] + +use sp_std::prelude::*; +use frame_system::ensure_none; +use frame_support::{decl_module, storage, weights::DispatchClass}; +use sp_inherents::{InherentData, InherentIdentifier, MakeFatalError, ProvideInherent}; + +use cumulus_primitives::{ + inherents::{DownwardMessagesType, DOWNWARD_MESSAGES_IDENTIFIER}, + well_known_keys, + DownwardMessageHandler, InboundDownwardMessage, +}; + +/// Configuration trait of the message broker pallet. +pub trait Trait: frame_system::Trait { + /// The downward message handlers that will be informed when a message is received. + type DownwardMessageHandlers: DownwardMessageHandler; +} + +decl_module! { + pub struct Module for enum Call where origin: T::Origin { + /// An entrypoint for an inherent to deposit downward messages into the runtime. It accepts + /// and processes the list of downward messages. + #[weight = (10, DispatchClass::Mandatory)] + fn receive_downward_messages(origin, messages: Vec) { + ensure_none(origin)?; + + let messages_len = messages.len() as u32; + for message in messages { + T::DownwardMessageHandlers::handle_downward_message(message); + } + + // Store the processed_downward_messages here so that it's will be accessible from + // PVF's `validate_block` wrapper and collation pipeline. + storage::unhashed::put( + well_known_keys::PROCESSED_DOWNWARD_MESSAGES, + &messages_len, + ); + } + } +} + +impl ProvideInherent for Module { + type Call = Call; + type Error = MakeFatalError<()>; + const INHERENT_IDENTIFIER: InherentIdentifier = DOWNWARD_MESSAGES_IDENTIFIER; + + fn create_inherent(data: &InherentData) -> Option { + data.get_data::(&DOWNWARD_MESSAGES_IDENTIFIER) + .expect("Downward messages inherent data failed to decode") + .map(|msgs| Call::receive_downward_messages(msgs)) + } +} diff --git a/cumulus/primitives/src/lib.rs b/cumulus/primitives/src/lib.rs index 869a49c41d..e8f9d90b31 100644 --- a/cumulus/primitives/src/lib.rs +++ b/cumulus/primitives/src/lib.rs @@ -73,7 +73,7 @@ pub mod well_known_keys { #[impl_trait_for_tuples::impl_for_tuples(30)] pub trait DownwardMessageHandler { /// Handle the given downward message. - fn handle_downward_message(msg: &InboundDownwardMessage); + fn handle_downward_message(msg: InboundDownwardMessage); } /// A trait which is called when the validation data is set. diff --git a/cumulus/rococo-parachains/runtime/Cargo.toml b/cumulus/rococo-parachains/runtime/Cargo.toml index da797ecae0..44df3db1a7 100644 --- a/cumulus/rococo-parachains/runtime/Cargo.toml +++ b/cumulus/rococo-parachains/runtime/Cargo.toml @@ -37,6 +37,7 @@ pallet-transaction-payment = { git = "https://github.com/paritytech/substrate", cumulus-runtime = { path = "../../runtime", default-features = false } cumulus-parachain-upgrade = { path = "../../parachain-upgrade", default-features = false } cumulus-primitives = { path = "../../primitives", default-features = false } +cumulus-message-broker = { path = "../../message-broker", default-features = false } # Polkadot dependencies polkadot-parachain = { git = "https://github.com/paritytech/polkadot", default-features = false , branch = "master" } @@ -72,5 +73,6 @@ std = [ "rococo-parachain-primitives/std", "cumulus-runtime/std", "cumulus-parachain-upgrade/std", + "cumulus-message-broker/std", "cumulus-primitives/std", ] diff --git a/cumulus/rococo-parachains/runtime/src/lib.rs b/cumulus/rococo-parachains/runtime/src/lib.rs index 38bc092970..b86db734e3 100644 --- a/cumulus/rococo-parachains/runtime/src/lib.rs +++ b/cumulus/rococo-parachains/runtime/src/lib.rs @@ -200,6 +200,10 @@ impl cumulus_parachain_upgrade::Trait for Runtime { type OnValidationData = (); } +impl cumulus_message_broker::Trait for Runtime { + type DownwardMessageHandlers = (); +} + impl parachain_info::Trait for Runtime {} construct_runtime! { @@ -214,6 +218,7 @@ construct_runtime! { Sudo: pallet_sudo::{Module, Call, Storage, Config, Event}, RandomnessCollectiveFlip: pallet_randomness_collective_flip::{Module, Call, Storage}, ParachainUpgrade: cumulus_parachain_upgrade::{Module, Call, Storage, Inherent, Event}, + MessageBroker: cumulus_message_broker::{Module, Call, Inherent}, TransactionPayment: pallet_transaction_payment::{Module, Storage}, ParachainInfo: parachain_info::{Module, Storage, Config}, }