Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 0ce339b

Browse files
EgorPopelyaevbkonturruseinov
authored
Tests + backport fix for teleport_assets (ReceiveTeleportedAsset) (#2235) (#2238)
* Wwstmint test for ReceiveTeleportedAsset * Missing fix for `weigh_multi_assets` * Added tests for statemine/statemint * [Enhancement] Use XCM V3 for initiate_teleport weight calc (#2102) * [Enhancement] Use XCM V3 for initiate_teleport weight calc * deref * replicate in all the runtimes * fmt * better handling for AllOf * fmt * small type fix * replicate the fix for all runtimes --------- Co-authored-by: parity-processbot <> * removed `frame_support::sp_tracing::try_init_simple();` * Review fixes * Removed `as u64` --------- Co-authored-by: Branislav Kontur <[email protected]> Co-authored-by: Roman Useinov <[email protected]>
1 parent d3271da commit 0ce339b

File tree

9 files changed

+210
-35
lines changed

9 files changed

+210
-35
lines changed

parachains/runtimes/assets/statemine/src/weights/xcm/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,24 @@ trait WeighMultiAssets {
2828
fn weigh_multi_assets(&self, weight: Weight) -> Weight;
2929
}
3030

31-
const MAX_ASSETS: u32 = 100;
31+
const MAX_ASSETS: u64 = 100;
3232

3333
impl WeighMultiAssets for MultiAssetFilter {
3434
fn weigh_multi_assets(&self, weight: Weight) -> Weight {
3535
match self {
3636
Self::Definite(assets) =>
3737
weight.saturating_mul(assets.inner().into_iter().count() as u64),
3838
Self::Wild(asset) => match asset {
39-
All => weight.saturating_mul(MAX_ASSETS as u64),
39+
All => weight.saturating_mul(MAX_ASSETS),
4040
AllOf { fun, .. } => match fun {
4141
WildFungibility::Fungible => weight,
4242
// Magic number 2 has to do with the fact that we could have up to 2 times
4343
// MaxAssetsIntoHolding in the worst-case scenario.
4444
WildFungibility::NonFungible =>
4545
weight.saturating_mul((MaxAssetsIntoHolding::get() * 2) as u64),
4646
},
47-
AllCounted(count) => weight.saturating_mul(*count as u64),
48-
AllOfCounted { count, .. } => weight.saturating_mul(*count as u64),
47+
AllCounted(count) => weight.saturating_mul(MAX_ASSETS.min(*count as u64)),
48+
AllOfCounted { count, .. } => weight.saturating_mul(MAX_ASSETS.min(*count as u64)),
4949
},
5050
}
5151
}

parachains/runtimes/assets/statemine/tests/tests.rs

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
use asset_test_utils::{ExtBuilder, RuntimeHelper};
2+
use codec::Encode;
23
use cumulus_primitives_utility::ChargeWeightInFungibles;
34
use frame_support::{
4-
assert_noop, assert_ok,
5+
assert_noop, assert_ok, sp_io,
56
traits::PalletInfo,
67
weights::{Weight, WeightToFee as WeightToFeeT},
78
};
89
use parachains_common::{AccountId, AuraId};
910
use statemine_runtime::xcm_config::AssetFeeAsExistentialDepositMultiplierFeeCharger;
1011
pub use statemine_runtime::{
1112
constants::fee::WeightToFee, xcm_config::XcmConfig, Assets, Balances, ExistentialDeposit,
12-
Runtime, SessionKeys, System,
13+
ReservedDmpWeight, Runtime, SessionKeys, System,
1314
};
1415
use xcm::latest::prelude::*;
15-
use xcm_executor::traits::WeightTrader;
16+
use xcm_executor::{traits::WeightTrader, XcmExecutor};
1617

1718
pub const ALICE: [u8; 32] = [1u8; 32];
1819

@@ -388,3 +389,51 @@ fn test_asset_xcm_trader_not_possible_for_non_sufficient_assets() {
388389
assert_eq!(Assets::total_supply(1), minimum_asset_balance);
389390
});
390391
}
392+
393+
#[test]
394+
fn receive_teleported_asset_works() {
395+
ExtBuilder::<Runtime>::default()
396+
.with_collators(vec![AccountId::from(ALICE)])
397+
.with_session_keys(vec![(
398+
AccountId::from(ALICE),
399+
AccountId::from(ALICE),
400+
SessionKeys { aura: AuraId::from(sp_core::sr25519::Public::from_raw(ALICE)) },
401+
)])
402+
.build()
403+
.execute_with(|| {
404+
let xcm = Xcm(vec![
405+
ReceiveTeleportedAsset(MultiAssets::from(vec![MultiAsset {
406+
id: Concrete(MultiLocation { parents: 1, interior: Here }),
407+
fun: Fungible(10000000000000),
408+
}])),
409+
ClearOrigin,
410+
BuyExecution {
411+
fees: MultiAsset {
412+
id: Concrete(MultiLocation { parents: 1, interior: Here }),
413+
fun: Fungible(10000000000000),
414+
},
415+
weight_limit: Limited(Weight::from_parts(303531000, 65536)),
416+
},
417+
DepositAsset {
418+
assets: Wild(AllCounted(1)),
419+
beneficiary: MultiLocation {
420+
parents: 0,
421+
interior: X1(AccountId32 {
422+
network: None,
423+
id: [
424+
18, 153, 85, 112, 1, 245, 88, 21, 211, 252, 181, 60, 116, 70, 58,
425+
203, 12, 246, 209, 77, 70, 57, 179, 64, 152, 44, 96, 135, 127, 56,
426+
70, 9,
427+
],
428+
}),
429+
},
430+
},
431+
]);
432+
let hash = xcm.using_encoded(sp_io::hashing::blake2_256);
433+
434+
let weight_limit = ReservedDmpWeight::get();
435+
436+
let outcome = XcmExecutor::<XcmConfig>::execute_xcm(Parent, xcm, hash, weight_limit);
437+
assert_eq!(outcome.ensure_complete(), Ok(()));
438+
})
439+
}

parachains/runtimes/assets/statemint/src/weights/xcm/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,24 @@ trait WeighMultiAssets {
2828
fn weigh_multi_assets(&self, weight: Weight) -> Weight;
2929
}
3030

31-
const MAX_ASSETS: u32 = 100;
31+
const MAX_ASSETS: u64 = 100;
3232

3333
impl WeighMultiAssets for MultiAssetFilter {
3434
fn weigh_multi_assets(&self, weight: Weight) -> Weight {
3535
match self {
3636
Self::Definite(assets) =>
3737
weight.saturating_mul(assets.inner().into_iter().count() as u64),
3838
Self::Wild(asset) => match asset {
39-
All => weight.saturating_mul(MAX_ASSETS as u64),
39+
All => weight.saturating_mul(MAX_ASSETS),
4040
AllOf { fun, .. } => match fun {
4141
WildFungibility::Fungible => weight,
4242
// Magic number 2 has to do with the fact that we could have up to 2 times
4343
// MaxAssetsIntoHolding in the worst-case scenario.
4444
WildFungibility::NonFungible =>
4545
weight.saturating_mul((MaxAssetsIntoHolding::get() * 2) as u64),
4646
},
47-
AllCounted(count) => weight.saturating_mul(*count as u64),
48-
AllOfCounted { count, .. } => weight.saturating_mul(*count as u64),
47+
AllCounted(count) => weight.saturating_mul(MAX_ASSETS.min(*count as u64)),
48+
AllOfCounted { count, .. } => weight.saturating_mul(MAX_ASSETS.min(*count as u64)),
4949
},
5050
}
5151
}

parachains/runtimes/assets/statemint/tests/tests.rs

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
use asset_test_utils::{ExtBuilder, RuntimeHelper};
2+
use codec::Encode;
23
use cumulus_primitives_utility::ChargeWeightInFungibles;
34
use frame_support::{
4-
assert_noop, assert_ok,
5+
assert_noop, assert_ok, sp_io,
56
traits::PalletInfo,
67
weights::{Weight, WeightToFee as WeightToFeeT},
78
};
89
use parachains_common::{AccountId, StatemintAuraId as AuraId};
910
use statemint_runtime::xcm_config::AssetFeeAsExistentialDepositMultiplierFeeCharger;
1011
pub use statemint_runtime::{
1112
constants::fee::WeightToFee, xcm_config::XcmConfig, Assets, Balances, ExistentialDeposit,
12-
Runtime, SessionKeys, System,
13+
ReservedDmpWeight, Runtime, SessionKeys, System,
1314
};
1415
use xcm::latest::prelude::*;
15-
use xcm_executor::traits::WeightTrader;
16+
use xcm_executor::{traits::WeightTrader, XcmExecutor};
1617

1718
pub const ALICE: [u8; 32] = [1u8; 32];
1819

@@ -400,3 +401,51 @@ fn test_asset_xcm_trader_not_possible_for_non_sufficient_assets() {
400401
assert_eq!(Assets::total_supply(1), minimum_asset_balance);
401402
});
402403
}
404+
405+
#[test]
406+
fn receive_teleported_asset_works() {
407+
ExtBuilder::<Runtime>::default()
408+
.with_collators(vec![AccountId::from(ALICE)])
409+
.with_session_keys(vec![(
410+
AccountId::from(ALICE),
411+
AccountId::from(ALICE),
412+
SessionKeys { aura: AuraId::from(sp_core::ed25519::Public::from_raw(ALICE)) },
413+
)])
414+
.build()
415+
.execute_with(|| {
416+
let xcm = Xcm(vec![
417+
ReceiveTeleportedAsset(MultiAssets::from(vec![MultiAsset {
418+
id: Concrete(MultiLocation { parents: 1, interior: Here }),
419+
fun: Fungible(10000000000000),
420+
}])),
421+
ClearOrigin,
422+
BuyExecution {
423+
fees: MultiAsset {
424+
id: Concrete(MultiLocation { parents: 1, interior: Here }),
425+
fun: Fungible(10000000000000),
426+
},
427+
weight_limit: Limited(Weight::from_parts(303531000, 65536)),
428+
},
429+
DepositAsset {
430+
assets: Wild(AllCounted(1)),
431+
beneficiary: MultiLocation {
432+
parents: 0,
433+
interior: X1(AccountId32 {
434+
network: None,
435+
id: [
436+
18, 153, 85, 112, 1, 245, 88, 21, 211, 252, 181, 60, 116, 70, 58,
437+
203, 12, 246, 209, 77, 70, 57, 179, 64, 152, 44, 96, 135, 127, 56,
438+
70, 9,
439+
],
440+
}),
441+
},
442+
},
443+
]);
444+
let hash = xcm.using_encoded(sp_io::hashing::blake2_256);
445+
446+
let weight_limit = ReservedDmpWeight::get();
447+
448+
let outcome = XcmExecutor::<XcmConfig>::execute_xcm(Parent, xcm, hash, weight_limit);
449+
assert_eq!(outcome.ensure_complete(), Ok(()));
450+
})
451+
}

parachains/runtimes/assets/westmint/src/weights/xcm/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,24 @@ trait WeighMultiAssets {
2828
fn weigh_multi_assets(&self, weight: Weight) -> Weight;
2929
}
3030

31-
const MAX_ASSETS: u32 = 100;
31+
const MAX_ASSETS: u64 = 100;
3232

3333
impl WeighMultiAssets for MultiAssetFilter {
3434
fn weigh_multi_assets(&self, weight: Weight) -> Weight {
3535
match self {
3636
Self::Definite(assets) =>
3737
weight.saturating_mul(assets.inner().into_iter().count() as u64),
3838
Self::Wild(asset) => match asset {
39-
All => weight.saturating_mul(MAX_ASSETS as u64),
39+
All => weight.saturating_mul(MAX_ASSETS),
4040
AllOf { fun, .. } => match fun {
4141
WildFungibility::Fungible => weight,
4242
// Magic number 2 has to do with the fact that we could have up to 2 times
4343
// MaxAssetsIntoHolding in the worst-case scenario.
4444
WildFungibility::NonFungible =>
4545
weight.saturating_mul((MaxAssetsIntoHolding::get() * 2) as u64),
4646
},
47-
AllCounted(count) => weight.saturating_mul(*count as u64),
48-
AllOfCounted { count, .. } => weight.saturating_mul(*count as u64),
47+
AllCounted(count) => weight.saturating_mul(MAX_ASSETS.min(*count as u64)),
48+
AllOfCounted { count, .. } => weight.saturating_mul(MAX_ASSETS.min(*count as u64)),
4949
},
5050
}
5151
}

parachains/runtimes/assets/westmint/tests/tests.rs

Lines changed: 82 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
use asset_test_utils::{ExtBuilder, RuntimeHelper};
2+
use codec::{DecodeLimit, Encode};
23
use cumulus_primitives_utility::ChargeWeightInFungibles;
34
use frame_support::{
4-
assert_noop, assert_ok,
5+
assert_noop, assert_ok, sp_io,
56
traits::PalletInfo,
67
weights::{Weight, WeightToFee as WeightToFeeT},
78
};
89
use parachains_common::{AccountId, AuraId};
9-
use westmint_runtime::xcm_config::AssetFeeAsExistentialDepositMultiplierFeeCharger;
1010
pub use westmint_runtime::{
1111
constants::fee::WeightToFee, xcm_config::XcmConfig, Assets, Balances, ExistentialDeposit,
12-
Runtime, SessionKeys, System,
12+
ReservedDmpWeight, Runtime, SessionKeys, System,
1313
};
14-
use xcm::latest::prelude::*;
15-
use xcm_executor::traits::WeightTrader;
14+
use westmint_runtime::{xcm_config::AssetFeeAsExistentialDepositMultiplierFeeCharger, RuntimeCall};
15+
use xcm::{latest::prelude::*, VersionedXcm, MAX_XCM_DECODE_DEPTH};
16+
use xcm_executor::{traits::WeightTrader, XcmExecutor};
1617

1718
pub const ALICE: [u8; 32] = [1u8; 32];
1819

@@ -386,3 +387,79 @@ fn test_asset_xcm_trader_not_possible_for_non_sufficient_assets() {
386387
assert_eq!(Assets::total_supply(1), minimum_asset_balance);
387388
});
388389
}
390+
391+
#[test]
392+
fn receive_teleported_asset_works() {
393+
ExtBuilder::<Runtime>::default()
394+
.with_collators(vec![AccountId::from(ALICE)])
395+
.with_session_keys(vec![(
396+
AccountId::from(ALICE),
397+
AccountId::from(ALICE),
398+
SessionKeys { aura: AuraId::from(sp_core::sr25519::Public::from_raw(ALICE)) },
399+
)])
400+
.build()
401+
.execute_with(|| {
402+
let xcm = Xcm(vec![
403+
ReceiveTeleportedAsset(MultiAssets::from(vec![MultiAsset {
404+
id: Concrete(MultiLocation { parents: 1, interior: Here }),
405+
fun: Fungible(10000000000000),
406+
}])),
407+
ClearOrigin,
408+
BuyExecution {
409+
fees: MultiAsset {
410+
id: Concrete(MultiLocation { parents: 1, interior: Here }),
411+
fun: Fungible(10000000000000),
412+
},
413+
weight_limit: Limited(Weight::from_parts(303531000, 65536)),
414+
},
415+
DepositAsset {
416+
assets: Wild(AllCounted(1)),
417+
beneficiary: MultiLocation {
418+
parents: 0,
419+
interior: X1(AccountId32 {
420+
network: None,
421+
id: [
422+
18, 153, 85, 112, 1, 245, 88, 21, 211, 252, 181, 60, 116, 70, 58,
423+
203, 12, 246, 209, 77, 70, 57, 179, 64, 152, 44, 96, 135, 127, 56,
424+
70, 9,
425+
],
426+
}),
427+
},
428+
},
429+
]);
430+
let hash = xcm.using_encoded(sp_io::hashing::blake2_256);
431+
432+
let weight_limit = ReservedDmpWeight::get();
433+
434+
let outcome = XcmExecutor::<XcmConfig>::execute_xcm(Parent, xcm, hash, weight_limit);
435+
assert_eq!(outcome.ensure_complete(), Ok(()));
436+
})
437+
}
438+
439+
#[test]
440+
fn plain_receive_teleported_asset_works() {
441+
ExtBuilder::<Runtime>::default()
442+
.with_collators(vec![AccountId::from(ALICE)])
443+
.with_session_keys(vec![(
444+
AccountId::from(ALICE),
445+
AccountId::from(ALICE),
446+
SessionKeys { aura: AuraId::from(sp_core::sr25519::Public::from_raw(ALICE)) },
447+
)])
448+
.build()
449+
.execute_with(|| {
450+
let data = hex_literal::hex!("02100204000100000b00a0724e18090a13000100000b00a0724e180901e20f5e480d010004000101001299557001f55815d3fcb53c74463acb0cf6d14d4639b340982c60877f384609").to_vec();
451+
let message_id = sp_io::hashing::blake2_256(&data);
452+
453+
let maybe_msg = VersionedXcm::<RuntimeCall>::decode_all_with_depth_limit(
454+
MAX_XCM_DECODE_DEPTH,
455+
&mut data.as_ref(),
456+
)
457+
.map(xcm::v3::Xcm::<RuntimeCall>::try_from).expect("failed").expect("failed");
458+
459+
let weight_limit = ReservedDmpWeight::get();
460+
461+
let outcome =
462+
XcmExecutor::<XcmConfig>::execute_xcm(Parent, maybe_msg, message_id, weight_limit);
463+
assert_eq!(outcome.ensure_complete(), Ok(()));
464+
})
465+
}

parachains/runtimes/bridge-hubs/bridge-hub-kusama/src/weights/xcm/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,24 @@ trait WeighMultiAssets {
2828
fn weigh_multi_assets(&self, weight: Weight) -> Weight;
2929
}
3030

31-
const MAX_ASSETS: u32 = 100;
31+
const MAX_ASSETS: u64 = 100;
3232

3333
impl WeighMultiAssets for MultiAssetFilter {
3434
fn weigh_multi_assets(&self, weight: Weight) -> Weight {
3535
match self {
3636
Self::Definite(assets) =>
3737
weight.saturating_mul(assets.inner().into_iter().count() as u64),
3838
Self::Wild(asset) => match asset {
39-
All => weight.saturating_mul(MAX_ASSETS as u64),
39+
All => weight.saturating_mul(MAX_ASSETS),
4040
AllOf { fun, .. } => match fun {
4141
WildFungibility::Fungible => weight,
4242
// Magic number 2 has to do with the fact that we could have up to 2 times
4343
// MaxAssetsIntoHolding in the worst-case scenario.
4444
WildFungibility::NonFungible =>
4545
weight.saturating_mul((MaxAssetsIntoHolding::get() * 2) as u64),
4646
},
47-
AllCounted(count) => weight.saturating_mul(*count as u64),
48-
AllOfCounted { count, .. } => weight.saturating_mul(*count as u64),
47+
AllCounted(count) => weight.saturating_mul(MAX_ASSETS.min(*count as u64)),
48+
AllOfCounted { count, .. } => weight.saturating_mul(MAX_ASSETS.min(*count as u64)),
4949
},
5050
}
5151
}

parachains/runtimes/bridge-hubs/bridge-hub-polkadot/src/weights/xcm/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,24 @@ trait WeighMultiAssets {
2828
fn weigh_multi_assets(&self, weight: Weight) -> Weight;
2929
}
3030

31-
const MAX_ASSETS: u32 = 100;
31+
const MAX_ASSETS: u64 = 100;
3232

3333
impl WeighMultiAssets for MultiAssetFilter {
3434
fn weigh_multi_assets(&self, weight: Weight) -> Weight {
3535
match self {
3636
Self::Definite(assets) =>
3737
weight.saturating_mul(assets.inner().into_iter().count() as u64),
3838
Self::Wild(asset) => match asset {
39-
All => weight.saturating_mul(MAX_ASSETS as u64),
39+
All => weight.saturating_mul(MAX_ASSETS),
4040
AllOf { fun, .. } => match fun {
4141
WildFungibility::Fungible => weight,
4242
// Magic number 2 has to do with the fact that we could have up to 2 times
4343
// MaxAssetsIntoHolding in the worst-case scenario.
4444
WildFungibility::NonFungible =>
4545
weight.saturating_mul((MaxAssetsIntoHolding::get() * 2) as u64),
4646
},
47-
AllCounted(count) => weight.saturating_mul(*count as u64),
48-
AllOfCounted { count, .. } => weight.saturating_mul(*count as u64),
47+
AllCounted(count) => weight.saturating_mul(MAX_ASSETS.min(*count as u64)),
48+
AllOfCounted { count, .. } => weight.saturating_mul(MAX_ASSETS.min(*count as u64)),
4949
},
5050
}
5151
}

0 commit comments

Comments
 (0)