Skip to content

Commit

Permalink
Integrate a governance XCM origin (paritytech#407)
Browse files Browse the repository at this point in the history
* Introduce the converter into the hub

* Parachain recognises Rococo governance body as admin

* Whitespace

* Use UsingComponents for fee payment in XCM

* Fixes

* Fixes for XCM permissions

* Remove encode_call test

* Fixes

* Fixes

* Fixes
  • Loading branch information
gavofyork authored Apr 28, 2021
1 parent 859524f commit 0ab15b2
Show file tree
Hide file tree
Showing 8 changed files with 235 additions and 98 deletions.
36 changes: 27 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 39 additions & 20 deletions pallets/parachain-system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ impl<T: Config> Module<T> {
assert_eq!(
params.relay_parent_storage_root,
validation_data.relay_parent_storage_root,
"Relay parent stoarage root doesn't match",
"Relay parent storage root doesn't match",
);
});
}
Expand All @@ -485,25 +485,38 @@ impl<T: Config> Module<T> {
downward_messages: Vec<InboundDownwardMessage>,
) -> Result<Weight, DispatchError> {
let dm_count = downward_messages.len() as u32;

let mut weight_used = 0;

// Reference fu to avoid the `move` capture.
let weight_used_mut_ref = &mut weight_used;
let result_mqc_head = LastDmqMqcHead::mutate(move |mqc| {
for downward_message in downward_messages {
mqc.extend_downward(&downward_message);
*weight_used_mut_ref += T::DownwardMessageHandlers::handle_downward_message(downward_message);
}
mqc.0
});
if dm_count != 0 {
let mut processed_count = 0;

// After hashing each message in the message queue chain submitted by the collator, we should
// arrive to the MQC head provided by the relay chain.
ensure!(
result_mqc_head == expected_dmq_mqc_head,
Error::<T>::DmpMqcMismatch
);
Self::deposit_event(RawEvent::DownwardMessagesReceived(dm_count));

// Reference fu to avoid the `move` capture.
let weight_used_ref = &mut weight_used;
let processed_count_ref = &mut processed_count;
let result_mqc_head = LastDmqMqcHead::mutate(move |mqc| {
for downward_message in downward_messages {
mqc.extend_downward(&downward_message);
*weight_used_ref += T::DownwardMessageHandlers::handle_downward_message(downward_message);
*processed_count_ref += 1;
}
mqc.0
});

Self::deposit_event(RawEvent::DownwardMessagesProcessed(
processed_count,
weight_used,
result_mqc_head.clone(),
expected_dmq_mqc_head.clone(),
));

// After hashing each message in the message queue chain submitted by the collator, we should
// arrive to the MQC head provided by the relay chain.
assert_eq!(result_mqc_head, expected_dmq_mqc_head);
} else {
assert_eq!(LastDmqMqcHead::get().0, expected_dmq_mqc_head);
}

// Store the processed_downward_messages here so that it will be accessible from
// PVF's `validate_block` wrapper and collation pipeline.
Expand Down Expand Up @@ -791,12 +804,18 @@ impl<T: Config> ProvideInherent for Module<T> {

decl_event! {
pub enum Event<T> where Hash = <T as frame_system::Config>::Hash {
// The validation function has been scheduled to apply as of the contained relay chain block number.
/// The validation function has been scheduled to apply as of the contained relay chain block number.
ValidationFunctionStored(RelayChainBlockNumber),
// The validation function was applied as of the contained relay chain block number.
/// The validation function was applied as of the contained relay chain block number.
ValidationFunctionApplied(RelayChainBlockNumber),
// An upgrade has been authorized.
/// An upgrade has been authorized.
UpgradeAuthorized(Hash),
/// Downward messages were processed using the given weight.
/// \[ count, weight_used, result_mqc_head, expected_mqc_head \]
DownwardMessagesProcessed(u32, Weight, relay_chain::Hash, relay_chain::Hash),
/// Some downward messages have been received and will be processed.
/// \[ count \]
DownwardMessagesReceived(u32),
}
}

Expand Down
1 change: 1 addition & 0 deletions pallets/xcm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ codec = { package = "parity-scale-codec", version = "2.0.0", default-features =
serde = { version = "1.0.101", optional = true, features = ["derive"] }

sp-std = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" }
sp-io = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" }
sp-runtime = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" }
frame-support = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" }
frame-system = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" }
Expand Down
62 changes: 59 additions & 3 deletions pallets/xcm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@

#![cfg_attr(not(feature = "std"), no_std)]

use cumulus_primitives_core::ParaId;
use sp_std::convert::TryFrom;
use cumulus_primitives_core::{ParaId, DownwardMessageHandler, InboundDownwardMessage};
use codec::{Encode, Decode};
use sp_runtime::traits::BadOrigin;
use xcm::{VersionedXcm, v0::{Xcm, Junction, Outcome, ExecuteXcm}};
use frame_support::{traits::Get, dispatch::Weight};
pub use pallet::*;

#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;

Expand All @@ -36,16 +40,68 @@ pub mod pallet {

/// The module configuration trait.
#[pallet::config]
pub trait Config: frame_system::Config {}
pub trait Config: frame_system::Config {
/// The overarching event type.
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;

type XcmExecutor: ExecuteXcm<Self::Call>;

#[pallet::constant]
type MaxWeight: Get<Weight>;
}

#[pallet::error]
pub enum Error<T> {}
pub enum Error<T> {
}

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}

#[pallet::call]
impl<T: Config> Pallet<T> {}

#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
#[pallet::metadata(T::BlockNumber = "BlockNumber")]
pub enum Event<T: Config> {
/// Downward message is invalid XCM.
/// \[ id \]
InvalidFormat([u8; 8]),
/// Downward message is unsupported version of XCM.
/// \[ id \]
UnsupportedVersion([u8; 8]),
/// Downward message executed with the given outcome.
/// \[ id, outcome \]
ExecutedDownward([u8; 8], Outcome),
}

/// For an incoming downward message, this just adapts an XCM executor and executes DMP messages
/// immediately up until some `MaxWeight` at which point it errors. Their origin is asserted to be
/// the Parent location.
impl<T: Config> DownwardMessageHandler for Pallet<T> {
fn handle_downward_message(msg: InboundDownwardMessage) -> Weight {
let id = sp_io::hashing::twox_64(&msg.msg[..]);
let msg = VersionedXcm::<T::Call>::decode(&mut &msg.msg[..])
.map(Xcm::<T::Call>::try_from);
match msg {
Ok(Ok(x)) => {
let weight_limit = T::MaxWeight::get();
let outcome = T::XcmExecutor::execute_xcm(Junction::Parent.into(), x, weight_limit);
let weight_used = outcome.weight_used();
Self::deposit_event(Event::ExecutedDownward(id, outcome));
weight_used
}
Ok(Err(())) => {
Self::deposit_event(Event::UnsupportedVersion(id));
0
},
Err(_) => {
Self::deposit_event(Event::InvalidFormat(id));
0
},
}
}
}
}

/// Origin for the parachains module.
Expand Down
6 changes: 6 additions & 0 deletions rococo-parachains/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ sp-inherents = { git = "https://github.com/paritytech/substrate", default-featur
frame-support = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" }
frame-executive = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" }
frame-system = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" }
pallet-assets = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" }
pallet-balances = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" }
pallet-randomness-collective-flip = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" }
pallet-timestamp = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" }
Expand All @@ -48,6 +49,10 @@ xcm-builder = { git = "https://github.com/paritytech/polkadot", default-features
xcm-executor = { git = "https://github.com/paritytech/polkadot", default-features = false, branch = "master" }
pallet-xcm = { git = "https://github.com/paritytech/polkadot", default-features = false, branch = "master" }

[dev-dependencies]
hex-literal = "0.3.1"
hex = "0.4.3"

[build-dependencies]
substrate-wasm-builder = "3.0.0"

Expand All @@ -71,6 +76,7 @@ std = [
"frame-support/std",
"frame-executive/std",
"frame-system/std",
"pallet-assets/std",
"pallet-balances/std",
"pallet-randomness-collective-flip/std",
"pallet-timestamp/std",
Expand Down
Loading

0 comments on commit 0ab15b2

Please sign in to comment.