Skip to content

Commit

Permalink
Refactored assert_matches_reserve_asset_deposited_instructions
Browse files Browse the repository at this point in the history
Signed-off-by: Branislav Kontur <bkontur@gmail.com>
Signed-off-by: Adrian Catangiu <adrian@parity.io>

Co-authored-by: Branislav Kontur <bkontur@gmail.com>
Co-authored-by: Adrian Catangiu <adrian@parity.io>
  • Loading branch information
bkontur and acatangiu committed Oct 26, 2023
1 parent b0dd90d commit 65ed92f
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 58 deletions.
44 changes: 44 additions & 0 deletions cumulus/parachains/runtimes/assets/test-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,48 @@
pub mod test_cases;
pub mod test_cases_over_bridge;
pub mod xcm_helpers;

use frame_support::traits::ProcessMessageError;
pub use parachains_runtimes_test_utils::*;
use std::fmt::Debug;

use xcm::latest::prelude::*;
use xcm_builder::{CreateMatcher, MatchXcm};

/// Helper function to verify `xcm` contains all relevant instructions expected on destination
/// chain as part of a reserve-asset-transfer.
pub(crate) fn assert_matches_reserve_asset_deposited_instructions<RuntimeCall: Debug>(
xcm: &mut Xcm<RuntimeCall>,
expected_reserve_assets_deposited: &MultiAssets,
expected_beneficiary: &MultiLocation,
) {
let _ = xcm
.0
.matcher()
.skip_inst_while(|inst| !matches!(inst, ReserveAssetDeposited(..)))
.expect("no instruction ReserveAssetDeposited?")
.match_next_inst(|instr| match instr {
ReserveAssetDeposited(reserve_assets) => {
assert_eq!(reserve_assets, expected_reserve_assets_deposited);
Ok(())
},
_ => Err(ProcessMessageError::BadFormat),
})
.expect("expected instruction ReserveAssetDeposited")
.match_next_inst(|instr| match instr {
ClearOrigin => Ok(()),
_ => Err(ProcessMessageError::BadFormat),
})
.expect("expected instruction ClearOrigin")
.match_next_inst(|instr| match instr {
BuyExecution { .. } => Ok(()),
_ => Err(ProcessMessageError::BadFormat),
})
.expect("expected instruction BuyExecution")
.match_next_inst(|instr| match instr {
DepositAsset { assets: _, beneficiary } if beneficiary == expected_beneficiary =>
Ok(()),
_ => Err(ProcessMessageError::BadFormat),
})
.expect("expected instruction DepositAsset");
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
//! Module contains predefined test-case scenarios for `Runtime` with various assets transferred
//! over a bridge.

use crate::assert_matches_reserve_asset_deposited_instructions;
use codec::Encode;
use cumulus_primitives_core::XcmpMessageSource;
use frame_support::{
assert_ok,
traits::{Currency, OnFinalize, OnInitialize, OriginTrait, ProcessMessageError},
traits::{Currency, Get, OnFinalize, OnInitialize, OriginTrait, ProcessMessageError},
};
use frame_system::pallet_prelude::BlockNumberFor;
use parachains_common::{AccountId, Balance};
Expand Down Expand Up @@ -173,14 +174,20 @@ pub fn limited_reserve_transfer_assets_for_native_asset_works<
}),
};

let assets_to_transfer = MultiAssets::from(asset_to_transfer);
let mut expected_assets = assets_to_transfer.clone();
let context = XcmConfig::UniversalLocation::get();
expected_assets
.reanchor(&target_location_from_different_consensus, context)
.unwrap();

let expected_beneficiary = target_destination_account;

// Make sure sender has enough funds for paying delivery fees
let handling_delivery_fees = {
// Probable XCM with `ReserveAssetDeposited`.
let mut expected_reserve_asset_deposited_message = Xcm(vec![
ReserveAssetDeposited(MultiAssets::from(vec![MultiAsset {
id: Concrete(Default::default()),
fun: Fungible(balance_to_transfer),
}])),
ReserveAssetDeposited(MultiAssets::from(expected_assets.clone())),
ClearOrigin,
BuyExecution {
fees: MultiAsset {
Expand All @@ -189,17 +196,16 @@ pub fn limited_reserve_transfer_assets_for_native_asset_works<
},
weight_limit: Unlimited,
},
DepositAsset {
assets: Wild(AllCounted(1)),
beneficiary: target_destination_account,
},
DepositAsset { assets: Wild(AllCounted(1)), beneficiary: expected_beneficiary },
SetTopic([
220, 188, 144, 32, 213, 83, 111, 175, 44, 210, 111, 19, 90, 165, 191, 112,
140, 247, 192, 124, 42, 17, 153, 141, 114, 34, 189, 20, 83, 69, 237, 173,
]),
]);
assert_matches_pallet_xcm_reserve_transfer_assets_instructions(
assert_matches_reserve_asset_deposited_instructions(
&mut expected_reserve_asset_deposited_message,
&expected_assets,
&expected_beneficiary,
);

// Call `SendXcm::validate` to get delivery fees.
Expand Down Expand Up @@ -232,11 +238,12 @@ pub fn limited_reserve_transfer_assets_for_native_asset_works<
RuntimeHelper::<Runtime, AllPalletsWithoutSystem>::origin_of(alice_account.clone()),
Box::new(target_location_from_different_consensus.into_versioned()),
Box::new(target_destination_account.into_versioned()),
Box::new(VersionedMultiAssets::from(MultiAssets::from(asset_to_transfer))),
Box::new(VersionedMultiAssets::from(assets_to_transfer)),
0,
weight_limit,
));

// check events
// check pallet_xcm attempted
RuntimeHelper::<Runtime, AllPalletsWithoutSystem>::assert_pallet_xcm_event_outcome(
&unwrap_pallet_xcm_event,
Expand All @@ -260,7 +267,6 @@ pub fn limited_reserve_transfer_assets_for_native_asset_works<
local_bridge_hub_para_id.into(),
)
.unwrap();

assert_eq!(
xcm_sent_message_hash,
Some(xcm_sent.using_encoded(sp_io::hashing::blake2_256))
Expand Down Expand Up @@ -309,7 +315,11 @@ pub fn limited_reserve_transfer_assets_for_native_asset_works<
.split_global()
.expect("split works");
assert_eq!(destination, &target_location_junctions_without_global_consensus);
assert_matches_pallet_xcm_reserve_transfer_assets_instructions(inner_xcm);
assert_matches_reserve_asset_deposited_instructions(
inner_xcm,
&expected_assets,
&expected_beneficiary,
);
Ok(())
},
_ => Err(ProcessMessageError::BadFormat),
Expand Down Expand Up @@ -471,15 +481,21 @@ pub fn receive_reserve_asset_deposited_from_different_consensus_works<
0.into()
);

let expected_assets = MultiAssets::from(vec![MultiAsset {
id: Concrete(foreign_asset_id_multilocation),
fun: Fungible(transfered_foreign_asset_id_amount),
}]);
let expected_beneficiary = MultiLocation {
parents: 0,
interior: X1(AccountId32 { network: None, id: target_account.clone().into() }),
};

// Call received XCM execution
let xcm = Xcm(vec![
DescendOrigin(bridge_instance),
UniversalOrigin(universal_origin),
DescendOrigin(descend_origin),
ReserveAssetDeposited(MultiAssets::from(vec![MultiAsset {
id: Concrete(foreign_asset_id_multilocation),
fun: Fungible(transfered_foreign_asset_id_amount),
}])),
ReserveAssetDeposited(expected_assets.clone()),
ClearOrigin,
BuyExecution {
fees: MultiAsset {
Expand All @@ -488,22 +504,17 @@ pub fn receive_reserve_asset_deposited_from_different_consensus_works<
},
weight_limit: Unlimited,
},
DepositAsset {
assets: Wild(AllCounted(1)),
beneficiary: MultiLocation {
parents: 0,
interior: X1(AccountId32 {
network: None,
id: target_account.clone().into(),
}),
},
},
DepositAsset { assets: Wild(AllCounted(1)), beneficiary: expected_beneficiary },
SetTopic([
220, 188, 144, 32, 213, 83, 111, 175, 44, 210, 111, 19, 90, 165, 191, 112, 140,
247, 192, 124, 42, 17, 153, 141, 114, 34, 189, 20, 83, 69, 237, 173,
]),
]);
assert_matches_pallet_xcm_reserve_transfer_assets_instructions(&mut xcm.clone());
assert_matches_reserve_asset_deposited_instructions(
&mut xcm.clone(),
&expected_assets,
&expected_beneficiary,
);

let hash = xcm.using_encoded(sp_io::hashing::blake2_256);

Expand Down Expand Up @@ -564,36 +575,6 @@ pub fn receive_reserve_asset_deposited_from_different_consensus_works<
})
}

fn assert_matches_pallet_xcm_reserve_transfer_assets_instructions<RuntimeCall>(
xcm: &mut Xcm<RuntimeCall>,
) {
let _ = xcm
.0
.matcher()
.skip_inst_while(|inst| !matches!(inst, ReserveAssetDeposited(..)))
.expect("no instruction ReserveAssetDeposited?")
.match_next_inst(|instr| match instr {
ReserveAssetDeposited(..) => Ok(()),
_ => Err(ProcessMessageError::BadFormat),
})
.expect("expected instruction ReserveAssetDeposited")
.match_next_inst(|instr| match instr {
ClearOrigin => Ok(()),
_ => Err(ProcessMessageError::BadFormat),
})
.expect("expected instruction ClearOrigin")
.match_next_inst(|instr| match instr {
BuyExecution { .. } => Ok(()),
_ => Err(ProcessMessageError::BadFormat),
})
.expect("expected instruction BuyExecution")
.match_next_inst(|instr| match instr {
DepositAsset { .. } => Ok(()),
_ => Err(ProcessMessageError::BadFormat),
})
.expect("expected instruction DepositAsset");
}

pub fn report_bridge_status_from_xcm_bridge_router_works<
Runtime,
AllPalletsWithoutSystem,
Expand Down

0 comments on commit 65ed92f

Please sign in to comment.