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::v1::{Junction::*, MultiLocation, NetworkId};25use xcm::latest::{prelude::*, Weight};26use xcm_builder::{27 AccountId32Aliases, EnsureXcmOrigin, FixedWeightBounds, LocationInverter, ParentAsSuperuser,28 RelayChainAsNative, SiblingParachainAsNative, SiblingParachainConvertsVia,29 SignedAccountId32AsNative, SignedToAccountId32, SovereignSignedViaLocation, ParentIsPreset,30};31use xcm_executor::{Config, XcmExecutor, traits::ShouldExecute};32use sp_std::{marker::PhantomData, vec::Vec};33use crate::{34 Runtime, Call, Event, Origin, ParachainInfo, ParachainSystem, PolkadotXcm, XcmpQueue,35 xcm_barrier::Barrier,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;5152use xcm_assets::{AssetTransactors, IsReserve, Trader};5354parameter_types! {55 pub const RelayLocation: MultiLocation = MultiLocation::parent();56 pub const RelayNetwork: NetworkId = NetworkId::Polkadot;57 pub RelayOrigin: Origin = cumulus_pallet_xcm::Origin::Relay.into();58 pub Ancestry: MultiLocation = Parachain(ParachainInfo::parachain_id().into()).into();59 pub SelfLocation: MultiLocation = MultiLocation::new(1, X1(Parachain(ParachainInfo::get().into())));6061 62 pub UnitWeightCost: Weight = 1_000_000;63 pub const MaxInstructions: u32 = 100;64}6566676869pub type LocationToAccountId = (70 71 ParentIsPreset<AccountId>,72 73 SiblingParachainConvertsVia<Sibling, AccountId>,74 75 AccountId32Aliases<RelayNetwork, AccountId>,76);777879pub type LocalOriginToLocation = (SignedToAccountId32<Origin, AccountId, RelayNetwork>,);80818283pub type XcmRouter = (84 85 cumulus_primitives_utility::ParentAsUmp<ParachainSystem, ()>,86 87 XcmpQueue,88);8990919293pub type XcmOriginToTransactDispatchOrigin = (94 95 96 97 SovereignSignedViaLocation<LocationToAccountId, Origin>,98 99 100 RelayChainAsNative<RelayOrigin, Origin>,101 102 103 SiblingParachainAsNative<cumulus_pallet_xcm::Origin, Origin>,104 105 106 ParentAsSuperuser<Origin>,107 108 109 SignedAccountId32AsNative<RelayNetwork, Origin>,110 111 XcmPassthrough<Origin>,112);113114pub trait TryPass {115 fn try_pass<Call>(origin: &MultiLocation, message: &mut Xcm<Call>) -> Result<(), ()>;116}117118#[impl_trait_for_tuples::impl_for_tuples(30)]119impl TryPass for Tuple {120 fn try_pass<Call>(origin: &MultiLocation, message: &mut Xcm<Call>) -> Result<(), ()> {121 for_tuples!( #(122 Tuple::try_pass(origin, message)?;123 )* );124125 Ok(())126 }127}128129pub struct DenyTransact;130impl TryPass for DenyTransact {131 fn try_pass<Call>(_origin: &MultiLocation, message: &mut Xcm<Call>) -> Result<(), ()> {132 let transact_inst = message133 .0134 .iter()135 .find(|inst| matches![inst, Instruction::Transact { .. }]);136137 if transact_inst.is_some() {138 log::warn!(139 target: "xcm::barrier",140 "transact XCM rejected"141 );142143 Err(())144 } else {145 Ok(())146 }147 }148}149150151152pub struct DenyThenTry<Deny, Allow>(PhantomData<Deny>, PhantomData<Allow>)153where154 Deny: TryPass,155 Allow: ShouldExecute;156157impl<Deny, Allow> ShouldExecute for DenyThenTry<Deny, Allow>158where159 Deny: TryPass,160 Allow: ShouldExecute,161{162 fn should_execute<Call>(163 origin: &MultiLocation,164 message: &mut Xcm<Call>,165 max_weight: Weight,166 weight_credit: &mut Weight,167 ) -> Result<(), ()> {168 Deny::try_pass(origin, message)?;169 Allow::should_execute(origin, message, max_weight, weight_credit)170 }171}172173174pub struct DenyExchangeWithUnknownLocation<T>(PhantomData<T>);175impl<T: Get<Vec<MultiLocation>>> TryPass for DenyExchangeWithUnknownLocation<T> {176 fn try_pass<Call>(origin: &MultiLocation, message: &mut Xcm<Call>) -> Result<(), ()> {177 let allowed_locations = T::get();178179 180 let mut allowed = allowed_locations.contains(origin);181182 message.0.iter().for_each(|inst| match inst {183 DepositReserveAsset { dest: dst, .. } => {184 allowed |= allowed_locations.contains(dst);185 }186 TransferReserveAsset { dest: dst, .. } => {187 allowed |= allowed_locations.contains(dst);188 }189 _ => {}190 });191192 if allowed {193 return Ok(());194 }195196 log::warn!(197 target: "xcm::barrier",198 "Unexpected deposit or transfer location"199 );200 201 Err(())202 }203}204205pub type Weigher = FixedWeightBounds<UnitWeightCost, Call, MaxInstructions>;206207pub struct XcmConfig<T>(PhantomData<T>);208impl<T> Config for XcmConfig<T>209where210 T: pallet_configuration::Config,211{212 type Call = Call;213 type XcmSender = XcmRouter;214 215 type AssetTransactor = AssetTransactors;216 type OriginConverter = XcmOriginToTransactDispatchOrigin;217 type IsReserve = IsReserve;218 type IsTeleporter = (); 219 type LocationInverter = LocationInverter<Ancestry>;220 type Barrier = Barrier;221 type Weigher = Weigher;222 type Trader = Trader<T>;223 type ResponseHandler = (); 224 type SubscriptionService = PolkadotXcm;225226 type AssetTrap = PolkadotXcm;227 type AssetClaims = PolkadotXcm;228}229230impl pallet_xcm::Config for Runtime {231 type Event = Event;232 type SendXcmOrigin = EnsureXcmOrigin<Origin, LocalOriginToLocation>;233 type XcmRouter = XcmRouter;234 type ExecuteXcmOrigin = EnsureXcmOrigin<Origin, LocalOriginToLocation>;235 type XcmExecuteFilter = Everything;236 type XcmExecutor = XcmExecutor<XcmConfig<Self>>;237 type XcmTeleportFilter = Everything;238 type XcmReserveTransferFilter = Everything;239 type Weigher = FixedWeightBounds<UnitWeightCost, Call, MaxInstructions>;240 type LocationInverter = LocationInverter<Ancestry>;241 type Origin = Origin;242 type Call = Call;243 const VERSION_DISCOVERY_QUEUE_SIZE: u32 = 100;244 type AdvertisedXcmVersion = pallet_xcm::CurrentXcmVersion;245}246247impl cumulus_pallet_xcm::Config for Runtime {248 type Event = Event;249 type XcmExecutor = XcmExecutor<XcmConfig<Self>>;250}251252impl cumulus_pallet_xcmp_queue::Config for Runtime {253 type WeightInfo = ();254 type Event = Event;255 type XcmExecutor = XcmExecutor<XcmConfig<Self>>;256 type ChannelInfo = ParachainSystem;257 type VersionWrapper = ();258 type ExecuteOverweightOrigin = frame_system::EnsureRoot<AccountId>;259 type ControllerOrigin = EnsureRoot<AccountId>;260 type ControllerOriginConverter = XcmOriginToTransactDispatchOrigin;261}262263impl cumulus_pallet_dmp_queue::Config for Runtime {264 type Event = Event;265 type XcmExecutor = XcmExecutor<XcmConfig<Self>>;266 type ExecuteOverweightOrigin = frame_system::EnsureRoot<AccountId>;267}