From 4dba15479b2da875b39acd9331f7f7c2ce1d3b21 Mon Sep 17 00:00:00 2001 From: Daniel Shiposha Date: Thu, 30 May 2024 13:42:02 +0000 Subject: [PATCH] fix: xcm unit test --- --- a/runtime/common/tests/xcm.rs +++ b/runtime/common/tests/xcm.rs @@ -22,7 +22,12 @@ }; use super::{last_events, new_test_ext, AccountId}; -use crate::{PolkadotXcm, Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin}; +use crate::{ + runtime_common::config::xcm::XcmExecutorConfig, PolkadotXcm, Runtime, RuntimeCall, + RuntimeEvent, RuntimeOrigin, +}; + +type XcmExecutor = staging_xcm_executor::XcmExecutor>; const ALICE: AccountId = AccountId::new([0u8; 32]); const BOB: AccountId = AccountId::new([1u8; 32]); @@ -32,38 +37,37 @@ #[test] pub fn xcm_transact_is_forbidden() { new_test_ext(vec![(ALICE, INITIAL_BALANCE)]).execute_with(|| { - PolkadotXcm::execute( - RuntimeOrigin::signed(ALICE), - Box::new(VersionedXcm::from(Xcm(vec![Transact { - origin_kind: OriginKind::Native, - require_weight_at_most: Weight::from_parts(1000, 1000), - call: RuntimeCall::Balances( - pallet_balances::Call::::transfer_keep_alive { - dest: BOB.into(), - value: INITIAL_BALANCE / 2, - }, - ) - .encode() - .into(), - }]))), - Weight::from_parts(1001000, 2000), + let max_weight = Weight::from_parts(1001000, 2000); + + let origin: Location = AccountId32 { + network: None, + id: *ALICE.as_ref(), + } + .into(); + let message = Xcm(vec![Transact { + origin_kind: OriginKind::Native, + require_weight_at_most: Weight::from_parts(1000, 1000), + call: RuntimeCall::Balances(pallet_balances::Call::::transfer_keep_alive { + dest: BOB.into(), + value: INITIAL_BALANCE / 2, + }) + .encode() + .into(), + }]); + let mut hash = message.using_encoded(sp_io::hashing::blake2_256); + let weight_limit = max_weight; + let weight_credit = max_weight; + + let error = XcmExecutor::prepare_and_execute( + origin, + message, + &mut hash, + weight_limit, + weight_credit, ) - .expect( - "XCM execute must succeed, the error should be in the `PolkadotXcm::Attempted` event", - ); + .ensure_complete() + .expect_err("XCM Transact shouldn't succeed"); - let xcm_event = &last_events(1)[0]; - match xcm_event { - RuntimeEvent::PolkadotXcm(pallet_xcm::Event::::Attempted { - outcome: Outcome::Incomplete { - used: _weight, - error: Error::NoPermission, - }, - }) => { /* Pass */ } - _ => panic!( - "Expected PolkadotXcm.Attempted(Incomplete(_weight, NoPermission)),\ - found: {xcm_event:#?}" - ), - } + assert_eq!(error, Error::NoPermission); }); } -- gitstuff