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

difftreelog

fix xcm unit test

Daniel Shiposha2024-05-30parent: #84a956b.patch.diff
in: master

1 file changed

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 frame_support::pallet_prelude::Weight;18use parity_scale_codec::Encode;19use staging_xcm::{20	latest::{prelude::*, Error},21	VersionedXcm,22};2324use super::{last_events, new_test_ext, AccountId};25use crate::{PolkadotXcm, Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin};2627const ALICE: AccountId = AccountId::new([0u8; 32]);28const BOB: AccountId = AccountId::new([1u8; 32]);2930const INITIAL_BALANCE: u128 = 10_000_000_000_000_000_000_000; // 10_000 UNQ3132#[test]33pub fn xcm_transact_is_forbidden() {34	new_test_ext(vec![(ALICE, INITIAL_BALANCE)]).execute_with(|| {35		PolkadotXcm::execute(36			RuntimeOrigin::signed(ALICE),37			Box::new(VersionedXcm::from(Xcm(vec![Transact {38				origin_kind: OriginKind::Native,39				require_weight_at_most: Weight::from_parts(1000, 1000),40				call: RuntimeCall::Balances(41					pallet_balances::Call::<Runtime>::transfer_keep_alive {42						dest: BOB.into(),43						value: INITIAL_BALANCE / 2,44					},45				)46				.encode()47				.into(),48			}]))),49			Weight::from_parts(1001000, 2000),50		)51		.expect(52			"XCM execute must succeed, the error should be in the `PolkadotXcm::Attempted` event",53		);5455		let xcm_event = &last_events(1)[0];56		match xcm_event {57			RuntimeEvent::PolkadotXcm(pallet_xcm::Event::<Runtime>::Attempted {58				outcome: Outcome::Incomplete {59					used: _weight,60					error: Error::NoPermission,61				},62			}) => { /* Pass */ }63			_ => panic!(64				"Expected PolkadotXcm.Attempted(Incomplete(_weight, NoPermission)),\65				found: {xcm_event:#?}"66			),67		}68	});69}
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 frame_support::pallet_prelude::Weight;18use parity_scale_codec::Encode;19use staging_xcm::{20	latest::{prelude::*, Error},21	VersionedXcm,22};2324use super::{last_events, new_test_ext, AccountId};25use crate::{26	runtime_common::config::xcm::XcmExecutorConfig, PolkadotXcm, Runtime, RuntimeCall,27	RuntimeEvent, RuntimeOrigin,28};2930type XcmExecutor = staging_xcm_executor::XcmExecutor<XcmExecutorConfig<Runtime>>;3132const ALICE: AccountId = AccountId::new([0u8; 32]);33const BOB: AccountId = AccountId::new([1u8; 32]);3435const INITIAL_BALANCE: u128 = 10_000_000_000_000_000_000_000; // 10_000 UNQ3637#[test]38pub fn xcm_transact_is_forbidden() {39	new_test_ext(vec![(ALICE, INITIAL_BALANCE)]).execute_with(|| {40		let max_weight = Weight::from_parts(1001000, 2000);4142		let origin: Location = AccountId32 {43			network: None,44			id: *ALICE.as_ref(),45		}46		.into();47		let message = Xcm(vec![Transact {48			origin_kind: OriginKind::Native,49			require_weight_at_most: Weight::from_parts(1000, 1000),50			call: RuntimeCall::Balances(pallet_balances::Call::<Runtime>::transfer_keep_alive {51				dest: BOB.into(),52				value: INITIAL_BALANCE / 2,53			})54			.encode()55			.into(),56		}]);57		let mut hash = message.using_encoded(sp_io::hashing::blake2_256);58		let weight_limit = max_weight;59		let weight_credit = max_weight;6061		let error = XcmExecutor::prepare_and_execute(62			origin,63			message,64			&mut hash,65			weight_limit,66			weight_credit,67		)68		.ensure_complete()69		.expect_err("XCM Transact shouldn't succeed");7071		assert_eq!(error, Error::NoPermission);72	});73}