git.delta.rocks / unique-network / refs/commits / e90158d94962

difftreelog

source

runtime/common/tests/xcm.rs4.3 KiBsourcehistory
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617use xcm_executor::traits::ShouldExecute;18use xcm::latest::prelude::*;19use logtest::Logger;20use crate::RuntimeCall;21use super::new_test_ext;2223fn catch_xcm_barrier_log(logger: &mut Logger, expected_msg: &str) -> Result<(), String> {24	for record in logger {25		if record.target() == "xcm::barrier" && record.args() == expected_msg {26			return Ok(());27		}28	}2930	Err(format!(31		"the expected XCM barrier log `{}` is not found",32		expected_msg33	))34}3536/// WARNING: Uses log capturing37/// See https://docs.rs/logtest/latest/logtest/index.html#constraints38pub fn barrier_denies_transact<B: ShouldExecute>(logger: &mut Logger) {39	let location = MultiLocation {40		parents: 0,41		interior: Junctions::Here,42	};4344	// We will never decode this "call",45	// so it is irrelevant what we are passing to the `transact` cmd.46	let fake_encoded_call = vec![0u8];4748	let transact_inst = Transact {49		origin_type: OriginKind::Superuser,50		require_weight_at_most: 0,51		call: fake_encoded_call.into(),52	};5354	let mut xcm_program = Xcm::<RuntimeCall>(vec![transact_inst]);5556	let max_weight = 100_000;57	let mut weight_credit = 100_000_000;5859	let result = B::should_execute(&location, &mut xcm_program, max_weight, &mut weight_credit);6061	assert!(62		result.is_err(),63		"the barrier should disallow the XCM transact cmd"64	);6566	catch_xcm_barrier_log(logger, "transact XCM rejected").unwrap();67}6869fn xcm_execute<B: ShouldExecute>(70	self_para_id: u32,71	location: &MultiLocation,72	xcm: &mut Xcm<RuntimeCall>,73) -> Result<(), ()> {74	new_test_ext(self_para_id).execute_with(|| {75		let max_weight = 100_000;76		let mut weight_credit = 100_000_000;7778		B::should_execute(&location, xcm, max_weight, &mut weight_credit)79	})80}8182fn make_multiassets(location: &MultiLocation) -> MultiAssets {83	let id = AssetId::Concrete(location.clone());84	let fun = Fungibility::Fungible(42);85	let multiasset = MultiAsset { id, fun };8687	multiasset.into()88}8990fn make_transfer_reserve_asset(location: &MultiLocation) -> Xcm<RuntimeCall> {91	let assets = make_multiassets(location);92	let inst = TransferReserveAsset {93		assets,94		dest: location.clone(),95		xcm: Xcm(vec![]),96	};9798	Xcm::<RuntimeCall>(vec![inst])99}100101fn make_deposit_reserve_asset(location: &MultiLocation) -> Xcm<RuntimeCall> {102	let assets = make_multiassets(location);103	let inst = DepositReserveAsset {104		assets: assets.into(),105		max_assets: 42,106		dest: location.clone(),107		xcm: Xcm(vec![]),108	};109110	Xcm::<RuntimeCall>(vec![inst])111}112113fn expect_transfer_location_denied<B: ShouldExecute>(114	logger: &mut Logger,115	self_para_id: u32,116	location: &MultiLocation,117	xcm: &mut Xcm<RuntimeCall>,118) -> Result<(), String> {119	let result = xcm_execute::<B>(self_para_id, location, xcm);120121	if result.is_ok() {122		return Err("the barrier should deny the unknown location".into());123	}124125	catch_xcm_barrier_log(logger, "Unexpected deposit or transfer location")126}127128/// WARNING: Uses log capturing129/// See https://docs.rs/logtest/latest/logtest/index.html#constraints130pub fn barrier_denies_transfer_from_unknown_location<B>(131	logger: &mut Logger,132	self_para_id: u32,133) -> Result<(), String>134where135	B: ShouldExecute,136{137	const UNKNOWN_PARACHAIN_ID: u32 = 4057;138139	let unknown_location = MultiLocation {140		parents: 1,141		interior: X1(Parachain(UNKNOWN_PARACHAIN_ID)),142	};143144	let mut transfer_reserve_asset = make_transfer_reserve_asset(&unknown_location);145	let mut deposit_reserve_asset = make_deposit_reserve_asset(&unknown_location);146147	expect_transfer_location_denied::<B>(148		logger,149		self_para_id,150		&unknown_location,151		&mut transfer_reserve_asset,152	)?;153154	expect_transfer_location_denied::<B>(155		logger,156		self_para_id,157		&unknown_location,158		&mut deposit_reserve_asset,159	)?;160161	Ok(())162}