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

Commit

Permalink
Added StartsWithExplicitGlobalConsensus to ignores (#2338)
Browse files Browse the repository at this point in the history
  • Loading branch information
bkontur authored Mar 16, 2023
1 parent 60a6089 commit 018b800
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 6 deletions.
60 changes: 58 additions & 2 deletions parachains/runtimes/assets/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ pub type ForeignAssetsConvertedConcreteId<AdditionalMultiLocationExclusionFilter
#[cfg(test)]
mod tests {
use super::*;
use crate::matching::StartsWithExplicitGlobalConsensus;
use xcm::latest::prelude::*;
use xcm_executor::traits::{Convert, Error as MatchError, MatchesFungibles};

Expand Down Expand Up @@ -197,10 +198,17 @@ mod tests {
fn multi_location_converted_concrete_id_converter_works() {
frame_support::parameter_types! {
pub Parachain100Pattern: MultiLocation = MultiLocation::new(1, X1(Parachain(100)));
pub UniversalLocationNetworkId: NetworkId = NetworkId::ByGenesis([9; 32]);
}

// setup convert
type Convert = ForeignAssetsConvertedConcreteId<StartsWith<Parachain100Pattern>, u128>;
type Convert = ForeignAssetsConvertedConcreteId<
(
StartsWith<Parachain100Pattern>,
StartsWithExplicitGlobalConsensus<UniversalLocationNetworkId>,
),
u128,
>;

let test_data = vec![
// excluded as local
Expand All @@ -212,13 +220,33 @@ mod tests {
),
// excluded as parent
(ma_1000(1, Here), Err(MatchError::AssetNotHandled)),
// excluded as additional filter
// excluded as additional filter - Parachain100Pattern
(ma_1000(1, X1(Parachain(100))), Err(MatchError::AssetNotHandled)),
(ma_1000(1, X2(Parachain(100), GeneralIndex(1234))), Err(MatchError::AssetNotHandled)),
(
ma_1000(1, X3(Parachain(100), PalletInstance(13), GeneralIndex(1234))),
Err(MatchError::AssetNotHandled),
),
// excluded as additional filter - StartsWithExplicitGlobalConsensus
(
ma_1000(1, X1(GlobalConsensus(NetworkId::ByGenesis([9; 32])))),
Err(MatchError::AssetNotHandled),
),
(
ma_1000(2, X1(GlobalConsensus(NetworkId::ByGenesis([9; 32])))),
Err(MatchError::AssetNotHandled),
),
(
ma_1000(
2,
X3(
GlobalConsensus(NetworkId::ByGenesis([9; 32])),
Parachain(200),
GeneralIndex(1234),
),
),
Err(MatchError::AssetNotHandled),
),
// ok
(ma_1000(1, X1(Parachain(200))), Ok((MultiLocation::new(1, X1(Parachain(200))), 1000))),
(ma_1000(2, X1(Parachain(200))), Ok((MultiLocation::new(2, X1(Parachain(200))), 1000))),
Expand All @@ -230,6 +258,34 @@ mod tests {
ma_1000(2, X2(Parachain(200), GeneralIndex(1234))),
Ok((MultiLocation::new(2, X2(Parachain(200), GeneralIndex(1234))), 1000)),
),
(
ma_1000(2, X1(GlobalConsensus(NetworkId::ByGenesis([7; 32])))),
Ok((
MultiLocation::new(2, X1(GlobalConsensus(NetworkId::ByGenesis([7; 32])))),
1000,
)),
),
(
ma_1000(
2,
X3(
GlobalConsensus(NetworkId::ByGenesis([7; 32])),
Parachain(200),
GeneralIndex(1234),
),
),
Ok((
MultiLocation::new(
2,
X3(
GlobalConsensus(NetworkId::ByGenesis([7; 32])),
Parachain(200),
GeneralIndex(1234),
),
),
1000,
)),
),
];

for (multi_asset, expected_result) in test_data {
Expand Down
12 changes: 12 additions & 0 deletions parachains/runtimes/assets/common/src/matching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ impl<Location: Get<MultiLocation>> Contains<MultiLocation> for Equals<Location>
}
}

pub struct StartsWithExplicitGlobalConsensus<T>(sp_std::marker::PhantomData<T>);
impl<Network: Get<NetworkId>> Contains<MultiLocation>
for StartsWithExplicitGlobalConsensus<Network>
{
fn contains(t: &MultiLocation) -> bool {
match t.interior.global_consensus() {
Ok(requested_network) if requested_network.eq(&Network::get()) => true,
_ => false,
}
}
}

frame_support::parameter_types! {
pub LocalMultiLocationPattern: MultiLocation = MultiLocation::new(0, Here);
pub ParentLocation: MultiLocation = MultiLocation::parent();
Expand Down
14 changes: 12 additions & 2 deletions parachains/runtimes/assets/statemine/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ use super::{
ParachainInfo, ParachainSystem, PolkadotXcm, Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin,
TrustBackedAssetsInstance, WeightToFee, XcmpQueue,
};
use assets_common::matching::{FromSiblingParachain, IsForeignConcreteAsset, StartsWith};
use assets_common::matching::{
FromSiblingParachain, IsForeignConcreteAsset, StartsWith, StartsWithExplicitGlobalConsensus,
};
use frame_support::{
match_types, parameter_types,
traits::{ConstU32, Contains, Everything, Nothing, PalletInfoAccess},
Expand Down Expand Up @@ -49,6 +51,7 @@ parameter_types! {
pub RelayChainOrigin: RuntimeOrigin = cumulus_pallet_xcm::Origin::Relay.into();
pub UniversalLocation: InteriorMultiLocation =
X2(GlobalConsensus(RelayNetwork::get().unwrap()), Parachain(ParachainInfo::parachain_id().into()));
pub UniversalLocationNetworkId: NetworkId = UniversalLocation::get().global_consensus().unwrap();
pub TrustBackedAssetsPalletLocation: MultiLocation =
PalletInstance(<Assets as PalletInfoAccess>::index() as u8).into();
pub CheckingAccount: AccountId = PolkadotXcm::check_account();
Expand Down Expand Up @@ -103,7 +106,14 @@ pub type FungiblesTransactor = FungiblesAdapter<

/// `AssetId/Balance` converter for `TrustBackedAssets`
pub type ForeignAssetsConvertedConcreteId = assets_common::ForeignAssetsConvertedConcreteId<
StartsWith<TrustBackedAssetsPalletLocation>,
(
// Ignore `TrustBackedAssets` explicitly
StartsWith<TrustBackedAssetsPalletLocation>,
// Ignore asset which starts explicitly with our `GlobalConsensus(NetworkId)`, means:
// - foreign assets from our consensus should be: `MultiLocation {parent: 1, X*(Parachain(xyz))}
// - foreign assets outside our consensus with the same `GlobalConsensus(NetworkId)` wont be accepted here
StartsWithExplicitGlobalConsensus<UniversalLocationNetworkId>,
),
Balance,
>;

Expand Down
14 changes: 12 additions & 2 deletions parachains/runtimes/assets/westmint/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ use super::{
TrustBackedAssetsInstance, WeightToFee, XcmpQueue,
};
use crate::ForeignAssets;
use assets_common::matching::{FromSiblingParachain, IsForeignConcreteAsset, StartsWith};
use assets_common::matching::{
FromSiblingParachain, IsForeignConcreteAsset, StartsWith, StartsWithExplicitGlobalConsensus,
};
use frame_support::{
match_types, parameter_types,
traits::{ConstU32, Contains, Everything, Nothing, PalletInfoAccess},
Expand Down Expand Up @@ -50,6 +52,7 @@ parameter_types! {
pub RelayChainOrigin: RuntimeOrigin = cumulus_pallet_xcm::Origin::Relay.into();
pub UniversalLocation: InteriorMultiLocation =
X2(GlobalConsensus(RelayNetwork::get().unwrap()), Parachain(ParachainInfo::parachain_id().into()));
pub UniversalLocationNetworkId: NetworkId = UniversalLocation::get().global_consensus().unwrap();
pub TrustBackedAssetsPalletLocation: MultiLocation =
PalletInstance(<Assets as PalletInfoAccess>::index() as u8).into();
pub CheckingAccount: AccountId = PolkadotXcm::check_account();
Expand Down Expand Up @@ -104,7 +107,14 @@ pub type FungiblesTransactor = FungiblesAdapter<

/// `AssetId/Balance` converter for `TrustBackedAssets`
pub type ForeignAssetsConvertedConcreteId = assets_common::ForeignAssetsConvertedConcreteId<
StartsWith<TrustBackedAssetsPalletLocation>,
(
// Ignore `TrustBackedAssets` explicitly
StartsWith<TrustBackedAssetsPalletLocation>,
// Ignore asset which starts explicitly with our `GlobalConsensus(NetworkId)`, means:
// - foreign assets from our consensus should be: `MultiLocation {parent: 1, X*(Parachain(xyz))}
// - foreign assets outside our consensus with the same `GlobalConsensus(NetworkId)` wont be accepted here
StartsWithExplicitGlobalConsensus<UniversalLocationNetworkId>,
),
Balance,
>;

Expand Down

0 comments on commit 018b800

Please sign in to comment.