git.delta.rocks / unique-network / refs/commits / 4e84fdd5539f

difftreelog

refactor xcm transact forbidden test

Daniel Shiposha2023-03-27parent: #48ebb54.patch.diff
in: master

17 files changed

modifiedruntime/common/tests/mod.rsdiffbeforeafterboth
--- a/runtime/common/tests/mod.rs
+++ b/runtime/common/tests/mod.rs
@@ -14,22 +14,49 @@
 // You should have received a copy of the GNU General Public License
 // along with Unique Network. If not, see <http://www.gnu.org/licenses/>.
 
-use sp_runtime::BuildStorage;
+use sp_runtime::{BuildStorage, Storage};
 use sp_core::{Public, Pair};
 use sp_std::vec;
 use up_common::types::AuraId;
-use crate::{GenesisConfig, ParachainInfoConfig};
+use crate::{Runtime, GenesisConfig, ParachainInfoConfig, RuntimeEvent, System};
+
+pub use sp_runtime::AccountId32 as AccountId;
+pub type Balance = u128;
 
 pub mod xcm;
 
+#[cfg(any(feature = "opal-runtime", feature = "quartz-runtime"))]
+/// PARA_ID for Opal/Sapphire/Quartz
+const PARA_ID: u32 = 2095;
+
+#[cfg(feature = "unique-runtime")]
+/// PARA_ID for Unique
+const PARA_ID: u32 = 2037;
+
 fn get_from_seed<TPublic: Public>(seed: &str) -> <TPublic::Pair as Pair>::Public {
 	TPublic::Pair::from_string(&format!("//{}", seed), None)
 		.expect("static values are valid; qed")
 		.public()
 }
 
+fn last_events(n: usize) -> Vec<RuntimeEvent> {
+	System::events().into_iter().map(|e| e.event).rev().take(n).rev().collect()
+}
+
+fn new_test_ext(balances: Vec<(AccountId, Balance)>) -> sp_io::TestExternalities {
+	let mut storage = make_basic_storage();
+
+	pallet_balances::GenesisConfig::<Runtime> { balances }
+		.assimilate_storage(&mut storage)
+		.unwrap();
+
+		let mut ext = sp_io::TestExternalities::new(storage);
+		ext.execute_with(|| System::set_block_number(1));
+		ext
+}
+
 #[cfg(feature = "collator-selection")]
-fn new_test_ext(para_id: u32) -> sp_io::TestExternalities {
+fn make_basic_storage() -> Storage {
 	use sp_core::{sr25519};
 	use sp_runtime::traits::{IdentifyAccount, Verify};
 	use crate::{AccountId, Signature, SessionKeys, CollatorSelectionConfig, SessionConfig};
@@ -66,7 +93,7 @@
 		collator_selection: CollatorSelectionConfig { invulnerables },
 		session: SessionConfig { keys },
 		parachain_info: ParachainInfoConfig {
-			parachain_id: para_id.into(),
+			parachain_id: PARA_ID.into(),
 		},
 		..GenesisConfig::default()
 	};
@@ -75,7 +102,7 @@
 }
 
 #[cfg(not(feature = "collator-selection"))]
-fn new_test_ext(para_id: u32) -> sp_io::TestExternalities {
+fn make_basic_storage() -> Storage {
 	use crate::AuraConfig;
 
 	let cfg = GenesisConfig {
@@ -86,7 +113,7 @@
 			],
 		},
 		parachain_info: ParachainInfoConfig {
-			parachain_id: para_id.into(),
+			parachain_id: PARA_ID.into(),
 		},
 		..GenesisConfig::default()
 	};
modifiedruntime/common/tests/xcm.rsdiffbeforeafterboth
before · runtime/common/tests/xcm.rs
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;22use frame_support::pallet_prelude::Weight;2324fn catch_xcm_barrier_log(logger: &mut Logger, expected_msg: &str) -> Result<(), String> {25	for record in logger {26		if record.target() == "xcm::barrier" && record.args() == expected_msg {27			return Ok(());28		}29	}3031	Err(format!(32		"the expected XCM barrier log `{}` is not found",33		expected_msg34	))35}3637/// WARNING: Uses log capturing38/// See https://docs.rs/logtest/latest/logtest/index.html#constraints39pub fn barrier_denies_transact<B: ShouldExecute>(logger: &mut Logger) {40	let location = MultiLocation {41		parents: 0,42		interior: Junctions::Here,43	};4445	// We will never decode this "call",46	// so it is irrelevant what we are passing to the `transact` cmd.47	let fake_encoded_call = vec![0u8];4849	let transact_inst: Instruction<RuntimeCall> = Transact {50		origin_kind: OriginKind::Superuser,51		require_weight_at_most: Weight::default(),52		call: fake_encoded_call.into(),53	};5455	let mut xcm_program = vec![transact_inst];5657	let max_weight = Weight::from_parts(100_000, 100_000);58	let mut weight_credit = Weight::from_parts(100_000_000, 100_000_000);5960	let result = B::should_execute(&location, &mut xcm_program, max_weight, &mut weight_credit);6162	assert!(63		result.is_err(),64		"the barrier should disallow the XCM transact cmd"65	);6667	catch_xcm_barrier_log(logger, "transact XCM rejected").unwrap();68}6970fn xcm_execute<B: ShouldExecute>(71	self_para_id: u32,72	location: &MultiLocation,73	xcm: &mut [Instruction<RuntimeCall>],74) -> Result<(), ()> {75	new_test_ext(self_para_id).execute_with(|| {76		let max_weight = Weight::from_parts(100_000, 100_000);77		let mut weight_credit = Weight::from_parts(100_000_000, 100_000_000);7879		B::should_execute(&location, xcm, max_weight, &mut weight_credit)80	})81}8283fn make_multiassets(location: &MultiLocation) -> MultiAssets {84	let id = AssetId::Concrete(location.clone());85	let fun = Fungibility::Fungible(42);86	let multiasset = MultiAsset { id, fun };8788	multiasset.into()89}9091fn make_transfer_reserve_asset(location: &MultiLocation) -> Xcm<RuntimeCall> {92	let assets = make_multiassets(location);93	let inst = TransferReserveAsset {94		assets,95		dest: location.clone(),96		xcm: Xcm(vec![]),97	};9899	Xcm::<RuntimeCall>(vec![inst])100}101102fn make_deposit_reserve_asset(location: &MultiLocation) -> Xcm<RuntimeCall> {103	let assets = make_multiassets(location);104	let inst = DepositReserveAsset {105		assets: assets.into(),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 [Instruction<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.0,152	)?;153154	expect_transfer_location_denied::<B>(155		logger,156		self_para_id,157		&unknown_location,158		&mut deposit_reserve_asset.0,159	)?;160161	Ok(())162}
after · runtime/common/tests/xcm.rs
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::{18	VersionedXcm,19	latest::{prelude::*, Error}20};21use codec::Encode;22use crate::{Runtime, RuntimeCall, RuntimeOrigin, RuntimeEvent, PolkadotXcm};23use super::{new_test_ext, last_events, AccountId};24use frame_support::{25	pallet_prelude::Weight,26};2728const ALICE: AccountId = AccountId::new([0u8; 32]);29const BOB: AccountId = AccountId::new([1u8; 32]);3031const INITIAL_BALANCE: u128 = 1000000000000000000_0000; // 1000 UNQ 3233#[test]34pub fn xcm_transact_is_forbidden() {35	new_test_ext(vec![(ALICE, INITIAL_BALANCE)]).execute_with(|| {36		PolkadotXcm::execute(37			RuntimeOrigin::signed(ALICE),38			Box::new(VersionedXcm::from(Xcm(vec![39				Transact {40					origin_kind: OriginKind::Native,41					require_weight_at_most: Weight::from_parts(1000, 1000),42					call: RuntimeCall::Balances(43						pallet_balances::Call::<Runtime>::transfer {44							dest: BOB.into(),45							value: INITIAL_BALANCE / 2,46						}47					).encode().into(),48				}49			]))),50			Weight::from_parts(1001000, 2000),51		).expect("XCM execute must succeed, the error should be in the `PolkadotXcm::Attempted` event");5253		let xcm_event = &last_events(1)[0];54		match xcm_event {55			RuntimeEvent::PolkadotXcm(56				pallet_xcm::Event::<Runtime>::Attempted(57					Outcome::Incomplete(_weight, Error::NoPermission)58				)59			) => { /* Pass */ },60			_ => panic!(61				"Expected PolkadotXcm.Attempted(Incomplete(_weight, NoPermission)),\62				found: {xcm_event:#?}"63			)64		}65	});66}
modifiedruntime/opal/Cargo.tomldiffbeforeafterboth
--- a/runtime/opal/Cargo.toml
+++ b/runtime/opal/Cargo.toml
@@ -307,8 +307,5 @@
 
 impl-trait-for-tuples = { workspace = true }
 
-[dev-dependencies]
-logtest = { workspace = true }
-
 [build-dependencies]
 substrate-wasm-builder = { workspace = true }
modifiedruntime/opal/src/lib.rsdiffbeforeafterboth
--- a/runtime/opal/src/lib.rs
+++ b/runtime/opal/src/lib.rs
@@ -41,9 +41,6 @@
 
 pub mod xcm_barrier;
 
-#[cfg(test)]
-mod tests;
-
 pub use runtime_common::*;
 
 pub const RUNTIME_NAME: &str = "opal";
deletedruntime/opal/src/tests/logcapture.rsdiffbeforeafterboth
--- a/runtime/opal/src/tests/logcapture.rs
+++ /dev/null
@@ -1,25 +0,0 @@
-// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.
-// This file is part of Unique Network.
-
-// Unique Network 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.
-
-// Unique Network 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 Unique Network. If not, see <http://www.gnu.org/licenses/>.
-
-use logtest::Logger;
-use super::xcm::opal_xcm_tests;
-
-#[test]
-fn opal_log_capture_tests() {
-	let mut logger = Logger::start();
-
-	opal_xcm_tests(&mut logger);
-}
deletedruntime/opal/src/tests/mod.rsdiffbeforeafterboth
--- a/runtime/opal/src/tests/mod.rs
+++ /dev/null
@@ -1,18 +0,0 @@
-// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.
-// This file is part of Unique Network.
-
-// Unique Network 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.
-
-// Unique Network 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 Unique Network. If not, see <http://www.gnu.org/licenses/>.
-
-mod logcapture;
-mod xcm;
deletedruntime/opal/src/tests/xcm.rsdiffbeforeafterboth
--- a/runtime/opal/src/tests/xcm.rs
+++ /dev/null
@@ -1,27 +0,0 @@
-// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.
-// This file is part of Unique Network.
-
-// Unique Network 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.
-
-// Unique Network 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 Unique Network. If not, see <http://www.gnu.org/licenses/>.
-
-use logtest::Logger;
-use crate::{runtime_common::tests::xcm::*, xcm_barrier::Barrier};
-
-const OPAL_PARA_ID: u32 = 2095; // Same as Quartz
-
-pub fn opal_xcm_tests(logger: &mut Logger) {
-	barrier_denies_transact::<Barrier>(logger);
-
-	barrier_denies_transfer_from_unknown_location::<Barrier>(logger, OPAL_PARA_ID)
-		.expect_err("opal runtime allows any location");
-}
modifiedruntime/quartz/Cargo.tomldiffbeforeafterboth
--- a/runtime/quartz/Cargo.toml
+++ b/runtime/quartz/Cargo.toml
@@ -300,8 +300,5 @@
 
 impl-trait-for-tuples = { workspace = true }
 
-[dev-dependencies]
-logtest = { workspace = true }
-
 [build-dependencies]
 substrate-wasm-builder = { workspace = true }
modifiedruntime/quartz/src/lib.rsdiffbeforeafterboth
--- a/runtime/quartz/src/lib.rs
+++ b/runtime/quartz/src/lib.rs
@@ -41,9 +41,6 @@
 
 pub mod xcm_barrier;
 
-#[cfg(test)]
-mod tests;
-
 pub use runtime_common::*;
 
 #[cfg(feature = "become-sapphire")]
deletedruntime/quartz/src/tests/logcapture.rsdiffbeforeafterboth
--- a/runtime/quartz/src/tests/logcapture.rs
+++ /dev/null
@@ -1,25 +0,0 @@
-// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.
-// This file is part of Unique Network.
-
-// Unique Network 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.
-
-// Unique Network 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 Unique Network. If not, see <http://www.gnu.org/licenses/>.
-
-use logtest::Logger;
-use super::xcm::quartz_xcm_tests;
-
-#[test]
-fn quartz_log_capture_tests() {
-	let mut logger = Logger::start();
-
-	quartz_xcm_tests(&mut logger);
-}
deletedruntime/quartz/src/tests/mod.rsdiffbeforeafterboth
--- a/runtime/quartz/src/tests/mod.rs
+++ /dev/null
@@ -1,18 +0,0 @@
-// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.
-// This file is part of Unique Network.
-
-// Unique Network 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.
-
-// Unique Network 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 Unique Network. If not, see <http://www.gnu.org/licenses/>.
-
-mod logcapture;
-mod xcm;
deletedruntime/quartz/src/tests/xcm.rsdiffbeforeafterboth
--- a/runtime/quartz/src/tests/xcm.rs
+++ /dev/null
@@ -1,27 +0,0 @@
-// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.
-// This file is part of Unique Network.
-
-// Unique Network 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.
-
-// Unique Network 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 Unique Network. If not, see <http://www.gnu.org/licenses/>.
-
-use logtest::Logger;
-use crate::{runtime_common::tests::xcm::*, xcm_barrier::Barrier};
-
-const QUARTZ_PARA_ID: u32 = 2095;
-
-pub fn quartz_xcm_tests(logger: &mut Logger) {
-	barrier_denies_transact::<Barrier>(logger);
-
-	barrier_denies_transfer_from_unknown_location::<Barrier>(logger, QUARTZ_PARA_ID)
-		.expect("quartz runtime denies an unknown location");
-}
modifiedruntime/unique/Cargo.tomldiffbeforeafterboth
--- a/runtime/unique/Cargo.toml
+++ b/runtime/unique/Cargo.toml
@@ -298,8 +298,5 @@
 
 impl-trait-for-tuples = { workspace = true }
 
-[dev-dependencies]
-logtest = { workspace = true }
-
 [build-dependencies]
 substrate-wasm-builder = { workspace = true }
modifiedruntime/unique/src/lib.rsdiffbeforeafterboth
--- a/runtime/unique/src/lib.rs
+++ b/runtime/unique/src/lib.rs
@@ -41,9 +41,6 @@
 
 pub mod xcm_barrier;
 
-#[cfg(test)]
-mod tests;
-
 pub use runtime_common::*;
 
 pub const RUNTIME_NAME: &str = "unique";
deletedruntime/unique/src/tests/logcapture.rsdiffbeforeafterboth
--- a/runtime/unique/src/tests/logcapture.rs
+++ /dev/null
@@ -1,25 +0,0 @@
-// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.
-// This file is part of Unique Network.
-
-// Unique Network 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.
-
-// Unique Network 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 Unique Network. If not, see <http://www.gnu.org/licenses/>.
-
-use logtest::Logger;
-use super::xcm::unique_xcm_tests;
-
-#[test]
-fn unique_log_capture_tests() {
-	let mut logger = Logger::start();
-
-	unique_xcm_tests(&mut logger);
-}
deletedruntime/unique/src/tests/mod.rsdiffbeforeafterboth
--- a/runtime/unique/src/tests/mod.rs
+++ /dev/null
@@ -1,18 +0,0 @@
-// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.
-// This file is part of Unique Network.
-
-// Unique Network 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.
-
-// Unique Network 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 Unique Network. If not, see <http://www.gnu.org/licenses/>.
-
-mod logcapture;
-mod xcm;
deletedruntime/unique/src/tests/xcm.rsdiffbeforeafterboth
--- a/runtime/unique/src/tests/xcm.rs
+++ /dev/null
@@ -1,27 +0,0 @@
-// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.
-// This file is part of Unique Network.
-
-// Unique Network 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.
-
-// Unique Network 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 Unique Network. If not, see <http://www.gnu.org/licenses/>.
-
-use logtest::Logger;
-use crate::{runtime_common::tests::xcm::*, xcm_barrier::Barrier};
-
-const UNIQUE_PARA_ID: u32 = 2037;
-
-pub fn unique_xcm_tests(logger: &mut Logger) {
-	barrier_denies_transact::<Barrier>(logger);
-
-	barrier_denies_transfer_from_unknown_location::<Barrier>(logger, UNIQUE_PARA_ID)
-		.expect("unique runtime denies an unknown location");
-}