diff --git a/message-broker/src/lib.rs b/message-broker/src/lib.rs index 47733e811a..31acd3c1ca 100644 --- a/message-broker/src/lib.rs +++ b/message-broker/src/lib.rs @@ -21,15 +21,19 @@ #![cfg_attr(not(feature = "std"), no_std)] -use sp_std::prelude::*; +use frame_support::{ + decl_module, decl_storage, storage, + weights::{DispatchClass, Weight}, + traits::Get, + StorageValue, +}; use frame_system::ensure_none; -use frame_support::{decl_module, storage, weights::DispatchClass}; use sp_inherents::{InherentData, InherentIdentifier, MakeFatalError, ProvideInherent}; +use sp_std::{prelude::*, cmp}; use cumulus_primitives::{ inherents::{DownwardMessagesType, DOWNWARD_MESSAGES_IDENTIFIER}, - well_known_keys, - DownwardMessageHandler, InboundDownwardMessage, + well_known_keys, DownwardMessageHandler, InboundDownwardMessage, UpwardMessage, }; /// Configuration trait of the message broker pallet. @@ -38,6 +42,12 @@ pub trait Trait: frame_system::Trait { type DownwardMessageHandlers: DownwardMessageHandler; } +decl_storage! { + trait Store for Module as MessageBroker { + PendingUpwardMessages: Vec; + } +} + 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 @@ -58,6 +68,40 @@ decl_module! { &messages_len, ); } + + fn on_initialize() -> Weight { + // Reads and writes performed by `on_finalize`. + T::DbWeight::get().reads_writes(1, 2) + } + + fn on_finalize() { + // TODO: this should be not a constant, but sourced from the relay-chain configuration. + const UMP_MSG_NUM_PER_CANDIDATE: usize = 5; + + ::PendingUpwardMessages::mutate(|up| { + let num = cmp::min(UMP_MSG_NUM_PER_CANDIDATE, up.len()); + storage::unhashed::put( + well_known_keys::UPWARD_MESSAGES, + &up[0..num], + ); + *up = up.split_off(num); + }); + } + } +} + +/// An error that can be raised upon sending an upward message. +pub enum SendUpErr { + /// The message sent is too big. + TooBig, +} + +impl Module { + pub fn send_upward_message(message: UpwardMessage) -> Result<(), SendUpErr> { + // TODO: check the message against the limit. The limit should be sourced from the + // relay-chain configuration. + ::PendingUpwardMessages::append(message); + Ok(()) } } diff --git a/primitives/src/lib.rs b/primitives/src/lib.rs index e8f9d90b31..69480c79e4 100644 --- a/primitives/src/lib.rs +++ b/primitives/src/lib.rs @@ -20,11 +20,7 @@ pub use polkadot_core_primitives as relay_chain; pub use polkadot_core_primitives::InboundDownwardMessage; -/// A generic upward message from a Parachain to the Relay Chain. -/// -/// It is "generic" in such a way, that the actual message is encoded in the `data` field. -/// Besides the `data` it also holds the `origin` of the message. -pub use polkadot_parachain::primitives::UpwardMessage as GenericUpwardMessage; +pub use polkadot_parachain::primitives::UpwardMessage; pub use polkadot_parachain::primitives::{Id as ParaId, ValidationParams}; pub use polkadot_primitives::v1::{ PersistedValidationData, TransientValidationData, ValidationData, @@ -54,7 +50,7 @@ pub mod inherents { pub mod well_known_keys { /// The storage key for the upward messages. /// - /// The upward messages are stored as SCALE encoded `Vec`. + /// The upward messages are stored as SCALE encoded `Vec`. pub const UPWARD_MESSAGES: &'static [u8] = b":cumulus_upward_messages:"; /// Current validation data. diff --git a/rococo-parachains/runtime/src/lib.rs b/rococo-parachains/runtime/src/lib.rs index b86db734e3..1d72f04890 100644 --- a/rococo-parachains/runtime/src/lib.rs +++ b/rococo-parachains/runtime/src/lib.rs @@ -218,7 +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}, + MessageBroker: cumulus_message_broker::{Module, Storage, Call, Inherent}, TransactionPayment: pallet_transaction_payment::{Module, Storage}, ParachainInfo: parachain_info::{Module, Storage, Config}, } diff --git a/runtime/src/validate_block/implementation.rs b/runtime/src/validate_block/implementation.rs index 600fa0c066..e850dd2b42 100644 --- a/runtime/src/validate_block/implementation.rs +++ b/runtime/src/validate_block/implementation.rs @@ -31,7 +31,7 @@ use cumulus_primitives::{ well_known_keys::{ NEW_VALIDATION_CODE, PROCESSED_DOWNWARD_MESSAGES, UPWARD_MESSAGES, VALIDATION_DATA, }, - GenericUpwardMessage, ValidationData, + UpwardMessage, ValidationData, }; use sp_externalities::{set_and_run_with_externalities}; use sp_externalities::{Externalities, ExtensionStore, Error, Extension}; @@ -148,7 +148,7 @@ pub fn validate_block>(params: ValidationParams) - // Extract potential upward messages from the storage. let upward_messages = match overlay.storage(UPWARD_MESSAGES).flatten() { - Some(encoded) => Vec::::decode(&mut &encoded[..]) + Some(encoded) => Vec::::decode(&mut &encoded[..]) .expect("Upward messages vec is not correctly encoded in the storage!"), None => Vec::new(), };