referrerpolicy=no-referrer-when-downgrade

coretime_westend_runtime/
xcm_config.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Cumulus.
3// SPDX-License-Identifier: Apache-2.0
4
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// 	http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17use super::{
18	AccountId, AllPalletsWithSystem, Balance, Balances, BaseDeliveryFee, Broker, FeeAssetId,
19	ParachainInfo, ParachainSystem, PolkadotXcm, Runtime, RuntimeCall, RuntimeEvent,
20	RuntimeHoldReason, RuntimeOrigin, TransactionByteFee, WeightToFee, XcmpQueue,
21};
22use frame_support::{
23	pallet_prelude::PalletInfoAccess,
24	parameter_types,
25	traits::{
26		fungible::HoldConsideration, tokens::imbalance::ResolveTo, ConstU32, ConstU8, Contains,
27		Equals, Everything, LinearStoragePrice, Nothing,
28	},
29};
30use frame_system::EnsureRoot;
31use pallet_collator_selection::StakingPotAccountId;
32use pallet_xcm::{AuthorizedAliasers, XcmPassthrough};
33use parachains_common::xcm_config::{
34	AliasAccountId32FromSiblingSystemChain, AllSiblingSystemParachains, ConcreteAssetFromSystem,
35	ParentRelayOrSiblingParachains, RelayOrOtherSystemParachains,
36};
37use polkadot_parachain_primitives::primitives::Sibling;
38use polkadot_runtime_common::xcm_sender::ExponentialPrice;
39use testnet_parachains_constants::westend::locations::AssetHubLocation;
40use westend_runtime_constants::system_parachain::COLLECTIVES_ID;
41use xcm::latest::{prelude::*, WESTEND_GENESIS_HASH};
42use xcm_builder::{
43	AccountId32Aliases, AliasChildLocation, AliasOriginRootUsingFilter,
44	AllowExplicitUnpaidExecutionFrom, AllowHrmpNotificationsFromRelayChain,
45	AllowKnownQueryResponses, AllowSubscriptionsFrom, AllowTopLevelPaidExecutionFrom,
46	DenyRecursively, DenyReserveTransferToRelayChain, DenyThenTry, DescribeAllTerminal,
47	DescribeFamily, EnsureXcmOrigin, FrameTransactionalProcessor, FungibleAdapter,
48	HashedDescription, IsConcrete, IsParentsOnly, LocationAsSuperuser, NonFungibleAdapter,
49	ParentAsSuperuser, ParentIsPreset, RelayChainAsNative, SendXcmFeeToAccount,
50	SiblingParachainAsNative, SiblingParachainConvertsVia, SignedAccountId32AsNative,
51	SignedToAccountId32, SovereignSignedViaLocation, TakeWeightCredit, TrailingSetTopicAsId,
52	UsingComponents, WeightInfoBounds, WithComputedOrigin, WithUniqueTopic,
53	XcmFeeManagerFromComponents,
54};
55use xcm_executor::XcmExecutor;
56
57// Re-export
58pub use testnet_parachains_constants::westend::locations::GovernanceLocation;
59
60parameter_types! {
61	pub const RootLocation: Location = Location::here();
62	pub const TokenRelayLocation: Location = Location::parent();
63	pub const RelayNetwork: Option<NetworkId> = Some(NetworkId::ByGenesis(WESTEND_GENESIS_HASH));
64	pub RelayChainOrigin: RuntimeOrigin = cumulus_pallet_xcm::Origin::Relay.into();
65	pub UniversalLocation: InteriorLocation =
66		[GlobalConsensus(RelayNetwork::get().unwrap()), Parachain(ParachainInfo::parachain_id().into())].into();
67	pub BrokerPalletLocation: Location =
68		PalletInstance(<Broker as PalletInfoAccess>::index() as u8).into();
69	pub const MaxInstructions: u32 = 100;
70	pub const MaxAssetsIntoHolding: u32 = 64;
71	pub FellowshipLocation: Location = Location::new(1, Parachain(COLLECTIVES_ID));
72}
73
74/// Type for specifying how a `Location` can be converted into an `AccountId`. This is used
75/// when determining ownership of accounts for asset transacting and when attempting to use XCM
76/// `Transact` in order to determine the dispatch Origin.
77pub type LocationToAccountId = (
78	// The parent (Relay-chain) origin converts to the parent `AccountId`.
79	ParentIsPreset<AccountId>,
80	// Sibling parachain origins convert to AccountId via the `ParaId::into`.
81	SiblingParachainConvertsVia<Sibling, AccountId>,
82	// Straight up local `AccountId32` origins just alias directly to `AccountId`.
83	AccountId32Aliases<RelayNetwork, AccountId>,
84	// Foreign locations alias into accounts according to a hash of their standard description.
85	HashedDescription<AccountId, DescribeFamily<DescribeAllTerminal>>,
86);
87
88/// Means for transacting the native currency on this chain.
89pub type FungibleTransactor = FungibleAdapter<
90	// Use this currency:
91	Balances,
92	// Use this currency when it is a fungible asset matching the given location or name:
93	IsConcrete<TokenRelayLocation>,
94	// Do a simple punn to convert an `AccountId32` `Location` into a native chain
95	// `AccountId`:
96	LocationToAccountId,
97	// Our chain's `AccountId` type (we can't get away without mentioning it explicitly):
98	AccountId,
99	// We don't track any teleports of `Balances`.
100	(),
101>;
102
103/// Means for transacting coretime regions on this chain.
104pub type RegionTransactor = NonFungibleAdapter<
105	// Use this non-fungible implementation:
106	Broker,
107	// This adapter will handle coretime regions from the broker pallet.
108	IsConcrete<BrokerPalletLocation>,
109	// Convert an XCM Location into a local account id:
110	LocationToAccountId,
111	// Our chain's account ID type (we can't get away without mentioning it explicitly):
112	AccountId,
113	// We don't track any teleports.
114	(),
115>;
116
117/// Means for transacting assets on this chain.
118pub type AssetTransactors = (FungibleTransactor, RegionTransactor);
119
120/// This is the type we use to convert an (incoming) XCM origin into a local `Origin` instance,
121/// ready for dispatching a transaction with XCM's `Transact`. There is an `OriginKind` that can
122/// bias the kind of local `Origin` it will become.
123pub type XcmOriginToTransactDispatchOrigin = (
124	// Governance location can gain root.
125	LocationAsSuperuser<Equals<GovernanceLocation>, RuntimeOrigin>,
126	// Sovereign account converter; this attempts to derive an `AccountId` from the origin location
127	// using `LocationToAccountId` and then turn that into the usual `Signed` origin. Useful for
128	// foreign chains who want to have a local sovereign account on this chain that they control.
129	SovereignSignedViaLocation<LocationToAccountId, RuntimeOrigin>,
130	// Native converter for Relay-chain (Parent) location; will convert to a `Relay` origin when
131	// recognized.
132	RelayChainAsNative<RelayChainOrigin, RuntimeOrigin>,
133	// Native converter for sibling Parachains; will convert to a `SiblingPara` origin when
134	// recognized.
135	SiblingParachainAsNative<cumulus_pallet_xcm::Origin, RuntimeOrigin>,
136	// Superuser converter for the Relay-chain (Parent) location. This will allow it to issue a
137	// transaction from the Root origin.
138	ParentAsSuperuser<RuntimeOrigin>,
139	// Native signed account converter; this just converts an `AccountId32` origin into a normal
140	// `RuntimeOrigin::Signed` origin of the same 32-byte value.
141	SignedAccountId32AsNative<RelayNetwork, RuntimeOrigin>,
142	// XCM origins can be represented natively under the XCM pallet's `Xcm` origin.
143	XcmPassthrough<RuntimeOrigin>,
144);
145
146pub struct FellowsPlurality;
147impl Contains<Location> for FellowsPlurality {
148	fn contains(location: &Location) -> bool {
149		matches!(
150			location.unpack(),
151			(1, [Parachain(COLLECTIVES_ID), Plurality { id: BodyId::Technical, .. }])
152		)
153	}
154}
155
156pub type Barrier = TrailingSetTopicAsId<
157	DenyThenTry<
158		DenyRecursively<DenyReserveTransferToRelayChain>,
159		(
160			// Allow local users to buy weight credit.
161			TakeWeightCredit,
162			// Expected responses are OK.
163			AllowKnownQueryResponses<PolkadotXcm>,
164			WithComputedOrigin<
165				(
166					// If the message is one that immediately attempts to pay for execution, then
167					// allow it.
168					AllowTopLevelPaidExecutionFrom<Everything>,
169					// Parent, the Fellows plurality, and sibling system parachains get free
170					// execution.
171					AllowExplicitUnpaidExecutionFrom<(
172						IsParentsOnly<ConstU8<1>>,
173						RelayOrOtherSystemParachains<AllSiblingSystemParachains, Runtime>,
174						FellowsPlurality,
175						Equals<GovernanceLocation>,
176					)>,
177					// Subscriptions for version tracking are OK.
178					AllowSubscriptionsFrom<ParentRelayOrSiblingParachains>,
179					// HRMP notifications from the relay chain are OK.
180					AllowHrmpNotificationsFromRelayChain,
181				),
182				UniversalLocation,
183				ConstU32<8>,
184			>,
185		),
186	>,
187>;
188
189parameter_types! {
190	/// The accumulation account on this chain.
191	pub AccumulateAccount: AccountId = pallet_accumulate_and_forward::Pallet::<Runtime>::accumulation_account();
192	pub AccumulateForwardLocation: Location = {
193		AccountId32 { network: None, id: AccumulateAccount::get().into() }.into()
194	};
195}
196
197/// Locations that will not be charged fees in the executor, neither for execution nor delivery.
198/// We only waive fees for system functions, which these locations represent.
199pub type WaivedLocations = (
200	Equals<RootLocation>,
201	RelayOrOtherSystemParachains<AllSiblingSystemParachains, Runtime>,
202	Equals<AccumulateForwardLocation>,
203);
204
205/// Cases where a remote origin is accepted as trusted Teleporter for a given asset:
206/// - WND with the parent Relay Chain and sibling parachains.
207pub type TrustedTeleporters = ConcreteAssetFromSystem<TokenRelayLocation>;
208
209/// Defines origin aliasing rules for this chain.
210///
211/// - Allow any origin to alias into a child sub-location (equivalent to DescendOrigin),
212/// - Allow same accounts to alias into each other across system chains,
213/// - Allow AssetHub root to alias into anything,
214/// - Allow origins explicitly authorized to alias into target location.
215pub type TrustedAliasers = (
216	AliasChildLocation,
217	AliasAccountId32FromSiblingSystemChain,
218	AliasOriginRootUsingFilter<AssetHubLocation, Everything>,
219	AuthorizedAliasers<Runtime>,
220);
221
222pub struct XcmConfig;
223impl xcm_executor::Config for XcmConfig {
224	type RuntimeCall = RuntimeCall;
225	type XcmSender = XcmRouter;
226	type XcmEventEmitter = PolkadotXcm;
227	type AssetTransactor = AssetTransactors;
228	type OriginConverter = XcmOriginToTransactDispatchOrigin;
229	// Coretime chain does not recognize a reserve location for any asset. Users must teleport ROC
230	// where allowed (e.g. with the Relay Chain).
231	type IsReserve = ();
232	type IsTeleporter = TrustedTeleporters;
233	type UniversalLocation = UniversalLocation;
234	type Barrier = Barrier;
235	type Weigher = WeightInfoBounds<
236		crate::weights::xcm::CoretimeWestendXcmWeight<RuntimeCall>,
237		RuntimeCall,
238		MaxInstructions,
239	>;
240	// TODO: once DAP allocates collator budgets, redirect XCM execution fees to the accumulation
241	// account instead of StakingPot (use crate::DealWithFeesAccumulate as the OnUnbalanced
242	// handler).
243	type Trader = UsingComponents<
244		WeightToFee,
245		TokenRelayLocation,
246		AccountId,
247		Balances,
248		ResolveTo<StakingPotAccountId<Runtime>, Balances>,
249	>;
250	type ResponseHandler = PolkadotXcm;
251	type AssetTrap = PolkadotXcm;
252	type SubscriptionService = PolkadotXcm;
253	type PalletInstancesInfo = AllPalletsWithSystem;
254	type MaxAssetsIntoHolding = MaxAssetsIntoHolding;
255	type AssetLocker = ();
256	type AssetExchanger = ();
257	type FeeManager = XcmFeeManagerFromComponents<
258		WaivedLocations,
259		SendXcmFeeToAccount<Self::AssetTransactor, AccumulateAccount>,
260	>;
261	type MessageExporter = ();
262	type UniversalAliases = Nothing;
263	type CallDispatcher = RuntimeCall;
264	type SafeCallFilter = Everything;
265	type Aliasers = TrustedAliasers;
266	type TransactionalProcessor = FrameTransactionalProcessor;
267	type HrmpNewChannelOpenRequestHandler = ();
268	type HrmpChannelAcceptedHandler = ();
269	type HrmpChannelClosingHandler = ();
270	type XcmRecorder = PolkadotXcm;
271}
272
273/// Converts a local signed origin into an XCM location. Forms the basis for local origins
274/// sending/executing XCMs.
275pub type LocalOriginToLocation = SignedToAccountId32<RuntimeOrigin, AccountId, RelayNetwork>;
276
277pub type PriceForParentDelivery =
278	ExponentialPrice<FeeAssetId, BaseDeliveryFee, TransactionByteFee, ParachainSystem>;
279
280/// The means for routing XCM messages which are not for local execution into the right message
281/// queues.
282pub type XcmRouter = WithUniqueTopic<(
283	// Two routers - use UMP to communicate with the relay chain:
284	cumulus_primitives_utility::ParentAsUmp<ParachainSystem, PolkadotXcm, PriceForParentDelivery>,
285	// ..and XCMP to communicate with the sibling chains.
286	XcmpQueue,
287)>;
288
289parameter_types! {
290	pub const DepositPerItem: Balance = crate::deposit(1, 0);
291	pub const DepositPerByte: Balance = crate::deposit(0, 1);
292	pub const AuthorizeAliasHoldReason: RuntimeHoldReason = RuntimeHoldReason::PolkadotXcm(pallet_xcm::HoldReason::AuthorizeAlias);
293}
294
295impl pallet_xcm::Config for Runtime {
296	type RuntimeEvent = RuntimeEvent;
297	// We want to disallow users sending (arbitrary) XCM programs from this chain.
298	type SendXcmOrigin = EnsureXcmOrigin<RuntimeOrigin, ()>;
299	type XcmRouter = XcmRouter;
300	// We support local origins dispatching XCM executions.
301	type ExecuteXcmOrigin = EnsureXcmOrigin<RuntimeOrigin, LocalOriginToLocation>;
302	type XcmExecuteFilter = Everything;
303	type XcmExecutor = XcmExecutor<XcmConfig>;
304	type XcmTeleportFilter = Everything;
305	type XcmReserveTransferFilter = Everything;
306	type Weigher = WeightInfoBounds<
307		crate::weights::xcm::CoretimeWestendXcmWeight<RuntimeCall>,
308		RuntimeCall,
309		MaxInstructions,
310	>;
311	type UniversalLocation = UniversalLocation;
312	type RuntimeOrigin = RuntimeOrigin;
313	type RuntimeCall = RuntimeCall;
314	const VERSION_DISCOVERY_QUEUE_SIZE: u32 = 100;
315	type AdvertisedXcmVersion = pallet_xcm::CurrentXcmVersion;
316	type Currency = Balances;
317	type CurrencyMatcher = ();
318	type TrustedLockers = ();
319	type SovereignAccountOf = LocationToAccountId;
320	type MaxLockers = ConstU32<8>;
321	type WeightInfo = crate::weights::pallet_xcm::WeightInfo<Runtime>;
322	type AdminOrigin = EnsureRoot<AccountId>;
323	type MaxRemoteLockConsumers = ConstU32<0>;
324	type RemoteLockConsumerIdentifier = ();
325	// xcm_executor::Config::Aliasers also uses pallet_xcm::AuthorizedAliasers.
326	type AuthorizedAliasConsideration = HoldConsideration<
327		AccountId,
328		Balances,
329		AuthorizeAliasHoldReason,
330		LinearStoragePrice<DepositPerItem, DepositPerByte, Balance>,
331	>;
332}
333
334impl cumulus_pallet_xcm::Config for Runtime {
335	type RuntimeEvent = RuntimeEvent;
336	type XcmExecutor = XcmExecutor<XcmConfig>;
337}