referrerpolicy=no-referrer-when-downgrade

staging_xcm_builder/
matches_location.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Polkadot.
3
4// Polkadot is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Polkadot is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Polkadot.  If not, see <http://www.gnu.org/licenses/>.
16
17//! Various implementations and utilities for matching and filtering `Location` and
18//! `InteriorLocation` types.
19
20use core::marker::PhantomData;
21use frame_support::traits::{Contains, Get};
22use sp_runtime::traits::MaybeEquivalence;
23use xcm::latest::{InteriorLocation, Location, NetworkId};
24
25/// An implementation of `Contains` that checks for `Location` or
26/// `InteriorLocation` if starts with the provided type `T`.
27pub struct StartsWith<T, L = Location>(core::marker::PhantomData<(T, L)>);
28impl<T: Get<L>, L: TryInto<Location> + Clone> Contains<L> for StartsWith<T, L> {
29	fn contains(location: &L) -> bool {
30		let latest_location: Location =
31			if let Ok(location) = (*location).clone().try_into() { location } else { return false };
32		let latest_t = if let Ok(location) = T::get().try_into() { location } else { return false };
33		latest_location.starts_with(&latest_t)
34	}
35}
36impl<T: Get<InteriorLocation>> Contains<InteriorLocation> for StartsWith<T> {
37	fn contains(t: &InteriorLocation) -> bool {
38		t.starts_with(&T::get())
39	}
40}
41
42/// An implementation of `Contains` that checks for `Location` or
43/// `InteriorLocation` if starts with expected `GlobalConsensus(NetworkId)` provided as type
44/// `T`.
45pub struct StartsWithExplicitGlobalConsensus<T>(core::marker::PhantomData<T>);
46impl<T: Get<NetworkId>> Contains<Location> for StartsWithExplicitGlobalConsensus<T> {
47	fn contains(location: &Location) -> bool {
48		matches!(location.interior().global_consensus(), Ok(requested_network) if requested_network.eq(&T::get()))
49	}
50}
51impl<T: Get<NetworkId>> Contains<InteriorLocation> for StartsWithExplicitGlobalConsensus<T> {
52	fn contains(location: &InteriorLocation) -> bool {
53		matches!(location.global_consensus(), Ok(requested_network) if requested_network.eq(&T::get()))
54	}
55}
56
57/// An adapter implementation of `MaybeEquivalence` which can convert between the latest `Location`
58/// and other versions that implement `TryInto<Location>` and `TryFrom<Location>`.
59pub struct WithLatestLocationConverter<Target>(PhantomData<Target>);
60impl<Target: TryInto<Location> + TryFrom<Location> + Clone> MaybeEquivalence<Location, Target>
61	for WithLatestLocationConverter<Target>
62{
63	fn convert(old: &Location) -> Option<Target> {
64		(*old).clone().try_into().ok()
65	}
66
67	fn convert_back(new: &Target) -> Option<Location> {
68		new.clone().try_into().ok()
69	}
70}