git.delta.rocks / unique-network / refs/commits / 1f5718ca8c83

difftreelog

source

runtime/common/config/xcm/mod.rs11.0 KiBsourcehistory
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617use frame_support::{18	parameter_types,19	traits::{ConstU32, Contains, Everything, Get, Nothing, ProcessMessageError},20};21use frame_system::EnsureRoot;22use pallet_xcm::XcmPassthrough;23use polkadot_parachain_primitives::primitives::Sibling;24use sp_std::marker::PhantomData;25use staging_xcm::{26	latest::{prelude::*, MultiLocation, Weight},27	v3::Instruction,28};29use staging_xcm_builder::{30	AccountId32Aliases, EnsureXcmOrigin, FixedWeightBounds, ParentAsSuperuser, ParentIsPreset,31	RelayChainAsNative, SiblingParachainAsNative, SiblingParachainConvertsVia,32	SignedAccountId32AsNative, SignedToAccountId32, SovereignSignedViaLocation,33};34use staging_xcm_executor::{35	traits::{Properties, ShouldExecute},36	XcmExecutor,37};38use up_common::types::AccountId;3940use crate::{41	xcm_barrier::Barrier, AllPalletsWithSystem, Balances, ParachainInfo, ParachainSystem,42	PolkadotXcm, RelayNetwork, Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin, XcmpQueue,43};4445#[cfg(feature = "foreign-assets")]46pub mod foreignassets;4748#[cfg(not(feature = "foreign-assets"))]49pub mod nativeassets;5051#[cfg(feature = "foreign-assets")]52pub use foreignassets as xcm_assets;53#[cfg(not(feature = "foreign-assets"))]54pub use nativeassets as xcm_assets;55use xcm_assets::{AssetTransactor, IsReserve, Trader};5657#[cfg(feature = "governance")]58use crate::runtime_common::config::governance;5960parameter_types! {61	pub const RelayLocation: MultiLocation = MultiLocation::parent();62	pub RelayOrigin: RuntimeOrigin = cumulus_pallet_xcm::Origin::Relay.into();63	pub UniversalLocation: InteriorMultiLocation = (64		GlobalConsensus(crate::RelayNetwork::get()),65		Parachain(ParachainInfo::get().into()),66	).into();67	pub SelfLocation: MultiLocation = MultiLocation::new(1, X1(Parachain(ParachainInfo::get().into())));6869	// One XCM operation is 1_000_000 weight - almost certainly a conservative estimate.70	pub UnitWeightCost: Weight = Weight::from_parts(1_000_000, 1000); // ?71	pub const MaxInstructions: u32 = 100;72}7374/// Type for specifying how a `MultiLocation` can be converted into an `AccountId`. This is used75/// when determining ownership of accounts for asset transacting and when attempting to use XCM76/// `Transact` in order to determine the dispatch Origin.77pub type LocationToAccountId = (78	// The parent (Relay-chain) origin converts to the default `AccountId`.79	ParentIsPreset<AccountId>,80	// Sibling parachain origins convert to AccountId via the `ParaId::into`.81	SiblingParachainConvertsVia<Sibling, AccountId>,82	// Straight up local `AccountId32` origins just alias directly to `AccountId`.83	AccountId32Aliases<RelayNetwork, AccountId>,84);8586/// No local origins on this chain are allowed to dispatch XCM sends/executions.87pub type LocalOriginToLocation = (SignedToAccountId32<RuntimeOrigin, AccountId, RelayNetwork>,);8889/// The means for routing XCM messages which are not for local execution into the right message90/// queues.91pub type XcmRouter = (92	// Two routers - use UMP to communicate with the relay chain:93	cumulus_primitives_utility::ParentAsUmp<ParachainSystem, PolkadotXcm, ()>,94	// ..and XCMP to communicate with the sibling chains.95	XcmpQueue,96);9798/// This is the type we use to convert an (incoming) XCM origin into a local `Origin` instance,99/// ready for dispatching a transaction with Xcm's `Transact`. There is an `OriginKind` which can100/// biases the kind of local `Origin` it will become.101pub type XcmOriginToTransactDispatchOrigin = (102	// Sovereign account converter; this attempts to derive an `AccountId` from the origin location103	// using `LocationToAccountId` and then turn that into the usual `Signed` origin. Useful for104	// foreign chains who want to have a local sovereign account on this chain which they control.105	SovereignSignedViaLocation<LocationToAccountId, RuntimeOrigin>,106	// Native converter for Relay-chain (Parent) location; will converts to a `Relay` origin when107	// recognised.108	RelayChainAsNative<RelayOrigin, RuntimeOrigin>,109	// Native converter for sibling Parachains; will convert to a `SiblingPara` origin when110	// recognised.111	SiblingParachainAsNative<cumulus_pallet_xcm::Origin, RuntimeOrigin>,112	// Superuser converter for the Relay-chain (Parent) location. This will allow it to issue a113	// transaction from the Root origin.114	ParentAsSuperuser<RuntimeOrigin>,115	// Native signed account converter; this just converts an `AccountId32` origin into a normal116	// `Origin::Signed` origin of the same 32-byte value.117	SignedAccountId32AsNative<RelayNetwork, RuntimeOrigin>,118	// Xcm origins can be represented natively under the Xcm pallet's Xcm origin.119	XcmPassthrough<RuntimeOrigin>,120);121122pub trait TryPass {123	fn try_pass<Call>(124		origin: &MultiLocation,125		message: &mut [Instruction<Call>],126	) -> Result<(), ProcessMessageError>;127}128129#[impl_trait_for_tuples::impl_for_tuples(30)]130impl TryPass for Tuple {131	fn try_pass<Call>(132		origin: &MultiLocation,133		message: &mut [Instruction<Call>],134	) -> Result<(), ProcessMessageError> {135		for_tuples!( #(136			Tuple::try_pass(origin, message)?;137		)* );138139		Ok(())140	}141}142143/// Deny executing the XCM if it matches any of the Deny filter regardless of anything else.144/// If it passes the Deny, and matches one of the Allow cases then it is let through.145pub struct DenyThenTry<Deny, Allow>(PhantomData<Deny>, PhantomData<Allow>)146where147	Deny: TryPass,148	Allow: ShouldExecute;149150impl<Deny, Allow> ShouldExecute for DenyThenTry<Deny, Allow>151where152	Deny: TryPass,153	Allow: ShouldExecute,154{155	fn should_execute<Call>(156		origin: &MultiLocation,157		message: &mut [Instruction<Call>],158		max_weight: Weight,159		properties: &mut Properties,160	) -> Result<(), ProcessMessageError> {161		Deny::try_pass(origin, message)?;162		Allow::should_execute(origin, message, max_weight, properties)163	}164}165166pub type Weigher = FixedWeightBounds<UnitWeightCost, RuntimeCall, MaxInstructions>;167168pub struct XcmCallFilter;169impl XcmCallFilter {170	fn allow_gov_and_sys_call(call: &RuntimeCall) -> bool {171		match call {172			RuntimeCall::System(..) => true,173174			#[cfg(feature = "governance")]175			RuntimeCall::Identity(..)176			| RuntimeCall::Preimage(..)177			| RuntimeCall::Democracy(..)178			| RuntimeCall::Council(..)179			| RuntimeCall::TechnicalCommittee(..)180			| RuntimeCall::CouncilMembership(..)181			| RuntimeCall::TechnicalCommitteeMembership(..)182			| RuntimeCall::FellowshipCollective(..)183			| RuntimeCall::FellowshipReferenda(..) => true,184			_ => false,185		}186	}187188	fn allow_utility_call(call: &RuntimeCall) -> bool {189		match call {190			RuntimeCall::Utility(pallet_utility::Call::batch { calls, .. }) => {191				calls.iter().all(Self::allow_gov_and_sys_call)192			}193			RuntimeCall::Utility(pallet_utility::Call::batch_all { calls, .. }) => {194				calls.iter().all(Self::allow_gov_and_sys_call)195			}196			RuntimeCall::Utility(pallet_utility::Call::as_derivative { call, .. }) => {197				Self::allow_gov_and_sys_call(call)198			}199			RuntimeCall::Utility(pallet_utility::Call::dispatch_as { call, .. }) => {200				Self::allow_gov_and_sys_call(call)201			}202			RuntimeCall::Utility(pallet_utility::Call::force_batch { calls, .. }) => {203				calls.iter().all(Self::allow_gov_and_sys_call)204			}205			_ => false,206		}207	}208}209210impl Contains<RuntimeCall> for XcmCallFilter {211	fn contains(call: &RuntimeCall) -> bool {212		Self::allow_gov_and_sys_call(call) || Self::allow_utility_call(call)213	}214}215216pub struct XcmExecutorConfig<T>(PhantomData<T>);217impl<T> staging_xcm_executor::Config for XcmExecutorConfig<T>218where219	T: pallet_configuration::Config,220{221	type RuntimeCall = RuntimeCall;222	type XcmSender = XcmRouter;223	// How to withdraw and deposit an asset.224	type AssetTransactor = AssetTransactor;225	type OriginConverter = XcmOriginToTransactDispatchOrigin;226	type IsReserve = IsReserve;227	type IsTeleporter = (); // Teleportation is disabled228	type UniversalLocation = UniversalLocation;229	type Barrier = Barrier;230	type Weigher = Weigher;231	type Trader = Trader<T>;232	type ResponseHandler = PolkadotXcm;233	type SubscriptionService = PolkadotXcm;234	type PalletInstancesInfo = AllPalletsWithSystem;235	type MaxAssetsIntoHolding = ConstU32<8>;236237	type AssetTrap = PolkadotXcm;238	type AssetClaims = PolkadotXcm;239	type AssetLocker = ();240	type AssetExchanger = ();241	type FeeManager = ();242	type MessageExporter = ();243	type UniversalAliases = Nothing;244	type CallDispatcher = RuntimeCall;245	type SafeCallFilter = XcmCallFilter;246	type Aliasers = Nothing;247}248249#[cfg(feature = "runtime-benchmarks")]250parameter_types! {251	pub ReachableDest: Option<MultiLocation> = Some(Parent.into());252}253254impl pallet_xcm::Config for Runtime {255	type RuntimeEvent = RuntimeEvent;256	type SendXcmOrigin = EnsureXcmOrigin<RuntimeOrigin, ()>;257	type XcmRouter = XcmRouter;258	type ExecuteXcmOrigin = EnsureXcmOrigin<RuntimeOrigin, LocalOriginToLocation>;259	type XcmExecuteFilter = Everything;260	type XcmExecutor = XcmExecutor<XcmExecutorConfig<Self>>;261	type XcmTeleportFilter = Everything;262	type XcmReserveTransferFilter = Everything;263	type Weigher = FixedWeightBounds<UnitWeightCost, RuntimeCall, MaxInstructions>;264	type RuntimeOrigin = RuntimeOrigin;265	type RuntimeCall = RuntimeCall;266	const VERSION_DISCOVERY_QUEUE_SIZE: u32 = 100;267	type AdvertisedXcmVersion = pallet_xcm::CurrentXcmVersion;268	type UniversalLocation = UniversalLocation;269	type Currency = Balances;270	type CurrencyMatcher = ();271	type TrustedLockers = ();272	type SovereignAccountOf = LocationToAccountId;273	type MaxLockers = ConstU32<8>;274	type WeightInfo = crate::weights::xcm::SubstrateWeight<Runtime>;275	type AdminOrigin = EnsureRoot<AccountId>;276	type MaxRemoteLockConsumers = ConstU32<0>;277	type RemoteLockConsumerIdentifier = ();278	#[cfg(feature = "runtime-benchmarks")]279	type ReachableDest = ReachableDest;280}281282impl cumulus_pallet_xcm::Config for Runtime {283	type RuntimeEvent = RuntimeEvent;284	type XcmExecutor = XcmExecutor<XcmExecutorConfig<Self>>;285}286287impl cumulus_pallet_xcmp_queue::Config for Runtime {288	type WeightInfo = ();289	type RuntimeEvent = RuntimeEvent;290	type XcmExecutor = XcmExecutor<XcmExecutorConfig<Self>>;291	type ChannelInfo = ParachainSystem;292	type VersionWrapper = PolkadotXcm;293	type ExecuteOverweightOrigin = frame_system::EnsureRoot<AccountId>;294295	#[cfg(feature = "governance")]296	type ControllerOrigin = governance::RootOrTechnicalCommitteeMember;297298	#[cfg(not(feature = "governance"))]299	type ControllerOrigin = frame_system::EnsureRoot<AccountId>;300301	type ControllerOriginConverter = XcmOriginToTransactDispatchOrigin;302	type PriceForSiblingDelivery = ();303}304305impl cumulus_pallet_dmp_queue::Config for Runtime {306	type RuntimeEvent = RuntimeEvent;307	type XcmExecutor = XcmExecutor<XcmExecutorConfig<Self>>;308	type ExecuteOverweightOrigin = frame_system::EnsureRoot<AccountId>;309}