Make System Parachains trusted Teleporters (#1368)

Make System Parachain trusted Teleporters of each other.

Migration of https://github.com/paritytech/cumulus/pull/2842

---------

Co-authored-by: command-bot <>
Co-authored-by: joe petrowski <25483142+joepetrowski@users.noreply.github.com>
This commit is contained in:
Ignacio Palacios
2023-10-16 19:52:33 +02:00
committed by GitHub
parent 4145902f1c
commit e0065cb81b
24 changed files with 465 additions and 84 deletions
@@ -15,6 +15,7 @@
use crate::impls::AccountIdOf;
use core::marker::PhantomData;
use cumulus_primitives_core::{IsSystem, ParaId};
use frame_support::{
traits::{fungibles::Inspect, tokens::ConversionToAssetBalance, ContainsPair},
weights::Weight,
@@ -78,3 +79,85 @@ impl<Location: Get<MultiLocation>> ContainsPair<MultiAsset, MultiLocation>
matches!(asset.id, Concrete(ref id) if id == origin && origin == &Location::get())
}
}
/// Accepts an asset if it is a concrete asset from the system (Relay Chain or system parachain).
pub struct ConcreteAssetFromSystem<AssetLocation>(PhantomData<AssetLocation>);
impl<AssetLocation: Get<MultiLocation>> ContainsPair<MultiAsset, MultiLocation>
for ConcreteAssetFromSystem<AssetLocation>
{
fn contains(asset: &MultiAsset, origin: &MultiLocation) -> bool {
log::trace!(target: "xcm::contains", "ConcreteAssetFromSystem asset: {:?}, origin: {:?}", asset, origin);
let is_system = match origin {
// The Relay Chain
MultiLocation { parents: 1, interior: Here } => true,
// System parachain
MultiLocation { parents: 1, interior: X1(Parachain(id)) } =>
ParaId::from(*id).is_system(),
// Others
_ => false,
};
matches!(asset.id, Concrete(id) if id == AssetLocation::get()) && is_system
}
}
#[cfg(test)]
mod tests {
use frame_support::parameter_types;
use super::{
ConcreteAssetFromSystem, ContainsPair, GeneralIndex, Here, MultiAsset, MultiLocation,
PalletInstance, Parachain, Parent,
};
parameter_types! {
pub const RelayLocation: MultiLocation = MultiLocation::parent();
}
#[test]
fn concrete_asset_from_relay_works() {
let expected_asset: MultiAsset = (Parent, 1000000).into();
let expected_origin: MultiLocation = (Parent, Here).into();
assert!(<ConcreteAssetFromSystem<RelayLocation>>::contains(
&expected_asset,
&expected_origin
));
}
#[test]
fn concrete_asset_from_sibling_system_para_fails_for_wrong_asset() {
let unexpected_assets: Vec<MultiAsset> = vec![
(Here, 1000000).into(),
((PalletInstance(50), GeneralIndex(1)), 1000000).into(),
((Parent, Parachain(1000), PalletInstance(50), GeneralIndex(1)), 1000000).into(),
];
let expected_origin: MultiLocation = (Parent, Parachain(1000)).into();
unexpected_assets.iter().for_each(|asset| {
assert!(!<ConcreteAssetFromSystem<RelayLocation>>::contains(asset, &expected_origin));
});
}
#[test]
fn concrete_asset_from_sibling_system_para_works_for_correct_asset() {
// (para_id, expected_result)
let test_data = vec![
(0, true),
(1, true),
(1000, true),
(1999, true),
(2000, false), // Not a System Parachain
(2001, false), // Not a System Parachain
];
let expected_asset: MultiAsset = (Parent, 1000000).into();
for (para_id, expected_result) in test_data {
let origin: MultiLocation = (Parent, Parachain(para_id)).into();
assert_eq!(
expected_result,
<ConcreteAssetFromSystem<RelayLocation>>::contains(&expected_asset, &origin)
);
}
}
}