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, NetworkId};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,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 const RelayNetwork: NetworkId = NetworkId::Polkadot;56 pub RelayOrigin: RuntimeOrigin = cumulus_pallet_xcm::Origin::Relay.into();57 pub Ancestry: MultiLocation = Parachain(ParachainInfo::parachain_id().into()).into();58 pub SelfLocation: MultiLocation = MultiLocation::new(1, X1(Parachain(ParachainInfo::get().into())));5960 61 pub UnitWeightCost: Weight = 1_000_000;62 pub const MaxInstructions: u32 = 100;63}6465666768pub type LocationToAccountId = (69 70 ParentIsPreset<AccountId>,71 72 SiblingParachainConvertsVia<Sibling, AccountId>,73 74 AccountId32Aliases<RelayNetwork, AccountId>,75);767778pub type LocalOriginToLocation = (SignedToAccountId32<RuntimeOrigin, AccountId, RelayNetwork>,);79808182pub type XcmRouter = (83 84 cumulus_primitives_utility::ParentAsUmp<ParachainSystem, ()>,85 86 XcmpQueue,87);8889909192pub type XcmOriginToTransactDispatchOrigin = (93 94 95 96 SovereignSignedViaLocation<LocationToAccountId, RuntimeOrigin>,97 98 99 RelayChainAsNative<RelayOrigin, RuntimeOrigin>,100 101 102 SiblingParachainAsNative<cumulus_pallet_xcm::Origin, RuntimeOrigin>,103 104 105 ParentAsSuperuser<RuntimeOrigin>,106 107 108 SignedAccountId32AsNative<RelayNetwork, RuntimeOrigin>,109 110 XcmPassthrough<RuntimeOrigin>,111);112113pub trait TryPass {114 fn try_pass<Call>(origin: &MultiLocation, message: &mut Xcm<Call>) -> Result<(), ()>;115}116117#[impl_trait_for_tuples::impl_for_tuples(30)]118impl TryPass for Tuple {119 fn try_pass<Call>(origin: &MultiLocation, message: &mut Xcm<Call>) -> Result<(), ()> {120 for_tuples!( #(121 Tuple::try_pass(origin, message)?;122 )* );123124 Ok(())125 }126}127128pub struct DenyTransact;129impl TryPass for DenyTransact {130 fn try_pass<Call>(_origin: &MultiLocation, message: &mut Xcm<Call>) -> Result<(), ()> {131 let transact_inst = message132 .0133 .iter()134 .find(|inst| matches![inst, Instruction::Transact { .. }]);135136 if transact_inst.is_some() {137 log::warn!(138 target: "xcm::barrier",139 "transact XCM rejected"140 );141142 Err(())143 } else {144 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}171172173pub struct DenyExchangeWithUnknownLocation<T>(PhantomData<T>);174impl<T: Get<Vec<MultiLocation>>> TryPass for DenyExchangeWithUnknownLocation<T> {175 fn try_pass<Call>(origin: &MultiLocation, message: &mut Xcm<Call>) -> Result<(), ()> {176 let allowed_locations = T::get();177178 179 let mut allowed = allowed_locations.contains(origin);180181 message.0.iter().for_each(|inst| match inst {182 DepositReserveAsset { dest: dst, .. } => {183 allowed |= allowed_locations.contains(dst);184 }185 TransferReserveAsset { dest: dst, .. } => {186 allowed |= allowed_locations.contains(dst);187 }188 InitiateReserveWithdraw { reserve: dst, .. } => {189 allowed |= allowed_locations.contains(dst);190 }191 _ => {}192 });193194 if allowed {195 return Ok(());196 }197198 log::warn!(199 target: "xcm::barrier",200 "Unexpected deposit or transfer location"201 );202 203 Err(())204 }205}206207pub type Weigher = FixedWeightBounds<UnitWeightCost, RuntimeCall, MaxInstructions>;208209pub struct XcmConfig<T>(PhantomData<T>);210impl<T> Config for XcmConfig<T>211where212 T: pallet_configuration::Config,213{214 type RuntimeCall = RuntimeCall;215 type XcmSender = XcmRouter;216 217 type AssetTransactor = AssetTransactors;218 type OriginConverter = XcmOriginToTransactDispatchOrigin;219 type IsReserve = IsReserve;220 type IsTeleporter = (); 221 type LocationInverter = LocationInverter<Ancestry>;222 type Barrier = Barrier;223 type Weigher = Weigher;224 type Trader = Trader<T>;225 type ResponseHandler = (); 226 type SubscriptionService = PolkadotXcm;227228 type AssetTrap = PolkadotXcm;229 type AssetClaims = PolkadotXcm;230}231232impl pallet_xcm::Config for Runtime {233 type RuntimeEvent = RuntimeEvent;234 type SendXcmOrigin = EnsureXcmOrigin<RuntimeOrigin, LocalOriginToLocation>;235 type XcmRouter = XcmRouter;236 type ExecuteXcmOrigin = EnsureXcmOrigin<RuntimeOrigin, LocalOriginToLocation>;237 type XcmExecuteFilter = Everything;238 type XcmExecutor = XcmExecutor<XcmConfig<Self>>;239 type XcmTeleportFilter = Everything;240 type XcmReserveTransferFilter = Everything;241 type Weigher = FixedWeightBounds<UnitWeightCost, RuntimeCall, MaxInstructions>;242 type LocationInverter = LocationInverter<Ancestry>;243 type RuntimeOrigin = RuntimeOrigin;244 type RuntimeCall = RuntimeCall;245 const VERSION_DISCOVERY_QUEUE_SIZE: u32 = 100;246 type AdvertisedXcmVersion = pallet_xcm::CurrentXcmVersion;247}248249impl cumulus_pallet_xcm::Config for Runtime {250 type RuntimeEvent = RuntimeEvent;251 type XcmExecutor = XcmExecutor<XcmConfig<Self>>;252}253254impl cumulus_pallet_xcmp_queue::Config for Runtime {255 type WeightInfo = ();256 type RuntimeEvent = RuntimeEvent;257 type XcmExecutor = XcmExecutor<XcmConfig<Self>>;258 type ChannelInfo = ParachainSystem;259 type VersionWrapper = ();260 type ExecuteOverweightOrigin = frame_system::EnsureRoot<AccountId>;261 type ControllerOrigin = EnsureRoot<AccountId>;262 type ControllerOriginConverter = XcmOriginToTransactDispatchOrigin;263}264265impl cumulus_pallet_dmp_queue::Config for Runtime {266 type RuntimeEvent = RuntimeEvent;267 type XcmExecutor = XcmExecutor<XcmConfig<Self>>;268 type ExecuteOverweightOrigin = frame_system::EnsureRoot<AccountId>;269}