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

difftreelog

test(xcm) add transact barrier test

Daniel Shiposha2022-08-29parent: #8c9b07c.patch.diff
in: master

6 files changed

modifiedruntime/common/mod.rsdiffbeforeafterboth
--- a/runtime/common/mod.rs
+++ b/runtime/common/mod.rs
@@ -24,6 +24,9 @@
 pub mod sponsoring;
 pub mod weights;
 
+#[cfg(test)]
+mod tests;
+
 use sp_core::H160;
 use frame_support::traits::{Currency, OnUnbalanced, Imbalance};
 use sp_runtime::{
addedruntime/common/tests/mod.rsdiffbeforeafterboth
--- /dev/null
+++ b/runtime/common/tests/mod.rs
@@ -0,0 +1,17 @@
+// 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 xcm;
addedruntime/common/tests/xcm.rsdiffbeforeafterboth
--- /dev/null
+++ b/runtime/common/tests/xcm.rs
@@ -0,0 +1,86 @@
+// 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 xcm_executor::traits::ShouldExecute;
+use xcm::latest::prelude::*;
+use logtest::Logger;
+use crate::{
+    Call,
+    xcm_config::Barrier,
+};
+
+fn catch_xcm_barrier_log(logger: &mut Logger, expected_msg: &str) {
+    for record in logger {
+        if record.target() == "xcm::barrier"
+        && record.args() == expected_msg {
+            return;
+        }
+    }
+
+    panic!("the expected XCM barrier log `{}` is not found", expected_msg);
+}
+
+#[test]
+fn xcm_barrier_does_not_allow_transact() {
+    // We have a `AllowTopLevelPaidExecutionFrom` barrier,
+    // so an XCM program should start from one of the following commands: 
+    // * `WithdrawAsset`
+    // * `ReceiveTeleportedAsset`
+    // * `ReserveAssetDeposited`
+    // * `ClaimAsset` 
+    // We use the `WithdrawAsset` in this test
+
+    let location = MultiLocation {
+        parents: 0,
+        interior: Junctions::Here,
+    };
+
+    // let id = AssetId::Concrete(location.clone());
+    // let fun = Fungibility::Fungible(42);
+    // let multiasset = MultiAsset {
+    //     id,
+    //     fun,
+    // };
+    // let withdraw_inst = WithdrawAsset(multiasset.into());
+
+    // We will never decode this "call",
+    // so it is irrelevant what we are passing to the `transact` cmd.
+    let fake_encoded_call = vec![0u8];
+
+    let transact_inst = Transact {
+        origin_type: OriginKind::Superuser,
+        require_weight_at_most: 0,
+        call: fake_encoded_call.into(),
+    };
+
+    let mut xcm_program = Xcm::<Call>(vec![transact_inst]);
+
+    let mut logger = Logger::start();
+
+    let max_weight = 100_000;
+    let mut weight_credit = 0;
+
+    let result = Barrier::should_execute(
+        &location,
+        &mut xcm_program,
+        max_weight,
+        &mut weight_credit
+    );
+
+    assert!(result.is_err(), "the barrier should disallow the XCM transact cmd");
+
+    catch_xcm_barrier_log(&mut logger, "transact XCM rejected");
+}
modifiedruntime/opal/Cargo.tomldiffbeforeafterboth
463pallet-foreing-assets = { default-features = false, path = "../../pallets/foreing-assets" }463pallet-foreing-assets = { default-features = false, path = "../../pallets/foreing-assets" }
464464
465################################################################################465################################################################################
466# Build Dependencies466# Dev Dependencies
467
468[dev-dependencies.logtest]
469version = "2.0.0"
470
471################################################################################
472# Build Dependencies
467473
468[build-dependencies.substrate-wasm-builder]474[build-dependencies.substrate-wasm-builder]
469git = "https://github.com/paritytech/substrate"475git = "https://github.com/paritytech/substrate"
modifiedruntime/quartz/Cargo.tomldiffbeforeafterboth
--- a/runtime/quartz/Cargo.toml
+++ b/runtime/quartz/Cargo.toml
@@ -471,6 +471,12 @@
 pallet-foreing-assets = { default-features = false, path = "../../pallets/foreing-assets" }
 
 ################################################################################
+# Dev Dependencies
+
+[dev-dependencies.logtest]
+version = "2.0.0"
+
+################################################################################
 # Build Dependencies
 
 [build-dependencies.substrate-wasm-builder]
modifiedruntime/unique/Cargo.tomldiffbeforeafterboth
--- a/runtime/unique/Cargo.toml
+++ b/runtime/unique/Cargo.toml
@@ -463,6 +463,12 @@
 pallet-foreing-assets = { default-features = false, path = "../../pallets/foreing-assets" }
 
 ################################################################################
+# Dev Dependencies
+
+[dev-dependencies.logtest]
+version = "2.0.0"
+
+################################################################################
 # Build Dependencies
 
 [build-dependencies.substrate-wasm-builder]