mock.rs 6.88 KiB
Newer Older
// Copyright (C) Parity Technologies (UK) Ltd.
// 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/>.

//! A mock runtime for XCM benchmarking.

use crate::{generic, mock::*, *};
use frame_support::{
	parameter_types,
	traits::{Everything, OriginTrait},
	weights::Weight,
};
use sp_core::H256;
use sp_runtime::{
	testing::Header,
	traits::{BlakeTwo256, IdentityLookup, TrailingZeroInput},
};
use xcm_builder::{
Gavin Wood's avatar
Gavin Wood committed
	test_utils::{
		Assets, TestAssetExchanger, TestAssetLocker, TestAssetTrap, TestSubscriptionService,
		TestUniversalAliases,
	},
	AllowUnpaidExecutionFrom,
};
use xcm_executor::traits::ConvertOrigin;

type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
type Block = frame_system::mocking::MockBlock<Test>;

frame_support::construct_runtime!(
	pub enum Test where
		Block = Block,
		NodeBlock = Block,
		UncheckedExtrinsic = UncheckedExtrinsic,
	{
		System: frame_system::{Pallet, Call, Config, Storage, Event<T>},
		XcmGenericBenchmarks: generic::{Pallet},
	}
);

parameter_types! {
	pub const BlockHashCount: u64 = 250;
	pub BlockWeights: frame_system::limits::BlockWeights =
Gavin Wood's avatar
Gavin Wood committed
		frame_system::limits::BlockWeights::simple_max(Weight::from_parts(1024, u64::MAX));
}

impl frame_system::Config for Test {
	type BaseCallFilter = Everything;
	type BlockWeights = ();
	type BlockLength = ();
	type DbWeight = ();
Sergej Sakac's avatar
Sergej Sakac committed
	type RuntimeOrigin = RuntimeOrigin;
	type Index = u64;
	type BlockNumber = u64;
	type Hash = H256;
	type RuntimeCall = RuntimeCall;
	type Hashing = BlakeTwo256;
	type AccountId = u64;
	type Lookup = IdentityLookup<Self::AccountId>;
	type Header = Header;
	type RuntimeEvent = RuntimeEvent;
	type BlockHashCount = BlockHashCount;
	type Version = ();
	type PalletInfo = PalletInfo;
	type AccountData = pallet_balances::AccountData<u64>;
	type OnNewAccount = ();
	type OnKilledAccount = ();
	type SystemWeightInfo = ();
	type SS58Prefix = ();
	type OnSetCode = ();
	type MaxConsumers = frame_support::traits::ConstU32<16>;
}

/// The benchmarks in this pallet should never need an asset transactor to begin with.
pub struct NoAssetTransactor;
impl xcm_executor::traits::TransactAsset for NoAssetTransactor {
Gavin Wood's avatar
Gavin Wood committed
	fn deposit_asset(_: &MultiAsset, _: &MultiLocation, _: &XcmContext) -> Result<(), XcmError> {
Gavin Wood's avatar
Gavin Wood committed
	fn withdraw_asset(
		_: &MultiAsset,
		_: &MultiLocation,
		_: Option<&XcmContext>,
	) -> Result<Assets, XcmError> {
		unreachable!();
	}
}

parameter_types! {
	pub const MaxInstructions: u32 = 100;
Gavin Wood's avatar
Gavin Wood committed
	pub const MaxAssetsIntoHolding: u32 = 64;
}

pub struct XcmConfig;
impl xcm_executor::Config for XcmConfig {
	type RuntimeCall = RuntimeCall;
	type XcmSender = DevNull;
	type AssetTransactor = NoAssetTransactor;
Sergej Sakac's avatar
Sergej Sakac committed
	type OriginConverter = AlwaysSignedByDefault<RuntimeOrigin>;
	type IsReserve = AllAssetLocationsPass;
	type IsTeleporter = ();
Gavin Wood's avatar
Gavin Wood committed
	type UniversalLocation = UniversalLocation;
	type Barrier = AllowUnpaidExecutionFrom<Everything>;
	type Weigher = xcm_builder::FixedWeightBounds<UnitWeightCost, RuntimeCall, MaxInstructions>;
	type Trader = xcm_builder::FixedRateOfFungible<WeightPrice, ()>;
	type ResponseHandler = DevNull;
	type AssetTrap = TestAssetTrap;
Gavin Wood's avatar
Gavin Wood committed
	type AssetLocker = TestAssetLocker;
	type AssetExchanger = TestAssetExchanger;
	type AssetClaims = TestAssetTrap;
	type SubscriptionService = TestSubscriptionService;
Gavin Wood's avatar
Gavin Wood committed
	type PalletInstancesInfo = AllPalletsWithSystem;
	type MaxAssetsIntoHolding = MaxAssetsIntoHolding;
	type FeeManager = ();
	// No bridges yet...
	type MessageExporter = ();
	type UniversalAliases = TestUniversalAliases;
	type CallDispatcher = RuntimeCall;
	type SafeCallFilter = Everything;
}

impl crate::Config for Test {
	type XcmConfig = XcmConfig;
	type AccountIdConverter = AccountIdConverter;
	fn valid_destination() -> Result<MultiLocation, BenchmarkError> {
		let valid_destination: MultiLocation =
Gavin Wood's avatar
Gavin Wood committed
			Junction::AccountId32 { network: None, id: [0u8; 32] }.into();
Gavin Wood's avatar
Gavin Wood committed
	fn worst_case_holding(depositable_count: u32) -> MultiAssets {
		crate::mock_worst_case_holding(
			depositable_count,
			<XcmConfig as xcm_executor::Config>::MaxAssetsIntoHolding::get(),
		)
	}
}

impl generic::Config for Test {
	type RuntimeCall = RuntimeCall;

	fn worst_case_response() -> (u64, Response) {
		let assets: MultiAssets = (Concrete(Here.into()), 100).into();
		(0, Response::Assets(assets))
	}

Gavin Wood's avatar
Gavin Wood committed
	fn worst_case_asset_exchange() -> Result<(MultiAssets, MultiAssets), BenchmarkError> {
	fn universal_alias() -> Result<(MultiLocation, Junction), BenchmarkError> {
		Ok((Here.into(), GlobalConsensus(ByGenesis([0; 32]))))
Gavin Wood's avatar
Gavin Wood committed
	}

	fn transact_origin_and_runtime_call(
	) -> Result<(MultiLocation, <Self as generic::Config>::RuntimeCall), BenchmarkError> {
		Ok((Default::default(), frame_system::Call::remark_with_event { remark: vec![] }.into()))
	}

	fn subscribe_origin() -> Result<MultiLocation, BenchmarkError> {
		Ok(Default::default())
	}

	fn claimable_asset() -> Result<(MultiLocation, MultiLocation, MultiAssets), BenchmarkError> {
		let assets: MultiAssets = (Concrete(Here.into()), 100).into();
		let ticket = MultiLocation { parents: 0, interior: X1(GeneralIndex(0)) };
		Ok((Default::default(), ticket, assets))
	}
Gavin Wood's avatar
Gavin Wood committed

	fn unlockable_asset() -> Result<(MultiLocation, MultiLocation, MultiAsset), BenchmarkError> {
		let assets: MultiAsset = (Concrete(Here.into()), 100).into();
		Ok((Default::default(), Default::default(), assets))
	}

	fn export_message_origin_and_destination(
	) -> Result<(MultiLocation, NetworkId, InteriorMultiLocation), BenchmarkError> {
		// No MessageExporter in tests
		Err(BenchmarkError::Skip)
	}
#[cfg(feature = "runtime-benchmarks")]
pub fn new_test_ext() -> sp_io::TestExternalities {
	use sp_runtime::BuildStorage;
	let t = GenesisConfig { ..Default::default() }.build_storage().unwrap();
	sp_tracing::try_init_simple();
	t.into()
}

Sergej Sakac's avatar
Sergej Sakac committed
pub struct AlwaysSignedByDefault<RuntimeOrigin>(core::marker::PhantomData<RuntimeOrigin>);
impl<RuntimeOrigin> ConvertOrigin<RuntimeOrigin> for AlwaysSignedByDefault<RuntimeOrigin>
Sergej Sakac's avatar
Sergej Sakac committed
	RuntimeOrigin: OriginTrait,
	<RuntimeOrigin as OriginTrait>::AccountId: Decode,
{
	fn convert_origin(
		_origin: impl Into<MultiLocation>,
		_kind: OriginKind,
Sergej Sakac's avatar
Sergej Sakac committed
	) -> Result<RuntimeOrigin, MultiLocation> {
		Ok(RuntimeOrigin::signed(
			<RuntimeOrigin as OriginTrait>::AccountId::decode(&mut TrailingZeroInput::zeroes())
				.expect("infinite length input; no invalid inputs for type; qed"),
		))