1234567891011121314151617use frame_support::{18 traits::{Everything, Get},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_builder::{26 AccountId32Aliases, EnsureXcmOrigin, FixedWeightBounds, LocationInverter, ParentAsSuperuser,27 RelayChainAsNative, SiblingParachainAsNative, SiblingParachainConvertsVia,28 SignedAccountId32AsNative, SignedToAccountId32, SovereignSignedViaLocation, ParentIsPreset,29};30use xcm_executor::{Config, XcmExecutor, traits::ShouldExecute};31use sp_std::{marker::PhantomData, vec::Vec};32use crate::{33 Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin, ParachainInfo, ParachainSystem, PolkadotXcm,34 XcmpQueue, xcm_barrier::Barrier, RelayNetwork,35};3637use up_common::types::AccountId;3839#[cfg(feature = "foreign-assets")]40pub mod foreignassets;4142#[cfg(not(feature = "foreign-assets"))]43pub mod nativeassets;4445#[cfg(feature = "foreign-assets")]46pub use foreignassets as xcm_assets;4748#[cfg(not(feature = "foreign-assets"))]49pub use nativeassets as xcm_assets;5051use xcm_assets::{AssetTransactors, IsReserve, Trader};5253parameter_types! {54 pub const RelayLocation: MultiLocation = MultiLocation::parent();55 pub RelayOrigin: RuntimeOrigin = cumulus_pallet_xcm::Origin::Relay.into();56 pub Ancestry: MultiLocation = Parachain(ParachainInfo::parachain_id().into()).into();57 pub SelfLocation: MultiLocation = MultiLocation::new(1, X1(Parachain(ParachainInfo::get().into())));5859 60 pub UnitWeightCost: Weight = 1_000_000;61 pub const MaxInstructions: u32 = 100;62}6364656667pub type LocationToAccountId = (68 69 ParentIsPreset<AccountId>,70 71 SiblingParachainConvertsVia<Sibling, AccountId>,72 73 AccountId32Aliases<RelayNetwork, AccountId>,74);757677pub type LocalOriginToLocation = (SignedToAccountId32<RuntimeOrigin, AccountId, RelayNetwork>,);78798081pub type XcmRouter = (82 83 cumulus_primitives_utility::ParentAsUmp<ParachainSystem, ()>,84 85 XcmpQueue,86);8788899091pub type XcmOriginToTransactDispatchOrigin = (92 93 94 95 SovereignSignedViaLocation<LocationToAccountId, RuntimeOrigin>,96 97 98 RelayChainAsNative<RelayOrigin, RuntimeOrigin>,99 100 101 SiblingParachainAsNative<cumulus_pallet_xcm::Origin, RuntimeOrigin>,102 103 104 ParentAsSuperuser<RuntimeOrigin>,105 106 107 SignedAccountId32AsNative<RelayNetwork, RuntimeOrigin>,108 109 XcmPassthrough<RuntimeOrigin>,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 if transact_inst.is_some() {136 log::warn!(137 target: "xcm::barrier",138 "transact XCM rejected"139 );140141 Err(())142 } else {143 Ok(())144 }145 }146}147148149150pub struct DenyThenTry<Deny, Allow>(PhantomData<Deny>, PhantomData<Allow>)151where152 Deny: TryPass,153 Allow: ShouldExecute;154155impl<Deny, Allow> ShouldExecute for DenyThenTry<Deny, Allow>156where157 Deny: TryPass,158 Allow: ShouldExecute,159{160 fn should_execute<Call>(161 origin: &MultiLocation,162 message: &mut Xcm<Call>,163 max_weight: Weight,164 weight_credit: &mut Weight,165 ) -> Result<(), ()> {166 Deny::try_pass(origin, message)?;167 Allow::should_execute(origin, message, max_weight, weight_credit)168 }169}170171172pub struct DenyExchangeWithUnknownLocation<T>(PhantomData<T>);173impl<T: Get<Vec<MultiLocation>>> TryPass for DenyExchangeWithUnknownLocation<T> {174 fn try_pass<Call>(origin: &MultiLocation, message: &mut Xcm<Call>) -> Result<(), ()> {175 let allowed_locations = T::get();176177 178 let mut allowed = allowed_locations.contains(origin);179180 message.0.iter().for_each(|inst| match inst {181 DepositReserveAsset { dest: dst, .. } => {182 allowed |= allowed_locations.contains(dst);183 }184 TransferReserveAsset { dest: dst, .. } => {185 allowed |= allowed_locations.contains(dst);186 }187 InitiateReserveWithdraw { reserve: dst, .. } => {188 allowed |= allowed_locations.contains(dst);189 }190 _ => {}191 });192193 if allowed {194 return Ok(());195 }196197 log::warn!(198 target: "xcm::barrier",199 "Unexpected deposit or transfer location"200 );201 202 Err(())203 }204}205206pub type Weigher = FixedWeightBounds<UnitWeightCost, RuntimeCall, MaxInstructions>;207208pub struct XcmConfig<T>(PhantomData<T>);209impl<T> Config for XcmConfig<T>210where211 T: pallet_configuration::Config,212{213 type RuntimeCall = RuntimeCall;214 type XcmSender = XcmRouter;215 216 type AssetTransactor = AssetTransactors;217 type OriginConverter = XcmOriginToTransactDispatchOrigin;218 type IsReserve = IsReserve;219 type IsTeleporter = (); 220 type LocationInverter = LocationInverter<Ancestry>;221 type Barrier = Barrier;222 type Weigher = Weigher;223 type Trader = Trader<T>;224 type ResponseHandler = (); 225 type SubscriptionService = PolkadotXcm;226227 type AssetTrap = PolkadotXcm;228 type AssetClaims = PolkadotXcm;229}230231impl pallet_xcm::Config for Runtime {232 type RuntimeEvent = RuntimeEvent;233 type SendXcmOrigin = EnsureXcmOrigin<RuntimeOrigin, LocalOriginToLocation>;234 type XcmRouter = XcmRouter;235 type ExecuteXcmOrigin = EnsureXcmOrigin<RuntimeOrigin, LocalOriginToLocation>;236 type XcmExecuteFilter = Everything;237 type XcmExecutor = XcmExecutor<XcmConfig<Self>>;238 type XcmTeleportFilter = Everything;239 type XcmReserveTransferFilter = Everything;240 type Weigher = FixedWeightBounds<UnitWeightCost, RuntimeCall, MaxInstructions>;241 type LocationInverter = LocationInverter<Ancestry>;242 type RuntimeOrigin = RuntimeOrigin;243 type RuntimeCall = RuntimeCall;244 const VERSION_DISCOVERY_QUEUE_SIZE: u32 = 100;245 type AdvertisedXcmVersion = pallet_xcm::CurrentXcmVersion;246}247248impl cumulus_pallet_xcm::Config for Runtime {249 type RuntimeEvent = RuntimeEvent;250 type XcmExecutor = XcmExecutor<XcmConfig<Self>>;251}252253impl cumulus_pallet_xcmp_queue::Config for Runtime {254 type WeightInfo = ();255 type RuntimeEvent = RuntimeEvent;256 type XcmExecutor = XcmExecutor<XcmConfig<Self>>;257 type ChannelInfo = ParachainSystem;258 type VersionWrapper = ();259 type ExecuteOverweightOrigin = frame_system::EnsureRoot<AccountId>;260 type ControllerOrigin = EnsureRoot<AccountId>;261 type ControllerOriginConverter = XcmOriginToTransactDispatchOrigin;262}263264impl cumulus_pallet_dmp_queue::Config for Runtime {265 type RuntimeEvent = RuntimeEvent;266 type XcmExecutor = XcmExecutor<XcmConfig<Self>>;267 type ExecuteOverweightOrigin = frame_system::EnsureRoot<AccountId>;268}