1234567891011121314151617use frame_support::{traits::Everything, weights::Weight, parameter_types};18use frame_system::EnsureRoot;19use pallet_xcm::XcmPassthrough;20use polkadot_parachain::primitives::Sibling;21use xcm::v1::{Junction::*, MultiLocation, NetworkId};22use xcm::latest::{Instruction, Xcm};23use xcm_builder::{24 AccountId32Aliases, EnsureXcmOrigin, FixedWeightBounds, LocationInverter, ParentAsSuperuser,25 RelayChainAsNative, SiblingParachainAsNative, SiblingParachainConvertsVia,26 SignedAccountId32AsNative, SignedToAccountId32, SovereignSignedViaLocation, ParentIsPreset,27};28use xcm_executor::{Config, XcmExecutor, traits::ShouldExecute};29use sp_std::marker::PhantomData;30use crate::{31 Runtime, Call, Event, Origin, ParachainInfo, ParachainSystem, PolkadotXcm, XcmpQueue,32 xcm_config::Barrier,33};3435use up_common::types::AccountId;3637#[cfg(feature = "foreign-assets")]38mod foreignassets;3940#[cfg(not(feature = "foreign-assets"))]41mod nativeassets;4243#[cfg(feature = "foreign-assets")]44use foreignassets as xcm_assets;4546#[cfg(not(feature = "foreign-assets"))]47use nativeassets as xcm_assets;4849use xcm_assets::{AssetTransactors, IsReserve, Trader};5051parameter_types! {52 pub const RelayLocation: MultiLocation = MultiLocation::parent();53 pub const RelayNetwork: NetworkId = NetworkId::Polkadot;54 pub RelayOrigin: Origin = cumulus_pallet_xcm::Origin::Relay.into();55 pub Ancestry: MultiLocation = Parachain(ParachainInfo::parachain_id().into()).into();56}5758596061pub type LocationToAccountId = (62 63 ParentIsPreset<AccountId>,64 65 SiblingParachainConvertsVia<Sibling, AccountId>,66 67 AccountId32Aliases<RelayNetwork, AccountId>,68);697071pub type LocalOriginToLocation = (SignedToAccountId32<Origin, AccountId, RelayNetwork>,);72737475pub type XcmRouter = (76 77 cumulus_primitives_utility::ParentAsUmp<ParachainSystem, ()>,78 79 XcmpQueue,80);8182838485pub type XcmOriginToTransactDispatchOrigin = (86 87 88 89 SovereignSignedViaLocation<LocationToAccountId, Origin>,90 91 92 RelayChainAsNative<RelayOrigin, Origin>,93 94 95 SiblingParachainAsNative<cumulus_pallet_xcm::Origin, Origin>,96 97 98 ParentAsSuperuser<Origin>,99 100 101 SignedAccountId32AsNative<RelayNetwork, Origin>,102 103 XcmPassthrough<Origin>,104);105106parameter_types! {107 108 pub UnitWeightCost: Weight = 1_000_000;109 pub const MaxInstructions: u32 = 100;110}111112pub trait TryPass {113 fn try_pass<Call>(origin: &MultiLocation, message: &mut Xcm<Call>) -> Result<(), ()>;114}115116#[impl_trait_for_tuples::impl_for_tuples(30)]117impl TryPass for Tuple {118 fn try_pass<Call>(origin: &MultiLocation, message: &mut Xcm<Call>) -> Result<(), ()> {119 for_tuples!( #(120 Tuple::try_pass(origin, message)?;121 )* );122123 Ok(())124 }125}126127pub struct DenyTransact;128impl TryPass for DenyTransact {129 fn try_pass<Call>(_origin: &MultiLocation, message: &mut Xcm<Call>) -> Result<(), ()> {130 let transact_inst = message131 .0132 .iter()133 .find(|inst| matches![inst, Instruction::Transact { .. }]);134135 match transact_inst {136 Some(_) => {137 log::warn!(138 target: "xcm::barrier",139 "transact XCM rejected"140 );141142 Err(())143 }144 None => Ok(()),145 }146 }147}148149150151pub struct DenyThenTry<Deny, Allow>(PhantomData<Deny>, PhantomData<Allow>)152where153 Deny: TryPass,154 Allow: ShouldExecute;155156impl<Deny, Allow> ShouldExecute for DenyThenTry<Deny, Allow>157where158 Deny: TryPass,159 Allow: ShouldExecute,160{161 fn should_execute<Call>(162 origin: &MultiLocation,163 message: &mut Xcm<Call>,164 max_weight: Weight,165 weight_credit: &mut Weight,166 ) -> Result<(), ()> {167 Deny::try_pass(origin, message)?;168 Allow::should_execute(origin, message, max_weight, weight_credit)169 }170}171172pub struct XcmConfig<T>(PhantomData<T>);173impl<T> Config for XcmConfig<T>174where175 T: pallet_configuration::Config,176{177 type Call = Call;178 type XcmSender = XcmRouter;179 180 type AssetTransactor = AssetTransactors;181 type OriginConverter = XcmOriginToTransactDispatchOrigin;182 type IsReserve = IsReserve;183 type IsTeleporter = (); 184 type LocationInverter = LocationInverter<Ancestry>;185 type Barrier = Barrier;186 type Weigher = FixedWeightBounds<UnitWeightCost, Call, MaxInstructions>;187 type Trader = Trader<T>;188 type ResponseHandler = (); 189 type SubscriptionService = PolkadotXcm;190191 type AssetTrap = PolkadotXcm;192 type AssetClaims = PolkadotXcm;193}194195impl pallet_xcm::Config for Runtime {196 type Event = Event;197 type SendXcmOrigin = EnsureXcmOrigin<Origin, LocalOriginToLocation>;198 type XcmRouter = XcmRouter;199 type ExecuteXcmOrigin = EnsureXcmOrigin<Origin, LocalOriginToLocation>;200 type XcmExecuteFilter = Everything;201 type XcmExecutor = XcmExecutor<XcmConfig<Self>>;202 type XcmTeleportFilter = Everything;203 type XcmReserveTransferFilter = Everything;204 type Weigher = FixedWeightBounds<UnitWeightCost, Call, MaxInstructions>;205 type LocationInverter = LocationInverter<Ancestry>;206 type Origin = Origin;207 type Call = Call;208 const VERSION_DISCOVERY_QUEUE_SIZE: u32 = 100;209 type AdvertisedXcmVersion = pallet_xcm::CurrentXcmVersion;210}211212impl cumulus_pallet_xcm::Config for Runtime {213 type Event = Event;214 type XcmExecutor = XcmExecutor<XcmConfig<Self>>;215}216217impl cumulus_pallet_xcmp_queue::Config for Runtime {218 type WeightInfo = ();219 type Event = Event;220 type XcmExecutor = XcmExecutor<XcmConfig<Self>>;221 type ChannelInfo = ParachainSystem;222 type VersionWrapper = ();223 type ExecuteOverweightOrigin = frame_system::EnsureRoot<AccountId>;224 type ControllerOrigin = EnsureRoot<AccountId>;225 type ControllerOriginConverter = XcmOriginToTransactDispatchOrigin;226}227228impl cumulus_pallet_dmp_queue::Config for Runtime {229 type Event = Event;230 type XcmExecutor = XcmExecutor<XcmConfig<Self>>;231 type ExecuteOverweightOrigin = frame_system::EnsureRoot<AccountId>;232}