mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-30 11:41:02 +00:00
Integrate UMP into message-broker (#244)
This commit is contained in:
@@ -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<T: Trait> as MessageBroker {
|
||||
PendingUpwardMessages: Vec<UpwardMessage>;
|
||||
}
|
||||
}
|
||||
|
||||
decl_module! {
|
||||
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
|
||||
@@ -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;
|
||||
|
||||
<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(())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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<GenericUpwardMessage>`.
|
||||
/// The upward messages are stored as SCALE encoded `Vec<UpwardMessage>`.
|
||||
pub const UPWARD_MESSAGES: &'static [u8] = b":cumulus_upward_messages:";
|
||||
|
||||
/// Current validation data.
|
||||
|
||||
@@ -218,7 +218,7 @@ construct_runtime! {
|
||||
Sudo: pallet_sudo::{Module, Call, Storage, Config<T>, Event<T>},
|
||||
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},
|
||||
}
|
||||
|
||||
@@ -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<B: BlockT, E: ExecuteBlock<B>>(params: ValidationParams) -
|
||||
|
||||
// Extract potential upward messages from the storage.
|
||||
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!"),
|
||||
None => Vec::new(),
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user