Skip to content
mock.rs 22.9 KiB
Newer Older
// Copyright (C) Parity Technologies (UK) Ltd.
Gavin Wood's avatar
Gavin Wood 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/>.

//! Mock implementations to test XCM builder configuration types.

use crate::{
	barriers::{AllowSubscriptionsFrom, RespectSuspension, TrailingSetTopicAsId},
	test_utils::*,
};
Gavin Wood's avatar
Gavin Wood committed
pub use crate::{
	AliasForeignAccountId32, AllowExplicitUnpaidExecutionFrom, AllowKnownQueryResponses,
	AllowTopLevelPaidExecutionFrom, AllowUnpaidExecutionFrom, FixedRateOfFungible,
	FixedWeightBounds, TakeWeightCredit,
Gavin Wood's avatar
Gavin Wood committed
};
use frame_support::traits::{ContainsPair, Everything};
pub use frame_support::{
	dispatch::{DispatchInfo, DispatchResultWithPostInfo, GetDispatchInfo, PostDispatchInfo},
Francisco Aguirre's avatar
Francisco Aguirre committed
	ensure, parameter_types,
	sp_runtime::{traits::Dispatchable, DispatchError, DispatchErrorWithPostInfo},
	traits::{Contains, Get, IsInVec},
Gavin Wood's avatar
Gavin Wood committed
};
pub use parity_scale_codec::{Decode, Encode};
pub use sp_std::{
	cell::{Cell, RefCell},
Gavin Wood's avatar
Gavin Wood committed
	collections::{btree_map::BTreeMap, btree_set::BTreeSet},
	fmt::Debug,
};
pub use xcm::latest::{prelude::*, Weight};
use xcm_executor::traits::{Properties, QueryHandler, QueryResponseStatus};
Gavin Wood's avatar
Gavin Wood committed
pub use xcm_executor::{
	traits::{
		AssetExchange, AssetLock, CheckSuspension, ConvertOrigin, Enact, ExportXcm, FeeManager,
		FeeReason, LockError, OnResponse, TransactAsset,
Gavin Wood's avatar
Gavin Wood committed
	},
Francisco Aguirre's avatar
Francisco Aguirre committed
	AssetsInHolding, Config,
Gavin Wood's avatar
Gavin Wood committed
};

#[derive(Debug)]
Gavin Wood's avatar
Gavin Wood committed
pub enum TestOrigin {
	Root,
	Relay,
	Signed(u64),
	Parachain(u32),
}

/// A dummy call.
///
/// Each item contains the amount of weight that it *wants* to consume as the first item, and the
/// actual amount (if different from the former) in the second option.
Gavin Wood's avatar
Gavin Wood committed
#[derive(Debug, Encode, Decode, Eq, PartialEq, Clone, Copy, scale_info::TypeInfo)]
pub enum TestCall {
	OnlyRoot(Weight, Option<Weight>),
	OnlyParachain(Weight, Option<Weight>, Option<u32>),
	OnlySigned(Weight, Option<Weight>, Option<u64>),
	Any(Weight, Option<Weight>),
}
impl Dispatchable for TestCall {
	type RuntimeOrigin = TestOrigin;
	type Config = ();
	type Info = ();
	type PostInfo = PostDispatchInfo;
	fn dispatch(self, origin: Self::RuntimeOrigin) -> DispatchResultWithPostInfo {
		let mut post_info = PostDispatchInfo::default();
		let maybe_actual = match self {
			TestCall::OnlyRoot(_, maybe_actual) |
			TestCall::OnlySigned(_, maybe_actual, _) |
			TestCall::OnlyParachain(_, maybe_actual, _) |
			TestCall::Any(_, maybe_actual) => maybe_actual,
		};
		post_info.actual_weight = maybe_actual;
		if match (&origin, &self) {
			(TestOrigin::Parachain(i), TestCall::OnlyParachain(_, _, Some(j))) => i == j,
			(TestOrigin::Signed(i), TestCall::OnlySigned(_, _, Some(j))) => i == j,
			(TestOrigin::Root, TestCall::OnlyRoot(..)) |
			(TestOrigin::Parachain(_), TestCall::OnlyParachain(_, _, None)) |
			(TestOrigin::Signed(_), TestCall::OnlySigned(_, _, None)) |
			(_, TestCall::Any(..)) => true,
			_ => false,
		} {
			Ok(post_info)
		} else {
			Err(DispatchErrorWithPostInfo { error: DispatchError::BadOrigin, post_info })
		}
	}
}

impl GetDispatchInfo for TestCall {
	fn get_dispatch_info(&self) -> DispatchInfo {
		let weight = *match self {
			TestCall::OnlyRoot(estimate, ..) |
			TestCall::OnlyParachain(estimate, ..) |
			TestCall::OnlySigned(estimate, ..) |
			TestCall::Any(estimate, ..) => estimate,
		};
		DispatchInfo { weight, ..Default::default() }
	}
}

thread_local! {
Francisco Aguirre's avatar
Francisco Aguirre committed
	pub static SENT_XCM: RefCell<Vec<(Location, Xcm<()>, XcmHash)>> = RefCell::new(Vec::new());
Gavin Wood's avatar
Gavin Wood committed
	pub static EXPORTED_XCM: RefCell<
Francisco Aguirre's avatar
Francisco Aguirre committed
		Vec<(NetworkId, u32, InteriorLocation, InteriorLocation, Xcm<()>, XcmHash)>
Gavin Wood's avatar
Gavin Wood committed
	> = RefCell::new(Vec::new());
	pub static EXPORTER_OVERRIDE: RefCell<Option<(
		fn(
			NetworkId,
			u32,
Francisco Aguirre's avatar
Francisco Aguirre committed
			&InteriorLocation,
			&InteriorLocation,
Gavin Wood's avatar
Gavin Wood committed
			&Xcm<()>,
Francisco Aguirre's avatar
Francisco Aguirre committed
		) -> Result<Assets, SendError>,
Gavin Wood's avatar
Gavin Wood committed
		fn(
			NetworkId,
			u32,
Francisco Aguirre's avatar
Francisco Aguirre committed
			InteriorLocation,
			InteriorLocation,
Gavin Wood's avatar
Gavin Wood committed
			Xcm<()>,
		) -> Result<XcmHash, SendError>,
	)>> = RefCell::new(None);
Francisco Aguirre's avatar
Francisco Aguirre committed
	pub static SEND_PRICE: RefCell<Assets> = RefCell::new(Assets::new());
	pub static SUSPENDED: Cell<bool> = Cell::new(false);
Gavin Wood's avatar
Gavin Wood committed
}
Francisco Aguirre's avatar
Francisco Aguirre committed
pub fn sent_xcm() -> Vec<(Location, opaque::Xcm, XcmHash)> {
Gavin Wood's avatar
Gavin Wood committed
	SENT_XCM.with(|q| (*q.borrow()).clone())
}
Francisco Aguirre's avatar
Francisco Aguirre committed
pub fn set_send_price(p: impl Into<Asset>) {
Gavin Wood's avatar
Gavin Wood committed
	SEND_PRICE.with(|l| l.replace(p.into().into()));
}
pub fn exported_xcm(
Francisco Aguirre's avatar
Francisco Aguirre committed
) -> Vec<(NetworkId, u32, InteriorLocation, InteriorLocation, opaque::Xcm, XcmHash)> {
Gavin Wood's avatar
Gavin Wood committed
	EXPORTED_XCM.with(|q| (*q.borrow()).clone())
}
pub fn set_exporter_override(
	price: fn(
		NetworkId,
		u32,
Francisco Aguirre's avatar
Francisco Aguirre committed
		&InteriorLocation,
		&InteriorLocation,
Gavin Wood's avatar
Gavin Wood committed
		&Xcm<()>,
Francisco Aguirre's avatar
Francisco Aguirre committed
	) -> Result<Assets, SendError>,
Gavin Wood's avatar
Gavin Wood committed
	deliver: fn(
		NetworkId,
		u32,
Francisco Aguirre's avatar
Francisco Aguirre committed
		InteriorLocation,
		InteriorLocation,
Gavin Wood's avatar
Gavin Wood committed
		Xcm<()>,
	) -> Result<XcmHash, SendError>,
) {
	EXPORTER_OVERRIDE.with(|x| x.replace(Some((price, deliver))));
}
#[allow(dead_code)]
pub fn clear_exporter_override() {
	EXPORTER_OVERRIDE.with(|x| x.replace(None));
}
pub struct TestMessageSender;
impl SendXcm for TestMessageSender {
Francisco Aguirre's avatar
Francisco Aguirre committed
	type Ticket = (Location, Xcm<()>, XcmHash);
Gavin Wood's avatar
Gavin Wood committed
	fn validate(
Francisco Aguirre's avatar
Francisco Aguirre committed
		dest: &mut Option<Location>,
Gavin Wood's avatar
Gavin Wood committed
		msg: &mut Option<Xcm<()>>,
Francisco Aguirre's avatar
Francisco Aguirre committed
	) -> SendResult<(Location, Xcm<()>, XcmHash)> {
Gavin Wood's avatar
Gavin Wood committed
		let msg = msg.take().unwrap();
		let hash = fake_message_hash(&msg);
		let triplet = (dest.take().unwrap(), msg, hash);
		Ok((triplet, SEND_PRICE.with(|l| l.borrow().clone())))
	}
Francisco Aguirre's avatar
Francisco Aguirre committed
	fn deliver(triplet: (Location, Xcm<()>, XcmHash)) -> Result<XcmHash, SendError> {
Gavin Wood's avatar
Gavin Wood committed
		let hash = triplet.2;
		SENT_XCM.with(|q| q.borrow_mut().push(triplet));
		Ok(hash)
	}
}
pub struct TestMessageExporter;
impl ExportXcm for TestMessageExporter {
Francisco Aguirre's avatar
Francisco Aguirre committed
	type Ticket = (NetworkId, u32, InteriorLocation, InteriorLocation, Xcm<()>, XcmHash);
Gavin Wood's avatar
Gavin Wood committed
	fn validate(
		network: NetworkId,
		channel: u32,
Francisco Aguirre's avatar
Francisco Aguirre committed
		uni_src: &mut Option<InteriorLocation>,
		dest: &mut Option<InteriorLocation>,
Gavin Wood's avatar
Gavin Wood committed
		msg: &mut Option<Xcm<()>>,
Francisco Aguirre's avatar
Francisco Aguirre committed
	) -> SendResult<(NetworkId, u32, InteriorLocation, InteriorLocation, Xcm<()>, XcmHash)> {
Gavin Wood's avatar
Gavin Wood committed
		let (s, d, m) = (uni_src.take().unwrap(), dest.take().unwrap(), msg.take().unwrap());
Francisco Aguirre's avatar
Francisco Aguirre committed
		let r: Result<Assets, SendError> = EXPORTER_OVERRIDE.with(|e| {
Gavin Wood's avatar
Gavin Wood committed
			if let Some((ref f, _)) = &*e.borrow() {
				f(network, channel, &s, &d, &m)
			} else {
Francisco Aguirre's avatar
Francisco Aguirre committed
				Ok(Assets::new())
Gavin Wood's avatar
Gavin Wood committed
			}
		});
		let h = fake_message_hash(&m);
		match r {
			Ok(price) => Ok(((network, channel, s, d, m, h), price)),
			Err(e) => {
				*uni_src = Some(s);
				*dest = Some(d);
				*msg = Some(m);
				Err(e)
			},
		}
	}
	fn deliver(
Francisco Aguirre's avatar
Francisco Aguirre committed
		tuple: (NetworkId, u32, InteriorLocation, InteriorLocation, Xcm<()>, XcmHash),
Gavin Wood's avatar
Gavin Wood committed
	) -> Result<XcmHash, SendError> {
		EXPORTER_OVERRIDE.with(|e| {
			if let Some((_, ref f)) = &*e.borrow() {
				let (network, channel, uni_src, dest, msg, _hash) = tuple;
				f(network, channel, uni_src, dest, msg)
			} else {
				let hash = tuple.5;
				EXPORTED_XCM.with(|q| q.borrow_mut().push(tuple));
				Ok(hash)
			}
		})
	}
}

thread_local! {
Francisco Aguirre's avatar
Francisco Aguirre committed
	pub static ASSETS: RefCell<BTreeMap<Location, AssetsInHolding>> = RefCell::new(BTreeMap::new());
Gavin Wood's avatar
Gavin Wood committed
}
Francisco Aguirre's avatar
Francisco Aguirre committed
pub fn assets(who: impl Into<Location>) -> AssetsInHolding {
Gavin Wood's avatar
Gavin Wood committed
	ASSETS.with(|a| a.borrow().get(&who.into()).cloned()).unwrap_or_default()
}
Francisco Aguirre's avatar
Francisco Aguirre committed
pub fn asset_list(who: impl Into<Location>) -> Vec<Asset> {
	Assets::from(assets(who)).into_inner()
Gavin Wood's avatar
Gavin Wood committed
}
Francisco Aguirre's avatar
Francisco Aguirre committed
pub fn add_asset(who: impl Into<Location>, what: impl Into<Asset>) {
	ASSETS.with(|a| {
		a.borrow_mut()
			.entry(who.into())
			.or_insert(AssetsInHolding::new())
			.subsume(what.into())
	});
Gavin Wood's avatar
Gavin Wood committed
}
Francisco Aguirre's avatar
Francisco Aguirre committed
pub fn clear_assets(who: impl Into<Location>) {
	ASSETS.with(|a| a.borrow_mut().remove(&who.into()));
}
Gavin Wood's avatar
Gavin Wood committed

pub struct TestAssetTransactor;
impl TransactAsset for TestAssetTransactor {
	fn deposit_asset(
Francisco Aguirre's avatar
Francisco Aguirre committed
		what: &Asset,
		who: &Location,
		_context: Option<&XcmContext>,
Gavin Wood's avatar
Gavin Wood committed
	) -> Result<(), XcmError> {
Francisco Aguirre's avatar
Francisco Aguirre committed
		add_asset(who.clone(), what.clone());
Gavin Wood's avatar
Gavin Wood committed
		Ok(())
	}

	fn withdraw_asset(
Francisco Aguirre's avatar
Francisco Aguirre committed
		what: &Asset,
		who: &Location,
Gavin Wood's avatar
Gavin Wood committed
		_maybe_context: Option<&XcmContext>,
Francisco Aguirre's avatar
Francisco Aguirre committed
	) -> Result<AssetsInHolding, XcmError> {
Gavin Wood's avatar
Gavin Wood committed
		ASSETS.with(|a| {
			a.borrow_mut()
				.get_mut(who)
				.ok_or(XcmError::NotWithdrawable)?
				.try_take(what.clone().into())
				.map_err(|_| XcmError::NotWithdrawable)
		})
	}
}

Francisco Aguirre's avatar
Francisco Aguirre committed
pub fn to_account(l: impl Into<Location>) -> Result<u64, Location> {
	let l = l.into();
	Ok(match l.unpack() {
Gavin Wood's avatar
Gavin Wood committed
		// Siblings at 2000+id
Francisco Aguirre's avatar
Francisco Aguirre committed
		(1, [Parachain(id)]) => 2000 + *id as u64,
Gavin Wood's avatar
Gavin Wood committed
		// Accounts are their number
Francisco Aguirre's avatar
Francisco Aguirre committed
		(0, [AccountIndex64 { index, .. }]) => *index,
Gavin Wood's avatar
Gavin Wood committed
		// Children at 1000+id
Francisco Aguirre's avatar
Francisco Aguirre committed
		(0, [Parachain(id)]) => 1000 + *id as u64,
Gavin Wood's avatar
Gavin Wood committed
		// Self at 3000
Francisco Aguirre's avatar
Francisco Aguirre committed
		(0, []) => 3000,
Gavin Wood's avatar
Gavin Wood committed
		// Parent at 3001
Francisco Aguirre's avatar
Francisco Aguirre committed
		(1, []) => 3001,
		_ => {
Gavin Wood's avatar
Gavin Wood committed
			// Is it a foreign-consensus?
			let uni = ExecutorUniversalLocation::get();
			if l.parents as usize != uni.len() {
				return Err(l)
			}
			match l.first_interior() {
				Some(GlobalConsensus(Kusama)) => 4000,
				Some(GlobalConsensus(Polkadot)) => 4001,
				_ => return Err(l),
			}
		},
	})
}

pub struct TestOriginConverter;
impl ConvertOrigin<TestOrigin> for TestOriginConverter {
	fn convert_origin(
Francisco Aguirre's avatar
Francisco Aguirre committed
		origin: impl Into<Location>,
Gavin Wood's avatar
Gavin Wood committed
		kind: OriginKind,
Francisco Aguirre's avatar
Francisco Aguirre committed
	) -> Result<TestOrigin, Location> {
Gavin Wood's avatar
Gavin Wood committed
		use OriginKind::*;
Francisco Aguirre's avatar
Francisco Aguirre committed
		let origin = origin.into();
		match (kind, origin.unpack()) {
Gavin Wood's avatar
Gavin Wood committed
			(Superuser, _) => Ok(TestOrigin::Root),
Francisco Aguirre's avatar
Francisco Aguirre committed
			(SovereignAccount, _) => Ok(TestOrigin::Signed(to_account(origin)?)),
			(Native, (0, [Parachain(id)])) => Ok(TestOrigin::Parachain(*id)),
			(Native, (1, [])) => Ok(TestOrigin::Relay),
			(Native, (0, [AccountIndex64 { index, .. }])) => Ok(TestOrigin::Signed(*index)),
			_ => Err(origin),
Gavin Wood's avatar
Gavin Wood committed
		}
	}
}

thread_local! {
Francisco Aguirre's avatar
Francisco Aguirre committed
	pub static IS_RESERVE: RefCell<BTreeMap<Location, Vec<AssetFilter>>> = RefCell::new(BTreeMap::new());
	pub static IS_TELEPORTER: RefCell<BTreeMap<Location, Vec<AssetFilter>>> = RefCell::new(BTreeMap::new());
	pub static UNIVERSAL_ALIASES: RefCell<BTreeSet<(Location, Junction)>> = RefCell::new(BTreeSet::new());
Gavin Wood's avatar
Gavin Wood committed
}
Francisco Aguirre's avatar
Francisco Aguirre committed
pub fn add_reserve(from: Location, asset: AssetFilter) {
Gavin Wood's avatar
Gavin Wood committed
	IS_RESERVE.with(|r| r.borrow_mut().entry(from).or_default().push(asset));
}
#[allow(dead_code)]
Francisco Aguirre's avatar
Francisco Aguirre committed
pub fn add_teleporter(from: Location, asset: AssetFilter) {
Gavin Wood's avatar
Gavin Wood committed
	IS_TELEPORTER.with(|r| r.borrow_mut().entry(from).or_default().push(asset));
}
Francisco Aguirre's avatar
Francisco Aguirre committed
pub fn add_universal_alias(bridge: impl Into<Location>, consensus: impl Into<Junction>) {
Gavin Wood's avatar
Gavin Wood committed
	UNIVERSAL_ALIASES.with(|r| r.borrow_mut().insert((bridge.into(), consensus.into())));
}
pub fn clear_universal_aliases() {
	UNIVERSAL_ALIASES.with(|r| r.replace(Default::default()));
}

pub struct TestIsReserve;
Francisco Aguirre's avatar
Francisco Aguirre committed
impl ContainsPair<Asset, Location> for TestIsReserve {
	fn contains(asset: &Asset, origin: &Location) -> bool {
Gavin Wood's avatar
Gavin Wood committed
		IS_RESERVE
			.with(|r| r.borrow().get(origin).map_or(false, |v| v.iter().any(|a| a.matches(asset))))
	}
}
pub struct TestIsTeleporter;
Francisco Aguirre's avatar
Francisco Aguirre committed
impl ContainsPair<Asset, Location> for TestIsTeleporter {
	fn contains(asset: &Asset, origin: &Location) -> bool {
Gavin Wood's avatar
Gavin Wood committed
		IS_TELEPORTER
			.with(|r| r.borrow().get(origin).map_or(false, |v| v.iter().any(|a| a.matches(asset))))
	}
}

pub struct TestUniversalAliases;
Francisco Aguirre's avatar
Francisco Aguirre committed
impl Contains<(Location, Junction)> for TestUniversalAliases {
	fn contains(t: &(Location, Junction)) -> bool {
Gavin Wood's avatar
Gavin Wood committed
		UNIVERSAL_ALIASES.with(|r| r.borrow().contains(t))
	}
}

pub enum ResponseSlot {
Francisco Aguirre's avatar
Francisco Aguirre committed
	Expecting(Location),
Gavin Wood's avatar
Gavin Wood committed
	Received(Response),
}
thread_local! {
	pub static QUERIES: RefCell<BTreeMap<u64, ResponseSlot>> = RefCell::new(BTreeMap::new());
}
pub struct TestResponseHandler;
impl OnResponse for TestResponseHandler {
Francisco Aguirre's avatar
Francisco Aguirre committed
	fn expecting_response(origin: &Location, query_id: u64, _querier: Option<&Location>) -> bool {
Gavin Wood's avatar
Gavin Wood committed
		QUERIES.with(|q| match q.borrow().get(&query_id) {
			Some(ResponseSlot::Expecting(ref l)) => l == origin,
			_ => false,
		})
	}
	fn on_response(
Francisco Aguirre's avatar
Francisco Aguirre committed
		_origin: &Location,
Gavin Wood's avatar
Gavin Wood committed
		query_id: u64,
Francisco Aguirre's avatar
Francisco Aguirre committed
		_querier: Option<&Location>,
Gavin Wood's avatar
Gavin Wood committed
		response: xcm::latest::Response,
		_max_weight: Weight,
		_context: &XcmContext,
	) -> Weight {
		QUERIES.with(|q| {
			q.borrow_mut().entry(query_id).and_modify(|v| {
				if matches!(*v, ResponseSlot::Expecting(..)) {
					*v = ResponseSlot::Received(response);
				}
			});
		});
		Weight::from_parts(10, 10)
	}
}
Francisco Aguirre's avatar
Francisco Aguirre committed
pub fn expect_response(query_id: u64, from: Location) {
Gavin Wood's avatar
Gavin Wood committed
	QUERIES.with(|q| q.borrow_mut().insert(query_id, ResponseSlot::Expecting(from)));
}
pub fn response(query_id: u64) -> Option<Response> {
	QUERIES.with(|q| {
		q.borrow().get(&query_id).and_then(|v| match v {
			ResponseSlot::Received(r) => Some(r.clone()),
			_ => None,
		})
	})
}

/// Mock implementation of the [`QueryHandler`] trait for creating XCM success queries and expecting
/// responses.
pub struct TestQueryHandler<T, BlockNumber>(core::marker::PhantomData<(T, BlockNumber)>);
impl<T: Config, BlockNumber: sp_runtime::traits::Zero + Encode> QueryHandler
	for TestQueryHandler<T, BlockNumber>
{
	type QueryId = u64;
	type BlockNumber = BlockNumber;
	type Error = XcmError;
	type UniversalLocation = T::UniversalLocation;

	fn new_query(
Francisco Aguirre's avatar
Francisco Aguirre committed
		responder: impl Into<Location>,
		_timeout: Self::BlockNumber,
Francisco Aguirre's avatar
Francisco Aguirre committed
		_match_querier: impl Into<Location>,
	) -> Self::QueryId {
		let query_id = 1;
		expect_response(query_id, responder.into());
		query_id
	}

	fn report_outcome(
		message: &mut Xcm<()>,
Francisco Aguirre's avatar
Francisco Aguirre committed
		responder: impl Into<Location>,
		timeout: Self::BlockNumber,
	) -> Result<Self::QueryId, Self::Error> {
		let responder = responder.into();
		let destination = Self::UniversalLocation::get()
			.invert_target(&responder)
			.map_err(|()| XcmError::LocationNotInvertible)?;
		let query_id = Self::new_query(responder, timeout, Here);
		let response_info = QueryResponseInfo { destination, query_id, max_weight: Weight::zero() };
		let report_error = Xcm(vec![ReportError(response_info)]);
		message.0.insert(0, SetAppendix(report_error));
		Ok(query_id)
	}

	fn take_response(query_id: Self::QueryId) -> QueryResponseStatus<Self::BlockNumber> {
		QUERIES
			.with(|q| {
				q.borrow().get(&query_id).and_then(|v| match v {
					ResponseSlot::Received(r) => Some(QueryResponseStatus::Ready {
						response: r.clone(),
						at: Self::BlockNumber::zero(),
					}),
					_ => Some(QueryResponseStatus::NotFound),
				})
			})
			.unwrap_or(QueryResponseStatus::NotFound)
	}

	#[cfg(feature = "runtime-benchmarks")]
	fn expect_response(_id: Self::QueryId, _response: xcm::latest::Response) {
		// Unnecessary since it's only a test implementation
	}
}

Gavin Wood's avatar
Gavin Wood committed
parameter_types! {
Francisco Aguirre's avatar
Francisco Aguirre committed
	pub static ExecutorUniversalLocation: InteriorLocation
Gavin Wood's avatar
Gavin Wood committed
		= (ByGenesis([0; 32]), Parachain(42)).into();
	pub UnitWeightCost: Weight = Weight::from_parts(10, 10);
}
parameter_types! {
	// Nothing is allowed to be paid/unpaid by default.
Francisco Aguirre's avatar
Francisco Aguirre committed
	pub static AllowExplicitUnpaidFrom: Vec<Location> = vec![];
	pub static AllowUnpaidFrom: Vec<Location> = vec![];
	pub static AllowPaidFrom: Vec<Location> = vec![];
	pub static AllowSubsFrom: Vec<Location> = vec![];
Gavin Wood's avatar
Gavin Wood committed
	// 1_000_000_000_000 => 1 unit of asset for 1 unit of ref time weight.
	// 1024 * 1024 => 1 unit of asset for 1 unit of proof size weight.
	pub static WeightPrice: (AssetId, u128, u128) =
		(From::from(Here), 1_000_000_000_000, 1024 * 1024);
	pub static MaxInstructions: u32 = 100;
}

pub struct TestSuspender;
impl CheckSuspension for TestSuspender {
	fn is_suspended<Call>(
Francisco Aguirre's avatar
Francisco Aguirre committed
		_origin: &Location,
		_instructions: &mut [Instruction<Call>],
		_max_weight: Weight,
		_properties: &mut Properties,
	) -> bool {
		SUSPENDED.with(|s| s.get())
	}
}

impl TestSuspender {
	pub fn set_suspended(suspended: bool) {
		SUSPENDED.with(|s| s.set(suspended));
	}
}

Gavin Wood's avatar
Gavin Wood committed
pub type TestBarrier = (
	TakeWeightCredit,
	AllowKnownQueryResponses<TestResponseHandler>,
	AllowTopLevelPaidExecutionFrom<IsInVec<AllowPaidFrom>>,
	AllowExplicitUnpaidExecutionFrom<IsInVec<AllowExplicitUnpaidFrom>>,
	AllowUnpaidExecutionFrom<IsInVec<AllowUnpaidFrom>>,
	AllowSubscriptionsFrom<IsInVec<AllowSubsFrom>>,
);

thread_local! {
	pub static IS_WAIVED: RefCell<Vec<FeeReason>> = RefCell::new(vec![]);
}
#[allow(dead_code)]
pub fn set_fee_waiver(waived: Vec<FeeReason>) {
	IS_WAIVED.with(|l| l.replace(waived));
}

pub struct TestFeeManager;
impl FeeManager for TestFeeManager {
Francisco Aguirre's avatar
Francisco Aguirre committed
	fn is_waived(_: Option<&Location>, r: FeeReason) -> bool {
Gavin Wood's avatar
Gavin Wood committed
		IS_WAIVED.with(|l| l.borrow().contains(&r))
	}
Francisco Aguirre's avatar
Francisco Aguirre committed
	fn handle_fee(_: Assets, _: Option<&XcmContext>, _: FeeReason) {}
Gavin Wood's avatar
Gavin Wood committed
}

#[derive(Clone, Eq, PartialEq, Debug)]
pub enum LockTraceItem {
Francisco Aguirre's avatar
Francisco Aguirre committed
	Lock { unlocker: Location, asset: Asset, owner: Location },
	Unlock { unlocker: Location, asset: Asset, owner: Location },
	Note { locker: Location, asset: Asset, owner: Location },
	Reduce { locker: Location, asset: Asset, owner: Location },
Gavin Wood's avatar
Gavin Wood committed
}
thread_local! {
	pub static NEXT_INDEX: RefCell<u32> = RefCell::new(0);
	pub static LOCK_TRACE: RefCell<Vec<LockTraceItem>> = RefCell::new(Vec::new());
Francisco Aguirre's avatar
Francisco Aguirre committed
	pub static ALLOWED_UNLOCKS: RefCell<BTreeMap<(Location, Location), AssetsInHolding>> = RefCell::new(BTreeMap::new());
	pub static ALLOWED_REQUEST_UNLOCKS: RefCell<BTreeMap<(Location, Location), AssetsInHolding>> = RefCell::new(BTreeMap::new());
Gavin Wood's avatar
Gavin Wood committed
}

pub fn take_lock_trace() -> Vec<LockTraceItem> {
	LOCK_TRACE.with(|l| l.replace(Vec::new()))
}
pub fn allow_unlock(
Francisco Aguirre's avatar
Francisco Aguirre committed
	unlocker: impl Into<Location>,
	asset: impl Into<Asset>,
	owner: impl Into<Location>,
Gavin Wood's avatar
Gavin Wood committed
) {
	ALLOWED_UNLOCKS.with(|l| {
		l.borrow_mut()
			.entry((owner.into(), unlocker.into()))
			.or_default()
			.subsume(asset.into())
	});
}
pub fn disallow_unlock(
Francisco Aguirre's avatar
Francisco Aguirre committed
	unlocker: impl Into<Location>,
	asset: impl Into<Asset>,
	owner: impl Into<Location>,
Gavin Wood's avatar
Gavin Wood committed
) {
	ALLOWED_UNLOCKS.with(|l| {
		l.borrow_mut()
			.entry((owner.into(), unlocker.into()))
			.or_default()
			.saturating_take(asset.into().into())
	});
}
Francisco Aguirre's avatar
Francisco Aguirre committed
pub fn unlock_allowed(unlocker: &Location, asset: &Asset, owner: &Location) -> bool {
Gavin Wood's avatar
Gavin Wood committed
	ALLOWED_UNLOCKS.with(|l| {
		l.borrow_mut()
Francisco Aguirre's avatar
Francisco Aguirre committed
			.get(&(owner.clone(), unlocker.clone()))
Gavin Wood's avatar
Gavin Wood committed
			.map_or(false, |x| x.contains_asset(asset))
	})
}
pub fn allow_request_unlock(
Francisco Aguirre's avatar
Francisco Aguirre committed
	locker: impl Into<Location>,
	asset: impl Into<Asset>,
	owner: impl Into<Location>,
Gavin Wood's avatar
Gavin Wood committed
) {
	ALLOWED_REQUEST_UNLOCKS.with(|l| {
		l.borrow_mut()
			.entry((owner.into(), locker.into()))
			.or_default()
			.subsume(asset.into())
	});
}
pub fn disallow_request_unlock(
Francisco Aguirre's avatar
Francisco Aguirre committed
	locker: impl Into<Location>,
	asset: impl Into<Asset>,
	owner: impl Into<Location>,
Gavin Wood's avatar
Gavin Wood committed
) {
	ALLOWED_REQUEST_UNLOCKS.with(|l| {
		l.borrow_mut()
			.entry((owner.into(), locker.into()))
			.or_default()
			.saturating_take(asset.into().into())
	});
}
Francisco Aguirre's avatar
Francisco Aguirre committed
pub fn request_unlock_allowed(locker: &Location, asset: &Asset, owner: &Location) -> bool {
Gavin Wood's avatar
Gavin Wood committed
	ALLOWED_REQUEST_UNLOCKS.with(|l| {
		l.borrow_mut()
Francisco Aguirre's avatar
Francisco Aguirre committed
			.get(&(owner.clone(), locker.clone()))
Gavin Wood's avatar
Gavin Wood committed
			.map_or(false, |x| x.contains_asset(asset))
	})
}

pub struct TestTicket(LockTraceItem);
impl Enact for TestTicket {
	fn enact(self) -> Result<(), LockError> {
		match &self.0 {
			LockTraceItem::Lock { unlocker, asset, owner } =>
Francisco Aguirre's avatar
Francisco Aguirre committed
				allow_unlock(unlocker.clone(), asset.clone(), owner.clone()),
Gavin Wood's avatar
Gavin Wood committed
			LockTraceItem::Unlock { unlocker, asset, owner } =>
Francisco Aguirre's avatar
Francisco Aguirre committed
				disallow_unlock(unlocker.clone(), asset.clone(), owner.clone()),
Gavin Wood's avatar
Gavin Wood committed
			LockTraceItem::Reduce { locker, asset, owner } =>
Francisco Aguirre's avatar
Francisco Aguirre committed
				disallow_request_unlock(locker.clone(), asset.clone(), owner.clone()),
Gavin Wood's avatar
Gavin Wood committed
			_ => {},
		}
		LOCK_TRACE.with(move |l| l.borrow_mut().push(self.0));
		Ok(())
	}
}

pub struct TestAssetLock;
impl AssetLock for TestAssetLock {
	type LockTicket = TestTicket;
	type UnlockTicket = TestTicket;
	type ReduceTicket = TestTicket;

	fn prepare_lock(
Francisco Aguirre's avatar
Francisco Aguirre committed
		unlocker: Location,
		asset: Asset,
		owner: Location,
Gavin Wood's avatar
Gavin Wood committed
	) -> Result<Self::LockTicket, LockError> {
Francisco Aguirre's avatar
Francisco Aguirre committed
		ensure!(assets(owner.clone()).contains_asset(&asset), LockError::AssetNotOwned);
Gavin Wood's avatar
Gavin Wood committed
		Ok(TestTicket(LockTraceItem::Lock { unlocker, asset, owner }))
	}

	fn prepare_unlock(
Francisco Aguirre's avatar
Francisco Aguirre committed
		unlocker: Location,
		asset: Asset,
		owner: Location,
Gavin Wood's avatar
Gavin Wood committed
	) -> Result<Self::UnlockTicket, LockError> {
		ensure!(unlock_allowed(&unlocker, &asset, &owner), LockError::NotLocked);
		Ok(TestTicket(LockTraceItem::Unlock { unlocker, asset, owner }))
	}

Francisco Aguirre's avatar
Francisco Aguirre committed
	fn note_unlockable(locker: Location, asset: Asset, owner: Location) -> Result<(), LockError> {
		allow_request_unlock(locker.clone(), asset.clone(), owner.clone());
Gavin Wood's avatar
Gavin Wood committed
		let item = LockTraceItem::Note { locker, asset, owner };
		LOCK_TRACE.with(move |l| l.borrow_mut().push(item));
		Ok(())
	}

	fn prepare_reduce_unlockable(
Francisco Aguirre's avatar
Francisco Aguirre committed
		locker: Location,
		asset: Asset,
		owner: Location,
Gavin Wood's avatar
Gavin Wood committed
	) -> Result<Self::ReduceTicket, xcm_executor::traits::LockError> {
		ensure!(request_unlock_allowed(&locker, &asset, &owner), LockError::NotLocked);
		Ok(TestTicket(LockTraceItem::Reduce { locker, asset, owner }))
	}
}

thread_local! {
Francisco Aguirre's avatar
Francisco Aguirre committed
	pub static EXCHANGE_ASSETS: RefCell<AssetsInHolding> = RefCell::new(AssetsInHolding::new());
Gavin Wood's avatar
Gavin Wood committed
}
Francisco Aguirre's avatar
Francisco Aguirre committed
pub fn set_exchange_assets(assets: impl Into<Assets>) {
Gavin Wood's avatar
Gavin Wood committed
	EXCHANGE_ASSETS.with(|a| a.replace(assets.into().into()));
}
Francisco Aguirre's avatar
Francisco Aguirre committed
pub fn exchange_assets() -> Assets {
Gavin Wood's avatar
Gavin Wood committed
	EXCHANGE_ASSETS.with(|a| a.borrow().clone().into())
}
pub struct TestAssetExchange;
impl AssetExchange for TestAssetExchange {
	fn exchange_asset(
Francisco Aguirre's avatar
Francisco Aguirre committed
		_origin: Option<&Location>,
		give: AssetsInHolding,
		want: &Assets,
Gavin Wood's avatar
Gavin Wood committed
		maximal: bool,
Francisco Aguirre's avatar
Francisco Aguirre committed
	) -> Result<AssetsInHolding, AssetsInHolding> {
Gavin Wood's avatar
Gavin Wood committed
		let mut have = EXCHANGE_ASSETS.with(|l| l.borrow().clone());
		ensure!(have.contains_assets(want), give);
		let get = if maximal {
Francisco Aguirre's avatar
Francisco Aguirre committed
			std::mem::replace(&mut have, AssetsInHolding::new())
Gavin Wood's avatar
Gavin Wood committed
		} else {
			have.saturating_take(want.clone().into())
		};
		have.subsume_assets(give);
		EXCHANGE_ASSETS.with(|l| l.replace(have));
		Ok(get)
	}
}

Francisco Aguirre's avatar
Francisco Aguirre committed
pub struct SiblingPrefix;
impl Contains<Location> for SiblingPrefix {
	fn contains(loc: &Location) -> bool {
		matches!(loc.unpack(), (1, [Parachain(_)]))
	}
}

pub struct ChildPrefix;
impl Contains<Location> for ChildPrefix {
	fn contains(loc: &Location) -> bool {
		matches!(loc.unpack(), (0, [Parachain(_)]))
	}
}

pub struct ParentPrefix;
impl Contains<Location> for ParentPrefix {
	fn contains(loc: &Location) -> bool {
		matches!(loc.unpack(), (1, []))
	}
Gavin Wood's avatar
Gavin Wood committed
pub struct TestConfig;
impl Config for TestConfig {
	type RuntimeCall = TestCall;
	type XcmSender = TestMessageSender;
	type AssetTransactor = TestAssetTransactor;
	type OriginConverter = TestOriginConverter;
	type IsReserve = TestIsReserve;
	type IsTeleporter = TestIsTeleporter;
	type UniversalLocation = ExecutorUniversalLocation;
	type Barrier = TrailingSetTopicAsId<RespectSuspension<TestBarrier, TestSuspender>>;
Gavin Wood's avatar
Gavin Wood committed
	type Weigher = FixedWeightBounds<UnitWeightCost, TestCall, MaxInstructions>;
	type Trader = FixedRateOfFungible<WeightPrice, ()>;
	type ResponseHandler = TestResponseHandler;
	type AssetTrap = TestAssetTrap;
	type AssetLocker = TestAssetLock;
	type AssetExchanger = TestAssetExchange;
	type AssetClaims = TestAssetTrap;
	type SubscriptionService = TestSubscriptionService;
	type PalletInstancesInfo = TestPalletsInfo;
	type MaxAssetsIntoHolding = MaxAssetsIntoHolding;
	type FeeManager = TestFeeManager;
	type UniversalAliases = TestUniversalAliases;
	type MessageExporter = TestMessageExporter;
	type CallDispatcher = TestCall;
	type SafeCallFilter = Everything;
	type Aliasers = AliasForeignAccountId32<SiblingPrefix>;
Gavin Wood's avatar
Gavin Wood committed
}

Francisco Aguirre's avatar
Francisco Aguirre committed
pub fn fungible_multi_asset(location: Location, amount: u128) -> Asset {
Gavin Wood's avatar
Gavin Wood committed
	(AssetId::from(location), Fungibility::Fungible(amount)).into()
}

pub fn fake_message_hash<T>(message: &Xcm<T>) -> XcmHash {
	message.using_encoded(sp_io::hashing::blake2_256)
}