Skip to content
parachain.rs 11.1 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/>.

//! Parachain runtime mock.

use codec::{Decode, Encode};
use frame_support::{
	construct_runtime, derive_impl, parameter_types,
Vincent Ulitzsch's avatar
Vincent Ulitzsch committed
	traits::{Everything, Nothing},
	weights::{constants::WEIGHT_REF_TIME_PER_SECOND, Weight},
Vincent Ulitzsch's avatar
Vincent Ulitzsch committed
};

use frame_system::EnsureRoot;
Vincent Ulitzsch's avatar
Vincent Ulitzsch committed
use sp_runtime::{
	generic,
	traits::{AccountIdLookup, BlakeTwo256, Hash, IdentifyAccount, Verify},
	MultiAddress, MultiSignature,
Vincent Ulitzsch's avatar
Vincent Ulitzsch committed
};
use sp_std::prelude::*;
Vincent Ulitzsch's avatar
Vincent Ulitzsch committed

use pallet_xcm::XcmPassthrough;
use polkadot_core_primitives::BlockNumber as RelayBlockNumber;
use polkadot_parachain_primitives::primitives::{
Vincent Ulitzsch's avatar
Vincent Ulitzsch committed
	DmpMessageHandler, Id as ParaId, Sibling, XcmpMessageFormat, XcmpMessageHandler,
};
use xcm::{latest::prelude::*, VersionedXcm};
use xcm_builder::{
	AccountId32Aliases, AllowUnpaidExecutionFrom, EnsureXcmOrigin, FixedRateOfFungible,
	FixedWeightBounds, FrameTransactionalProcessor, FungibleAdapter, IsConcrete, NativeAsset,
	ParentIsPreset, SiblingParachainConvertsVia, 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 = 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>;
Vincent Ulitzsch's avatar
Vincent Ulitzsch committed
}

parameter_types! {
	pub const ReservedXcmpWeight: Weight = Weight::from_parts(WEIGHT_REF_TIME_PER_SECOND.saturating_div(4), 0);
	pub const ReservedDmpWeight: Weight = Weight::from_parts(WEIGHT_REF_TIME_PER_SECOND.saturating_div(4), 0);
Vincent Ulitzsch's avatar
Vincent Ulitzsch committed
}

parameter_types! {
Francisco Aguirre's avatar
Francisco Aguirre committed
	pub const KsmLocation: Location = Location::parent();
Vincent Ulitzsch's avatar
Vincent Ulitzsch committed
	pub const RelayNetwork: NetworkId = NetworkId::Kusama;
Francisco Aguirre's avatar
Francisco Aguirre committed
	pub UniversalLocation: InteriorLocation = Parachain(MsgQueue::parachain_id().into()).into();
Vincent Ulitzsch's avatar
Vincent Ulitzsch committed
}

pub type LocationToAccountId = (
Vincent Ulitzsch's avatar
Vincent Ulitzsch committed
	SiblingParachainConvertsVia<Sibling, AccountId>,
	AccountId32Aliases<RelayNetwork, AccountId>,
);

pub type XcmOriginToCallOrigin = (
Sergej Sakac's avatar
Sergej Sakac committed
	SovereignSignedViaLocation<LocationToAccountId, RuntimeOrigin>,
	SignedAccountId32AsNative<RelayNetwork, RuntimeOrigin>,
	XcmPassthrough<RuntimeOrigin>,
Vincent Ulitzsch's avatar
Vincent Ulitzsch committed
);

parameter_types! {
Gavin Wood's avatar
Gavin Wood committed
	pub const UnitWeightCost: Weight = Weight::from_parts(1, 1);
Francisco Aguirre's avatar
Francisco Aguirre committed
	pub KsmPerSecondPerByte: (AssetId, u128, u128) = (AssetId(Parent.into()), 1, 1);
Vincent Ulitzsch's avatar
Vincent Ulitzsch committed
	pub const MaxInstructions: u32 = 100;
Gavin Wood's avatar
Gavin Wood committed
	pub const MaxAssetsIntoHolding: u32 = 64;
Vincent Ulitzsch's avatar
Vincent Ulitzsch committed
}

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

pub type XcmRouter = super::ParachainXcmRouter<MsgQueue>;
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 = XcmOriginToCallOrigin;
	type IsReserve = NativeAsset;
	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<UnitWeightCost, 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;
Vincent Ulitzsch's avatar
Vincent Ulitzsch committed
}

#[frame_support::pallet]
pub mod mock_msg_queue {
	use super::*;
	use frame_support::pallet_prelude::*;

	#[pallet::config]
	pub trait Config: frame_system::Config {
		type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
		type XcmExecutor: ExecuteXcm<Self::RuntimeCall>;
Vincent Ulitzsch's avatar
Vincent Ulitzsch committed
	}

	#[pallet::call]
	impl<T: Config> Pallet<T> {}

	#[pallet::pallet]
	#[pallet::without_storage_info]
Vincent Ulitzsch's avatar
Vincent Ulitzsch committed
	pub struct Pallet<T>(_);

	#[pallet::storage]
	#[pallet::getter(fn parachain_id)]
	pub(super) type ParachainId<T: Config> = StorageValue<_, ParaId, ValueQuery>;

	#[pallet::storage]
	#[pallet::getter(fn received_dmp)]
	/// A queue of received DMP messages
	pub(super) type ReceivedDmp<T: Config> = StorageValue<_, Vec<Xcm<T::RuntimeCall>>, ValueQuery>;
Vincent Ulitzsch's avatar
Vincent Ulitzsch committed

	impl<T: Config> Get<ParaId> for Pallet<T> {
		fn get() -> ParaId {
			Self::parachain_id()
		}
	}

	pub type MessageId = [u8; 32];

	#[pallet::event]
	#[pallet::generate_deposit(pub(super) fn deposit_event)]
	pub enum Event<T: Config> {
		// XCMP
		/// Some XCM was executed OK.
		Success(Option<T::Hash>),
		/// Some XCM failed.
		Fail(Option<T::Hash>, XcmError),
		/// Bad XCM version used.
		BadVersion(Option<T::Hash>),
		/// Bad XCM format used.
		BadFormat(Option<T::Hash>),

		// DMP
		/// Downward message is invalid XCM.
		InvalidFormat(MessageId),
		/// Downward message is unsupported version of XCM.
		UnsupportedVersion(MessageId),
		/// Downward message executed with the given outcome.
		ExecutedDownward(MessageId, Outcome),
	}

	impl<T: Config> Pallet<T> {
		pub fn set_para_id(para_id: ParaId) {
			ParachainId::<T>::put(para_id);
		}

		fn handle_xcmp_message(
			sender: ParaId,
			_sent_at: RelayBlockNumber,
			xcm: VersionedXcm<T::RuntimeCall>,
Vincent Ulitzsch's avatar
Vincent Ulitzsch committed
			max_weight: Weight,
		) -> Result<Weight, XcmError> {
			let hash = Encode::using_encoded(&xcm, T::Hashing::hash);
Francisco Aguirre's avatar
Francisco Aguirre committed
			let mut message_hash = xcm.using_encoded(sp_io::hashing::blake2_256);
			let (result, event) = match Xcm::<T::RuntimeCall>::try_from(xcm) {
Vincent Ulitzsch's avatar
Vincent Ulitzsch committed
				Ok(xcm) => {
Francisco Aguirre's avatar
Francisco Aguirre committed
					let location = Location::new(1, [Parachain(sender.into())]);
					match T::XcmExecutor::prepare_and_execute(
						location,
						xcm,
						&mut message_hash,
						max_weight,
						Weight::zero(),
					) {
						Outcome::Error { error } => (Err(error), Event::Fail(Some(hash), error)),
						Outcome::Complete { used } => (Ok(used), Event::Success(Some(hash))),
Vincent Ulitzsch's avatar
Vincent Ulitzsch committed
						// As far as the caller is concerned, this was dispatched without error, so
						// we just report the weight used.
Francisco Aguirre's avatar
Francisco Aguirre committed
						Outcome::Incomplete { used, error } =>
							(Ok(used), Event::Fail(Some(hash), error)),
Vincent Ulitzsch's avatar
Vincent Ulitzsch committed
					}
				},
				Err(()) => (Err(XcmError::UnhandledXcmVersion), Event::BadVersion(Some(hash))),
			};
			Self::deposit_event(event);
			result
		}
	}

	impl<T: Config> XcmpMessageHandler for Pallet<T> {
		fn handle_xcmp_messages<'a, I: Iterator<Item = (ParaId, RelayBlockNumber, &'a [u8])>>(
			iter: I,
			max_weight: Weight,
		) -> Weight {
			for (sender, sent_at, data) in iter {
				let mut data_ref = data;
				let _ = XcmpMessageFormat::decode(&mut data_ref)
					.expect("Simulator encodes with versioned xcm format; qed");

				let mut remaining_fragments = data_ref;
Vincent Ulitzsch's avatar
Vincent Ulitzsch committed
				while !remaining_fragments.is_empty() {
					if let Ok(xcm) =
						VersionedXcm::<T::RuntimeCall>::decode(&mut remaining_fragments)
					{
Vincent Ulitzsch's avatar
Vincent Ulitzsch committed
						let _ = Self::handle_xcmp_message(sender, sent_at, xcm, max_weight);
					} else {
						debug_assert!(false, "Invalid incoming XCMP message data");
					}
				}
			}
			max_weight
		}
	}

	impl<T: Config> DmpMessageHandler for Pallet<T> {
		fn handle_dmp_messages(
			iter: impl Iterator<Item = (RelayBlockNumber, Vec<u8>)>,
			limit: Weight,
		) -> Weight {
			for (_i, (_sent_at, data)) in iter.enumerate() {
Francisco Aguirre's avatar
Francisco Aguirre committed
				let mut id = sp_io::hashing::blake2_256(&data[..]);
				let maybe_msg = VersionedXcm::<T::RuntimeCall>::decode(&mut &data[..])
					.map(Xcm::<T::RuntimeCall>::try_from);
Vincent Ulitzsch's avatar
Vincent Ulitzsch committed
				match maybe_msg {
					Err(_) => {
						Self::deposit_event(Event::InvalidFormat(id));
					},
					Ok(Err(())) => {
						Self::deposit_event(Event::UnsupportedVersion(id));
					},
					Ok(Ok(x)) => {
Francisco Aguirre's avatar
Francisco Aguirre committed
						let outcome = T::XcmExecutor::prepare_and_execute(
							Parent,
							x.clone(),
							&mut id,
							limit,
							Weight::zero(),
						);
Vincent Ulitzsch's avatar
Vincent Ulitzsch committed
						<ReceivedDmp<T>>::append(x);
						Self::deposit_event(Event::ExecutedDownward(id, outcome));
					},
				}
			}
			limit
		}
	}
}

impl mock_msg_queue::Config for Runtime {
	type RuntimeEvent = RuntimeEvent;
Vincent Ulitzsch's avatar
Vincent Ulitzsch committed
	type XcmExecutor = XcmExecutor<XcmConfig>;
}

Sergej Sakac's avatar
Sergej Sakac committed
pub type LocalOriginToLocation = SignedToAccountId32<RuntimeOrigin, AccountId, RelayNetwork>;
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 = EnsureXcmOrigin<RuntimeOrigin, LocalOriginToLocation>;
Vincent Ulitzsch's avatar
Vincent Ulitzsch committed
	type XcmRouter = XcmRouter;
Sergej Sakac's avatar
Sergej Sakac committed
	type ExecuteXcmOrigin = EnsureXcmOrigin<RuntimeOrigin, LocalOriginToLocation>;
Vincent Ulitzsch's avatar
Vincent Ulitzsch committed
	type XcmExecuteFilter = Everything;
	type XcmExecutor = XcmExecutor<XcmConfig>;
	type XcmTeleportFilter = Nothing;
	type XcmReserveTransferFilter = Everything;
	type Weigher = FixedWeightBounds<UnitWeightCost, 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 = LocationToAccountId;
	type MaxLockers = frame_support::traits::ConstU32<8>;
	type MaxRemoteLockConsumers = frame_support::traits::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
}

construct_runtime!(
		System: frame_system,
		Balances: pallet_balances,
		MsgQueue: mock_msg_queue,
		PolkadotXcm: pallet_xcm,