relay_chain.rs 8.35 KiB
Newer Older
// Copyright (C) Parity Technologies (UK) Ltd.
Vincent Ulitzsch's avatar
Vincent Ulitzsch committed
// This file is part of Polkadot.

// Polkadot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Polkadot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Polkadot.  If not, see <http://www.gnu.org/licenses/>.

//! Relay chain runtime mock.

use frame_support::{
	construct_runtime, derive_impl, parameter_types,
	traits::{Everything, Nothing, ProcessMessage, ProcessMessageError},
	weights::{Weight, WeightMeter},
Vincent Ulitzsch's avatar
Vincent Ulitzsch committed
};

use frame_system::EnsureRoot;
use sp_core::ConstU32;
use sp_runtime::{
	generic,
	traits::{BlakeTwo256, IdentifyAccount, Verify},
	MultiAddress, MultiSignature,
};
use polkadot_parachain_primitives::primitives::Id as ParaId;
use polkadot_runtime_parachains::{
	configuration,
	inclusion::{AggregateMessageOrigin, UmpQueueId},
	origin, shared,
};
Vincent Ulitzsch's avatar
Vincent Ulitzsch committed
use xcm::latest::prelude::*;
use xcm_builder::{
	AccountId32Aliases, AllowUnpaidExecutionFrom, ChildParachainAsNative,
	ChildParachainConvertsVia, ChildSystemParachainAsSuperuser, FixedRateOfFungible,
	FixedWeightBounds, FrameTransactionalProcessor, FungibleAdapter, IsConcrete,
	SignedAccountId32AsNative, SignedToAccountId32, SovereignSignedViaLocation,
Vincent Ulitzsch's avatar
Vincent Ulitzsch committed
};
use xcm_executor::{Config, XcmExecutor};

pub type SignedExtra = (frame_system::CheckNonZeroSender<Runtime>,);

pub type BlockNumber = u64;
pub type Address = MultiAddress<AccountId, ()>;
pub type Header = generic::Header<BlockNumber, BlakeTwo256>;
pub type UncheckedExtrinsic =
	generic::UncheckedExtrinsic<Address, RuntimeCall, Signature, SignedExtra>;
pub type Block = generic::Block<Header, UncheckedExtrinsic>;

pub type Signature = MultiSignature;
pub type AccountId = <<Signature as Verify>::Signer as IdentifyAccount>::AccountId;
Vincent Ulitzsch's avatar
Vincent Ulitzsch committed
pub type Balance = u128;

parameter_types! {
	pub const BlockHashCount: u32 = 250;
#[derive_impl(frame_system::config_preludes::TestDefaultConfig)]
Vincent Ulitzsch's avatar
Vincent Ulitzsch committed
impl frame_system::Config for Runtime {
	type AccountId = AccountId;
	type Lookup = sp_runtime::traits::AccountIdLookup<AccountId, ()>;
Vincent Ulitzsch's avatar
Vincent Ulitzsch committed
	type AccountData = pallet_balances::AccountData<Balance>;
}

parameter_types! {
	pub ExistentialDeposit: Balance = 1;
	pub const MaxLocks: u32 = 50;
	pub const MaxReserves: u32 = 50;
}

impl pallet_balances::Config for Runtime {
	type MaxLocks = MaxLocks;
	type Balance = Balance;
	type RuntimeEvent = RuntimeEvent;
Vincent Ulitzsch's avatar
Vincent Ulitzsch committed
	type DustRemoval = ();
	type ExistentialDeposit = ExistentialDeposit;
	type AccountStore = System;
	type WeightInfo = ();
	type MaxReserves = MaxReserves;
	type ReserveIdentifier = [u8; 8];
	type RuntimeHoldReason = RuntimeHoldReason;
	type RuntimeFreezeReason = RuntimeFreezeReason;
	type FreezeIdentifier = ();
	type MaxFreezes = ConstU32<0>;
impl shared::Config for Runtime {
	type DisabledValidators = ();
}
Vincent Ulitzsch's avatar
Vincent Ulitzsch committed

impl configuration::Config for Runtime {
	type WeightInfo = configuration::TestWeightInfo;
}

parameter_types! {
Francisco Aguirre's avatar
Francisco Aguirre committed
	pub const TokenLocation: Location = Here.into_location();
Gavin Wood's avatar
Gavin Wood committed
	pub const ThisNetwork: NetworkId = NetworkId::ByGenesis([0; 32]);
	pub const AnyNetwork: Option<NetworkId> = None;
	pub UniversalLocation: InteriorLocation = ThisNetwork::get().into();
Vincent Ulitzsch's avatar
Vincent Ulitzsch committed
}

pub type SovereignAccountOf =
Gavin Wood's avatar
Gavin Wood committed
	(ChildParachainConvertsVia<ParaId, AccountId>, AccountId32Aliases<ThisNetwork, AccountId>);
Vincent Ulitzsch's avatar
Vincent Ulitzsch committed

pub type LocalAssetTransactor =
	FungibleAdapter<Balances, IsConcrete<TokenLocation>, SovereignAccountOf, AccountId, ()>;
Vincent Ulitzsch's avatar
Vincent Ulitzsch committed

type LocalOriginConverter = (
Sergej Sakac's avatar
Sergej Sakac committed
	SovereignSignedViaLocation<SovereignAccountOf, RuntimeOrigin>,
	ChildParachainAsNative<origin::Origin, RuntimeOrigin>,
Gavin Wood's avatar
Gavin Wood committed
	SignedAccountId32AsNative<ThisNetwork, RuntimeOrigin>,
Sergej Sakac's avatar
Sergej Sakac committed
	ChildSystemParachainAsSuperuser<ParaId, RuntimeOrigin>,
Vincent Ulitzsch's avatar
Vincent Ulitzsch committed
);

parameter_types! {
Gavin Wood's avatar
Gavin Wood committed
	pub const BaseXcmWeight: Weight = Weight::from_parts(1_000, 1_000);
Francisco Aguirre's avatar
Francisco Aguirre committed
	pub KsmPerSecondPerByte: (AssetId, u128, u128) = (AssetId(TokenLocation::get()), 1, 1);
	pub const MaxInstructions: u32 = u32::MAX;
Gavin Wood's avatar
Gavin Wood committed
	pub const MaxAssetsIntoHolding: u32 = 64;
Vincent Ulitzsch's avatar
Vincent Ulitzsch committed
}

pub type XcmRouter = super::RelayChainXcmRouter;
pub type Barrier = AllowUnpaidExecutionFrom<Everything>;

pub struct XcmConfig;
impl Config for XcmConfig {
	type RuntimeCall = RuntimeCall;
Vincent Ulitzsch's avatar
Vincent Ulitzsch committed
	type XcmSender = XcmRouter;
	type AssetTransactor = LocalAssetTransactor;
	type OriginConverter = LocalOriginConverter;
	type IsReserve = ();
	type IsTeleporter = ();
Gavin Wood's avatar
Gavin Wood committed
	type UniversalLocation = UniversalLocation;
Vincent Ulitzsch's avatar
Vincent Ulitzsch committed
	type Barrier = Barrier;
	type Weigher = FixedWeightBounds<BaseXcmWeight, RuntimeCall, MaxInstructions>;
Gavin Wood's avatar
Gavin Wood committed
	type Trader = FixedRateOfFungible<KsmPerSecondPerByte, ()>;
Vincent Ulitzsch's avatar
Vincent Ulitzsch committed
	type ResponseHandler = ();
	type AssetTrap = ();
Gavin Wood's avatar
Gavin Wood committed
	type AssetLocker = ();
	type AssetExchanger = ();
Vincent Ulitzsch's avatar
Vincent Ulitzsch committed
	type AssetClaims = ();
	type SubscriptionService = ();
Gavin Wood's avatar
Gavin Wood committed
	type PalletInstancesInfo = ();
	type FeeManager = ();
	type MaxAssetsIntoHolding = MaxAssetsIntoHolding;
	type MessageExporter = ();
	type UniversalAliases = Nothing;
	type CallDispatcher = RuntimeCall;
	type SafeCallFilter = Everything;
	type Aliasers = Nothing;
	type TransactionalProcessor = FrameTransactionalProcessor;
	type HrmpNewChannelOpenRequestHandler = ();
	type HrmpChannelAcceptedHandler = ();
	type HrmpChannelClosingHandler = ();
Gavin Wood's avatar
Gavin Wood committed
pub type LocalOriginToLocation = SignedToAccountId32<RuntimeOrigin, AccountId, ThisNetwork>;

Vincent Ulitzsch's avatar
Vincent Ulitzsch committed
impl pallet_xcm::Config for Runtime {
	type RuntimeEvent = RuntimeEvent;
Sergej Sakac's avatar
Sergej Sakac committed
	type SendXcmOrigin = xcm_builder::EnsureXcmOrigin<RuntimeOrigin, LocalOriginToLocation>;
Vincent Ulitzsch's avatar
Vincent Ulitzsch committed
	type XcmRouter = XcmRouter;
	// Anyone can execute XCM messages locally...
Sergej Sakac's avatar
Sergej Sakac committed
	type ExecuteXcmOrigin = xcm_builder::EnsureXcmOrigin<RuntimeOrigin, LocalOriginToLocation>;
Vincent Ulitzsch's avatar
Vincent Ulitzsch committed
	type XcmExecuteFilter = Nothing;
	type XcmExecutor = XcmExecutor<XcmConfig>;
	type XcmTeleportFilter = Everything;
	type XcmReserveTransferFilter = Everything;
	type Weigher = FixedWeightBounds<BaseXcmWeight, RuntimeCall, MaxInstructions>;
Gavin Wood's avatar
Gavin Wood committed
	type UniversalLocation = UniversalLocation;
Sergej Sakac's avatar
Sergej Sakac committed
	type RuntimeOrigin = RuntimeOrigin;
	type RuntimeCall = RuntimeCall;
Vincent Ulitzsch's avatar
Vincent Ulitzsch committed
	const VERSION_DISCOVERY_QUEUE_SIZE: u32 = 100;
	type AdvertisedXcmVersion = pallet_xcm::CurrentXcmVersion;
Gavin Wood's avatar
Gavin Wood committed
	type Currency = Balances;
	type CurrencyMatcher = ();
	type TrustedLockers = ();
	type SovereignAccountOf = SovereignAccountOf;
	type MaxLockers = ConstU32<8>;
	type MaxRemoteLockConsumers = ConstU32<0>;
	type RemoteLockConsumerIdentifier = ();
Gavin Wood's avatar
Gavin Wood committed
	type WeightInfo = pallet_xcm::TestWeightInfo;
	type AdminOrigin = EnsureRoot<AccountId>;
Vincent Ulitzsch's avatar
Vincent Ulitzsch committed
}

parameter_types! {
	pub const FirstMessageFactorPercent: u64 = 100;
Vincent Ulitzsch's avatar
Vincent Ulitzsch committed
}

impl origin::Config for Runtime {}

parameter_types! {
	/// Amount of weight that can be spent per block to service messages.
	pub MessageQueueServiceWeight: Weight = Weight::from_parts(1_000_000_000, 1_000_000);
	pub const MessageQueueHeapSize: u32 = 65_536;
	pub const MessageQueueMaxStale: u32 = 16;
}

/// Message processor to handle any messages that were enqueued into the `MessageQueue` pallet.
pub struct MessageProcessor;
impl ProcessMessage for MessageProcessor {
	type Origin = AggregateMessageOrigin;

	fn process_message(
		message: &[u8],
		origin: Self::Origin,
		meter: &mut WeightMeter,
	) -> Result<bool, ProcessMessageError> {
		let para = match origin {
			AggregateMessageOrigin::Ump(UmpQueueId::Para(para)) => para,
		};
		xcm_builder::ProcessXcmMessage::<
			Junction,
			xcm_executor::XcmExecutor<XcmConfig>,
			RuntimeCall,
		>::process_message(message, Junction::Parachain(para.into()), meter, id)
	}
}

impl pallet_message_queue::Config for Runtime {
	type RuntimeEvent = RuntimeEvent;
	type Size = u32;
	type HeapSize = MessageQueueHeapSize;
	type MaxStale = MessageQueueMaxStale;
	type ServiceWeight = MessageQueueServiceWeight;
	type IdleMaxServiceWeight = ();
	#[cfg(not(feature = "runtime-benchmarks"))]
	type MessageProcessor = MessageProcessor;
	#[cfg(feature = "runtime-benchmarks")]
	type MessageProcessor =
		pallet_message_queue::mock_helpers::NoopMessageProcessor<AggregateMessageOrigin>;
	type QueueChangeHandler = ();
Vincent Ulitzsch's avatar
Vincent Ulitzsch committed
construct_runtime!(
		System: frame_system,
		Balances: pallet_balances,
		ParasOrigin: origin,
		XcmPallet: pallet_xcm,
		MessageQueue: pallet_message_queue,