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

difftreelog

source

runtime/common/tests/xcm.rs4.7 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::Call;21use super::new_test_ext;2223fn catch_xcm_barrier_log(24    logger: &mut Logger,25    expected_msg: &str26) -> Result<(), String> {27    for record in logger {28        if record.target() == "xcm::barrier"29        && record.args() == expected_msg {30            return Ok(());31        }32    }3334    Err(format!("the expected XCM barrier log `{}` is not found", expected_msg))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 = Transact {50        origin_type: OriginKind::Superuser,51        require_weight_at_most: 0,52        call: fake_encoded_call.into(),53    };5455    let mut xcm_program = Xcm::<Call>(vec![transact_inst]);5657    let max_weight = 100_000;58    let mut weight_credit = 100_000_000;5960    let result = B::should_execute(61        &location,62        &mut xcm_program,63        max_weight,64        &mut weight_credit65    );6667    assert!(result.is_err(), "the barrier should disallow the XCM transact cmd");6869    catch_xcm_barrier_log(logger, "transact XCM rejected").unwrap();70}7172fn xcm_execute<B: ShouldExecute>(73    self_para_id: u32,74    location: &MultiLocation,75    xcm: &mut Xcm<Call>76) -> Result<(), ()> {77    new_test_ext(self_para_id).execute_with(|| {78        let max_weight = 100_000;79        let mut weight_credit = 100_000_000;8081        B::should_execute(82            &location,83            xcm,84            max_weight,85            &mut weight_credit86        )87    })88}8990fn make_multiassets(location: &MultiLocation) -> MultiAssets {91    let id = AssetId::Concrete(location.clone());92    let fun = Fungibility::Fungible(42);93    let multiasset = MultiAsset {94        id,95        fun,96    };9798    multiasset.into()99}100101fn make_transfer_reserve_asset(location: &MultiLocation) -> Xcm<Call> {102    let assets = make_multiassets(location);103    let inst = TransferReserveAsset {104        assets,105        dest: location.clone(),106        xcm: Xcm(vec![]),107    };108109    Xcm::<Call>(vec![inst])110}111112fn make_deposit_reserve_asset(location: &MultiLocation) -> Xcm<Call> {113    let assets = make_multiassets(location);114    let inst = DepositReserveAsset {115        assets: assets.into(),116        max_assets: 42,117        dest: location.clone(),118        xcm: Xcm(vec![]),119    };120121    Xcm::<Call>(vec![inst])122}123124fn expect_transfer_location_denied<B: ShouldExecute>(125    logger: &mut Logger,126    self_para_id: u32,127    location: &MultiLocation,128    xcm: &mut Xcm<Call>129) -> Result<(), String> {130    let result = xcm_execute::<B>(self_para_id, location, xcm);131132    if result.is_ok() {133        return Err("the barrier should deny the unknown location".into());134    }135136    catch_xcm_barrier_log(logger, "Unexpected deposit or transfer location")137}138139/// WARNING: Uses log capturing140/// See https://docs.rs/logtest/latest/logtest/index.html#constraints141pub fn barrier_denies_transfer_from_unknown_location<B>(142    logger: &mut Logger,143    self_para_id: u32,144) -> Result<(), String>145where146    B: ShouldExecute147{148    const UNKNOWN_PARACHAIN_ID: u32 = 4057;149150    let unknown_location = MultiLocation {151        parents: 1,152        interior: X1(Parachain(UNKNOWN_PARACHAIN_ID)),153    };154155    let mut transfer_reserve_asset = make_transfer_reserve_asset(&unknown_location);156    let mut deposit_reserve_asset = make_deposit_reserve_asset(&unknown_location);157158    expect_transfer_location_denied::<B>(159        logger,160        self_para_id,161        &unknown_location,162        &mut transfer_reserve_asset163    )?;164165    expect_transfer_location_denied::<B>(166        logger,167        self_para_id,168        &unknown_location,169        &mut deposit_reserve_asset170    )?;171172    Ok(())173}