This repository was archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 370
Introduce message broker for receiving and sending relay chain messages #80
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| [package] | ||
| name = "cumulus-message-broker" | ||
| version = "0.1.0" | ||
| authors = ["Parity Technologies <admin@parity.io>"] | ||
| 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", | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 <http://www.gnu.org/licenses/>. | ||
|
|
||
| //! 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<T: Trait> 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<T: Trait> UpwardMessageSender for Module<T> { | ||
| fn send_upward_message(_: &()) -> Result<(), ()> { | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| impl<T: Trait> ProvideInherent for Module<T> { | ||
| type Call = Call<T>; | ||
| type Error = MakeFatalError<()>; | ||
| const INHERENT_IDENTIFIER: InherentIdentifier = DOWNWARD_MESSAGES_IDENTIFIER; | ||
|
|
||
| fn create_inherent(data: &InherentData) -> Option<Self::Call> { | ||
| data.get_data::<DownwardMessagesType>(&DOWNWARD_MESSAGES_IDENTIFIER) | ||
| .expect("Downward messages inherent data failed to decode") | ||
| .map(|msgs| Call::execute_downward_messages(msgs)) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| [package] | ||
| name = "cumulus-primitives" | ||
| version = "0.1.0" | ||
| authors = ["Parity Technologies <admin@parity.io>"] | ||
| 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", | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 <http://www.gnu.org/licenses/>. | ||
|
|
||
| //! Cumulus related primitive types and traits. | ||
JoshOrndorff marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| #![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<(), ()>; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why does the broker pallet need to implement this trait? What kinds of messages will it be sending? Maybe the idea is that it will aggregate messages from other pallets and send them?
Will other pallets that want to send upward messages (eg. WrappedDOT) implement
UpwardMessageSenderthemselves? Or will they depend on this broker pallet to send messages on their behalf?I realize this is a stub implementation, but what is the plan for how these messages will actually reach the relay chain? I guess messages will be appended to the storage at
UPWARD_MESSAGESand then somhow they will be included in the relay chain block?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah it will aggregate it, I may need to rename something. If you have some suggestions, I'm open.
In your WrappedDot example you would implement
DownwardMessageHandlerfor your module and add atype UpwardMessageSender: UpwardMessageSender;to your trait. This upward message sender can be used to send these messages from your pallet and will be initialized in the runtime.Exactly. I will extract them on the validation side and return them to Polkadot to handle them ;)