referrerpolicy=no-referrer-when-downgrade

cumulus_pallet_xcm/
lib.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Cumulus.
3// SPDX-License-Identifier: Apache-2.0
4
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// 	http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17//! Pallet for stuff specific to parachains' usage of XCM. Right now that's just the origin
18//! used by parachains when receiving `Transact` messages from other parachains or the Relay chain
19//! which must be natively represented.
20
21#![cfg_attr(not(feature = "std"), no_std)]
22
23use codec::{Decode, Encode};
24use cumulus_primitives_core::ParaId;
25pub use pallet::*;
26use scale_info::TypeInfo;
27use sp_runtime::{traits::BadOrigin, RuntimeDebug};
28use xcm::latest::{ExecuteXcm, Outcome};
29
30#[frame_support::pallet]
31pub mod pallet {
32	use super::*;
33	use frame_support::pallet_prelude::*;
34
35	#[pallet::pallet]
36	pub struct Pallet<T>(_);
37
38	/// The module configuration trait.
39	#[pallet::config]
40	pub trait Config: frame_system::Config {
41		/// The overarching event type.
42		#[allow(deprecated)]
43		type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
44
45		type XcmExecutor: ExecuteXcm<Self::RuntimeCall>;
46	}
47
48	#[pallet::event]
49	pub enum Event<T: Config> {
50		/// Downward message is invalid XCM.
51		/// \[ id \]
52		InvalidFormat([u8; 32]),
53		/// Downward message is unsupported version of XCM.
54		/// \[ id \]
55		UnsupportedVersion([u8; 32]),
56		/// Downward message executed with the given outcome.
57		/// \[ id, outcome \]
58		ExecutedDownward([u8; 32], Outcome),
59	}
60
61	/// Origin for the parachains module.
62	#[derive(
63		PartialEq,
64		Eq,
65		Clone,
66		Encode,
67		Decode,
68		DecodeWithMemTracking,
69		TypeInfo,
70		RuntimeDebug,
71		MaxEncodedLen,
72	)]
73	#[pallet::origin]
74	pub enum Origin {
75		/// It comes from the (parent) relay chain.
76		Relay,
77		/// It comes from a (sibling) parachain.
78		SiblingParachain(ParaId),
79	}
80
81	#[pallet::call]
82	impl<T: Config> Pallet<T> {}
83
84	impl From<ParaId> for Origin {
85		fn from(id: ParaId) -> Origin {
86			Origin::SiblingParachain(id)
87		}
88	}
89	impl From<u32> for Origin {
90		fn from(id: u32) -> Origin {
91			Origin::SiblingParachain(id.into())
92		}
93	}
94}
95
96/// Ensure that the origin `o` represents a sibling parachain.
97/// Returns `Ok` with the parachain ID of the sibling or an `Err` otherwise.
98pub fn ensure_sibling_para<OuterOrigin>(o: OuterOrigin) -> Result<ParaId, BadOrigin>
99where
100	OuterOrigin: Into<Result<Origin, OuterOrigin>>,
101{
102	match o.into() {
103		Ok(Origin::SiblingParachain(id)) => Ok(id),
104		_ => Err(BadOrigin),
105	}
106}
107
108/// Ensure that the origin `o` represents is the relay chain.
109/// Returns `Ok` if it does or an `Err` otherwise.
110pub fn ensure_relay<OuterOrigin>(o: OuterOrigin) -> Result<(), BadOrigin>
111where
112	OuterOrigin: Into<Result<Origin, OuterOrigin>>,
113{
114	match o.into() {
115		Ok(Origin::Relay) => Ok(()),
116		_ => Err(BadOrigin),
117	}
118}