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;
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, Eq, Clone, Encode, Decode, DecodeWithMemTracking, TypeInfo, Debug, MaxEncodedLen,
64	)]
65	#[pallet::origin]
66	pub enum Origin {
67		/// It comes from the (parent) relay chain.
68		Relay,
69		/// It comes from a (sibling) parachain.
70		SiblingParachain(ParaId),
71	}
72
73	#[pallet::call]
74	impl<T: Config> Pallet<T> {}
75
76	impl From<ParaId> for Origin {
77		fn from(id: ParaId) -> Origin {
78			Origin::SiblingParachain(id)
79		}
80	}
81	impl From<u32> for Origin {
82		fn from(id: u32) -> Origin {
83			Origin::SiblingParachain(id.into())
84		}
85	}
86}
87
88/// Ensure that the origin `o` represents a sibling parachain.
89/// Returns `Ok` with the parachain ID of the sibling or an `Err` otherwise.
90pub fn ensure_sibling_para<OuterOrigin>(o: OuterOrigin) -> Result<ParaId, BadOrigin>
91where
92	OuterOrigin: Into<Result<Origin, OuterOrigin>>,
93{
94	match o.into() {
95		Ok(Origin::SiblingParachain(id)) => Ok(id),
96		_ => Err(BadOrigin),
97	}
98}
99
100/// Ensure that the origin `o` represents is the relay chain.
101/// Returns `Ok` if it does or an `Err` otherwise.
102pub fn ensure_relay<OuterOrigin>(o: OuterOrigin) -> Result<(), BadOrigin>
103where
104	OuterOrigin: Into<Result<Origin, OuterOrigin>>,
105{
106	match o.into() {
107		Ok(Origin::Relay) => Ok(()),
108		_ => Err(BadOrigin),
109	}
110}