Integrate UMP into message-broker (#244)

This commit is contained in:
Sergei Shulepov
2020-11-20 21:32:58 +01:00
committed by GitHub
parent 001aa5bb00
commit 2d7f37ed1d
4 changed files with 53 additions and 13 deletions
+48 -4
View File
@@ -21,15 +21,19 @@
#![cfg_attr(not(feature = "std"), no_std)] #![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_system::ensure_none;
use frame_support::{decl_module, storage, weights::DispatchClass};
use sp_inherents::{InherentData, InherentIdentifier, MakeFatalError, ProvideInherent}; use sp_inherents::{InherentData, InherentIdentifier, MakeFatalError, ProvideInherent};
use sp_std::{prelude::*, cmp};
use cumulus_primitives::{ use cumulus_primitives::{
inherents::{DownwardMessagesType, DOWNWARD_MESSAGES_IDENTIFIER}, inherents::{DownwardMessagesType, DOWNWARD_MESSAGES_IDENTIFIER},
well_known_keys, well_known_keys, DownwardMessageHandler, InboundDownwardMessage, UpwardMessage,
DownwardMessageHandler, InboundDownwardMessage,
}; };
/// Configuration trait of the message broker pallet. /// Configuration trait of the message broker pallet.
@@ -38,6 +42,12 @@ pub trait Trait: frame_system::Trait {
type DownwardMessageHandlers: DownwardMessageHandler; type DownwardMessageHandlers: DownwardMessageHandler;
} }
decl_storage! {
trait Store for Module<T: Trait> as MessageBroker {
PendingUpwardMessages: Vec<UpwardMessage>;
}
}
decl_module! { decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin { pub struct Module<T: Trait> for enum Call where origin: T::Origin {
/// An entrypoint for an inherent to deposit downward messages into the runtime. It accepts /// An entrypoint for an inherent to deposit downward messages into the runtime. It accepts
@@ -58,6 +68,40 @@ decl_module! {
&messages_len, &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;
<Self as Store>::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<T: Trait> Module<T> {
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.
<Self as Store>::PendingUpwardMessages::append(message);
Ok(())
} }
} }
+2 -6
View File
@@ -20,11 +20,7 @@
pub use polkadot_core_primitives as relay_chain; pub use polkadot_core_primitives as relay_chain;
pub use polkadot_core_primitives::InboundDownwardMessage; pub use polkadot_core_primitives::InboundDownwardMessage;
/// A generic upward message from a Parachain to the Relay Chain. pub use polkadot_parachain::primitives::UpwardMessage;
///
/// 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::{Id as ParaId, ValidationParams}; pub use polkadot_parachain::primitives::{Id as ParaId, ValidationParams};
pub use polkadot_primitives::v1::{ pub use polkadot_primitives::v1::{
PersistedValidationData, TransientValidationData, ValidationData, PersistedValidationData, TransientValidationData, ValidationData,
@@ -54,7 +50,7 @@ pub mod inherents {
pub mod well_known_keys { pub mod well_known_keys {
/// The storage key for the upward messages. /// The storage key for the upward messages.
/// ///
/// The upward messages are stored as SCALE encoded `Vec<GenericUpwardMessage>`. /// The upward messages are stored as SCALE encoded `Vec<UpwardMessage>`.
pub const UPWARD_MESSAGES: &'static [u8] = b":cumulus_upward_messages:"; pub const UPWARD_MESSAGES: &'static [u8] = b":cumulus_upward_messages:";
/// Current validation data. /// Current validation data.
+1 -1
View File
@@ -218,7 +218,7 @@ construct_runtime! {
Sudo: pallet_sudo::{Module, Call, Storage, Config<T>, Event<T>}, Sudo: pallet_sudo::{Module, Call, Storage, Config<T>, Event<T>},
RandomnessCollectiveFlip: pallet_randomness_collective_flip::{Module, Call, Storage}, RandomnessCollectiveFlip: pallet_randomness_collective_flip::{Module, Call, Storage},
ParachainUpgrade: cumulus_parachain_upgrade::{Module, Call, Storage, Inherent, Event}, 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}, TransactionPayment: pallet_transaction_payment::{Module, Storage},
ParachainInfo: parachain_info::{Module, Storage, Config}, ParachainInfo: parachain_info::{Module, Storage, Config},
} }
@@ -31,7 +31,7 @@ use cumulus_primitives::{
well_known_keys::{ well_known_keys::{
NEW_VALIDATION_CODE, PROCESSED_DOWNWARD_MESSAGES, UPWARD_MESSAGES, VALIDATION_DATA, 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::{set_and_run_with_externalities};
use sp_externalities::{Externalities, ExtensionStore, Error, Extension}; use sp_externalities::{Externalities, ExtensionStore, Error, Extension};
@@ -148,7 +148,7 @@ pub fn validate_block<B: BlockT, E: ExecuteBlock<B>>(params: ValidationParams) -
// Extract potential upward messages from the storage. // Extract potential upward messages from the storage.
let upward_messages = match overlay.storage(UPWARD_MESSAGES).flatten() { let upward_messages = match overlay.storage(UPWARD_MESSAGES).flatten() {
Some(encoded) => Vec::<GenericUpwardMessage>::decode(&mut &encoded[..]) Some(encoded) => Vec::<UpwardMessage>::decode(&mut &encoded[..])
.expect("Upward messages vec is not correctly encoded in the storage!"), .expect("Upward messages vec is not correctly encoded in the storage!"),
None => Vec::new(), None => Vec::new(),
}; };