1234567891011121314151617use frame_support::{18 traits::{Everything, Get},19 weights::Weight,20 parameter_types,21};22use frame_system::EnsureRoot;23use pallet_xcm::XcmPassthrough;24use polkadot_parachain::primitives::Sibling;25use xcm::v1::{Junction::*, MultiLocation, NetworkId};26use xcm::latest::prelude::*;27use xcm_builder::{28 AccountId32Aliases, EnsureXcmOrigin, FixedWeightBounds, LocationInverter, ParentAsSuperuser,29 RelayChainAsNative, SiblingParachainAsNative, SiblingParachainConvertsVia,30 SignedAccountId32AsNative, SignedToAccountId32, SovereignSignedViaLocation, ParentIsPreset,31};32use xcm_executor::{Config, XcmExecutor, traits::ShouldExecute};33use sp_std::{marker::PhantomData, vec::Vec};34use crate::{35 Runtime, Call, Event, Origin, ParachainInfo, ParachainSystem, PolkadotXcm, XcmpQueue,36 xcm_barrier::Barrier,37};3839use up_common::types::AccountId;4041#[cfg(feature = "foreign-assets")]42pub mod foreignassets;4344#[cfg(not(feature = "foreign-assets"))]45pub mod nativeassets;4647#[cfg(feature = "foreign-assets")]48pub use foreignassets as xcm_assets;4950#[cfg(not(feature = "foreign-assets"))]51pub use nativeassets as xcm_assets;5253use xcm_assets::{AssetTransactors, IsReserve, Trader};5455parameter_types! {56 pub const RelayLocation: MultiLocation = MultiLocation::parent();57 pub const RelayNetwork: NetworkId = NetworkId::Polkadot;58 pub RelayOrigin: Origin = cumulus_pallet_xcm::Origin::Relay.into();59 pub Ancestry: MultiLocation = Parachain(ParachainInfo::parachain_id().into()).into();60 pub SelfLocation: MultiLocation = MultiLocation::new(1, X1(Parachain(ParachainInfo::get().into())));6162 63 pub UnitWeightCost: Weight = 1_000_000;64 pub const MaxInstructions: u32 = 100;65}6667686970pub type LocationToAccountId = (71 72 ParentIsPreset<AccountId>,73 74 SiblingParachainConvertsVia<Sibling, AccountId>,75 76 AccountId32Aliases<RelayNetwork, AccountId>,77);787980pub type LocalOriginToLocation = (SignedToAccountId32<Origin, AccountId, RelayNetwork>,);81828384pub type XcmRouter = (85 86 cumulus_primitives_utility::ParentAsUmp<ParachainSystem, ()>,87 88 XcmpQueue,89);9091929394pub type XcmOriginToTransactDispatchOrigin = (95 96 97 98 SovereignSignedViaLocation<LocationToAccountId, Origin>,99 100 101 RelayChainAsNative<RelayOrigin, Origin>,102 103 104 SiblingParachainAsNative<cumulus_pallet_xcm::Origin, Origin>,105 106 107 ParentAsSuperuser<Origin>,108 109 110 SignedAccountId32AsNative<RelayNetwork, Origin>,111 112 XcmPassthrough<Origin>,113);114115pub trait TryPass {116 fn try_pass<Call>(origin: &MultiLocation, message: &mut Xcm<Call>) -> Result<(), ()>;117}118119#[impl_trait_for_tuples::impl_for_tuples(30)]120impl TryPass for Tuple {121 fn try_pass<Call>(origin: &MultiLocation, message: &mut Xcm<Call>) -> Result<(), ()> {122 for_tuples!( #(123 Tuple::try_pass(origin, message)?;124 )* );125126 Ok(())127 }128}129130pub struct DenyTransact;131impl TryPass for DenyTransact {132 fn try_pass<Call>(_origin: &MultiLocation, message: &mut Xcm<Call>) -> Result<(), ()> {133 let transact_inst = message134 .0135 .iter()136 .find(|inst| matches![inst, Instruction::Transact { .. }]);137138 if transact_inst.is_some() {139 log::warn!(140 target: "xcm::barrier",141 "transact XCM rejected"142 );143144 Err(())145 } else {146 Ok(())147 }148 }149}150151152153pub struct DenyThenTry<Deny, Allow>(PhantomData<Deny>, PhantomData<Allow>)154where155 Deny: TryPass,156 Allow: ShouldExecute;157158impl<Deny, Allow> ShouldExecute for DenyThenTry<Deny, Allow>159where160 Deny: TryPass,161 Allow: ShouldExecute,162{163 fn should_execute<Call>(164 origin: &MultiLocation,165 message: &mut Xcm<Call>,166 max_weight: Weight,167 weight_credit: &mut Weight,168 ) -> Result<(), ()> {169 Deny::try_pass(origin, message)?;170 Allow::should_execute(origin, message, max_weight, weight_credit)171 }172}173174175pub struct DenyExchangeWithUnknownLocation<T>(PhantomData<T>);176impl<T: Get<Vec<MultiLocation>>> TryPass for DenyExchangeWithUnknownLocation<T> {177 fn try_pass<Call>(origin: &MultiLocation, message: &mut Xcm<Call>) -> Result<(), ()> {178 let allowed_locations = T::get();179180 181 let mut allowed = allowed_locations.contains(origin);182183 message.0.iter().for_each(|inst| match inst {184 DepositReserveAsset { dest: dst, .. } => {185 allowed |= allowed_locations.contains(dst);186 }187 TransferReserveAsset { dest: 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, Call, MaxInstructions>;207208pub struct XcmConfig<T>(PhantomData<T>);209impl<T> Config for XcmConfig<T>210where211 T: pallet_configuration::Config,212{213 type Call = Call;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 Event = Event;233 type SendXcmOrigin = EnsureXcmOrigin<Origin, LocalOriginToLocation>;234 type XcmRouter = XcmRouter;235 type ExecuteXcmOrigin = EnsureXcmOrigin<Origin, LocalOriginToLocation>;236 type XcmExecuteFilter = Everything;237 type XcmExecutor = XcmExecutor<XcmConfig<Self>>;238 type XcmTeleportFilter = Everything;239 type XcmReserveTransferFilter = Everything;240 type Weigher = FixedWeightBounds<UnitWeightCost, Call, MaxInstructions>;241 type LocationInverter = LocationInverter<Ancestry>;242 type Origin = Origin;243 type Call = Call;244 const VERSION_DISCOVERY_QUEUE_SIZE: u32 = 100;245 type AdvertisedXcmVersion = pallet_xcm::CurrentXcmVersion;246}247248impl cumulus_pallet_xcm::Config for Runtime {249 type Event = Event;250 type XcmExecutor = XcmExecutor<XcmConfig<Self>>;251}252253impl cumulus_pallet_xcmp_queue::Config for Runtime {254 type WeightInfo = ();255 type Event = Event;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 Event = Event;266 type XcmExecutor = XcmExecutor<XcmConfig<Self>>;267 type ExecuteOverweightOrigin = frame_system::EnsureRoot<AccountId>;268}