contracts_rococo_runtime/
xcm_config.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// SPDX-License-Identifier: Apache-2.0
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// 	http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16use super::{
17	AccountId, AllPalletsWithSystem, Balances, ParachainInfo, ParachainSystem, PolkadotXcm,
18	Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin, TransactionByteFee, WeightToFee, XcmpQueue,
19};
20use cumulus_primitives_core::AggregateMessageOrigin;
21use frame_support::{
22	parameter_types,
23	traits::{ConstU32, Contains, EitherOfDiverse, Equals, Everything, Nothing},
24	weights::Weight,
25};
26use frame_system::EnsureRoot;
27use pallet_xcm::{EnsureXcm, IsMajorityOfBody, XcmPassthrough};
28use parachains_common::{
29	xcm_config::{
30		AllSiblingSystemParachains, ConcreteAssetFromSystem, ParentRelayOrSiblingParachains,
31		RelayOrOtherSystemParachains,
32	},
33	TREASURY_PALLET_ID,
34};
35use polkadot_parachain_primitives::primitives::Sibling;
36use polkadot_runtime_common::xcm_sender::ExponentialPrice;
37use sp_runtime::traits::AccountIdConversion;
38use testnet_parachains_constants::rococo::currency::CENTS;
39use xcm::latest::{prelude::*, ROCOCO_GENESIS_HASH};
40use xcm_builder::{
41	AccountId32Aliases, AllowExplicitUnpaidExecutionFrom, AllowHrmpNotificationsFromRelayChain,
42	AllowKnownQueryResponses, AllowSubscriptionsFrom, AllowTopLevelPaidExecutionFrom,
43	DenyReserveTransferToRelayChain, DenyThenTry, DescribeAllTerminal, DescribeFamily,
44	EnsureXcmOrigin, FixedWeightBounds, FrameTransactionalProcessor, FungibleAdapter,
45	HashedDescription, IsConcrete, NativeAsset, ParentAsSuperuser, ParentIsPreset,
46	RelayChainAsNative, SendXcmFeeToAccount, SiblingParachainAsNative, SiblingParachainConvertsVia,
47	SignedAccountId32AsNative, SignedToAccountId32, SovereignSignedViaLocation, TakeWeightCredit,
48	TrailingSetTopicAsId, UsingComponents, WithComputedOrigin, WithUniqueTopic,
49	XcmFeeManagerFromComponents,
50};
51use xcm_executor::XcmExecutor;
52
53parameter_types! {
54	pub const RelayLocation: Location = Location::parent();
55	pub const RelayNetwork: NetworkId = NetworkId::ByGenesis(ROCOCO_GENESIS_HASH);
56	pub RelayChainOrigin: RuntimeOrigin = cumulus_pallet_xcm::Origin::Relay.into();
57	pub UniversalLocation: InteriorLocation = [GlobalConsensus(RelayNetwork::get()), Parachain(ParachainInfo::parachain_id().into())].into();
58	pub const ExecutiveBody: BodyId = BodyId::Executive;
59	pub TreasuryAccount: AccountId = TREASURY_PALLET_ID.into_account_truncating();
60	pub RelayTreasuryLocation: Location = (Parent, PalletInstance(rococo_runtime_constants::TREASURY_PALLET_ID)).into();
61}
62
63/// We allow root and the Relay Chain council to execute privileged collator selection operations.
64pub type CollatorSelectionUpdateOrigin = EitherOfDiverse<
65	EnsureRoot<AccountId>,
66	EnsureXcm<IsMajorityOfBody<RelayLocation, ExecutiveBody>>,
67>;
68
69/// Type for specifying how a `Location` can be converted into an `AccountId`. This is used
70/// when determining ownership of accounts for asset transacting and when attempting to use XCM
71/// `Transact` in order to determine the dispatch Origin.
72pub type LocationToAccountId = (
73	// The parent (Relay-chain) origin converts to the parent `AccountId`.
74	ParentIsPreset<AccountId>,
75	// Sibling parachain origins convert to AccountId via the `ParaId::into`.
76	SiblingParachainConvertsVia<Sibling, AccountId>,
77	// Straight up local `AccountId32` origins just alias directly to `AccountId`.
78	AccountId32Aliases<RelayNetwork, AccountId>,
79	// Foreign locations alias into accounts according to a hash of their standard description.
80	HashedDescription<AccountId, DescribeFamily<DescribeAllTerminal>>,
81);
82
83/// Means for transacting the native currency on this chain.
84pub type FungibleTransactor = FungibleAdapter<
85	// Use this currency:
86	Balances,
87	// Use this currency when it is a fungible asset matching the given location or name:
88	IsConcrete<RelayLocation>,
89	// Convert an XCM Location into a local account id:
90	LocationToAccountId,
91	// Our chain's account ID type (we can't get away without mentioning it explicitly):
92	AccountId,
93	// We don't track any teleports of `Balances`.
94	(),
95>;
96
97/// This is the type we use to convert an (incoming) XCM origin into a local `Origin` instance,
98/// ready for dispatching a transaction with Xcm's `Transact`. There is an `OriginKind` which can
99/// biases the kind of local `Origin` it will become.
100pub type XcmOriginToTransactDispatchOrigin = (
101	// Sovereign account converter; this attempts to derive an `AccountId` from the origin location
102	// using `LocationToAccountId` and then turn that into the usual `Signed` origin. Useful for
103	// foreign chains who want to have a local sovereign account on this chain which they control.
104	SovereignSignedViaLocation<LocationToAccountId, RuntimeOrigin>,
105	// Native converter for Relay-chain (Parent) location; will convert to a `Relay` origin when
106	// recognised.
107	RelayChainAsNative<RelayChainOrigin, RuntimeOrigin>,
108	// Native converter for sibling Parachains; will convert to a `SiblingPara` origin when
109	// recognised.
110	SiblingParachainAsNative<cumulus_pallet_xcm::Origin, RuntimeOrigin>,
111	// Superuser converter for the Relay-chain (Parent) location. This will allow it to issue a
112	// transaction from the Root origin.
113	ParentAsSuperuser<RuntimeOrigin>,
114	// Native signed account converter; this just converts an `AccountId32` origin into a normal
115	// `RuntimeOrigin::Signed` origin of the same 32-byte value.
116	SignedAccountId32AsNative<RelayNetwork, RuntimeOrigin>,
117	// Xcm origins can be represented natively under the Xcm pallet's Xcm origin.
118	XcmPassthrough<RuntimeOrigin>,
119);
120
121parameter_types! {
122	// One XCM operation is 1_000_000_000 weight - almost certainly a conservative estimate.
123	pub UnitWeightCost: Weight = Weight::from_parts(1_000_000_000, 64 * 1024);
124	pub const MaxInstructions: u32 = 100;
125}
126
127pub struct ParentOrParentsPlurality;
128impl Contains<Location> for ParentOrParentsPlurality {
129	fn contains(location: &Location) -> bool {
130		matches!(location.unpack(), (1, []) | (1, [Plurality { .. }]))
131	}
132}
133
134pub type Barrier = TrailingSetTopicAsId<
135	DenyThenTry<
136		DenyReserveTransferToRelayChain,
137		(
138			TakeWeightCredit,
139			// Expected responses are OK.
140			AllowKnownQueryResponses<PolkadotXcm>,
141			// Allow XCMs with some computed origins to pass through.
142			WithComputedOrigin<
143				(
144					// If the message is one that immediately attempts to pay for execution, then
145					// allow it.
146					AllowTopLevelPaidExecutionFrom<Everything>,
147					// Parent, its pluralities (i.e. governance bodies) and relay treasury pallet
148					// get free execution.
149					AllowExplicitUnpaidExecutionFrom<(
150						ParentOrParentsPlurality,
151						Equals<RelayTreasuryLocation>,
152					)>,
153					// Subscriptions for version tracking are OK.
154					AllowSubscriptionsFrom<ParentRelayOrSiblingParachains>,
155					// HRMP notifications from the relay chain are OK.
156					AllowHrmpNotificationsFromRelayChain,
157				),
158				UniversalLocation,
159				ConstU32<8>,
160			>,
161		),
162	>,
163>;
164
165/// Locations that will not be charged fees in the executor,
166/// either execution or delivery.
167/// We only waive fees for system functions, which these locations represent.
168pub type WaivedLocations = (
169	RelayOrOtherSystemParachains<AllSiblingSystemParachains, Runtime>,
170	Equals<RelayTreasuryLocation>,
171);
172
173pub type TrustedTeleporter = ConcreteAssetFromSystem<RelayLocation>;
174
175pub struct XcmConfig;
176impl xcm_executor::Config for XcmConfig {
177	type RuntimeCall = RuntimeCall;
178	type XcmSender = XcmRouter;
179	type AssetTransactor = FungibleTransactor;
180	type OriginConverter = XcmOriginToTransactDispatchOrigin;
181	type IsReserve = NativeAsset;
182	type IsTeleporter = TrustedTeleporter;
183	type UniversalLocation = UniversalLocation;
184	type Barrier = Barrier;
185	type Weigher = FixedWeightBounds<UnitWeightCost, RuntimeCall, MaxInstructions>;
186	type Trader = UsingComponents<WeightToFee, RelayLocation, AccountId, Balances, ()>;
187	type ResponseHandler = PolkadotXcm;
188	type AssetTrap = PolkadotXcm;
189	type AssetClaims = PolkadotXcm;
190	type SubscriptionService = PolkadotXcm;
191	type PalletInstancesInfo = AllPalletsWithSystem;
192	type MaxAssetsIntoHolding = ConstU32<8>;
193	type AssetLocker = ();
194	type AssetExchanger = ();
195	type FeeManager = XcmFeeManagerFromComponents<
196		WaivedLocations,
197		SendXcmFeeToAccount<Self::AssetTransactor, TreasuryAccount>,
198	>;
199	type MessageExporter = ();
200	type UniversalAliases = Nothing;
201	type CallDispatcher = RuntimeCall;
202	type SafeCallFilter = Everything;
203	type Aliasers = Nothing;
204	type TransactionalProcessor = FrameTransactionalProcessor;
205	type HrmpNewChannelOpenRequestHandler = ();
206	type HrmpChannelAcceptedHandler = ();
207	type HrmpChannelClosingHandler = ();
208	type XcmRecorder = PolkadotXcm;
209}
210
211/// Converts a local signed origin into an XCM location.
212/// Forms the basis for local origins sending/executing XCMs.
213pub type LocalOriginToLocation = SignedToAccountId32<RuntimeOrigin, AccountId, RelayNetwork>;
214
215pub type PriceForParentDelivery =
216	ExponentialPrice<FeeAssetId, BaseDeliveryFee, TransactionByteFee, ParachainSystem>;
217
218/// The means for routing XCM messages which are not for local execution into the right message
219/// queues.
220pub type XcmRouter = WithUniqueTopic<(
221	// Two routers - use UMP to communicate with the relay chain:
222	cumulus_primitives_utility::ParentAsUmp<ParachainSystem, PolkadotXcm, PriceForParentDelivery>,
223	// ..and XCMP to communicate with the sibling chains.
224	XcmpQueue,
225)>;
226
227impl pallet_xcm::Config for Runtime {
228	type RuntimeEvent = RuntimeEvent;
229	// We want to disallow users sending (arbitrary) XCMs from this chain.
230	type SendXcmOrigin = EnsureXcmOrigin<RuntimeOrigin, ()>;
231	type XcmRouter = XcmRouter;
232	// We support local origins dispatching XCM executions.
233	type ExecuteXcmOrigin = EnsureXcmOrigin<RuntimeOrigin, LocalOriginToLocation>;
234	// allowed.
235	type XcmExecuteFilter = Everything;
236	type XcmExecutor = XcmExecutor<XcmConfig>;
237	type XcmTeleportFilter = Everything;
238	type XcmReserveTransferFilter = Everything;
239	type Weigher = FixedWeightBounds<UnitWeightCost, RuntimeCall, MaxInstructions>;
240	type UniversalLocation = UniversalLocation;
241	type RuntimeOrigin = RuntimeOrigin;
242	type RuntimeCall = RuntimeCall;
243	const VERSION_DISCOVERY_QUEUE_SIZE: u32 = 100;
244	type AdvertisedXcmVersion = pallet_xcm::CurrentXcmVersion;
245	type Currency = Balances;
246	type CurrencyMatcher = ();
247	type TrustedLockers = ();
248	type SovereignAccountOf = LocationToAccountId;
249	type MaxLockers = ConstU32<8>;
250	// FIXME: Replace with benchmarked weight info
251	type WeightInfo = pallet_xcm::TestWeightInfo;
252	type AdminOrigin = EnsureRoot<AccountId>;
253	type MaxRemoteLockConsumers = ConstU32<0>;
254	type RemoteLockConsumerIdentifier = ();
255}
256
257impl cumulus_pallet_xcm::Config for Runtime {
258	type RuntimeEvent = RuntimeEvent;
259	type XcmExecutor = XcmExecutor<XcmConfig>;
260}
261
262parameter_types! {
263	/// The asset ID for the asset that we use to pay for message delivery fees.
264	pub FeeAssetId: AssetId = AssetId(RelayLocation::get());
265	/// The base fee for the message delivery fees.
266	pub const BaseDeliveryFee: u128 = CENTS.saturating_mul(3);
267}
268
269pub type PriceForSiblingParachainDelivery = polkadot_runtime_common::xcm_sender::ExponentialPrice<
270	FeeAssetId,
271	BaseDeliveryFee,
272	TransactionByteFee,
273	XcmpQueue,
274>;
275
276impl cumulus_pallet_xcmp_queue::Config for Runtime {
277	type RuntimeEvent = RuntimeEvent;
278	type ChannelInfo = ParachainSystem;
279	type VersionWrapper = PolkadotXcm;
280	// Enqueue XCMP messages from siblings for later processing.
281	#[cfg(feature = "runtime-benchmarks")]
282	type XcmpQueue = ();
283	#[cfg(not(feature = "runtime-benchmarks"))]
284	type XcmpQueue = frame_support::traits::TransformOrigin<
285		crate::MessageQueue,
286		AggregateMessageOrigin,
287		cumulus_primitives_core::ParaId,
288		parachains_common::message_queue::ParaIdToSibling,
289	>;
290	type MaxInboundSuspended = ConstU32<1_000>;
291	type MaxActiveOutboundChannels = ConstU32<128>;
292	// Most on-chain HRMP channels are configured to use 102400 bytes of max message size, so we
293	// need to set the page size larger than that until we reduce the channel size on-chain.
294	type MaxPageSize = ConstU32<{ 103 * 1024 }>;
295	type ControllerOrigin = EitherOfDiverse<
296		EnsureRoot<AccountId>,
297		EnsureXcm<IsMajorityOfBody<RelayLocation, ExecutiveBody>>,
298	>;
299	type ControllerOriginConverter = XcmOriginToTransactDispatchOrigin;
300	type WeightInfo = cumulus_pallet_xcmp_queue::weights::SubstrateWeight<Runtime>;
301	type PriceForSiblingDelivery = PriceForSiblingParachainDelivery;
302}
303
304impl cumulus_pallet_xcmp_queue::migration::v5::V5Config for Runtime {
305	// This must be the same as the `ChannelInfo` from the `Config`:
306	type ChannelList = ParachainSystem;
307}
308
309parameter_types! {
310	pub const RelayOrigin: AggregateMessageOrigin = AggregateMessageOrigin::Parent;
311}