From 41e5f0dcfd8003190ce24821d2c7305d63b4d4b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20K=C3=B6cher?= Date: Mon, 20 Apr 2020 16:22:05 +0200 Subject: [PATCH] Introduce message broker for receiving and sending relay chain messages (#80) * Start message broker implementation * Finish first stub implementation * Add features * Fix attribute * Update primitives/src/lib.rs Co-Authored-By: Joshy Orndorff Co-authored-by: Joshy Orndorff --- cumulus/Cargo.lock | 19 ++++++++ cumulus/Cargo.toml | 2 + cumulus/collator/Cargo.toml | 2 +- cumulus/consensus/Cargo.toml | 2 +- cumulus/message-broker/Cargo.toml | 27 +++++++++++ cumulus/message-broker/src/lib.rs | 75 +++++++++++++++++++++++++++++++ cumulus/network/Cargo.toml | 2 +- cumulus/primitives/Cargo.toml | 18 ++++++++ cumulus/primitives/src/lib.rs | 53 ++++++++++++++++++++++ cumulus/runtime/Cargo.toml | 2 +- cumulus/runtime/src/lib.rs | 3 +- 11 files changed, 200 insertions(+), 5 deletions(-) create mode 100644 cumulus/message-broker/Cargo.toml create mode 100644 cumulus/message-broker/src/lib.rs create mode 100644 cumulus/primitives/Cargo.toml create mode 100644 cumulus/primitives/src/lib.rs diff --git a/cumulus/Cargo.lock b/cumulus/Cargo.lock index 2d593bdb8d..548604dbca 100644 --- a/cumulus/Cargo.lock +++ b/cumulus/Cargo.lock @@ -797,6 +797,17 @@ dependencies = [ "tokio 0.1.22 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "cumulus-message-broker" +version = "0.1.0" +dependencies = [ + "cumulus-primitives 0.1.0", + "frame-support 2.0.0-alpha.5 (git+https://github.com/paritytech/substrate?branch=cumulus-branch)", + "frame-system 2.0.0-alpha.5 (git+https://github.com/paritytech/substrate?branch=cumulus-branch)", + "parity-scale-codec 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "sp-inherents 2.0.0-alpha.5 (git+https://github.com/paritytech/substrate?branch=cumulus-branch)", +] + [[package]] name = "cumulus-network" version = "0.1.0" @@ -813,6 +824,14 @@ dependencies = [ "sp-runtime 2.0.0-alpha.5 (git+https://github.com/paritytech/substrate?branch=cumulus-branch)", ] +[[package]] +name = "cumulus-primitives" +version = "0.1.0" +dependencies = [ + "impl-trait-for-tuples 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "sp-inherents 2.0.0-alpha.5 (git+https://github.com/paritytech/substrate?branch=cumulus-branch)", +] + [[package]] name = "cumulus-runtime" version = "0.1.0" diff --git a/cumulus/Cargo.toml b/cumulus/Cargo.toml index 05f0059432..89351ef655 100644 --- a/cumulus/Cargo.toml +++ b/cumulus/Cargo.toml @@ -1,7 +1,9 @@ [workspace] members = [ "consensus", + "message-broker", "network", + "primitives", "runtime", "test/runtime", "test/client", diff --git a/cumulus/collator/Cargo.toml b/cumulus/collator/Cargo.toml index ebcd946681..45dbb99782 100644 --- a/cumulus/collator/Cargo.toml +++ b/cumulus/collator/Cargo.toml @@ -27,7 +27,7 @@ cumulus-runtime = { path = "../runtime" } # Other dependencies log = "0.4.8" -codec = { package = "parity-scale-codec", version = "1.0.6", features = [ "derive" ] } +codec = { package = "parity-scale-codec", version = "1.3.0", features = [ "derive" ] } futures = { version = "0.3.1", features = ["compat"] } parking_lot = "0.9" diff --git a/cumulus/consensus/Cargo.toml b/cumulus/consensus/Cargo.toml index 05d6999b4c..405c8e6abf 100644 --- a/cumulus/consensus/Cargo.toml +++ b/cumulus/consensus/Cargo.toml @@ -24,5 +24,5 @@ polkadot-runtime = { git = "https://github.com/paritytech/polkadot", branch = "c # other deps futures = { version = "0.3.1", features = ["compat"] } tokio = "0.1.22" -codec = { package = "parity-scale-codec", version = "1.0.5", features = [ "derive" ] } +codec = { package = "parity-scale-codec", version = "1.3.0", features = [ "derive" ] } log = "0.4" diff --git a/cumulus/message-broker/Cargo.toml b/cumulus/message-broker/Cargo.toml new file mode 100644 index 0000000000..d2bf5358f4 --- /dev/null +++ b/cumulus/message-broker/Cargo.toml @@ -0,0 +1,27 @@ +[package] +name = "cumulus-message-broker" +version = "0.1.0" +authors = ["Parity Technologies "] +edition = "2018" + +[dependencies] +# substrate deps +frame-support = { git = "https://github.com/paritytech/substrate", branch = "cumulus-branch" } +frame-system = { git = "https://github.com/paritytech/substrate", branch = "cumulus-branch" } +sp-inherents = { git = "https://github.com/paritytech/substrate", branch = "cumulus-branch" } + +# Other dependencies +codec = { package = "parity-scale-codec", version = "1.3.0", features = [ "derive" ] } + +# Cumulus dependencies +cumulus-primitives = { path = "../primitives" } + +[features] +default = [ "std" ] +std = [ + "frame-support/std", + "frame-system/std", + "sp-inherents/std", + "codec/std", + "cumulus-primitives/std", +] diff --git a/cumulus/message-broker/src/lib.rs b/cumulus/message-broker/src/lib.rs new file mode 100644 index 0000000000..80886ba777 --- /dev/null +++ b/cumulus/message-broker/src/lib.rs @@ -0,0 +1,75 @@ +// 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 . + +//! Cumulus message broker pallet. +//! +//! This pallet provides support for handling downward and upward messages. + +#![cfg_attr(not(feature = "std"), no_std)] + +use cumulus_primitives::{ + inherents::{DownwardMessagesType, DOWNWARD_MESSAGES_IDENTIFIER}, + well_known_keys, DownwardMessageHandler, UpwardMessageSender, +}; +use frame_support::{ + decl_module, storage, + weights::{SimpleDispatchInfo, WeighData, Weight}, +}; +use frame_system::ensure_none; +use sp_inherents::{InherentData, InherentIdentifier, MakeFatalError, ProvideInherent}; + +/// Configuration trait of this 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 { + /// Executes the given downward messages by calling the message handlers. + /// + /// The origin of this call needs to be `None` as this is an inherent. + #[weight = SimpleDispatchInfo::FixedMandatory(10)] + fn execute_downward_messages(origin, messages: Vec<()>) { + ensure_none(origin)?; + messages.iter().for_each(T::DownwardMessageHandlers::handle_downward_message); + } + + fn on_initialize() -> Weight { + storage::unhashed::kill(well_known_keys::UPWARD_MESSAGES); + + SimpleDispatchInfo::default().weigh_data(()) + } + } +} + +impl UpwardMessageSender for Module { + fn send_upward_message(_: &()) -> Result<(), ()> { + Ok(()) + } +} + +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::execute_downward_messages(msgs)) + } +} diff --git a/cumulus/network/Cargo.toml b/cumulus/network/Cargo.toml index 3deb8a2e5a..21f1b6f1c7 100644 --- a/cumulus/network/Cargo.toml +++ b/cumulus/network/Cargo.toml @@ -20,4 +20,4 @@ polkadot-validation = { git = "https://github.com/paritytech/polkadot", branch = polkadot-network = { git = "https://github.com/paritytech/polkadot", branch = "cumulus-branch" } # other deps -codec = { package = "parity-scale-codec", version = "1.0.5", features = [ "derive" ] } +codec = { package = "parity-scale-codec", version = "1.3.0", features = [ "derive" ] } diff --git a/cumulus/primitives/Cargo.toml b/cumulus/primitives/Cargo.toml new file mode 100644 index 0000000000..cd772dcea8 --- /dev/null +++ b/cumulus/primitives/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "cumulus-primitives" +version = "0.1.0" +authors = ["Parity Technologies "] +edition = "2018" + +[dependencies] +# Substrate dependencies +sp-inherents = { git = "https://github.com/paritytech/substrate", branch = "cumulus-branch" } + +# Other dependencies +impl-trait-for-tuples = "0.1.3" + +[features] +default = [ "std" ] +std = [ + "sp-inherents/std", +] diff --git a/cumulus/primitives/src/lib.rs b/cumulus/primitives/src/lib.rs new file mode 100644 index 0000000000..2be6b5a05c --- /dev/null +++ b/cumulus/primitives/src/lib.rs @@ -0,0 +1,53 @@ +// 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 . + +//! Cumulus related primitive types and traits. + +#![cfg_attr(not(feature = "std"), no_std)] + +/// Identifiers and types related to Cumulus Inherents +pub mod inherents { + use sp_inherents::InherentIdentifier; + + /// Inherent identifier for downward messages. + pub const DOWNWARD_MESSAGES_IDENTIFIER: InherentIdentifier = *b"cumdownm"; + + /// The type of the inherent downward messages. + pub type DownwardMessagesType = Vec<()>; +} + +/// Well known keys for values in the storage. +pub mod well_known_keys { + /// The storage key for the upward messages. + /// + /// The upward messages are stored as SCALE encoded `Vec<()>`. + pub const UPWARD_MESSAGES: &'static [u8] = b":cumulus_upward_messages:"; +} + +/// Something that should be called when a downward message is received. +#[impl_trait_for_tuples::impl_for_tuples(30)] +pub trait DownwardMessageHandler { + /// Handle the given downward message. + fn handle_downward_message(msg: &()); +} + +/// Something that can send upward messages. +pub trait UpwardMessageSender { + /// Send an upward message to the relay chain. + /// + /// Returns an error if sending failed. + fn send_upward_message(msg: &()) -> Result<(), ()>; +} diff --git a/cumulus/runtime/Cargo.toml b/cumulus/runtime/Cargo.toml index 58f5339c01..98165169bf 100644 --- a/cumulus/runtime/Cargo.toml +++ b/cumulus/runtime/Cargo.toml @@ -6,7 +6,7 @@ edition = "2018" [dependencies] # Other dependencies -codec = { package = "parity-scale-codec", version = "1.0.5", default-features = false, features = [ "derive" ] } +codec = { package = "parity-scale-codec", version = "1.3.0", default-features = false, features = [ "derive" ] } memory-db = { version = "0.18.0", default-features = false } hash-db = { version = "0.15.2", default-features = false } trie-db = { version = "0.20.0", default-features = false } diff --git a/cumulus/runtime/src/lib.rs b/cumulus/runtime/src/lib.rs index a88278b0a1..e48896ea02 100644 --- a/cumulus/runtime/src/lib.rs +++ b/cumulus/runtime/src/lib.rs @@ -16,9 +16,10 @@ #![cfg_attr(not(feature = "std"), no_std)] +///! The Cumulus runtime to make a runtime a parachain. + use codec::{Decode, Encode}; use sp_runtime::traits::Block as BlockT; -///! The Cumulus runtime to make a runtime a parachain. use sp_std::vec::Vec; #[cfg(not(feature = "std"))]