difftreelog
feat allow xcm transact for sys,gov and utility
in: master
1 file changed
runtime/common/config/xcm/mod.rsdiffbeforeafterboth1// 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 traits::{Everything, Nothing, Get, ConstU32, ProcessMessageError},19 parameter_types,20};21use frame_system::EnsureRoot;22use pallet_xcm::XcmPassthrough;23use polkadot_parachain::primitives::Sibling;24use xcm::latest::{prelude::*, Weight, MultiLocation};25use xcm::v3::Instruction;26use xcm_builder::{27 AccountId32Aliases, EnsureXcmOrigin, FixedWeightBounds, ParentAsSuperuser, RelayChainAsNative,28 SiblingParachainAsNative, SiblingParachainConvertsVia, SignedAccountId32AsNative,29 SignedToAccountId32, SovereignSignedViaLocation, ParentIsPreset,30};31use xcm_executor::{XcmExecutor, traits::ShouldExecute};32use sp_std::marker::PhantomData;33use crate::{34 Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin, ParachainInfo, ParachainSystem, PolkadotXcm,35 XcmpQueue, xcm_barrier::Barrier, RelayNetwork, AllPalletsWithSystem, Balances,36};3738use up_common::types::AccountId;3940#[cfg(feature = "foreign-assets")]41pub mod foreignassets;4243#[cfg(not(feature = "foreign-assets"))]44pub mod nativeassets;4546#[cfg(feature = "foreign-assets")]47pub use foreignassets as xcm_assets;4849#[cfg(not(feature = "foreign-assets"))]50pub use nativeassets as xcm_assets;5152#[cfg(feature = "governance")]53use crate::runtime_common::config::governance;5455use xcm_assets::{AssetTransactor, IsReserve, Trader};5657parameter_types! {58 pub const RelayLocation: MultiLocation = MultiLocation::parent();59 pub RelayOrigin: RuntimeOrigin = cumulus_pallet_xcm::Origin::Relay.into();60 pub UniversalLocation: InteriorMultiLocation = (61 GlobalConsensus(crate::RelayNetwork::get()),62 Parachain(ParachainInfo::get().into()),63 ).into();64 pub SelfLocation: MultiLocation = MultiLocation::new(1, X1(Parachain(ParachainInfo::get().into())));6566 // One XCM operation is 1_000_000 weight - almost certainly a conservative estimate.67 pub UnitWeightCost: Weight = Weight::from_parts(1_000_000, 1000); // ?68 pub const MaxInstructions: u32 = 100;69}7071/// Type for specifying how a `MultiLocation` can be converted into an `AccountId`. This is used72/// when determining ownership of accounts for asset transacting and when attempting to use XCM73/// `Transact` in order to determine the dispatch Origin.74pub type LocationToAccountId = (75 // The parent (Relay-chain) origin converts to the default `AccountId`.76 ParentIsPreset<AccountId>,77 // Sibling parachain origins convert to AccountId via the `ParaId::into`.78 SiblingParachainConvertsVia<Sibling, AccountId>,79 // Straight up local `AccountId32` origins just alias directly to `AccountId`.80 AccountId32Aliases<RelayNetwork, AccountId>,81);8283/// No local origins on this chain are allowed to dispatch XCM sends/executions.84pub type LocalOriginToLocation = (SignedToAccountId32<RuntimeOrigin, AccountId, RelayNetwork>,);8586/// The means for routing XCM messages which are not for local execution into the right message87/// queues.88pub type XcmRouter = (89 // Two routers - use UMP to communicate with the relay chain:90 cumulus_primitives_utility::ParentAsUmp<ParachainSystem, PolkadotXcm, ()>,91 // ..and XCMP to communicate with the sibling chains.92 XcmpQueue,93);9495/// This is the type we use to convert an (incoming) XCM origin into a local `Origin` instance,96/// ready for dispatching a transaction with Xcm's `Transact`. There is an `OriginKind` which can97/// biases the kind of local `Origin` it will become.98pub type XcmOriginToTransactDispatchOrigin = (99 // Sovereign account converter; this attempts to derive an `AccountId` from the origin location100 // using `LocationToAccountId` and then turn that into the usual `Signed` origin. Useful for101 // foreign chains who want to have a local sovereign account on this chain which they control.102 SovereignSignedViaLocation<LocationToAccountId, RuntimeOrigin>,103 // Native converter for Relay-chain (Parent) location; will converts to a `Relay` origin when104 // recognised.105 RelayChainAsNative<RelayOrigin, RuntimeOrigin>,106 // Native converter for sibling Parachains; will convert to a `SiblingPara` origin when107 // recognised.108 SiblingParachainAsNative<cumulus_pallet_xcm::Origin, RuntimeOrigin>,109 // Superuser converter for the Relay-chain (Parent) location. This will allow it to issue a110 // transaction from the Root origin.111 ParentAsSuperuser<RuntimeOrigin>,112 // Native signed account converter; this just converts an `AccountId32` origin into a normal113 // `Origin::Signed` origin of the same 32-byte value.114 SignedAccountId32AsNative<RelayNetwork, RuntimeOrigin>,115 // Xcm origins can be represented natively under the Xcm pallet's Xcm origin.116 XcmPassthrough<RuntimeOrigin>,117);118119pub trait TryPass {120 fn try_pass<Call>(121 origin: &MultiLocation,122 message: &mut [Instruction<Call>],123 ) -> Result<(), ProcessMessageError>;124}125126#[impl_trait_for_tuples::impl_for_tuples(30)]127impl TryPass for Tuple {128 fn try_pass<Call>(129 origin: &MultiLocation,130 message: &mut [Instruction<Call>],131 ) -> Result<(), ProcessMessageError> {132 for_tuples!( #(133 Tuple::try_pass(origin, message)?;134 )* );135136 Ok(())137 }138}139140/// Deny executing the XCM if it matches any of the Deny filter regardless of anything else.141/// If it passes the Deny, and matches one of the Allow cases then it is let through.142pub struct DenyThenTry<Deny, Allow>(PhantomData<Deny>, PhantomData<Allow>)143where144 Deny: TryPass,145 Allow: ShouldExecute;146147impl<Deny, Allow> ShouldExecute for DenyThenTry<Deny, Allow>148where149 Deny: TryPass,150 Allow: ShouldExecute,151{152 fn should_execute<Call>(153 origin: &MultiLocation,154 message: &mut [Instruction<Call>],155 max_weight: Weight,156 weight_credit: &mut Weight,157 ) -> Result<(), ProcessMessageError> {158 Deny::try_pass(origin, message)?;159 Allow::should_execute(origin, message, max_weight, weight_credit)160 }161}162163pub type Weigher = FixedWeightBounds<UnitWeightCost, RuntimeCall, MaxInstructions>;164165pub struct XcmExecutorConfig<T>(PhantomData<T>);166impl<T> xcm_executor::Config for XcmExecutorConfig<T>167where168 T: pallet_configuration::Config,169{170 type RuntimeCall = RuntimeCall;171 type XcmSender = XcmRouter;172 // How to withdraw and deposit an asset.173 type AssetTransactor = AssetTransactor;174 type OriginConverter = XcmOriginToTransactDispatchOrigin;175 type IsReserve = IsReserve;176 type IsTeleporter = (); // Teleportation is disabled177 type UniversalLocation = UniversalLocation;178 type Barrier = Barrier;179 type Weigher = Weigher;180 type Trader = Trader<T>;181 type ResponseHandler = PolkadotXcm;182 type SubscriptionService = PolkadotXcm;183 type PalletInstancesInfo = AllPalletsWithSystem;184 type MaxAssetsIntoHolding = ConstU32<8>;185186 type AssetTrap = PolkadotXcm;187 type AssetClaims = PolkadotXcm;188 type AssetLocker = ();189 type AssetExchanger = ();190 type FeeManager = ();191 type MessageExporter = ();192 type UniversalAliases = Nothing;193 type CallDispatcher = RuntimeCall;194195 // Deny all XCM Transacts.196 type SafeCallFilter = Nothing;197}198199#[cfg(feature = "runtime-benchmarks")]200parameter_types! {201 pub ReachableDest: Option<MultiLocation> = Some(Parent.into());202}203204impl pallet_xcm::Config for Runtime {205 type RuntimeEvent = RuntimeEvent;206 type SendXcmOrigin = EnsureXcmOrigin<RuntimeOrigin, ()>;207 type XcmRouter = XcmRouter;208 type ExecuteXcmOrigin = EnsureXcmOrigin<RuntimeOrigin, LocalOriginToLocation>;209 type XcmExecuteFilter = Everything;210 type XcmExecutor = XcmExecutor<XcmExecutorConfig<Self>>;211 type XcmTeleportFilter = Everything;212 type XcmReserveTransferFilter = Everything;213 type Weigher = FixedWeightBounds<UnitWeightCost, RuntimeCall, MaxInstructions>;214 type RuntimeOrigin = RuntimeOrigin;215 type RuntimeCall = RuntimeCall;216 const VERSION_DISCOVERY_QUEUE_SIZE: u32 = 100;217 type AdvertisedXcmVersion = pallet_xcm::CurrentXcmVersion;218 type UniversalLocation = UniversalLocation;219 type Currency = Balances;220 type CurrencyMatcher = ();221 type TrustedLockers = ();222 type SovereignAccountOf = LocationToAccountId;223 type MaxLockers = ConstU32<8>;224 type WeightInfo = crate::weights::xcm::SubstrateWeight<Runtime>;225 type AdminOrigin = EnsureRoot<AccountId>;226 type MaxRemoteLockConsumers = ConstU32<0>;227 type RemoteLockConsumerIdentifier = ();228 #[cfg(feature = "runtime-benchmarks")]229 type ReachableDest = ReachableDest;230}231232impl cumulus_pallet_xcm::Config for Runtime {233 type RuntimeEvent = RuntimeEvent;234 type XcmExecutor = XcmExecutor<XcmExecutorConfig<Self>>;235}236237impl cumulus_pallet_xcmp_queue::Config for Runtime {238 type WeightInfo = ();239 type RuntimeEvent = RuntimeEvent;240 type XcmExecutor = XcmExecutor<XcmExecutorConfig<Self>>;241 type ChannelInfo = ParachainSystem;242 type VersionWrapper = PolkadotXcm;243 type ExecuteOverweightOrigin = frame_system::EnsureRoot<AccountId>;244245 #[cfg(feature = "governance")]246 type ControllerOrigin = governance::RootOrTechnicalCommitteeMember;247248 #[cfg(not(feature = "governance"))]249 type ControllerOrigin = frame_system::EnsureRoot<AccountId>;250251 type ControllerOriginConverter = XcmOriginToTransactDispatchOrigin;252 type PriceForSiblingDelivery = ();253}254255impl cumulus_pallet_dmp_queue::Config for Runtime {256 type RuntimeEvent = RuntimeEvent;257 type XcmExecutor = XcmExecutor<XcmExecutorConfig<Self>>;258 type ExecuteOverweightOrigin = frame_system::EnsureRoot<AccountId>;259}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 traits::{Everything, Nothing, Get, ConstU32, ProcessMessageError, Contains},19 parameter_types,20};21use frame_system::EnsureRoot;22use pallet_xcm::XcmPassthrough;23use polkadot_parachain::primitives::Sibling;24use xcm::latest::{prelude::*, Weight, MultiLocation};25use xcm::v3::Instruction;26use xcm_builder::{27 AccountId32Aliases, EnsureXcmOrigin, FixedWeightBounds, ParentAsSuperuser, RelayChainAsNative,28 SiblingParachainAsNative, SiblingParachainConvertsVia, SignedAccountId32AsNative,29 SignedToAccountId32, SovereignSignedViaLocation, ParentIsPreset,30};31use xcm_executor::{XcmExecutor, traits::ShouldExecute};32use sp_std::marker::PhantomData;33use crate::{34 Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin, ParachainInfo, ParachainSystem, PolkadotXcm,35 XcmpQueue, xcm_barrier::Barrier, RelayNetwork, AllPalletsWithSystem, Balances,36};3738use up_common::types::AccountId;3940#[cfg(feature = "foreign-assets")]41pub mod foreignassets;4243#[cfg(not(feature = "foreign-assets"))]44pub mod nativeassets;4546#[cfg(feature = "foreign-assets")]47pub use foreignassets as xcm_assets;4849#[cfg(not(feature = "foreign-assets"))]50pub use nativeassets as xcm_assets;5152#[cfg(feature = "governance")]53use crate::runtime_common::config::governance;5455use xcm_assets::{AssetTransactor, IsReserve, Trader};5657parameter_types! {58 pub const RelayLocation: MultiLocation = MultiLocation::parent();59 pub RelayOrigin: RuntimeOrigin = cumulus_pallet_xcm::Origin::Relay.into();60 pub UniversalLocation: InteriorMultiLocation = (61 GlobalConsensus(crate::RelayNetwork::get()),62 Parachain(ParachainInfo::get().into()),63 ).into();64 pub SelfLocation: MultiLocation = MultiLocation::new(1, X1(Parachain(ParachainInfo::get().into())));6566 // One XCM operation is 1_000_000 weight - almost certainly a conservative estimate.67 pub UnitWeightCost: Weight = Weight::from_parts(1_000_000, 1000); // ?68 pub const MaxInstructions: u32 = 100;69}7071/// Type for specifying how a `MultiLocation` can be converted into an `AccountId`. This is used72/// when determining ownership of accounts for asset transacting and when attempting to use XCM73/// `Transact` in order to determine the dispatch Origin.74pub type LocationToAccountId = (75 // The parent (Relay-chain) origin converts to the default `AccountId`.76 ParentIsPreset<AccountId>,77 // Sibling parachain origins convert to AccountId via the `ParaId::into`.78 SiblingParachainConvertsVia<Sibling, AccountId>,79 // Straight up local `AccountId32` origins just alias directly to `AccountId`.80 AccountId32Aliases<RelayNetwork, AccountId>,81);8283/// No local origins on this chain are allowed to dispatch XCM sends/executions.84pub type LocalOriginToLocation = (SignedToAccountId32<RuntimeOrigin, AccountId, RelayNetwork>,);8586/// The means for routing XCM messages which are not for local execution into the right message87/// queues.88pub type XcmRouter = (89 // Two routers - use UMP to communicate with the relay chain:90 cumulus_primitives_utility::ParentAsUmp<ParachainSystem, PolkadotXcm, ()>,91 // ..and XCMP to communicate with the sibling chains.92 XcmpQueue,93);9495/// This is the type we use to convert an (incoming) XCM origin into a local `Origin` instance,96/// ready for dispatching a transaction with Xcm's `Transact`. There is an `OriginKind` which can97/// biases the kind of local `Origin` it will become.98pub type XcmOriginToTransactDispatchOrigin = (99 // Sovereign account converter; this attempts to derive an `AccountId` from the origin location100 // using `LocationToAccountId` and then turn that into the usual `Signed` origin. Useful for101 // foreign chains who want to have a local sovereign account on this chain which they control.102 SovereignSignedViaLocation<LocationToAccountId, RuntimeOrigin>,103 // Native converter for Relay-chain (Parent) location; will converts to a `Relay` origin when104 // recognised.105 RelayChainAsNative<RelayOrigin, RuntimeOrigin>,106 // Native converter for sibling Parachains; will convert to a `SiblingPara` origin when107 // recognised.108 SiblingParachainAsNative<cumulus_pallet_xcm::Origin, RuntimeOrigin>,109 // Superuser converter for the Relay-chain (Parent) location. This will allow it to issue a110 // transaction from the Root origin.111 ParentAsSuperuser<RuntimeOrigin>,112 // Native signed account converter; this just converts an `AccountId32` origin into a normal113 // `Origin::Signed` origin of the same 32-byte value.114 SignedAccountId32AsNative<RelayNetwork, RuntimeOrigin>,115 // Xcm origins can be represented natively under the Xcm pallet's Xcm origin.116 XcmPassthrough<RuntimeOrigin>,117);118119pub trait TryPass {120 fn try_pass<Call>(121 origin: &MultiLocation,122 message: &mut [Instruction<Call>],123 ) -> Result<(), ProcessMessageError>;124}125126#[impl_trait_for_tuples::impl_for_tuples(30)]127impl TryPass for Tuple {128 fn try_pass<Call>(129 origin: &MultiLocation,130 message: &mut [Instruction<Call>],131 ) -> Result<(), ProcessMessageError> {132 for_tuples!( #(133 Tuple::try_pass(origin, message)?;134 )* );135136 Ok(())137 }138}139140/// Deny executing the XCM if it matches any of the Deny filter regardless of anything else.141/// If it passes the Deny, and matches one of the Allow cases then it is let through.142pub struct DenyThenTry<Deny, Allow>(PhantomData<Deny>, PhantomData<Allow>)143where144 Deny: TryPass,145 Allow: ShouldExecute;146147impl<Deny, Allow> ShouldExecute for DenyThenTry<Deny, Allow>148where149 Deny: TryPass,150 Allow: ShouldExecute,151{152 fn should_execute<Call>(153 origin: &MultiLocation,154 message: &mut [Instruction<Call>],155 max_weight: Weight,156 weight_credit: &mut Weight,157 ) -> Result<(), ProcessMessageError> {158 Deny::try_pass(origin, message)?;159 Allow::should_execute(origin, message, max_weight, weight_credit)160 }161}162163pub type Weigher = FixedWeightBounds<UnitWeightCost, RuntimeCall, MaxInstructions>;164165pub struct XcmCallFilter;166impl XcmCallFilter {167 fn allow_gov_and_sys_call(call: &RuntimeCall) -> bool {168 match call {169 RuntimeCall::System(..)170 | RuntimeCall::Identity(..)171 | RuntimeCall::Preimage(..)172 | RuntimeCall::Democracy(..)173 | RuntimeCall::Council(..)174 | RuntimeCall::TechnicalCommittee(..)175 | RuntimeCall::CouncilMembership(..)176 | RuntimeCall::TechnicalCommitteeMembership(..)177 | RuntimeCall::FellowshipCollective(..)178 | RuntimeCall::FellowshipReferenda(..) => true,179 _ => false,180 }181 }182183 fn allow_utility_call(call: &RuntimeCall) -> bool {184 match call {185 RuntimeCall::Utility(pallet_utility::Call::batch { calls, .. }) => {186 calls.iter().all(|call| Self::allow_gov_and_sys_call(call))187 }188 RuntimeCall::Utility(pallet_utility::Call::batch_all { calls, .. }) => {189 calls.iter().all(|call| Self::allow_gov_and_sys_call(call))190 }191 RuntimeCall::Utility(pallet_utility::Call::as_derivative { call, .. }) => {192 Self::allow_gov_and_sys_call(call)193 }194 RuntimeCall::Utility(pallet_utility::Call::dispatch_as { call, .. }) => {195 Self::allow_gov_and_sys_call(call)196 }197 RuntimeCall::Utility(pallet_utility::Call::force_batch { calls, .. }) => {198 calls.iter().all(|call| Self::allow_gov_and_sys_call(call))199 }200 _ => false,201 }202 }203}204205impl Contains<RuntimeCall> for XcmCallFilter {206 fn contains(call: &RuntimeCall) -> bool {207 Self::allow_gov_and_sys_call(call) || Self::allow_utility_call(call)208 }209}210211pub struct XcmExecutorConfig<T>(PhantomData<T>);212impl<T> xcm_executor::Config for XcmExecutorConfig<T>213where214 T: pallet_configuration::Config,215{216 type RuntimeCall = RuntimeCall;217 type XcmSender = XcmRouter;218 // How to withdraw and deposit an asset.219 type AssetTransactor = AssetTransactor;220 type OriginConverter = XcmOriginToTransactDispatchOrigin;221 type IsReserve = IsReserve;222 type IsTeleporter = (); // Teleportation is disabled223 type UniversalLocation = UniversalLocation;224 type Barrier = Barrier;225 type Weigher = Weigher;226 type Trader = Trader<T>;227 type ResponseHandler = PolkadotXcm;228 type SubscriptionService = PolkadotXcm;229 type PalletInstancesInfo = AllPalletsWithSystem;230 type MaxAssetsIntoHolding = ConstU32<8>;231232 type AssetTrap = PolkadotXcm;233 type AssetClaims = PolkadotXcm;234 type AssetLocker = ();235 type AssetExchanger = ();236 type FeeManager = ();237 type MessageExporter = ();238 type UniversalAliases = Nothing;239 type CallDispatcher = RuntimeCall;240 type SafeCallFilter = XcmCallFilter;241}242243#[cfg(feature = "runtime-benchmarks")]244parameter_types! {245 pub ReachableDest: Option<MultiLocation> = Some(Parent.into());246}247248impl pallet_xcm::Config for Runtime {249 type RuntimeEvent = RuntimeEvent;250 type SendXcmOrigin = EnsureXcmOrigin<RuntimeOrigin, ()>;251 type XcmRouter = XcmRouter;252 type ExecuteXcmOrigin = EnsureXcmOrigin<RuntimeOrigin, LocalOriginToLocation>;253 type XcmExecuteFilter = Everything;254 type XcmExecutor = XcmExecutor<XcmExecutorConfig<Self>>;255 type XcmTeleportFilter = Everything;256 type XcmReserveTransferFilter = Everything;257 type Weigher = FixedWeightBounds<UnitWeightCost, RuntimeCall, MaxInstructions>;258 type RuntimeOrigin = RuntimeOrigin;259 type RuntimeCall = RuntimeCall;260 const VERSION_DISCOVERY_QUEUE_SIZE: u32 = 100;261 type AdvertisedXcmVersion = pallet_xcm::CurrentXcmVersion;262 type UniversalLocation = UniversalLocation;263 type Currency = Balances;264 type CurrencyMatcher = ();265 type TrustedLockers = ();266 type SovereignAccountOf = LocationToAccountId;267 type MaxLockers = ConstU32<8>;268 type WeightInfo = crate::weights::xcm::SubstrateWeight<Runtime>;269 type AdminOrigin = EnsureRoot<AccountId>;270 type MaxRemoteLockConsumers = ConstU32<0>;271 type RemoteLockConsumerIdentifier = ();272 #[cfg(feature = "runtime-benchmarks")]273 type ReachableDest = ReachableDest;274}275276impl cumulus_pallet_xcm::Config for Runtime {277 type RuntimeEvent = RuntimeEvent;278 type XcmExecutor = XcmExecutor<XcmExecutorConfig<Self>>;279}280281impl cumulus_pallet_xcmp_queue::Config for Runtime {282 type WeightInfo = ();283 type RuntimeEvent = RuntimeEvent;284 type XcmExecutor = XcmExecutor<XcmExecutorConfig<Self>>;285 type ChannelInfo = ParachainSystem;286 type VersionWrapper = PolkadotXcm;287 type ExecuteOverweightOrigin = frame_system::EnsureRoot<AccountId>;288289 #[cfg(feature = "governance")]290 type ControllerOrigin = governance::RootOrTechnicalCommitteeMember;291292 #[cfg(not(feature = "governance"))]293 type ControllerOrigin = frame_system::EnsureRoot<AccountId>;294295 type ControllerOriginConverter = XcmOriginToTransactDispatchOrigin;296 type PriceForSiblingDelivery = ();297}298299impl cumulus_pallet_dmp_queue::Config for Runtime {300 type RuntimeEvent = RuntimeEvent;301 type XcmExecutor = XcmExecutor<XcmExecutorConfig<Self>>;302 type ExecuteOverweightOrigin = frame_system::EnsureRoot<AccountId>;303}