feat: initialize Kurdistan SDK - independent fork of Polkadot SDK

This commit is contained in:
2025-12-13 15:44:15 +03:00
commit 286de54384
6841 changed files with 1848356 additions and 0 deletions
+90
View File
@@ -0,0 +1,90 @@
[package]
name = "staging-xcm-builder"
description = "Tools & types for building with XCM and its executor."
authors.workspace = true
edition.workspace = true
license.workspace = true
version = "7.0.0"
homepage.workspace = true
repository.workspace = true
[lints]
workspace = true
[dependencies]
codec = { features = ["derive"], workspace = true }
environmental = { workspace = true }
frame-support = { workspace = true }
frame-system = { workspace = true }
impl-trait-for-tuples = { workspace = true }
pallet-asset-conversion = { workspace = true }
pallet-transaction-payment = { workspace = true }
scale-info = { features = ["derive"], workspace = true }
sp-arithmetic = { workspace = true }
sp-core = { workspace = true }
sp-io = { workspace = true }
sp-runtime = { workspace = true }
sp-weights = { workspace = true }
tracing = { workspace = true }
xcm = { workspace = true }
xcm-executor = { workspace = true }
# Pezkuwi dependencies
pezkuwi-teyrchain-primitives = { workspace = true }
[dev-dependencies]
pallet-assets = { workspace = true, default-features = true }
pallet-balances = { workspace = true, default-features = true }
pallet-salary = { workspace = true, default-features = true }
pallet-xcm = { workspace = true, default-features = true }
pezkuwi-primitives = { workspace = true, default-features = true }
pezkuwi-runtime-teyrchains = { workspace = true, default-features = true }
pezkuwi-test-runtime = { workspace = true }
primitive-types = { features = [
"codec",
"num-traits",
"scale-info",
], workspace = true }
sp-tracing = { workspace = true, default-features = true }
xcm-simulator = { workspace = true, default-features = true }
[features]
default = ["std"]
runtime-benchmarks = [
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"pallet-asset-conversion/runtime-benchmarks",
"pallet-assets/runtime-benchmarks",
"pallet-balances/runtime-benchmarks",
"pallet-salary/runtime-benchmarks",
"pallet-transaction-payment/runtime-benchmarks",
"pallet-xcm/runtime-benchmarks",
"pezkuwi-primitives/runtime-benchmarks",
"pezkuwi-runtime-teyrchains/runtime-benchmarks",
"pezkuwi-test-runtime/runtime-benchmarks",
"pezkuwi-teyrchain-primitives/runtime-benchmarks",
"sp-io/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
"xcm-executor/runtime-benchmarks",
"xcm-simulator/runtime-benchmarks",
"xcm/runtime-benchmarks",
]
std = [
"codec/std",
"environmental/std",
"frame-support/std",
"frame-system/std",
"pallet-asset-conversion/std",
"pallet-transaction-payment/std",
"pezkuwi-teyrchain-primitives/std",
"primitive-types/std",
"scale-info/std",
"sp-arithmetic/std",
"sp-core/std",
"sp-io/std",
"sp-runtime/std",
"sp-weights/std",
"tracing/std",
"xcm-executor/std",
"xcm/std",
]
@@ -0,0 +1,350 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
//! Adapters to work with [`frame_support::traits::fungibles`] through XCM.
use core::{marker::PhantomData, result};
use frame_support::traits::{Contains, Get};
use sp_runtime::traits::MaybeEquivalence;
use xcm::latest::prelude::*;
use xcm_executor::traits::{
Error as MatchError, MatchesFungibles, MatchesInstance, MatchesNonFungible, MatchesNonFungibles,
};
/// Converter struct implementing `AssetIdConversion` converting a numeric asset ID (must be
/// `TryFrom/TryInto<u128>`) into a `GeneralIndex` junction, prefixed by some `Location` value.
/// The `Location` value will typically be a `PalletInstance` junction.
pub struct AsPrefixedGeneralIndex<Prefix, AssetId, ConvertAssetId, L = Location>(
PhantomData<(Prefix, AssetId, ConvertAssetId, L)>,
);
impl<
Prefix: Get<L>,
AssetId: Clone,
ConvertAssetId: MaybeEquivalence<u128, AssetId>,
L: TryInto<Location> + TryFrom<Location> + Clone,
> MaybeEquivalence<L, AssetId> for AsPrefixedGeneralIndex<Prefix, AssetId, ConvertAssetId, L>
{
fn convert(id: &L) -> Option<AssetId> {
let prefix = Prefix::get();
let latest_prefix: Location = prefix.try_into().ok()?;
let latest_id: Location = (*id).clone().try_into().ok()?;
if latest_prefix.parent_count() != latest_id.parent_count() ||
latest_prefix
.interior()
.iter()
.enumerate()
.any(|(index, junction)| latest_id.interior().at(index) != Some(junction))
{
return None;
}
match latest_id.interior().at(latest_prefix.interior().len()) {
Some(Junction::GeneralIndex(id)) => ConvertAssetId::convert(&id),
_ => None,
}
}
fn convert_back(what: &AssetId) -> Option<L> {
let location = Prefix::get();
let mut latest_location: Location = location.try_into().ok()?;
let id = ConvertAssetId::convert_back(what)?;
latest_location.push_interior(Junction::GeneralIndex(id)).ok()?;
latest_location.try_into().ok()
}
}
pub struct ConvertedConcreteId<AssetId, Balance, ConvertAssetId, ConvertOther>(
PhantomData<(AssetId, Balance, ConvertAssetId, ConvertOther)>,
);
impl<
AssetId: Clone,
Balance: Clone,
ConvertAssetId: MaybeEquivalence<Location, AssetId>,
ConvertBalance: MaybeEquivalence<u128, Balance>,
> MatchesFungibles<AssetId, Balance>
for ConvertedConcreteId<AssetId, Balance, ConvertAssetId, ConvertBalance>
{
fn matches_fungibles(a: &Asset) -> result::Result<(AssetId, Balance), MatchError> {
let (amount, id) = match (&a.fun, &a.id) {
(Fungible(ref amount), AssetId(ref id)) => (amount, id),
_ => return Err(MatchError::AssetNotHandled),
};
let what = ConvertAssetId::convert(id).ok_or(MatchError::AssetIdConversionFailed)?;
let amount =
ConvertBalance::convert(amount).ok_or(MatchError::AmountToBalanceConversionFailed)?;
Ok((what, amount))
}
}
impl<
ClassId: Clone,
InstanceId: Clone,
ConvertClassId: MaybeEquivalence<Location, ClassId>,
ConvertInstanceId: MaybeEquivalence<AssetInstance, InstanceId>,
> MatchesNonFungibles<ClassId, InstanceId>
for ConvertedConcreteId<ClassId, InstanceId, ConvertClassId, ConvertInstanceId>
{
fn matches_nonfungibles(a: &Asset) -> result::Result<(ClassId, InstanceId), MatchError> {
let (instance, class) = match (&a.fun, &a.id) {
(NonFungible(ref instance), AssetId(ref class)) => (instance, class),
_ => return Err(MatchError::AssetNotHandled),
};
let what = ConvertClassId::convert(class).ok_or(MatchError::AssetIdConversionFailed)?;
let instance =
ConvertInstanceId::convert(instance).ok_or(MatchError::InstanceConversionFailed)?;
Ok((what, instance))
}
}
#[deprecated = "Use `ConvertedConcreteId` instead"]
pub type ConvertedConcreteAssetId<A, B, C, O> = ConvertedConcreteId<A, B, C, O>;
pub struct MatchedConvertedConcreteId<AssetId, Balance, MatchAssetId, ConvertAssetId, ConvertOther>(
PhantomData<(AssetId, Balance, MatchAssetId, ConvertAssetId, ConvertOther)>,
);
impl<
AssetId: Clone,
Balance: Clone,
MatchAssetId: Contains<Location>,
ConvertAssetId: MaybeEquivalence<Location, AssetId>,
ConvertBalance: MaybeEquivalence<u128, Balance>,
> MatchesFungibles<AssetId, Balance>
for MatchedConvertedConcreteId<AssetId, Balance, MatchAssetId, ConvertAssetId, ConvertBalance>
{
fn matches_fungibles(a: &Asset) -> result::Result<(AssetId, Balance), MatchError> {
let (amount, id) = match (&a.fun, &a.id) {
(Fungible(ref amount), AssetId(ref id)) if MatchAssetId::contains(id) => (amount, id),
_ => return Err(MatchError::AssetNotHandled),
};
let what = ConvertAssetId::convert(id).ok_or(MatchError::AssetIdConversionFailed)?;
let amount =
ConvertBalance::convert(amount).ok_or(MatchError::AmountToBalanceConversionFailed)?;
Ok((what, amount))
}
}
impl<
ClassId: Clone,
InstanceId: Clone,
MatchClassId: Contains<Location>,
ConvertClassId: MaybeEquivalence<Location, ClassId>,
ConvertInstanceId: MaybeEquivalence<AssetInstance, InstanceId>,
> MatchesNonFungibles<ClassId, InstanceId>
for MatchedConvertedConcreteId<
ClassId,
InstanceId,
MatchClassId,
ConvertClassId,
ConvertInstanceId,
>
{
fn matches_nonfungibles(a: &Asset) -> result::Result<(ClassId, InstanceId), MatchError> {
let (instance, class) = match (&a.fun, &a.id) {
(NonFungible(ref instance), AssetId(ref class)) if MatchClassId::contains(class) =>
(instance, class),
_ => return Err(MatchError::AssetNotHandled),
};
let what = ConvertClassId::convert(class).ok_or(MatchError::AssetIdConversionFailed)?;
let instance =
ConvertInstanceId::convert(instance).ok_or(MatchError::InstanceConversionFailed)?;
Ok((what, instance))
}
}
/// An adapter that implements the unified unique instances matcher [`MatchesInstance`] trait
/// for the [`MatchesNonFungibles`].
/// The resulting matcher expects the instances to be part of some class (i.e., instance group,
/// such as an NFT collection).
///
/// * `ClassId` is the ID of an instance class (e.g., NFT collection ID),
/// * `InstanceId` is a class-scoped ID of a class member's unique instance (e.g., an NFT ID inside
/// a collection).
pub struct MatchInClassInstances<Matcher>(PhantomData<Matcher>);
impl<ClassId, InstanceId, Matcher: MatchesNonFungibles<ClassId, InstanceId>>
MatchesInstance<(ClassId, InstanceId)> for MatchInClassInstances<Matcher>
{
fn matches_instance(a: &Asset) -> result::Result<(ClassId, InstanceId), MatchError> {
Matcher::matches_nonfungibles(a)
}
}
/// An adapter that implements the unified unique instances matcher [`MatchesInstance`] trait
/// for the [`MatchesNonFungible`].
/// The resulting matcher expects the instances to be fully individual, not belonging to any group
/// (such as an NFT collection).
///
/// In practice, this typically means that the `InstanceId` is an indivisible ID (i.e., it is not
/// composed of multiple IDs).
pub struct MatchClasslessInstances<Matcher>(PhantomData<Matcher>);
impl<InstanceId, Matcher: MatchesNonFungible<InstanceId>> MatchesInstance<InstanceId>
for MatchClasslessInstances<Matcher>
{
fn matches_instance(a: &Asset) -> result::Result<InstanceId, MatchError> {
Matcher::matches_nonfungible(a).ok_or(MatchError::AssetNotHandled)
}
}
#[cfg(test)]
mod tests {
use super::*;
use xcm_executor::traits::JustTry;
struct OnlyParentZero;
impl Contains<Location> for OnlyParentZero {
fn contains(a: &Location) -> bool {
match a {
Location { parents: 0, .. } => true,
_ => false,
}
}
}
#[test]
fn matched_converted_concrete_id_for_fungibles_works() {
type AssetIdForTrustBackedAssets = u32;
type Balance = u128;
frame_support::parameter_types! {
pub TrustBackedAssetsPalletLocation: Location = PalletInstance(50).into();
}
// ConvertedConcreteId cfg
type Converter = MatchedConvertedConcreteId<
AssetIdForTrustBackedAssets,
Balance,
OnlyParentZero,
AsPrefixedGeneralIndex<
TrustBackedAssetsPalletLocation,
AssetIdForTrustBackedAssets,
JustTry,
>,
JustTry,
>;
assert_eq!(
TrustBackedAssetsPalletLocation::get(),
Location { parents: 0, interior: [PalletInstance(50)].into() }
);
// err - does not match
assert_eq!(
Converter::matches_fungibles(&Asset {
id: AssetId(Location::new(1, [PalletInstance(50), GeneralIndex(1)])),
fun: Fungible(12345),
}),
Err(MatchError::AssetNotHandled)
);
// err - matches, but convert fails
assert_eq!(
Converter::matches_fungibles(&Asset {
id: AssetId(Location::new(
0,
[PalletInstance(50), GeneralKey { length: 1, data: [1; 32] }]
)),
fun: Fungible(12345),
}),
Err(MatchError::AssetIdConversionFailed)
);
// err - matches, but NonFungible
assert_eq!(
Converter::matches_fungibles(&Asset {
id: AssetId(Location::new(0, [PalletInstance(50), GeneralIndex(1)])),
fun: NonFungible(Index(54321)),
}),
Err(MatchError::AssetNotHandled)
);
// ok
assert_eq!(
Converter::matches_fungibles(&Asset {
id: AssetId(Location::new(0, [PalletInstance(50), GeneralIndex(1)])),
fun: Fungible(12345),
}),
Ok((1, 12345))
);
}
#[test]
fn matched_converted_concrete_id_for_nonfungibles_works() {
type ClassId = u32;
type ClassInstanceId = u64;
frame_support::parameter_types! {
pub TrustBackedAssetsPalletLocation: Location = PalletInstance(50).into();
}
// ConvertedConcreteId cfg
struct ClassInstanceIdConverter;
impl MaybeEquivalence<AssetInstance, ClassInstanceId> for ClassInstanceIdConverter {
fn convert(value: &AssetInstance) -> Option<ClassInstanceId> {
(*value).try_into().ok()
}
fn convert_back(value: &ClassInstanceId) -> Option<AssetInstance> {
Some(AssetInstance::from(*value))
}
}
type Converter = MatchedConvertedConcreteId<
ClassId,
ClassInstanceId,
OnlyParentZero,
AsPrefixedGeneralIndex<TrustBackedAssetsPalletLocation, ClassId, JustTry>,
ClassInstanceIdConverter,
>;
assert_eq!(
TrustBackedAssetsPalletLocation::get(),
Location { parents: 0, interior: [PalletInstance(50)].into() }
);
// err - does not match
assert_eq!(
Converter::matches_nonfungibles(&Asset {
id: AssetId(Location::new(1, [PalletInstance(50), GeneralIndex(1)])),
fun: NonFungible(Index(54321)),
}),
Err(MatchError::AssetNotHandled)
);
// err - matches, but convert fails
assert_eq!(
Converter::matches_nonfungibles(&Asset {
id: AssetId(Location::new(
0,
[PalletInstance(50), GeneralKey { length: 1, data: [1; 32] }]
)),
fun: NonFungible(Index(54321)),
}),
Err(MatchError::AssetIdConversionFailed)
);
// err - matches, but Fungible vs NonFungible
assert_eq!(
Converter::matches_nonfungibles(&Asset {
id: AssetId(Location::new(0, [PalletInstance(50), GeneralIndex(1)])),
fun: Fungible(12345),
}),
Err(MatchError::AssetNotHandled)
);
// ok
assert_eq!(
Converter::matches_nonfungibles(&Asset {
id: AssetId(Location::new(0, [PalletInstance(50), GeneralIndex(1)])),
fun: NonFungible(Index(54321)),
}),
Ok((1, 54321))
);
}
}
@@ -0,0 +1,22 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
//! Adapters for the AssetExchanger config item.
//!
//! E.g. types that implement the [`xcm_executor::traits::AssetExchange`] trait.
mod single_asset_adapter;
pub use single_asset_adapter::SingleAssetExchangeAdapter;
@@ -0,0 +1,210 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
//! Single asset exchange adapter.
extern crate alloc;
use alloc::vec;
use core::marker::PhantomData;
use frame_support::{ensure, traits::tokens::fungibles};
use pallet_asset_conversion::{QuotePrice, SwapCredit};
use xcm::prelude::*;
use xcm_executor::{
traits::{AssetExchange, MatchesFungibles},
AssetsInHolding,
};
/// An adapter from [`pallet_asset_conversion::SwapCredit`] and
/// [`pallet_asset_conversion::QuotePrice`] to [`xcm_executor::traits::AssetExchange`].
///
/// This adapter takes just one fungible asset in `give` and allows only one fungible asset in
/// `want`. If you need to handle more assets in either `give` or `want`, then you should use
/// another type that implements [`xcm_executor::traits::AssetExchange`] or build your own.
///
/// This adapter also only works for fungible assets.
///
/// `exchange_asset` and `quote_exchange_price` will both return an error if there's
/// more than one asset in `give` or `want`.
pub struct SingleAssetExchangeAdapter<AssetConversion, Fungibles, Matcher, AccountId>(
PhantomData<(AssetConversion, Fungibles, Matcher, AccountId)>,
);
impl<AssetConversion, Fungibles, Matcher, AccountId> AssetExchange
for SingleAssetExchangeAdapter<AssetConversion, Fungibles, Matcher, AccountId>
where
AssetConversion: SwapCredit<
AccountId,
Balance = u128,
AssetKind = Fungibles::AssetId,
Credit = fungibles::Credit<AccountId, Fungibles>,
> + QuotePrice<Balance = u128, AssetKind = Fungibles::AssetId>,
Fungibles: fungibles::Balanced<AccountId, Balance = u128>,
Matcher: MatchesFungibles<Fungibles::AssetId, Fungibles::Balance>,
{
fn exchange_asset(
_: Option<&Location>,
give: AssetsInHolding,
want: &Assets,
maximal: bool,
) -> Result<AssetsInHolding, AssetsInHolding> {
let mut give_iter = give.fungible_assets_iter();
let give_asset = give_iter.next().ok_or_else(|| {
tracing::trace!(
target: "xcm::SingleAssetExchangeAdapter::exchange_asset",
?give, "No fungible asset was in `give`.",
);
give.clone()
})?;
ensure!(give_iter.next().is_none(), give.clone()); // We only support 1 asset in `give`.
ensure!(give.non_fungible_assets_iter().next().is_none(), give.clone()); // We don't allow non-fungible assets.
ensure!(want.len() == 1, give.clone()); // We only support 1 asset in `want`.
let want_asset = want.get(0).ok_or_else(|| give.clone())?;
let (give_asset_id, give_amount) =
Matcher::matches_fungibles(&give_asset).map_err(|error| {
tracing::trace!(
target: "xcm::SingleAssetExchangeAdapter::exchange_asset",
?give_asset,
?error,
"Could not map XCM asset give to FRAME asset.",
);
give.clone()
})?;
let (want_asset_id, want_amount) =
Matcher::matches_fungibles(&want_asset).map_err(|error| {
tracing::trace!(
target: "xcm::SingleAssetExchangeAdapter::exchange_asset",
?want_asset,
?error,
"Could not map XCM asset want to FRAME asset."
);
give.clone()
})?;
// We have to do this to convert the XCM assets into credit the pool can use.
let swap_asset = give_asset_id.clone().into();
let credit_in = Fungibles::issue(give_asset_id, give_amount);
// Do the swap.
let (credit_out, maybe_credit_change) = if maximal {
// If `maximal`, then we swap exactly `credit_in` to get as much of `want_asset_id` as
// we can, with a minimum of `want_amount`.
let credit_out = <AssetConversion as SwapCredit<_>>::swap_exact_tokens_for_tokens(
vec![swap_asset, want_asset_id],
credit_in,
Some(want_amount),
)
.map_err(|(credit_in, error)| {
tracing::debug!(
target: "xcm::SingleAssetExchangeAdapter::exchange_asset",
?error,
"Could not perform the swap"
);
drop(credit_in);
give.clone()
})?;
// We don't have leftover assets if exchange was maximal.
(credit_out, None)
} else {
// If `minimal`, then we swap as little of `credit_in` as we can to get exactly
// `want_amount` of `want_asset_id`.
let (credit_out, credit_change) =
<AssetConversion as SwapCredit<_>>::swap_tokens_for_exact_tokens(
vec![swap_asset, want_asset_id],
credit_in,
want_amount,
)
.map_err(|(credit_in, error)| {
tracing::debug!(
target: "xcm::SingleAssetExchangeAdapter::exchange_asset",
?error,
"Could not perform the swap",
);
drop(credit_in);
give.clone()
})?;
(credit_out, if credit_change.peek() > 0 { Some(credit_change) } else { None })
};
// We create an `AssetsInHolding` instance by putting in the resulting asset
// of the exchange.
let resulting_asset: Asset = (want_asset.id.clone(), credit_out.peek()).into();
let mut result: AssetsInHolding = resulting_asset.into();
// If we have some leftover assets from the exchange, also put them in the result.
if let Some(credit_change) = maybe_credit_change {
let leftover_asset: Asset = (give_asset.id.clone(), credit_change.peek()).into();
result.subsume(leftover_asset);
}
Ok(result.into())
}
fn quote_exchange_price(give: &Assets, want: &Assets, maximal: bool) -> Option<Assets> {
if give.len() != 1 || want.len() != 1 {
return None;
} // We only support 1 asset in `give` or `want`.
let give_asset = give.get(0)?;
let want_asset = want.get(0)?;
// We first match both XCM assets to the asset ID types `AssetConversion` can handle.
let (give_asset_id, give_amount) = Matcher::matches_fungibles(give_asset)
.map_err(|error| {
tracing::trace!(
target: "xcm::SingleAssetExchangeAdapter::quote_exchange_price",
?give_asset,
?error,
"Could not map XCM asset to FRAME asset."
);
()
})
.ok()?;
let (want_asset_id, want_amount) = Matcher::matches_fungibles(want_asset)
.map_err(|error| {
tracing::trace!(
target: "xcm::SingleAssetExchangeAdapter::quote_exchange_price",
?want_asset,
?error,
"Could not map XCM asset to FRAME asset"
);
()
})
.ok()?;
// We quote the price.
if maximal {
// The amount of `want` resulting from swapping `give`.
let resulting_want =
<AssetConversion as QuotePrice>::quote_price_exact_tokens_for_tokens(
give_asset_id,
want_asset_id,
give_amount,
true, // Include fee.
)?;
Some((want_asset.id.clone(), resulting_want).into())
} else {
// The `give` amount required to obtain `want`.
let necessary_give =
<AssetConversion as QuotePrice>::quote_price_tokens_for_exact_tokens(
give_asset_id,
want_asset_id,
want_amount,
true, // Include fee.
)?;
Some((give_asset.id.clone(), necessary_give).into())
}
}
}
@@ -0,0 +1,379 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
//! Mock to test [`SingleAssetExchangeAdapter`].
use core::marker::PhantomData;
use frame_support::{
assert_ok, construct_runtime, derive_impl, ord_parameter_types, parameter_types,
traits::{
fungible::{self, NativeFromLeft, NativeOrWithId},
fungibles::Mutate,
tokens::imbalance::ResolveAssetTo,
AsEnsureOriginWithArg, Disabled, Equals, Everything, Nothing, OriginTrait,
PalletInfoAccess,
},
PalletId,
};
use sp_core::{ConstU128, ConstU32, Get};
use sp_runtime::{
traits::{AccountIdConversion, IdentityLookup, MaybeEquivalence, TryConvert, TryConvertInto},
BuildStorage, Permill,
};
use xcm::prelude::*;
use xcm_executor::{traits::ConvertLocation, XcmExecutor};
use crate::{FungibleAdapter, IsConcrete, MatchedConvertedConcreteId, StartsWith};
pub type Block = frame_system::mocking::MockBlock<Runtime>;
pub type AccountId = u64;
pub type Balance = u128;
construct_runtime! {
pub struct Runtime {
System: frame_system,
Balances: pallet_balances,
AssetsPallet: pallet_assets::<Instance1>,
PoolAssets: pallet_assets::<Instance2>,
XcmPallet: pallet_xcm,
AssetConversion: pallet_asset_conversion,
}
}
#[derive_impl(frame_system::config_preludes::TestDefaultConfig)]
impl frame_system::Config for Runtime {
type Block = Block;
type AccountId = AccountId;
type Lookup = IdentityLookup<AccountId>;
type AccountData = pallet_balances::AccountData<Balance>;
}
#[derive_impl(pallet_balances::config_preludes::TestDefaultConfig)]
impl pallet_balances::Config for Runtime {
type Balance = Balance;
type AccountStore = System;
type ExistentialDeposit = ConstU128<1>;
}
pub type TrustBackedAssetsInstance = pallet_assets::Instance1;
pub type PoolAssetsInstance = pallet_assets::Instance2;
#[derive_impl(pallet_assets::config_preludes::TestDefaultConfig)]
impl pallet_assets::Config<TrustBackedAssetsInstance> for Runtime {
type Currency = Balances;
type Balance = Balance;
type AssetDeposit = ConstU128<1>;
type AssetAccountDeposit = ConstU128<10>;
type MetadataDepositBase = ConstU128<1>;
type MetadataDepositPerByte = ConstU128<1>;
type ApprovalDeposit = ConstU128<1>;
type CreateOrigin = AsEnsureOriginWithArg<frame_system::EnsureSigned<AccountId>>;
type ForceOrigin = frame_system::EnsureRoot<AccountId>;
type Freezer = ();
type Holder = ();
type CallbackHandle = ();
}
#[derive_impl(pallet_assets::config_preludes::TestDefaultConfig)]
impl pallet_assets::Config<PoolAssetsInstance> for Runtime {
type Currency = Balances;
type Balance = Balance;
type AssetDeposit = ConstU128<1>;
type AssetAccountDeposit = ConstU128<10>;
type MetadataDepositBase = ConstU128<1>;
type MetadataDepositPerByte = ConstU128<1>;
type ApprovalDeposit = ConstU128<1>;
type CreateOrigin = AsEnsureOriginWithArg<frame_system::EnsureSigned<AccountId>>;
type ForceOrigin = frame_system::EnsureRoot<AccountId>;
type Freezer = ();
type Holder = ();
type CallbackHandle = ();
}
/// Union fungibles implementation for `Assets` and `Balances`.
pub type NativeAndAssets =
fungible::UnionOf<Balances, AssetsPallet, NativeFromLeft, NativeOrWithId<u32>, AccountId>;
parameter_types! {
pub const AssetConversionPalletId: PalletId = PalletId(*b"py/ascon");
pub const Native: NativeOrWithId<u32> = NativeOrWithId::Native;
pub const LiquidityWithdrawalFee: Permill = Permill::from_percent(0);
}
ord_parameter_types! {
pub const AssetConversionOrigin: AccountId =
AccountIdConversion::<AccountId>::into_account_truncating(&AssetConversionPalletId::get());
}
pub type PoolIdToAccountId = pallet_asset_conversion::AccountIdConverter<
AssetConversionPalletId,
(NativeOrWithId<u32>, NativeOrWithId<u32>),
>;
impl pallet_asset_conversion::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type Balance = Balance;
type HigherPrecisionBalance = sp_core::U256;
type AssetKind = NativeOrWithId<u32>;
type Assets = NativeAndAssets;
type PoolId = (Self::AssetKind, Self::AssetKind);
type PoolLocator = pallet_asset_conversion::WithFirstAsset<
Native,
AccountId,
Self::AssetKind,
PoolIdToAccountId,
>;
type PoolAssetId = u32;
type PoolAssets = PoolAssets;
type PoolSetupFee = ConstU128<100>; // Asset class deposit fees are sufficient to prevent spam
type PoolSetupFeeAsset = Native;
type PoolSetupFeeTarget = ResolveAssetTo<AssetConversionOrigin, Self::Assets>;
type LiquidityWithdrawalFee = LiquidityWithdrawalFee;
type LPFee = ConstU32<3>;
type PalletId = AssetConversionPalletId;
type MaxSwapPathLength = ConstU32<3>;
type MintMinLiquidity = ConstU128<100>;
type WeightInfo = ();
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = ();
}
/// We only alias local accounts.
pub type LocationToAccountId = AccountIndex64Aliases;
parameter_types! {
pub HereLocation: Location = Here.into_location();
pub WeightPerInstruction: Weight = Weight::from_parts(1, 1);
pub MaxInstructions: u32 = 100;
pub UniversalLocation: InteriorLocation = [GlobalConsensus(Pezkuwi), Teyrchain(1000)].into();
pub TrustBackedAssetsPalletIndex: u8 = <AssetsPallet as PalletInfoAccess>::index() as u8;
pub TrustBackedAssetsPalletLocation: Location = PalletInstance(TrustBackedAssetsPalletIndex::get()).into();
}
/// Adapter for the native token.
pub type FungibleTransactor = FungibleAdapter<
// Use this implementation of the `fungible::*` traits.
// `Balances` is the name given to the balances pallet
Balances,
// This transactor deals with the native token.
IsConcrete<HereLocation>,
// How to convert an XCM Location into a local account id.
// This is also something that's configured in the XCM executor.
LocationToAccountId,
// The type for account ids, only needed because `fungible` is generic over it.
AccountId,
// Not tracking teleports.
(),
>;
pub type Weigher = crate::FixedWeightBounds<WeightPerInstruction, RuntimeCall, MaxInstructions>;
pub struct LocationToAssetId;
impl MaybeEquivalence<Location, NativeOrWithId<u32>> for LocationToAssetId {
fn convert(location: &Location) -> Option<NativeOrWithId<u32>> {
let pallet_instance = TrustBackedAssetsPalletIndex::get();
match location.unpack() {
(0, [PalletInstance(instance), GeneralIndex(index)])
if *instance == pallet_instance =>
Some(NativeOrWithId::WithId(*index as u32)),
(0, []) => Some(NativeOrWithId::Native),
_ => None,
}
}
fn convert_back(asset_id: &NativeOrWithId<u32>) -> Option<Location> {
let pallet_instance = TrustBackedAssetsPalletIndex::get();
Some(match asset_id {
NativeOrWithId::WithId(id) =>
Location::new(0, [PalletInstance(pallet_instance), GeneralIndex((*id).into())]),
NativeOrWithId::Native => Location::new(0, []),
})
}
}
pub type PoolAssetsExchanger = crate::SingleAssetExchangeAdapter<
AssetConversion,
NativeAndAssets,
MatchedConvertedConcreteId<
NativeOrWithId<u32>,
Balance,
(StartsWith<TrustBackedAssetsPalletLocation>, Equals<HereLocation>),
LocationToAssetId,
TryConvertInto,
>,
AccountId,
>;
pub struct XcmConfig;
impl xcm_executor::Config for XcmConfig {
type RuntimeCall = RuntimeCall;
type XcmSender = ();
type XcmEventEmitter = ();
type AssetTransactor = FungibleTransactor;
type OriginConverter = ();
type IsReserve = ();
type IsTeleporter = ();
type UniversalLocation = UniversalLocation;
// This is not safe, you should use `crate::AllowTopLevelPaidExecutionFrom<T>` in a
// production chain
type Barrier = crate::AllowUnpaidExecutionFrom<Everything>;
type Weigher = Weigher;
type Trader = ();
type ResponseHandler = ();
type AssetTrap = ();
type AssetLocker = ();
type AssetExchanger = PoolAssetsExchanger;
type AssetClaims = ();
type SubscriptionService = ();
type PalletInstancesInfo = ();
type FeeManager = ();
type MaxAssetsIntoHolding = ConstU32<1>;
type MessageExporter = ();
type UniversalAliases = Nothing;
type CallDispatcher = RuntimeCall;
type SafeCallFilter = Everything;
type Aliasers = Nothing;
type TransactionalProcessor = crate::FrameTransactionalProcessor;
type HrmpNewChannelOpenRequestHandler = ();
type HrmpChannelAcceptedHandler = ();
type HrmpChannelClosingHandler = ();
type XcmRecorder = ();
}
/// Simple converter from a [`Location`] with an [`AccountIndex64`] junction and no parent to a
/// `u64`.
pub struct AccountIndex64Aliases;
impl ConvertLocation<AccountId> for AccountIndex64Aliases {
fn convert_location(location: &Location) -> Option<AccountId> {
let index = match location.unpack() {
(0, [AccountIndex64 { index, network: None }]) => index,
_ => return None,
};
Some((*index).into())
}
}
/// `Convert` implementation to convert from some a `Signed` (system) `Origin` into an
/// `AccountIndex64`.
///
/// Typically used when configuring `pallet-xcm` in tests to allow `u64` accounts to dispatch an XCM
/// from an `AccountIndex64` origin.
pub struct SignedToAccountIndex64<RuntimeOrigin, AccountId, Network>(
PhantomData<(RuntimeOrigin, AccountId, Network)>,
);
impl<RuntimeOrigin: OriginTrait + Clone, AccountId: Into<u64>, Network: Get<Option<NetworkId>>>
TryConvert<RuntimeOrigin, Location> for SignedToAccountIndex64<RuntimeOrigin, AccountId, Network>
where
RuntimeOrigin::PalletsOrigin: From<frame_system::RawOrigin<AccountId>>
+ TryInto<frame_system::RawOrigin<AccountId>, Error = RuntimeOrigin::PalletsOrigin>,
{
fn try_convert(o: RuntimeOrigin) -> Result<Location, RuntimeOrigin> {
o.try_with_caller(|caller| match caller.try_into() {
Ok(frame_system::RawOrigin::Signed(who)) =>
Ok(Junction::AccountIndex64 { network: Network::get(), index: who.into() }.into()),
Ok(other) => Err(other.into()),
Err(other) => Err(other),
})
}
}
parameter_types! {
pub const NoNetwork: Option<NetworkId> = None;
}
/// Converts a local signed origin into an XCM location. Forms the basis for local origins
/// sending/executing XCMs.
pub type LocalOriginToLocation = SignedToAccountIndex64<RuntimeOrigin, AccountId, NoNetwork>;
impl pallet_xcm::Config for Runtime {
// We turn off sending for these tests
type SendXcmOrigin = crate::EnsureXcmOrigin<RuntimeOrigin, ()>;
type XcmRouter = ();
// Anyone can execute XCM programs
type ExecuteXcmOrigin = crate::EnsureXcmOrigin<RuntimeOrigin, LocalOriginToLocation>;
// We execute any type of program
type XcmExecuteFilter = Everything;
// How we execute programs
type XcmExecutor = XcmExecutor<XcmConfig>;
// We don't allow teleports
type XcmTeleportFilter = Nothing;
// We don't allow reserve transfers
type XcmReserveTransferFilter = Nothing;
// Same weigher executor uses to weigh XCM programs
type Weigher = Weigher;
// Same universal location
type UniversalLocation = UniversalLocation;
// No version discovery needed
const VERSION_DISCOVERY_QUEUE_SIZE: u32 = 0;
type AdvertisedXcmVersion = pallet_xcm::CurrentXcmVersion;
type AdminOrigin = frame_system::EnsureRoot<AccountId>;
// No locking
type TrustedLockers = ();
type MaxLockers = frame_support::traits::ConstU32<0>;
type MaxRemoteLockConsumers = frame_support::traits::ConstU32<0>;
type RemoteLockConsumerIdentifier = ();
// How to turn locations into accounts
type SovereignAccountOf = LocationToAccountId;
// A currency to pay for things and its matcher, we are using the relay token
type Currency = Balances;
type CurrencyMatcher = crate::IsConcrete<HereLocation>;
// Pallet benchmarks, no need for this recipe
type WeightInfo = pallet_xcm::TestWeightInfo;
// Runtime types
type RuntimeOrigin = RuntimeOrigin;
type RuntimeCall = RuntimeCall;
type RuntimeEvent = RuntimeEvent;
// Aliasing is disabled: xcm_executor::Config::Aliasers is set to `Nothing`.
type AuthorizedAliasConsideration = Disabled;
}
pub const INITIAL_BALANCE: Balance = 1_000_000_000;
pub fn new_test_ext() -> sp_io::TestExternalities {
let mut t = frame_system::GenesisConfig::<Runtime>::default().build_storage().unwrap();
pallet_balances::GenesisConfig::<Runtime> {
balances: vec![(0, INITIAL_BALANCE), (1, INITIAL_BALANCE), (2, INITIAL_BALANCE)],
..Default::default()
}
.assimilate_storage(&mut t)
.unwrap();
let owner = 0;
let mut ext = sp_io::TestExternalities::new(t);
ext.execute_with(|| {
System::set_block_number(1);
assert_ok!(AssetsPallet::force_create(RuntimeOrigin::root(), 1, owner, false, 1,));
assert_ok!(AssetsPallet::mint_into(1, &owner, INITIAL_BALANCE,));
assert_ok!(AssetConversion::create_pool(
RuntimeOrigin::signed(owner),
Box::new(NativeOrWithId::Native),
Box::new(NativeOrWithId::WithId(1)),
));
assert_ok!(AssetConversion::add_liquidity(
RuntimeOrigin::signed(owner),
Box::new(NativeOrWithId::Native),
Box::new(NativeOrWithId::WithId(1)),
50_000_000,
100_000_000,
0,
0,
owner,
));
});
ext
}
@@ -0,0 +1,25 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
//! SingleAssetExchangeAdapter.
mod adapter;
pub use adapter::SingleAssetExchangeAdapter;
#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;
@@ -0,0 +1,233 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
//! Tests for the [`SingleAssetExchangeAdapter`] type.
use super::mock::*;
use xcm::prelude::*;
use xcm_executor::{traits::AssetExchange, AssetsInHolding};
// ========== Happy path ==========
/// Scenario:
/// Account #3 wants to use the local liquidity pool between two custom assets,
/// 1 and 2.
#[test]
fn maximal_exchange() {
new_test_ext().execute_with(|| {
let assets = PoolAssetsExchanger::exchange_asset(
None,
vec![([PalletInstance(2), GeneralIndex(1)], 10_000_000).into()].into(),
&vec![(Here, 2_000_000).into()].into(),
true, // Maximal
)
.unwrap();
let amount = get_amount_from_first_fungible(&assets);
assert_eq!(amount, 4_533_054);
});
}
#[test]
fn minimal_exchange() {
new_test_ext().execute_with(|| {
let assets = PoolAssetsExchanger::exchange_asset(
None,
vec![([PalletInstance(2), GeneralIndex(1)], 10_000_000).into()].into(),
&vec![(Here, 2_000_000).into()].into(),
false, // Minimal
)
.unwrap();
let (first_amount, second_amount) = get_amount_from_fungibles(&assets);
assert_eq!(first_amount, 2_000_000);
assert_eq!(second_amount, 5_820_795);
});
}
#[test]
fn maximal_quote() {
new_test_ext().execute_with(|| {
let assets = quote(
&([PalletInstance(2), GeneralIndex(1)], 10_000_000).into(),
&(Here, 2_000_000).into(),
true,
)
.unwrap();
let amount = get_amount_from_first_fungible(&assets.into());
// The amount of the native token resulting from swapping all `10_000_000` of the custom
// token.
assert_eq!(amount, 4_533_054);
});
}
#[test]
fn minimal_quote() {
new_test_ext().execute_with(|| {
let assets = quote(
&([PalletInstance(2), GeneralIndex(1)], 10_000_000).into(),
&(Here, 2_000_000).into(),
false,
)
.unwrap();
let amount = get_amount_from_first_fungible(&assets.into());
// The amount of the custom token needed to get `2_000_000` of the native token.
assert_eq!(amount, 4_179_205);
});
}
// ========== Unhappy path ==========
#[test]
fn no_asset_in_give() {
new_test_ext().execute_with(|| {
assert!(PoolAssetsExchanger::exchange_asset(
None,
vec![].into(),
&vec![(Here, 2_000_000).into()].into(),
true
)
.is_err());
});
}
#[test]
fn more_than_one_asset_in_give() {
new_test_ext().execute_with(|| {
assert!(PoolAssetsExchanger::exchange_asset(
None,
vec![([PalletInstance(2), GeneralIndex(1)], 1).into(), (Here, 2).into()].into(),
&vec![(Here, 2_000_000).into()].into(),
true
)
.is_err());
});
}
#[test]
fn no_asset_in_want() {
new_test_ext().execute_with(|| {
assert!(PoolAssetsExchanger::exchange_asset(
None,
vec![([PalletInstance(2), GeneralIndex(1)], 10_000_000).into()].into(),
&vec![].into(),
true
)
.is_err());
});
}
#[test]
fn more_than_one_asset_in_want() {
new_test_ext().execute_with(|| {
assert!(PoolAssetsExchanger::exchange_asset(
None,
vec![([PalletInstance(2), GeneralIndex(1)], 10_000_000).into()].into(),
&vec![(Here, 2_000_000).into(), ([PalletInstance(2), GeneralIndex(1)], 1).into()]
.into(),
true
)
.is_err());
});
}
#[test]
fn give_asset_does_not_match() {
new_test_ext().execute_with(|| {
let nonexistent_asset_id = 1000;
assert!(PoolAssetsExchanger::exchange_asset(
None,
vec![([PalletInstance(2), GeneralIndex(nonexistent_asset_id)], 10_000_000).into()]
.into(),
&vec![(Here, 2_000_000).into()].into(),
true
)
.is_err());
});
}
#[test]
fn want_asset_does_not_match() {
new_test_ext().execute_with(|| {
let nonexistent_asset_id = 1000;
assert!(PoolAssetsExchanger::exchange_asset(
None,
vec![(Here, 2_000_000).into()].into(),
&vec![([PalletInstance(2), GeneralIndex(nonexistent_asset_id)], 10_000_000).into()]
.into(),
true
)
.is_err());
});
}
#[test]
fn exchange_fails() {
new_test_ext().execute_with(|| {
assert!(PoolAssetsExchanger::exchange_asset(
None,
vec![([PalletInstance(2), GeneralIndex(1)], 10_000_000).into()].into(),
// We're asking for too much of the native token...
&vec![(Here, 200_000_000).into()].into(),
false, // Minimal
)
.is_err());
});
}
#[test]
fn non_fungible_asset_in_give() {
new_test_ext().execute_with(|| {
assert!(PoolAssetsExchanger::exchange_asset(
None,
// Using `u64` here will give us a non-fungible instead of a fungible.
vec![([PalletInstance(2), GeneralIndex(2)], 10_000_000u64).into()].into(),
&vec![(Here, 10_000_000).into()].into(),
false, // Minimal
)
.is_err());
});
}
// ========== Helper functions ==========
fn get_amount_from_first_fungible(assets: &AssetsInHolding) -> u128 {
let mut fungibles_iter = assets.fungible_assets_iter();
let first_fungible = fungibles_iter.next().unwrap();
let Fungible(amount) = first_fungible.fun else {
unreachable!("Asset should be fungible");
};
amount
}
fn get_amount_from_fungibles(assets: &AssetsInHolding) -> (u128, u128) {
let mut fungibles_iter = assets.fungible_assets_iter();
let first_fungible = fungibles_iter.next().unwrap();
let Fungible(first_amount) = first_fungible.fun else {
unreachable!("Asset should be fungible");
};
let second_fungible = fungibles_iter.next().unwrap();
let Fungible(second_amount) = second_fungible.fun else {
unreachable!("Asset should be fungible");
};
(first_amount, second_amount)
}
fn quote(asset_1: &Asset, asset_2: &Asset, maximal: bool) -> Option<Assets> {
PoolAssetsExchanger::quote_exchange_price(
&asset_1.clone().into(),
&asset_2.clone().into(),
maximal,
)
}
+680
View File
@@ -0,0 +1,680 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
//! Various implementations for `ShouldExecute`.
use crate::{CreateMatcher, MatchXcm};
use core::{cell::Cell, marker::PhantomData, ops::ControlFlow, result::Result};
use frame_support::{
ensure,
traits::{Contains, ContainsPair, Get, Nothing, ProcessMessageError},
};
use pezkuwi_teyrchain_primitives::primitives::IsSystem;
use xcm::prelude::*;
use xcm_executor::traits::{CheckSuspension, DenyExecution, OnResponse, Properties, ShouldExecute};
/// Execution barrier that just takes `max_weight` from `properties.weight_credit`.
///
/// Useful to allow XCM execution by local chain users via extrinsics.
/// E.g. `pallet_xcm::reserve_asset_transfer` to transfer a reserve asset
/// out of the local chain to another one.
pub struct TakeWeightCredit;
impl ShouldExecute for TakeWeightCredit {
fn should_execute<RuntimeCall>(
origin: &Location,
instructions: &mut [Instruction<RuntimeCall>],
max_weight: Weight,
properties: &mut Properties,
) -> Result<(), ProcessMessageError> {
tracing::trace!(
target: "xcm::barriers",
?origin,
?instructions,
?max_weight,
?properties,
"TakeWeightCredit"
);
properties.weight_credit = properties
.weight_credit
.checked_sub(&max_weight)
.ok_or(ProcessMessageError::Overweight(max_weight))?;
Ok(())
}
}
const MAX_ASSETS_FOR_BUY_EXECUTION: usize = 2;
/// Allows execution from `origin` if it is contained in `T` (i.e. `T::Contains(origin)`) taking
/// payments into account.
///
/// Only allows for `WithdrawAsset`, `ReceiveTeleportedAsset`, `ReserveAssetDeposited` and
/// `ClaimAsset` XCMs because they are the only ones that place assets in the Holding Register to
/// pay for execution.
pub struct AllowTopLevelPaidExecutionFrom<T>(PhantomData<T>);
impl<T: Contains<Location>> ShouldExecute for AllowTopLevelPaidExecutionFrom<T> {
fn should_execute<RuntimeCall>(
origin: &Location,
instructions: &mut [Instruction<RuntimeCall>],
max_weight: Weight,
properties: &mut Properties,
) -> Result<(), ProcessMessageError> {
tracing::trace!(
target: "xcm::barriers",
?origin,
?instructions,
?max_weight,
?properties,
"AllowTopLevelPaidExecutionFrom",
);
ensure!(T::contains(origin), ProcessMessageError::Unsupported);
// We will read up to 5 instructions. This allows up to 3 `ClearOrigin` instructions. We
// allow for more than one since anything beyond the first is a no-op and it's conceivable
// that composition of operations might result in more than one being appended.
let end = instructions.len().min(5);
instructions[..end]
.matcher()
.match_next_inst(|inst| match inst {
WithdrawAsset(ref assets) |
ReceiveTeleportedAsset(ref assets) |
ReserveAssetDeposited(ref assets) |
ClaimAsset { ref assets, .. } =>
if assets.len() <= MAX_ASSETS_FOR_BUY_EXECUTION {
Ok(())
} else {
Err(ProcessMessageError::BadFormat)
},
_ => Err(ProcessMessageError::BadFormat),
})?
.skip_inst_while(|inst| {
matches!(inst, ClearOrigin | AliasOrigin(..)) ||
matches!(inst, DescendOrigin(child) if child != &Here) ||
matches!(inst, SetHints { .. })
})?
.match_next_inst(|inst| match inst {
BuyExecution { weight_limit: Limited(ref mut weight), .. }
if weight.all_gte(max_weight) =>
{
*weight = max_weight;
Ok(())
},
BuyExecution { ref mut weight_limit, .. } if weight_limit == &Unlimited => {
*weight_limit = Limited(max_weight);
Ok(())
},
PayFees { .. } => Ok(()),
_ => Err(ProcessMessageError::Overweight(max_weight)),
})?;
Ok(())
}
}
/// A derivative barrier, which scans the first `MaxPrefixes` instructions for origin-alterers and
/// then evaluates `should_execute` of the `InnerBarrier` based on the remaining instructions and
/// the newly computed origin.
///
/// This effectively allows for the possibility of distinguishing an origin which is acting as a
/// router for its derivative locations (or as a bridge for a remote location) and an origin which
/// is actually trying to send a message for itself. In the former case, the message will be
/// prefixed with origin-mutating instructions.
///
/// Any barriers which should be interpreted based on the computed origin rather than the original
/// message origin should be subject to this. This is the case for most barriers since the
/// effective origin is generally more important than the routing origin. Any other barriers, and
/// especially those which should be interpreted only the routing origin should not be subject to
/// this.
///
/// E.g.
/// ```nocompile
/// type MyBarrier = (
/// TakeWeightCredit,
/// AllowTopLevelPaidExecutionFrom<DirectCustomerLocations>,
/// WithComputedOrigin<(
/// AllowTopLevelPaidExecutionFrom<DerivativeCustomerLocations>,
/// AllowUnpaidExecutionFrom<ParentLocation>,
/// AllowSubscriptionsFrom<AllowedSubscribers>,
/// AllowKnownQueryResponses<TheResponseHandler>,
/// )>,
/// );
/// ```
///
/// In the above example, `AllowUnpaidExecutionFrom` appears once underneath
/// `WithComputedOrigin`. This is in order to distinguish between messages which are notionally
/// from a derivative location of `ParentLocation` but that just happened to be sent via
/// `ParentLocation` rather than messages that were sent by the parent.
///
/// Similarly `AllowTopLevelPaidExecutionFrom` appears twice: once inside of `WithComputedOrigin`
/// where we provide the list of origins which are derivative origins, and then secondly outside
/// of `WithComputedOrigin` where we provide the list of locations which are direct origins. It's
/// reasonable for these lists to be merged into one and that used both inside and out.
///
/// Finally, we see `AllowSubscriptionsFrom` and `AllowKnownQueryResponses` are both inside of
/// `WithComputedOrigin`. This means that if a message begins with origin-mutating instructions,
/// then it must be the finally computed origin which we accept subscriptions or expect a query
/// response from. For example, even if an origin appeared in the `AllowedSubscribers` list, we
/// would ignore this rule if it began with origin mutators and they changed the origin to something
/// which was not on the list.
pub struct WithComputedOrigin<InnerBarrier, LocalUniversal, MaxPrefixes>(
PhantomData<(InnerBarrier, LocalUniversal, MaxPrefixes)>,
);
impl<InnerBarrier: ShouldExecute, LocalUniversal: Get<InteriorLocation>, MaxPrefixes: Get<u32>>
ShouldExecute for WithComputedOrigin<InnerBarrier, LocalUniversal, MaxPrefixes>
{
fn should_execute<Call>(
origin: &Location,
instructions: &mut [Instruction<Call>],
max_weight: Weight,
properties: &mut Properties,
) -> Result<(), ProcessMessageError> {
tracing::trace!(
target: "xcm::barriers",
?origin,
?instructions,
?max_weight,
?properties,
"WithComputedOrigin"
);
let mut actual_origin = origin.clone();
let skipped = Cell::new(0usize);
// NOTE: We do not check the validity of `UniversalOrigin` here, meaning that a malicious
// origin could place a `UniversalOrigin` in order to spoof some location which gets free
// execution. This technical could get it past the barrier condition, but the execution
// would instantly fail since the first instruction would cause an error with the
// invalid UniversalOrigin.
instructions.matcher().match_next_inst_while(
|_| skipped.get() < MaxPrefixes::get() as usize,
|inst| {
match inst {
UniversalOrigin(new_global) => {
// Note the origin is *relative to local consensus*! So we need to escape
// local consensus with the `parents` before diving in into the
// `universal_location`.
actual_origin =
Junctions::from([*new_global]).relative_to(&LocalUniversal::get());
},
DescendOrigin(j) => {
let Ok(_) = actual_origin.append_with(j.clone()) else {
return Err(ProcessMessageError::Unsupported);
};
},
_ => return Ok(ControlFlow::Break(())),
};
skipped.set(skipped.get() + 1);
Ok(ControlFlow::Continue(()))
},
)?;
InnerBarrier::should_execute(
&actual_origin,
&mut instructions[skipped.get()..],
max_weight,
properties,
)
}
}
/// Sets the message ID to `t` using a `SetTopic(t)` in the last position if present.
///
/// Note that the message ID does not necessarily have to be unique; it is the
/// sender's responsibility to ensure uniqueness.
///
/// Requires some inner barrier to pass on the rest of the message.
pub struct TrailingSetTopicAsId<InnerBarrier>(PhantomData<InnerBarrier>);
impl<InnerBarrier: ShouldExecute> ShouldExecute for TrailingSetTopicAsId<InnerBarrier> {
fn should_execute<Call>(
origin: &Location,
instructions: &mut [Instruction<Call>],
max_weight: Weight,
properties: &mut Properties,
) -> Result<(), ProcessMessageError> {
tracing::trace!(
target: "xcm::barriers",
?origin,
?instructions,
?max_weight,
?properties,
"TrailingSetTopicAsId"
);
let until = if let Some(SetTopic(t)) = instructions.last() {
properties.message_id = Some(*t);
instructions.len() - 1
} else {
instructions.len()
};
InnerBarrier::should_execute(&origin, &mut instructions[..until], max_weight, properties)
}
}
/// Barrier condition that allows for a `SuspensionChecker` that controls whether or not the XCM
/// executor will be suspended from executing the given XCM.
pub struct RespectSuspension<Inner, SuspensionChecker>(PhantomData<(Inner, SuspensionChecker)>);
impl<Inner, SuspensionChecker> ShouldExecute for RespectSuspension<Inner, SuspensionChecker>
where
Inner: ShouldExecute,
SuspensionChecker: CheckSuspension,
{
fn should_execute<Call>(
origin: &Location,
instructions: &mut [Instruction<Call>],
max_weight: Weight,
properties: &mut Properties,
) -> Result<(), ProcessMessageError> {
if SuspensionChecker::is_suspended(origin, instructions, max_weight, properties) {
Err(ProcessMessageError::Yield)
} else {
Inner::should_execute(origin, instructions, max_weight, properties)
}
}
}
/// Allows execution from any origin that is contained in `T` (i.e. `T::Contains(origin)`).
///
/// Use only for executions from completely trusted origins, from which no permissionless messages
/// can be sent.
pub struct AllowUnpaidExecutionFrom<T>(PhantomData<T>);
impl<T: Contains<Location>> ShouldExecute for AllowUnpaidExecutionFrom<T> {
fn should_execute<RuntimeCall>(
origin: &Location,
instructions: &mut [Instruction<RuntimeCall>],
max_weight: Weight,
properties: &mut Properties,
) -> Result<(), ProcessMessageError> {
tracing::trace!(
target: "xcm::barriers",
?origin, ?instructions, ?max_weight, ?properties,
"AllowUnpaidExecutionFrom"
);
ensure!(T::contains(origin), ProcessMessageError::Unsupported);
Ok(())
}
}
/// Allows execution from any origin that is contained in `T` (i.e. `T::Contains(origin)`) if the
/// message explicitly includes the `UnpaidExecution` instruction.
///
/// Use only for executions from trusted origin groups.
///
/// Allows for the message to receive teleports or reserve asset transfers and altering
/// the origin before indicating `UnpaidExecution`.
///
/// Origin altering instructions are executed so the barrier can more accurately reject messages
/// whose effective origin at the time of calling `UnpaidExecution` is not allowed.
/// This means `T` will be checked against the actual origin _after_ being modified by prior
/// instructions.
///
/// In order to execute the `AliasOrigin` instruction, the `Aliasers` type should be set to the same
/// `Aliasers` item in the XCM configuration. If it isn't, then all messages with an `AliasOrigin`
/// instruction will be rejected.
pub struct AllowExplicitUnpaidExecutionFrom<T, Aliasers = Nothing>(PhantomData<(T, Aliasers)>);
impl<T: Contains<Location>, Aliasers: ContainsPair<Location, Location>> ShouldExecute
for AllowExplicitUnpaidExecutionFrom<T, Aliasers>
{
fn should_execute<Call>(
origin: &Location,
instructions: &mut [Instruction<Call>],
max_weight: Weight,
properties: &mut Properties,
) -> Result<(), ProcessMessageError> {
tracing::trace!(
target: "xcm::barriers",
?origin, ?instructions, ?max_weight, ?properties,
"AllowExplicitUnpaidExecutionFrom",
);
// We will read up to 5 instructions before `UnpaidExecution`.
// This allows up to 3 asset transfer instructions, thus covering all possible transfer
// types, followed by a potential origin altering instruction, and a potential `SetHints`.
let mut actual_origin = origin.clone();
let processed = Cell::new(0usize);
let instructions_to_process = 5;
instructions
.matcher()
// We skip set hints and all types of asset transfer instructions.
.match_next_inst_while(
|inst| {
processed.get() < instructions_to_process &&
matches!(
inst,
ReceiveTeleportedAsset(_) |
ReserveAssetDeposited(_) | WithdrawAsset(_) |
SetHints { .. }
)
},
|_| {
processed.set(processed.get() + 1);
Ok(ControlFlow::Continue(()))
},
)?
// Then we go through all origin altering instructions and we
// alter the original origin.
.match_next_inst_while(
|_| processed.get() < instructions_to_process,
|inst| {
match inst {
ClearOrigin => {
// We don't support the `ClearOrigin` instruction since we always need
// to know the origin to know if it's allowed unpaid execution.
return Err(ProcessMessageError::Unsupported);
},
AliasOrigin(target) =>
if Aliasers::contains(&actual_origin, &target) {
actual_origin = target.clone();
} else {
return Err(ProcessMessageError::Unsupported);
},
DescendOrigin(child) if child != &Here => {
let Ok(_) = actual_origin.append_with(child.clone()) else {
return Err(ProcessMessageError::Unsupported);
};
},
_ => return Ok(ControlFlow::Break(())),
};
processed.set(processed.get() + 1);
Ok(ControlFlow::Continue(()))
},
)?
// We finally match on the required `UnpaidExecution` instruction.
.match_next_inst(|inst| match inst {
UnpaidExecution { weight_limit: Limited(m), .. } if m.all_gte(max_weight) => Ok(()),
UnpaidExecution { weight_limit: Unlimited, .. } => Ok(()),
_ => Err(ProcessMessageError::Overweight(max_weight)),
})?;
// After processing all the instructions, `actual_origin` was modified and we
// check if it's allowed to have unpaid execution.
ensure!(T::contains(&actual_origin), ProcessMessageError::Unsupported);
Ok(())
}
}
/// Allows a message only if it is from a system-level child teyrchain.
pub struct IsChildSystemTeyrchain<ParaId>(PhantomData<ParaId>);
impl<ParaId: IsSystem + From<u32>> Contains<Location> for IsChildSystemTeyrchain<ParaId> {
fn contains(l: &Location) -> bool {
matches!(
l.interior().as_slice(),
[Junction::Teyrchain(id)]
if ParaId::from(*id).is_system() && l.parent_count() == 0,
)
}
}
/// Matches if the given location is a system-level sibling teyrchain.
pub struct IsSiblingSystemTeyrchain<ParaId, SelfParaId>(PhantomData<(ParaId, SelfParaId)>);
impl<ParaId: IsSystem + From<u32> + Eq, SelfParaId: Get<ParaId>> Contains<Location>
for IsSiblingSystemTeyrchain<ParaId, SelfParaId>
{
fn contains(l: &Location) -> bool {
matches!(
l.unpack(),
(1, [Junction::Teyrchain(id)])
if SelfParaId::get() != ParaId::from(*id) && ParaId::from(*id).is_system(),
)
}
}
/// Matches if the given location contains only the specified amount of parents and no interior
/// junctions.
pub struct IsParentsOnly<Count>(PhantomData<Count>);
impl<Count: Get<u8>> Contains<Location> for IsParentsOnly<Count> {
fn contains(t: &Location) -> bool {
t.contains_parents_only(Count::get())
}
}
/// Allows only messages if the generic `ResponseHandler` expects them via `expecting_response`.
pub struct AllowKnownQueryResponses<ResponseHandler>(PhantomData<ResponseHandler>);
impl<ResponseHandler: OnResponse> ShouldExecute for AllowKnownQueryResponses<ResponseHandler> {
fn should_execute<RuntimeCall>(
origin: &Location,
instructions: &mut [Instruction<RuntimeCall>],
max_weight: Weight,
properties: &mut Properties,
) -> Result<(), ProcessMessageError> {
tracing::trace!(
target: "xcm::barriers",
?origin, ?instructions, ?max_weight, ?properties,
"AllowKnownQueryResponses"
);
instructions
.matcher()
.assert_remaining_insts(1)?
.match_next_inst(|inst| match inst {
QueryResponse { query_id, querier, .. }
if ResponseHandler::expecting_response(origin, *query_id, querier.as_ref()) =>
Ok(()),
_ => Err(ProcessMessageError::BadFormat),
})?;
Ok(())
}
}
/// Allows execution from `origin` if it is just a straight `SubscribeVersion` or
/// `UnsubscribeVersion` instruction.
pub struct AllowSubscriptionsFrom<T>(PhantomData<T>);
impl<T: Contains<Location>> ShouldExecute for AllowSubscriptionsFrom<T> {
fn should_execute<RuntimeCall>(
origin: &Location,
instructions: &mut [Instruction<RuntimeCall>],
max_weight: Weight,
properties: &mut Properties,
) -> Result<(), ProcessMessageError> {
tracing::trace!(
target: "xcm::barriers",
?origin, ?instructions, ?max_weight, ?properties,
"AllowSubscriptionsFrom",
);
ensure!(T::contains(origin), ProcessMessageError::Unsupported);
instructions
.matcher()
.assert_remaining_insts(1)?
.match_next_inst(|inst| match inst {
SubscribeVersion { .. } | UnsubscribeVersion => Ok(()),
_ => Err(ProcessMessageError::BadFormat),
})?;
Ok(())
}
}
/// Allows execution for the Relay Chain origin (represented as `Location::parent()`) if it is just
/// a straight `HrmpNewChannelOpenRequest`, `HrmpChannelAccepted`, or `HrmpChannelClosing`
/// instruction.
///
/// Note: This barrier fulfills safety recommendations for the mentioned instructions - see their
/// documentation.
pub struct AllowHrmpNotificationsFromRelayChain;
impl ShouldExecute for AllowHrmpNotificationsFromRelayChain {
fn should_execute<RuntimeCall>(
origin: &Location,
instructions: &mut [Instruction<RuntimeCall>],
max_weight: Weight,
properties: &mut Properties,
) -> Result<(), ProcessMessageError> {
tracing::trace!(
target: "xcm::barriers",
?origin, ?instructions, ?max_weight, ?properties,
"AllowHrmpNotificationsFromRelayChain"
);
// accept only the Relay Chain
ensure!(matches!(origin.unpack(), (1, [])), ProcessMessageError::Unsupported);
// accept only HRMP notifications and nothing else
instructions
.matcher()
.assert_remaining_insts(1)?
.match_next_inst(|inst| match inst {
HrmpNewChannelOpenRequest { .. } |
HrmpChannelAccepted { .. } |
HrmpChannelClosing { .. } => Ok(()),
_ => Err(ProcessMessageError::BadFormat),
})?;
Ok(())
}
}
/// Deny executing the XCM if it matches any of the Deny filter regardless of anything else.
/// If it passes the Deny, and matches one of the Allow cases then it is let through.
pub struct DenyThenTry<Deny, Allow>(PhantomData<Deny>, PhantomData<Allow>)
where
Deny: DenyExecution,
Allow: ShouldExecute;
impl<Deny, Allow> ShouldExecute for DenyThenTry<Deny, Allow>
where
Deny: DenyExecution,
Allow: ShouldExecute,
{
fn should_execute<RuntimeCall>(
origin: &Location,
message: &mut [Instruction<RuntimeCall>],
max_weight: Weight,
properties: &mut Properties,
) -> Result<(), ProcessMessageError> {
Deny::deny_execution(origin, message, max_weight, properties)?;
Allow::should_execute(origin, message, max_weight, properties)
}
}
// See issue <https://github.com/paritytech/polkadot/issues/5233>
pub struct DenyReserveTransferToRelayChain;
impl DenyExecution for DenyReserveTransferToRelayChain {
fn deny_execution<RuntimeCall>(
origin: &Location,
message: &mut [Instruction<RuntimeCall>],
_max_weight: Weight,
_properties: &mut Properties,
) -> Result<(), ProcessMessageError> {
message.matcher().match_next_inst_while(
|_| true,
|inst| match inst {
InitiateReserveWithdraw {
reserve: Location { parents: 1, interior: Here },
..
} |
DepositReserveAsset { dest: Location { parents: 1, interior: Here }, .. } |
TransferReserveAsset { dest: Location { parents: 1, interior: Here }, .. } => {
Err(ProcessMessageError::Unsupported) // Deny
},
// An unexpected reserve transfer has arrived from the Relay Chain. Generally,
// `IsReserve` should not allow this, but we just log it here.
ReserveAssetDeposited { .. }
if matches!(origin, Location { parents: 1, interior: Here }) =>
{
tracing::debug!(
target: "xcm::barriers",
"Unexpected ReserveAssetDeposited from the Relay Chain",
);
Ok(ControlFlow::Continue(()))
},
_ => Ok(ControlFlow::Continue(())),
},
)?;
Ok(())
}
}
environmental::environmental!(recursion_count: u8);
/// Denies execution if the XCM contains instructions not meant to run on this chain,
/// first checking at the top-level and then **recursively**.
///
/// This barrier only applies to **locally executed** XCM instructions (`SetAppendix`,
/// `SetErrorHandler`, and `ExecuteWithOrigin`). Remote parts of the XCM are expected to be
/// validated by the receiving chain's barrier.
///
/// Note: Ensures that restricted instructions do not execute on the local chain, enforcing stricter
/// execution policies while allowing remote chains to enforce their own rules.
pub struct DenyRecursively<Inner>(PhantomData<Inner>);
impl<Inner: DenyExecution> DenyRecursively<Inner> {
/// Recursively applies the deny filter to a nested XCM.
///
/// Ensures that restricted instructions are blocked at any depth within the XCM.
/// Uses a **recursion counter** to prevent stack overflows from deep nesting.
fn deny_recursively<RuntimeCall>(
origin: &Location,
xcm: &mut Xcm<RuntimeCall>,
max_weight: Weight,
properties: &mut Properties,
) -> Result<ControlFlow<()>, ProcessMessageError> {
// Initialise recursion counter for this execution context.
recursion_count::using_once(&mut 1, || {
// Prevent stack overflow by enforcing a recursion depth limit.
recursion_count::with(|count| {
if *count > xcm_executor::RECURSION_LIMIT {
tracing::debug!(
target: "xcm::barriers",
"Recursion limit exceeded (count: {count}), origin: {:?}, xcm: {:?}, max_weight: {:?}, properties: {:?}",
origin, xcm, max_weight, properties
);
return None;
}
*count = count.saturating_add(1);
Some(())
}).flatten().ok_or(ProcessMessageError::StackLimitReached)?;
// Ensure the counter is decremented even if an early return occurs.
sp_core::defer! {
recursion_count::with(|count| {
*count = count.saturating_sub(1);
});
}
// Recursively check the nested XCM instructions.
Self::deny_execution(origin, xcm.inner_mut(), max_weight, properties)
})?;
Ok(ControlFlow::Continue(()))
}
}
impl<Inner: DenyExecution> DenyExecution for DenyRecursively<Inner> {
/// Denies execution of restricted local nested XCM instructions.
///
/// This checks for `SetAppendix`, `SetErrorHandler`, and `ExecuteWithOrigin` instruction
/// applying the deny filter **recursively** to any nested XCMs found.
fn deny_execution<RuntimeCall>(
origin: &Location,
instructions: &mut [Instruction<RuntimeCall>],
max_weight: Weight,
properties: &mut Properties,
) -> Result<(), ProcessMessageError> {
// First, check if the top-level message should be denied.
Inner::deny_execution(origin, instructions, max_weight, properties).inspect_err(|e| {
tracing::debug!(
target: "xcm::barriers",
"DenyRecursively::Inner denied execution, origin: {:?}, instructions: {:?}, max_weight: {:?}, properties: {:?}, error: {:?}",
origin, instructions, max_weight, properties, e
);
})?;
// If the top-level check passes, check nested instructions recursively.
instructions.matcher().match_next_inst_while(
|_| true,
|inst| match inst {
SetAppendix(nested_xcm) |
SetErrorHandler(nested_xcm) |
ExecuteWithOrigin { xcm: nested_xcm, .. } => Self::deny_recursively::<RuntimeCall>(
origin, nested_xcm, max_weight, properties,
),
_ => Ok(ControlFlow::Continue(())),
},
)?;
// Permit everything else
Ok(())
}
}
+192
View File
@@ -0,0 +1,192 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
//! A set of traits that define how a pallet interface with XCM.
//! Controller traits defined in this module are high-level traits that will rely on other traits
//! from `xcm-executor` to perform their tasks.
use alloc::boxed::Box;
use frame_support::{
dispatch::{DispatchErrorWithPostInfo, WithPostDispatchInfo},
pallet_prelude::DispatchError,
};
use xcm::prelude::*;
pub use xcm_executor::traits::QueryHandler;
/// Umbrella trait for all Controller traits.
pub trait Controller<Origin, RuntimeCall, Timeout>:
ExecuteController<Origin, RuntimeCall> + SendController<Origin> + QueryController<Origin, Timeout>
{
}
impl<T, Origin, RuntimeCall, Timeout> Controller<Origin, RuntimeCall, Timeout> for T where
T: ExecuteController<Origin, RuntimeCall>
+ SendController<Origin>
+ QueryController<Origin, Timeout>
{
}
/// Weight functions needed for [`ExecuteController`].
pub trait ExecuteControllerWeightInfo {
/// Weight for [`ExecuteController::execute`]
fn execute() -> Weight;
}
/// Execute an XCM locally, for a given origin.
///
/// An implementation of that trait will handle the low-level details of the execution, such as:
/// - Validating and Converting the origin to a Location.
/// - Handling versioning.
/// - Calling the internal executor, which implements [`ExecuteXcm`].
pub trait ExecuteController<Origin, RuntimeCall> {
/// Weight information for ExecuteController functions.
type WeightInfo: ExecuteControllerWeightInfo;
/// Attempt to execute an XCM locally, returns Ok with the weight consumed if the execution
/// complete successfully, Err otherwise.
///
/// # Parameters
///
/// - `origin`: the origin of the call.
/// - `message`: the XCM program to be executed.
/// - `max_weight`: the maximum weight that can be consumed by the execution.
fn execute(
origin: Origin,
message: Box<VersionedXcm<RuntimeCall>>,
max_weight: Weight,
) -> Result<Weight, DispatchErrorWithPostInfo>;
}
/// Weight functions needed for [`SendController`].
pub trait SendControllerWeightInfo {
/// Weight for [`SendController::send`]
fn send() -> Weight;
}
/// Send an XCM from a given origin.
///
/// An implementation of that trait will handle the low-level details of dispatching an XCM, such
/// as:
/// - Validating and Converting the origin to an interior location.
/// - Handling versioning.
/// - Calling the internal router, which implements [`SendXcm`].
pub trait SendController<Origin> {
/// Weight information for SendController functions.
type WeightInfo: SendControllerWeightInfo;
/// Send an XCM to be executed by a remote location.
///
/// # Parameters
///
/// - `origin`: the origin of the call.
/// - `dest`: the destination of the message.
/// - `msg`: the XCM to be sent.
fn send(
origin: Origin,
dest: Box<VersionedLocation>,
message: Box<VersionedXcm<()>>,
) -> Result<XcmHash, DispatchError>;
}
/// Weight functions needed for [`QueryController`].
pub trait QueryControllerWeightInfo {
/// Weight for [`QueryController::query`]
fn query() -> Weight;
/// Weight for [`QueryHandler::take_response`]
fn take_response() -> Weight;
}
/// Query a remote location, from a given origin.
///
/// An implementation of that trait will handle the low-level details of querying a remote location,
/// such as:
/// - Validating and Converting the origin to an interior location.
/// - Handling versioning.
/// - Calling the [`QueryHandler`] to register the query.
pub trait QueryController<Origin, Timeout>: QueryHandler {
/// Weight information for QueryController functions.
type WeightInfo: QueryControllerWeightInfo;
/// Query a remote location.
///
/// # Parameters
///
/// - `origin`: the origin of the call, used to determine the responder.
/// - `timeout`: the maximum block number that the query should be responded to.
/// - `match_querier`: the querier that the query should be responded to.
fn query(
origin: Origin,
timeout: Timeout,
match_querier: VersionedLocation,
) -> Result<QueryId, DispatchError>;
}
impl<Origin, RuntimeCall> ExecuteController<Origin, RuntimeCall> for () {
type WeightInfo = ();
fn execute(
_origin: Origin,
_message: Box<VersionedXcm<RuntimeCall>>,
_max_weight: Weight,
) -> Result<Weight, DispatchErrorWithPostInfo> {
Err(DispatchError::Other("ExecuteController::execute not implemented")
.with_weight(Weight::zero()))
}
}
impl ExecuteControllerWeightInfo for () {
fn execute() -> Weight {
Weight::zero()
}
}
impl<Origin> SendController<Origin> for () {
type WeightInfo = ();
fn send(
_origin: Origin,
_dest: Box<VersionedLocation>,
_message: Box<VersionedXcm<()>>,
) -> Result<XcmHash, DispatchError> {
Ok(Default::default())
}
}
impl SendControllerWeightInfo for () {
fn send() -> Weight {
Weight::zero()
}
}
impl QueryControllerWeightInfo for () {
fn query() -> Weight {
Weight::zero()
}
fn take_response() -> Weight {
Weight::zero()
}
}
impl<Origin, Timeout> QueryController<Origin, Timeout> for () {
type WeightInfo = ();
fn query(
_origin: Origin,
_timeout: Timeout,
_match_querier: VersionedLocation,
) -> Result<QueryId, DispatchError> {
Ok(Default::default())
}
}
@@ -0,0 +1,247 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
//! Adapters to work with `frame_support::traits::Currency` through XCM.
#![allow(deprecated)]
use super::MintLocation;
use core::{fmt::Debug, marker::PhantomData, result};
use frame_support::traits::{ExistenceRequirement::AllowDeath, Get, WithdrawReasons};
use sp_runtime::traits::CheckedSub;
use xcm::latest::{Asset, Error as XcmError, Location, Result, XcmContext};
use xcm_executor::{
traits::{ConvertLocation, MatchesFungible, TransactAsset},
AssetsInHolding,
};
/// Asset transaction errors.
enum Error {
/// The given asset is not handled. (According to [`XcmError::AssetNotFound`])
AssetNotHandled,
/// `Location` to `AccountId` conversion failed.
AccountIdConversionFailed,
}
impl From<Error> for XcmError {
fn from(e: Error) -> Self {
use XcmError::FailedToTransactAsset;
match e {
Error::AssetNotHandled => XcmError::AssetNotFound,
Error::AccountIdConversionFailed => FailedToTransactAsset("AccountIdConversionFailed"),
}
}
}
/// Simple adapter to use a currency as asset transactor. This type can be used as `type
/// AssetTransactor` in `xcm::Config`.
///
/// # Example
/// ```
/// use codec::Decode;
/// use frame_support::{parameter_types, PalletId};
/// use sp_runtime::traits::{AccountIdConversion, TrailingZeroInput};
/// use xcm::latest::prelude::*;
/// use staging_xcm_builder::{ParentIsPreset, CurrencyAdapter, IsConcrete};
///
/// /// Our chain's account id.
/// type AccountId = sp_runtime::AccountId32;
///
/// /// Our relay chain's location.
/// parameter_types! {
/// pub RelayChain: Location = Parent.into();
/// pub CheckingAccount: AccountId = PalletId(*b"checking").into_account_truncating();
/// }
///
/// /// Some items that implement `ConvertLocation<AccountId>`. Can be more, but for now we just assume we accept
/// /// messages from the parent (relay chain).
/// pub type LocationConverter = (ParentIsPreset<AccountId>);
///
/// /// Just a dummy implementation of `Currency`. Normally this would be `Balances`.
/// pub type CurrencyImpl = ();
///
/// /// Final currency adapter. This can be used in `xcm::Config` to specify how asset related transactions happen.
/// pub type AssetTransactor = CurrencyAdapter<
/// // Use this `Currency` impl instance:
/// CurrencyImpl,
/// // The matcher: use the currency when the asset is a concrete asset in our relay chain.
/// IsConcrete<RelayChain>,
/// // The local converter: default account of the parent relay chain.
/// LocationConverter,
/// // Our chain's account ID type.
/// AccountId,
/// // The checking account. Can be any deterministic inaccessible account.
/// CheckingAccount,
/// >;
/// ```
#[deprecated = "Use `FungibleAdapter` instead"]
pub struct CurrencyAdapter<Currency, Matcher, AccountIdConverter, AccountId, CheckedAccount>(
PhantomData<(Currency, Matcher, AccountIdConverter, AccountId, CheckedAccount)>,
);
impl<
Currency: frame_support::traits::Currency<AccountId>,
Matcher: MatchesFungible<Currency::Balance>,
AccountIdConverter: ConvertLocation<AccountId>,
AccountId: Clone, // can't get away without it since Currency is generic over it.
CheckedAccount: Get<Option<(AccountId, MintLocation)>>,
> CurrencyAdapter<Currency, Matcher, AccountIdConverter, AccountId, CheckedAccount>
{
fn can_accrue_checked(_checked_account: AccountId, _amount: Currency::Balance) -> Result {
Ok(())
}
fn can_reduce_checked(checked_account: AccountId, amount: Currency::Balance) -> Result {
let new_balance = Currency::free_balance(&checked_account)
.checked_sub(&amount)
.ok_or(XcmError::NotWithdrawable)?;
Currency::ensure_can_withdraw(
&checked_account,
amount,
WithdrawReasons::TRANSFER,
new_balance,
)
.map_err(|error| {
tracing::debug!(target: "xcm::currency_adapter", ?error, "Failed to ensure can withdraw");
XcmError::NotWithdrawable
})
}
fn accrue_checked(checked_account: AccountId, amount: Currency::Balance) {
let _ = Currency::deposit_creating(&checked_account, amount);
Currency::deactivate(amount);
}
fn reduce_checked(checked_account: AccountId, amount: Currency::Balance) {
let ok =
Currency::withdraw(&checked_account, amount, WithdrawReasons::TRANSFER, AllowDeath)
.is_ok();
if ok {
Currency::reactivate(amount);
} else {
frame_support::defensive!(
"`can_check_in` must have returned `true` immediately prior; qed"
);
}
}
}
impl<
Currency: frame_support::traits::Currency<AccountId>,
Matcher: MatchesFungible<Currency::Balance>,
AccountIdConverter: ConvertLocation<AccountId>,
AccountId: Clone + Debug, // can't get away without it since Currency is generic over it.
CheckedAccount: Get<Option<(AccountId, MintLocation)>>,
> TransactAsset
for CurrencyAdapter<Currency, Matcher, AccountIdConverter, AccountId, CheckedAccount>
{
fn can_check_in(origin: &Location, what: &Asset, _context: &XcmContext) -> Result {
tracing::trace!(target: "xcm::currency_adapter", ?origin, ?what, "can_check_in origin");
// Check we handle this asset.
let amount: Currency::Balance =
Matcher::matches_fungible(what).ok_or(Error::AssetNotHandled)?;
match CheckedAccount::get() {
Some((checked_account, MintLocation::Local)) =>
Self::can_reduce_checked(checked_account, amount),
Some((checked_account, MintLocation::NonLocal)) =>
Self::can_accrue_checked(checked_account, amount),
None => Ok(()),
}
}
fn check_in(origin: &Location, what: &Asset, _context: &XcmContext) {
tracing::trace!(target: "xcm::currency_adapter", ?origin, ?what, "check_in origin");
if let Some(amount) = Matcher::matches_fungible(what) {
match CheckedAccount::get() {
Some((checked_account, MintLocation::Local)) =>
Self::reduce_checked(checked_account, amount),
Some((checked_account, MintLocation::NonLocal)) =>
Self::accrue_checked(checked_account, amount),
None => (),
}
}
}
fn can_check_out(dest: &Location, what: &Asset, _context: &XcmContext) -> Result {
tracing::trace!(target: "xcm::currency_adapter", ?dest, ?what, "can_check_out");
let amount = Matcher::matches_fungible(what).ok_or(Error::AssetNotHandled)?;
match CheckedAccount::get() {
Some((checked_account, MintLocation::Local)) =>
Self::can_accrue_checked(checked_account, amount),
Some((checked_account, MintLocation::NonLocal)) =>
Self::can_reduce_checked(checked_account, amount),
None => Ok(()),
}
}
fn check_out(dest: &Location, what: &Asset, _context: &XcmContext) {
tracing::trace!(target: "xcm::currency_adapter", ?dest, ?what, "check_out");
if let Some(amount) = Matcher::matches_fungible(what) {
match CheckedAccount::get() {
Some((checked_account, MintLocation::Local)) =>
Self::accrue_checked(checked_account, amount),
Some((checked_account, MintLocation::NonLocal)) =>
Self::reduce_checked(checked_account, amount),
None => (),
}
}
}
fn deposit_asset(what: &Asset, who: &Location, _context: Option<&XcmContext>) -> Result {
tracing::trace!(target: "xcm::currency_adapter", ?what, ?who, "deposit_asset");
// Check we handle this asset.
let amount = Matcher::matches_fungible(&what).ok_or(Error::AssetNotHandled)?;
let who =
AccountIdConverter::convert_location(who).ok_or(Error::AccountIdConversionFailed)?;
let _imbalance = Currency::deposit_creating(&who, amount);
Ok(())
}
fn withdraw_asset(
what: &Asset,
who: &Location,
_maybe_context: Option<&XcmContext>,
) -> result::Result<AssetsInHolding, XcmError> {
tracing::trace!(target: "xcm::currency_adapter", ?what, ?who, "withdraw_asset");
// Check we handle this asset.
let amount = Matcher::matches_fungible(what).ok_or(Error::AssetNotHandled)?;
let who =
AccountIdConverter::convert_location(who).ok_or(Error::AccountIdConversionFailed)?;
let _ = Currency::withdraw(&who, amount, WithdrawReasons::TRANSFER, AllowDeath).map_err(
|error| {
tracing::debug!(target: "xcm::currency_adapter", ?error, ?who, ?amount, "Failed to withdraw asset");
XcmError::FailedToTransactAsset(error.into())
},
)?;
Ok(what.clone().into())
}
fn internal_transfer_asset(
asset: &Asset,
from: &Location,
to: &Location,
_context: &XcmContext,
) -> result::Result<AssetsInHolding, XcmError> {
tracing::trace!(target: "xcm::currency_adapter", ?asset, ?from, ?to, "internal_transfer_asset");
let amount = Matcher::matches_fungible(asset).ok_or(Error::AssetNotHandled)?;
let from =
AccountIdConverter::convert_location(from).ok_or(Error::AccountIdConversionFailed)?;
let to =
AccountIdConverter::convert_location(to).ok_or(Error::AccountIdConversionFailed)?;
Currency::transfer(&from, &to, amount, AllowDeath).map_err(|error| {
tracing::debug!(target: "xcm::currency_adapter", ?error, ?from, ?to, ?amount, "Failed to transfer asset");
XcmError::FailedToTransactAsset(error.into())
})?;
Ok(asset.clone().into())
}
}
+138
View File
@@ -0,0 +1,138 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
use core::marker::PhantomData;
use frame_support::traits::{Contains, Get};
use xcm::prelude::*;
use xcm_executor::traits::{FeeManager, FeeReason, TransactAsset};
/// Handles the fees that are taken by certain XCM instructions.
pub trait HandleFee {
/// Do something with the fee which has been paid. Doing nothing here silently burns the
/// fees.
///
/// Returns any part of the fee that wasn't consumed.
fn handle_fee(fee: Assets, context: Option<&XcmContext>, reason: FeeReason) -> Assets;
}
// Default `HandleFee` implementation that just burns the fee.
impl HandleFee for () {
fn handle_fee(_: Assets, _: Option<&XcmContext>, _: FeeReason) -> Assets {
Assets::new()
}
}
#[impl_trait_for_tuples::impl_for_tuples(1, 30)]
impl HandleFee for Tuple {
fn handle_fee(fee: Assets, context: Option<&XcmContext>, reason: FeeReason) -> Assets {
let mut unconsumed_fee = fee;
for_tuples!( #(
unconsumed_fee = Tuple::handle_fee(unconsumed_fee, context, reason.clone());
if unconsumed_fee.is_none() {
return unconsumed_fee;
}
)* );
unconsumed_fee
}
}
/// A `FeeManager` implementation that permits the specified `WaivedLocations` to not pay for fees
/// and that uses the provided `HandleFee` implementation otherwise.
pub struct XcmFeeManagerFromComponents<WaivedLocations, HandleFee>(
PhantomData<(WaivedLocations, HandleFee)>,
);
impl<WaivedLocations: Contains<Location>, FeeHandler: HandleFee> FeeManager
for XcmFeeManagerFromComponents<WaivedLocations, FeeHandler>
{
fn is_waived(origin: Option<&Location>, _: FeeReason) -> bool {
let Some(loc) = origin else { return false };
WaivedLocations::contains(loc)
}
fn handle_fee(fee: Assets, context: Option<&XcmContext>, reason: FeeReason) {
FeeHandler::handle_fee(fee, context, reason);
}
}
/// A `HandleFee` implementation that simply deposits the fees into a specific on-chain
/// `ReceiverAccount`.
///
/// It reuses the `AssetTransactor` configured on the XCM executor to deposit fee assets. If
/// the `AssetTransactor` returns an error while calling `deposit_asset`, then a warning will be
/// logged and the fee burned.
#[deprecated(
note = "`XcmFeeToAccount` will be removed in January 2025. Use `SendXcmFeeToAccount` instead."
)]
#[allow(dead_code)]
pub struct XcmFeeToAccount<AssetTransactor, AccountId, ReceiverAccount>(
PhantomData<(AssetTransactor, AccountId, ReceiverAccount)>,
);
#[allow(deprecated)]
impl<
AssetTransactor: TransactAsset,
AccountId: Clone + Into<[u8; 32]>,
ReceiverAccount: Get<AccountId>,
> HandleFee for XcmFeeToAccount<AssetTransactor, AccountId, ReceiverAccount>
{
fn handle_fee(fee: Assets, context: Option<&XcmContext>, _reason: FeeReason) -> Assets {
let dest = AccountId32 { network: None, id: ReceiverAccount::get().into() }.into();
deposit_or_burn_fee::<AssetTransactor>(fee, context, dest);
Assets::new()
}
}
/// A `HandleFee` implementation that simply deposits the fees into a specific on-chain
/// `ReceiverAccount`.
///
/// It reuses the `AssetTransactor` configured on the XCM executor to deposit fee assets. If
/// the `AssetTransactor` returns an error while calling `deposit_asset`, then a warning will be
/// logged and the fee burned.
///
/// `ReceiverAccount` should implement `Get<Location>`.
pub struct SendXcmFeeToAccount<AssetTransactor, ReceiverAccount>(
PhantomData<(AssetTransactor, ReceiverAccount)>,
);
impl<AssetTransactor: TransactAsset, ReceiverAccount: Get<Location>> HandleFee
for SendXcmFeeToAccount<AssetTransactor, ReceiverAccount>
{
fn handle_fee(fee: Assets, context: Option<&XcmContext>, _reason: FeeReason) -> Assets {
deposit_or_burn_fee::<AssetTransactor>(fee, context, ReceiverAccount::get());
Assets::new()
}
}
/// Try to deposit the given fee in the specified account.
/// Burns the fee in case of a failure.
pub fn deposit_or_burn_fee<AssetTransactor: TransactAsset>(
fee: Assets,
context: Option<&XcmContext>,
dest: Location,
) {
for asset in fee.into_inner() {
if let Err(e) = AssetTransactor::deposit_asset(&asset, &dest, context) {
tracing::trace!(
target: "xcm::fees",
"`AssetTransactor::deposit_asset` returned error: {e:?}. Burning fee: {asset:?}. \
They might be burned.",
);
}
}
}
@@ -0,0 +1,210 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
//! Various implementations of `ContainsPair<Asset, Location>` or
//! `Contains<(Location, Vec<Asset>)>`.
use alloc::vec::Vec;
use core::marker::PhantomData;
use frame_support::traits::{Contains, ContainsPair, Get};
use xcm::latest::{Asset, AssetFilter, AssetId, Location, WildAsset};
/// Accepts an asset iff it is a native asset.
pub struct NativeAsset;
impl ContainsPair<Asset, Location> for NativeAsset {
fn contains(asset: &Asset, origin: &Location) -> bool {
tracing::trace!(target: "xcm::contains", ?asset, ?origin, "NativeAsset");
matches!(asset.id, AssetId(ref id) if id == origin)
}
}
/// Accepts an asset if it is contained in the given `T`'s `Get` implementation.
pub struct Case<T>(PhantomData<T>);
impl<T: Get<(AssetFilter, Location)>> ContainsPair<Asset, Location> for Case<T> {
fn contains(asset: &Asset, origin: &Location) -> bool {
tracing::trace!(target: "xcm::contains", ?asset, ?origin, "Case asset");
let (a, o) = T::get();
a.matches(asset) && &o == origin
}
}
/// Accepts a tuple `(location, assets)` if the `location` is contained in the `Contains`
/// implementation of the given `Location` and if every asset from `assets` matches at least one of
/// the `AssetFilter` instances provided by the `Get` implementation of `AssetFilters`.
pub struct LocationWithAssetFilters<LocationFilter, AssetFilters>(
core::marker::PhantomData<(LocationFilter, AssetFilters)>,
);
impl<LocationFilter: Contains<Location>, AssetFilters: Get<Vec<AssetFilter>>>
Contains<(Location, Vec<Asset>)> for LocationWithAssetFilters<LocationFilter, AssetFilters>
{
fn contains((location, assets): &(Location, Vec<Asset>)) -> bool {
tracing::trace!(target: "xcm::contains", ?location, ?assets, "LocationWithAssetFilters");
// `location` must match the `Location` filter.
if !LocationFilter::contains(location) {
return false;
}
// All `assets` must match at least one of the `AssetFilters`.
let filters = AssetFilters::get();
assets.iter().all(|asset| {
for filter in &filters {
if filter.matches(asset) {
return true;
}
}
false
})
}
}
/// Implementation of `Get<Vec<AssetFilter>>` which accepts every asset.
/// (For example, it can be used with `LocationWithAssetFilters`).
pub struct AllAssets;
impl Get<Vec<AssetFilter>> for AllAssets {
fn get() -> Vec<AssetFilter> {
alloc::vec![AssetFilter::Wild(WildAsset::All)]
}
}
#[cfg(test)]
mod tests {
use super::*;
use frame_support::traits::Equals;
use xcm::latest::prelude::*;
#[test]
fn location_with_asset_filters_works() {
frame_support::parameter_types! {
pub ParaA: Location = Location::new(1, [Teyrchain(1001)]);
pub ParaB: Location = Location::new(1, [Teyrchain(1002)]);
pub ParaC: Location = Location::new(1, [Teyrchain(1003)]);
pub AssetXLocation: Location = Location::new(1, [GeneralIndex(1111)]);
pub AssetYLocation: Location = Location::new(1, [GeneralIndex(2222)]);
pub AssetZLocation: Location = Location::new(1, [GeneralIndex(3333)]);
pub OnlyAssetXOrAssetY: alloc::vec::Vec<AssetFilter> = alloc::vec![
Wild(AllOf { fun: WildFungible, id: AssetId(AssetXLocation::get()) }),
Wild(AllOf { fun: WildFungible, id: AssetId(AssetYLocation::get()) }),
];
pub OnlyAssetZ: alloc::vec::Vec<AssetFilter> = alloc::vec![
Wild(AllOf { fun: WildFungible, id: AssetId(AssetZLocation::get()) })
];
}
let test_data: Vec<(Location, Vec<Asset>, bool)> = vec![
(ParaA::get(), vec![(AssetXLocation::get(), 1).into()], true),
(ParaA::get(), vec![(AssetYLocation::get(), 1).into()], true),
(ParaA::get(), vec![(AssetZLocation::get(), 1).into()], false),
(
ParaA::get(),
vec![(AssetXLocation::get(), 1).into(), (AssetYLocation::get(), 1).into()],
true,
),
(
ParaA::get(),
vec![(AssetXLocation::get(), 1).into(), (AssetZLocation::get(), 1).into()],
false,
),
(
ParaA::get(),
vec![(AssetYLocation::get(), 1).into(), (AssetZLocation::get(), 1).into()],
false,
),
(
ParaA::get(),
vec![
(AssetXLocation::get(), 1).into(),
(AssetYLocation::get(), 1).into(),
(AssetZLocation::get(), 1).into(),
],
false,
),
(ParaB::get(), vec![(AssetXLocation::get(), 1).into()], false),
(ParaB::get(), vec![(AssetYLocation::get(), 1).into()], false),
(ParaB::get(), vec![(AssetZLocation::get(), 1).into()], true),
(
ParaB::get(),
vec![(AssetXLocation::get(), 1).into(), (AssetYLocation::get(), 1).into()],
false,
),
(
ParaB::get(),
vec![(AssetXLocation::get(), 1).into(), (AssetZLocation::get(), 1).into()],
false,
),
(
ParaB::get(),
vec![(AssetYLocation::get(), 1).into(), (AssetZLocation::get(), 1).into()],
false,
),
(
ParaB::get(),
vec![
(AssetXLocation::get(), 1).into(),
(AssetYLocation::get(), 1).into(),
(AssetZLocation::get(), 1).into(),
],
false,
),
(ParaC::get(), vec![(AssetXLocation::get(), 1).into()], true),
(ParaC::get(), vec![(AssetYLocation::get(), 1).into()], true),
(ParaC::get(), vec![(AssetZLocation::get(), 1).into()], true),
(
ParaC::get(),
vec![(AssetXLocation::get(), 1).into(), (AssetYLocation::get(), 1).into()],
true,
),
(
ParaC::get(),
vec![(AssetXLocation::get(), 1).into(), (AssetZLocation::get(), 1).into()],
true,
),
(
ParaC::get(),
vec![(AssetYLocation::get(), 1).into(), (AssetZLocation::get(), 1).into()],
true,
),
(
ParaC::get(),
vec![
(AssetXLocation::get(), 1).into(),
(AssetYLocation::get(), 1).into(),
(AssetZLocation::get(), 1).into(),
],
true,
),
];
type Filter = (
// For ParaA accept only asset X and Y.
LocationWithAssetFilters<Equals<ParaA>, OnlyAssetXOrAssetY>,
// For ParaB accept only asset Z.
LocationWithAssetFilters<Equals<ParaB>, OnlyAssetZ>,
// For ParaC accept all assets.
LocationWithAssetFilters<Equals<ParaC>, AllAssets>,
);
for (location, assets, expected_result) in test_data {
assert_eq!(
Filter::contains(&(location.clone(), assets.clone())),
expected_result,
"expected_result: {expected_result} not matched for (location, assets): ({:?}, {:?})!", location, assets,
)
}
}
}
@@ -0,0 +1,335 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
//! Adapters to work with [`frame_support::traits::fungible`] through XCM.
use super::MintLocation;
use core::{fmt::Debug, marker::PhantomData, result};
use frame_support::traits::{
tokens::{
fungible, Fortitude::Polite, Precision::Exact, Preservation::Expendable, Provenance::Minted,
},
Get,
};
use xcm::latest::prelude::*;
use xcm_executor::{
traits::{ConvertLocation, Error as MatchError, MatchesFungible, TransactAsset},
AssetsInHolding,
};
/// [`TransactAsset`] implementation that allows the use of a [`fungible`] implementation for
/// handling an asset in the XCM executor.
/// Only works for transfers.
pub struct FungibleTransferAdapter<Fungible, Matcher, AccountIdConverter, AccountId>(
PhantomData<(Fungible, Matcher, AccountIdConverter, AccountId)>,
);
impl<
Fungible: fungible::Mutate<AccountId>,
Matcher: MatchesFungible<Fungible::Balance>,
AccountIdConverter: ConvertLocation<AccountId>,
AccountId: Eq + Clone + Debug,
> TransactAsset for FungibleTransferAdapter<Fungible, Matcher, AccountIdConverter, AccountId>
{
fn internal_transfer_asset(
what: &Asset,
from: &Location,
to: &Location,
_context: &XcmContext,
) -> result::Result<AssetsInHolding, XcmError> {
tracing::trace!(
target: "xcm::fungible_adapter",
?what, ?from, ?to,
"internal_transfer_asset",
);
// Check we handle the asset
let amount = Matcher::matches_fungible(what).ok_or(MatchError::AssetNotHandled)?;
let source = AccountIdConverter::convert_location(from)
.ok_or(MatchError::AccountIdConversionFailed)?;
let dest = AccountIdConverter::convert_location(to)
.ok_or(MatchError::AccountIdConversionFailed)?;
Fungible::transfer(&source, &dest, amount, Expendable).map_err(|error| {
tracing::debug!(
target: "xcm::fungible_adapter", ?error, ?source, ?dest, ?amount,
"Failed to transfer asset",
);
XcmError::FailedToTransactAsset(error.into())
})?;
Ok(what.clone().into())
}
}
/// [`TransactAsset`] implementation that allows the use of a [`fungible`] implementation for
/// handling an asset in the XCM executor.
/// Works for everything but transfers.
pub struct FungibleMutateAdapter<Fungible, Matcher, AccountIdConverter, AccountId, CheckingAccount>(
PhantomData<(Fungible, Matcher, AccountIdConverter, AccountId, CheckingAccount)>,
);
impl<
Fungible: fungible::Mutate<AccountId>,
Matcher: MatchesFungible<Fungible::Balance>,
AccountIdConverter: ConvertLocation<AccountId>,
AccountId: Eq + Clone + Debug,
CheckingAccount: Get<Option<(AccountId, MintLocation)>>,
> FungibleMutateAdapter<Fungible, Matcher, AccountIdConverter, AccountId, CheckingAccount>
{
fn can_accrue_checked(checking_account: AccountId, amount: Fungible::Balance) -> XcmResult {
Fungible::can_deposit(&checking_account, amount, Minted)
.into_result()
.map_err(|error| {
tracing::debug!(
target: "xcm::fungible_adapter", ?error, ?checking_account, ?amount,
"Failed to deposit funds into account",
);
XcmError::NotDepositable
})
}
fn can_reduce_checked(checking_account: AccountId, amount: Fungible::Balance) -> XcmResult {
Fungible::can_withdraw(&checking_account, amount)
.into_result(false)
.map_err(|error| {
tracing::debug!(
target: "xcm::fungible_adapter", ?error, ?checking_account, ?amount,
"Failed to withdraw funds from account",
);
XcmError::NotWithdrawable
})
.map(|_| ())
}
fn accrue_checked(checking_account: AccountId, amount: Fungible::Balance) {
let ok = Fungible::mint_into(&checking_account, amount).is_ok();
debug_assert!(ok, "`can_accrue_checked` must have returned `true` immediately prior; qed");
}
fn reduce_checked(checking_account: AccountId, amount: Fungible::Balance) {
let ok = Fungible::burn_from(&checking_account, amount, Expendable, Exact, Polite).is_ok();
debug_assert!(ok, "`can_reduce_checked` must have returned `true` immediately prior; qed");
}
}
impl<
Fungible: fungible::Mutate<AccountId>,
Matcher: MatchesFungible<Fungible::Balance>,
AccountIdConverter: ConvertLocation<AccountId>,
AccountId: Eq + Clone + Debug,
CheckingAccount: Get<Option<(AccountId, MintLocation)>>,
> TransactAsset
for FungibleMutateAdapter<Fungible, Matcher, AccountIdConverter, AccountId, CheckingAccount>
{
fn can_check_in(origin: &Location, what: &Asset, _context: &XcmContext) -> XcmResult {
tracing::trace!(
target: "xcm::fungible_adapter",
?origin, ?what,
"can_check_in origin",
);
// Check we handle this asset
let amount = Matcher::matches_fungible(what).ok_or(MatchError::AssetNotHandled)?;
match CheckingAccount::get() {
Some((checking_account, MintLocation::Local)) =>
Self::can_reduce_checked(checking_account, amount),
Some((checking_account, MintLocation::NonLocal)) =>
Self::can_accrue_checked(checking_account, amount),
None => Ok(()),
}
}
fn check_in(origin: &Location, what: &Asset, _context: &XcmContext) {
tracing::trace!(
target: "xcm::fungible_adapter",
?origin, ?what,
"check_in origin",
);
if let Some(amount) = Matcher::matches_fungible(what) {
match CheckingAccount::get() {
Some((checking_account, MintLocation::Local)) =>
Self::reduce_checked(checking_account, amount),
Some((checking_account, MintLocation::NonLocal)) =>
Self::accrue_checked(checking_account, amount),
None => (),
}
}
}
fn can_check_out(dest: &Location, what: &Asset, _context: &XcmContext) -> XcmResult {
tracing::trace!(
target: "xcm::fungible_adapter",
?dest,
?what,
"can_check_out",
);
let amount = Matcher::matches_fungible(what).ok_or(MatchError::AssetNotHandled)?;
match CheckingAccount::get() {
Some((checking_account, MintLocation::Local)) =>
Self::can_accrue_checked(checking_account, amount),
Some((checking_account, MintLocation::NonLocal)) =>
Self::can_reduce_checked(checking_account, amount),
None => Ok(()),
}
}
fn check_out(dest: &Location, what: &Asset, _context: &XcmContext) {
tracing::trace!(
target: "xcm::fungible_adapter",
?dest,
?what,
"check_out",
);
if let Some(amount) = Matcher::matches_fungible(what) {
match CheckingAccount::get() {
Some((checking_account, MintLocation::Local)) =>
Self::accrue_checked(checking_account, amount),
Some((checking_account, MintLocation::NonLocal)) =>
Self::reduce_checked(checking_account, amount),
None => (),
}
}
}
fn deposit_asset(what: &Asset, who: &Location, _context: Option<&XcmContext>) -> XcmResult {
tracing::trace!(
target: "xcm::fungible_adapter",
?what, ?who,
"deposit_asset",
);
let amount = Matcher::matches_fungible(what).ok_or(MatchError::AssetNotHandled)?;
let who = AccountIdConverter::convert_location(who)
.ok_or(MatchError::AccountIdConversionFailed)?;
Fungible::mint_into(&who, amount).map_err(|error| {
tracing::debug!(
target: "xcm::fungible_adapter", ?error, ?who, ?amount,
"Failed to deposit assets",
);
XcmError::FailedToTransactAsset(error.into())
})?;
Ok(())
}
fn withdraw_asset(
what: &Asset,
who: &Location,
_context: Option<&XcmContext>,
) -> result::Result<AssetsInHolding, XcmError> {
tracing::trace!(
target: "xcm::fungible_adapter",
?what, ?who,
"withdraw_asset",
);
let amount = Matcher::matches_fungible(what).ok_or(MatchError::AssetNotHandled)?;
let who = AccountIdConverter::convert_location(who)
.ok_or(MatchError::AccountIdConversionFailed)?;
Fungible::burn_from(&who, amount, Expendable, Exact, Polite).map_err(|error| {
tracing::debug!(
target: "xcm::fungible_adapter", ?error, ?who, ?amount,
"Failed to withdraw assets",
);
XcmError::FailedToTransactAsset(error.into())
})?;
Ok(what.clone().into())
}
}
/// [`TransactAsset`] implementation that allows the use of a [`fungible`] implementation for
/// handling an asset in the XCM executor.
/// Works for everything, transfers and teleport bookkeeping.
pub struct FungibleAdapter<Fungible, Matcher, AccountIdConverter, AccountId, CheckingAccount>(
PhantomData<(Fungible, Matcher, AccountIdConverter, AccountId, CheckingAccount)>,
);
impl<
Fungible: fungible::Mutate<AccountId>,
Matcher: MatchesFungible<Fungible::Balance>,
AccountIdConverter: ConvertLocation<AccountId>,
AccountId: Eq + Clone + Debug,
CheckingAccount: Get<Option<(AccountId, MintLocation)>>,
> TransactAsset
for FungibleAdapter<Fungible, Matcher, AccountIdConverter, AccountId, CheckingAccount>
{
fn can_check_in(origin: &Location, what: &Asset, context: &XcmContext) -> XcmResult {
FungibleMutateAdapter::<
Fungible,
Matcher,
AccountIdConverter,
AccountId,
CheckingAccount,
>::can_check_in(origin, what, context)
}
fn check_in(origin: &Location, what: &Asset, context: &XcmContext) {
FungibleMutateAdapter::<
Fungible,
Matcher,
AccountIdConverter,
AccountId,
CheckingAccount,
>::check_in(origin, what, context)
}
fn can_check_out(dest: &Location, what: &Asset, context: &XcmContext) -> XcmResult {
FungibleMutateAdapter::<
Fungible,
Matcher,
AccountIdConverter,
AccountId,
CheckingAccount,
>::can_check_out(dest, what, context)
}
fn check_out(dest: &Location, what: &Asset, context: &XcmContext) {
FungibleMutateAdapter::<
Fungible,
Matcher,
AccountIdConverter,
AccountId,
CheckingAccount,
>::check_out(dest, what, context)
}
fn deposit_asset(what: &Asset, who: &Location, context: Option<&XcmContext>) -> XcmResult {
FungibleMutateAdapter::<
Fungible,
Matcher,
AccountIdConverter,
AccountId,
CheckingAccount,
>::deposit_asset(what, who, context)
}
fn withdraw_asset(
what: &Asset,
who: &Location,
maybe_context: Option<&XcmContext>,
) -> result::Result<AssetsInHolding, XcmError> {
FungibleMutateAdapter::<
Fungible,
Matcher,
AccountIdConverter,
AccountId,
CheckingAccount,
>::withdraw_asset(what, who, maybe_context)
}
fn internal_transfer_asset(
what: &Asset,
from: &Location,
to: &Location,
context: &XcmContext,
) -> result::Result<AssetsInHolding, XcmError> {
FungibleTransferAdapter::<Fungible, Matcher, AccountIdConverter, AccountId>::internal_transfer_asset(
what, from, to, context
)
}
}
@@ -0,0 +1,426 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
//! Adapters to work with [`frame_support::traits::fungibles`] through XCM.
use core::{fmt::Debug, marker::PhantomData, result};
use frame_support::traits::{
tokens::{
fungibles, Fortitude::Polite, Precision::Exact, Preservation::Expendable,
Provenance::Minted,
},
Contains, Get,
};
use xcm::latest::prelude::*;
use xcm_executor::traits::{ConvertLocation, Error as MatchError, MatchesFungibles, TransactAsset};
/// `TransactAsset` implementation to convert a `fungibles` implementation to become usable in XCM.
pub struct FungiblesTransferAdapter<Assets, Matcher, AccountIdConverter, AccountId>(
PhantomData<(Assets, Matcher, AccountIdConverter, AccountId)>,
);
impl<
Assets: fungibles::Mutate<AccountId>,
Matcher: MatchesFungibles<Assets::AssetId, Assets::Balance>,
AccountIdConverter: ConvertLocation<AccountId>,
AccountId: Eq + Clone + Debug, /* can't get away without it since Currency is generic
* over it. */
> TransactAsset for FungiblesTransferAdapter<Assets, Matcher, AccountIdConverter, AccountId>
{
fn internal_transfer_asset(
what: &Asset,
from: &Location,
to: &Location,
_context: &XcmContext,
) -> result::Result<xcm_executor::AssetsInHolding, XcmError> {
tracing::trace!(
target: "xcm::fungibles_adapter",
?what, ?from, ?to,
"internal_transfer_asset"
);
// Check we handle this asset.
let (asset_id, amount) = Matcher::matches_fungibles(what)?;
let source = AccountIdConverter::convert_location(from)
.ok_or(MatchError::AccountIdConversionFailed)?;
let dest = AccountIdConverter::convert_location(to)
.ok_or(MatchError::AccountIdConversionFailed)?;
Assets::transfer(asset_id.clone(), &source, &dest, amount, Expendable).map_err(|e| {
tracing::debug!(target: "xcm::fungibles_adapter", error = ?e, ?asset_id, ?source, ?dest, ?amount, "Failed internal transfer asset");
XcmError::FailedToTransactAsset(e.into())
})?;
Ok(what.clone().into())
}
}
/// The location which is allowed to mint a particular asset.
#[derive(Copy, Clone, Eq, PartialEq)]
pub enum MintLocation {
/// This chain is allowed to mint the asset. When we track teleports of the asset we ensure
/// that no more of the asset returns back to the chain than has been sent out.
Local,
/// This chain is not allowed to mint the asset. When we track teleports of the asset we ensure
/// that no more of the asset is sent out from the chain than has been previously received.
NonLocal,
}
/// Simple trait to indicate whether an asset is subject to having its teleportation into and out of
/// this chain recorded and if so in what `MintLocation`.
///
/// The overall purpose of asset-checking is to ensure either no more assets are teleported into a
/// chain than the outstanding balance of assets which were previously teleported out (as in the
/// case of locally-minted assets); or that no more assets are teleported out of a chain than the
/// outstanding balance of assets which have previously been teleported in (as in the case of chains
/// where the `asset` is not minted locally).
pub trait AssetChecking<AssetId> {
/// Return the teleportation asset-checking policy for the given `asset`. `None` implies no
/// checking. Otherwise the policy detailed by the inner `MintLocation` should be respected by
/// teleportation.
fn asset_checking(asset: &AssetId) -> Option<MintLocation>;
}
/// Implementation of `AssetChecking` which subjects no assets to having their teleportations
/// recorded.
pub struct NoChecking;
impl<AssetId> AssetChecking<AssetId> for NoChecking {
fn asset_checking(_: &AssetId) -> Option<MintLocation> {
None
}
}
/// Implementation of `AssetChecking` which subjects a given set of assets `T` to having their
/// teleportations recorded with a `MintLocation::Local`.
pub struct LocalMint<T>(core::marker::PhantomData<T>);
impl<AssetId, T: Contains<AssetId>> AssetChecking<AssetId> for LocalMint<T> {
fn asset_checking(asset: &AssetId) -> Option<MintLocation> {
match T::contains(asset) {
true => Some(MintLocation::Local),
false => None,
}
}
}
/// Implementation of `AssetChecking` which subjects a given set of assets `T` to having their
/// teleportations recorded with a `MintLocation::NonLocal`.
pub struct NonLocalMint<T>(core::marker::PhantomData<T>);
impl<AssetId, T: Contains<AssetId>> AssetChecking<AssetId> for NonLocalMint<T> {
fn asset_checking(asset: &AssetId) -> Option<MintLocation> {
match T::contains(asset) {
true => Some(MintLocation::NonLocal),
false => None,
}
}
}
/// Implementation of `AssetChecking` which subjects a given set of assets `L` to having their
/// teleportations recorded with a `MintLocation::Local` and a second set of assets `R` to having
/// their teleportations recorded with a `MintLocation::NonLocal`.
pub struct DualMint<L, R>(core::marker::PhantomData<(L, R)>);
impl<AssetId, L: Contains<AssetId>, R: Contains<AssetId>> AssetChecking<AssetId>
for DualMint<L, R>
{
fn asset_checking(asset: &AssetId) -> Option<MintLocation> {
if L::contains(asset) {
Some(MintLocation::Local)
} else if R::contains(asset) {
Some(MintLocation::NonLocal)
} else {
None
}
}
}
pub struct FungiblesMutateAdapter<
Assets,
Matcher,
AccountIdConverter,
AccountId,
CheckAsset,
CheckingAccount,
>(PhantomData<(Assets, Matcher, AccountIdConverter, AccountId, CheckAsset, CheckingAccount)>);
impl<
Assets: fungibles::Mutate<AccountId>,
Matcher: MatchesFungibles<Assets::AssetId, Assets::Balance>,
AccountIdConverter: ConvertLocation<AccountId>,
AccountId: Eq + Clone + Debug, /* can't get away without it since Currency is generic
* over it. */
CheckAsset: AssetChecking<Assets::AssetId>,
CheckingAccount: Get<AccountId>,
>
FungiblesMutateAdapter<Assets, Matcher, AccountIdConverter, AccountId, CheckAsset, CheckingAccount>
{
fn can_accrue_checked(asset_id: Assets::AssetId, amount: Assets::Balance) -> XcmResult {
let checking_account = CheckingAccount::get();
Assets::can_deposit(asset_id, &checking_account, amount, Minted)
.into_result()
.map_err(|error| {
tracing::debug!(
target: "xcm::fungibles_adapter", ?error, ?checking_account, ?amount,
"Failed to check if asset can be accrued"
);
XcmError::NotDepositable
})
}
fn can_reduce_checked(asset_id: Assets::AssetId, amount: Assets::Balance) -> XcmResult {
let checking_account = CheckingAccount::get();
Assets::can_withdraw(asset_id, &checking_account, amount)
.into_result(false)
.map_err(|error| {
tracing::debug!(
target: "xcm::fungibles_adapter", ?error, ?checking_account, ?amount,
"Failed to check if asset can be reduced"
);
XcmError::NotWithdrawable
})
.map(|_| ())
}
fn accrue_checked(asset_id: Assets::AssetId, amount: Assets::Balance) {
let checking_account = CheckingAccount::get();
let ok = Assets::mint_into(asset_id, &checking_account, amount).is_ok();
debug_assert!(ok, "`can_accrue_checked` must have returned `true` immediately prior; qed");
}
fn reduce_checked(asset_id: Assets::AssetId, amount: Assets::Balance) {
let checking_account = CheckingAccount::get();
let ok = Assets::burn_from(asset_id, &checking_account, amount, Expendable, Exact, Polite)
.is_ok();
debug_assert!(ok, "`can_reduce_checked` must have returned `true` immediately prior; qed");
}
}
impl<
Assets: fungibles::Mutate<AccountId>,
Matcher: MatchesFungibles<Assets::AssetId, Assets::Balance>,
AccountIdConverter: ConvertLocation<AccountId>,
AccountId: Eq + Clone + Debug, /* can't get away without it since Currency is generic
* over it. */
CheckAsset: AssetChecking<Assets::AssetId>,
CheckingAccount: Get<AccountId>,
> TransactAsset
for FungiblesMutateAdapter<
Assets,
Matcher,
AccountIdConverter,
AccountId,
CheckAsset,
CheckingAccount,
>
{
fn can_check_in(origin: &Location, what: &Asset, _context: &XcmContext) -> XcmResult {
tracing::trace!(
target: "xcm::fungibles_adapter",
?origin, ?what,
"can_check_in"
);
// Check we handle this asset.
let (asset_id, amount) = Matcher::matches_fungibles(what)?;
match CheckAsset::asset_checking(&asset_id) {
// We track this asset's teleports to ensure no more come in than have gone out.
Some(MintLocation::Local) => Self::can_reduce_checked(asset_id, amount),
// We track this asset's teleports to ensure no more go out than have come in.
Some(MintLocation::NonLocal) => Self::can_accrue_checked(asset_id, amount),
_ => Ok(()),
}
}
fn check_in(origin: &Location, what: &Asset, _context: &XcmContext) {
tracing::trace!(
target: "xcm::fungibles_adapter",
?origin, ?what,
"check_in"
);
if let Ok((asset_id, amount)) = Matcher::matches_fungibles(what) {
match CheckAsset::asset_checking(&asset_id) {
// We track this asset's teleports to ensure no more come in than have gone out.
Some(MintLocation::Local) => Self::reduce_checked(asset_id, amount),
// We track this asset's teleports to ensure no more go out than have come in.
Some(MintLocation::NonLocal) => Self::accrue_checked(asset_id, amount),
_ => (),
}
}
}
fn can_check_out(origin: &Location, what: &Asset, _context: &XcmContext) -> XcmResult {
tracing::trace!(
target: "xcm::fungibles_adapter",
?origin, ?what,
"can_check_out"
);
// Check we handle this asset.
let (asset_id, amount) = Matcher::matches_fungibles(what)?;
match CheckAsset::asset_checking(&asset_id) {
// We track this asset's teleports to ensure no more come in than have gone out.
Some(MintLocation::Local) => Self::can_accrue_checked(asset_id, amount),
// We track this asset's teleports to ensure no more go out than have come in.
Some(MintLocation::NonLocal) => Self::can_reduce_checked(asset_id, amount),
_ => Ok(()),
}
}
fn check_out(dest: &Location, what: &Asset, _context: &XcmContext) {
tracing::trace!(
target: "xcm::fungibles_adapter",
?dest, ?what,
"check_out"
);
if let Ok((asset_id, amount)) = Matcher::matches_fungibles(what) {
match CheckAsset::asset_checking(&asset_id) {
// We track this asset's teleports to ensure no more come in than have gone out.
Some(MintLocation::Local) => Self::accrue_checked(asset_id, amount),
// We track this asset's teleports to ensure no more go out than have come in.
Some(MintLocation::NonLocal) => Self::reduce_checked(asset_id, amount),
_ => (),
}
}
}
fn deposit_asset(what: &Asset, who: &Location, _context: Option<&XcmContext>) -> XcmResult {
tracing::trace!(
target: "xcm::fungibles_adapter",
?what, ?who,
"deposit_asset"
);
// Check we handle this asset.
let (asset_id, amount) = Matcher::matches_fungibles(what)?;
let who = AccountIdConverter::convert_location(who)
.ok_or(MatchError::AccountIdConversionFailed)?;
Assets::mint_into(asset_id, &who, amount).map_err(|error| {
tracing::debug!(target: "xcm::fungibles_adapter", ?error, ?who, ?amount, "Failed to deposit asset");
XcmError::FailedToTransactAsset(error.into())
})?;
Ok(())
}
fn withdraw_asset(
what: &Asset,
who: &Location,
_maybe_context: Option<&XcmContext>,
) -> result::Result<xcm_executor::AssetsInHolding, XcmError> {
tracing::trace!(
target: "xcm::fungibles_adapter",
?what, ?who,
"withdraw_asset"
);
// Check we handle this asset.
let (asset_id, amount) = Matcher::matches_fungibles(what)?;
let who = AccountIdConverter::convert_location(who)
.ok_or(MatchError::AccountIdConversionFailed)?;
Assets::burn_from(asset_id, &who, amount, Expendable, Exact, Polite).map_err(|error| {
tracing::debug!(target: "xcm::fungibles_adapter", ?error, ?who, ?amount, "Failed to withdraw asset");
XcmError::FailedToTransactAsset(error.into())
})?;
Ok(what.clone().into())
}
}
pub struct FungiblesAdapter<
Assets,
Matcher,
AccountIdConverter,
AccountId,
CheckAsset,
CheckingAccount,
>(PhantomData<(Assets, Matcher, AccountIdConverter, AccountId, CheckAsset, CheckingAccount)>);
impl<
Assets: fungibles::Mutate<AccountId>,
Matcher: MatchesFungibles<Assets::AssetId, Assets::Balance>,
AccountIdConverter: ConvertLocation<AccountId>,
AccountId: Eq + Clone + Debug, /* can't get away without it since Currency is generic
* over it. */
CheckAsset: AssetChecking<Assets::AssetId>,
CheckingAccount: Get<AccountId>,
> TransactAsset
for FungiblesAdapter<Assets, Matcher, AccountIdConverter, AccountId, CheckAsset, CheckingAccount>
{
fn can_check_in(origin: &Location, what: &Asset, context: &XcmContext) -> XcmResult {
FungiblesMutateAdapter::<
Assets,
Matcher,
AccountIdConverter,
AccountId,
CheckAsset,
CheckingAccount,
>::can_check_in(origin, what, context)
}
fn check_in(origin: &Location, what: &Asset, context: &XcmContext) {
FungiblesMutateAdapter::<
Assets,
Matcher,
AccountIdConverter,
AccountId,
CheckAsset,
CheckingAccount,
>::check_in(origin, what, context)
}
fn can_check_out(dest: &Location, what: &Asset, context: &XcmContext) -> XcmResult {
FungiblesMutateAdapter::<
Assets,
Matcher,
AccountIdConverter,
AccountId,
CheckAsset,
CheckingAccount,
>::can_check_out(dest, what, context)
}
fn check_out(dest: &Location, what: &Asset, context: &XcmContext) {
FungiblesMutateAdapter::<
Assets,
Matcher,
AccountIdConverter,
AccountId,
CheckAsset,
CheckingAccount,
>::check_out(dest, what, context)
}
fn deposit_asset(what: &Asset, who: &Location, context: Option<&XcmContext>) -> XcmResult {
FungiblesMutateAdapter::<
Assets,
Matcher,
AccountIdConverter,
AccountId,
CheckAsset,
CheckingAccount,
>::deposit_asset(what, who, context)
}
fn withdraw_asset(
what: &Asset,
who: &Location,
maybe_context: Option<&XcmContext>,
) -> result::Result<xcm_executor::AssetsInHolding, XcmError> {
FungiblesMutateAdapter::<
Assets,
Matcher,
AccountIdConverter,
AccountId,
CheckAsset,
CheckingAccount,
>::withdraw_asset(what, who, maybe_context)
}
fn internal_transfer_asset(
what: &Asset,
from: &Location,
to: &Location,
context: &XcmContext,
) -> result::Result<xcm_executor::AssetsInHolding, XcmError> {
FungiblesTransferAdapter::<Assets, Matcher, AccountIdConverter, AccountId>::internal_transfer_asset(
what, from, to, context
)
}
}
+158
View File
@@ -0,0 +1,158 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
//! # XCM-Builder
//!
//! Types and helpers for *building* XCM configuration.
#![cfg_attr(not(feature = "std"), no_std)]
extern crate alloc;
extern crate core;
#[cfg(test)]
mod tests;
#[cfg(feature = "std")]
pub mod test_utils;
mod asset_conversion;
#[allow(deprecated)]
pub use asset_conversion::ConvertedConcreteAssetId;
pub use asset_conversion::{
AsPrefixedGeneralIndex, ConvertedConcreteId, MatchClasslessInstances, MatchInClassInstances,
MatchedConvertedConcreteId,
};
mod asset_exchange;
pub use asset_exchange::SingleAssetExchangeAdapter;
mod barriers;
pub use barriers::{
AllowExplicitUnpaidExecutionFrom, AllowHrmpNotificationsFromRelayChain,
AllowKnownQueryResponses, AllowSubscriptionsFrom, AllowTopLevelPaidExecutionFrom,
AllowUnpaidExecutionFrom, DenyRecursively, DenyReserveTransferToRelayChain, DenyThenTry,
IsChildSystemTeyrchain, IsParentsOnly, IsSiblingSystemTeyrchain, RespectSuspension,
TakeWeightCredit, TrailingSetTopicAsId, WithComputedOrigin,
};
mod controller;
pub use controller::{
Controller, ExecuteController, ExecuteControllerWeightInfo, QueryController,
QueryControllerWeightInfo, QueryHandler, SendController, SendControllerWeightInfo,
};
mod currency_adapter;
#[allow(deprecated)]
pub use currency_adapter::CurrencyAdapter;
mod fee_handling;
pub use fee_handling::{
deposit_or_burn_fee, HandleFee, SendXcmFeeToAccount, XcmFeeManagerFromComponents,
};
mod filter_asset_location;
pub use filter_asset_location::{AllAssets, Case, LocationWithAssetFilters, NativeAsset};
mod fungible_adapter;
pub use fungible_adapter::{FungibleAdapter, FungibleMutateAdapter, FungibleTransferAdapter};
mod fungibles_adapter;
pub use fungibles_adapter::{
AssetChecking, DualMint, FungiblesAdapter, FungiblesMutateAdapter, FungiblesTransferAdapter,
LocalMint, MintLocation, NoChecking, NonLocalMint,
};
mod location_conversion;
#[allow(deprecated)]
pub use location_conversion::ForeignChainAliasAccount;
pub use location_conversion::{
Account32Hash, AccountId32Aliases, AccountKey20Aliases, AliasesIntoAccountId32,
ChildTeyrchainConvertsVia, DescribeAccountId32Terminal, DescribeAccountIdTerminal,
DescribeAccountKey20Terminal, DescribeAllTerminal, DescribeBodyTerminal, DescribeFamily,
DescribeLocation, DescribePalletTerminal, DescribeTerminus, DescribeTreasuryVoiceTerminal,
ExternalConsensusLocationsConverterFor, GlobalConsensusConvertsFor,
GlobalConsensusTeyrchainConvertsFor, HashedDescription, LocalTreasuryVoiceConvertsVia,
ParentIsPreset, SiblingTeyrchainConvertsVia,
};
mod matches_location;
pub use matches_location::{
StartsWith, StartsWithExplicitGlobalConsensus, WithLatestLocationConverter,
};
mod matches_token;
pub use matches_token::IsConcrete;
mod matcher;
pub use matcher::{CreateMatcher, MatchXcm, Matcher};
pub mod unique_instances;
mod nonfungibles_adapter;
pub use nonfungibles_adapter::{
NonFungiblesAdapter, NonFungiblesMutateAdapter, NonFungiblesTransferAdapter,
};
mod nonfungible_adapter;
pub use nonfungible_adapter::{
NonFungibleAdapter, NonFungibleMutateAdapter, NonFungibleTransferAdapter,
};
mod origin_aliases;
pub use origin_aliases::*;
mod origin_conversion;
pub use origin_conversion::{
BackingToPlurality, ChildSystemTeyrchainAsSuperuser, ChildTeyrchainAsNative, EnsureXcmOrigin,
LocationAsSuperuser, OriginToPluralityVoice, ParentAsSuperuser, RelayChainAsNative,
SiblingSystemTeyrchainAsSuperuser, SiblingTeyrchainAsNative, SignedAccountId32AsNative,
SignedAccountKey20AsNative, SignedToAccountId32, SovereignSignedViaLocation,
};
mod pay;
pub use pay::{
FixedLocation, LocatableAssetId, PayAccountId32OnChainOverXcm, PayOverXcm, PayOverXcmWithHelper,
};
mod process_xcm_message;
pub use process_xcm_message::ProcessXcmMessage;
mod routing;
pub use routing::{
EnsureDecodableXcm, EnsureDelivery, InspectMessageQueues, WithTopicSource, WithUniqueTopic,
};
mod transactional;
pub use transactional::FrameTransactionalProcessor;
#[allow(deprecated)]
pub use universal_exports::UnpaidLocalExporter;
mod transfer;
pub use transfer::{Transfer, TransferOverXcm, TransferOverXcmHelper, TransferStatus};
mod universal_exports;
pub use universal_exports::{
ensure_is_remote, BridgeBlobDispatcher, BridgeMessage, DispatchBlob, DispatchBlobError,
ExporterFor, HaulBlob, HaulBlobError, HaulBlobExporter, LocalExporter, NetworkExportTable,
NetworkExportTableItem, SovereignPaidRemoteExporter, UnpaidRemoteExporter,
};
mod weight;
pub use weight::{
FixedRateOfFungible, FixedWeightBounds, TakeRevenue, UsingComponents, WeightInfoBounds,
};
File diff suppressed because it is too large Load Diff
+205
View File
@@ -0,0 +1,205 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
//! XCM matcher API, used primarily for writing barrier conditions.
use core::ops::ControlFlow;
use frame_support::traits::ProcessMessageError;
use xcm::latest::{Instruction, Location};
/// Creates an instruction matcher from an XCM. Since XCM versions differ, we need to make a trait
/// here to unify the interfaces among them.
pub trait CreateMatcher {
/// The concrete matcher type.
type Matcher;
/// Method that creates and returns the matcher type from `Self`.
fn matcher(self) -> Self::Matcher;
}
impl<'a, Call> CreateMatcher for &'a mut [Instruction<Call>] {
type Matcher = Matcher<'a, Call>;
fn matcher(self) -> Self::Matcher {
let total_inst = self.len();
Matcher { xcm: self, current_idx: 0, total_inst }
}
}
/// API that allows to pattern-match against anything that is contained within an XCM.
///
/// The intended usage of the matcher API is to enable the ability to chain successive methods of
/// this trait together, along with the ? operator for the purpose of facilitating the writing,
/// maintenance and auditability of XCM barriers.
///
/// Example:
/// ```rust
/// use frame_support::traits::ProcessMessageError;
/// use xcm::latest::Instruction;
/// use staging_xcm_builder::{CreateMatcher, MatchXcm};
///
/// let mut msg = [Instruction::<()>::ClearOrigin];
/// let res = msg
/// .matcher()
/// .assert_remaining_insts(1)?
/// .match_next_inst(|inst| match inst {
/// Instruction::<()>::ClearOrigin => Ok(()),
/// _ => Err(ProcessMessageError::BadFormat),
/// });
/// assert!(res.is_ok());
///
/// Ok::<(), ProcessMessageError>(())
/// ```
pub trait MatchXcm {
/// The concrete instruction type. Necessary to specify as it changes between XCM versions.
type Inst;
/// The `Location` type. Necessary to specify as it changes between XCM versions.
type Loc;
/// The error type to throw when errors happen during matching.
type Error;
/// Returns success if the number of instructions that still have not been iterated over
/// equals `n`, otherwise returns an error.
fn assert_remaining_insts(self, n: usize) -> Result<Self, Self::Error>
where
Self: Sized;
/// Accepts a closure `f` that contains an argument signifying the next instruction to be
/// iterated over. The closure can then be used to check whether the instruction matches a
/// given condition, and can also be used to mutate the fields of an instruction.
///
/// The closure `f` returns success when the instruction passes the condition, otherwise it
/// returns an error, which will ultimately be returned by this function.
fn match_next_inst<F>(self, f: F) -> Result<Self, Self::Error>
where
Self: Sized,
F: FnMut(&mut Self::Inst) -> Result<(), Self::Error>;
/// Attempts to continuously iterate through the instructions while applying `f` to each of
/// them, until either the last instruction or `cond` returns false.
///
/// If `f` returns an error, then iteration halts and the function returns that error.
/// Otherwise, `f` returns a `ControlFlow` which signifies whether the iteration breaks or
/// continues.
fn match_next_inst_while<C, F>(self, cond: C, f: F) -> Result<Self, Self::Error>
where
Self: Sized,
C: Fn(&Self::Inst) -> bool,
F: FnMut(&mut Self::Inst) -> Result<ControlFlow<()>, Self::Error>;
/// Iterate instructions forward until `cond` returns false. When there are no more instructions
/// to be read, an error is returned.
fn skip_inst_while<C>(self, cond: C) -> Result<Self, Self::Error>
where
Self: Sized,
C: Fn(&Self::Inst) -> bool,
{
Self::match_next_inst_while(self, cond, |_| Ok(ControlFlow::Continue(())))
}
}
/// Struct created from calling `fn matcher()` on a mutable slice of `Instruction`s.
///
/// Implements `MatchXcm` to allow an iterator-like API to match against each `Instruction`
/// contained within the slice, which facilitates the building of XCM barriers.
pub struct Matcher<'a, Call> {
pub(crate) xcm: &'a mut [Instruction<Call>],
pub(crate) current_idx: usize,
pub(crate) total_inst: usize,
}
impl<'a, Call> MatchXcm for Matcher<'a, Call> {
type Error = ProcessMessageError;
type Inst = Instruction<Call>;
type Loc = Location;
fn assert_remaining_insts(self, n: usize) -> Result<Self, Self::Error>
where
Self: Sized,
{
if self.total_inst - self.current_idx != n {
return Err(ProcessMessageError::BadFormat);
}
Ok(self)
}
fn match_next_inst<F>(mut self, mut f: F) -> Result<Self, Self::Error>
where
Self: Sized,
F: FnMut(&mut Self::Inst) -> Result<(), Self::Error>,
{
if self.current_idx < self.total_inst {
f(&mut self.xcm[self.current_idx])?;
self.current_idx += 1;
Ok(self)
} else {
Err(ProcessMessageError::BadFormat)
}
}
fn match_next_inst_while<C, F>(mut self, cond: C, mut f: F) -> Result<Self, Self::Error>
where
Self: Sized,
C: Fn(&Self::Inst) -> bool,
F: FnMut(&mut Self::Inst) -> Result<ControlFlow<()>, Self::Error>,
{
if self.current_idx >= self.total_inst {
return Err(ProcessMessageError::BadFormat);
}
while self.current_idx < self.total_inst && cond(&self.xcm[self.current_idx]) {
if let ControlFlow::Break(()) = f(&mut self.xcm[self.current_idx])? {
break;
}
self.current_idx += 1;
}
Ok(self)
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::{vec, vec::Vec};
use xcm::latest::prelude::*;
#[test]
fn match_next_inst_works() {
let test_cases: Vec<(Vec<Instruction<()>>, bool)> =
vec![(vec![ClearOrigin], true), (vec![Trap(0)], false)];
for (mut xcm, expected) in test_cases.into_iter() {
let result = xcm.matcher().match_next_inst(|inst| match inst {
ClearOrigin => Ok(()),
_ => Err(ProcessMessageError::Unsupported),
});
assert_eq!(result.is_ok(), expected);
}
}
#[test]
fn match_next_inst_while_works() {
let mut xcm: Vec<Instruction<()>> = vec![ClearOrigin];
let _ = xcm
.matcher()
.match_next_inst_while(|_| true, |_| Ok(ControlFlow::Continue(())))
.unwrap();
}
}
@@ -0,0 +1,73 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
//! Various implementations and utilities for matching and filtering `Location` and
//! `InteriorLocation` types.
use core::marker::PhantomData;
use frame_support::traits::{Contains, Get};
use sp_runtime::traits::MaybeEquivalence;
use xcm::latest::{InteriorLocation, Location, NetworkId};
/// An implementation of `Contains` that checks for `Location` or
/// `InteriorLocation` if starts with the provided type `T`.
pub struct StartsWith<T, L = Location>(core::marker::PhantomData<(T, L)>);
impl<T: Get<L>, L: TryInto<Location> + Clone> Contains<L> for StartsWith<T, L> {
fn contains(location: &L) -> bool {
let latest_location: Location = if let Ok(location) = (*location).clone().try_into() {
location
} else {
return false;
};
let latest_t = if let Ok(location) = T::get().try_into() { location } else { return false };
latest_location.starts_with(&latest_t)
}
}
impl<T: Get<InteriorLocation>> Contains<InteriorLocation> for StartsWith<T> {
fn contains(t: &InteriorLocation) -> bool {
t.starts_with(&T::get())
}
}
/// An implementation of `Contains` that checks for `Location` or
/// `InteriorLocation` if starts with expected `GlobalConsensus(NetworkId)` provided as type
/// `T`.
pub struct StartsWithExplicitGlobalConsensus<T>(core::marker::PhantomData<T>);
impl<T: Get<NetworkId>> Contains<Location> for StartsWithExplicitGlobalConsensus<T> {
fn contains(location: &Location) -> bool {
matches!(location.interior().global_consensus(), Ok(requested_network) if requested_network.eq(&T::get()))
}
}
impl<T: Get<NetworkId>> Contains<InteriorLocation> for StartsWithExplicitGlobalConsensus<T> {
fn contains(location: &InteriorLocation) -> bool {
matches!(location.global_consensus(), Ok(requested_network) if requested_network.eq(&T::get()))
}
}
/// An adapter implementation of `MaybeEquivalence` which can convert between the latest `Location`
/// and other versions that implement `TryInto<Location>` and `TryFrom<Location>`.
pub struct WithLatestLocationConverter<Target>(PhantomData<Target>);
impl<Target: TryInto<Location> + TryFrom<Location> + Clone> MaybeEquivalence<Location, Target>
for WithLatestLocationConverter<Target>
{
fn convert(old: &Location) -> Option<Target> {
(*old).clone().try_into().ok()
}
fn convert_back(new: &Target) -> Option<Location> {
new.clone().try_into().ok()
}
}
@@ -0,0 +1,64 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
//! Various implementations for the `MatchesFungible` trait.
use core::marker::PhantomData;
use frame_support::traits::Get;
use xcm::latest::{
Asset, AssetId, AssetInstance,
Fungibility::{Fungible, NonFungible},
Location,
};
use xcm_executor::traits::{MatchesFungible, MatchesNonFungible};
/// Converts a `Asset` into balance `B` if its id is equal to that
/// given by `T`'s `Get`.
///
/// # Example
///
/// ```
/// use xcm::latest::{Location, Parent};
/// use staging_xcm_builder::IsConcrete;
/// use xcm_executor::traits::MatchesFungible;
///
/// frame_support::parameter_types! {
/// pub TargetLocation: Location = Parent.into();
/// }
///
/// # fn main() {
/// let asset = (Parent, 999).into();
/// // match `asset` if it is a concrete asset in `TargetLocation`.
/// assert_eq!(<IsConcrete<TargetLocation> as MatchesFungible<u128>>::matches_fungible(&asset), Some(999));
/// # }
/// ```
pub struct IsConcrete<T>(PhantomData<T>);
impl<T: Get<Location>, B: TryFrom<u128>> MatchesFungible<B> for IsConcrete<T> {
fn matches_fungible(a: &Asset) -> Option<B> {
match (&a.id, &a.fun) {
(AssetId(ref id), Fungible(ref amount)) if id == &T::get() => (*amount).try_into().ok(),
_ => None,
}
}
}
impl<T: Get<Location>, I: TryFrom<AssetInstance>> MatchesNonFungible<I> for IsConcrete<T> {
fn matches_nonfungible(a: &Asset) -> Option<I> {
match (&a.id, &a.fun) {
(AssetId(id), NonFungible(instance)) if id == &T::get() => (*instance).try_into().ok(),
_ => None,
}
}
}
@@ -0,0 +1,350 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
//! Adapters to work with [`frame_support::traits::tokens::nonfungible`] through XCM.
use crate::MintLocation;
use core::{fmt::Debug, marker::PhantomData, result};
use frame_support::{
ensure,
traits::{tokens::nonfungible, Get},
};
use xcm::latest::prelude::*;
use xcm_executor::traits::{
ConvertLocation, Error as MatchError, MatchesNonFungible, TransactAsset,
};
const LOG_TARGET: &str = "xcm::nonfungible_adapter";
/// [`TransactAsset`] implementation that allows the use of a [`nonfungible`] implementation for
/// handling an asset in the XCM executor.
/// Only works for transfers.
pub struct NonFungibleTransferAdapter<NonFungible, Matcher, AccountIdConverter, AccountId>(
PhantomData<(NonFungible, Matcher, AccountIdConverter, AccountId)>,
)
where
NonFungible: nonfungible::Transfer<AccountId>;
impl<
NonFungible: nonfungible::Transfer<AccountId>,
Matcher: MatchesNonFungible<NonFungible::ItemId>,
AccountIdConverter: ConvertLocation<AccountId>,
AccountId: Clone + Debug, // can't get away without it since Currency is generic over it.
> TransactAsset for NonFungibleTransferAdapter<NonFungible, Matcher, AccountIdConverter, AccountId>
where
NonFungible::ItemId: Debug,
{
fn transfer_asset(
what: &Asset,
from: &Location,
to: &Location,
context: &XcmContext,
) -> result::Result<xcm_executor::AssetsInHolding, XcmError> {
tracing::trace!(
target: LOG_TARGET,
?what,
?from,
?to,
?context,
"transfer_asset",
);
// Check we handle this asset.
let instance = Matcher::matches_nonfungible(what).ok_or(MatchError::AssetNotHandled)?;
let destination = AccountIdConverter::convert_location(to)
.ok_or(MatchError::AccountIdConversionFailed)?;
NonFungible::transfer(&instance, &destination).map_err(|e| {
tracing::debug!(target: LOG_TARGET, ?e, ?instance, ?destination, "Failed to transfer non-fungible asset");
XcmError::FailedToTransactAsset(e.into())
})?;
Ok(what.clone().into())
}
}
/// [`TransactAsset`] implementation that allows the use of a [`nonfungible`] implementation for
/// handling an asset in the XCM executor.
/// Works for teleport bookkeeping.
pub struct NonFungibleMutateAdapter<
NonFungible,
Matcher,
AccountIdConverter,
AccountId,
CheckingAccount,
>(PhantomData<(NonFungible, Matcher, AccountIdConverter, AccountId, CheckingAccount)>)
where
NonFungible: nonfungible::Mutate<AccountId>,
NonFungible::ItemId: Debug;
impl<
NonFungible: nonfungible::Mutate<AccountId>,
Matcher: MatchesNonFungible<NonFungible::ItemId>,
AccountIdConverter: ConvertLocation<AccountId>,
AccountId: Clone + Eq + Debug, /* can't get away without it since Currency is generic
* over it. */
CheckingAccount: Get<Option<(AccountId, MintLocation)>>,
> NonFungibleMutateAdapter<NonFungible, Matcher, AccountIdConverter, AccountId, CheckingAccount>
where
NonFungible::ItemId: Debug,
{
fn can_accrue_checked(instance: NonFungible::ItemId) -> XcmResult {
ensure!(NonFungible::owner(&instance).is_none(), XcmError::NotDepositable);
Ok(())
}
fn can_reduce_checked(checking_account: AccountId, instance: NonFungible::ItemId) -> XcmResult {
// This is an asset whose teleports we track.
let owner = NonFungible::owner(&instance);
ensure!(owner == Some(checking_account), XcmError::NotWithdrawable);
ensure!(NonFungible::can_transfer(&instance), XcmError::NotWithdrawable);
Ok(())
}
fn accrue_checked(checking_account: AccountId, instance: NonFungible::ItemId) {
let ok = NonFungible::mint_into(&instance, &checking_account).is_ok();
debug_assert!(ok, "`mint_into` cannot generally fail; qed");
}
fn reduce_checked(instance: NonFungible::ItemId) {
let ok = NonFungible::burn(&instance, None).is_ok();
debug_assert!(ok, "`can_check_in` must have returned `true` immediately prior; qed");
}
}
impl<
NonFungible: nonfungible::Mutate<AccountId>,
Matcher: MatchesNonFungible<NonFungible::ItemId>,
AccountIdConverter: ConvertLocation<AccountId>,
AccountId: Clone + Eq + Debug, /* can't get away without it since Currency is generic
* over it. */
CheckingAccount: Get<Option<(AccountId, MintLocation)>>,
> TransactAsset
for NonFungibleMutateAdapter<NonFungible, Matcher, AccountIdConverter, AccountId, CheckingAccount>
where
NonFungible::ItemId: Debug,
{
fn can_check_in(origin: &Location, what: &Asset, context: &XcmContext) -> XcmResult {
tracing::trace!(
target: LOG_TARGET,
?origin,
?what,
?context,
"can_check_in",
);
// Check we handle this asset.
let instance = Matcher::matches_nonfungible(what).ok_or(MatchError::AssetNotHandled)?;
match CheckingAccount::get() {
// We track this asset's teleports to ensure no more come in than have gone out.
Some((checking_account, MintLocation::Local)) =>
Self::can_reduce_checked(checking_account, instance),
// We track this asset's teleports to ensure no more go out than have come in.
Some((_, MintLocation::NonLocal)) => Self::can_accrue_checked(instance),
_ => Ok(()),
}
}
fn check_in(origin: &Location, what: &Asset, context: &XcmContext) {
tracing::trace!(
target: LOG_TARGET,
?origin,
?what,
?context,
"check_in",
);
if let Some(instance) = Matcher::matches_nonfungible(what) {
match CheckingAccount::get() {
// We track this asset's teleports to ensure no more come in than have gone out.
Some((_, MintLocation::Local)) => Self::reduce_checked(instance),
// We track this asset's teleports to ensure no more go out than have come in.
Some((checking_account, MintLocation::NonLocal)) =>
Self::accrue_checked(checking_account, instance),
_ => (),
}
}
}
fn can_check_out(dest: &Location, what: &Asset, context: &XcmContext) -> XcmResult {
tracing::trace!(
target: LOG_TARGET,
?dest,
?what,
?context,
"can_check_out",
);
// Check we handle this asset.
let instance = Matcher::matches_nonfungible(what).ok_or(MatchError::AssetNotHandled)?;
match CheckingAccount::get() {
// We track this asset's teleports to ensure no more come in than have gone out.
Some((_, MintLocation::Local)) => Self::can_accrue_checked(instance),
// We track this asset's teleports to ensure no more go out than have come in.
Some((checking_account, MintLocation::NonLocal)) =>
Self::can_reduce_checked(checking_account, instance),
_ => Ok(()),
}
}
fn check_out(dest: &Location, what: &Asset, context: &XcmContext) {
tracing::trace!(
target: LOG_TARGET,
?dest,
?what,
?context,
"check_out",
);
if let Some(instance) = Matcher::matches_nonfungible(what) {
match CheckingAccount::get() {
// We track this asset's teleports to ensure no more come in than have gone out.
Some((checking_account, MintLocation::Local)) =>
Self::accrue_checked(checking_account, instance),
// We track this asset's teleports to ensure no more go out than have come in.
Some((_, MintLocation::NonLocal)) => Self::reduce_checked(instance),
_ => (),
}
}
}
fn deposit_asset(what: &Asset, who: &Location, context: Option<&XcmContext>) -> XcmResult {
tracing::trace!(
target: LOG_TARGET,
?what,
?who,
?context,
"deposit_asset",
);
// Check we handle this asset.
let instance = Matcher::matches_nonfungible(what).ok_or(MatchError::AssetNotHandled)?;
let who = AccountIdConverter::convert_location(who)
.ok_or(MatchError::AccountIdConversionFailed)?;
NonFungible::mint_into(&instance, &who).map_err(|e| {
tracing::debug!(target: LOG_TARGET, ?e, ?instance, ?who, "Failed to mint asset");
XcmError::FailedToTransactAsset(e.into())
})
}
fn withdraw_asset(
what: &Asset,
who: &Location,
maybe_context: Option<&XcmContext>,
) -> result::Result<xcm_executor::AssetsInHolding, XcmError> {
tracing::trace!(
target: LOG_TARGET,
?what,
?who,
?maybe_context,
"withdraw_asset"
);
// Check we handle this asset.
let who = AccountIdConverter::convert_location(who)
.ok_or(MatchError::AccountIdConversionFailed)?;
let instance = Matcher::matches_nonfungible(what).ok_or(MatchError::AssetNotHandled)?;
NonFungible::burn(&instance, Some(&who)).map_err(|e| {
tracing::debug!(target: LOG_TARGET, ?e, ?instance, ?who, "Failed to burn asset");
XcmError::FailedToTransactAsset(e.into())
})?;
Ok(what.clone().into())
}
}
/// [`TransactAsset`] implementation that allows the use of a [`nonfungible`] implementation for
/// handling an asset in the XCM executor.
/// Works for everything.
pub struct NonFungibleAdapter<NonFungible, Matcher, AccountIdConverter, AccountId, CheckingAccount>(
PhantomData<(NonFungible, Matcher, AccountIdConverter, AccountId, CheckingAccount)>,
)
where
NonFungible: nonfungible::Mutate<AccountId> + nonfungible::Transfer<AccountId>,
NonFungible::ItemId: Debug;
impl<
NonFungible: nonfungible::Mutate<AccountId> + nonfungible::Transfer<AccountId>,
Matcher: MatchesNonFungible<NonFungible::ItemId>,
AccountIdConverter: ConvertLocation<AccountId>,
AccountId: Clone + Eq + Debug, /* can't get away without it since Currency is generic
* over it. */
CheckingAccount: Get<Option<(AccountId, MintLocation)>>,
> TransactAsset
for NonFungibleAdapter<NonFungible, Matcher, AccountIdConverter, AccountId, CheckingAccount>
where
NonFungible::ItemId: Debug,
{
fn can_check_in(origin: &Location, what: &Asset, context: &XcmContext) -> XcmResult {
NonFungibleMutateAdapter::<
NonFungible,
Matcher,
AccountIdConverter,
AccountId,
CheckingAccount,
>::can_check_in(origin, what, context)
}
fn check_in(origin: &Location, what: &Asset, context: &XcmContext) {
NonFungibleMutateAdapter::<
NonFungible,
Matcher,
AccountIdConverter,
AccountId,
CheckingAccount,
>::check_in(origin, what, context)
}
fn can_check_out(dest: &Location, what: &Asset, context: &XcmContext) -> XcmResult {
NonFungibleMutateAdapter::<
NonFungible,
Matcher,
AccountIdConverter,
AccountId,
CheckingAccount,
>::can_check_out(dest, what, context)
}
fn check_out(dest: &Location, what: &Asset, context: &XcmContext) {
NonFungibleMutateAdapter::<
NonFungible,
Matcher,
AccountIdConverter,
AccountId,
CheckingAccount,
>::check_out(dest, what, context)
}
fn deposit_asset(what: &Asset, who: &Location, context: Option<&XcmContext>) -> XcmResult {
NonFungibleMutateAdapter::<
NonFungible,
Matcher,
AccountIdConverter,
AccountId,
CheckingAccount,
>::deposit_asset(what, who, context)
}
fn withdraw_asset(
what: &Asset,
who: &Location,
maybe_context: Option<&XcmContext>,
) -> result::Result<xcm_executor::AssetsInHolding, XcmError> {
NonFungibleMutateAdapter::<
NonFungible,
Matcher,
AccountIdConverter,
AccountId,
CheckingAccount,
>::withdraw_asset(what, who, maybe_context)
}
fn transfer_asset(
what: &Asset,
from: &Location,
to: &Location,
context: &XcmContext,
) -> result::Result<xcm_executor::AssetsInHolding, XcmError> {
NonFungibleTransferAdapter::<NonFungible, Matcher, AccountIdConverter, AccountId>::transfer_asset(
what, from, to, context,
)
}
}
@@ -0,0 +1,387 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
//! Adapters to work with [`frame_support::traits::tokens::nonfungibles`] through XCM.
use crate::{AssetChecking, MintLocation};
use core::{fmt::Debug, marker::PhantomData, result};
use frame_support::{
ensure,
traits::{tokens::nonfungibles, Get},
};
use xcm::latest::prelude::*;
use xcm_executor::traits::{
ConvertLocation, Error as MatchError, MatchesNonFungibles, TransactAsset,
};
const LOG_TARGET: &str = "xcm::nonfungibles_adapter";
/// [`TransactAsset`] implementation that allows the use of a [`nonfungibles`] implementation for
/// handling an asset in the XCM executor.
/// Only works for transfers.
pub struct NonFungiblesTransferAdapter<Assets, Matcher, AccountIdConverter, AccountId>(
PhantomData<(Assets, Matcher, AccountIdConverter, AccountId)>,
)
where
Assets: nonfungibles::Transfer<AccountId>,
Assets::CollectionId: Debug,
Assets::ItemId: Debug;
impl<
Assets: nonfungibles::Transfer<AccountId>,
Matcher: MatchesNonFungibles<Assets::CollectionId, Assets::ItemId>,
AccountIdConverter: ConvertLocation<AccountId>,
AccountId: Clone + Debug, // can't get away without it since Currency is generic over it.
> TransactAsset for NonFungiblesTransferAdapter<Assets, Matcher, AccountIdConverter, AccountId>
where
Assets::CollectionId: Debug,
Assets::ItemId: Debug,
{
fn transfer_asset(
what: &Asset,
from: &Location,
to: &Location,
context: &XcmContext,
) -> result::Result<xcm_executor::AssetsInHolding, XcmError> {
tracing::trace!(
target: LOG_TARGET,
?what,
?from,
?to,
?context,
"transfer_asset",
);
// Check we handle this asset.
let (class, instance) = Matcher::matches_nonfungibles(what)?;
let destination = AccountIdConverter::convert_location(to)
.ok_or(MatchError::AccountIdConversionFailed)?;
Assets::transfer(&class, &instance, &destination).map_err(|e| {
tracing::debug!(target: LOG_TARGET, ?e, ?class, ?instance, ?destination, "Failed to transfer asset");
XcmError::FailedToTransactAsset(e.into())
})?;
Ok(what.clone().into())
}
}
/// [`TransactAsset`] implementation that allows the use of a [`nonfungibles`] implementation for
/// handling an asset in the XCM executor.
/// Only works for teleport bookkeeping.
pub struct NonFungiblesMutateAdapter<
Assets,
Matcher,
AccountIdConverter,
AccountId,
CheckAsset,
CheckingAccount,
>(PhantomData<(Assets, Matcher, AccountIdConverter, AccountId, CheckAsset, CheckingAccount)>);
impl<
Assets: nonfungibles::Mutate<AccountId>,
Matcher: MatchesNonFungibles<Assets::CollectionId, Assets::ItemId>,
AccountIdConverter: ConvertLocation<AccountId>,
AccountId: Clone + Eq, // can't get away without it since Currency is generic over it.
CheckAsset: AssetChecking<Assets::CollectionId>,
CheckingAccount: Get<Option<AccountId>>,
>
NonFungiblesMutateAdapter<
Assets,
Matcher,
AccountIdConverter,
AccountId,
CheckAsset,
CheckingAccount,
>
{
fn can_accrue_checked(class: Assets::CollectionId, instance: Assets::ItemId) -> XcmResult {
ensure!(Assets::owner(&class, &instance).is_none(), XcmError::NotDepositable);
Ok(())
}
fn can_reduce_checked(class: Assets::CollectionId, instance: Assets::ItemId) -> XcmResult {
if let Some(checking_account) = CheckingAccount::get() {
// This is an asset whose teleports we track.
let owner = Assets::owner(&class, &instance);
ensure!(owner == Some(checking_account), XcmError::NotWithdrawable);
ensure!(Assets::can_transfer(&class, &instance), XcmError::NotWithdrawable);
}
Ok(())
}
fn accrue_checked(class: Assets::CollectionId, instance: Assets::ItemId) {
if let Some(checking_account) = CheckingAccount::get() {
let ok = Assets::mint_into(&class, &instance, &checking_account).is_ok();
debug_assert!(ok, "`mint_into` cannot generally fail; qed");
}
}
fn reduce_checked(class: Assets::CollectionId, instance: Assets::ItemId) {
let ok = Assets::burn(&class, &instance, None).is_ok();
debug_assert!(ok, "`can_check_in` must have returned `true` immediately prior; qed");
}
}
impl<
Assets: nonfungibles::Mutate<AccountId>,
Matcher: MatchesNonFungibles<Assets::CollectionId, Assets::ItemId>,
AccountIdConverter: ConvertLocation<AccountId>,
AccountId: Clone + Eq + Debug, /* can't get away without it since Currency is generic
* over it. */
CheckAsset: AssetChecking<Assets::CollectionId>,
CheckingAccount: Get<Option<AccountId>>,
> TransactAsset
for NonFungiblesMutateAdapter<
Assets,
Matcher,
AccountIdConverter,
AccountId,
CheckAsset,
CheckingAccount,
>
where
Assets::CollectionId: Debug,
Assets::ItemId: Debug,
{
fn can_check_in(origin: &Location, what: &Asset, context: &XcmContext) -> XcmResult {
tracing::trace!(
target: LOG_TARGET,
?origin,
?what,
?context,
"can_check_in",
);
// Check we handle this asset.
let (class, instance) = Matcher::matches_nonfungibles(what)?;
match CheckAsset::asset_checking(&class) {
// We track this asset's teleports to ensure no more come in than have gone out.
Some(MintLocation::Local) => Self::can_reduce_checked(class, instance),
// We track this asset's teleports to ensure no more go out than have come in.
Some(MintLocation::NonLocal) => Self::can_accrue_checked(class, instance),
_ => Ok(()),
}
}
fn check_in(origin: &Location, what: &Asset, context: &XcmContext) {
tracing::trace!(
target: LOG_TARGET,
?origin,
?what,
?context,
"check_in",
);
if let Ok((class, instance)) = Matcher::matches_nonfungibles(what) {
match CheckAsset::asset_checking(&class) {
// We track this asset's teleports to ensure no more come in than have gone out.
Some(MintLocation::Local) => Self::reduce_checked(class, instance),
// We track this asset's teleports to ensure no more go out than have come in.
Some(MintLocation::NonLocal) => Self::accrue_checked(class, instance),
_ => (),
}
}
}
fn can_check_out(dest: &Location, what: &Asset, context: &XcmContext) -> XcmResult {
tracing::trace!(
target: LOG_TARGET,
?dest,
?what,
?context,
"can_check_out",
);
// Check we handle this asset.
let (class, instance) = Matcher::matches_nonfungibles(what)?;
match CheckAsset::asset_checking(&class) {
// We track this asset's teleports to ensure no more come in than have gone out.
Some(MintLocation::Local) => Self::can_accrue_checked(class, instance),
// We track this asset's teleports to ensure no more go out than have come in.
Some(MintLocation::NonLocal) => Self::can_reduce_checked(class, instance),
_ => Ok(()),
}
}
fn check_out(dest: &Location, what: &Asset, context: &XcmContext) {
tracing::trace!(
target: LOG_TARGET,
?dest,
?what,
?context,
"check_out",
);
if let Ok((class, instance)) = Matcher::matches_nonfungibles(what) {
match CheckAsset::asset_checking(&class) {
// We track this asset's teleports to ensure no more come in than have gone out.
Some(MintLocation::Local) => Self::accrue_checked(class, instance),
// We track this asset's teleports to ensure no more go out than have come in.
Some(MintLocation::NonLocal) => Self::reduce_checked(class, instance),
_ => (),
}
}
}
fn deposit_asset(what: &Asset, who: &Location, context: Option<&XcmContext>) -> XcmResult {
tracing::trace!(
target: LOG_TARGET,
?what,
?who,
?context,
"deposit_asset",
);
// Check we handle this asset.
let (class, instance) = Matcher::matches_nonfungibles(what)?;
let who = AccountIdConverter::convert_location(who)
.ok_or(MatchError::AccountIdConversionFailed)?;
Assets::mint_into(&class, &instance, &who).map_err(|e| {
tracing::debug!(target: LOG_TARGET, ?e, ?class, ?instance, ?who, "Failed to mint asset");
XcmError::FailedToTransactAsset(e.into())
})
}
fn withdraw_asset(
what: &Asset,
who: &Location,
maybe_context: Option<&XcmContext>,
) -> result::Result<xcm_executor::AssetsInHolding, XcmError> {
tracing::trace!(
target: LOG_TARGET,
?what,
?who,
?maybe_context,
"withdraw_asset",
);
// Check we handle this asset.
let who = AccountIdConverter::convert_location(who)
.ok_or(MatchError::AccountIdConversionFailed)?;
let (class, instance) = Matcher::matches_nonfungibles(what)?;
Assets::burn(&class, &instance, Some(&who)).map_err(|e| {
tracing::debug!(target: LOG_TARGET, ?e, ?class, ?instance, ?who, "Failed to burn asset");
XcmError::FailedToTransactAsset(e.into())
})?;
Ok(what.clone().into())
}
}
/// [`TransactAsset`] implementation that allows the use of a [`nonfungibles`] implementation for
/// handling an asset in the XCM executor.
/// Works for everything.
pub struct NonFungiblesAdapter<
Assets,
Matcher,
AccountIdConverter,
AccountId,
CheckAsset,
CheckingAccount,
>(PhantomData<(Assets, Matcher, AccountIdConverter, AccountId, CheckAsset, CheckingAccount)>)
where
Assets: nonfungibles::Transfer<AccountId>,
Assets::CollectionId: Debug,
Assets::ItemId: Debug;
impl<
Assets: nonfungibles::Mutate<AccountId> + nonfungibles::Transfer<AccountId>,
Matcher: MatchesNonFungibles<Assets::CollectionId, Assets::ItemId>,
AccountIdConverter: ConvertLocation<AccountId>,
AccountId: Clone + Eq + Debug, /* can't get away without it since Currency is generic
* over it. */
CheckAsset: AssetChecking<Assets::CollectionId>,
CheckingAccount: Get<Option<AccountId>>,
> TransactAsset
for NonFungiblesAdapter<
Assets,
Matcher,
AccountIdConverter,
AccountId,
CheckAsset,
CheckingAccount,
>
where
Assets::CollectionId: Debug,
Assets::ItemId: Debug,
{
fn can_check_in(origin: &Location, what: &Asset, context: &XcmContext) -> XcmResult {
NonFungiblesMutateAdapter::<
Assets,
Matcher,
AccountIdConverter,
AccountId,
CheckAsset,
CheckingAccount,
>::can_check_in(origin, what, context)
}
fn check_in(origin: &Location, what: &Asset, context: &XcmContext) {
NonFungiblesMutateAdapter::<
Assets,
Matcher,
AccountIdConverter,
AccountId,
CheckAsset,
CheckingAccount,
>::check_in(origin, what, context)
}
fn can_check_out(dest: &Location, what: &Asset, context: &XcmContext) -> XcmResult {
NonFungiblesMutateAdapter::<
Assets,
Matcher,
AccountIdConverter,
AccountId,
CheckAsset,
CheckingAccount,
>::can_check_out(dest, what, context)
}
fn check_out(dest: &Location, what: &Asset, context: &XcmContext) {
NonFungiblesMutateAdapter::<
Assets,
Matcher,
AccountIdConverter,
AccountId,
CheckAsset,
CheckingAccount,
>::check_out(dest, what, context)
}
fn deposit_asset(what: &Asset, who: &Location, context: Option<&XcmContext>) -> XcmResult {
NonFungiblesMutateAdapter::<
Assets,
Matcher,
AccountIdConverter,
AccountId,
CheckAsset,
CheckingAccount,
>::deposit_asset(what, who, context)
}
fn withdraw_asset(
what: &Asset,
who: &Location,
maybe_context: Option<&XcmContext>,
) -> result::Result<xcm_executor::AssetsInHolding, XcmError> {
NonFungiblesMutateAdapter::<
Assets,
Matcher,
AccountIdConverter,
AccountId,
CheckAsset,
CheckingAccount,
>::withdraw_asset(what, who, maybe_context)
}
fn transfer_asset(
what: &Asset,
from: &Location,
to: &Location,
context: &XcmContext,
) -> result::Result<xcm_executor::AssetsInHolding, XcmError> {
NonFungiblesTransferAdapter::<Assets, Matcher, AccountIdConverter, AccountId>::transfer_asset(
what, from, to, context,
)
}
}
@@ -0,0 +1,71 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
//! Implementation for `ContainsPair<Location, Location>`.
use core::marker::PhantomData;
use frame_support::traits::{Contains, ContainsPair, Get};
use xcm::latest::prelude::*;
/// Alias a Foreign `AccountId32` with a local `AccountId32` if the foreign `AccountId32` matches
/// the `Prefix` pattern.
///
/// Requires that the prefixed origin `AccountId32` matches the target `AccountId32`.
pub struct AliasForeignAccountId32<Prefix>(PhantomData<Prefix>);
impl<Prefix: Contains<Location>> ContainsPair<Location, Location>
for AliasForeignAccountId32<Prefix>
{
fn contains(origin: &Location, target: &Location) -> bool {
if let (prefix, Some(account_id @ AccountId32 { .. })) =
origin.clone().split_last_interior()
{
return Prefix::contains(&prefix) &&
*target == Location { parents: 0, interior: [account_id].into() };
}
false
}
}
/// Alias a descendant location of the original origin.
pub struct AliasChildLocation;
impl ContainsPair<Location, Location> for AliasChildLocation {
fn contains(origin: &Location, target: &Location) -> bool {
return target.starts_with(origin);
}
}
/// Alias a location if it passes `Filter` and the original origin is root of `Origin`.
///
/// This can be used to allow (trusted) system chains root to alias into other locations.
/// **Warning**: do not use with untrusted `Origin` chains.
pub struct AliasOriginRootUsingFilter<Origin, Filter>(PhantomData<(Origin, Filter)>);
impl<Origin, Filter> ContainsPair<Location, Location> for AliasOriginRootUsingFilter<Origin, Filter>
where
Origin: Get<Location>,
Filter: Contains<Location>,
{
fn contains(origin: &Location, target: &Location) -> bool {
// check that `origin` is a root location
match origin.unpack() {
(1, [Teyrchain(_)]) |
(2, [GlobalConsensus(_)]) |
(2, [GlobalConsensus(_), Teyrchain(_)]) => (),
_ => return false,
};
// check that `origin` matches `Origin` and `target` matches `Filter`
return Origin::get().eq(origin) && Filter::contains(target);
}
}
@@ -0,0 +1,453 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
//! Various implementations for `ConvertOrigin`.
use core::marker::PhantomData;
use frame_support::traits::{Contains, EnsureOrigin, Get, GetBacking, OriginTrait};
use frame_system::RawOrigin as SystemRawOrigin;
use pezkuwi_teyrchain_primitives::primitives::IsSystem;
use sp_runtime::traits::TryConvert;
use xcm::latest::{BodyId, BodyPart, Junction, Junctions::*, Location, NetworkId, OriginKind};
use xcm_executor::traits::{ConvertLocation, ConvertOrigin};
/// Sovereign accounts use the system's `Signed` origin with an account ID derived from the
/// `LocationConverter`.
pub struct SovereignSignedViaLocation<LocationConverter, RuntimeOrigin>(
PhantomData<(LocationConverter, RuntimeOrigin)>,
);
impl<LocationConverter: ConvertLocation<RuntimeOrigin::AccountId>, RuntimeOrigin: OriginTrait>
ConvertOrigin<RuntimeOrigin> for SovereignSignedViaLocation<LocationConverter, RuntimeOrigin>
where
RuntimeOrigin::AccountId: Clone,
{
fn convert_origin(
origin: impl Into<Location>,
kind: OriginKind,
) -> Result<RuntimeOrigin, Location> {
let origin = origin.into();
tracing::trace!(
target: "xcm::origin_conversion",
?origin, ?kind,
"SovereignSignedViaLocation",
);
if let OriginKind::SovereignAccount = kind {
let location = LocationConverter::convert_location(&origin).ok_or(origin)?;
Ok(RuntimeOrigin::signed(location).into())
} else {
Err(origin)
}
}
}
pub struct ParentAsSuperuser<RuntimeOrigin>(PhantomData<RuntimeOrigin>);
impl<RuntimeOrigin: OriginTrait> ConvertOrigin<RuntimeOrigin> for ParentAsSuperuser<RuntimeOrigin> {
fn convert_origin(
origin: impl Into<Location>,
kind: OriginKind,
) -> Result<RuntimeOrigin, Location> {
let origin = origin.into();
tracing::trace!(target: "xcm::origin_conversion", ?origin, ?kind, "ParentAsSuperuser",);
if kind == OriginKind::Superuser && origin.contains_parents_only(1) {
Ok(RuntimeOrigin::root())
} else {
Err(origin)
}
}
}
pub struct ChildSystemTeyrchainAsSuperuser<ParaId, RuntimeOrigin>(
PhantomData<(ParaId, RuntimeOrigin)>,
);
impl<ParaId: IsSystem + From<u32>, RuntimeOrigin: OriginTrait> ConvertOrigin<RuntimeOrigin>
for ChildSystemTeyrchainAsSuperuser<ParaId, RuntimeOrigin>
{
fn convert_origin(
origin: impl Into<Location>,
kind: OriginKind,
) -> Result<RuntimeOrigin, Location> {
let origin = origin.into();
tracing::trace!(target: "xcm::origin_conversion", ?origin, ?kind, "ChildSystemTeyrchainAsSuperuser",);
match (kind, origin.unpack()) {
(OriginKind::Superuser, (0, [Junction::Teyrchain(id)]))
if ParaId::from(*id).is_system() =>
Ok(RuntimeOrigin::root()),
_ => Err(origin),
}
}
}
pub struct SiblingSystemTeyrchainAsSuperuser<ParaId, RuntimeOrigin>(
PhantomData<(ParaId, RuntimeOrigin)>,
);
impl<ParaId: IsSystem + From<u32>, RuntimeOrigin: OriginTrait> ConvertOrigin<RuntimeOrigin>
for SiblingSystemTeyrchainAsSuperuser<ParaId, RuntimeOrigin>
{
fn convert_origin(
origin: impl Into<Location>,
kind: OriginKind,
) -> Result<RuntimeOrigin, Location> {
let origin = origin.into();
tracing::trace!(
target: "xcm::origin_conversion",
?origin, ?kind,
"SiblingSystemTeyrchainAsSuperuser",
);
match (kind, origin.unpack()) {
(OriginKind::Superuser, (1, [Junction::Teyrchain(id)]))
if ParaId::from(*id).is_system() =>
Ok(RuntimeOrigin::root()),
_ => Err(origin),
}
}
}
pub struct ChildTeyrchainAsNative<TeyrchainOrigin, RuntimeOrigin>(
PhantomData<(TeyrchainOrigin, RuntimeOrigin)>,
);
impl<TeyrchainOrigin: From<u32>, RuntimeOrigin: From<TeyrchainOrigin>> ConvertOrigin<RuntimeOrigin>
for ChildTeyrchainAsNative<TeyrchainOrigin, RuntimeOrigin>
{
fn convert_origin(
origin: impl Into<Location>,
kind: OriginKind,
) -> Result<RuntimeOrigin, Location> {
let origin = origin.into();
tracing::trace!(target: "xcm::origin_conversion", ?origin, ?kind, "ChildTeyrchainAsNative");
match (kind, origin.unpack()) {
(OriginKind::Native, (0, [Junction::Teyrchain(id)])) =>
Ok(RuntimeOrigin::from(TeyrchainOrigin::from(*id))),
_ => Err(origin),
}
}
}
pub struct SiblingTeyrchainAsNative<TeyrchainOrigin, RuntimeOrigin>(
PhantomData<(TeyrchainOrigin, RuntimeOrigin)>,
);
impl<TeyrchainOrigin: From<u32>, RuntimeOrigin: From<TeyrchainOrigin>> ConvertOrigin<RuntimeOrigin>
for SiblingTeyrchainAsNative<TeyrchainOrigin, RuntimeOrigin>
{
fn convert_origin(
origin: impl Into<Location>,
kind: OriginKind,
) -> Result<RuntimeOrigin, Location> {
let origin = origin.into();
tracing::trace!(
target: "xcm::origin_conversion",
?origin, ?kind,
"SiblingTeyrchainAsNative",
);
match (kind, origin.unpack()) {
(OriginKind::Native, (1, [Junction::Teyrchain(id)])) =>
Ok(RuntimeOrigin::from(TeyrchainOrigin::from(*id))),
_ => Err(origin),
}
}
}
// Our Relay-chain has a native origin given by the `Get`ter.
pub struct RelayChainAsNative<RelayOrigin, RuntimeOrigin>(
PhantomData<(RelayOrigin, RuntimeOrigin)>,
);
impl<RelayOrigin: Get<RuntimeOrigin>, RuntimeOrigin> ConvertOrigin<RuntimeOrigin>
for RelayChainAsNative<RelayOrigin, RuntimeOrigin>
{
fn convert_origin(
origin: impl Into<Location>,
kind: OriginKind,
) -> Result<RuntimeOrigin, Location> {
let origin = origin.into();
tracing::trace!(target: "xcm::origin_conversion", ?origin, ?kind, "RelayChainAsNative");
if kind == OriginKind::Native && origin.contains_parents_only(1) {
Ok(RelayOrigin::get())
} else {
Err(origin)
}
}
}
pub struct SignedAccountId32AsNative<Network, RuntimeOrigin>(PhantomData<(Network, RuntimeOrigin)>);
impl<Network: Get<Option<NetworkId>>, RuntimeOrigin: OriginTrait> ConvertOrigin<RuntimeOrigin>
for SignedAccountId32AsNative<Network, RuntimeOrigin>
where
RuntimeOrigin::AccountId: From<[u8; 32]>,
{
fn convert_origin(
origin: impl Into<Location>,
kind: OriginKind,
) -> Result<RuntimeOrigin, Location> {
let origin = origin.into();
tracing::trace!(
target: "xcm::origin_conversion",
?origin, ?kind,
"SignedAccountId32AsNative",
);
match (kind, origin.unpack()) {
(OriginKind::Native, (0, [Junction::AccountId32 { id, network }]))
if matches!(network, None) || *network == Network::get() =>
Ok(RuntimeOrigin::signed((*id).into())),
_ => Err(origin),
}
}
}
pub struct SignedAccountKey20AsNative<Network, RuntimeOrigin>(
PhantomData<(Network, RuntimeOrigin)>,
);
impl<Network: Get<Option<NetworkId>>, RuntimeOrigin: OriginTrait> ConvertOrigin<RuntimeOrigin>
for SignedAccountKey20AsNative<Network, RuntimeOrigin>
where
RuntimeOrigin::AccountId: From<[u8; 20]>,
{
fn convert_origin(
origin: impl Into<Location>,
kind: OriginKind,
) -> Result<RuntimeOrigin, Location> {
let origin = origin.into();
tracing::trace!(
target: "xcm::origin_conversion",
?origin, ?kind,
"SignedAccountKey20AsNative",
);
match (kind, origin.unpack()) {
(OriginKind::Native, (0, [Junction::AccountKey20 { key, network }]))
if (matches!(network, None) || *network == Network::get()) =>
Ok(RuntimeOrigin::signed((*key).into())),
_ => Err(origin),
}
}
}
/// `EnsureOrigin` barrier to convert from dispatch origin to XCM origin, if one exists.
pub struct EnsureXcmOrigin<RuntimeOrigin, Conversion>(PhantomData<(RuntimeOrigin, Conversion)>);
impl<RuntimeOrigin: OriginTrait + Clone, Conversion: TryConvert<RuntimeOrigin, Location>>
EnsureOrigin<RuntimeOrigin> for EnsureXcmOrigin<RuntimeOrigin, Conversion>
where
RuntimeOrigin::PalletsOrigin: PartialEq,
{
type Success = Location;
fn try_origin(o: RuntimeOrigin) -> Result<Self::Success, RuntimeOrigin> {
let o = match Conversion::try_convert(o) {
Ok(location) => return Ok(location),
Err(o) => o,
};
// We institute a root fallback so root can always represent the context. This
// guarantees that `successful_origin` will work.
if o.caller() == RuntimeOrigin::root().caller() {
Ok(Here.into())
} else {
Err(o)
}
}
#[cfg(feature = "runtime-benchmarks")]
fn try_successful_origin() -> Result<RuntimeOrigin, ()> {
Ok(RuntimeOrigin::root())
}
}
/// `Convert` implementation to convert from some a `Signed` (system) `Origin` into an
/// `AccountId32`.
///
/// Typically used when configuring `pallet-xcm` for allowing normal accounts to dispatch an XCM
/// from an `AccountId32` origin.
pub struct SignedToAccountId32<RuntimeOrigin, AccountId, Network>(
PhantomData<(RuntimeOrigin, AccountId, Network)>,
);
impl<
RuntimeOrigin: OriginTrait + Clone,
AccountId: Into<[u8; 32]>,
Network: Get<Option<NetworkId>>,
> TryConvert<RuntimeOrigin, Location> for SignedToAccountId32<RuntimeOrigin, AccountId, Network>
where
RuntimeOrigin::PalletsOrigin: From<SystemRawOrigin<AccountId>>
+ TryInto<SystemRawOrigin<AccountId>, Error = RuntimeOrigin::PalletsOrigin>,
{
fn try_convert(o: RuntimeOrigin) -> Result<Location, RuntimeOrigin> {
o.try_with_caller(|caller| match caller.try_into() {
Ok(SystemRawOrigin::Signed(who)) =>
Ok(Junction::AccountId32 { network: Network::get(), id: who.into() }.into()),
Ok(other) => Err(other.into()),
Err(other) => Err(other),
})
}
}
/// `Convert` implementation to convert from some an origin which implements `Backing` into a
/// corresponding `Plurality` `Location`.
///
/// Typically used when configuring `pallet-xcm` for allowing a collective's Origin to dispatch an
/// XCM from a `Plurality` origin.
pub struct BackingToPlurality<RuntimeOrigin, COrigin, Body>(
PhantomData<(RuntimeOrigin, COrigin, Body)>,
);
impl<RuntimeOrigin: OriginTrait + Clone, COrigin: GetBacking, Body: Get<BodyId>>
TryConvert<RuntimeOrigin, Location> for BackingToPlurality<RuntimeOrigin, COrigin, Body>
where
RuntimeOrigin::PalletsOrigin:
From<COrigin> + TryInto<COrigin, Error = RuntimeOrigin::PalletsOrigin>,
{
fn try_convert(o: RuntimeOrigin) -> Result<Location, RuntimeOrigin> {
o.try_with_caller(|caller| match caller.try_into() {
Ok(co) => match co.get_backing() {
Some(backing) => Ok(Junction::Plurality {
id: Body::get(),
part: BodyPart::Fraction { nom: backing.approvals, denom: backing.eligible },
}
.into()),
None => Err(co.into()),
},
Err(other) => Err(other),
})
}
}
/// `Convert` implementation to convert from an origin which passes the check of an `EnsureOrigin`
/// into a voice of a given pluralistic `Body`.
pub struct OriginToPluralityVoice<RuntimeOrigin, EnsureBodyOrigin, Body>(
PhantomData<(RuntimeOrigin, EnsureBodyOrigin, Body)>,
);
impl<RuntimeOrigin: Clone, EnsureBodyOrigin: EnsureOrigin<RuntimeOrigin>, Body: Get<BodyId>>
TryConvert<RuntimeOrigin, Location>
for OriginToPluralityVoice<RuntimeOrigin, EnsureBodyOrigin, Body>
{
fn try_convert(o: RuntimeOrigin) -> Result<Location, RuntimeOrigin> {
match EnsureBodyOrigin::try_origin(o) {
Ok(_) => Ok(Junction::Plurality { id: Body::get(), part: BodyPart::Voice }.into()),
Err(o) => Err(o),
}
}
}
/// Converter that allows specific `Location`s to act as a superuser (`RuntimeOrigin::root()`)
/// if it matches the predefined `WhitelistedSuperuserLocations` filter and `OriginKind::Superuser`.
pub struct LocationAsSuperuser<WhitelistedSuperuserLocations, RuntimeOrigin>(
PhantomData<(WhitelistedSuperuserLocations, RuntimeOrigin)>,
);
impl<WhitelistedSuperuserLocations: Contains<Location>, RuntimeOrigin: OriginTrait>
ConvertOrigin<RuntimeOrigin>
for LocationAsSuperuser<WhitelistedSuperuserLocations, RuntimeOrigin>
{
fn convert_origin(
origin: impl Into<Location>,
kind: OriginKind,
) -> Result<RuntimeOrigin, Location> {
let origin = origin.into();
tracing::trace!(
target: "xcm::origin_conversion",
?origin, ?kind,
"LocationAsSuperuser",
);
match (kind, &origin) {
(OriginKind::Superuser, loc) if WhitelistedSuperuserLocations::contains(loc) =>
Ok(RuntimeOrigin::root()),
_ => Err(origin),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use frame_support::{construct_runtime, derive_impl, parameter_types, traits::Equals};
use xcm::latest::{Junction::*, OriginKind};
type Block = frame_system::mocking::MockBlock<Test>;
construct_runtime!(
pub enum Test
{
System: frame_system,
}
);
#[derive_impl(frame_system::config_preludes::TestDefaultConfig)]
impl frame_system::Config for Test {
type Block = Block;
}
parameter_types! {
pub SuperuserLocation: Location = Location::new(0, Teyrchain(1));
}
#[test]
fn superuser_location_works() {
let test_conversion = |loc, kind| {
LocationAsSuperuser::<Equals<SuperuserLocation>, RuntimeOrigin>::convert_origin(
loc, kind,
)
};
// Location that was set as SuperUserLocation should result in success conversion to Root
assert!(matches!(test_conversion(SuperuserLocation::get(), OriginKind::Superuser), Ok(..)));
// Same Location as SuperUserLocation::get()
assert!(matches!(
test_conversion(Location::new(0, Teyrchain(1)), OriginKind::Superuser),
Ok(..)
));
// Same Location but different origin kind
assert!(matches!(test_conversion(SuperuserLocation::get(), OriginKind::Native), Err(..)));
assert!(matches!(
test_conversion(SuperuserLocation::get(), OriginKind::SovereignAccount),
Err(..)
));
assert!(matches!(test_conversion(SuperuserLocation::get(), OriginKind::Xcm), Err(..)));
// No other location should result in successful conversion to Root
// thus expecting Err in all cases below
//
// Non-matching teyrchain number
assert!(matches!(
test_conversion(Location::new(0, Teyrchain(2)), OriginKind::Superuser),
Err(..)
));
// Non-matching parents count
assert!(matches!(
test_conversion(Location::new(1, Teyrchain(1)), OriginKind::Superuser),
Err(..)
));
// Child location of SuperUserLocation
assert!(matches!(
test_conversion(
Location::new(1, [Teyrchain(1), GeneralIndex(0)]),
OriginKind::Superuser
),
Err(..)
));
// Here
assert!(matches!(test_conversion(Location::new(0, Here), OriginKind::Superuser), Err(..)));
// Parent
assert!(matches!(test_conversion(Location::new(1, Here), OriginKind::Superuser), Err(..)));
// Some random account
assert!(matches!(
test_conversion(
Location::new(0, AccountId32 { network: None, id: [0u8; 32] }),
OriginKind::Superuser
),
Err(..)
));
// Child location of SuperUserLocation
assert!(matches!(
test_conversion(
Location::new(0, [Teyrchain(1), AccountId32 { network: None, id: [1u8; 32] }]),
OriginKind::Superuser
),
Err(..)
));
}
}
+180
View File
@@ -0,0 +1,180 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
//! `PayOverXcm` struct for paying through XCM and getting the status back.
use crate::{transfer::TransferOverXcmHelperT, TransferOverXcmHelper};
use core::marker::PhantomData;
use frame_support::traits::{
tokens::{Pay, PaymentStatus},
Get,
};
use sp_runtime::traits::TryConvert;
use xcm::prelude::*;
use xcm_executor::traits::WaiveDeliveryFees;
/// Implementation of the `frame_support::traits::tokens::Pay` trait, to allow
/// for XCM-based payments of a given `Balance` of some asset ID existing on some chain under
/// ownership of some `Interior` location of the local chain to a particular `Beneficiary`. The
/// `AssetKind` value is not itself bounded (to avoid the issue of needing to wrap some preexisting
/// datatype), however a converter type `AssetKindToLocatableAsset` must be provided in order to
/// translate it into a `LocatableAsset`, which comprises both an XCM `Location` describing
/// the XCM endpoint on which the asset to be paid resides and an XCM `AssetId` to identify the
/// specific asset at that endpoint.
///
/// This relies on the XCM `TransferAsset` instruction. A trait `BeneficiaryRefToLocation` must be
/// provided in order to convert the `Beneficiary` reference into a location usable by
/// `TransferAsset`.
///
/// `PayOverXcm::pay` is asynchronous, and returns a `QueryId` which can then be used in
/// `check_payment` to check the status of the XCM transaction.
///
/// See also `PayAccountId32OverXcm` which is similar to this except that `BeneficiaryRefToLocation`
/// need not be supplied and `Beneficiary` must implement `Into<[u8; 32]>`.
///
/// The implementation of this type assumes:
///
/// - The sending account on the remote chain is fixed (derived from the `Interior` location),
/// rather than being fully configurable.
/// - The remote chain waives the XCM execution fee (`PaysRemoteFee::No`).
///
/// See also [super::transfer::TransferOverXcm] for a more generic implementation with a flexible
/// sender account on the remote chain, and not making the assumption that the remote XCM execution
/// fee is waived.
pub type PayOverXcm<
Interior,
Router,
Querier,
Timeout,
Beneficiary,
AssetKind,
AssetKindToLocatableAsset,
BeneficiaryRefToLocation,
> = PayOverXcmWithHelper<
Interior,
TransferOverXcmHelper<
Router,
Querier,
WaiveDeliveryFees,
Timeout,
Beneficiary,
AssetKind,
AssetKindToLocatableAsset,
BeneficiaryRefToLocation,
>,
>;
/// Simpler than [`PayOverXcm`] the low-level XCM configuration is extracted to the
/// `TransferOverXcmHelper` type.
pub struct PayOverXcmWithHelper<Interior, TransferOverXcmHelper>(
PhantomData<(Interior, TransferOverXcmHelper)>,
);
impl<Interior, TransferOverXcmHelper> Pay for PayOverXcmWithHelper<Interior, TransferOverXcmHelper>
where
Interior: Get<InteriorLocation>,
TransferOverXcmHelper: TransferOverXcmHelperT<Balance = u128, QueryId = QueryId>,
{
type Balance = u128;
type Beneficiary = TransferOverXcmHelper::Beneficiary;
type AssetKind = TransferOverXcmHelper::AssetKind;
type Id = TransferOverXcmHelper::QueryId;
type Error = xcm::latest::Error;
fn pay(
who: &Self::Beneficiary,
asset_kind: Self::AssetKind,
amount: Self::Balance,
) -> Result<Self::Id, Self::Error> {
TransferOverXcmHelper::send_remote_transfer_xcm(
Interior::get().into(),
who,
asset_kind,
amount,
None,
)
}
fn check_payment(id: Self::Id) -> PaymentStatus {
TransferOverXcmHelper::check_transfer(id)
}
#[cfg(feature = "runtime-benchmarks")]
fn ensure_successful(
beneficiary: &Self::Beneficiary,
asset_kind: Self::AssetKind,
balance: Self::Balance,
) {
TransferOverXcmHelper::ensure_successful(beneficiary, asset_kind, balance);
}
#[cfg(feature = "runtime-benchmarks")]
fn ensure_concluded(id: Self::Id) {
TransferOverXcmHelper::ensure_concluded(id);
}
}
/// Specialization of the [`PayOverXcm`] trait to allow `[u8; 32]`-based `AccountId` values to be
/// paid on a remote chain.
///
/// Implementation of the [`frame_support::traits::tokens::Pay`] trait, to allow
/// for XCM payments of a given `Balance` of `AssetKind` existing on a `DestinationChain` under
/// ownership of some `Interior` location of the local chain to a particular `Beneficiary`.
///
/// This relies on the XCM `TransferAsset` instruction. `Beneficiary` must implement
/// `Into<[u8; 32]>` (as 32-byte `AccountId`s generally do), and the actual XCM beneficiary will be
/// the location consisting of a single `AccountId32` junction with an appropriate account and no
/// specific network.
///
/// `PayOverXcm::pay` is asynchronous, and returns a `QueryId` which can then be used in
/// `check_payment` to check the status of the XCM transaction.
pub type PayAccountId32OnChainOverXcm<
DestinationChain,
Interior,
Router,
Querier,
Timeout,
Beneficiary,
AssetKind,
> = PayOverXcm<
Interior,
Router,
Querier,
Timeout,
Beneficiary,
AssetKind,
crate::AliasesIntoAccountId32<(), Beneficiary>,
FixedLocation<DestinationChain>,
>;
/// Simple struct which contains both an XCM `location` and `asset_id` to identify an asset which
/// exists on some chain.
pub struct LocatableAssetId {
/// The asset's ID.
pub asset_id: AssetId,
/// The (relative) location in which the asset ID is meaningful.
pub location: Location,
}
/// Adapter `struct` which implements a conversion from any `AssetKind` into a [`LocatableAssetId`]
/// value using a fixed `Location` for the `location` field.
pub struct FixedLocation<FixedLocationValue>(core::marker::PhantomData<FixedLocationValue>);
impl<FixedLocationValue: Get<Location>, AssetKind: Into<AssetId>>
TryConvert<AssetKind, LocatableAssetId> for FixedLocation<FixedLocationValue>
{
fn try_convert(value: AssetKind) -> Result<LocatableAssetId, AssetKind> {
Ok(LocatableAssetId { asset_id: value.into(), location: FixedLocationValue::get() })
}
}
@@ -0,0 +1,277 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
//! Implementation of `ProcessMessage` for an `ExecuteXcm` implementation.
use codec::{Decode, DecodeLimit, FullCodec, MaxEncodedLen};
use core::{fmt::Debug, marker::PhantomData};
use frame_support::{
dispatch::GetDispatchInfo,
traits::{ProcessMessage, ProcessMessageError},
};
use scale_info::TypeInfo;
use sp_weights::{Weight, WeightMeter};
use xcm::{prelude::*, MAX_XCM_DECODE_DEPTH};
const LOG_TARGET: &str = "xcm::process-message";
/// A message processor that delegates execution to an `XcmExecutor`.
pub struct ProcessXcmMessage<MessageOrigin, XcmExecutor, Call>(
PhantomData<(MessageOrigin, XcmExecutor, Call)>,
);
impl<
MessageOrigin: Into<Location> + FullCodec + MaxEncodedLen + Clone + Eq + PartialEq + TypeInfo + Debug,
XcmExecutor: ExecuteXcm<Call>,
Call: Decode + GetDispatchInfo,
> ProcessMessage for ProcessXcmMessage<MessageOrigin, XcmExecutor, Call>
{
type Origin = MessageOrigin;
/// Process the given message, using no more than the remaining `weight` to do so.
fn process_message(
message: &[u8],
origin: Self::Origin,
meter: &mut WeightMeter,
id: &mut XcmHash,
) -> Result<bool, ProcessMessageError> {
let versioned_message = VersionedXcm::<Call>::decode_all_with_depth_limit(
MAX_XCM_DECODE_DEPTH,
&mut &message[..],
)
.map_err(|e| {
tracing::trace!(
target: LOG_TARGET,
?e,
"`VersionedXcm` failed to decode",
);
ProcessMessageError::Corrupt
})?;
let message = Xcm::<Call>::try_from(versioned_message).map_err(|_| {
tracing::trace!(
target: LOG_TARGET,
"Failed to convert `VersionedXcm` into `xcm::prelude::Xcm`!",
);
ProcessMessageError::Unsupported
})?;
let pre = XcmExecutor::prepare(message, Weight::MAX).map_err(|_| {
tracing::trace!(
target: LOG_TARGET,
"Failed to prepare message.",
);
ProcessMessageError::Unsupported
})?;
// The worst-case weight:
let required = pre.weight_of();
if !meter.can_consume(required) {
tracing::trace!(
target: LOG_TARGET,
"Xcm required {required} more than remaining {}",
meter.remaining(),
);
return Err(ProcessMessageError::Overweight(required));
}
let (consumed, result) = match XcmExecutor::execute(origin.into(), pre, id, Weight::zero())
{
Outcome::Complete { used } => {
tracing::trace!(
target: LOG_TARGET,
"XCM message execution complete, used weight: {used}",
);
(used, Ok(true))
},
Outcome::Incomplete { used, error: InstructionError { index, error } } => {
tracing::trace!(
target: LOG_TARGET,
?error,
?index,
?used,
"XCM message execution incomplete",
);
(used, Ok(false))
},
// In the error-case we assume the worst case and consume all possible weight.
Outcome::Error(InstructionError { error, index }) => {
tracing::trace!(
target: LOG_TARGET,
?error,
?index,
"XCM message execution error",
);
let error = match error {
xcm::latest::Error::ExceedsStackLimit => ProcessMessageError::StackLimitReached,
_ => ProcessMessageError::Unsupported,
};
(required, Err(error))
},
};
meter.consume(consumed);
result
}
}
#[cfg(test)]
mod tests {
use super::*;
use alloc::vec;
use codec::Encode;
use frame_support::{
assert_err, assert_ok,
traits::{ProcessMessageError, ProcessMessageError::*},
};
use pezkuwi_test_runtime::*;
use xcm::{v3, v4, v5, VersionedXcm};
const ORIGIN: Junction = Junction::OnlyChild;
/// The processor to use for tests.
type Processor =
ProcessXcmMessage<Junction, xcm_executor::XcmExecutor<xcm_config::XcmConfig>, RuntimeCall>;
#[test]
fn process_message_trivial_works() {
// ClearOrigin works.
assert!(process(v3_xcm(true)).unwrap());
assert!(process(v4_xcm(true)).unwrap());
assert!(process(v5_xcm(true)).unwrap());
}
#[test]
fn process_message_trivial_fails() {
// Trap makes it fail.
sp_io::TestExternalities::default().execute_with(|| {
assert!(!process(v3_xcm(false)).unwrap());
assert!(!process(v4_xcm(false)).unwrap());
assert!(!process(v5_xcm(false)).unwrap());
});
}
#[test]
fn process_message_corrupted_fails() {
let msgs: &[&[u8]] = &[&[], &[55, 66], &[123, 222, 233]];
for msg in msgs {
assert_err!(process_raw(msg), Corrupt);
}
}
#[test]
fn process_message_exceeds_limits_fails() {
struct MockedExecutor;
impl ExecuteXcm<()> for MockedExecutor {
type Prepared = xcm_executor::WeighedMessage<()>;
fn prepare(
message: xcm::latest::Xcm<()>,
_: Weight,
) -> core::result::Result<Self::Prepared, InstructionError> {
Ok(xcm_executor::WeighedMessage::new(Weight::zero(), message))
}
fn execute(
_: impl Into<Location>,
_: Self::Prepared,
_: &mut XcmHash,
_: Weight,
) -> Outcome {
Outcome::Error(InstructionError {
index: 0,
error: xcm::latest::Error::ExceedsStackLimit,
})
}
fn charge_fees(_location: impl Into<Location>, _fees: Assets) -> xcm::latest::Result {
unreachable!()
}
}
type Processor = ProcessXcmMessage<Junction, MockedExecutor, ()>;
let xcm = VersionedXcm::from(xcm::latest::Xcm::<()>(vec![
xcm::latest::Instruction::<()>::ClearOrigin,
]));
assert_err!(
Processor::process_message(
&xcm.encode(),
ORIGIN,
&mut WeightMeter::new(),
&mut [0; 32]
),
ProcessMessageError::StackLimitReached,
);
}
#[test]
fn process_message_overweight_fails() {
sp_io::TestExternalities::default().execute_with(|| {
for msg in [v4_xcm(true), v4_xcm(false), v4_xcm(false), v3_xcm(false)] {
let msg = &msg.encode()[..];
// Errors if we stay below a weight limit of 1000.
for i in 0..10 {
let meter = &mut WeightMeter::with_limit((i * 10).into());
let mut id = [0; 32];
assert_err!(
Processor::process_message(msg, ORIGIN, meter, &mut id),
Overweight(1000.into())
);
assert_eq!(meter.consumed(), 0.into());
}
// Works with a limit of 1000.
let meter = &mut WeightMeter::with_limit(1000.into());
let mut id = [0; 32];
assert_ok!(Processor::process_message(msg, ORIGIN, meter, &mut id));
assert_eq!(meter.consumed(), 1000.into());
}
});
}
fn v3_xcm(success: bool) -> VersionedXcm<RuntimeCall> {
let instr = if success {
v3::Instruction::<RuntimeCall>::ClearOrigin
} else {
v3::Instruction::<RuntimeCall>::Trap(1)
};
VersionedXcm::V3(v3::Xcm::<RuntimeCall>(vec![instr]))
}
fn v4_xcm(success: bool) -> VersionedXcm<RuntimeCall> {
let instr = if success {
v4::Instruction::<RuntimeCall>::ClearOrigin
} else {
v4::Instruction::<RuntimeCall>::Trap(1)
};
VersionedXcm::V4(v4::Xcm::<RuntimeCall>(vec![instr]))
}
fn v5_xcm(success: bool) -> VersionedXcm<RuntimeCall> {
let instr = if success {
v5::Instruction::<RuntimeCall>::ClearOrigin
} else {
v5::Instruction::<RuntimeCall>::Trap(1)
};
VersionedXcm::V5(v5::Xcm::<RuntimeCall>(vec![instr]))
}
fn process(msg: VersionedXcm<RuntimeCall>) -> Result<bool, ProcessMessageError> {
process_raw(msg.encode().as_slice())
}
fn process_raw(raw: &[u8]) -> Result<bool, ProcessMessageError> {
Processor::process_message(raw, ORIGIN, &mut WeightMeter::new(), &mut [0; 32])
}
}
+233
View File
@@ -0,0 +1,233 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
//! Various implementations for `SendXcm`.
use alloc::vec::Vec;
use codec::Encode;
use core::{marker::PhantomData, result::Result};
use frame_system::unique;
use xcm::prelude::*;
use xcm_executor::{traits::FeeReason, FeesMode};
/// Wrapper router which, if the message does not already end with a `SetTopic` instruction,
/// appends one to the message filled with a universally unique ID. This ID is returned from a
/// successful `deliver`.
///
/// If the message does already end with a `SetTopic` instruction, then it is the responsibility
/// of the code author to ensure that the ID supplied to `SetTopic` is universally unique. Due to
/// this property, consumers of the topic ID must be aware that a user-supplied ID may not be
/// unique.
///
/// This is designed to be at the top-level of any routers, since it will always mutate the
/// passed `message` reference into a `None`. Don't try to combine it within a tuple except as the
/// last element.
pub struct WithUniqueTopic<Inner>(PhantomData<Inner>);
impl<Inner: SendXcm> SendXcm for WithUniqueTopic<Inner> {
type Ticket = (Inner::Ticket, [u8; 32]);
fn validate(
destination: &mut Option<Location>,
message: &mut Option<Xcm<()>>,
) -> SendResult<Self::Ticket> {
let mut message = message.take().ok_or(SendError::MissingArgument)?;
let unique_id = if let Some(SetTopic(id)) = message.last() {
*id
} else {
let unique_id = unique(&message);
message.0.push(SetTopic(unique_id));
unique_id
};
let (ticket, assets) = Inner::validate(destination, &mut Some(message))?;
Ok(((ticket, unique_id), assets))
}
fn deliver(ticket: Self::Ticket) -> Result<XcmHash, SendError> {
let (ticket, unique_id) = ticket;
Inner::deliver(ticket)?;
Ok(unique_id)
}
#[cfg(feature = "runtime-benchmarks")]
fn ensure_successful_delivery(location: Option<Location>) {
Inner::ensure_successful_delivery(location);
}
}
impl<Inner: InspectMessageQueues> InspectMessageQueues for WithUniqueTopic<Inner> {
fn clear_messages() {
Inner::clear_messages()
}
fn get_messages() -> Vec<(VersionedLocation, Vec<VersionedXcm<()>>)> {
Inner::get_messages()
}
}
pub trait SourceTopic {
fn source_topic(entropy: impl Encode) -> XcmHash;
}
impl SourceTopic for () {
fn source_topic(_: impl Encode) -> XcmHash {
[0u8; 32]
}
}
/// Wrapper router which, if the message does not already end with a `SetTopic` instruction,
/// prepends one to the message filled with an ID from `TopicSource`. This ID is returned from a
/// successful `deliver`.
///
/// This is designed to be at the top-level of any routers, since it will always mutate the
/// passed `message` reference into a `None`. Don't try to combine it within a tuple except as the
/// last element.
pub struct WithTopicSource<Inner, TopicSource>(PhantomData<(Inner, TopicSource)>);
impl<Inner: SendXcm, TopicSource: SourceTopic> SendXcm for WithTopicSource<Inner, TopicSource> {
type Ticket = (Inner::Ticket, [u8; 32]);
fn validate(
destination: &mut Option<Location>,
message: &mut Option<Xcm<()>>,
) -> SendResult<Self::Ticket> {
let mut message = message.take().ok_or(SendError::MissingArgument)?;
let unique_id = if let Some(SetTopic(id)) = message.last() {
*id
} else {
let unique_id = TopicSource::source_topic(&message);
message.0.push(SetTopic(unique_id));
unique_id
};
let (ticket, assets) = Inner::validate(destination, &mut Some(message.clone())).map_err(|e| {
tracing::debug!(target: "xcm::validate::WithTopicSource", ?destination, ?message, error = ?e, "Failed to validate");
SendError::NotApplicable
})?;
Ok(((ticket, unique_id), assets))
}
fn deliver(ticket: Self::Ticket) -> Result<XcmHash, SendError> {
let (ticket, unique_id) = ticket;
Inner::deliver(ticket)?;
Ok(unique_id)
}
#[cfg(feature = "runtime-benchmarks")]
fn ensure_successful_delivery(location: Option<Location>) {
Inner::ensure_successful_delivery(location);
}
}
/// Trait for a type which ensures all requirements for successful delivery with XCM transport
/// layers.
pub trait EnsureDelivery {
/// Prepare all requirements for successful `XcmSender: SendXcm` passing (accounts, balances,
/// channels ...). Returns:
/// - possible `FeesMode` which is expected to be set to executor
/// - possible `Assets` which are expected to be subsume to the Holding Register
fn ensure_successful_delivery(
origin_ref: &Location,
dest: &Location,
fee_reason: FeeReason,
) -> (Option<FeesMode>, Option<Assets>);
}
/// Tuple implementation for `EnsureDelivery`.
#[impl_trait_for_tuples::impl_for_tuples(30)]
impl EnsureDelivery for Tuple {
fn ensure_successful_delivery(
origin_ref: &Location,
dest: &Location,
fee_reason: FeeReason,
) -> (Option<FeesMode>, Option<Assets>) {
for_tuples!( #(
// If the implementation returns something, we're done; if not, let others try.
match Tuple::ensure_successful_delivery(origin_ref, dest, fee_reason.clone()) {
r @ (Some(_), Some(_)) | r @ (Some(_), None) | r @ (None, Some(_)) => return r,
(None, None) => (),
}
)* );
// doing nothing
(None, None)
}
}
/// Inspects messages in queues.
/// Meant to be used in runtime APIs, not in runtimes.
pub trait InspectMessageQueues {
/// Clear the queues at the beginning of Runtime API call, so that subsequent
/// `Self::get_messages()` will return only messages generated by said Runtime API.
fn clear_messages();
/// Get queued messages and their destinations.
fn get_messages() -> Vec<(VersionedLocation, Vec<VersionedXcm<()>>)>;
}
#[impl_trait_for_tuples::impl_for_tuples(30)]
impl InspectMessageQueues for Tuple {
fn clear_messages() {
for_tuples!( #(
Tuple::clear_messages();
)* );
}
fn get_messages() -> Vec<(VersionedLocation, Vec<VersionedXcm<()>>)> {
let mut messages = Vec::new();
for_tuples!( #(
messages.append(&mut Tuple::get_messages());
)* );
messages
}
}
/// A wrapper router that attempts to *encode* and *decode* passed XCM `message` to ensure that the
/// receiving side will be able to decode, at least with the same XCM version.
///
/// This is designed to be at the top-level of any routers which do the real delivery. While other
/// routers can manipulate the `message`, we cannot access the final XCM due to the generic
/// `Inner::Ticket`. Therefore, this router aims to validate at least the passed `message`.
///
/// NOTE: For use in mock runtimes which don't have the DMP/UMP/HRMP XCM validations.
pub struct EnsureDecodableXcm<Inner>(core::marker::PhantomData<Inner>);
impl<Inner: SendXcm> SendXcm for EnsureDecodableXcm<Inner> {
type Ticket = Inner::Ticket;
fn validate(
destination: &mut Option<Location>,
message: &mut Option<Xcm<()>>,
) -> SendResult<Self::Ticket> {
if let Some(msg) = message {
let versioned_xcm = VersionedXcm::<()>::from(msg.clone());
if versioned_xcm.check_is_decodable().is_err() {
tracing::debug!(
target: "xcm::validate_xcm_nesting",
?versioned_xcm,
?msg,
"EnsureDecodableXcm `validate_xcm_nesting` failed"
);
return Err(SendError::Transport("EnsureDecodableXcm validate_xcm_nesting error"));
}
}
Inner::validate(destination, message)
}
fn deliver(ticket: Self::Ticket) -> Result<XcmHash, SendError> {
Inner::deliver(ticket)
}
#[cfg(feature = "runtime-benchmarks")]
fn ensure_successful_delivery(location: Option<Location>) {
Inner::ensure_successful_delivery(location);
}
}
+220
View File
@@ -0,0 +1,220 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
// Shared test utilities and implementations for the XCM Builder.
use alloc::vec::Vec;
use frame_support::{
parameter_types,
traits::{Contains, CrateVersion, PalletInfoData, PalletsInfoAccess},
};
pub use xcm::latest::{prelude::*, Weight};
use xcm_executor::traits::{ClaimAssets, DropAssets, VersionChangeNotifier};
pub use xcm_executor::{
traits::{
AssetExchange, AssetLock, ConvertOrigin, Enact, LockError, OnResponse, TransactAsset,
},
AssetsInHolding, Config,
};
parameter_types! {
pub static SubscriptionRequests: Vec<(Location, Option<(QueryId, Weight)>)> = vec![];
pub static MaxAssetsIntoHolding: u32 = 4;
}
pub struct TestSubscriptionService;
impl VersionChangeNotifier for TestSubscriptionService {
fn start(
location: &Location,
query_id: QueryId,
max_weight: Weight,
_context: &XcmContext,
) -> XcmResult {
let mut r = SubscriptionRequests::get();
r.push((location.clone(), Some((query_id, max_weight))));
SubscriptionRequests::set(r);
Ok(())
}
fn stop(location: &Location, _context: &XcmContext) -> XcmResult {
let mut r = SubscriptionRequests::get();
r.retain(|(l, _q)| l != location);
r.push((location.clone(), None));
SubscriptionRequests::set(r);
Ok(())
}
fn is_subscribed(location: &Location) -> bool {
let r = SubscriptionRequests::get();
r.iter().any(|(l, q)| l == location && q.is_some())
}
}
parameter_types! {
pub static TrappedAssets: Vec<(Location, Assets)> = vec![];
}
pub struct TestAssetTrap;
impl DropAssets for TestAssetTrap {
fn drop_assets(origin: &Location, assets: AssetsInHolding, _context: &XcmContext) -> Weight {
let mut t: Vec<(Location, Assets)> = TrappedAssets::get();
t.push((origin.clone(), assets.into()));
TrappedAssets::set(t);
Weight::from_parts(5, 5)
}
}
impl ClaimAssets for TestAssetTrap {
fn claim_assets(
origin: &Location,
ticket: &Location,
what: &Assets,
_context: &XcmContext,
) -> bool {
let mut t: Vec<(Location, Assets)> = TrappedAssets::get();
if let (0, [GeneralIndex(i)]) = ticket.unpack() {
if let Some((l, a)) = t.get(*i as usize) {
if l == origin && a == what {
t.swap_remove(*i as usize);
TrappedAssets::set(t);
return true;
}
}
}
false
}
}
pub struct TestAssetExchanger;
impl AssetExchange for TestAssetExchanger {
fn exchange_asset(
_origin: Option<&Location>,
_give: AssetsInHolding,
want: &Assets,
_maximal: bool,
) -> Result<AssetsInHolding, AssetsInHolding> {
Ok(want.clone().into())
}
fn quote_exchange_price(give: &Assets, _want: &Assets, _maximal: bool) -> Option<Assets> {
Some(give.clone())
}
}
pub struct TestPalletsInfo;
impl PalletsInfoAccess for TestPalletsInfo {
fn count() -> usize {
2
}
fn infos() -> Vec<PalletInfoData> {
vec![
PalletInfoData {
index: 0,
name: "System",
module_name: "pallet_system",
crate_version: CrateVersion { major: 1, minor: 10, patch: 1 },
},
PalletInfoData {
index: 1,
name: "Balances",
module_name: "pallet_balances",
crate_version: CrateVersion { major: 1, minor: 42, patch: 69 },
},
]
}
}
pub struct TestUniversalAliases;
impl Contains<(Location, Junction)> for TestUniversalAliases {
fn contains(aliases: &(Location, Junction)) -> bool {
&aliases.0 == &Here.into_location() && &aliases.1 == &GlobalConsensus(ByGenesis([0; 32]))
}
}
parameter_types! {
pub static LockedAssets: Vec<(Location, Asset)> = vec![];
}
pub struct TestLockTicket(Location, Asset);
impl Enact for TestLockTicket {
fn enact(self) -> Result<(), LockError> {
let mut locked_assets = LockedAssets::get();
locked_assets.push((self.0, self.1));
LockedAssets::set(locked_assets);
Ok(())
}
}
pub struct TestUnlockTicket(Location, Asset);
impl Enact for TestUnlockTicket {
fn enact(self) -> Result<(), LockError> {
let mut locked_assets = LockedAssets::get();
if let Some((idx, _)) = locked_assets
.iter()
.enumerate()
.find(|(_, (origin, asset))| origin == &self.0 && asset == &self.1)
{
locked_assets.remove(idx);
}
LockedAssets::set(locked_assets);
Ok(())
}
}
pub struct TestReduceTicket;
impl Enact for TestReduceTicket {
fn enact(self) -> Result<(), LockError> {
Ok(())
}
}
pub struct TestAssetLocker;
impl AssetLock for TestAssetLocker {
type LockTicket = TestLockTicket;
type UnlockTicket = TestUnlockTicket;
type ReduceTicket = TestReduceTicket;
fn prepare_lock(
unlocker: Location,
asset: Asset,
_owner: Location,
) -> Result<TestLockTicket, LockError> {
Ok(TestLockTicket(unlocker, asset))
}
fn prepare_unlock(
unlocker: Location,
asset: Asset,
_owner: Location,
) -> Result<TestUnlockTicket, LockError> {
Ok(TestUnlockTicket(unlocker, asset))
}
fn note_unlockable(
_locker: Location,
_asset: Asset,
_owner: Location,
) -> Result<(), LockError> {
Ok(())
}
fn prepare_reduce_unlockable(
_locker: Location,
_asset: Asset,
_owner: Location,
) -> Result<TestReduceTicket, LockError> {
Ok(TestReduceTicket)
}
}
@@ -0,0 +1,254 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
use super::*;
#[test]
fn alias_foreign_account_sibling_prefix() {
// Accounts Differ
assert!(!AliasForeignAccountId32::<SiblingPrefix>::contains(
&(Parent, Teyrchain(1), AccountId32 { network: None, id: [0; 32] }).into(),
&(AccountId32 { network: None, id: [1; 32] }).into()
));
assert!(AliasForeignAccountId32::<SiblingPrefix>::contains(
&(Parent, Teyrchain(1), AccountId32 { network: None, id: [0; 32] }).into(),
&(AccountId32 { network: None, id: [0; 32] }).into()
));
}
#[test]
fn alias_foreign_account_child_prefix() {
// Accounts Differ
assert!(!AliasForeignAccountId32::<ChildPrefix>::contains(
&(Teyrchain(1), AccountId32 { network: None, id: [0; 32] }).into(),
&(AccountId32 { network: None, id: [1; 32] }).into()
));
assert!(AliasForeignAccountId32::<ChildPrefix>::contains(
&(Teyrchain(1), AccountId32 { network: None, id: [0; 32] }).into(),
&(AccountId32 { network: None, id: [0; 32] }).into()
));
}
#[test]
fn alias_foreign_account_parent_prefix() {
// Accounts Differ
assert!(!AliasForeignAccountId32::<ParentPrefix>::contains(
&(Parent, AccountId32 { network: None, id: [0; 32] }).into(),
&(AccountId32 { network: None, id: [1; 32] }).into()
));
assert!(AliasForeignAccountId32::<ParentPrefix>::contains(
&(Parent, AccountId32 { network: None, id: [0; 32] }).into(),
&(AccountId32 { network: None, id: [0; 32] }).into()
));
}
#[test]
fn alias_origin_should_work() {
AllowUnpaidFrom::set(vec![
(Parent, Teyrchain(1), AccountId32 { network: None, id: [0; 32] }).into(),
(Teyrchain(1), AccountId32 { network: None, id: [0; 32] }).into(),
]);
let message = Xcm(vec![AliasOrigin((AccountId32 { network: None, id: [0; 32] }).into())]);
let mut hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
(Teyrchain(1), AccountId32 { network: None, id: [0; 32] }),
message.clone(),
&mut hash,
Weight::from_parts(50, 50),
Weight::zero(),
);
assert_eq!(
r,
Outcome::Incomplete {
used: Weight::from_parts(10, 10),
error: InstructionError { index: 0, error: XcmError::NoPermission },
}
);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
(Parent, Teyrchain(1), AccountId32 { network: None, id: [0; 32] }),
message.clone(),
&mut hash,
Weight::from_parts(50, 50),
Weight::zero(),
);
assert_eq!(r, Outcome::Complete { used: Weight::from_parts(10, 10) });
}
#[test]
fn alias_child_location() {
// parents differ
assert!(!AliasChildLocation::contains(
&Location::new(0, Teyrchain(1)),
&Location::new(1, Teyrchain(1)),
));
assert!(!AliasChildLocation::contains(
&Location::new(0, Here),
&Location::new(1, Teyrchain(1)),
));
assert!(!AliasChildLocation::contains(&Location::new(1, Here), &Location::new(2, Here),));
// interiors differ
assert!(!AliasChildLocation::contains(
&Location::new(1, Teyrchain(1)),
&Location::new(1, OnlyChild),
));
assert!(!AliasChildLocation::contains(
&Location::new(1, Teyrchain(1)),
&Location::new(1, Teyrchain(12)),
));
assert!(!AliasChildLocation::contains(
&Location::new(1, [Teyrchain(1), AccountId32 { network: None, id: [0; 32] }]),
&Location::new(1, [Teyrchain(1), AccountId32 { network: None, id: [1; 32] }]),
));
assert!(!AliasChildLocation::contains(
&Location::new(1, [Teyrchain(1), AccountId32 { network: None, id: [0; 32] }]),
&Location::new(1, [Teyrchain(1), AccountId32 { network: None, id: [1; 32] }]),
));
// child to parent not allowed
assert!(!AliasChildLocation::contains(
&Location::new(1, [Teyrchain(1), AccountId32 { network: None, id: [0; 32] }]),
&Location::new(1, [Teyrchain(1)]),
));
assert!(!AliasChildLocation::contains(
&Location::new(1, [Teyrchain(1), AccountId32 { network: None, id: [0; 32] }]),
&Location::new(1, Here),
));
// parent to child should work
assert!(AliasChildLocation::contains(
&Location::new(1, Here),
&Location::new(1, [Teyrchain(1), AccountId32 { network: None, id: [1; 32] }]),
));
assert!(
AliasChildLocation::contains(&Location::new(1, Here), &Location::new(1, Teyrchain(1)),)
);
assert!(AliasChildLocation::contains(
&Location::new(0, Here),
&Location::new(0, PalletInstance(42)),
));
assert!(AliasChildLocation::contains(
&Location::new(2, GlobalConsensus(Kusama)),
&Location::new(2, [GlobalConsensus(Kusama), Teyrchain(42), GeneralIndex(12)]),
));
}
#[test]
fn alias_trusted_root_location() {
const ALICE: [u8; 32] = [111u8; 32];
const BOB: [u8; 32] = [222u8; 32];
const BOB_ON_ETH: [u8; 20] = [222u8; 20];
parameter_types! {
pub AliceOnAssetHub: Location = Location::new(1, [Teyrchain(1000), AccountId32 { id: ALICE, network: None }]);
pub SystemAssetHubLocation: Location = Location::new(1, [Teyrchain(1000)]);
}
struct MatchSiblingAccounts;
impl Contains<Location> for MatchSiblingAccounts {
fn contains(location: &Location) -> bool {
matches!(location.unpack(), (1, [Teyrchain(_), AccountId32 { .. }]))
}
}
struct MatchOtherGlobalConsensus;
impl Contains<Location> for MatchOtherGlobalConsensus {
fn contains(location: &Location) -> bool {
matches!(location.unpack(), (2, [GlobalConsensus(_)]) | (2, [GlobalConsensus(_), _]))
}
}
type AliceOnAssetHubAliasesSiblingAccounts =
AliasOriginRootUsingFilter<AliceOnAssetHub, MatchSiblingAccounts>;
type AssetHubAliasesSiblingAccounts =
AliasOriginRootUsingFilter<SystemAssetHubLocation, MatchSiblingAccounts>;
type AssetHubAliasesOtherGlobalConsensus =
AliasOriginRootUsingFilter<SystemAssetHubLocation, MatchOtherGlobalConsensus>;
// Fails if origin is not the root of a chain.
assert!(!AliceOnAssetHubAliasesSiblingAccounts::contains(
&Location::new(1, [Teyrchain(1000), AccountId32 { id: ALICE, network: None }]),
&Location::new(1, [Teyrchain(1000), AccountId32 { id: BOB, network: None }]),
));
assert!(!AliceOnAssetHubAliasesSiblingAccounts::contains(
&Location::new(1, [Teyrchain(1000), AccountId32 { id: ALICE, network: None }]),
&Location::new(2, [GlobalConsensus(NetworkId::Ethereum { chain_id: 1 })]),
));
assert!(!AliceOnAssetHubAliasesSiblingAccounts::contains(
&Location::new(1, [Teyrchain(1000), AccountId32 { id: ALICE, network: None }]),
&Location::new(
2,
[
GlobalConsensus(NetworkId::Ethereum { chain_id: 1 }),
AccountKey20 { key: BOB_ON_ETH, network: None }
]
),
));
// Fails if origin doesn't match.
assert!(!AssetHubAliasesSiblingAccounts::contains(
&Location::new(1, [Teyrchain(1001)]),
&Location::new(1, [Teyrchain(1000), AccountId32 { id: BOB, network: None }]),
));
assert!(!AssetHubAliasesOtherGlobalConsensus::contains(
&Location::new(1, [Teyrchain(1001)]),
&Location::new(
2,
[
GlobalConsensus(NetworkId::Ethereum { chain_id: 1 }),
AccountKey20 { key: BOB_ON_ETH, network: None }
]
),
));
// Fails if filter doesn't match.
assert!(!AssetHubAliasesSiblingAccounts::contains(
&Location::new(1, [Teyrchain(1000)]),
&Location::new(2, [GlobalConsensus(NetworkId::Ethereum { chain_id: 1 })]),
));
assert!(!AssetHubAliasesSiblingAccounts::contains(
&Location::new(1, [Teyrchain(1000)]),
&Location::new(
2,
[
GlobalConsensus(NetworkId::Ethereum { chain_id: 1 }),
AccountKey20 { key: BOB_ON_ETH, network: None }
]
),
));
assert!(!AssetHubAliasesOtherGlobalConsensus::contains(
&Location::new(1, [Teyrchain(1000)]),
&Location::new(1, [Teyrchain(1000), AccountId32 { id: BOB, network: None }]),
));
// Works when origin is a chain that matches Origin and filter also matches.
assert!(AssetHubAliasesSiblingAccounts::contains(
&Location::new(1, [Teyrchain(1000)]),
&Location::new(1, [Teyrchain(1000), AccountId32 { id: BOB, network: None }]),
));
assert!(AssetHubAliasesOtherGlobalConsensus::contains(
&Location::new(1, [Teyrchain(1000)]),
&Location::new(
2,
[
GlobalConsensus(NetworkId::Ethereum { chain_id: 1 }),
AccountKey20 { key: BOB_ON_ETH, network: None }
]
),
));
}
+554
View File
@@ -0,0 +1,554 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
use super::*;
#[test]
fn exchange_asset_should_work() {
AllowUnpaidFrom::set(vec![Parent.into()]);
add_asset(Parent, (Parent, 1000u128));
set_exchange_assets(vec![(Here, 100u128).into()]);
let message = Xcm(vec![
WithdrawAsset((Parent, 100u128).into()),
SetAppendix(
vec![DepositAsset { assets: AllCounted(2).into(), beneficiary: Parent.into() }].into(),
),
ExchangeAsset {
give: Definite((Parent, 50u128).into()),
want: (Here, 50u128).into(),
maximal: true,
},
]);
let mut hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
Parent,
message,
&mut hash,
Weight::from_parts(50, 50),
Weight::zero(),
);
assert_eq!(r, Outcome::Complete { used: Weight::from_parts(40, 40) });
assert_eq!(asset_list(Parent), vec![(Here, 100u128).into(), (Parent, 950u128).into()]);
assert_eq!(exchange_assets(), vec![(Parent, 50u128).into()].into());
}
#[test]
fn exchange_asset_without_maximal_should_work() {
AllowUnpaidFrom::set(vec![Parent.into()]);
add_asset(Parent, (Parent, 1000u128));
set_exchange_assets(vec![(Here, 100u128).into()]);
let message = Xcm(vec![
WithdrawAsset((Parent, 100u128).into()),
SetAppendix(
vec![DepositAsset { assets: AllCounted(2).into(), beneficiary: Parent.into() }].into(),
),
ExchangeAsset {
give: Definite((Parent, 50).into()),
want: (Here, 50u128).into(),
maximal: false,
},
]);
let mut hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
Parent,
message,
&mut hash,
Weight::from_parts(50, 50),
Weight::zero(),
);
assert_eq!(r, Outcome::Complete { used: Weight::from_parts(40, 40) });
assert_eq!(asset_list(Parent), vec![(Here, 50u128).into(), (Parent, 950u128).into()]);
assert_eq!(exchange_assets(), vec![(Here, 50u128).into(), (Parent, 50u128).into()].into());
}
#[test]
fn exchange_asset_should_fail_when_no_deal_possible() {
AllowUnpaidFrom::set(vec![Parent.into()]);
add_asset(Parent, (Parent, 1000u128));
set_exchange_assets(vec![(Here, 100u128).into()]);
let message = Xcm(vec![
WithdrawAsset((Parent, 150u128).into()),
SetAppendix(
vec![DepositAsset { assets: AllCounted(2).into(), beneficiary: Parent.into() }].into(),
),
ExchangeAsset {
give: Definite((Parent, 150u128).into()),
want: (Here, 150u128).into(),
maximal: false,
},
]);
let mut hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
Parent,
message,
&mut hash,
Weight::from_parts(50, 50),
Weight::zero(),
);
assert_eq!(
r,
Outcome::Incomplete {
used: Weight::from_parts(40, 40),
error: InstructionError { index: 2, error: XcmError::NoDeal },
}
);
assert_eq!(asset_list(Parent), vec![(Parent, 1000u128).into()]);
assert_eq!(exchange_assets(), vec![(Here, 100u128).into()].into());
}
#[test]
fn paying_reserve_deposit_should_work() {
AllowPaidFrom::set(vec![Parent.into()]);
add_reserve(Parent.into(), (Parent, WildFungible).into());
WeightPrice::set((Parent.into(), 1_000_000_000_000, 1024 * 1024));
let fees = (Parent, 60u128).into();
let message = Xcm(vec![
ReserveAssetDeposited((Parent, 100u128).into()),
BuyExecution { fees, weight_limit: Limited(Weight::from_parts(30, 30)) },
DepositAsset { assets: AllCounted(1).into(), beneficiary: Here.into() },
]);
let mut hash = fake_message_hash(&message);
let weight_limit = Weight::from_parts(50, 50);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
Parent,
message,
&mut hash,
weight_limit,
Weight::zero(),
);
assert_eq!(r, Outcome::Complete { used: Weight::from_parts(30, 30) });
assert_eq!(asset_list(Here), vec![(Parent, 40u128).into()]);
}
#[test]
fn transfer_should_work() {
// we'll let them have message execution for free.
AllowUnpaidFrom::set(vec![[Teyrchain(1)].into()]);
// Child teyrchain #1 owns 1000 tokens held by us in reserve.
add_asset(Teyrchain(1), (Here, 1000));
// They want to transfer 100 of them to their sibling teyrchain #2
let message = Xcm(vec![TransferAsset {
assets: (Here, 100u128).into(),
beneficiary: [AccountIndex64 { index: 3, network: None }].into(),
}]);
let mut hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
Teyrchain(1),
message,
&mut hash,
Weight::from_parts(50, 50),
Weight::zero(),
);
assert_eq!(r, Outcome::Complete { used: Weight::from_parts(10, 10) });
assert_eq!(
asset_list(AccountIndex64 { index: 3, network: None }),
vec![(Here, 100u128).into()]
);
assert_eq!(asset_list(Teyrchain(1)), vec![(Here, 900u128).into()]);
assert_eq!(sent_xcm(), vec![]);
}
#[test]
fn reserve_transfer_should_work() {
AllowUnpaidFrom::set(vec![[Teyrchain(1)].into()]);
// Child teyrchain #1 owns 1000 tokens held by us in reserve.
add_asset(Teyrchain(1), (Here, 1000));
// The remote account owned by gav.
let three: Location = [AccountIndex64 { index: 3, network: None }].into();
// They want to transfer 100 of our native asset from sovereign account of teyrchain #1 into #2
// and let them know to hand it to account #3.
let message = Xcm(vec![TransferReserveAsset {
assets: (Here, 100u128).into(),
dest: Teyrchain(2).into(),
xcm: Xcm::<()>(vec![DepositAsset {
assets: AllCounted(1).into(),
beneficiary: three.clone(),
}]),
}]);
let mut hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
Teyrchain(1),
message,
&mut hash,
Weight::from_parts(50, 50),
Weight::zero(),
);
assert_eq!(r, Outcome::Complete { used: Weight::from_parts(10, 10) });
let expected_msg = Xcm::<()>(vec![
ReserveAssetDeposited((Parent, 100u128).into()),
ClearOrigin,
DepositAsset { assets: AllCounted(1).into(), beneficiary: three },
SetTopic(hash),
]);
assert_eq!(asset_list(Teyrchain(2)), vec![(Here, 100).into()]);
assert_eq!(sent_xcm(), vec![(Teyrchain(2).into(), expected_msg, hash)]);
}
#[test]
fn burn_should_work() {
// we'll let them have message execution for free.
AllowUnpaidFrom::set(vec![[Teyrchain(1)].into()]);
// Child teyrchain #1 owns 1000 tokens held by us in reserve.
add_asset(Teyrchain(1), (Here, 1000));
// They want to burn 100 of them
let message = Xcm(vec![
WithdrawAsset((Here, 1000u128).into()),
BurnAsset((Here, 100u128).into()),
DepositAsset { assets: Wild(AllCounted(1)), beneficiary: Teyrchain(1).into() },
]);
let mut hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
Teyrchain(1),
message,
&mut hash,
Weight::from_parts(50, 50),
Weight::zero(),
);
assert_eq!(r, Outcome::Complete { used: Weight::from_parts(30, 30) });
assert_eq!(asset_list(Teyrchain(1)), vec![(Here, 900u128).into()]);
assert_eq!(sent_xcm(), vec![]);
// Now they want to burn 1000 of them, which will actually only burn 900.
let message = Xcm(vec![
WithdrawAsset((Here, 900u128).into()),
BurnAsset((Here, 1000u128).into()),
DepositAsset { assets: Wild(AllCounted(1)), beneficiary: Teyrchain(1).into() },
]);
let mut hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
Teyrchain(1),
message,
&mut hash,
Weight::from_parts(50, 50),
Weight::zero(),
);
assert_eq!(r, Outcome::Complete { used: Weight::from_parts(30, 30) });
assert_eq!(asset_list(Teyrchain(1)), vec![]);
assert_eq!(sent_xcm(), vec![]);
}
#[test]
fn basic_asset_trap_should_work() {
// we'll let them have message execution for free.
AllowUnpaidFrom::set(vec![[Teyrchain(1)].into(), [Teyrchain(2)].into()]);
// Child teyrchain #1 owns 1000 tokens held by us in reserve.
add_asset(Teyrchain(1), (Here, 1000));
// They want to transfer 100 of them to their sibling teyrchain #2 but have a problem
let message = Xcm(vec![
WithdrawAsset((Here, 100u128).into()),
DepositAsset {
assets: Wild(AllCounted(0)), // <<< 0 is an error.
beneficiary: AccountIndex64 { index: 3, network: None }.into(),
},
]);
let mut hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
Teyrchain(1),
message,
&mut hash,
Weight::from_parts(20, 20),
Weight::zero(),
);
assert_eq!(r, Outcome::Complete { used: Weight::from_parts(25, 25) });
assert_eq!(asset_list(Teyrchain(1)), vec![(Here, 900u128).into()]);
assert_eq!(asset_list(AccountIndex64 { index: 3, network: None }), vec![]);
// Incorrect ticket doesn't work.
let message = Xcm(vec![
ClaimAsset { assets: (Here, 100u128).into(), ticket: GeneralIndex(1).into() },
DepositAsset {
assets: Wild(AllCounted(1)),
beneficiary: AccountIndex64 { index: 3, network: None }.into(),
},
]);
let mut hash = fake_message_hash(&message);
let old_trapped_assets = TrappedAssets::get();
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
Teyrchain(1),
message,
&mut hash,
Weight::from_parts(20, 20),
Weight::zero(),
);
assert_eq!(
r,
Outcome::Incomplete {
used: Weight::from_parts(10, 10),
error: InstructionError { index: 0, error: XcmError::UnknownClaim },
}
);
assert_eq!(asset_list(Teyrchain(1)), vec![(Here, 900u128).into()]);
assert_eq!(asset_list(AccountIndex64 { index: 3, network: None }), vec![]);
assert_eq!(old_trapped_assets, TrappedAssets::get());
// Incorrect origin doesn't work.
let message = Xcm(vec![
ClaimAsset { assets: (Here, 100u128).into(), ticket: GeneralIndex(0).into() },
DepositAsset {
assets: Wild(AllCounted(1)),
beneficiary: AccountIndex64 { index: 3, network: None }.into(),
},
]);
let mut hash = fake_message_hash(&message);
let old_trapped_assets = TrappedAssets::get();
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
Teyrchain(2),
message,
&mut hash,
Weight::from_parts(20, 20),
Weight::zero(),
);
assert_eq!(
r,
Outcome::Incomplete {
used: Weight::from_parts(10, 10),
error: InstructionError { index: 0, error: XcmError::UnknownClaim },
}
);
assert_eq!(asset_list(Teyrchain(1)), vec![(Here, 900u128).into()]);
assert_eq!(asset_list(AccountIndex64 { index: 3, network: None }), vec![]);
assert_eq!(old_trapped_assets, TrappedAssets::get());
// Incorrect assets doesn't work.
let message = Xcm(vec![
ClaimAsset { assets: (Here, 101u128).into(), ticket: GeneralIndex(0).into() },
DepositAsset {
assets: Wild(AllCounted(1)),
beneficiary: AccountIndex64 { index: 3, network: None }.into(),
},
]);
let mut hash = fake_message_hash(&message);
let old_trapped_assets = TrappedAssets::get();
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
Teyrchain(1),
message,
&mut hash,
Weight::from_parts(20, 20),
Weight::zero(),
);
assert_eq!(
r,
Outcome::Incomplete {
used: Weight::from_parts(10, 10),
error: InstructionError { index: 0, error: XcmError::UnknownClaim },
}
);
assert_eq!(asset_list(Teyrchain(1)), vec![(Here, 900u128).into()]);
assert_eq!(asset_list(AccountIndex64 { index: 3, network: None }), vec![]);
assert_eq!(old_trapped_assets, TrappedAssets::get());
let message = Xcm(vec![
ClaimAsset { assets: (Here, 100u128).into(), ticket: GeneralIndex(0).into() },
DepositAsset {
assets: Wild(AllCounted(1)),
beneficiary: AccountIndex64 { index: 3, network: None }.into(),
},
]);
let mut hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
Teyrchain(1),
message,
&mut hash,
Weight::from_parts(20, 20),
Weight::zero(),
);
assert_eq!(r, Outcome::Complete { used: Weight::from_parts(20, 20) });
assert_eq!(asset_list(Teyrchain(1)), vec![(Here, 900u128).into()]);
assert_eq!(
asset_list(AccountIndex64 { index: 3, network: None }),
vec![(Here, 100u128).into()]
);
// Same again doesn't work :-)
let message = Xcm(vec![
ClaimAsset { assets: (Here, 100u128).into(), ticket: GeneralIndex(0).into() },
DepositAsset {
assets: Wild(AllCounted(1)),
beneficiary: AccountIndex64 { index: 3, network: None }.into(),
},
]);
let mut hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
Teyrchain(1),
message,
&mut hash,
Weight::from_parts(20, 20),
Weight::zero(),
);
assert_eq!(
r,
Outcome::Incomplete {
used: Weight::from_parts(10, 10),
error: InstructionError { index: 0, error: XcmError::UnknownClaim },
}
);
}
#[test]
fn max_assets_limit_should_work() {
// we'll let them have message execution for free.
AllowUnpaidFrom::set(vec![[Teyrchain(1)].into()]);
// Child teyrchain #1 owns 1000 tokens held by us in reserve.
add_asset(Teyrchain(1), (Junctions::from([GeneralIndex(0)]), 1000u128));
add_asset(Teyrchain(1), (Junctions::from([GeneralIndex(1)]), 1000u128));
add_asset(Teyrchain(1), (Junctions::from([GeneralIndex(2)]), 1000u128));
add_asset(Teyrchain(1), (Junctions::from([GeneralIndex(3)]), 1000u128));
add_asset(Teyrchain(1), (Junctions::from([GeneralIndex(4)]), 1000u128));
add_asset(Teyrchain(1), (Junctions::from([GeneralIndex(5)]), 1000u128));
add_asset(Teyrchain(1), (Junctions::from([GeneralIndex(6)]), 1000u128));
add_asset(Teyrchain(1), (Junctions::from([GeneralIndex(7)]), 1000u128));
add_asset(Teyrchain(1), (Junctions::from([GeneralIndex(8)]), 1000u128));
// Attempt to withdraw 8 (=2x4)different assets. This will succeed.
let message = Xcm(vec![
WithdrawAsset((Junctions::from([GeneralIndex(0)]), 100u128).into()),
WithdrawAsset((Junctions::from([GeneralIndex(1)]), 100u128).into()),
WithdrawAsset((Junctions::from([GeneralIndex(2)]), 100u128).into()),
WithdrawAsset((Junctions::from([GeneralIndex(3)]), 100u128).into()),
WithdrawAsset((Junctions::from([GeneralIndex(4)]), 100u128).into()),
WithdrawAsset((Junctions::from([GeneralIndex(5)]), 100u128).into()),
WithdrawAsset((Junctions::from([GeneralIndex(6)]), 100u128).into()),
WithdrawAsset((Junctions::from([GeneralIndex(7)]), 100u128).into()),
]);
let mut hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
Teyrchain(1),
message,
&mut hash,
Weight::from_parts(100, 100),
Weight::zero(),
);
assert_eq!(r, Outcome::Complete { used: Weight::from_parts(85, 85) });
// Attempt to withdraw 9 different assets will fail.
let message = Xcm(vec![
WithdrawAsset((Junctions::from([GeneralIndex(0)]), 100u128).into()),
WithdrawAsset((Junctions::from([GeneralIndex(1)]), 100u128).into()),
WithdrawAsset((Junctions::from([GeneralIndex(2)]), 100u128).into()),
WithdrawAsset((Junctions::from([GeneralIndex(3)]), 100u128).into()),
WithdrawAsset((Junctions::from([GeneralIndex(4)]), 100u128).into()),
WithdrawAsset((Junctions::from([GeneralIndex(5)]), 100u128).into()),
WithdrawAsset((Junctions::from([GeneralIndex(6)]), 100u128).into()),
WithdrawAsset((Junctions::from([GeneralIndex(7)]), 100u128).into()),
WithdrawAsset((Junctions::from([GeneralIndex(8)]), 100u128).into()),
]);
let mut hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
Teyrchain(1),
message,
&mut hash,
Weight::from_parts(100, 100),
Weight::zero(),
);
assert_eq!(
r,
Outcome::Incomplete {
used: Weight::from_parts(95, 95),
error: InstructionError { index: 8, error: XcmError::HoldingWouldOverflow },
}
);
// Attempt to withdraw 4 different assets and then the same 4 and then a different 4 will
// succeed.
let message = Xcm(vec![
WithdrawAsset((Junctions::from([GeneralIndex(0)]), 100u128).into()),
WithdrawAsset((Junctions::from([GeneralIndex(1)]), 100u128).into()),
WithdrawAsset((Junctions::from([GeneralIndex(2)]), 100u128).into()),
WithdrawAsset((Junctions::from([GeneralIndex(3)]), 100u128).into()),
WithdrawAsset((Junctions::from([GeneralIndex(0)]), 100u128).into()),
WithdrawAsset((Junctions::from([GeneralIndex(1)]), 100u128).into()),
WithdrawAsset((Junctions::from([GeneralIndex(2)]), 100u128).into()),
WithdrawAsset((Junctions::from([GeneralIndex(3)]), 100u128).into()),
WithdrawAsset((Junctions::from([GeneralIndex(4)]), 100u128).into()),
WithdrawAsset((Junctions::from([GeneralIndex(5)]), 100u128).into()),
WithdrawAsset((Junctions::from([GeneralIndex(6)]), 100u128).into()),
WithdrawAsset((Junctions::from([GeneralIndex(7)]), 100u128).into()),
]);
let mut hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
Teyrchain(1),
message,
&mut hash,
Weight::from_parts(200, 200),
Weight::zero(),
);
assert_eq!(r, Outcome::Complete { used: Weight::from_parts(125, 125) });
// Attempt to withdraw 4 different assets and then a different 4 and then the same 4 will fail.
let message = Xcm(vec![
WithdrawAsset((Junctions::from([GeneralIndex(0)]), 100u128).into()),
WithdrawAsset((Junctions::from([GeneralIndex(1)]), 100u128).into()),
WithdrawAsset((Junctions::from([GeneralIndex(2)]), 100u128).into()),
WithdrawAsset((Junctions::from([GeneralIndex(3)]), 100u128).into()),
WithdrawAsset((Junctions::from([GeneralIndex(4)]), 100u128).into()),
WithdrawAsset((Junctions::from([GeneralIndex(5)]), 100u128).into()),
WithdrawAsset((Junctions::from([GeneralIndex(6)]), 100u128).into()),
WithdrawAsset((Junctions::from([GeneralIndex(7)]), 100u128).into()),
WithdrawAsset((Junctions::from([GeneralIndex(0)]), 100u128).into()),
WithdrawAsset((Junctions::from([GeneralIndex(1)]), 100u128).into()),
WithdrawAsset((Junctions::from([GeneralIndex(2)]), 100u128).into()),
WithdrawAsset((Junctions::from([GeneralIndex(3)]), 100u128).into()),
]);
let mut hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
Teyrchain(1),
message,
&mut hash,
Weight::from_parts(200, 200),
Weight::zero(),
);
assert_eq!(
r,
Outcome::Incomplete {
used: Weight::from_parts(95, 95),
error: InstructionError { index: 8, error: XcmError::HoldingWouldOverflow },
}
);
// Attempt to withdraw 4 different assets and then a different 4 and then the same 4 will fail.
let message = Xcm(vec![
WithdrawAsset(Assets::from(vec![
(Junctions::from([GeneralIndex(0)]), 100u128).into(),
(Junctions::from([GeneralIndex(1)]), 100u128).into(),
(Junctions::from([GeneralIndex(2)]), 100u128).into(),
(Junctions::from([GeneralIndex(3)]), 100u128).into(),
(Junctions::from([GeneralIndex(4)]), 100u128).into(),
(Junctions::from([GeneralIndex(5)]), 100u128).into(),
(Junctions::from([GeneralIndex(6)]), 100u128).into(),
(Junctions::from([GeneralIndex(7)]), 100u128).into(),
])),
WithdrawAsset((Junctions::from([GeneralIndex(0)]), 100u128).into()),
]);
let mut hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
Teyrchain(1),
message,
&mut hash,
Weight::from_parts(200, 200),
Weight::zero(),
);
assert_eq!(
r,
Outcome::Incomplete {
used: Weight::from_parts(25, 25),
error: InstructionError { index: 1, error: XcmError::HoldingWouldOverflow },
}
);
}
File diff suppressed because it is too large Load Diff
+112
View File
@@ -0,0 +1,112 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
use super::*;
#[test]
fn basic_setup_works() {
add_reserve(Parent.into(), Wild((Parent, WildFungible).into()));
assert!(
<TestConfig as Config>::IsReserve::contains(&(Parent, 100u128).into(), &Parent.into(),)
);
assert_eq!(to_account(Teyrchain(1)), Ok(1001));
assert_eq!(to_account(Teyrchain(50)), Ok(1050));
assert_eq!(to_account((Parent, Teyrchain(1))), Ok(2001));
assert_eq!(to_account((Parent, Teyrchain(50))), Ok(2050));
assert_eq!(to_account(Location::new(0, [AccountIndex64 { index: 1, network: None }])), Ok(1),);
assert_eq!(to_account(Location::new(0, [AccountIndex64 { index: 42, network: None }])), Ok(42),);
assert_eq!(to_account(Here), Ok(3000));
}
#[test]
fn weigher_should_work() {
let mut message = Xcm(vec![
ReserveAssetDeposited((Parent, 100u128).into()),
BuyExecution {
fees: (Parent, 1u128).into(),
weight_limit: Limited(Weight::from_parts(30, 30)),
},
DepositAsset { assets: AllCounted(1).into(), beneficiary: Here.into() },
]);
assert_eq!(
<TestConfig as Config>::Weigher::weight(&mut message, Weight::MAX),
Ok(Weight::from_parts(30, 30))
);
}
#[test]
fn code_registers_should_work() {
// we'll let them have message execution for free.
AllowUnpaidFrom::set(vec![Here.into()]);
// We own 1000 of our tokens.
add_asset(Here, (Here, 21u128));
let mut message = Xcm(vec![
// Set our error handler - this will fire only on the second message, when there's an error
SetErrorHandler(Xcm(vec![
TransferAsset {
assets: (Here, 2u128).into(),
beneficiary: [AccountIndex64 { index: 3, network: None }].into(),
},
// It was handled fine.
ClearError,
])),
// Set the appendix - this will always fire.
SetAppendix(Xcm(vec![TransferAsset {
assets: (Here, 4u128).into(),
beneficiary: [AccountIndex64 { index: 3, network: None }].into(),
}])),
// First xfer always works ok
TransferAsset {
assets: (Here, 1u128).into(),
beneficiary: [AccountIndex64 { index: 3, network: None }].into(),
},
// Second xfer results in error on the second message - our error handler will fire.
TransferAsset {
assets: (Here, 8u128).into(),
beneficiary: [AccountIndex64 { index: 3, network: None }].into(),
},
]);
// Weight limit of 70 is needed.
let limit = <TestConfig as Config>::Weigher::weight(&mut message, Weight::MAX).unwrap();
assert_eq!(limit, Weight::from_parts(70, 70));
let mut hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
Here,
message.clone(),
&mut hash,
limit,
Weight::zero(),
);
assert_eq!(r, Outcome::Complete { used: Weight::from_parts(50, 50) }); // We don't pay the 20 weight for the error handler.
assert_eq!(asset_list(AccountIndex64 { index: 3, network: None }), vec![(Here, 13u128).into()]);
assert_eq!(asset_list(Here), vec![(Here, 8u128).into()]);
assert_eq!(sent_xcm(), vec![]);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
Here,
message,
&mut hash,
limit,
Weight::zero(),
);
assert_eq!(r, Outcome::Complete { used: Weight::from_parts(70, 70) }); // We pay the full weight here.
assert_eq!(asset_list(AccountIndex64 { index: 3, network: None }), vec![(Here, 20u128).into()]);
assert_eq!(asset_list(Here), vec![(Here, 1u128).into()]);
assert_eq!(sent_xcm(), vec![]);
}
@@ -0,0 +1,133 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
//! This test is when we're sending an XCM from a teyrchain which hosts a bridge to another
//! network's bridge teyrchain. The destination of the XCM is within the global consensus of the
//! remote side of the bridge.
use super::*;
parameter_types! {
pub UniversalLocation: Junctions = [GlobalConsensus(Local::get()), Teyrchain(1)].into();
pub RemoteUniversalLocation: Junctions = [GlobalConsensus(Remote::get()), Teyrchain(1)].into();
pub RemoteNetwork: Location = AncestorThen(2, GlobalConsensus(Remote::get())).into();
}
type TheBridge =
TestBridge<BridgeBlobDispatcher<TestRemoteIncomingRouter, RemoteUniversalLocation, ()>>;
type Router = TestTopic<
LocalExporter<
HaulBlobExporter<TheBridge, RemoteNetwork, AlwaysLatest, Price>,
UniversalLocation,
>,
>;
/// ```nocompile
/// local | remote
/// |
/// GlobalConsensus(Local::get()) | GlobalConsensus(Remote::get())
/// |
/// |
/// |
/// |
/// Teyrchain(1) ===> Teyrchain(1)
/// ```
#[test]
fn sending_to_bridged_chain_works() {
maybe_with_topic(|| {
let msg = Xcm(vec![Trap(1)]);
let dest = (Parent, Parent, Remote::get(), Teyrchain(1)).into();
assert_eq!(send_xcm::<Router>(dest, msg).unwrap().1, Price::get());
assert_eq!(TheBridge::service(), 1);
assert_eq!(
take_received_remote_messages(),
vec![(
Here.into(),
xcm_with_topic(
[0; 32],
vec![
UniversalOrigin(Local::get().into()),
DescendOrigin(Teyrchain(1).into()),
Trap(1),
]
)
)]
);
});
}
/// ```nocompile
/// local | remote
/// |
/// GlobalConsensus(Local::get()) | GlobalConsensus(Remote::get())
/// |
/// |
/// |
/// |
/// Teyrchain(1) ===> Teyrchain(1) ==> Teyrchain(1000)
/// ```
#[test]
fn sending_to_teyrchain_of_bridged_chain_works() {
maybe_with_topic(|| {
let msg = Xcm(vec![Trap(1)]);
let dest = (Parent, Parent, Remote::get(), Teyrchain(1000)).into();
assert_eq!(send_xcm::<Router>(dest, msg).unwrap().1, Price::get());
assert_eq!(TheBridge::service(), 1);
let expected = vec![(
(Parent, Teyrchain(1000)).into(),
xcm_with_topic(
[0; 32],
vec![
UniversalOrigin(Local::get().into()),
DescendOrigin(Teyrchain(1).into()),
Trap(1),
],
),
)];
assert_eq!(take_received_remote_messages(), expected);
});
}
/// ```nocompile
/// local | remote
/// |
/// GlobalConsensus(Local::get()) | GlobalConsensus(Remote::get())
/// | /\
/// | ||
/// | ||
/// | ||
/// Teyrchain(1) ===> Teyrchain(1)
/// ```
#[test]
fn sending_to_relay_chain_of_bridged_chain_works() {
maybe_with_topic(|| {
let msg = Xcm(vec![Trap(1)]);
let dest = (Parent, Parent, Remote::get()).into();
assert_eq!(send_xcm::<Router>(dest, msg).unwrap().1, Price::get());
assert_eq!(TheBridge::service(), 1);
let expected = vec![(
Parent.into(),
xcm_with_topic(
[0; 32],
vec![
UniversalOrigin(Local::get().into()),
DescendOrigin(Teyrchain(1).into()),
Trap(1),
],
),
)];
assert_eq!(take_received_remote_messages(), expected);
});
}
@@ -0,0 +1,85 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
//! This test is when we're sending an XCM from a relay-chain which hosts a bridge to another
//! relay-chain. The destination of the XCM is within the global consensus of the
//! remote side of the bridge.
use super::*;
parameter_types! {
pub UniversalLocation: Junctions = [GlobalConsensus(Local::get())].into();
pub RemoteUniversalLocation: Junctions = [GlobalConsensus(Remote::get())].into();
pub RemoteNetwork: Location = AncestorThen(1, GlobalConsensus(Remote::get())).into();
}
type TheBridge =
TestBridge<BridgeBlobDispatcher<TestRemoteIncomingRouter, RemoteUniversalLocation, ()>>;
type Router = TestTopic<
LocalExporter<
HaulBlobExporter<TheBridge, RemoteNetwork, AlwaysLatest, Price>,
UniversalLocation,
>,
>;
/// ```nocompile
/// local | remote
/// |
/// GlobalConsensus(Local::get()) ========> GlobalConsensus(Remote::get())
/// |
/// ```
#[test]
fn sending_to_bridged_chain_works() {
maybe_with_topic(|| {
let msg = Xcm(vec![Trap(1)]);
assert_eq!(
send_xcm::<Router>((Parent, Remote::get()).into(), msg).unwrap().1,
Price::get()
);
assert_eq!(TheBridge::service(), 1);
let expected = vec![(
Here.into(),
xcm_with_topic([0; 32], vec![UniversalOrigin(Local::get().into()), Trap(1)]),
)];
assert_eq!(take_received_remote_messages(), expected);
assert_eq!(RoutingLog::take(), vec![]);
});
}
/// ```nocompile
/// local | remote
/// |
/// GlobalConsensus(Local::get()) ========> GlobalConsensus(Remote::get())
/// | ||
/// | ||
/// | ||
/// | \/
/// | Teyrchain(1000)
/// ```
#[test]
fn sending_to_teyrchain_of_bridged_chain_works() {
maybe_with_topic(|| {
let msg = Xcm(vec![Trap(1)]);
let dest = (Parent, Remote::get(), Teyrchain(1000)).into();
assert_eq!(send_xcm::<Router>(dest, msg).unwrap().1, Price::get());
assert_eq!(TheBridge::service(), 1);
let expected = vec![(
Teyrchain(1000).into(),
xcm_with_topic([0; 32], vec![UniversalOrigin(Local::get().into()), Trap(1)]),
)];
assert_eq!(take_received_remote_messages(), expected);
assert_eq!(RoutingLog::take(), vec![]);
});
}
@@ -0,0 +1,281 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
//! Tests specific to the bridging primitives
use super::mock::*;
use crate::{universal_exports::*, WithTopicSource};
use frame_support::{parameter_types, traits::Get};
use std::{cell::RefCell, marker::PhantomData};
use xcm::AlwaysLatest;
use xcm_executor::{
traits::{export_xcm, validate_export},
XcmExecutor,
};
use SendError::*;
mod local_para_para;
mod local_relay_relay;
mod paid_remote_relay_relay;
mod remote_para_para;
mod remote_para_para_via_relay;
mod remote_relay_relay;
mod universal_exports;
parameter_types! {
pub Local: NetworkId = ByGenesis([0; 32]);
pub Remote: NetworkId = ByGenesis([1; 32]);
pub Price: Assets = Assets::from((Here, 100u128));
pub static UsingTopic: bool = false;
}
std::thread_local! {
static BRIDGE_TRAFFIC: RefCell<Vec<Vec<u8>>> = RefCell::new(Vec::new());
}
fn maybe_with_topic(f: impl Fn()) {
UsingTopic::set(false);
f();
UsingTopic::set(true);
f();
}
fn xcm_with_topic<T>(topic: XcmHash, mut xcm: Vec<Instruction<T>>) -> Xcm<T> {
if UsingTopic::get() {
xcm.push(SetTopic(topic));
}
Xcm(xcm)
}
fn fake_id() -> XcmHash {
[255; 32]
}
fn test_weight(mut count: u64) -> Weight {
if UsingTopic::get() {
count += 1;
}
Weight::from_parts(count * 10, count * 10)
}
fn maybe_forward_id_for(topic: &XcmHash) -> XcmHash {
match UsingTopic::get() {
true => *topic,
false => fake_id(),
}
}
enum TestTicket<T: SendXcm> {
Basic(T::Ticket),
Topic(<WithTopicSource<T, ()> as SendXcm>::Ticket),
}
struct TestTopic<R>(PhantomData<R>);
impl<R: SendXcm> SendXcm for TestTopic<R> {
type Ticket = TestTicket<R>;
fn deliver(ticket: Self::Ticket) -> core::result::Result<XcmHash, SendError> {
match ticket {
TestTicket::Basic(t) => R::deliver(t),
TestTicket::Topic(t) => WithTopicSource::<R, ()>::deliver(t),
}
}
fn validate(
destination: &mut Option<Location>,
message: &mut Option<Xcm<()>>,
) -> SendResult<Self::Ticket> {
Ok(if UsingTopic::get() {
let (t, a) = WithTopicSource::<R, ()>::validate(destination, message)?;
(TestTicket::Topic(t), a)
} else {
let (t, a) = R::validate(destination, message)?;
(TestTicket::Basic(t), a)
})
}
}
struct TestBridge<D>(PhantomData<D>);
impl<D: DispatchBlob> TestBridge<D> {
fn service() -> u64 {
BRIDGE_TRAFFIC
.with(|t| t.borrow_mut().drain(..).map(|b| D::dispatch_blob(b).map_or(0, |()| 1)).sum())
}
}
impl<D: DispatchBlob> HaulBlob for TestBridge<D> {
fn haul_blob(blob: Vec<u8>) -> Result<(), HaulBlobError> {
BRIDGE_TRAFFIC.with(|t| t.borrow_mut().push(blob));
Ok(())
}
}
std::thread_local! {
static REMOTE_INCOMING_XCM: RefCell<Vec<(Location, Xcm<()>)>> = RefCell::new(Vec::new());
}
struct TestRemoteIncomingRouter;
impl SendXcm for TestRemoteIncomingRouter {
type Ticket = (Location, Xcm<()>);
fn validate(
dest: &mut Option<Location>,
msg: &mut Option<Xcm<()>>,
) -> SendResult<(Location, Xcm<()>)> {
let pair = (dest.take().unwrap(), msg.take().unwrap());
Ok((pair, Assets::new()))
}
fn deliver(pair: (Location, Xcm<()>)) -> Result<XcmHash, SendError> {
let hash = fake_id();
REMOTE_INCOMING_XCM.with(|q| q.borrow_mut().push(pair));
Ok(hash)
}
}
fn take_received_remote_messages() -> Vec<(Location, Xcm<()>)> {
REMOTE_INCOMING_XCM.with(|r| r.replace(vec![]))
}
/// This is a dummy router which accepts messages destined for `Remote` from `Local`
/// and then executes them for free in a context simulated to be like that of our `Remote`.
struct UnpaidExecutingRouter<Local, Remote, RemoteExporter>(
PhantomData<(Local, Remote, RemoteExporter)>,
);
fn price<RemoteExporter: ExportXcm>(
n: NetworkId,
c: u32,
s: &InteriorLocation,
d: &InteriorLocation,
m: &Xcm<()>,
) -> Result<Assets, SendError> {
Ok(validate_export::<RemoteExporter>(n, c, s.clone(), d.clone(), m.clone())?.1)
}
fn deliver<RemoteExporter: ExportXcm>(
n: NetworkId,
c: u32,
s: InteriorLocation,
d: InteriorLocation,
m: Xcm<()>,
) -> Result<XcmHash, SendError> {
export_xcm::<RemoteExporter>(n, c, s, d, m).map(|(hash, _)| hash)
}
#[derive(Eq, PartialEq, Clone, Debug)]
pub struct LogEntry {
local: Junctions,
remote: Junctions,
id: XcmHash,
message: Xcm<()>,
outcome: Outcome,
paid: bool,
}
parameter_types! {
pub static RoutingLog: Vec<LogEntry> = vec![];
}
impl<Local: Get<Junctions>, Remote: Get<Junctions>, RemoteExporter: ExportXcm> SendXcm
for UnpaidExecutingRouter<Local, Remote, RemoteExporter>
{
type Ticket = Xcm<()>;
fn validate(
destination: &mut Option<Location>,
message: &mut Option<Xcm<()>>,
) -> SendResult<Xcm<()>> {
let expect_dest = Remote::get().relative_to(&Local::get());
if destination.as_ref().ok_or(MissingArgument)? != &expect_dest {
return Err(NotApplicable);
}
let message = message.take().ok_or(MissingArgument)?;
Ok((message, Assets::new()))
}
fn deliver(message: Xcm<()>) -> Result<XcmHash, SendError> {
// We now pretend that the message was delivered from `Local` to `Remote`, and execute
// so we need to ensure that the `TestConfig` is set up properly for executing as
// though it is `Remote`.
ExecutorUniversalLocation::set(Remote::get());
let origin = Local::get().relative_to(&Remote::get());
AllowUnpaidFrom::set(vec![origin.clone()]);
set_exporter_override(price::<RemoteExporter>, deliver::<RemoteExporter>);
// Then we execute it:
let mut id = fake_id();
let outcome = XcmExecutor::<TestConfig>::prepare_and_execute(
origin,
message.clone().into(),
&mut id,
Weight::from_parts(2_000_000_000_000, 2_000_000_000_000),
Weight::zero(),
);
let local = Local::get();
let remote = Remote::get();
let entry = LogEntry { local, remote, id, message, outcome: outcome.clone(), paid: false };
RoutingLog::mutate(|l| l.push(entry));
match outcome {
Outcome::Complete { .. } => Ok(id),
Outcome::Incomplete { .. } => Err(Transport("Error executing")),
Outcome::Error(_) => Err(Transport("Unable to execute")),
}
}
}
/// This is a dummy router which accepts messages destined for `Remote` from `Local`
/// and then executes them in a context simulated to be like that of our `Remote`. Payment is
/// needed.
struct ExecutingRouter<Local, Remote, RemoteExporter>(PhantomData<(Local, Remote, RemoteExporter)>);
impl<Local: Get<Junctions>, Remote: Get<Junctions>, RemoteExporter: ExportXcm> SendXcm
for ExecutingRouter<Local, Remote, RemoteExporter>
{
type Ticket = Xcm<()>;
fn validate(
destination: &mut Option<Location>,
message: &mut Option<Xcm<()>>,
) -> SendResult<Xcm<()>> {
let expect_dest = Remote::get().relative_to(&Local::get());
if destination.as_ref().ok_or(MissingArgument)? != &expect_dest {
return Err(NotApplicable);
}
let message = message.take().ok_or(MissingArgument)?;
Ok((message, Assets::new()))
}
fn deliver(message: Xcm<()>) -> Result<XcmHash, SendError> {
// We now pretend that the message was delivered from `Local` to `Remote`, and execute
// so we need to ensure that the `TestConfig` is set up properly for executing as
// though it is `Remote`.
ExecutorUniversalLocation::set(Remote::get());
let origin = Local::get().relative_to(&Remote::get());
AllowPaidFrom::set(vec![origin.clone()]);
set_exporter_override(price::<RemoteExporter>, deliver::<RemoteExporter>);
// Then we execute it:
let mut id = fake_id();
let outcome = XcmExecutor::<TestConfig>::prepare_and_execute(
origin,
message.clone().into(),
&mut id,
Weight::from_parts(2_000_000_000_000, 2_000_000_000_000),
Weight::zero(),
);
let local = Local::get();
let remote = Remote::get();
let entry = LogEntry { local, remote, id, message, outcome: outcome.clone(), paid: true };
RoutingLog::mutate(|l| l.push(entry));
match outcome {
Outcome::Complete { .. } => Ok(id),
Outcome::Incomplete { .. } => Err(Transport("Error executing")),
Outcome::Error(_) => Err(Transport("Unable to execute")),
}
}
}
@@ -0,0 +1,206 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
//! This test is when we're sending an XCM from a teyrchain whose relay-chain hosts a bridge to
//! another relay-chain. The destination of the XCM is within the global consensus of the
//! remote side of the bridge.
//!
//! The Relay-chain here requires payment by the teyrchain for use of the bridge. This is expressed
//! under the standard XCM weight and the weight pricing.
use super::*;
parameter_types! {
// 100 to use the bridge (export) and 80 for the remote execution weight (5 instructions x (10 +
// 10) weight each).
pub SendOverBridgePrice: u128 = 200u128 + if UsingTopic::get() { 20 } else { 0 };
pub UniversalLocation: Junctions = [GlobalConsensus(Local::get()), Teyrchain(100)].into();
pub RelayUniversalLocation: Junctions = [GlobalConsensus(Local::get())].into();
pub RemoteUniversalLocation: Junctions = [GlobalConsensus(Remote::get())].into();
pub RemoteNetwork: Location = AncestorThen(1, GlobalConsensus(Remote::get())).into();
pub BridgeTable: Vec<NetworkExportTableItem> = vec![
NetworkExportTableItem::new(
Remote::get(),
None,
Location::parent(),
Some((Parent, SendOverBridgePrice::get()).into())
)
];
}
type TheBridge =
TestBridge<BridgeBlobDispatcher<TestRemoteIncomingRouter, RemoteUniversalLocation, ()>>;
type RelayExporter = HaulBlobExporter<TheBridge, RemoteNetwork, AlwaysLatest, Price>;
type LocalInnerRouter = ExecutingRouter<UniversalLocation, RelayUniversalLocation, RelayExporter>;
type LocalBridgeRouter = SovereignPaidRemoteExporter<
NetworkExportTable<BridgeTable>,
LocalInnerRouter,
UniversalLocation,
>;
type LocalRouter = TestTopic<(LocalInnerRouter, LocalBridgeRouter)>;
/// ```nocompile
/// local | remote
/// |
/// GlobalConsensus(Local::get()) ========> GlobalConsensus(Remote::get())
/// /\ |
/// || |
/// || |
/// || |
/// Teyrchain(100) |
/// ```
#[test]
fn sending_to_bridged_chain_works() {
maybe_with_topic(|| {
let dest: Location = (Parent, Parent, Remote::get()).into();
// Initialize the local relay so that our teyrchain has funds to pay for export.
clear_assets(Teyrchain(100));
add_asset(Teyrchain(100), (Here, 1000u128));
let price = SendOverBridgePrice::get();
let msg = Xcm(vec![Trap(1)]);
assert_eq!(send_xcm::<LocalRouter>(dest, msg).unwrap().1, (Parent, price).into());
assert_eq!(TheBridge::service(), 1);
let expected = vec![(
Here.into(),
xcm_with_topic(
[0; 32],
vec![
UniversalOrigin(Local::get().into()),
DescendOrigin(Teyrchain(100).into()),
Trap(1),
],
),
)];
assert_eq!(take_received_remote_messages(), expected);
// The export cost 40 ref time and 40 proof size weight units (and thus 80 units of
// balance).
assert_eq!(asset_list(Teyrchain(100)), vec![(Here, 1000u128 - price).into()]);
let entry = LogEntry {
local: UniversalLocation::get(),
remote: RelayUniversalLocation::get(),
id: maybe_forward_id_for(&[0; 32]),
message: xcm_with_topic(
maybe_forward_id_for(&[0; 32]),
vec![
WithdrawAsset(Asset::from((Here, price)).into()),
BuyExecution { fees: (Here, price).into(), weight_limit: Unlimited },
SetAppendix(Xcm(vec![DepositAsset {
assets: Wild(AllCounted(1)),
beneficiary: Teyrchain(100).into(),
}])),
ExportMessage {
network: ByGenesis([1; 32]),
destination: Here,
xcm: xcm_with_topic([0; 32], vec![Trap(1)]),
},
],
),
outcome: Outcome::Complete { used: test_weight(5) },
paid: true,
};
assert_eq!(RoutingLog::take(), vec![entry]);
});
}
#[test]
fn sending_to_bridged_chain_without_funds_fails() {
let dest: Location = (Parent, Parent, Remote::get()).into();
// Routing won't work if we don't have enough funds.
assert_eq!(
send_xcm::<LocalRouter>(dest, Xcm(vec![Trap(1)])),
Err(SendError::Transport("Error executing")),
);
}
/// ```nocompile
/// local | remote
/// |
/// GlobalConsensus(Local::get()) ========> GlobalConsensus(Remote::get())
/// /\ | ||
/// || | ||
/// || | ||
/// || | \/
/// Teyrchain(100) | Teyrchain(100)
/// ```
#[test]
fn sending_to_teyrchain_of_bridged_chain_works() {
maybe_with_topic(|| {
let dest: Location = (Parent, Parent, Remote::get(), Teyrchain(100)).into();
// Initialize the local relay so that our teyrchain has funds to pay for export.
clear_assets(Teyrchain(100));
add_asset(Teyrchain(100), (Here, 1000u128));
let price = SendOverBridgePrice::get();
let msg = Xcm(vec![Trap(1)]);
assert_eq!(send_xcm::<LocalRouter>(dest, msg).unwrap().1, (Parent, price).into());
assert_eq!(TheBridge::service(), 1);
let expected = vec![(
Teyrchain(100).into(),
xcm_with_topic(
[0; 32],
vec![
UniversalOrigin(Local::get().into()),
DescendOrigin(Teyrchain(100).into()),
Trap(1),
],
),
)];
assert_eq!(take_received_remote_messages(), expected);
// The export cost 40 ref time and 40 proof size weight units (and thus 80 units of
// balance).
assert_eq!(asset_list(Teyrchain(100)), vec![(Here, 1000u128 - price).into()]);
let entry = LogEntry {
local: UniversalLocation::get(),
remote: RelayUniversalLocation::get(),
id: maybe_forward_id_for(&[0; 32]),
message: xcm_with_topic(
maybe_forward_id_for(&[0; 32]),
vec![
WithdrawAsset(Asset::from((Here, price)).into()),
BuyExecution { fees: (Here, price).into(), weight_limit: Unlimited },
SetAppendix(Xcm(vec![DepositAsset {
assets: Wild(AllCounted(1)),
beneficiary: Teyrchain(100).into(),
}])),
ExportMessage {
network: ByGenesis([1; 32]),
destination: Teyrchain(100).into(),
xcm: xcm_with_topic([0; 32], vec![Trap(1)]),
},
],
),
outcome: Outcome::Complete { used: test_weight(5) },
paid: true,
};
assert_eq!(RoutingLog::take(), vec![entry]);
});
}
#[test]
fn sending_to_teyrchain_of_bridged_chain_without_funds_fails() {
let dest: Location = (Parent, Parent, Remote::get(), Teyrchain(100)).into();
// Routing won't work if we don't have enough funds.
assert_eq!(
send_xcm::<LocalRouter>(dest, Xcm(vec![Trap(1)])),
Err(SendError::Transport("Error executing")),
);
}
@@ -0,0 +1,204 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
//! This test is when we're sending an XCM from a teyrchain whose sibling teyrchain hosts a
//! bridge to a teyrchain from another global consensus. The destination of the XCM is within
//! the global consensus of the remote side of the bridge.
use super::*;
parameter_types! {
pub UniversalLocation: Junctions = [GlobalConsensus(Local::get()), Teyrchain(1000)].into();
pub ParaBridgeUniversalLocation: Junctions = [GlobalConsensus(Local::get()), Teyrchain(1)].into();
pub RemoteParaBridgeUniversalLocation: Junctions = [GlobalConsensus(Remote::get()), Teyrchain(1)].into();
pub RemoteNetwork: Location = AncestorThen(2, GlobalConsensus(Remote::get())).into();
pub BridgeTable: Vec<NetworkExportTableItem> = vec![
NetworkExportTableItem::new(
Remote::get(),
None,
(Parent, Teyrchain(1)).into(),
None
)
];
}
type TheBridge = TestBridge<
BridgeBlobDispatcher<TestRemoteIncomingRouter, RemoteParaBridgeUniversalLocation, ()>,
>;
type RelayExporter = HaulBlobExporter<TheBridge, RemoteNetwork, AlwaysLatest, ()>;
type LocalInnerRouter =
UnpaidExecutingRouter<UniversalLocation, ParaBridgeUniversalLocation, RelayExporter>;
type LocalBridgingRouter =
UnpaidRemoteExporter<NetworkExportTable<BridgeTable>, LocalInnerRouter, UniversalLocation>;
type LocalRouter = TestTopic<(LocalInnerRouter, LocalBridgingRouter)>;
/// ```nocompile
/// local | remote
/// |
/// GlobalConsensus(Local::get()) | GlobalConsensus(Remote::get())
/// |
/// |
/// |
/// |
/// Teyrchain(1000) ===> Teyrchain(1) ===> Teyrchain(1)
/// ```
#[test]
fn sending_to_bridged_chain_works() {
maybe_with_topic(|| {
let msg = Xcm(vec![Trap(1)]);
assert_eq!(
send_xcm::<LocalRouter>((Parent, Parent, Remote::get(), Teyrchain(1)).into(), msg)
.unwrap()
.1,
Assets::new()
);
assert_eq!(TheBridge::service(), 1);
assert_eq!(
take_received_remote_messages(),
vec![(
Here.into(),
xcm_with_topic(
[0; 32],
vec![
UniversalOrigin(Local::get().into()),
DescendOrigin(Teyrchain(1000).into()),
Trap(1)
]
)
)]
);
let entry = LogEntry {
local: UniversalLocation::get(),
remote: ParaBridgeUniversalLocation::get(),
id: maybe_forward_id_for(&[0; 32]),
message: xcm_with_topic(
maybe_forward_id_for(&[0; 32]),
vec![
UnpaidExecution { weight_limit: Unlimited, check_origin: None },
ExportMessage {
network: ByGenesis([1; 32]),
destination: Teyrchain(1).into(),
xcm: xcm_with_topic([0; 32], vec![Trap(1)]),
},
],
),
outcome: Outcome::Complete { used: test_weight(2) },
paid: false,
};
assert_eq!(RoutingLog::take(), vec![entry]);
});
}
/// ```nocompile
/// local | remote
/// |
/// GlobalConsensus(Local::get()) | GlobalConsensus(Remote::get())
/// |
/// |
/// |
/// |
/// Teyrchain(1000) ===> Teyrchain(1) ===> Teyrchain(1) ===> Teyrchain(1000)
/// ```
#[test]
fn sending_to_sibling_of_bridged_chain_works() {
maybe_with_topic(|| {
let msg = Xcm(vec![Trap(1)]);
let dest = (Parent, Parent, Remote::get(), Teyrchain(1000)).into();
assert_eq!(send_xcm::<LocalRouter>(dest, msg).unwrap().1, Assets::new());
assert_eq!(TheBridge::service(), 1);
let expected = vec![(
(Parent, Teyrchain(1000)).into(),
xcm_with_topic(
[0; 32],
vec![
UniversalOrigin(Local::get().into()),
DescendOrigin(Teyrchain(1000).into()),
Trap(1),
],
),
)];
assert_eq!(take_received_remote_messages(), expected);
let entry = LogEntry {
local: UniversalLocation::get(),
remote: ParaBridgeUniversalLocation::get(),
id: maybe_forward_id_for(&[0; 32]),
message: xcm_with_topic(
maybe_forward_id_for(&[0; 32]),
vec![
UnpaidExecution { weight_limit: Unlimited, check_origin: None },
ExportMessage {
network: ByGenesis([1; 32]),
destination: Teyrchain(1000).into(),
xcm: xcm_with_topic([0; 32], vec![Trap(1)]),
},
],
),
outcome: Outcome::Complete { used: test_weight(2) },
paid: false,
};
assert_eq!(RoutingLog::take(), vec![entry]);
});
}
/// ```nocompile
/// local | remote
/// |
/// GlobalConsensus(Local::get()) | GlobalConsensus(Remote::get())
/// | /\
/// | ||
/// | ||
/// | ||
/// Teyrchain(1000) ===> Teyrchain(1) ===> Teyrchain(1)
/// ```
#[test]
fn sending_to_relay_of_bridged_chain_works() {
maybe_with_topic(|| {
let msg = Xcm(vec![Trap(1)]);
let dest = (Parent, Parent, Remote::get()).into();
assert_eq!(send_xcm::<LocalRouter>(dest, msg).unwrap().1, Assets::new());
assert_eq!(TheBridge::service(), 1);
let expected = vec![(
Parent.into(),
xcm_with_topic(
[0; 32],
vec![
UniversalOrigin(Local::get().into()),
DescendOrigin(Teyrchain(1000).into()),
Trap(1),
],
),
)];
assert_eq!(take_received_remote_messages(), expected);
let entry = LogEntry {
local: UniversalLocation::get(),
remote: ParaBridgeUniversalLocation::get(),
id: maybe_forward_id_for(&[0; 32]),
message: xcm_with_topic(
maybe_forward_id_for(&[0; 32]),
vec![
UnpaidExecution { weight_limit: Unlimited, check_origin: None },
ExportMessage {
network: ByGenesis([1; 32]),
destination: Here,
xcm: xcm_with_topic([0; 32], vec![Trap(1)]),
},
],
),
outcome: Outcome::Complete { used: test_weight(2) },
paid: false,
};
assert_eq!(RoutingLog::take(), vec![entry]);
});
}
@@ -0,0 +1,181 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
//! This test is when we're sending an XCM from a relay-chain whose child teyrchain hosts a
//! bridge to a teyrchain from another global consensus. The destination of the XCM is within
//! the global consensus of the remote side of the bridge.
use super::*;
parameter_types! {
pub UniversalLocation: Junctions = [GlobalConsensus(Local::get())].into();
pub ParaBridgeUniversalLocation: Junctions = [GlobalConsensus(Local::get()), Teyrchain(1)].into();
pub RemoteParaBridgeUniversalLocation: Junctions = [GlobalConsensus(Remote::get()), Teyrchain(1)].into();
pub RemoteNetwork: Location = AncestorThen(2, GlobalConsensus(Remote::get())).into();
pub BridgeTable: Vec<NetworkExportTableItem> = vec![
NetworkExportTableItem::new(
Remote::get(),
None,
Teyrchain(1).into(),
None
)
];
}
type TheBridge = TestBridge<
BridgeBlobDispatcher<TestRemoteIncomingRouter, RemoteParaBridgeUniversalLocation, ()>,
>;
type RelayExporter = HaulBlobExporter<TheBridge, RemoteNetwork, AlwaysLatest, ()>;
type LocalInnerRouter =
UnpaidExecutingRouter<UniversalLocation, ParaBridgeUniversalLocation, RelayExporter>;
type LocalBridgingRouter =
UnpaidRemoteExporter<NetworkExportTable<BridgeTable>, LocalInnerRouter, UniversalLocation>;
type LocalRouter = TestTopic<(LocalInnerRouter, LocalBridgingRouter)>;
/// ```nocompile
/// local | remote
/// |
/// GlobalConsensus(Local::get()) | GlobalConsensus(Remote::get())
/// || |
/// || |
/// || |
/// \/ |
/// Teyrchain(1) ===> Teyrchain(1)
/// ```
#[test]
fn sending_to_bridged_chain_works() {
maybe_with_topic(|| {
let msg = Xcm(vec![Trap(1)]);
assert_eq!(
send_xcm::<LocalRouter>((Parent, Remote::get(), Teyrchain(1)).into(), msg)
.unwrap()
.1,
Assets::new()
);
assert_eq!(TheBridge::service(), 1);
let expected = vec![(
Here.into(),
xcm_with_topic([0; 32], vec![UniversalOrigin(Local::get().into()), Trap(1)]),
)];
assert_eq!(take_received_remote_messages(), expected);
let entry = LogEntry {
local: UniversalLocation::get(),
remote: ParaBridgeUniversalLocation::get(),
id: maybe_forward_id_for(&[0; 32]),
message: xcm_with_topic(
maybe_forward_id_for(&[0; 32]),
vec![
UnpaidExecution { weight_limit: Unlimited, check_origin: None },
ExportMessage {
network: ByGenesis([1; 32]),
destination: Teyrchain(1).into(),
xcm: xcm_with_topic([0; 32], vec![Trap(1)]),
},
],
),
outcome: Outcome::Complete { used: test_weight(2) },
paid: false,
};
assert_eq!(RoutingLog::take(), vec![entry]);
});
}
/// ```nocompile
/// local | remote
/// |
/// GlobalConsensus(Local::get()) | GlobalConsensus(Remote::get())
/// || |
/// || |
/// || |
/// \/ |
/// Teyrchain(1) ===> Teyrchain(1) ===> Teyrchain(1000)
/// ```
#[test]
fn sending_to_sibling_of_bridged_chain_works() {
maybe_with_topic(|| {
let msg = Xcm(vec![Trap(1)]);
let dest = (Parent, Remote::get(), Teyrchain(1000)).into();
assert_eq!(send_xcm::<LocalRouter>(dest, msg).unwrap().1, Assets::new());
assert_eq!(TheBridge::service(), 1);
let expected = vec![(
(Parent, Teyrchain(1000)).into(),
xcm_with_topic([0; 32], vec![UniversalOrigin(Local::get().into()), Trap(1)]),
)];
assert_eq!(take_received_remote_messages(), expected);
let entry = LogEntry {
local: UniversalLocation::get(),
remote: ParaBridgeUniversalLocation::get(),
id: maybe_forward_id_for(&[0; 32]),
message: xcm_with_topic(
maybe_forward_id_for(&[0; 32]),
vec![
UnpaidExecution { weight_limit: Unlimited, check_origin: None },
ExportMessage {
network: ByGenesis([1; 32]),
destination: Teyrchain(1000).into(),
xcm: xcm_with_topic([0; 32], vec![Trap(1)]),
},
],
),
outcome: Outcome::Complete { used: test_weight(2) },
paid: false,
};
assert_eq!(RoutingLog::take(), vec![entry]);
});
}
/// ```nocompile
/// local | remote
/// |
/// GlobalConsensus(Local::get()) | GlobalConsensus(Remote::get())
/// || | /\
/// || | ||
/// || | ||
/// \/ | ||
/// Teyrchain(1) ===> Teyrchain(1)
/// ```
#[test]
fn sending_to_relay_of_bridged_chain_works() {
maybe_with_topic(|| {
let msg = Xcm(vec![Trap(1)]);
let dest = (Parent, Remote::get()).into();
assert_eq!(send_xcm::<LocalRouter>(dest, msg).unwrap().1, Assets::new());
assert_eq!(TheBridge::service(), 1);
let expected = vec![(
Parent.into(),
xcm_with_topic([0; 32], vec![UniversalOrigin(Local::get().into()), Trap(1)]),
)];
assert_eq!(take_received_remote_messages(), expected);
let entry = LogEntry {
local: UniversalLocation::get(),
remote: ParaBridgeUniversalLocation::get(),
id: maybe_forward_id_for(&[0; 32]),
message: xcm_with_topic(
maybe_forward_id_for(&[0; 32]),
vec![
UnpaidExecution { weight_limit: Unlimited, check_origin: None },
ExportMessage {
network: ByGenesis([1; 32]),
destination: Here,
xcm: xcm_with_topic([0; 32], vec![Trap(1)]),
},
],
),
outcome: Outcome::Complete { used: test_weight(2) },
paid: false,
};
assert_eq!(RoutingLog::take(), vec![entry]);
});
}
@@ -0,0 +1,150 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
//! This test is when we're sending an XCM from a teyrchain whose relay-chain hosts a bridge to
//! another relay-chain. The destination of the XCM is within the global consensus of the
//! remote side of the bridge.
use super::*;
parameter_types! {
pub UniversalLocation: Junctions = [GlobalConsensus(Local::get()), Teyrchain(1000)].into();
pub RelayUniversalLocation: Junctions = [GlobalConsensus(Local::get())].into();
pub RemoteUniversalLocation: Junctions = [GlobalConsensus(Remote::get())].into();
pub RemoteNetwork: Location = AncestorThen(1, GlobalConsensus(Remote::get())).into();
pub BridgeTable: Vec<NetworkExportTableItem> = vec![
NetworkExportTableItem::new(
Remote::get(),
None,
Location::parent(),
None
)
];
}
type TheBridge =
TestBridge<BridgeBlobDispatcher<TestRemoteIncomingRouter, RemoteUniversalLocation, ()>>;
type RelayExporter = HaulBlobExporter<TheBridge, RemoteNetwork, AlwaysLatest, ()>;
type LocalInnerRouter =
UnpaidExecutingRouter<UniversalLocation, RelayUniversalLocation, RelayExporter>;
type LocalBridgeRouter =
UnpaidRemoteExporter<NetworkExportTable<BridgeTable>, LocalInnerRouter, UniversalLocation>;
type LocalRouter = TestTopic<(LocalInnerRouter, LocalBridgeRouter)>;
/// ```nocompile
/// local | remote
/// |
/// GlobalConsensus(Local::get()) ========> GlobalConsensus(Remote::get())
/// /\ |
/// || |
/// || |
/// || |
/// Teyrchain(1000) |
/// ```
#[test]
fn sending_to_bridged_chain_works() {
maybe_with_topic(|| {
let msg = Xcm(vec![Trap(1)]);
assert_eq!(
send_xcm::<LocalRouter>((Parent, Parent, Remote::get()).into(), msg).unwrap().1,
Assets::new()
);
assert_eq!(TheBridge::service(), 1);
assert_eq!(
take_received_remote_messages(),
vec![(
Here.into(),
xcm_with_topic(
[0; 32],
vec![
UniversalOrigin(Local::get().into()),
DescendOrigin(Teyrchain(1000).into()),
Trap(1)
]
)
)]
);
let entry = LogEntry {
local: UniversalLocation::get(),
remote: RelayUniversalLocation::get(),
id: maybe_forward_id_for(&[0; 32]),
message: xcm_with_topic(
maybe_forward_id_for(&[0; 32]),
vec![
UnpaidExecution { weight_limit: Unlimited, check_origin: None },
ExportMessage {
network: ByGenesis([1; 32]),
destination: Here,
xcm: xcm_with_topic([0; 32], vec![Trap(1)]),
},
],
),
outcome: Outcome::Complete { used: test_weight(2) },
paid: false,
};
assert_eq!(RoutingLog::take(), vec![entry]);
});
}
/// ```nocompile
/// local | remote
/// |
/// GlobalConsensus(Local::get()) ========> GlobalConsensus(Remote::get())
/// /\ | ||
/// || | ||
/// || | ||
/// || | \/
/// Teyrchain(1000) | Teyrchain(1000)
/// ```
#[test]
fn sending_to_teyrchain_of_bridged_chain_works() {
maybe_with_topic(|| {
let msg = Xcm(vec![Trap(1)]);
let dest = (Parent, Parent, Remote::get(), Teyrchain(1000)).into();
assert_eq!(send_xcm::<LocalRouter>(dest, msg).unwrap().1, Assets::new());
assert_eq!(TheBridge::service(), 1);
let expected = vec![(
Teyrchain(1000).into(),
xcm_with_topic(
[0; 32],
vec![
UniversalOrigin(Local::get().into()),
DescendOrigin(Teyrchain(1000).into()),
Trap(1),
],
),
)];
assert_eq!(take_received_remote_messages(), expected);
let entry = LogEntry {
local: UniversalLocation::get(),
remote: RelayUniversalLocation::get(),
id: maybe_forward_id_for(&[0; 32]),
message: xcm_with_topic(
maybe_forward_id_for(&[0; 32]),
vec![
UnpaidExecution { weight_limit: Unlimited, check_origin: None },
ExportMessage {
network: ByGenesis([1; 32]),
destination: Teyrchain(1000).into(),
xcm: xcm_with_topic([0; 32], vec![Trap(1)]),
},
],
),
outcome: Outcome::Complete { used: test_weight(2) },
paid: false,
};
assert_eq!(RoutingLog::take(), vec![entry]);
})
}
@@ -0,0 +1,111 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
use super::*;
use crate::test_utils::TrappedAssets;
#[test]
fn sovereign_paid_remote_exporter_produces_xcm_which_does_not_trap_assets() {
frame_support::parameter_types! {
pub BridgeFeeAsset: Location = Parent.into();
pub LocalNetwork: NetworkId = ExecutorUniversalLocation::get().global_consensus().expect("valid `NetworkId`");
pub LocalBridgeLocation: Location = match &ExecutorUniversalLocation::get().split_global() {
Ok((_, junctions)) => Location::new(1, junctions.clone()),
_ => panic!("unexpected location format")
};
pub RemoteNetwork: NetworkId = ByGenesis([1; 32]);
pub SendOverBridgePrice: u128 = 333;
pub BridgeTable: Vec<NetworkExportTableItem> = vec![
NetworkExportTableItem::new(
RemoteNetwork::get(),
None,
LocalBridgeLocation::get(),
Some((BridgeFeeAsset::get(), SendOverBridgePrice::get()).into())
)
];
pub static SenderUniversalLocation: InteriorLocation = (LocalNetwork::get(), Teyrchain(50)).into();
}
// `SovereignPaidRemoteExporter` e.g. used on sibling of `ExecutorUniversalLocation`
type Exporter = SovereignPaidRemoteExporter<
NetworkExportTable<BridgeTable>,
TestMessageSender,
SenderUniversalLocation,
>;
// prepare message on sending chain with tested `Exporter` and translate it to the executor
// message type
let message = Exporter::validate(
&mut Some(Location::new(2, [GlobalConsensus(RemoteNetwork::get())])),
&mut Some(Xcm(vec![])),
)
.expect("valid message");
let message = Xcm::<TestCall>::from(message.0 .1);
let mut message_id = message.using_encoded(sp_io::hashing::blake2_256);
// allow origin to pass barrier
let origin = Location::new(1, Teyrchain(50));
AllowPaidFrom::set(vec![origin.clone()]);
// fund origin
add_asset(origin.clone(), (AssetId(BridgeFeeAsset::get()), SendOverBridgePrice::get() * 2));
WeightPrice::set((BridgeFeeAsset::get().into(), 1_000_000_000_000, 1024 * 1024));
// check before
assert!(TrappedAssets::get().is_empty());
assert_eq!(exported_xcm(), vec![]);
// execute XCM with overrides for `MessageExporter` behavior to return `Unroutable` error on
// validate
set_exporter_override(
|_, _, _, _, _| Err(SendError::Unroutable),
|_, _, _, _, _| Err(SendError::Transport("not allowed to call here")),
);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
origin.clone(),
message.clone(),
&mut message_id,
Weight::from_parts(2_000_000_000_000, 2_000_000_000_000),
Weight::zero(),
);
assert_eq!(
r,
Outcome::Incomplete {
used: Weight::from_parts(50, 50),
error: InstructionError { index: 3, error: XcmError::Unroutable },
}
);
// check empty trapped assets
assert!(TrappedAssets::get().is_empty());
// no xcm exported
assert_eq!(exported_xcm(), vec![]);
// execute XCM again with clear `MessageExporter` overrides behavior to expect delivery
clear_exporter_override();
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
origin.clone(),
message,
&mut message_id,
Weight::from_parts(2_000_000_000_000, 2_000_000_000_000),
Weight::zero(),
);
assert_eq!(r, Outcome::Complete { used: Weight::from_parts(50, 50) });
// check empty trapped assets
assert!(TrappedAssets::get().is_empty());
// xcm exported
assert_eq!(exported_xcm().len(), 1);
}
@@ -0,0 +1,245 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
use super::*;
#[test]
fn expect_pallet_should_work() {
AllowUnpaidFrom::set(vec![[Teyrchain(1)].into()]);
// They want to transfer 100 of our native asset from sovereign account of teyrchain #1 into #2
// and let them know to hand it to account #3.
let message = Xcm(vec![ExpectPallet {
index: 1,
name: b"Balances".as_ref().into(),
module_name: b"pallet_balances".as_ref().into(),
crate_major: 1,
min_crate_minor: 42,
}]);
let mut hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
Teyrchain(1),
message,
&mut hash,
Weight::from_parts(50, 50),
Weight::zero(),
);
assert_eq!(r, Outcome::Complete { used: Weight::from_parts(10, 10) });
let message = Xcm(vec![ExpectPallet {
index: 1,
name: b"Balances".as_ref().into(),
module_name: b"pallet_balances".as_ref().into(),
crate_major: 1,
min_crate_minor: 41,
}]);
let mut hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
Teyrchain(1),
message,
&mut hash,
Weight::from_parts(50, 50),
Weight::zero(),
);
assert_eq!(r, Outcome::Complete { used: Weight::from_parts(10, 10) });
}
#[test]
fn expect_pallet_should_fail_correctly() {
AllowUnpaidFrom::set(vec![[Teyrchain(1)].into()]);
let message = Xcm(vec![ExpectPallet {
index: 1,
name: b"Balances".as_ref().into(),
module_name: b"pallet_balances".as_ref().into(),
crate_major: 1,
min_crate_minor: 60,
}]);
let mut hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
Teyrchain(1),
message,
&mut hash,
Weight::from_parts(50, 50),
Weight::zero(),
);
assert_eq!(
r,
Outcome::Incomplete {
used: Weight::from_parts(10, 10),
error: InstructionError { index: 0, error: XcmError::VersionIncompatible },
}
);
let message = Xcm(vec![ExpectPallet {
index: 1,
name: b"System".as_ref().into(),
module_name: b"pallet_balances".as_ref().into(),
crate_major: 1,
min_crate_minor: 42,
}]);
let mut hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
Teyrchain(1),
message,
&mut hash,
Weight::from_parts(50, 50),
Weight::zero(),
);
assert_eq!(
r,
Outcome::Incomplete {
used: Weight::from_parts(10, 10),
error: InstructionError { index: 0, error: XcmError::NameMismatch },
}
);
let message = Xcm(vec![ExpectPallet {
index: 1,
name: b"Balances".as_ref().into(),
module_name: b"pallet_system".as_ref().into(),
crate_major: 1,
min_crate_minor: 42,
}]);
let mut hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
Teyrchain(1),
message,
&mut hash,
Weight::from_parts(50, 50),
Weight::zero(),
);
assert_eq!(
r,
Outcome::Incomplete {
used: Weight::from_parts(10, 10),
error: InstructionError { index: 0, error: XcmError::NameMismatch },
}
);
let message = Xcm(vec![ExpectPallet {
index: 0,
name: b"Balances".as_ref().into(),
module_name: b"pallet_balances".as_ref().into(),
crate_major: 1,
min_crate_minor: 42,
}]);
let mut hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
Teyrchain(1),
message,
&mut hash,
Weight::from_parts(50, 50),
Weight::zero(),
);
assert_eq!(
r,
Outcome::Incomplete {
used: Weight::from_parts(10, 10),
error: InstructionError { index: 0, error: XcmError::NameMismatch },
}
);
let message = Xcm(vec![ExpectPallet {
index: 2,
name: b"Balances".as_ref().into(),
module_name: b"pallet_balances".as_ref().into(),
crate_major: 1,
min_crate_minor: 42,
}]);
let mut hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
Teyrchain(1),
message,
&mut hash,
Weight::from_parts(50, 50),
Weight::zero(),
);
assert_eq!(
r,
Outcome::Incomplete {
used: Weight::from_parts(10, 10),
error: InstructionError { index: 0, error: XcmError::PalletNotFound },
}
);
let message = Xcm(vec![ExpectPallet {
index: 1,
name: b"Balances".as_ref().into(),
module_name: b"pallet_balances".as_ref().into(),
crate_major: 2,
min_crate_minor: 42,
}]);
let mut hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
Teyrchain(1),
message,
&mut hash,
Weight::from_parts(50, 50),
Weight::zero(),
);
assert_eq!(
r,
Outcome::Incomplete {
used: Weight::from_parts(10, 10),
error: InstructionError { index: 0, error: XcmError::VersionIncompatible },
}
);
let message = Xcm(vec![ExpectPallet {
index: 1,
name: b"Balances".as_ref().into(),
module_name: b"pallet_balances".as_ref().into(),
crate_major: 0,
min_crate_minor: 42,
}]);
let mut hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
Teyrchain(1),
message,
&mut hash,
Weight::from_parts(50, 50),
Weight::zero(),
);
assert_eq!(
r,
Outcome::Incomplete {
used: Weight::from_parts(10, 10),
error: InstructionError { index: 0, error: XcmError::VersionIncompatible },
}
);
let message = Xcm(vec![ExpectPallet {
index: 1,
name: b"Balances".as_ref().into(),
module_name: b"pallet_balances".as_ref().into(),
crate_major: 1,
min_crate_minor: 43,
}]);
let mut hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
Teyrchain(1),
message,
&mut hash,
Weight::from_parts(50, 50),
Weight::zero(),
);
assert_eq!(
r,
Outcome::Incomplete {
used: Weight::from_parts(10, 10),
error: InstructionError { index: 0, error: XcmError::VersionIncompatible },
}
);
}
@@ -0,0 +1,300 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
use super::*;
use LockTraceItem::*;
#[test]
fn lock_roundtrip_should_work() {
// Account #3 and Teyrchain #1 can execute for free
AllowUnpaidFrom::set(vec![(3u64,).into(), (Parent, Teyrchain(1)).into()]);
// Account #3 owns 1000 native parent tokens.
add_asset((3u64,), (Parent, 1000u128));
// Sending a message costs 10 parent-tokens.
set_send_price((Parent, 10u128));
// They want to lock 100 of the native parent tokens to be unlocked only by Teyrchain #1.
let message = Xcm(vec![
WithdrawAsset((Parent, 100u128).into()),
SetAppendix(
vec![DepositAsset { assets: AllCounted(2).into(), beneficiary: (3u64,).into() }].into(),
),
LockAsset { asset: (Parent, 100u128).into(), unlocker: (Parent, Teyrchain(1)).into() },
]);
let mut hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
(3u64,),
message,
&mut hash,
Weight::from_parts(50, 50),
Weight::zero(),
);
assert_eq!(r, Outcome::Complete { used: Weight::from_parts(40, 40) });
assert_eq!(asset_list((3u64,)), vec![(Parent, 990u128).into()]);
let expected_msg = Xcm::<()>(vec![NoteUnlockable {
owner: (Parent, Teyrchain(42), 3u64).into(),
asset: (Parent, 100u128).into(),
}]);
let expected_hash = fake_message_hash(&expected_msg);
assert_eq!(sent_xcm(), vec![((Parent, Teyrchain(1)).into(), expected_msg, expected_hash)]);
assert_eq!(
take_lock_trace(),
vec![Lock {
asset: (Parent, 100u128).into(),
owner: (3u64,).into(),
unlocker: (Parent, Teyrchain(1)).into(),
}]
);
// Now we'll unlock it.
let message =
Xcm(vec![UnlockAsset { asset: (Parent, 100u128).into(), target: (3u64,).into() }]);
let mut hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
(Parent, Teyrchain(1)),
message,
&mut hash,
Weight::from_parts(50, 50),
Weight::zero(),
);
assert_eq!(r, Outcome::Complete { used: Weight::from_parts(10, 10) });
}
#[test]
fn auto_fee_paying_should_work() {
// Account #3 and Teyrchain #1 can execute for free
AllowUnpaidFrom::set(vec![(3u64,).into()]);
// Account #3 owns 1000 native parent tokens.
add_asset((3u64,), (Parent, 1000u128));
// Sending a message costs 10 parent-tokens.
set_send_price((Parent, 10u128));
// They want to lock 100 of the native parent tokens to be unlocked only by Teyrchain #1.
let message = Xcm(vec![
SetFeesMode { jit_withdraw: true },
LockAsset { asset: (Parent, 100u128).into(), unlocker: (Parent, Teyrchain(1)).into() },
]);
let mut hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
(3u64,),
message,
&mut hash,
Weight::from_parts(50, 50),
Weight::zero(),
);
assert_eq!(r, Outcome::Complete { used: Weight::from_parts(20, 20) });
assert_eq!(asset_list((3u64,)), vec![(Parent, 990u128).into()]);
}
#[test]
fn lock_should_fail_correctly() {
// Account #3 can execute for free
AllowUnpaidFrom::set(vec![(3u64,).into(), (Parent, Teyrchain(1)).into()]);
// #3 wants to lock 100 of the native parent tokens to be unlocked only by teyrchain ../#1,
// but they don't have any.
let message = Xcm(vec![LockAsset {
asset: (Parent, 100u128).into(),
unlocker: (Parent, Teyrchain(1)).into(),
}]);
let mut hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
(3u64,),
message,
&mut hash,
Weight::from_parts(50, 50),
Weight::zero(),
);
assert_eq!(
r,
Outcome::Incomplete {
used: Weight::from_parts(10, 10),
error: InstructionError { index: 0, error: XcmError::LockError },
}
);
assert_eq!(sent_xcm(), vec![]);
assert_eq!(take_lock_trace(), vec![]);
// Account #3 owns 1000 native parent tokens.
add_asset((3u64,), (Parent, 1000u128));
// But we require a price to be paid for the sending
set_send_price((Parent, 10u128));
// #3 wants to lock 100 of the native parent tokens to be unlocked only by teyrchain ../#1,
// but there's nothing to pay the fees for sending the notification message.
let message = Xcm(vec![LockAsset {
asset: (Parent, 100u128).into(),
unlocker: (Parent, Teyrchain(1)).into(),
}]);
let mut hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
(3u64,),
message,
&mut hash,
Weight::from_parts(50, 50),
Weight::zero(),
);
assert_eq!(
r,
Outcome::Incomplete {
used: Weight::from_parts(10, 10),
error: InstructionError { index: 0, error: XcmError::NotHoldingFees },
}
);
assert_eq!(sent_xcm(), vec![]);
assert_eq!(take_lock_trace(), vec![]);
}
#[test]
fn remote_unlock_roundtrip_should_work() {
// Account #3 can execute for free
AllowUnpaidFrom::set(vec![(3u64,).into(), (Parent, Teyrchain(1)).into()]);
// Account #3 owns 1000 native parent tokens.
add_asset((3u64,), (Parent, 1000u128));
// Sending a message costs 10 parent-tokens.
set_send_price((Parent, 10u128));
// We have been told by Teyrchain #1 that Account #3 has locked funds which we can unlock.
// Previously, we must have sent a LockAsset instruction to Teyrchain #1.
// This caused Teyrchain #1 to send us the NoteUnlockable instruction.
let message =
Xcm(vec![NoteUnlockable { asset: (Parent, 100u128).into(), owner: (3u64,).into() }]);
let mut hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
(Parent, Teyrchain(1)),
message,
&mut hash,
Weight::from_parts(50, 50),
Weight::zero(),
);
assert_eq!(r, Outcome::Complete { used: Weight::from_parts(10, 10) });
assert_eq!(
take_lock_trace(),
vec![Note {
asset: (Parent, 100u128).into(),
owner: (3u64,).into(),
locker: (Parent, Teyrchain(1)).into(),
}]
);
// Let's request those funds be unlocked.
let message = Xcm(vec![
WithdrawAsset((Parent, 100u128).into()),
SetAppendix(
vec![DepositAsset { assets: AllCounted(2).into(), beneficiary: (3u64,).into() }].into(),
),
RequestUnlock { asset: (Parent, 100u128).into(), locker: (Parent, Teyrchain(1)).into() },
]);
let mut hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
(3u64,),
message,
&mut hash,
Weight::from_parts(50, 50),
Weight::zero(),
);
assert_eq!(r, Outcome::Complete { used: Weight::from_parts(40, 40) });
assert_eq!(asset_list((3u64,)), vec![(Parent, 990u128).into()]);
let expected_msg = Xcm::<()>(vec![UnlockAsset {
target: (Parent, Teyrchain(42), 3u64).into(),
asset: (Parent, 100u128).into(),
}]);
let expected_hash = fake_message_hash(&expected_msg);
assert_eq!(sent_xcm(), vec![((Parent, Teyrchain(1)).into(), expected_msg, expected_hash)]);
assert_eq!(
take_lock_trace(),
vec![Reduce {
asset: (Parent, 100u128).into(),
owner: (3u64,).into(),
locker: (Parent, Teyrchain(1)).into(),
}]
);
}
#[test]
fn remote_unlock_should_fail_correctly() {
// Account #3 can execute for free
AllowUnpaidFrom::set(vec![(3u64,).into(), (Parent, Teyrchain(1)).into()]);
// But we require a price to be paid for the sending
set_send_price((Parent, 10u128));
// We want to unlock 100 of the native parent tokens which were locked for us on teyrchain.
// This won't work as we don't have any record of them being locked for us.
// No message will be sent and no lock records changed.
let message = Xcm(vec![RequestUnlock {
asset: (Parent, 100u128).into(),
locker: (Parent, Teyrchain(1)).into(),
}]);
let mut hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
(3u64,),
message,
&mut hash,
Weight::from_parts(50, 50),
Weight::zero(),
);
assert_eq!(
r,
Outcome::Incomplete {
used: Weight::from_parts(10, 10),
error: InstructionError { index: 0, error: XcmError::LockError },
}
);
assert_eq!(sent_xcm(), vec![]);
assert_eq!(take_lock_trace(), vec![]);
// We have been told by Teyrchain #1 that Account #3 has locked funds which we can unlock.
let message =
Xcm(vec![NoteUnlockable { asset: (Parent, 100u128).into(), owner: (3u64,).into() }]);
let mut hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
(Parent, Teyrchain(1)),
message,
&mut hash,
Weight::from_parts(50, 50),
Weight::zero(),
);
assert_eq!(r, Outcome::Complete { used: Weight::from_parts(10, 10) });
let _discard = take_lock_trace();
// We want to unlock 100 of the native parent tokens which were locked for us on teyrchain.
// This won't work now as we don't have the funds to send the onward message.
// No message will be sent and no lock records changed.
let message = Xcm(vec![RequestUnlock {
asset: (Parent, 100u128).into(),
locker: (Parent, Teyrchain(1)).into(),
}]);
let mut hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
(3u64,),
message,
&mut hash,
Weight::from_parts(50, 50),
Weight::zero(),
);
assert_eq!(
r,
Outcome::Incomplete {
used: Weight::from_parts(10, 10),
error: InstructionError { index: 0, error: XcmError::NotHoldingFees },
}
);
assert_eq!(sent_xcm(), vec![]);
assert_eq!(take_lock_trace(), vec![]);
}
+782
View File
@@ -0,0 +1,782 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
//! Mock implementations to test XCM builder configuration types.
use crate::{
barriers::{AllowSubscriptionsFrom, RespectSuspension, TrailingSetTopicAsId},
test_utils::*,
EnsureDecodableXcm,
};
pub use crate::{
AliasChildLocation, AliasForeignAccountId32, AllowExplicitUnpaidExecutionFrom,
AllowKnownQueryResponses, AllowTopLevelPaidExecutionFrom, AllowUnpaidExecutionFrom,
FixedRateOfFungible, FixedWeightBounds, TakeWeightCredit,
};
pub use alloc::collections::{btree_map::BTreeMap, btree_set::BTreeSet};
pub use codec::{Decode, DecodeWithMemTracking, Encode};
pub use core::{
cell::{Cell, RefCell},
fmt::Debug,
ops::ControlFlow,
};
use frame_support::traits::{ContainsPair, Everything};
pub use frame_support::{
dispatch::{DispatchInfo, DispatchResultWithPostInfo, GetDispatchInfo, PostDispatchInfo},
ensure, parameter_types,
sp_runtime::{traits::Dispatchable, DispatchError, DispatchErrorWithPostInfo},
traits::{Contains, Get, IsInVec},
};
pub use xcm::latest::{prelude::*, QueryId, Weight};
pub use xcm_executor::{
traits::{
AssetExchange, AssetLock, CheckSuspension, ConvertOrigin, DenyExecution, Enact, ExportXcm,
FeeManager, FeeReason, LockError, OnResponse, Properties, QueryHandler,
QueryResponseStatus, TransactAsset,
},
AssetsInHolding, Config,
};
pub use xcm_simulator::helpers::derive_topic_id;
#[derive(Debug)]
pub enum TestOrigin {
Root,
Relay,
Signed(u64),
Teyrchain(u32),
}
/// A dummy call.
///
/// Each item contains the amount of weight that it *wants* to consume as the first item, and the
/// actual amount (if different from the former) in the second option.
#[derive(
Debug, Encode, Decode, DecodeWithMemTracking, Eq, PartialEq, Clone, Copy, scale_info::TypeInfo,
)]
pub enum TestCall {
OnlyRoot(Weight, Option<Weight>),
OnlyTeyrchain(Weight, Option<Weight>, Option<u32>),
OnlySigned(Weight, Option<Weight>, Option<u64>),
Any(Weight, Option<Weight>),
}
impl Dispatchable for TestCall {
type RuntimeOrigin = TestOrigin;
type Config = ();
type Info = ();
type PostInfo = PostDispatchInfo;
fn dispatch(self, origin: Self::RuntimeOrigin) -> DispatchResultWithPostInfo {
let mut post_info = PostDispatchInfo::default();
let maybe_actual = match self {
TestCall::OnlyRoot(_, maybe_actual) |
TestCall::OnlySigned(_, maybe_actual, _) |
TestCall::OnlyTeyrchain(_, maybe_actual, _) |
TestCall::Any(_, maybe_actual) => maybe_actual,
};
post_info.actual_weight = maybe_actual;
if match (&origin, &self) {
(TestOrigin::Teyrchain(i), TestCall::OnlyTeyrchain(_, _, Some(j))) => i == j,
(TestOrigin::Signed(i), TestCall::OnlySigned(_, _, Some(j))) => i == j,
(TestOrigin::Root, TestCall::OnlyRoot(..)) |
(TestOrigin::Teyrchain(_), TestCall::OnlyTeyrchain(_, _, None)) |
(TestOrigin::Signed(_), TestCall::OnlySigned(_, _, None)) |
(_, TestCall::Any(..)) => true,
_ => false,
} {
Ok(post_info)
} else {
Err(DispatchErrorWithPostInfo { error: DispatchError::BadOrigin, post_info })
}
}
}
impl GetDispatchInfo for TestCall {
fn get_dispatch_info(&self) -> DispatchInfo {
let call_weight = *match self {
TestCall::OnlyRoot(estimate, ..) |
TestCall::OnlyTeyrchain(estimate, ..) |
TestCall::OnlySigned(estimate, ..) |
TestCall::Any(estimate, ..) => estimate,
};
DispatchInfo { call_weight, ..Default::default() }
}
}
thread_local! {
pub static SENT_XCM: RefCell<Vec<(Location, Xcm<()>, XcmHash)>> = RefCell::new(Vec::new());
pub static EXPORTED_XCM: RefCell<
Vec<(NetworkId, u32, InteriorLocation, InteriorLocation, Xcm<()>, XcmHash)>
> = RefCell::new(Vec::new());
pub static EXPORTER_OVERRIDE: RefCell<Option<(
fn(
NetworkId,
u32,
&InteriorLocation,
&InteriorLocation,
&Xcm<()>,
) -> Result<Assets, SendError>,
fn(
NetworkId,
u32,
InteriorLocation,
InteriorLocation,
Xcm<()>,
) -> Result<XcmHash, SendError>,
)>> = RefCell::new(None);
pub static SEND_PRICE: RefCell<Assets> = RefCell::new(Assets::new());
pub static SUSPENDED: Cell<bool> = Cell::new(false);
}
pub fn sent_xcm() -> Vec<(Location, opaque::Xcm, XcmHash)> {
SENT_XCM.with(|q| (*q.borrow()).clone())
}
pub fn set_send_price(p: impl Into<Asset>) {
SEND_PRICE.with(|l| l.replace(p.into().into()));
}
pub fn exported_xcm(
) -> Vec<(NetworkId, u32, InteriorLocation, InteriorLocation, opaque::Xcm, XcmHash)> {
EXPORTED_XCM.with(|q| (*q.borrow()).clone())
}
pub fn set_exporter_override(
price: fn(
NetworkId,
u32,
&InteriorLocation,
&InteriorLocation,
&Xcm<()>,
) -> Result<Assets, SendError>,
deliver: fn(
NetworkId,
u32,
InteriorLocation,
InteriorLocation,
Xcm<()>,
) -> Result<XcmHash, SendError>,
) {
EXPORTER_OVERRIDE.with(|x| x.replace(Some((price, deliver))));
}
#[allow(dead_code)]
pub fn clear_exporter_override() {
EXPORTER_OVERRIDE.with(|x| x.replace(None));
}
pub struct TestMessageSenderImpl;
impl SendXcm for TestMessageSenderImpl {
type Ticket = (Location, Xcm<()>, XcmHash);
fn validate(
dest: &mut Option<Location>,
msg: &mut Option<Xcm<()>>,
) -> SendResult<(Location, Xcm<()>, XcmHash)> {
let msg = msg.take().unwrap();
let hash = derive_topic_id(&msg);
let triplet = (dest.take().unwrap(), msg, hash);
Ok((triplet, SEND_PRICE.with(|l| l.borrow().clone())))
}
fn deliver(triplet: (Location, Xcm<()>, XcmHash)) -> Result<XcmHash, SendError> {
let hash = triplet.2;
SENT_XCM.with(|q| q.borrow_mut().push(triplet));
Ok(hash)
}
}
pub type TestMessageSender = EnsureDecodableXcm<TestMessageSenderImpl>;
pub struct TestMessageExporter;
impl ExportXcm for TestMessageExporter {
type Ticket = (NetworkId, u32, InteriorLocation, InteriorLocation, Xcm<()>, XcmHash);
fn validate(
network: NetworkId,
channel: u32,
uni_src: &mut Option<InteriorLocation>,
dest: &mut Option<InteriorLocation>,
msg: &mut Option<Xcm<()>>,
) -> SendResult<(NetworkId, u32, InteriorLocation, InteriorLocation, Xcm<()>, XcmHash)> {
let (s, d, m) = (uni_src.take().unwrap(), dest.take().unwrap(), msg.take().unwrap());
let r: Result<Assets, SendError> = EXPORTER_OVERRIDE.with(|e| {
if let Some((ref f, _)) = &*e.borrow() {
f(network, channel, &s, &d, &m)
} else {
Ok(Assets::new())
}
});
let h = derive_topic_id(&m);
match r {
Ok(price) => Ok(((network, channel, s, d, m, h), price)),
Err(e) => {
*uni_src = Some(s);
*dest = Some(d);
*msg = Some(m);
Err(e)
},
}
}
fn deliver(
tuple: (NetworkId, u32, InteriorLocation, InteriorLocation, Xcm<()>, XcmHash),
) -> Result<XcmHash, SendError> {
EXPORTER_OVERRIDE.with(|e| {
if let Some((_, ref f)) = &*e.borrow() {
let (network, channel, uni_src, dest, msg, _hash) = tuple;
f(network, channel, uni_src, dest, msg)
} else {
let hash = tuple.5;
EXPORTED_XCM.with(|q| q.borrow_mut().push(tuple));
Ok(hash)
}
})
}
}
thread_local! {
pub static ASSETS: RefCell<BTreeMap<Location, AssetsInHolding>> = RefCell::new(BTreeMap::new());
}
pub fn assets(who: impl Into<Location>) -> AssetsInHolding {
ASSETS.with(|a| a.borrow().get(&who.into()).cloned()).unwrap_or_default()
}
pub fn asset_list(who: impl Into<Location>) -> Vec<Asset> {
Assets::from(assets(who)).into_inner()
}
pub fn add_asset(who: impl Into<Location>, what: impl Into<Asset>) {
ASSETS.with(|a| {
a.borrow_mut()
.entry(who.into())
.or_insert(AssetsInHolding::new())
.subsume(what.into())
});
}
pub fn clear_assets(who: impl Into<Location>) {
ASSETS.with(|a| a.borrow_mut().remove(&who.into()));
}
pub struct TestAssetTransactor;
impl TransactAsset for TestAssetTransactor {
fn deposit_asset(
what: &Asset,
who: &Location,
_context: Option<&XcmContext>,
) -> Result<(), XcmError> {
add_asset(who.clone(), what.clone());
Ok(())
}
fn withdraw_asset(
what: &Asset,
who: &Location,
_maybe_context: Option<&XcmContext>,
) -> Result<AssetsInHolding, XcmError> {
ASSETS.with(|a| {
a.borrow_mut()
.get_mut(who)
.ok_or(XcmError::NotWithdrawable)?
.try_take(what.clone().into())
.map_err(|_| XcmError::NotWithdrawable)
})
}
}
pub fn to_account(l: impl Into<Location>) -> Result<u64, Location> {
let l = l.into();
Ok(match l.unpack() {
// Siblings at 2000+id
(1, [Teyrchain(id)]) => 2000 + *id as u64,
// Accounts are their number
(0, [AccountIndex64 { index, .. }]) => *index,
// Children at 1000+id
(0, [Teyrchain(id)]) => 1000 + *id as u64,
// Self at 3000
(0, []) => 3000,
// Parent at 3001
(1, []) => 3001,
_ => {
// Is it a foreign-consensus?
let uni = ExecutorUniversalLocation::get();
if l.parents as usize != uni.len() {
return Err(l);
}
match l.first_interior() {
Some(GlobalConsensus(Kusama)) => 4000,
Some(GlobalConsensus(Pezkuwi)) => 4001,
_ => return Err(l),
}
},
})
}
pub struct TestOriginConverter;
impl ConvertOrigin<TestOrigin> for TestOriginConverter {
fn convert_origin(
origin: impl Into<Location>,
kind: OriginKind,
) -> Result<TestOrigin, Location> {
use OriginKind::*;
let origin = origin.into();
match (kind, origin.unpack()) {
(Superuser, _) => Ok(TestOrigin::Root),
(SovereignAccount, _) => Ok(TestOrigin::Signed(to_account(origin)?)),
(Native, (0, [Teyrchain(id)])) => Ok(TestOrigin::Teyrchain(*id)),
(Native, (1, [])) => Ok(TestOrigin::Relay),
(Native, (0, [AccountIndex64 { index, .. }])) => Ok(TestOrigin::Signed(*index)),
_ => Err(origin),
}
}
}
thread_local! {
pub static IS_RESERVE: RefCell<BTreeMap<Location, Vec<AssetFilter>>> = RefCell::new(BTreeMap::new());
pub static IS_TELEPORTER: RefCell<BTreeMap<Location, Vec<AssetFilter>>> = RefCell::new(BTreeMap::new());
pub static UNIVERSAL_ALIASES: RefCell<BTreeSet<(Location, Junction)>> = RefCell::new(BTreeSet::new());
}
pub fn add_reserve(from: Location, asset: AssetFilter) {
IS_RESERVE.with(|r| r.borrow_mut().entry(from).or_default().push(asset));
}
#[allow(dead_code)]
pub fn add_teleporter(from: Location, asset: AssetFilter) {
IS_TELEPORTER.with(|r| r.borrow_mut().entry(from).or_default().push(asset));
}
pub fn add_universal_alias(bridge: impl Into<Location>, consensus: impl Into<Junction>) {
UNIVERSAL_ALIASES.with(|r| r.borrow_mut().insert((bridge.into(), consensus.into())));
}
pub fn clear_universal_aliases() {
UNIVERSAL_ALIASES.with(|r| r.replace(Default::default()));
}
pub struct TestIsReserve;
impl ContainsPair<Asset, Location> for TestIsReserve {
fn contains(asset: &Asset, origin: &Location) -> bool {
IS_RESERVE
.with(|r| r.borrow().get(origin).map_or(false, |v| v.iter().any(|a| a.matches(asset))))
}
}
pub struct TestIsTeleporter;
impl ContainsPair<Asset, Location> for TestIsTeleporter {
fn contains(asset: &Asset, origin: &Location) -> bool {
IS_TELEPORTER
.with(|r| r.borrow().get(origin).map_or(false, |v| v.iter().any(|a| a.matches(asset))))
}
}
pub struct TestUniversalAliases;
impl Contains<(Location, Junction)> for TestUniversalAliases {
fn contains(t: &(Location, Junction)) -> bool {
UNIVERSAL_ALIASES.with(|r| r.borrow().contains(t))
}
}
pub enum ResponseSlot {
Expecting(Location),
Received(Response),
}
thread_local! {
pub static QUERIES: RefCell<BTreeMap<u64, ResponseSlot>> = RefCell::new(BTreeMap::new());
}
pub struct TestResponseHandler;
impl OnResponse for TestResponseHandler {
fn expecting_response(origin: &Location, query_id: u64, _querier: Option<&Location>) -> bool {
QUERIES.with(|q| match q.borrow().get(&query_id) {
Some(ResponseSlot::Expecting(ref l)) => l == origin,
_ => false,
})
}
fn on_response(
_origin: &Location,
query_id: u64,
_querier: Option<&Location>,
response: xcm::latest::Response,
_max_weight: Weight,
_context: &XcmContext,
) -> Weight {
QUERIES.with(|q| {
q.borrow_mut().entry(query_id).and_modify(|v| {
if matches!(*v, ResponseSlot::Expecting(..)) {
*v = ResponseSlot::Received(response);
}
});
});
Weight::from_parts(10, 10)
}
}
pub fn expect_response(query_id: u64, from: Location) {
QUERIES.with(|q| q.borrow_mut().insert(query_id, ResponseSlot::Expecting(from)));
}
pub fn response(query_id: u64) -> Option<Response> {
QUERIES.with(|q| {
q.borrow().get(&query_id).and_then(|v| match v {
ResponseSlot::Received(r) => Some(r.clone()),
_ => None,
})
})
}
/// Mock implementation of the [`QueryHandler`] trait for creating XCM success queries and expecting
/// responses.
pub struct TestQueryHandler<T, BlockNumber>(core::marker::PhantomData<(T, BlockNumber)>);
impl<T: Config, BlockNumber: sp_runtime::traits::Zero + Encode> QueryHandler
for TestQueryHandler<T, BlockNumber>
{
type BlockNumber = BlockNumber;
type Error = XcmError;
type UniversalLocation = T::UniversalLocation;
fn new_query(
responder: impl Into<Location>,
_timeout: Self::BlockNumber,
_match_querier: impl Into<Location>,
) -> QueryId {
let query_id = 1;
expect_response(query_id, responder.into());
query_id
}
fn report_outcome(
message: &mut Xcm<()>,
responder: impl Into<Location>,
timeout: Self::BlockNumber,
) -> Result<QueryId, Self::Error> {
let responder = responder.into();
let destination = Self::UniversalLocation::get()
.invert_target(&responder)
.map_err(|()| XcmError::LocationNotInvertible)?;
let query_id = Self::new_query(responder, timeout, Here);
let response_info = QueryResponseInfo { destination, query_id, max_weight: Weight::zero() };
let report_error = Xcm(vec![ReportError(response_info)]);
message.0.insert(0, SetAppendix(report_error));
Ok(query_id)
}
fn take_response(query_id: QueryId) -> QueryResponseStatus<Self::BlockNumber> {
QUERIES
.with(|q| {
q.borrow().get(&query_id).and_then(|v| match v {
ResponseSlot::Received(r) => Some(QueryResponseStatus::Ready {
response: r.clone(),
at: Self::BlockNumber::zero(),
}),
_ => Some(QueryResponseStatus::NotFound),
})
})
.unwrap_or(QueryResponseStatus::NotFound)
}
#[cfg(feature = "runtime-benchmarks")]
fn expect_response(_id: QueryId, _response: xcm::latest::Response) {
// Unnecessary since it's only a test implementation
}
}
parameter_types! {
pub static ExecutorUniversalLocation: InteriorLocation
= (ByGenesis([0; 32]), Teyrchain(42)).into();
pub UnitWeightCost: Weight = Weight::from_parts(10, 10);
}
parameter_types! {
// Nothing is allowed to be paid/unpaid by default.
pub static AllowExplicitUnpaidFrom: Vec<Location> = vec![];
pub static AllowUnpaidFrom: Vec<Location> = vec![];
pub static AllowPaidFrom: Vec<Location> = vec![];
pub static AllowSubsFrom: Vec<Location> = vec![];
// 1_000_000_000_000 => 1 unit of asset for 1 unit of ref time weight.
// 1024 * 1024 => 1 unit of asset for 1 unit of proof size weight.
pub static WeightPrice: (AssetId, u128, u128) =
(From::from(Here), 1_000_000_000_000, 1024 * 1024);
pub static MaxInstructions: u32 = 100;
}
pub struct TestSuspender;
impl CheckSuspension for TestSuspender {
fn is_suspended<Call>(
_origin: &Location,
_instructions: &mut [Instruction<Call>],
_max_weight: Weight,
_properties: &mut Properties,
) -> bool {
SUSPENDED.with(|s| s.get())
}
}
impl TestSuspender {
pub fn set_suspended(suspended: bool) {
SUSPENDED.with(|s| s.set(suspended));
}
}
pub type TestBarrier = (
TakeWeightCredit,
AllowKnownQueryResponses<TestResponseHandler>,
AllowTopLevelPaidExecutionFrom<IsInVec<AllowPaidFrom>>,
AllowExplicitUnpaidExecutionFrom<IsInVec<AllowExplicitUnpaidFrom>>,
AllowUnpaidExecutionFrom<IsInVec<AllowUnpaidFrom>>,
AllowSubscriptionsFrom<IsInVec<AllowSubsFrom>>,
);
thread_local! {
pub static IS_WAIVED: RefCell<Vec<FeeReason>> = RefCell::new(vec![]);
}
#[allow(dead_code)]
pub fn set_fee_waiver(waived: Vec<FeeReason>) {
IS_WAIVED.with(|l| l.replace(waived));
}
pub struct TestFeeManager;
impl FeeManager for TestFeeManager {
fn is_waived(_: Option<&Location>, r: FeeReason) -> bool {
IS_WAIVED.with(|l| l.borrow().contains(&r))
}
fn handle_fee(_: Assets, _: Option<&XcmContext>, _: FeeReason) {}
}
#[derive(Clone, Eq, PartialEq, Debug)]
pub enum LockTraceItem {
Lock { unlocker: Location, asset: Asset, owner: Location },
Unlock { unlocker: Location, asset: Asset, owner: Location },
Note { locker: Location, asset: Asset, owner: Location },
Reduce { locker: Location, asset: Asset, owner: Location },
}
thread_local! {
pub static NEXT_INDEX: RefCell<u32> = RefCell::new(0);
pub static LOCK_TRACE: RefCell<Vec<LockTraceItem>> = RefCell::new(Vec::new());
pub static ALLOWED_UNLOCKS: RefCell<BTreeMap<(Location, Location), AssetsInHolding>> = RefCell::new(BTreeMap::new());
pub static ALLOWED_REQUEST_UNLOCKS: RefCell<BTreeMap<(Location, Location), AssetsInHolding>> = RefCell::new(BTreeMap::new());
}
pub fn take_lock_trace() -> Vec<LockTraceItem> {
LOCK_TRACE.with(|l| l.replace(Vec::new()))
}
pub fn allow_unlock(
unlocker: impl Into<Location>,
asset: impl Into<Asset>,
owner: impl Into<Location>,
) {
ALLOWED_UNLOCKS.with(|l| {
l.borrow_mut()
.entry((owner.into(), unlocker.into()))
.or_default()
.subsume(asset.into())
});
}
pub fn disallow_unlock(
unlocker: impl Into<Location>,
asset: impl Into<Asset>,
owner: impl Into<Location>,
) {
ALLOWED_UNLOCKS.with(|l| {
l.borrow_mut()
.entry((owner.into(), unlocker.into()))
.or_default()
.saturating_take(asset.into().into())
});
}
pub fn unlock_allowed(unlocker: &Location, asset: &Asset, owner: &Location) -> bool {
ALLOWED_UNLOCKS.with(|l| {
l.borrow_mut()
.get(&(owner.clone(), unlocker.clone()))
.map_or(false, |x| x.contains_asset(asset))
})
}
pub fn allow_request_unlock(
locker: impl Into<Location>,
asset: impl Into<Asset>,
owner: impl Into<Location>,
) {
ALLOWED_REQUEST_UNLOCKS.with(|l| {
l.borrow_mut()
.entry((owner.into(), locker.into()))
.or_default()
.subsume(asset.into())
});
}
pub fn disallow_request_unlock(
locker: impl Into<Location>,
asset: impl Into<Asset>,
owner: impl Into<Location>,
) {
ALLOWED_REQUEST_UNLOCKS.with(|l| {
l.borrow_mut()
.entry((owner.into(), locker.into()))
.or_default()
.saturating_take(asset.into().into())
});
}
pub fn request_unlock_allowed(locker: &Location, asset: &Asset, owner: &Location) -> bool {
ALLOWED_REQUEST_UNLOCKS.with(|l| {
l.borrow_mut()
.get(&(owner.clone(), locker.clone()))
.map_or(false, |x| x.contains_asset(asset))
})
}
pub struct TestTicket(LockTraceItem);
impl Enact for TestTicket {
fn enact(self) -> Result<(), LockError> {
match &self.0 {
LockTraceItem::Lock { unlocker, asset, owner } =>
allow_unlock(unlocker.clone(), asset.clone(), owner.clone()),
LockTraceItem::Unlock { unlocker, asset, owner } =>
disallow_unlock(unlocker.clone(), asset.clone(), owner.clone()),
LockTraceItem::Reduce { locker, asset, owner } =>
disallow_request_unlock(locker.clone(), asset.clone(), owner.clone()),
_ => {},
}
LOCK_TRACE.with(move |l| l.borrow_mut().push(self.0));
Ok(())
}
}
pub struct TestAssetLock;
impl AssetLock for TestAssetLock {
type LockTicket = TestTicket;
type UnlockTicket = TestTicket;
type ReduceTicket = TestTicket;
fn prepare_lock(
unlocker: Location,
asset: Asset,
owner: Location,
) -> Result<Self::LockTicket, LockError> {
ensure!(assets(owner.clone()).contains_asset(&asset), LockError::AssetNotOwned);
Ok(TestTicket(LockTraceItem::Lock { unlocker, asset, owner }))
}
fn prepare_unlock(
unlocker: Location,
asset: Asset,
owner: Location,
) -> Result<Self::UnlockTicket, LockError> {
ensure!(unlock_allowed(&unlocker, &asset, &owner), LockError::NotLocked);
Ok(TestTicket(LockTraceItem::Unlock { unlocker, asset, owner }))
}
fn note_unlockable(locker: Location, asset: Asset, owner: Location) -> Result<(), LockError> {
allow_request_unlock(locker.clone(), asset.clone(), owner.clone());
let item = LockTraceItem::Note { locker, asset, owner };
LOCK_TRACE.with(move |l| l.borrow_mut().push(item));
Ok(())
}
fn prepare_reduce_unlockable(
locker: Location,
asset: Asset,
owner: Location,
) -> Result<Self::ReduceTicket, xcm_executor::traits::LockError> {
ensure!(request_unlock_allowed(&locker, &asset, &owner), LockError::NotLocked);
Ok(TestTicket(LockTraceItem::Reduce { locker, asset, owner }))
}
}
thread_local! {
pub static EXCHANGE_ASSETS: RefCell<AssetsInHolding> = RefCell::new(AssetsInHolding::new());
}
pub fn set_exchange_assets(assets: impl Into<Assets>) {
EXCHANGE_ASSETS.with(|a| a.replace(assets.into().into()));
}
pub fn exchange_assets() -> Assets {
EXCHANGE_ASSETS.with(|a| a.borrow().clone().into())
}
pub struct TestAssetExchange;
impl AssetExchange for TestAssetExchange {
fn exchange_asset(
_origin: Option<&Location>,
give: AssetsInHolding,
want: &Assets,
maximal: bool,
) -> Result<AssetsInHolding, AssetsInHolding> {
let mut have = EXCHANGE_ASSETS.with(|l| l.borrow().clone());
ensure!(have.contains_assets(want), give);
let get = if maximal {
std::mem::replace(&mut have, AssetsInHolding::new())
} else {
have.saturating_take(want.clone().into())
};
have.subsume_assets(give);
EXCHANGE_ASSETS.with(|l| l.replace(have));
Ok(get)
}
fn quote_exchange_price(give: &Assets, want: &Assets, maximal: bool) -> Option<Assets> {
let mut have = EXCHANGE_ASSETS.with(|l| l.borrow().clone());
if !have.contains_assets(want) {
return None;
}
let get = if maximal {
have.saturating_take(give.clone().into())
} else {
have.saturating_take(want.clone().into())
};
let result: Vec<Asset> = get.fungible_assets_iter().collect();
Some(result.into())
}
}
pub struct SiblingPrefix;
impl Contains<Location> for SiblingPrefix {
fn contains(loc: &Location) -> bool {
matches!(loc.unpack(), (1, [Teyrchain(_)]))
}
}
pub struct ChildPrefix;
impl Contains<Location> for ChildPrefix {
fn contains(loc: &Location) -> bool {
matches!(loc.unpack(), (0, [Teyrchain(_)]))
}
}
pub struct ParentPrefix;
impl Contains<Location> for ParentPrefix {
fn contains(loc: &Location) -> bool {
matches!(loc.unpack(), (1, []))
}
}
/// Pairs (location1, location2) where location1 can alias as location2.
pub type Aliasers = (AliasForeignAccountId32<SiblingPrefix>, AliasChildLocation);
pub struct TestConfig;
impl Config for TestConfig {
type RuntimeCall = TestCall;
type XcmSender = TestMessageSender;
type XcmEventEmitter = ();
type AssetTransactor = TestAssetTransactor;
type OriginConverter = TestOriginConverter;
type IsReserve = TestIsReserve;
type IsTeleporter = TestIsTeleporter;
type UniversalLocation = ExecutorUniversalLocation;
type Barrier = TrailingSetTopicAsId<RespectSuspension<TestBarrier, TestSuspender>>;
type Weigher = FixedWeightBounds<UnitWeightCost, TestCall, MaxInstructions>;
type Trader = FixedRateOfFungible<WeightPrice, ()>;
type ResponseHandler = TestResponseHandler;
type AssetTrap = TestAssetTrap;
type AssetLocker = TestAssetLock;
type AssetExchanger = TestAssetExchange;
type AssetClaims = TestAssetTrap;
type SubscriptionService = TestSubscriptionService;
type PalletInstancesInfo = TestPalletsInfo;
type MaxAssetsIntoHolding = MaxAssetsIntoHolding;
type FeeManager = TestFeeManager;
type UniversalAliases = TestUniversalAliases;
type MessageExporter = TestMessageExporter;
type CallDispatcher = TestCall;
type SafeCallFilter = Everything;
type Aliasers = Aliasers;
type TransactionalProcessor = ();
type HrmpNewChannelOpenRequestHandler = ();
type HrmpChannelAcceptedHandler = ();
type HrmpChannelClosingHandler = ();
type XcmRecorder = ();
}
pub fn fungible_multi_asset(location: Location, amount: u128) -> Asset {
(AssetId::from(location), Fungibility::Fungible(amount)).into()
}
pub fn fake_message_hash<T>(message: &Xcm<T>) -> XcmHash {
message.using_encoded(sp_io::hashing::blake2_256)
}
+42
View File
@@ -0,0 +1,42 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
use super::{test_utils::*, *};
use alloc::{vec, vec::Vec};
use frame_support::{
assert_err,
traits::{ConstU32, ContainsPair, ProcessMessageError},
weights::constants::{WEIGHT_PROOF_SIZE_PER_MB, WEIGHT_REF_TIME_PER_SECOND},
};
use xcm_executor::{traits::prelude::*, Config, XcmExecutor};
mod mock;
use mock::*;
mod aliases;
mod assets;
mod barriers;
mod basic;
mod bridging;
mod expecting;
mod locking;
mod origins;
mod pay;
mod querying;
mod routing;
mod transacting;
mod version_subscriptions;
mod weight;
@@ -0,0 +1,171 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
use super::*;
#[test]
fn universal_origin_should_work() {
AllowUnpaidFrom::set(vec![[Teyrchain(1)].into(), [Teyrchain(2)].into()]);
clear_universal_aliases();
// Teyrchain 1 may represent Kusama to us
add_universal_alias(Teyrchain(1), Kusama);
// Teyrchain 2 may represent Pezkuwi to us
add_universal_alias(Teyrchain(2), Pezkuwi);
let message = Xcm(vec![
UniversalOrigin(GlobalConsensus(Kusama)),
TransferAsset { assets: (Parent, 100u128).into(), beneficiary: Here.into() },
]);
let mut hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
Teyrchain(2),
message,
&mut hash,
Weight::from_parts(50, 50),
Weight::zero(),
);
assert_eq!(
r,
Outcome::Incomplete {
used: Weight::from_parts(10, 10),
error: InstructionError { index: 0, error: XcmError::InvalidLocation },
}
);
let message = Xcm(vec![
UniversalOrigin(GlobalConsensus(Kusama)),
TransferAsset { assets: (Parent, 100u128).into(), beneficiary: Here.into() },
]);
let mut hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
Teyrchain(1),
message,
&mut hash,
Weight::from_parts(50, 50),
Weight::zero(),
);
assert_eq!(
r,
Outcome::Incomplete {
used: Weight::from_parts(20, 20),
error: InstructionError { index: 1, error: XcmError::NotWithdrawable },
}
);
add_asset((Ancestor(2), GlobalConsensus(Kusama)), (Parent, 100));
let message = Xcm(vec![
UniversalOrigin(GlobalConsensus(Kusama)),
TransferAsset { assets: (Parent, 100u128).into(), beneficiary: Here.into() },
]);
let mut hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
Teyrchain(1),
message,
&mut hash,
Weight::from_parts(50, 50),
Weight::zero(),
);
assert_eq!(r, Outcome::Complete { used: Weight::from_parts(20, 20) });
assert_eq!(asset_list((Ancestor(2), GlobalConsensus(Kusama))), vec![]);
}
#[test]
fn export_message_should_work() {
// Bridge chain (assumed to be Relay) lets Teyrchain #1 have message execution for free.
AllowUnpaidFrom::set(vec![[Teyrchain(1)].into()]);
// Local teyrchain #1 issues a transfer asset on Pezkuwi Relay-chain, transferring 100 Planck
// to Pezkuwi teyrchain #2.
let expected_message = Xcm(vec![TransferAsset {
assets: (Here, 100u128).into(),
beneficiary: Teyrchain(2).into(),
}]);
let expected_hash = fake_message_hash(&expected_message);
let message = Xcm(vec![ExportMessage {
network: Pezkuwi,
destination: Here,
xcm: expected_message.clone(),
}]);
let mut hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
Teyrchain(1),
message,
&mut hash,
Weight::from_parts(50, 50),
Weight::zero(),
);
assert_eq!(r, Outcome::Complete { used: Weight::from_parts(10, 10) });
let uni_src = (ByGenesis([0; 32]), Teyrchain(42), Teyrchain(1)).into();
assert_eq!(
exported_xcm(),
vec![(Pezkuwi, 403611790, uni_src, Here, expected_message, expected_hash)]
);
}
#[test]
fn unpaid_execution_should_work() {
// Bridge chain (assumed to be Relay) lets Teyrchain #1 have message execution for free.
AllowUnpaidFrom::set(vec![[Teyrchain(1)].into()]);
// Bridge chain (assumed to be Relay) lets Teyrchain #2 have message execution for free if it
// asks.
AllowExplicitUnpaidFrom::set(vec![[Teyrchain(2)].into()]);
// Asking for unpaid execution of up to 9 weight on the assumption it is origin of #2.
let message = Xcm(vec![UnpaidExecution {
weight_limit: Limited(Weight::from_parts(9, 9)),
check_origin: Some(Teyrchain(2).into()),
}]);
let mut hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
Teyrchain(1),
message.clone(),
&mut hash,
Weight::from_parts(50, 50),
Weight::zero(),
);
assert_eq!(
r,
Outcome::Incomplete {
used: Weight::from_parts(10, 10),
error: InstructionError { index: 0, error: XcmError::BadOrigin },
}
);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
Teyrchain(2),
message.clone(),
&mut hash,
Weight::from_parts(50, 50),
Weight::zero(),
);
assert_eq!(
r,
Outcome::Incomplete {
used: Weight::from_parts(10, 10),
error: InstructionError { index: 0, error: XcmError::Barrier },
}
);
let message = Xcm(vec![UnpaidExecution {
weight_limit: Limited(Weight::from_parts(10, 10)),
check_origin: Some(Teyrchain(2).into()),
}]);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
Teyrchain(2),
message.clone(),
&mut hash,
Weight::from_parts(50, 50),
Weight::zero(),
);
assert_eq!(r, Outcome::Complete { used: Weight::from_parts(10, 10) });
}
@@ -0,0 +1,350 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
use super::*;
use frame_support::{
construct_runtime, derive_impl, parameter_types,
traits::{AsEnsureOriginWithArg, ConstU32, Disabled, Everything, Nothing},
};
use frame_system::{EnsureRoot, EnsureSigned};
use pezkuwi_primitives::{AccountIndex, BlakeTwo256, Signature};
use sp_runtime::{generic, traits::MaybeEquivalence, AccountId32, BuildStorage};
use xcm_executor::{traits::ConvertLocation, XcmExecutor};
use xcm_simulator::ParaId;
pub type TxExtension = (
frame_system::AuthorizeCall<Test>,
frame_system::CheckNonZeroSender<Test>,
frame_system::CheckSpecVersion<Test>,
frame_system::CheckTxVersion<Test>,
frame_system::CheckGenesis<Test>,
frame_system::CheckMortality<Test>,
frame_system::CheckNonce<Test>,
frame_system::CheckWeight<Test>,
frame_system::WeightReclaim<Test>,
);
pub type Address = sp_runtime::MultiAddress<AccountId, AccountIndex>;
pub type UncheckedExtrinsic =
generic::UncheckedExtrinsic<Address, RuntimeCall, Signature, TxExtension>;
pub type Header = generic::Header<BlockNumber, BlakeTwo256>;
pub type Block = generic::Block<Header, UncheckedExtrinsic>;
pub type BlockNumber = u32;
pub type AccountId = AccountId32;
construct_runtime!(
pub enum Test {
System: frame_system,
Balances: pallet_balances,
Assets: pallet_assets,
Salary: pallet_salary,
XcmPallet: pallet_xcm,
}
);
#[derive_impl(frame_system::config_preludes::TestDefaultConfig)]
impl frame_system::Config for Test {
type Block = Block;
type AccountData = pallet_balances::AccountData<Balance>;
type AccountId = AccountId;
type Lookup = sp_runtime::traits::IdentityLookup<AccountId>;
}
pub type Balance = u128;
parameter_types! {
pub const ExistentialDeposit: Balance = 1;
}
impl pallet_balances::Config for Test {
type MaxLocks = ConstU32<0>;
type Balance = Balance;
type RuntimeEvent = RuntimeEvent;
type DustRemoval = ();
type ExistentialDeposit = ExistentialDeposit;
type AccountStore = System;
type WeightInfo = ();
type MaxReserves = ();
type ReserveIdentifier = [u8; 8];
type RuntimeHoldReason = RuntimeHoldReason;
type RuntimeFreezeReason = RuntimeFreezeReason;
type FreezeIdentifier = ();
type MaxFreezes = ConstU32<0>;
type DoneSlashHandler = ();
}
parameter_types! {
pub const AssetDeposit: u128 = 1_000_000;
pub const MetadataDepositBase: u128 = 1_000_000;
pub const MetadataDepositPerByte: u128 = 100_000;
pub const AssetAccountDeposit: u128 = 1_000_000;
pub const ApprovalDeposit: u128 = 1_000_000;
pub const AssetsStringLimit: u32 = 50;
pub const RemoveItemsLimit: u32 = 50;
}
impl pallet_assets::Config for Test {
type RuntimeEvent = RuntimeEvent;
type Balance = Balance;
type AssetId = AssetIdForAssets;
type ReserveData = ();
type Currency = Balances;
type CreateOrigin = AsEnsureOriginWithArg<EnsureSigned<AccountId>>;
type ForceOrigin = EnsureRoot<AccountId>;
type AssetDeposit = AssetDeposit;
type MetadataDepositBase = MetadataDepositBase;
type MetadataDepositPerByte = MetadataDepositPerByte;
type AssetAccountDeposit = AssetAccountDeposit;
type ApprovalDeposit = ApprovalDeposit;
type StringLimit = AssetsStringLimit;
type Holder = ();
type Freezer = ();
type Extra = ();
type WeightInfo = ();
type RemoveItemsLimit = RemoveItemsLimit;
type AssetIdParameter = AssetIdForAssets;
type CallbackHandle = ();
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = ();
}
parameter_types! {
pub const RelayLocation: Location = Location::parent();
pub const AnyNetwork: Option<NetworkId> = None;
pub MockRuntimeTeyrchainId: ParaId = 42u32.into();
pub UniversalLocation: InteriorLocation = (ByGenesis([0; 32]), Teyrchain(MockRuntimeTeyrchainId::get().into())).into();
pub UnitWeightCost: u64 = 1_000;
pub const BaseXcmWeight: Weight = Weight::from_parts(1_000, 1_000);
pub CurrencyPerSecondPerByte: (AssetId, u128, u128) = (AssetId(RelayLocation::get()), 1, 1);
pub TrustedAssets: (AssetFilter, Location) = (All.into(), Here.into());
pub const MaxInstructions: u32 = 100;
pub const MaxAssetsIntoHolding: u32 = 64;
pub CheckingAccount: AccountId = XcmPallet::check_account();
}
/// Type representing both a location and an asset that is held at that location.
/// The id of the held asset is relative to the location where it is being held.
#[derive(Encode, Decode, Clone, PartialEq, Eq, Debug)]
pub struct AssetKind {
pub destination: Location,
pub asset_id: AssetId,
}
pub struct LocatableAssetKindConverter;
impl sp_runtime::traits::TryConvert<AssetKind, LocatableAssetId> for LocatableAssetKindConverter {
fn try_convert(value: AssetKind) -> Result<LocatableAssetId, AssetKind> {
Ok(LocatableAssetId { asset_id: value.asset_id, location: value.destination })
}
}
type AssetIdForAssets = u128;
pub struct FromLocationToAsset<Location, AssetId>(core::marker::PhantomData<(Location, AssetId)>);
impl MaybeEquivalence<Location, AssetIdForAssets>
for FromLocationToAsset<Location, AssetIdForAssets>
{
fn convert(value: &Location) -> Option<AssetIdForAssets> {
match value.unpack() {
(0, []) => Some(0 as AssetIdForAssets),
(1, []) => Some(1 as AssetIdForAssets),
(0, [PalletInstance(1), GeneralIndex(index)]) if ![0, 1].contains(index) =>
Some(*index as AssetIdForAssets),
_ => None,
}
}
fn convert_back(value: &AssetIdForAssets) -> Option<Location> {
match value {
0u128 => Some(Location { parents: 1, interior: Here }),
1u128 => Some(Location { parents: 0, interior: Here }),
para_id @ 1..=1000 =>
Some(Location { parents: 1, interior: [Teyrchain(*para_id as u32)].into() }),
_ => None,
}
}
}
/// Converts a local signed origin into an XCM location. Forms the basis for local origins
/// sending/executing XCMs.
pub type LocalOriginToLocation = SignedToAccountId32<RuntimeOrigin, AccountId, AnyNetwork>;
pub type LocalAssetsTransactor = FungiblesAdapter<
Assets,
ConvertedConcreteId<
AssetIdForAssets,
Balance,
FromLocationToAsset<Location, AssetIdForAssets>,
JustTry,
>,
SovereignAccountOf,
AccountId,
NoChecking,
CheckingAccount,
>;
type OriginConverter = (
pallet_xcm::XcmPassthrough<RuntimeOrigin>,
SignedAccountId32AsNative<AnyNetwork, RuntimeOrigin>,
);
type Barrier = AllowUnpaidExecutionFrom<Everything>;
#[derive(Clone)]
pub struct DummyWeightTrader;
impl WeightTrader for DummyWeightTrader {
fn new() -> Self {
DummyWeightTrader
}
fn buy_weight(
&mut self,
_weight: Weight,
_payment: xcm_executor::AssetsInHolding,
_context: &XcmContext,
) -> Result<xcm_executor::AssetsInHolding, XcmError> {
Ok(xcm_executor::AssetsInHolding::default())
}
}
pub struct XcmConfig;
impl xcm_executor::Config for XcmConfig {
type RuntimeCall = RuntimeCall;
type XcmSender = TestMessageSender;
type XcmEventEmitter = XcmPallet;
type AssetTransactor = LocalAssetsTransactor;
type OriginConverter = OriginConverter;
type IsReserve = ();
type IsTeleporter = ();
type UniversalLocation = UniversalLocation;
type Barrier = Barrier;
type Weigher = FixedWeightBounds<BaseXcmWeight, RuntimeCall, MaxInstructions>;
type Trader = DummyWeightTrader;
type ResponseHandler = XcmPallet;
type AssetTrap = XcmPallet;
type AssetLocker = ();
type AssetExchanger = ();
type AssetClaims = XcmPallet;
type SubscriptionService = XcmPallet;
type PalletInstancesInfo = ();
type MaxAssetsIntoHolding = MaxAssetsIntoHolding;
type FeeManager = ();
type MessageExporter = ();
type UniversalAliases = Nothing;
type CallDispatcher = RuntimeCall;
type SafeCallFilter = Everything;
type Aliasers = Nothing;
type TransactionalProcessor = ();
type HrmpNewChannelOpenRequestHandler = ();
type HrmpChannelAcceptedHandler = ();
type HrmpChannelClosingHandler = ();
type XcmRecorder = XcmPallet;
}
parameter_types! {
pub TreasuryAccountId: AccountId = AccountId::new([42u8; 32]);
}
pub struct TreasuryToAccount;
impl ConvertLocation<AccountId> for TreasuryToAccount {
fn convert_location(location: &Location) -> Option<AccountId> {
match location.unpack() {
(1, [Teyrchain(42), Plurality { id: BodyId::Treasury, part: BodyPart::Voice }]) =>
Some(TreasuryAccountId::get()), // Hardcoded test treasury account id
_ => None,
}
}
}
pub(crate) type SovereignAccountOf = (
AccountId32Aliases<AnyNetwork, AccountId>,
TreasuryToAccount,
HashedDescription<AccountId, DescribeFamily<DescribeAllTerminal>>,
);
impl pallet_xcm::Config for Test {
type RuntimeEvent = RuntimeEvent;
type SendXcmOrigin = EnsureXcmOrigin<RuntimeOrigin, LocalOriginToLocation>;
type XcmRouter = TestMessageSender;
type ExecuteXcmOrigin = EnsureXcmOrigin<RuntimeOrigin, LocalOriginToLocation>;
type XcmExecuteFilter = Everything;
type XcmExecutor = XcmExecutor<XcmConfig>;
type XcmTeleportFilter = Everything;
type XcmReserveTransferFilter = Everything;
type Weigher = FixedWeightBounds<BaseXcmWeight, RuntimeCall, MaxInstructions>;
type UniversalLocation = UniversalLocation;
type RuntimeOrigin = RuntimeOrigin;
type RuntimeCall = RuntimeCall;
const VERSION_DISCOVERY_QUEUE_SIZE: u32 = 100;
type AdvertisedXcmVersion = pallet_xcm::CurrentXcmVersion;
type TrustedLockers = ();
type SovereignAccountOf = SovereignAccountOf;
type Currency = Balances;
type CurrencyMatcher = IsConcrete<RelayLocation>;
type MaxLockers = frame_support::traits::ConstU32<8>;
type MaxRemoteLockConsumers = frame_support::traits::ConstU32<0>;
type RemoteLockConsumerIdentifier = ();
type WeightInfo = pallet_xcm::TestWeightInfo;
type AdminOrigin = EnsureRoot<AccountId>;
type AuthorizedAliasConsideration = Disabled;
}
pub const UNITS: Balance = 1_000_000_000_000;
pub const INITIAL_BALANCE: Balance = 100 * UNITS;
pub const MINIMUM_BALANCE: Balance = 1 * UNITS;
pub fn sibling_chain_account_id(para_id: u32, account: [u8; 32]) -> AccountId {
let location: Location =
(Parent, Teyrchain(para_id), Junction::AccountId32 { id: account, network: None }).into();
SovereignAccountOf::convert_location(&location).unwrap()
}
pub fn new_test_ext() -> sp_io::TestExternalities {
let mut t = frame_system::GenesisConfig::<Test>::default().build_storage().unwrap();
let admin_account: AccountId = AccountId::new([0u8; 32]);
pallet_assets::GenesisConfig::<Test> {
assets: vec![
(0, admin_account.clone(), true, MINIMUM_BALANCE),
(1, admin_account.clone(), true, MINIMUM_BALANCE),
(100, admin_account.clone(), true, MINIMUM_BALANCE),
],
metadata: vec![
(0, "Native token".encode(), "NTV".encode(), 12),
(1, "Relay token".encode(), "RLY".encode(), 12),
(100, "Test token".encode(), "TST".encode(), 12),
],
accounts: vec![
(0, sibling_chain_account_id(42, [3u8; 32]), INITIAL_BALANCE),
(1, TreasuryAccountId::get(), INITIAL_BALANCE),
(100, TreasuryAccountId::get(), INITIAL_BALANCE),
],
next_asset_id: None,
reserves: vec![],
}
.assimilate_storage(&mut t)
.unwrap();
let mut ext = sp_io::TestExternalities::new(t);
ext.execute_with(|| System::set_block_number(1));
ext
}
pub fn next_block() {
System::set_block_number(System::block_number() + 1);
}
pub fn run_to(block_number: BlockNumber) {
while System::block_number() < block_number {
next_block();
}
}
@@ -0,0 +1,22 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
use super::*;
mod mock;
mod pay;
mod salary;
mod transfer;
@@ -0,0 +1,161 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
//! Tests for making sure `PayOverXcm::pay` generates the correct message and sends it to the
//! correct destination
use super::{mock::*, *};
use frame_support::{assert_ok, traits::tokens::Pay};
parameter_types! {
pub SenderAccount: AccountId = AccountId::new([3u8; 32]);
pub InteriorAccount: InteriorLocation = AccountId32 { id: SenderAccount::get().into(), network: None }.into();
pub InteriorBody: InteriorLocation = Plurality { id: BodyId::Treasury, part: BodyPart::Voice }.into();
pub Timeout: BlockNumber = 5; // 5 blocks
}
/// Scenario:
/// Account #3 on the local chain, teyrchain 42, controls an amount of funds on teyrchain 2.
/// [`PayOverXcm::pay`] creates the correct message for account #3 to pay another account, account
/// #5, on teyrchain 2, remotely, in its native token.
#[test]
fn pay_over_xcm_works() {
let recipient = AccountId::new([5u8; 32]);
let asset_kind =
AssetKind { destination: (Parent, Teyrchain(2)).into(), asset_id: Here.into() };
let amount = 10 * UNITS;
new_test_ext().execute_with(|| {
// Check starting balance
assert_eq!(mock::Assets::balance(0, &recipient), 0);
assert_ok!(PayOverXcm::<
InteriorAccount,
TestMessageSender,
TestQueryHandler<TestConfig, BlockNumber>,
Timeout,
AccountId,
AssetKind,
LocatableAssetKindConverter,
AliasesIntoAccountId32<AnyNetwork, AccountId>,
>::pay(&recipient, asset_kind, amount));
let expected_message = Xcm(vec![
DescendOrigin(AccountId32 { id: SenderAccount::get().into(), network: None }.into()),
UnpaidExecution { weight_limit: Unlimited, check_origin: None },
SetAppendix(Xcm(vec![
SetFeesMode { jit_withdraw: true },
ReportError(QueryResponseInfo {
destination: (Parent, Teyrchain(42)).into(),
query_id: 1,
max_weight: Weight::zero(),
}),
])),
TransferAsset {
assets: (Here, amount).into(),
beneficiary: AccountId32 { id: recipient.clone().into(), network: None }.into(),
},
]);
let expected_hash = fake_message_hash(&expected_message);
assert_eq!(
sent_xcm(),
vec![((Parent, Teyrchain(2)).into(), expected_message, expected_hash)]
);
let (_, message, mut hash) = sent_xcm()[0].clone();
let message =
Xcm::<<XcmConfig as xcm_executor::Config>::RuntimeCall>::from(message.clone());
// Execute message in teyrchain 2 with teyrchain 42's origin
let origin = (Parent, Teyrchain(42));
XcmExecutor::<XcmConfig>::prepare_and_execute(
origin,
message,
&mut hash,
Weight::MAX,
Weight::zero(),
);
assert_eq!(mock::Assets::balance(0, &recipient), amount);
});
}
/// Scenario:
/// A pluralistic body, a Treasury, on the local chain, teyrchain 42, controls an amount of funds
/// on teyrchain 2. [`PayOverXcm::pay`] creates the correct message for the treasury to pay
/// another account, account #7, on teyrchain 2, remotely, in the relay's token.
#[test]
fn pay_over_xcm_governance_body() {
let recipient = AccountId::new([7u8; 32]);
let asset_kind =
AssetKind { destination: (Parent, Teyrchain(2)).into(), asset_id: Parent.into() };
let amount = 10 * UNITS;
let relay_asset_index = 1;
new_test_ext().execute_with(|| {
// Check starting balance
assert_eq!(mock::Assets::balance(relay_asset_index, &recipient), 0);
assert_ok!(PayOverXcm::<
InteriorBody,
TestMessageSender,
TestQueryHandler<TestConfig, BlockNumber>,
Timeout,
AccountId,
AssetKind,
LocatableAssetKindConverter,
AliasesIntoAccountId32<AnyNetwork, AccountId>,
>::pay(&recipient, asset_kind, amount));
let expected_message = Xcm(vec![
DescendOrigin(Plurality { id: BodyId::Treasury, part: BodyPart::Voice }.into()),
UnpaidExecution { weight_limit: Unlimited, check_origin: None },
SetAppendix(Xcm(vec![
SetFeesMode { jit_withdraw: true },
ReportError(QueryResponseInfo {
destination: (Parent, Teyrchain(42)).into(),
query_id: 1,
max_weight: Weight::zero(),
}),
])),
TransferAsset {
assets: (Parent, amount).into(),
beneficiary: AccountId32 { id: recipient.clone().into(), network: None }.into(),
},
]);
let expected_hash = fake_message_hash(&expected_message);
assert_eq!(
sent_xcm(),
vec![((Parent, Teyrchain(2)).into(), expected_message, expected_hash)]
);
let (_, message, mut hash) = sent_xcm()[0].clone();
let message =
Xcm::<<XcmConfig as xcm_executor::Config>::RuntimeCall>::from(message.clone());
// Execute message in teyrchain 2 with teyrchain 42's origin
let origin = (Parent, Teyrchain(42));
XcmExecutor::<XcmConfig>::prepare_and_execute(
origin,
message,
&mut hash,
Weight::MAX,
Weight::zero(),
);
assert_eq!(mock::Assets::balance(relay_asset_index, &recipient), amount);
});
}
@@ -0,0 +1,181 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
//! Tests for the integration between `PayOverXcm` and the salary pallet
use super::{mock::*, *};
use frame_support::{
assert_ok,
traits::{tokens::GetSalary, RankedMembers},
};
use sp_runtime::{traits::ConvertToValue, DispatchResult};
parameter_types! {
pub Interior: InteriorLocation = Plurality { id: BodyId::Treasury, part: BodyPart::Voice }.into();
pub Timeout: BlockNumber = 5;
pub AssetHub: Location = (Parent, Teyrchain(1)).into();
pub AssetIdGeneralIndex: u128 = 100;
pub AssetHubAssetId: AssetId = (PalletInstance(1), GeneralIndex(AssetIdGeneralIndex::get())).into();
pub LocatableAsset: LocatableAssetId = LocatableAssetId { asset_id: AssetHubAssetId::get(), location: AssetHub::get() };
}
type SalaryPayOverXcm = PayOverXcm<
Interior,
TestMessageSender,
TestQueryHandler<TestConfig, BlockNumber>,
Timeout,
AccountId,
(),
ConvertToValue<LocatableAsset>,
AliasesIntoAccountId32<AnyNetwork, AccountId>,
>;
type Rank = u128;
thread_local! {
pub static CLUB: RefCell<BTreeMap<AccountId, Rank>> = RefCell::new(BTreeMap::new());
}
pub struct TestClub;
impl RankedMembers for TestClub {
type AccountId = AccountId;
type Rank = Rank;
fn min_rank() -> Self::Rank {
0
}
fn rank_of(who: &Self::AccountId) -> Option<Self::Rank> {
CLUB.with(|club| club.borrow().get(who).cloned())
}
fn induct(who: &Self::AccountId) -> DispatchResult {
CLUB.with(|club| club.borrow_mut().insert(who.clone(), 0));
Ok(())
}
fn promote(who: &Self::AccountId) -> DispatchResult {
CLUB.with(|club| {
club.borrow_mut().entry(who.clone()).and_modify(|rank| *rank += 1);
});
Ok(())
}
fn demote(who: &Self::AccountId) -> DispatchResult {
CLUB.with(|club| match club.borrow().get(who) {
None => Err(sp_runtime::DispatchError::Unavailable),
Some(&0) => {
club.borrow_mut().remove(&who);
Ok(())
},
Some(_) => {
club.borrow_mut().entry(who.clone()).and_modify(|rank| *rank += 1);
Ok(())
},
})
}
}
fn set_rank(who: AccountId, rank: u128) {
CLUB.with(|club| club.borrow_mut().insert(who, rank));
}
parameter_types! {
pub const RegistrationPeriod: BlockNumber = 2;
pub const PayoutPeriod: BlockNumber = 2;
pub const FixedSalaryAmount: Balance = 10 * UNITS;
pub static Budget: Balance = FixedSalaryAmount::get();
}
pub struct FixedSalary;
impl GetSalary<Rank, AccountId, Balance> for FixedSalary {
fn get_salary(_rank: Rank, _who: &AccountId) -> Balance {
FixedSalaryAmount::get()
}
}
impl pallet_salary::Config for Test {
type WeightInfo = ();
type RuntimeEvent = RuntimeEvent;
type Paymaster = SalaryPayOverXcm;
type Members = TestClub;
type Salary = FixedSalary;
type RegistrationPeriod = RegistrationPeriod;
type PayoutPeriod = PayoutPeriod;
type Budget = Budget;
}
/// Scenario:
/// The salary pallet is used to pay a member over XCM.
/// The correct XCM message is generated and when executed in the remote chain,
/// the member receives the salary.
#[test]
fn salary_pay_over_xcm_works() {
let recipient = AccountId::new([1u8; 32]);
new_test_ext().execute_with(|| {
// Set the recipient as a member of a ranked collective
set_rank(recipient.clone(), 1);
// Check starting balance
assert_eq!(mock::Assets::balance(AssetIdGeneralIndex::get(), &recipient.clone()), 0);
// Use salary pallet to call `PayOverXcm::pay`
assert_ok!(Salary::init(RuntimeOrigin::signed(recipient.clone())));
run_to(5);
assert_ok!(Salary::induct(RuntimeOrigin::signed(recipient.clone())));
assert_ok!(Salary::bump(RuntimeOrigin::signed(recipient.clone())));
assert_ok!(Salary::register(RuntimeOrigin::signed(recipient.clone())));
run_to(7);
assert_ok!(Salary::payout(RuntimeOrigin::signed(recipient.clone())));
// Get message from mock transport layer
let (_, message, mut hash) = sent_xcm()[0].clone();
// Change type from `Xcm<()>` to `Xcm<RuntimeCall>` to be able to execute later
let message =
Xcm::<<XcmConfig as xcm_executor::Config>::RuntimeCall>::from(message.clone());
let expected_message: Xcm<RuntimeCall> = Xcm::<RuntimeCall>(vec![
DescendOrigin(Plurality { id: BodyId::Treasury, part: BodyPart::Voice }.into()),
UnpaidExecution { weight_limit: Unlimited, check_origin: None },
SetAppendix(Xcm(vec![
SetFeesMode { jit_withdraw: true },
ReportError(QueryResponseInfo {
destination: (Parent, Teyrchain(42)).into(),
query_id: 1,
max_weight: Weight::zero(),
}),
])),
TransferAsset {
assets: (AssetHubAssetId::get(), FixedSalaryAmount::get()).into(),
beneficiary: AccountId32 { id: recipient.clone().into(), network: None }.into(),
},
]);
assert_eq!(message, expected_message);
// Execute message as the asset hub
XcmExecutor::<XcmConfig>::prepare_and_execute(
(Parent, Teyrchain(42)),
message,
&mut hash,
Weight::MAX,
Weight::zero(),
);
// Recipient receives the payment
assert_eq!(
mock::Assets::balance(AssetIdGeneralIndex::get(), &recipient),
FixedSalaryAmount::get()
);
});
}
@@ -0,0 +1,185 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
//! Tests for making sure `TransferOverXcm::transfer` generates the correct message and sends it to
//! the correct destination
use super::{mock::*, *};
use crate::AliasesIntoAccountId32;
use frame_support::{
assert_ok, parameter_types,
traits::{fungible::Mutate, fungibles::Mutate as FungiblesMutate},
};
use xcm::{
latest::{InteriorLocation, Junctions::X2, Xcm},
v5::{AssetId, Location, Parent},
};
use xcm_executor::{traits::ConvertLocation, XcmExecutor};
parameter_types! {
pub SenderAccount: AccountId = AccountId::new([3u8; 32]);
pub SenderLocationOnTarget: Location = Location::new(
1,
X2([Teyrchain(MockRuntimeTeyrchainId::get().into()), AccountId32 { network: None, id: SenderAccount::get().into() }].into()),
);
pub SenderAccountOnTarget: AccountId = SovereignAccountOf::convert_location(&SenderLocationOnTarget::get()).expect("can convert");
pub InteriorAccount: InteriorLocation = AccountId32 { id: SenderAccount::get().into(), network: None }.into();
pub Timeout: BlockNumber = 5; // 5 blocks
}
type TestTransferOverXcm =
TransferOverXcm<AliasesIntoAccountId32<AnyNetwork, AccountId>, TestTransferOverXcmHelper>;
type TestTransferOverXcmHelper = TransferOverXcmHelper<
TestMessageSender,
TestQueryHandler<TestConfig, BlockNumber>,
TestFeeManager,
Timeout,
AccountId,
AssetKind,
LocatableAssetKindConverter,
AliasesIntoAccountId32<AnyNetwork, AccountId>,
>;
fn fungible_amount(asset: Asset) -> u128 {
let Asset { id: _, ref fun } = asset;
match fun {
Fungible(fee) => *fee,
NonFungible(_) => panic!("not fungible"),
}
}
/// Scenario:
/// Account #3 on the local chain, teyrchain 42, controls an amount of funds on teyrchain 2.
/// [`TransferOverXcm::transfer`] creates the correct message for account #3 to pay another account,
/// account #5, on teyrchain 1000, remotely, in the relay chains native token.
#[test]
fn transfer_over_xcm_works() {
let recipient = AccountId::new([5u8; 32]);
// transact the parents native asset on teyrchain 1000.
let asset_kind = AssetKind {
destination: (Parent, Teyrchain(1000)).into(),
asset_id: RelayLocation::get().into(),
};
let transfer_amount = INITIAL_BALANCE / 10;
new_test_ext().execute_with(|| {
// The teyrchain's native token
mock::Assets::set_balance(0, &SenderAccountOnTarget::get(), INITIAL_BALANCE);
// The relaychain's native token
mock::Assets::set_balance(1, &SenderAccountOnTarget::get(), INITIAL_BALANCE);
mock::Balances::set_balance(&SenderAccountOnTarget::get(), INITIAL_BALANCE);
// Check starting balance
assert_eq!(mock::Assets::balance(0, &recipient), 0);
assert_eq!(mock::Assets::balance(1, &recipient), 0);
let fee_asset =
Asset { id: AssetId(RelayLocation::get()), fun: Fungible(1_000_000_000_000_u128) };
assert_ok!(TestTransferOverXcm::transfer(
&SenderAccount::get(),
&recipient,
asset_kind.clone(),
transfer_amount,
Some(fee_asset.clone())
));
let expected_message = remote_transfer_xcm(
recipient.clone(),
(asset_kind.asset_id, transfer_amount).into(),
fee_asset.clone().into(),
);
assert_send_and_execute_msg(expected_message);
assert_eq!(mock::Assets::balance(1, &recipient), transfer_amount);
// The mock trader does not refund any weight. Hence, the balance is exactly the
// initial amount minus what we withdrew for transferring and paying the remote fees.
assert_eq!(
mock::Assets::balance(1, &SenderAccountOnTarget::get()),
INITIAL_BALANCE - transfer_amount - fungible_amount(fee_asset.into())
);
});
}
#[test]
fn sender_on_relative_to_asset_location_works() {
let asset_kind = AssetKind {
destination: (Parent, Teyrchain(1000)).into(),
asset_id: RelayLocation::get().into(),
};
let sender_on_remote = TestTransferOverXcmHelper::from_relative_to_asset_location(
&SenderAccount::get(),
asset_kind.clone(),
)
.unwrap();
assert_eq!(sender_on_remote, SenderLocationOnTarget::get());
}
fn assert_send_and_execute_msg(expected_message: Xcm<()>) {
let expected_hash = fake_message_hash(&expected_message);
assert_eq!(
sent_xcm(),
vec![((Parent, Teyrchain(1000)).into(), expected_message, expected_hash)]
);
let (_, message, mut hash) = sent_xcm()[0].clone();
let message = Xcm::<<XcmConfig as xcm_executor::Config>::RuntimeCall>::from(message.clone());
// Execute message in teyrchain 1000 with our teyrchains's origin
let origin = (Parent, Teyrchain(MockRuntimeTeyrchainId::get().into()));
let _result = XcmExecutor::<XcmConfig>::prepare_and_execute(
origin,
message,
&mut hash,
Weight::MAX,
Weight::zero(),
);
}
fn remote_transfer_xcm<Call>(
recipient: AccountId,
transfer_asset: Asset,
fee_asset: Asset,
) -> Xcm<Call> {
Xcm(vec![
// Change the origin to the local account on the target chain
DescendOrigin(AccountId32 { id: SenderAccount::get().into(), network: None }.into()),
WithdrawAsset(fee_asset.clone().into()),
PayFees { asset: fee_asset.clone() },
SetAppendix(Xcm(vec![
ReportError(QueryResponseInfo {
destination: (Parent, Teyrchain(MockRuntimeTeyrchainId::get().into())).into(),
query_id: 1,
max_weight: Weight::MAX,
}),
RefundSurplus,
DepositAsset {
assets: AssetFilter::Wild(WildAsset::All),
beneficiary: SenderLocationOnTarget::get(),
},
])),
TransferAsset {
beneficiary: AccountId32 { network: None, id: recipient.clone().into() }.into(),
assets: transfer_asset.into(),
},
])
}
@@ -0,0 +1,144 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
use super::*;
#[test]
fn pallet_query_should_work() {
AllowUnpaidFrom::set(vec![[Teyrchain(1)].into()]);
// They want to transfer 100 of our native asset from sovereign account of teyrchain #1 into #2
// and let them know to hand it to account #3.
let message = Xcm(vec![QueryPallet {
module_name: "Error".into(),
response_info: QueryResponseInfo {
destination: Teyrchain(1).into(),
query_id: 1,
max_weight: Weight::from_parts(50, 50),
},
}]);
let mut hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
Teyrchain(1),
message,
&mut hash,
Weight::from_parts(50, 50),
Weight::zero(),
);
assert_eq!(r, Outcome::Complete { used: Weight::from_parts(10, 10) });
let expected_msg = Xcm::<()>(vec![
QueryResponse {
query_id: 1,
max_weight: Weight::from_parts(50, 50),
response: Response::PalletsInfo(Default::default()),
querier: Some(Here.into()),
},
SetTopic(hash),
]);
assert_eq!(sent_xcm(), vec![(Teyrchain(1).into(), expected_msg, hash)]);
}
#[test]
fn pallet_query_with_results_should_work() {
AllowUnpaidFrom::set(vec![[Teyrchain(1)].into()]);
// They want to transfer 100 of our native asset from sovereign account of teyrchain #1 into #2
// and let them know to hand it to account #3.
let message = Xcm(vec![QueryPallet {
module_name: "pallet_balances".into(),
response_info: QueryResponseInfo {
destination: Teyrchain(1).into(),
query_id: 1,
max_weight: Weight::from_parts(50, 50),
},
}]);
let mut hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
Teyrchain(1),
message,
&mut hash,
Weight::from_parts(50, 50),
Weight::zero(),
);
assert_eq!(r, Outcome::Complete { used: Weight::from_parts(10, 10) });
let expected_msg = Xcm::<()>(vec![
QueryResponse {
query_id: 1,
max_weight: Weight::from_parts(50, 50),
response: Response::PalletsInfo(
vec![PalletInfo::new(
1,
b"Balances".as_ref().into(),
b"pallet_balances".as_ref().into(),
1,
42,
69,
)
.unwrap()]
.try_into()
.unwrap(),
),
querier: Some(Here.into()),
},
SetTopic(hash),
]);
assert_eq!(sent_xcm(), vec![(Teyrchain(1).into(), expected_msg, hash)]);
}
#[test]
fn prepaid_result_of_query_should_get_free_execution() {
let query_id = 33;
// We put this in manually here, but normally this would be done at the point of crafting the
// message.
expect_response(query_id, Parent.into());
let the_response = Response::Assets((Parent, 100u128).into());
let message = Xcm::<TestCall>(vec![QueryResponse {
query_id,
response: the_response.clone(),
max_weight: Weight::from_parts(10, 10),
querier: Some(Here.into()),
}]);
let mut hash = fake_message_hash(&message);
let weight_limit = Weight::from_parts(10, 10);
// First time the response gets through since we're expecting it...
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
Parent,
message.clone(),
&mut hash,
weight_limit,
Weight::zero(),
);
assert_eq!(r, Outcome::Complete { used: Weight::from_parts(10, 10) });
assert_eq!(response(query_id).unwrap(), the_response);
// Second time it doesn't, since we're not.
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
Parent,
message.clone(),
&mut hash,
weight_limit,
Weight::zero(),
);
assert_eq!(
r,
Outcome::Incomplete {
used: Weight::from_parts(10, 10),
error: InstructionError { index: 0, error: XcmError::Barrier },
}
);
}
@@ -0,0 +1,95 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
use super::*;
use frame_support::{assert_ok, traits::Everything};
use xcm_executor::traits::Properties;
fn props() -> Properties {
Properties { weight_credit: Weight::zero(), message_id: None }
}
#[test]
fn trailing_set_topic_as_id_with_unique_topic_should_work() {
type AllowSubscriptions = AllowSubscriptionsFrom<Everything>;
// check the validity of XCM for the `AllowSubscriptions` barrier
let valid_xcm = Xcm::<()>(vec![SubscribeVersion {
query_id: 42,
max_response_weight: Weight::from_parts(5000, 5000),
}]);
assert_eq!(
AllowSubscriptions::should_execute(
&Location::parent(),
valid_xcm.clone().inner_mut(),
Weight::from_parts(10, 10),
&mut props(),
),
Ok(())
);
// simulate sending `valid_xcm` with the `WithUniqueTopic` router
let mut sent_xcm = sp_io::TestExternalities::default().execute_with(|| {
assert_ok!(send_xcm::<WithUniqueTopic<TestMessageSender>>(Location::parent(), valid_xcm,));
sent_xcm()
});
assert_eq!(1, sent_xcm.len());
// `sent_xcm` should contain `SubscribeVersion` and have `SetTopic` added
let mut sent_xcm = sent_xcm.remove(0).1;
let _ = sent_xcm
.0
.matcher()
.assert_remaining_insts(2)
.expect("two instructions")
.match_next_inst(|instr| match instr {
SubscribeVersion { .. } => Ok(()),
_ => Err(ProcessMessageError::BadFormat),
})
.expect("expected instruction `SubscribeVersion`")
.match_next_inst(|instr| match instr {
SetTopic(..) => Ok(()),
_ => Err(ProcessMessageError::BadFormat),
})
.expect("expected instruction `SetTopic`");
// `sent_xcm` contains `SetTopic` and is now invalid for `AllowSubscriptions`
assert_eq!(
AllowSubscriptions::should_execute(
&Location::parent(),
sent_xcm.clone().inner_mut(),
Weight::from_parts(10, 10),
&mut props(),
),
Err(ProcessMessageError::BadFormat)
);
// let's apply `TrailingSetTopicAsId` before `AllowSubscriptions`
let mut props = props();
assert!(props.message_id.is_none());
// should pass, and the `message_id` is set
assert_eq!(
TrailingSetTopicAsId::<AllowSubscriptions>::should_execute(
&Location::parent(),
sent_xcm.clone().inner_mut(),
Weight::from_parts(10, 10),
&mut props,
),
Ok(())
);
assert!(props.message_id.is_some());
}
@@ -0,0 +1,334 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
use super::*;
#[test]
fn transacting_should_work() {
AllowUnpaidFrom::set(vec![Parent.into()]);
let message = Xcm::<TestCall>(vec![Transact {
origin_kind: OriginKind::Native,
call: TestCall::Any(Weight::from_parts(50, 50), None).encode().into(),
fallback_max_weight: None,
}]);
let mut hash = fake_message_hash(&message);
let weight_limit = Weight::from_parts(60, 60);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
Parent,
message,
&mut hash,
weight_limit,
Weight::zero(),
);
assert_eq!(r, Outcome::Complete { used: Weight::from_parts(60, 60) });
}
#[test]
fn transacting_should_respect_max_weight_requirement() {
AllowUnpaidFrom::set(vec![Parent.into()]);
let message = Xcm::<TestCall>(vec![Transact {
origin_kind: OriginKind::Native,
call: TestCall::Any(Weight::from_parts(50, 50), None).encode().into(),
fallback_max_weight: None,
}]);
let mut hash = fake_message_hash(&message);
let weight_limit = Weight::from_parts(60, 60);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
Parent,
message,
&mut hash,
weight_limit,
Weight::zero(),
);
assert_eq!(r, Outcome::Complete { used: Weight::from_parts(60, 60) });
}
#[test]
fn transacting_should_refund_weight() {
AllowUnpaidFrom::set(vec![Parent.into()]);
let message = Xcm::<TestCall>(vec![Transact {
origin_kind: OriginKind::Native,
call: TestCall::Any(Weight::from_parts(50, 50), Some(Weight::from_parts(30, 30)))
.encode()
.into(),
fallback_max_weight: None,
}]);
let mut hash = fake_message_hash(&message);
let weight_limit = Weight::from_parts(60, 60);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
Parent,
message,
&mut hash,
weight_limit,
Weight::zero(),
);
assert_eq!(r, Outcome::Complete { used: Weight::from_parts(40, 40) });
}
#[test]
fn paid_transacting_should_refund_payment_for_unused_weight() {
let one: Location = AccountIndex64 { index: 1, network: None }.into();
AllowPaidFrom::set(vec![one.clone()]);
add_asset(AccountIndex64 { index: 1, network: None }, (Parent, 200u128));
WeightPrice::set((Parent.into(), 1_000_000_000_000, 1024 * 1024));
let origin = one.clone();
let fees = (Parent, 200u128).into();
let message = Xcm::<TestCall>(vec![
WithdrawAsset((Parent, 200u128).into()), // enough for 200 units of weight.
BuyExecution { fees, weight_limit: Limited(Weight::from_parts(100, 100)) },
Transact {
origin_kind: OriginKind::Native,
// call estimated at 50 but only takes 10.
call: TestCall::Any(Weight::from_parts(50, 50), Some(Weight::from_parts(10, 10)))
.encode()
.into(),
fallback_max_weight: None,
},
RefundSurplus,
DepositAsset { assets: AllCounted(1).into(), beneficiary: one },
]);
let mut hash = fake_message_hash(&message);
let weight_limit = Weight::from_parts(100, 100);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
origin,
message,
&mut hash,
weight_limit,
Weight::zero(),
);
assert_eq!(r, Outcome::Complete { used: Weight::from_parts(60, 60) });
assert_eq!(
asset_list(AccountIndex64 { index: 1, network: None }),
vec![(Parent, 80u128).into()]
);
}
#[test]
fn report_successful_transact_status_should_work() {
AllowUnpaidFrom::set(vec![Parent.into()]);
let message = Xcm::<TestCall>(vec![
Transact {
origin_kind: OriginKind::Native,
call: TestCall::Any(Weight::from_parts(50, 50), None).encode().into(),
fallback_max_weight: None,
},
ReportTransactStatus(QueryResponseInfo {
destination: Parent.into(),
query_id: 42,
max_weight: Weight::from_parts(5000, 5000),
}),
]);
let mut hash = fake_message_hash(&message);
let weight_limit = Weight::from_parts(70, 70);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
Parent,
message,
&mut hash,
weight_limit,
Weight::zero(),
);
assert_eq!(r, Outcome::Complete { used: Weight::from_parts(70, 70) });
let expected_msg = Xcm(vec![
QueryResponse {
response: Response::DispatchResult(MaybeErrorCode::Success),
query_id: 42,
max_weight: Weight::from_parts(5000, 5000),
querier: Some(Here.into()),
},
SetTopic(hash),
]);
assert_eq!(sent_xcm(), vec![(Parent.into(), expected_msg, hash)]);
}
#[test]
fn report_failed_transact_status_should_work() {
AllowUnpaidFrom::set(vec![Parent.into()]);
let message = Xcm::<TestCall>(vec![
Transact {
origin_kind: OriginKind::Native,
call: TestCall::OnlyRoot(Weight::from_parts(50, 50), None).encode().into(),
fallback_max_weight: None,
},
ReportTransactStatus(QueryResponseInfo {
destination: Parent.into(),
query_id: 42,
max_weight: Weight::from_parts(5000, 5000),
}),
]);
let mut hash = fake_message_hash(&message);
let weight_limit = Weight::from_parts(70, 70);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
Parent,
message,
&mut hash,
weight_limit,
Weight::zero(),
);
assert_eq!(r, Outcome::Complete { used: Weight::from_parts(70, 70) });
let expected_msg = Xcm(vec![
QueryResponse {
response: Response::DispatchResult(vec![2].into()),
query_id: 42,
max_weight: Weight::from_parts(5000, 5000),
querier: Some(Here.into()),
},
SetTopic(hash),
]);
assert_eq!(sent_xcm(), vec![(Parent.into(), expected_msg, hash)]);
}
#[test]
fn expect_successful_transact_status_should_work() {
AllowUnpaidFrom::set(vec![Parent.into()]);
let message = Xcm::<TestCall>(vec![
Transact {
origin_kind: OriginKind::Native,
call: TestCall::Any(Weight::from_parts(50, 50), None).encode().into(),
fallback_max_weight: None,
},
ExpectTransactStatus(MaybeErrorCode::Success),
]);
let mut hash = fake_message_hash(&message);
let weight_limit = Weight::from_parts(70, 70);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
Parent,
message,
&mut hash,
weight_limit,
Weight::zero(),
);
assert_eq!(r, Outcome::Complete { used: Weight::from_parts(70, 70) });
let message = Xcm::<TestCall>(vec![
Transact {
origin_kind: OriginKind::Native,
call: TestCall::OnlyRoot(Weight::from_parts(50, 50), None).encode().into(),
fallback_max_weight: None,
},
ExpectTransactStatus(MaybeErrorCode::Success),
]);
let mut hash = fake_message_hash(&message);
let weight_limit = Weight::from_parts(70, 70);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
Parent,
message,
&mut hash,
weight_limit,
Weight::zero(),
);
assert_eq!(
r,
Outcome::Incomplete {
used: Weight::from_parts(70, 70),
error: InstructionError { index: 1, error: XcmError::ExpectationFalse },
}
);
}
#[test]
fn expect_failed_transact_status_should_work() {
AllowUnpaidFrom::set(vec![Parent.into()]);
let message = Xcm::<TestCall>(vec![
Transact {
origin_kind: OriginKind::Native,
call: TestCall::OnlyRoot(Weight::from_parts(50, 50), None).encode().into(),
fallback_max_weight: None,
},
ExpectTransactStatus(vec![2].into()),
]);
let mut hash = fake_message_hash(&message);
let weight_limit = Weight::from_parts(70, 70);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
Parent,
message,
&mut hash,
weight_limit,
Weight::zero(),
);
assert_eq!(r, Outcome::Complete { used: Weight::from_parts(70, 70) });
let message = Xcm::<TestCall>(vec![
Transact {
origin_kind: OriginKind::Native,
call: TestCall::Any(Weight::from_parts(50, 50), None).encode().into(),
fallback_max_weight: None,
},
ExpectTransactStatus(vec![2].into()),
]);
let mut hash = fake_message_hash(&message);
let weight_limit = Weight::from_parts(70, 70);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
Parent,
message,
&mut hash,
weight_limit,
Weight::zero(),
);
assert_eq!(
r,
Outcome::Incomplete {
used: Weight::from_parts(70, 70),
error: InstructionError { index: 1, error: XcmError::ExpectationFalse },
}
);
}
#[test]
fn clear_transact_status_should_work() {
AllowUnpaidFrom::set(vec![Parent.into()]);
let message = Xcm::<TestCall>(vec![
Transact {
origin_kind: OriginKind::Native,
call: TestCall::OnlyRoot(Weight::from_parts(50, 50), None).encode().into(),
fallback_max_weight: None,
},
ClearTransactStatus,
ReportTransactStatus(QueryResponseInfo {
destination: Parent.into(),
query_id: 42,
max_weight: Weight::from_parts(5000, 5000),
}),
]);
let mut hash = fake_message_hash(&message);
let weight_limit = Weight::from_parts(80, 80);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
Parent,
message,
&mut hash,
weight_limit,
Weight::zero(),
);
assert_eq!(r, Outcome::Complete { used: Weight::from_parts(80, 80) });
let expected_msg = Xcm(vec![
QueryResponse {
response: Response::DispatchResult(MaybeErrorCode::Success),
query_id: 42,
max_weight: Weight::from_parts(5000, 5000),
querier: Some(Here.into()),
},
SetTopic(hash),
]);
assert_eq!(sent_xcm(), vec![(Parent.into(), expected_msg, hash)]);
}
@@ -0,0 +1,237 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
use super::*;
#[test]
fn simple_version_subscriptions_should_work() {
AllowSubsFrom::set(vec![Parent.into()]);
let origin = Teyrchain(1000);
let message = Xcm::<TestCall>(vec![
SetAppendix(Xcm(vec![])),
SubscribeVersion { query_id: 42, max_response_weight: Weight::from_parts(5000, 5000) },
]);
let mut hash = fake_message_hash(&message);
let weight_limit = Weight::from_parts(20, 20);
// this case fails because the origin is not allowed
assert_eq!(
XcmExecutor::<TestConfig>::prepare_and_execute(
origin,
message.clone(),
&mut hash,
weight_limit,
Weight::zero(),
),
Outcome::Incomplete {
used: Weight::from_parts(20, 20),
error: InstructionError { index: 0, error: XcmError::Barrier },
}
);
// this case fails because the additional `SetAppendix` instruction is not allowed in the
// `AllowSubscriptionsFrom`
assert_eq!(
XcmExecutor::<TestConfig>::prepare_and_execute(
Parent,
message,
&mut hash,
weight_limit,
Weight::zero(),
),
Outcome::Incomplete {
used: Weight::from_parts(20, 20),
error: InstructionError { index: 0, error: XcmError::Barrier },
}
);
let message = Xcm::<TestCall>(vec![SubscribeVersion {
query_id: 42,
max_response_weight: Weight::from_parts(5000, 5000),
}]);
let mut hash = fake_message_hash(&message);
let weight_limit = Weight::from_parts(10, 10);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
origin,
message.clone(),
&mut hash,
weight_limit,
Weight::zero(),
);
assert_eq!(
r,
Outcome::Incomplete {
used: Weight::from_parts(10, 10),
error: InstructionError { index: 0, error: XcmError::Barrier },
}
);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
Parent,
message,
&mut hash,
weight_limit,
Weight::zero(),
);
assert_eq!(r, Outcome::Complete { used: Weight::from_parts(10, 10) });
assert_eq!(
SubscriptionRequests::get(),
vec![(Parent.into(), Some((42, Weight::from_parts(5000, 5000))))]
);
}
#[test]
fn version_subscription_instruction_should_work() {
let origin = Teyrchain(1000);
let message = Xcm::<TestCall>(vec![
DescendOrigin([AccountIndex64 { index: 1, network: None }].into()),
SubscribeVersion { query_id: 42, max_response_weight: Weight::from_parts(5000, 5000) },
]);
let mut hash = fake_message_hash(&message);
let weight_limit = Weight::from_parts(20, 20);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
origin,
message,
&mut hash,
weight_limit,
weight_limit,
);
assert_eq!(
r,
Outcome::Incomplete {
used: Weight::from_parts(20, 20),
error: InstructionError { index: 1, error: XcmError::BadOrigin },
}
);
let message = Xcm::<TestCall>(vec![
SetAppendix(Xcm(vec![])),
SubscribeVersion { query_id: 42, max_response_weight: Weight::from_parts(5000, 5000) },
]);
let mut hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
origin,
message,
&mut hash,
weight_limit,
weight_limit,
);
assert_eq!(r, Outcome::Complete { used: Weight::from_parts(20, 20) });
assert_eq!(
SubscriptionRequests::get(),
vec![(Teyrchain(1000).into(), Some((42, Weight::from_parts(5000, 5000))))]
);
}
#[test]
fn simple_version_unsubscriptions_should_work() {
AllowSubsFrom::set(vec![Parent.into()]);
let origin = Teyrchain(1000);
let message = Xcm::<TestCall>(vec![SetAppendix(Xcm(vec![])), UnsubscribeVersion]);
let mut hash = fake_message_hash(&message);
let weight_limit = Weight::from_parts(20, 20);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
origin,
message,
&mut hash,
weight_limit,
Weight::zero(),
);
assert_eq!(
r,
Outcome::Incomplete {
used: Weight::from_parts(20, 20),
error: InstructionError { index: 0, error: XcmError::Barrier },
}
);
let origin = Teyrchain(1000);
let message = Xcm::<TestCall>(vec![UnsubscribeVersion]);
let mut hash = fake_message_hash(&message);
let weight_limit = Weight::from_parts(10, 10);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
origin,
message.clone(),
&mut hash,
weight_limit,
Weight::zero(),
);
assert_eq!(
r,
Outcome::Incomplete {
used: Weight::from_parts(10, 10),
error: InstructionError { index: 0, error: XcmError::Barrier },
}
);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
Parent,
message,
&mut hash,
weight_limit,
Weight::zero(),
);
assert_eq!(r, Outcome::Complete { used: Weight::from_parts(10, 10) });
assert_eq!(SubscriptionRequests::get(), vec![(Parent.into(), None)]);
assert_eq!(sent_xcm(), vec![]);
}
#[test]
fn version_unsubscription_instruction_should_work() {
let origin = Teyrchain(1000);
// Not allowed to do it when origin has been changed.
let message = Xcm::<TestCall>(vec![
DescendOrigin([AccountIndex64 { index: 1, network: None }].into()),
UnsubscribeVersion,
]);
let mut hash = fake_message_hash(&message);
let weight_limit = Weight::from_parts(20, 20);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
origin,
message,
&mut hash,
weight_limit,
weight_limit,
);
assert_eq!(
r,
Outcome::Incomplete {
used: Weight::from_parts(20, 20),
error: InstructionError { index: 1, error: XcmError::BadOrigin },
}
);
// Fine to do it when origin is untouched.
let message = Xcm::<TestCall>(vec![SetAppendix(Xcm(vec![])), UnsubscribeVersion]);
let mut hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
origin,
message,
&mut hash,
weight_limit,
weight_limit,
);
assert_eq!(r, Outcome::Complete { used: Weight::from_parts(20, 20) });
assert_eq!(SubscriptionRequests::get(), vec![(Teyrchain(1000).into(), None)]);
assert_eq!(sent_xcm(), vec![]);
}
+315
View File
@@ -0,0 +1,315 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
use super::*;
#[test]
fn fixed_rate_of_fungible_should_work() {
parameter_types! {
pub static WeightPrice: (AssetId, u128, u128) =
(Here.into(), WEIGHT_REF_TIME_PER_SECOND.into(), WEIGHT_PROOF_SIZE_PER_MB.into());
}
let mut trader = FixedRateOfFungible::<WeightPrice, ()>::new();
let ctx = XcmContext { origin: None, message_id: XcmHash::default(), topic: None };
// supplies 100 unit of asset, 80 still remains after purchasing weight
assert_eq!(
trader.buy_weight(
Weight::from_parts(10, 10),
fungible_multi_asset(Here.into(), 100).into(),
&ctx,
),
Ok(fungible_multi_asset(Here.into(), 80).into()),
);
// should have nothing left, as 5 + 5 = 10, and we supplied 10 units of asset.
assert_eq!(
trader.buy_weight(
Weight::from_parts(5, 5),
fungible_multi_asset(Here.into(), 10).into(),
&ctx,
),
Ok(vec![].into()),
);
// should have 5 left, as there are no proof size components
assert_eq!(
trader.buy_weight(
Weight::from_parts(5, 0),
fungible_multi_asset(Here.into(), 10).into(),
&ctx,
),
Ok(fungible_multi_asset(Here.into(), 5).into()),
);
// not enough to purchase the combined weights
assert_err!(
trader.buy_weight(
Weight::from_parts(5, 5),
fungible_multi_asset(Here.into(), 5).into(),
&ctx,
),
XcmError::TooExpensive,
);
}
#[test]
fn errors_should_return_unused_weight() {
// we'll let them have message execution for free.
AllowUnpaidFrom::set(vec![Here.into()]);
// We own 1000 of our tokens.
add_asset(Here, (Here, 11u128));
let mut message = Xcm(vec![
// First xfer results in an error on the last message only
TransferAsset {
assets: (Here, 1u128).into(),
beneficiary: [AccountIndex64 { index: 3, network: None }].into(),
},
// Second xfer results in error third message and after
TransferAsset {
assets: (Here, 2u128).into(),
beneficiary: [AccountIndex64 { index: 3, network: None }].into(),
},
// Third xfer results in error second message and after
TransferAsset {
assets: (Here, 4u128).into(),
beneficiary: [AccountIndex64 { index: 3, network: None }].into(),
},
]);
// Weight limit of 70 is needed.
let limit = <TestConfig as Config>::Weigher::weight(&mut message, Weight::MAX).unwrap();
assert_eq!(limit, Weight::from_parts(30, 30));
let mut hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
Here,
message.clone(),
&mut hash,
limit,
Weight::zero(),
);
assert_eq!(r, Outcome::Complete { used: Weight::from_parts(30, 30) });
assert_eq!(asset_list(AccountIndex64 { index: 3, network: None }), vec![(Here, 7u128).into()]);
assert_eq!(asset_list(Here), vec![(Here, 4u128).into()]);
assert_eq!(sent_xcm(), vec![]);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
Here,
message.clone(),
&mut hash,
limit,
Weight::zero(),
);
assert_eq!(
r,
Outcome::Incomplete {
used: Weight::from_parts(30, 30),
error: InstructionError { index: 2, error: XcmError::NotWithdrawable },
}
);
assert_eq!(asset_list(AccountIndex64 { index: 3, network: None }), vec![(Here, 10u128).into()]);
assert_eq!(asset_list(Here), vec![(Here, 1u128).into()]);
assert_eq!(sent_xcm(), vec![]);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
Here,
message.clone(),
&mut hash,
limit,
Weight::zero(),
);
assert_eq!(
r,
Outcome::Incomplete {
used: Weight::from_parts(20, 20),
error: InstructionError { index: 1, error: XcmError::NotWithdrawable },
}
);
assert_eq!(asset_list(AccountIndex64 { index: 3, network: None }), vec![(Here, 11u128).into()]);
assert_eq!(asset_list(Here), vec![]);
assert_eq!(sent_xcm(), vec![]);
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
Here,
message,
&mut hash,
limit,
Weight::zero(),
);
assert_eq!(
r,
Outcome::Incomplete {
used: Weight::from_parts(10, 10),
error: InstructionError { index: 0, error: XcmError::NotWithdrawable },
}
);
assert_eq!(asset_list(AccountIndex64 { index: 3, network: None }), vec![(Here, 11u128).into()]);
assert_eq!(asset_list(Here), vec![]);
assert_eq!(sent_xcm(), vec![]);
}
#[test]
fn weight_bounds_should_respect_instructions_limit() {
use sp_tracing::capture_test_logs;
sp_tracing::init_for_tests();
MaxInstructions::set(3);
// 4 instructions are too many.
let log_capture = capture_test_logs!({
let mut message = Xcm(vec![ClearOrigin; 4]);
assert_eq!(
<TestConfig as Config>::Weigher::weight(&mut message, Weight::MAX),
Err(InstructionError { index: 3, error: XcmError::ExceedsStackLimit })
);
});
assert!(log_capture.contains(
"Weight calculation failed for message error=InstructionError { index: 3, error: ExceedsStackLimit } instructions_left=0 message_length=4"
));
let log_capture = capture_test_logs!({
let mut message =
Xcm(vec![SetErrorHandler(Xcm(vec![ClearOrigin])), SetAppendix(Xcm(vec![ClearOrigin]))]);
// 4 instructions are too many, even when hidden within 2.
assert_eq!(
<TestConfig as Config>::Weigher::weight(&mut message, Weight::MAX),
// We only include the index of the non-nested instruction.
Err(InstructionError { index: 1, error: XcmError::ExceedsStackLimit })
);
});
assert!(log_capture.contains(
"Weight calculation failed for message error=InstructionError { index: 1, error: ExceedsStackLimit } instructions_left=0 message_length=2"
));
let log_capture = capture_test_logs!({
let mut message =
Xcm(vec![SetErrorHandler(Xcm(vec![SetErrorHandler(Xcm(vec![SetErrorHandler(
Xcm(vec![ClearOrigin]),
)]))]))]);
// 4 instructions are too many, even when it's just one that's 3 levels deep.
assert_eq!(
<TestConfig as Config>::Weigher::weight(&mut message, Weight::MAX),
Err(InstructionError { index: 0, error: XcmError::ExceedsStackLimit })
);
});
assert!(log_capture.contains(
"Weight calculation failed for message error=InstructionError { index: 0, error: ExceedsStackLimit } instructions_left=0 message_length=1"
));
let log_capture = capture_test_logs!({
let mut message =
Xcm(vec![SetErrorHandler(Xcm(vec![SetErrorHandler(Xcm(vec![ClearOrigin]))]))]);
// 3 instructions are OK.
assert_eq!(
<TestConfig as Config>::Weigher::weight(&mut message, Weight::MAX),
Ok(Weight::from_parts(30, 30))
);
});
assert!(!log_capture.contains("Weight calculation failed for message"));
}
#[test]
fn weigher_returns_correct_instruction_index_on_error() {
// We have enough space for instructions.
MaxInstructions::set(10);
// But only enough weight for 3 instructions.
let max_weight = UnitWeightCost::get() * 3;
let mut message = Xcm(vec![ClearOrigin; 4]);
assert_eq!(
<TestConfig as Config>::Weigher::weight(&mut message, max_weight),
Err(InstructionError {
index: 3,
error: XcmError::WeightLimitReached(UnitWeightCost::get() * 4)
})
);
}
#[test]
fn weigher_weight_limit_correctly_accounts_for_nested_instructions() {
// We have enough space for instructions.
MaxInstructions::set(10);
// But only enough weight for 3 instructions.
let max_weight = UnitWeightCost::get() * 3;
let mut message = Xcm(vec![SetAppendix(Xcm(vec![ClearOrigin; 7]))]);
assert_eq!(
<TestConfig as Config>::Weigher::weight(&mut message, max_weight),
Err(InstructionError {
index: 0,
error: XcmError::WeightLimitReached(UnitWeightCost::get() * 4)
})
);
}
#[test]
fn weight_trader_tuple_should_work() {
let para_1: Location = Teyrchain(1).into();
let para_2: Location = Teyrchain(2).into();
parameter_types! {
pub static HereWeightPrice: (AssetId, u128, u128) =
(Here.into(), WEIGHT_REF_TIME_PER_SECOND.into(), WEIGHT_PROOF_SIZE_PER_MB.into());
pub static Para1WeightPrice: (AssetId, u128, u128) =
(Teyrchain(1).into(), WEIGHT_REF_TIME_PER_SECOND.into(), WEIGHT_PROOF_SIZE_PER_MB.into());
}
type Traders = (
// trader one
FixedRateOfFungible<HereWeightPrice, ()>,
// trader two
FixedRateOfFungible<Para1WeightPrice, ()>,
);
let mut traders = Traders::new();
let ctx = XcmContext { origin: None, message_id: XcmHash::default(), topic: None };
// trader one buys weight
assert_eq!(
traders.buy_weight(
Weight::from_parts(5, 5),
fungible_multi_asset(Here.into(), 10).into(),
&ctx
),
Ok(vec![].into()),
);
// trader one refunds
assert_eq!(
traders.refund_weight(Weight::from_parts(2, 2), &ctx),
Some(fungible_multi_asset(Here.into(), 4))
);
let mut traders = Traders::new();
// trader one failed; trader two buys weight
assert_eq!(
traders.buy_weight(
Weight::from_parts(5, 5),
fungible_multi_asset(para_1.clone(), 10).into(),
&ctx
),
Ok(vec![].into()),
);
// trader two refunds
assert_eq!(
traders.refund_weight(Weight::from_parts(2, 2), &ctx),
Some(fungible_multi_asset(para_1, 4))
);
let mut traders = Traders::new();
// all traders fails
assert_err!(
traders.buy_weight(Weight::from_parts(5, 5), fungible_multi_asset(para_2, 10).into(), &ctx),
XcmError::TooExpensive,
);
// and no refund
assert_eq!(traders.refund_weight(Weight::from_parts(2, 2), &ctx), None);
}
@@ -0,0 +1,43 @@
// Copyright 2021 Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
use frame_support::storage::{with_transaction, TransactionOutcome};
use sp_runtime::DispatchError;
use xcm::latest::prelude::*;
use xcm_executor::traits::ProcessTransaction;
/// Transactional processor implementation using frame transactional layers.
pub struct FrameTransactionalProcessor;
impl ProcessTransaction for FrameTransactionalProcessor {
const IS_TRANSACTIONAL: bool = true;
fn process<F>(f: F) -> Result<(), XcmError>
where
F: FnOnce() -> Result<(), XcmError>,
{
with_transaction(|| -> TransactionOutcome<Result<_, DispatchError>> {
let output = f();
match &output {
Ok(()) => TransactionOutcome::Commit(Ok(output)),
_ => TransactionOutcome::Rollback(Ok(output)),
}
})
.map_err(|error| {
tracing::debug!(target: "xcm::transactional", ?error, "Failed to process XCM transaction");
XcmError::ExceedsStackLimit
})?
}
}
+394
View File
@@ -0,0 +1,394 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
//! `TransferOverXcm` struct for paying through XCM and getting the status back.
use crate::LocatableAssetId;
use alloc::vec;
use core::{fmt::Debug, marker::PhantomData};
use frame_support::traits::Get;
use sp_runtime::traits::TryConvert;
use xcm::{latest::Error, opaque::lts::Weight, prelude::*};
use xcm_executor::traits::{FeeManager, FeeReason, QueryHandler, QueryResponseStatus};
pub use frame_support::traits::tokens::transfer::{Transfer, TransferStatus};
const LOG_TARGET: &str = "xcm::transfer_over_xcm";
/// Transfers an asset to a beneficiary on a remote chain via XCM.
///
/// The sender account on the destination chain is derived from the local account. For example:
///
/// `
/// Location::new(1, X2([Teyrchain(SourceParaId), from_location.interior]))
/// `
///
/// For a more specialized implementation, see [`super::pay::PayOverXcm`].
/// That variant assumes:
/// - The origin chain does **not** pay remote XCM execution fees on the destination chain.
/// - The sender account is fixed to a static `Interior` location of the origin chain.
///
/// The low-level XCM construction and configuration is handled by the generic
/// [`TransferOverXcmHelper`] type.
///
/// **NOTE**: It is assumed that the origin location has free execution on the remote chain
/// the `remote_fee` is `None.
pub struct TransferOverXcm<TransactorRefToLocation, TransferOverXcmHelper>(
PhantomData<(TransactorRefToLocation, TransferOverXcmHelper)>,
);
impl<TransactorRefToLocation, TransferOverXcmHelper> Transfer
for TransferOverXcm<TransactorRefToLocation, TransferOverXcmHelper>
where
TransferOverXcmHelper: TransferOverXcmHelperT<Balance = u128, QueryId = QueryId>,
TransactorRefToLocation: for<'a> TryConvert<&'a TransferOverXcmHelper::Beneficiary, Location>,
{
type Balance = u128;
type Sender = TransferOverXcmHelper::Beneficiary;
type Beneficiary = TransferOverXcmHelper::Beneficiary;
type AssetKind = TransferOverXcmHelper::AssetKind;
type RemoteFeeAsset = Asset;
type Id = TransferOverXcmHelper::QueryId;
type Error = Error;
fn transfer(
from: &Self::Sender,
to: &Self::Beneficiary,
asset_kind: Self::AssetKind,
amount: Self::Balance,
remote_fee: Option<Self::RemoteFeeAsset>,
) -> Result<Self::Id, Self::Error> {
let from_location = TransactorRefToLocation::try_convert(from).map_err(|error| {
tracing::error!(target: LOG_TARGET, ?error, "Failed to convert `Sender` to location");
Error::InvalidLocation
})?;
TransferOverXcmHelper::send_remote_transfer_xcm(
from_location.clone(),
to,
asset_kind,
amount,
remote_fee,
)
}
fn check_transfer(id: Self::Id) -> TransferStatus {
TransferOverXcmHelper::check_transfer(id)
}
#[cfg(feature = "runtime-benchmarks")]
fn ensure_successful(
beneficiary: &Self::Beneficiary,
asset_kind: Self::AssetKind,
balance: Self::Balance,
) {
TransferOverXcmHelper::ensure_successful(beneficiary, asset_kind, balance);
}
#[cfg(feature = "runtime-benchmarks")]
fn ensure_concluded(id: Self::Id) {
TransferOverXcmHelper::ensure_concluded(id);
}
}
/// Helper type to make a transfer on another chain via XCM.
pub struct TransferOverXcmHelper<
Router,
Querier,
XcmFeeHandler,
Timeout,
Transactor,
AssetKind,
AssetKindToLocatableAsset,
BeneficiaryRefToLocation,
>(
PhantomData<(
Router,
Querier,
XcmFeeHandler,
Timeout,
Transactor,
AssetKind,
AssetKindToLocatableAsset,
BeneficiaryRefToLocation,
)>,
);
/// Helper trait to abstract away the complexities of executing a remote transfer.
pub trait TransferOverXcmHelperT {
/// The beneficiary of a remote transfer.
type Beneficiary: Debug + Clone;
/// The type for the kinds of asset that are going to be paid.
type AssetKind: Debug + Clone;
/// The units of the currency.
type Balance: Debug + Clone;
/// The query id of an XCM Payment
type QueryId: Debug + Clone;
/// Construct and send a remote transfer xcm to the destination chain.
fn send_remote_transfer_xcm(
from_location: Location,
to: &Self::Beneficiary,
asset_kind: Self::AssetKind,
amount: Self::Balance,
remote_fee: Option<Asset>,
) -> Result<QueryId, Error>;
fn check_transfer(id: Self::QueryId) -> TransferStatus;
/// Ensure that a call to `send_remote_transfer_xcm` with the given parameters will be
/// successful if done immediately after this call. Used in benchmarking code.
#[cfg(feature = "runtime-benchmarks")]
fn ensure_successful(
beneficiary: &Self::Beneficiary,
asset_kind: Self::AssetKind,
balance: Self::Balance,
);
#[cfg(feature = "runtime-benchmarks")]
fn ensure_concluded(id: Self::QueryId);
}
impl<
Router: SendXcm,
Querier: QueryHandler,
XcmFeeHandler: FeeManager,
Timeout: Get<Querier::BlockNumber>,
Beneficiary: Clone + Debug,
AssetKind: Clone + Debug,
AssetKindToLocatableAsset: TryConvert<AssetKind, LocatableAssetId>,
BeneficiaryRefToLocation: for<'a> TryConvert<&'a Beneficiary, Location>,
> TransferOverXcmHelperT
for TransferOverXcmHelper<
Router,
Querier,
XcmFeeHandler,
Timeout,
Beneficiary,
AssetKind,
AssetKindToLocatableAsset,
BeneficiaryRefToLocation,
>
{
type Beneficiary = Beneficiary;
type AssetKind = AssetKind;
type Balance = u128;
type QueryId = QueryId;
/// Gets the XCM executing the transfer on the remote chain.
fn send_remote_transfer_xcm(
from_location: Location,
to: &Beneficiary,
asset_kind: AssetKind,
amount: Self::Balance,
remote_fee: Option<Asset>,
) -> Result<QueryId, Error> {
let locatable = Self::locatable_asset_id(asset_kind)?;
let LocatableAssetId { asset_id, location: asset_location } = locatable;
let origin_location_on_remote = Self::origin_location_on_remote(&asset_location)?;
let beneficiary = BeneficiaryRefToLocation::try_convert(to).map_err(|error| {
tracing::debug!(target: LOG_TARGET, ?error, "Failed to convert beneficiary to location");
Error::InvalidLocation
})?;
let query_id = Querier::new_query(
asset_location.clone(),
Timeout::get(),
from_location.interior.clone(),
);
let message = match remote_fee {
None => remote_transfer_xcm_free_execution(
from_location.clone(),
origin_location_on_remote,
beneficiary,
asset_id,
amount,
query_id,
)?,
Some(fee_asset) => remote_transfer_xcm_paying_fees(
from_location.clone(),
origin_location_on_remote,
beneficiary,
asset_id,
amount,
fee_asset,
query_id,
)?,
};
let (ticket, delivery_fees) =
Router::validate(&mut Some(asset_location), &mut Some(message))?;
Router::deliver(ticket)?;
if !XcmFeeHandler::is_waived(Some(&from_location), FeeReason::ChargeFees) {
XcmFeeHandler::handle_fee(delivery_fees, None, FeeReason::ChargeFees)
}
Ok(query_id)
}
fn check_transfer(id: Self::QueryId) -> TransferStatus {
use QueryResponseStatus::*;
match Querier::take_response(id) {
Ready { response, .. } => match response {
Response::ExecutionResult(None) => TransferStatus::Success,
Response::ExecutionResult(Some(_)) => TransferStatus::Failure,
_ => TransferStatus::Unknown,
},
Pending { .. } => TransferStatus::InProgress,
NotFound | UnexpectedVersion => TransferStatus::Unknown,
}
}
#[cfg(feature = "runtime-benchmarks")]
fn ensure_successful(_: &Self::Beneficiary, asset_kind: Self::AssetKind, _: Self::Balance) {
let locatable = AssetKindToLocatableAsset::try_convert(asset_kind).unwrap();
Router::ensure_successful_delivery(Some(locatable.location));
}
/// Ensure that a call to `check_payment` with the given parameters will return either `Success`
/// or `Failure`.
#[cfg(feature = "runtime-benchmarks")]
fn ensure_concluded(id: Self::QueryId) {
Querier::expect_response(id, Response::ExecutionResult(None));
}
}
impl<
Router,
Querier: QueryHandler,
XcmFeeHandler,
Timeout,
Beneficiary: Clone + Debug,
AssetKind: Clone + Debug,
AssetKindToLocatableAsset: TryConvert<AssetKind, LocatableAssetId>,
BeneficiaryRefToLocation: for<'a> TryConvert<&'a Beneficiary, Location>,
>
TransferOverXcmHelper<
Router,
Querier,
XcmFeeHandler,
Timeout,
Beneficiary,
AssetKind,
AssetKindToLocatableAsset,
BeneficiaryRefToLocation,
>
{
/// Returns the `from` relative to the asset's location.
///
/// This is the account that executes the transfer on the remote chain.
pub fn from_relative_to_asset_location(
from: &Beneficiary,
asset_kind: AssetKind,
) -> Result<Location, Error> {
let from_location = BeneficiaryRefToLocation::try_convert(from).map_err(|error| {
tracing::error!(target: LOG_TARGET, ?error, "Failed to convert from to location");
Error::InvalidLocation
})?;
let locatable = Self::locatable_asset_id(asset_kind)?;
let origin_location_on_remote = Self::origin_location_on_remote(&locatable.location)?;
append_from_to_target(from_location, origin_location_on_remote)
}
fn origin_location_on_remote(asset_location: &Location) -> Result<Location, Error> {
let origin_on_remote =
Querier::UniversalLocation::get().invert_target(asset_location).map_err(|()| {
tracing::debug!(target: LOG_TARGET, "Failed to invert asset location");
Error::LocationNotInvertible
})?;
tracing::trace!(target: LOG_TARGET, ?origin_on_remote, "Origin on destination");
Ok(origin_on_remote)
}
fn locatable_asset_id(asset_kind: AssetKind) -> Result<LocatableAssetId, Error> {
AssetKindToLocatableAsset::try_convert(asset_kind).map_err(|error| {
tracing::debug!(target: LOG_TARGET, ?error, "Failed to convert asset kind to locatable asset");
Error::InvalidLocation
})
}
}
fn remote_transfer_xcm_paying_fees(
from_location: Location,
origin_relative_to_remote: Location,
beneficiary: Location,
asset_id: AssetId,
amount: u128,
remote_fee: Asset,
query_id: QueryId,
) -> Result<Xcm<()>, Error> {
// Transform `from` into Location::new(1, XX([Teyrchain(source), from.interior }])
// We need this one for the refunds.
let from_at_target =
append_from_to_target(from_location.clone(), origin_relative_to_remote.clone())?;
tracing::trace!(target: LOG_TARGET, ?from_at_target, "From at target");
let xcm = Xcm(vec![
// Transform origin into Location::new(1, X2([Teyrchain(SourceParaId), from.interior }])
DescendOrigin(from_location.interior.clone()),
WithdrawAsset(vec![remote_fee.clone()].into()),
PayFees { asset: remote_fee },
SetAppendix(Xcm(vec![
ReportError(QueryResponseInfo {
destination: origin_relative_to_remote.clone(),
query_id,
max_weight: Weight::MAX,
}),
RefundSurplus,
DepositAsset { assets: AssetFilter::Wild(WildAsset::All), beneficiary: from_at_target },
])),
TransferAsset { beneficiary, assets: (asset_id, amount).into() },
]);
Ok(xcm)
}
fn remote_transfer_xcm_free_execution(
from_location: Location,
origin_relative_to_remote: Location,
beneficiary: Location,
asset_id: AssetId,
amount: u128,
query_id: QueryId,
) -> Result<Xcm<()>, Error> {
let xcm = Xcm(vec![
DescendOrigin(from_location.interior),
UnpaidExecution { weight_limit: Unlimited, check_origin: None },
SetAppendix(Xcm(vec![
SetFeesMode { jit_withdraw: true },
ReportError(QueryResponseInfo {
destination: origin_relative_to_remote,
query_id,
max_weight: Weight::zero(),
}),
])),
TransferAsset {
beneficiary,
assets: vec![Asset { id: asset_id, fun: Fungibility::Fungible(amount) }].into(),
},
]);
Ok(xcm)
}
fn append_from_to_target(from: Location, target: Location) -> Result<Location, Error> {
let from_at_target = target.appended_with(from).map_err(|_| Error::LocationFull)?;
Ok(from_at_target)
}
@@ -0,0 +1,166 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
use core::marker::PhantomData;
use frame_support::traits::tokens::asset_ops::{
common_strategies::{
ChangeOwnerFrom, ConfigValue, DeriveAndReportId, IfOwnedBy, Owner, WithConfig,
WithConfigValue,
},
AssetDefinition, Create, Restore, Stash, Update,
};
use xcm::latest::prelude::*;
use xcm_executor::traits::{ConvertLocation, Error as MatchError, MatchesInstance, TransactAsset};
use super::NonFungibleAsset;
const LOG_TARGET: &str = "xcm::unique_instances";
/// The `UniqueInstancesAdapter` implements the [`TransactAsset`] for existing unique instances
/// (NFT-like entities), for which the `Matcher` can deduce the instance ID from the XCM
/// [`AssetId`].
///
/// The adapter uses the following asset operations:
/// * [`Restore`] with the strategy to restore the instance to a given owner.
/// * [`Update`] with the strategy to change the instance's owner from one to another.
/// * [`Stash`] with the strategy to check the current owner before stashing.
///
/// Note on teleports: This adapter doesn't implement teleports since unique instances have
/// associated data that also should be teleported. Currently, neither XCM can transfer such data
/// nor does a standard approach exist in the ecosystem for this use case.
pub struct UniqueInstancesAdapter<AccountId, AccountIdConverter, Matcher, InstanceOps>(
PhantomData<(AccountId, AccountIdConverter, Matcher, InstanceOps)>,
);
impl<AccountId, AccountIdConverter, Matcher, InstanceOps> TransactAsset
for UniqueInstancesAdapter<AccountId, AccountIdConverter, Matcher, InstanceOps>
where
AccountId: 'static,
AccountIdConverter: ConvertLocation<AccountId>,
Matcher: MatchesInstance<InstanceOps::Id>,
InstanceOps: AssetDefinition
+ Restore<WithConfig<ConfigValue<Owner<AccountId>>>>
+ Update<ChangeOwnerFrom<AccountId>>
+ Stash<IfOwnedBy<AccountId>>,
{
fn deposit_asset(what: &Asset, who: &Location, context: Option<&XcmContext>) -> XcmResult {
tracing::trace!(
target: LOG_TARGET,
?what,
?who,
?context,
"deposit_asset",
);
let instance_id = Matcher::matches_instance(what)?;
let who = AccountIdConverter::convert_location(who)
.ok_or(MatchError::AccountIdConversionFailed)?;
InstanceOps::restore(&instance_id, WithConfig::from(Owner::with_config_value(who)))
.map_err(|e| XcmError::FailedToTransactAsset(e.into()))
}
fn withdraw_asset(
what: &Asset,
who: &Location,
maybe_context: Option<&XcmContext>,
) -> Result<xcm_executor::AssetsInHolding, XcmError> {
tracing::trace!(
target: LOG_TARGET,
?what,
?who,
?maybe_context,
"withdraw_asset",
);
let instance_id = Matcher::matches_instance(what)?;
let who = AccountIdConverter::convert_location(who)
.ok_or(MatchError::AccountIdConversionFailed)?;
InstanceOps::stash(&instance_id, IfOwnedBy::check(who))
.map_err(|e| XcmError::FailedToTransactAsset(e.into()))?;
Ok(what.clone().into())
}
fn internal_transfer_asset(
what: &Asset,
from: &Location,
to: &Location,
context: &XcmContext,
) -> Result<xcm_executor::AssetsInHolding, XcmError> {
tracing::trace!(
target: LOG_TARGET,
?what,
?from,
?to,
?context,
"internal_transfer_asset",
);
let instance_id = Matcher::matches_instance(what)?;
let from = AccountIdConverter::convert_location(from)
.ok_or(MatchError::AccountIdConversionFailed)?;
let to = AccountIdConverter::convert_location(to)
.ok_or(MatchError::AccountIdConversionFailed)?;
InstanceOps::update(&instance_id, ChangeOwnerFrom::check(from), &to)
.map_err(|e| XcmError::FailedToTransactAsset(e.into()))?;
Ok(what.clone().into())
}
}
/// The `UniqueInstancesDepositAdapter` implements the [`TransactAsset`] to create unique instances
/// (NFT-like entities), for which no `Matcher` can deduce the instance ID from the XCM
/// [`AssetId`]. Instead, this adapter requires the `InstanceCreateOp` to create an instance using
/// [`NonFungibleAsset`] as ID derivation parameter.
pub struct UniqueInstancesDepositAdapter<AccountId, AccountIdConverter, Id, InstanceCreateOp>(
PhantomData<(AccountId, AccountIdConverter, Id, InstanceCreateOp)>,
);
impl<AccountId, AccountIdConverter, Id, InstanceCreateOp> TransactAsset
for UniqueInstancesDepositAdapter<AccountId, AccountIdConverter, Id, InstanceCreateOp>
where
AccountIdConverter: ConvertLocation<AccountId>,
InstanceCreateOp:
Create<WithConfig<ConfigValue<Owner<AccountId>>, DeriveAndReportId<NonFungibleAsset, Id>>>,
{
fn deposit_asset(what: &Asset, who: &Location, context: Option<&XcmContext>) -> XcmResult {
tracing::trace!(
target: LOG_TARGET,
?what,
?who,
?context,
"deposit_asset",
);
let asset = match what.fun {
Fungibility::NonFungible(asset_instance) => (what.id.clone(), asset_instance),
_ => return Err(MatchError::AssetNotHandled.into()),
};
let who = AccountIdConverter::convert_location(who)
.ok_or(MatchError::AccountIdConversionFailed)?;
InstanceCreateOp::create(WithConfig::new(
Owner::with_config_value(who),
DeriveAndReportId::from(asset),
))
.map(|_reported_id| ())
.map_err(|e| XcmError::FailedToTransactAsset(e.into()))
}
}
@@ -0,0 +1,41 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
//! XCM utilities to work with NFT-like entities (unique instances).
//! The adapters and other utility types use the
//! [`asset_ops`](frame_support::traits::tokens::asset_ops) traits.
use sp_runtime::{traits::Convert, DispatchError};
use xcm::latest::prelude::*;
pub mod adapter;
pub use adapter::*;
/// An XCM ID for unique instances (non-fungible assets).
pub type NonFungibleAsset = (AssetId, AssetInstance);
/// Gets the XCM [AssetId] (i.e., extracts the NFT collection ID) from the [NonFungibleAsset].
pub struct ExtractAssetId;
impl Convert<NonFungibleAsset, AssetId> for ExtractAssetId {
fn convert((asset_id, _): NonFungibleAsset) -> AssetId {
asset_id
}
}
impl Convert<NonFungibleAsset, Result<AssetId, DispatchError>> for ExtractAssetId {
fn convert((asset_id, _): NonFungibleAsset) -> Result<AssetId, DispatchError> {
Ok(asset_id)
}
}
File diff suppressed because it is too large Load Diff
+370
View File
@@ -0,0 +1,370 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
use codec::Decode;
use core::{marker::PhantomData, result::Result};
use frame_support::{
dispatch::GetDispatchInfo,
traits::{
fungible::{Balanced, Credit, Inspect},
Get, OnUnbalanced as OnUnbalancedT,
},
weights::{
constants::{WEIGHT_PROOF_SIZE_PER_MB, WEIGHT_REF_TIME_PER_SECOND},
WeightToFee as WeightToFeeT,
},
};
use sp_runtime::traits::{SaturatedConversion, Saturating, Zero};
use xcm::latest::{prelude::*, GetWeight, Weight};
use xcm_executor::{
traits::{WeightBounds, WeightTrader},
AssetsInHolding,
};
pub struct FixedWeightBounds<T, C, M>(PhantomData<(T, C, M)>);
impl<T: Get<Weight>, C: Decode + GetDispatchInfo, M: Get<u32>> WeightBounds<C>
for FixedWeightBounds<T, C, M>
{
fn weight(message: &mut Xcm<C>, weight_limit: Weight) -> Result<Weight, InstructionError> {
tracing::trace!(target: "xcm::weight", ?message, "FixedWeightBounds");
let mut instructions_left = M::get();
Self::weight_with_limit(message, &mut instructions_left, weight_limit).inspect_err(
|&error| {
tracing::debug!(
target: "xcm::weight",
?error,
?instructions_left,
message_length = ?message.0.len(),
"Weight calculation failed for message"
);
},
)
}
fn instr_weight(instruction: &mut Instruction<C>) -> Result<Weight, XcmError> {
let mut max_value = u32::MAX;
Self::instr_weight_with_limit(instruction, &mut max_value, Weight::MAX).inspect_err(
|&error| {
tracing::debug!(
target: "xcm::weight",
?error,
?instruction,
instrs_limit = ?max_value,
"Weight calculation failed for instruction"
);
},
)
}
}
impl<T: Get<Weight>, C: Decode + GetDispatchInfo, M> FixedWeightBounds<T, C, M> {
fn weight_with_limit(
message: &mut Xcm<C>,
instructions_left: &mut u32,
weight_limit: Weight,
) -> Result<Weight, InstructionError> {
let mut total_weight: Weight = Weight::zero();
for (index, instruction) in message.0.iter_mut().enumerate() {
let index = index.try_into().unwrap_or(InstructionIndex::MAX);
*instructions_left = instructions_left
.checked_sub(1)
.ok_or_else(|| InstructionError { index, error: XcmError::ExceedsStackLimit })?;
let instruction_weight =
&Self::instr_weight_with_limit(instruction, instructions_left, weight_limit)
.map_err(|error| InstructionError { index, error })?;
total_weight = total_weight
.checked_add(instruction_weight)
.ok_or(InstructionError { index, error: XcmError::Overflow })?;
if total_weight.any_gt(weight_limit) {
return Err(InstructionError {
index,
error: XcmError::WeightLimitReached(total_weight),
});
}
}
Ok(total_weight)
}
fn instr_weight_with_limit(
instruction: &mut Instruction<C>,
instructions_left: &mut u32,
weight_limit: Weight,
) -> Result<Weight, XcmError> {
let instruction_weight = match instruction {
Transact { ref mut call, .. } =>
call.ensure_decoded()
.map_err(|_| XcmError::FailedToDecode)?
.get_dispatch_info()
.call_weight,
SetErrorHandler(xcm) | SetAppendix(xcm) | ExecuteWithOrigin { xcm, .. } =>
Self::weight_with_limit(xcm, instructions_left, weight_limit)
.map_err(|outcome_error| outcome_error.error)?,
_ => Weight::zero(),
};
let total_weight = T::get().checked_add(&instruction_weight).ok_or(XcmError::Overflow)?;
Ok(total_weight)
}
}
pub struct WeightInfoBounds<W, C, M>(PhantomData<(W, C, M)>);
impl<W, C, M> WeightBounds<C> for WeightInfoBounds<W, C, M>
where
W: XcmWeightInfo<C>,
C: Decode + GetDispatchInfo,
M: Get<u32>,
Instruction<C>: xcm::latest::GetWeight<W>,
{
fn weight(message: &mut Xcm<C>, weight_limit: Weight) -> Result<Weight, InstructionError> {
tracing::trace!(target: "xcm::weight", ?message, "WeightInfoBounds");
let mut instructions_left = M::get();
Self::weight_with_limit(message, &mut instructions_left, weight_limit).inspect_err(
|&error| {
tracing::debug!(
target: "xcm::weight",
?error,
?instructions_left,
message_length = ?message.0.len(),
"Weight calculation failed for message"
);
},
)
}
fn instr_weight(instruction: &mut Instruction<C>) -> Result<Weight, XcmError> {
let mut max_value = u32::MAX;
Self::instr_weight_with_limit(instruction, &mut max_value, Weight::MAX).inspect_err(
|&error| {
tracing::debug!(
target: "xcm::weight",
?error,
?instruction,
instrs_limit = ?max_value,
"Weight calculation failed for instruction"
);
},
)
}
}
impl<W, C, M> WeightInfoBounds<W, C, M>
where
W: XcmWeightInfo<C>,
C: Decode + GetDispatchInfo,
M: Get<u32>,
Instruction<C>: xcm::latest::GetWeight<W>,
{
fn weight_with_limit(
message: &mut Xcm<C>,
instructions_left: &mut u32,
weight_limit: Weight,
) -> Result<Weight, InstructionError> {
let mut total_weight: Weight = Weight::zero();
for (index, instruction) in message.0.iter_mut().enumerate() {
let index = index.try_into().unwrap_or(u8::MAX);
*instructions_left = instructions_left
.checked_sub(1)
.ok_or_else(|| InstructionError { index, error: XcmError::ExceedsStackLimit })?;
let instruction_weight =
&Self::instr_weight_with_limit(instruction, instructions_left, weight_limit)
.map_err(|error| InstructionError { index, error })?;
total_weight = total_weight
.checked_add(instruction_weight)
.ok_or(InstructionError { index, error: XcmError::Overflow })?;
if total_weight.any_gt(weight_limit) {
return Err(InstructionError {
index,
error: XcmError::WeightLimitReached(total_weight),
});
}
}
Ok(total_weight)
}
fn instr_weight_with_limit(
instruction: &mut Instruction<C>,
instructions_left: &mut u32,
weight_limit: Weight,
) -> Result<Weight, XcmError> {
let instruction_weight = match instruction {
Transact { ref mut call, .. } =>
call.ensure_decoded()
.map_err(|_| XcmError::FailedToDecode)?
.get_dispatch_info()
.call_weight,
SetErrorHandler(xcm) | SetAppendix(xcm) =>
Self::weight_with_limit(xcm, instructions_left, weight_limit)
.map_err(|outcome_error| outcome_error.error)?,
_ => Weight::zero(),
};
let total_weight = instruction
.weight()
.checked_add(&instruction_weight)
.ok_or(XcmError::Overflow)?;
Ok(total_weight)
}
}
/// Function trait for handling some revenue. Similar to a negative imbalance (credit) handler, but
/// for a `Asset`. Sensible implementations will deposit the asset in some known treasury or
/// block-author account.
pub trait TakeRevenue {
/// Do something with the given `revenue`, which is a single non-wildcard `Asset`.
fn take_revenue(revenue: Asset);
}
/// Null implementation just burns the revenue.
impl TakeRevenue for () {
fn take_revenue(_revenue: Asset) {}
}
/// Simple fee calculator that requires payment in a single fungible at a fixed rate.
///
/// The constant `Get` type parameter should be the fungible ID, the amount of it required for one
/// second of weight and the amount required for 1 MB of proof.
pub struct FixedRateOfFungible<T: Get<(AssetId, u128, u128)>, R: TakeRevenue>(
Weight,
u128,
PhantomData<(T, R)>,
);
impl<T: Get<(AssetId, u128, u128)>, R: TakeRevenue> WeightTrader for FixedRateOfFungible<T, R> {
fn new() -> Self {
Self(Weight::zero(), 0, PhantomData)
}
fn buy_weight(
&mut self,
weight: Weight,
payment: AssetsInHolding,
context: &XcmContext,
) -> Result<AssetsInHolding, XcmError> {
let (id, units_per_second, units_per_mb) = T::get();
tracing::trace!(
target: "xcm::weight",
?id, ?weight, ?payment, ?context,
"FixedRateOfFungible::buy_weight",
);
let amount = (units_per_second * (weight.ref_time() as u128) /
(WEIGHT_REF_TIME_PER_SECOND as u128)) +
(units_per_mb * (weight.proof_size() as u128) / (WEIGHT_PROOF_SIZE_PER_MB as u128));
if amount == 0 {
return Ok(payment);
}
let unused = payment.checked_sub((id, amount).into()).map_err(|error| {
tracing::error!(target: "xcm::weight", ?amount, ?error, "FixedRateOfFungible::buy_weight Failed to substract from payment");
XcmError::TooExpensive
})?;
self.0 = self.0.saturating_add(weight);
self.1 = self.1.saturating_add(amount);
Ok(unused)
}
fn refund_weight(&mut self, weight: Weight, context: &XcmContext) -> Option<Asset> {
let (id, units_per_second, units_per_mb) = T::get();
tracing::trace!(target: "xcm::weight", ?id, ?weight, ?context, "FixedRateOfFungible::refund_weight");
let weight = weight.min(self.0);
let amount = (units_per_second * (weight.ref_time() as u128) /
(WEIGHT_REF_TIME_PER_SECOND as u128)) +
(units_per_mb * (weight.proof_size() as u128) / (WEIGHT_PROOF_SIZE_PER_MB as u128));
self.0 -= weight;
self.1 = self.1.saturating_sub(amount);
if amount > 0 {
Some((id, amount).into())
} else {
None
}
}
}
impl<T: Get<(AssetId, u128, u128)>, R: TakeRevenue> Drop for FixedRateOfFungible<T, R> {
fn drop(&mut self) {
if self.1 > 0 {
R::take_revenue((T::get().0, self.1).into());
}
}
}
/// Weight trader which uses the configured `WeightToFee` to set the right price for weight and then
/// places any weight bought into the right account.
pub struct UsingComponents<
WeightToFee: WeightToFeeT<Balance = <Fungible as Inspect<AccountId>>::Balance>,
AssetIdValue: Get<Location>,
AccountId,
Fungible: Balanced<AccountId> + Inspect<AccountId>,
OnUnbalanced: OnUnbalancedT<Credit<AccountId, Fungible>>,
>(
Weight,
Fungible::Balance,
PhantomData<(WeightToFee, AssetIdValue, AccountId, Fungible, OnUnbalanced)>,
);
impl<
WeightToFee: WeightToFeeT<Balance = <Fungible as Inspect<AccountId>>::Balance>,
AssetIdValue: Get<Location>,
AccountId,
Fungible: Balanced<AccountId> + Inspect<AccountId>,
OnUnbalanced: OnUnbalancedT<Credit<AccountId, Fungible>>,
> WeightTrader for UsingComponents<WeightToFee, AssetIdValue, AccountId, Fungible, OnUnbalanced>
{
fn new() -> Self {
Self(Weight::zero(), Zero::zero(), PhantomData)
}
fn buy_weight(
&mut self,
weight: Weight,
payment: AssetsInHolding,
context: &XcmContext,
) -> Result<AssetsInHolding, XcmError> {
tracing::trace!(target: "xcm::weight", ?weight, ?payment, ?context, "UsingComponents::buy_weight");
let amount = WeightToFee::weight_to_fee(&weight);
let u128_amount: u128 = amount.try_into().map_err(|_| {
tracing::debug!(target: "xcm::weight", ?amount, "Weight fee could not be converted");
XcmError::Overflow
})?;
let required = Asset { id: AssetId(AssetIdValue::get()), fun: Fungible(u128_amount) };
let unused = payment.checked_sub(required).map_err(|error| {
tracing::debug!(target: "xcm::weight", ?error, "Failed to substract from payment");
XcmError::TooExpensive
})?;
self.0 = self.0.saturating_add(weight);
self.1 = self.1.saturating_add(amount);
Ok(unused)
}
fn refund_weight(&mut self, weight: Weight, context: &XcmContext) -> Option<Asset> {
tracing::trace!(target: "xcm::weight", ?weight, ?context, available_weight = ?self.0, available_amount = ?self.1, "UsingComponents::refund_weight");
let weight = weight.min(self.0);
let amount = WeightToFee::weight_to_fee(&weight);
self.0 -= weight;
self.1 = self.1.saturating_sub(amount);
let amount: u128 = amount.saturated_into();
tracing::trace!(target: "xcm::weight", ?amount, "UsingComponents::refund_weight");
if amount > 0 {
Some((AssetIdValue::get(), amount).into())
} else {
None
}
}
}
impl<
WeightToFee: WeightToFeeT<Balance = <Fungible as Inspect<AccountId>>::Balance>,
AssetId: Get<Location>,
AccountId,
Fungible: Balanced<AccountId> + Inspect<AccountId>,
OnUnbalanced: OnUnbalancedT<Credit<AccountId, Fungible>>,
> Drop for UsingComponents<WeightToFee, AssetId, AccountId, Fungible, OnUnbalanced>
{
fn drop(&mut self) {
OnUnbalanced::on_unbalanced(Fungible::issue(self.1));
}
}
+258
View File
@@ -0,0 +1,258 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
use core::cell::RefCell;
use frame_support::{
construct_runtime, derive_impl, parameter_types,
traits::{Disabled, Everything, Nothing},
weights::Weight,
};
use frame_system::EnsureRoot;
use primitive_types::H256;
use sp_runtime::{traits::IdentityLookup, AccountId32, BuildStorage};
use pezkuwi_runtime_teyrchains::{configuration, origin, shared};
use pezkuwi_teyrchain_primitives::primitives::Id as ParaId;
use xcm::latest::{opaque, prelude::*};
use xcm_executor::XcmExecutor;
use staging_xcm_builder as xcm_builder;
use xcm_builder::{
AccountId32Aliases, AllowTopLevelPaidExecutionFrom, AllowUnpaidExecutionFrom,
ChildSystemTeyrchainAsSuperuser, ChildTeyrchainAsNative, ChildTeyrchainConvertsVia,
EnsureDecodableXcm, FixedRateOfFungible, FixedWeightBounds, FungibleAdapter,
IsChildSystemTeyrchain, IsConcrete, MintLocation, RespectSuspension, SignedAccountId32AsNative,
SignedToAccountId32, SovereignSignedViaLocation, TakeWeightCredit,
};
use xcm_simulator::helpers::derive_topic_id;
pub type AccountId = AccountId32;
pub type Balance = u128;
thread_local! {
pub static SENT_XCM: RefCell<Vec<(Location, opaque::Xcm, XcmHash)>> = RefCell::new(Vec::new());
}
pub fn sent_xcm() -> Vec<(Location, opaque::Xcm, XcmHash)> {
SENT_XCM.with(|q| (*q.borrow()).clone())
}
pub struct TestSendXcm;
impl SendXcm for TestSendXcm {
type Ticket = (Location, Xcm<()>, XcmHash);
fn validate(
dest: &mut Option<Location>,
msg: &mut Option<Xcm<()>>,
) -> SendResult<(Location, Xcm<()>, XcmHash)> {
let msg = msg.take().unwrap();
let hash = derive_topic_id(&msg);
let triplet = (dest.take().unwrap(), msg, hash);
Ok((triplet, Assets::new()))
}
fn deliver(triplet: (Location, Xcm<()>, XcmHash)) -> Result<XcmHash, SendError> {
let hash = triplet.2;
SENT_XCM.with(|q| q.borrow_mut().push(triplet));
Ok(hash)
}
}
pub type TestXcmRouter = EnsureDecodableXcm<TestSendXcm>;
// copied from kusama constants
pub const UNITS: Balance = 1_000_000_000_000;
pub const CENTS: Balance = UNITS / 30_000;
#[derive_impl(frame_system::config_preludes::TestDefaultConfig)]
impl frame_system::Config for Runtime {
type RuntimeOrigin = RuntimeOrigin;
type RuntimeCall = RuntimeCall;
type Nonce = u64;
type Hash = H256;
type Hashing = ::sp_runtime::traits::BlakeTwo256;
type AccountId = AccountId;
type Lookup = IdentityLookup<Self::AccountId>;
type Block = Block;
type RuntimeEvent = RuntimeEvent;
type BlockWeights = ();
type BlockLength = ();
type Version = ();
type PalletInfo = PalletInfo;
type AccountData = pallet_balances::AccountData<Balance>;
type OnNewAccount = ();
type OnKilledAccount = ();
type DbWeight = ();
type BaseCallFilter = Everything;
type SystemWeightInfo = ();
type SS58Prefix = ();
type OnSetCode = ();
type MaxConsumers = frame_support::traits::ConstU32<16>;
}
parameter_types! {
pub ExistentialDeposit: Balance = 1 * CENTS;
}
#[derive_impl(pallet_balances::config_preludes::TestDefaultConfig)]
impl pallet_balances::Config for Runtime {
type Balance = Balance;
type ExistentialDeposit = ExistentialDeposit;
type AccountStore = System;
type ReserveIdentifier = [u8; 8];
}
impl shared::Config for Runtime {
type DisabledValidators = ();
}
impl configuration::Config for Runtime {
type WeightInfo = configuration::TestWeightInfo;
}
// aims to closely emulate the Kusama XcmConfig
parameter_types! {
pub const KsmLocation: Location = Location::here();
pub const KusamaNetwork: NetworkId = NetworkId::Kusama;
pub UniversalLocation: InteriorLocation = KusamaNetwork::get().into();
pub CheckAccount: (AccountId, MintLocation) = (XcmPallet::check_account(), MintLocation::Local);
}
pub type SovereignAccountOf =
(ChildTeyrchainConvertsVia<ParaId, AccountId>, AccountId32Aliases<KusamaNetwork, AccountId>);
pub type LocalCurrencyAdapter =
FungibleAdapter<Balances, IsConcrete<KsmLocation>, SovereignAccountOf, AccountId, CheckAccount>;
pub type LocalAssetTransactor = (LocalCurrencyAdapter,);
type LocalOriginConverter = (
SovereignSignedViaLocation<SovereignAccountOf, RuntimeOrigin>,
ChildTeyrchainAsNative<origin::Origin, RuntimeOrigin>,
SignedAccountId32AsNative<KusamaNetwork, RuntimeOrigin>,
ChildSystemTeyrchainAsSuperuser<ParaId, RuntimeOrigin>,
);
parameter_types! {
pub const BaseXcmWeight: Weight = Weight::from_parts(1_000_000_000, 1024);
pub KsmPerSecondPerByte: (AssetId, u128, u128) = (KsmLocation::get().into(), 1, 1);
}
pub type Barrier = (
TakeWeightCredit,
AllowTopLevelPaidExecutionFrom<Everything>,
// Unused/Untested
AllowUnpaidExecutionFrom<IsChildSystemTeyrchain<ParaId>>,
);
parameter_types! {
pub KusamaForAssetHub: (AssetFilter, Location) =
(Wild(AllOf { id: AssetId(Here.into()), fun: WildFungible }), Teyrchain(1000).into());
pub const MaxInstructions: u32 = 100;
pub const MaxAssetsIntoHolding: u32 = 4;
}
pub type TrustedTeleporters = (xcm_builder::Case<KusamaForAssetHub>,);
pub struct XcmConfig;
impl xcm_executor::Config for XcmConfig {
type RuntimeCall = RuntimeCall;
type XcmSender = TestXcmRouter;
type XcmEventEmitter = XcmPallet;
type AssetTransactor = LocalAssetTransactor;
type OriginConverter = LocalOriginConverter;
type IsReserve = ();
type IsTeleporter = TrustedTeleporters;
type UniversalLocation = UniversalLocation;
type Barrier = RespectSuspension<Barrier, XcmPallet>;
type Weigher = FixedWeightBounds<BaseXcmWeight, RuntimeCall, MaxInstructions>;
type Trader = FixedRateOfFungible<KsmPerSecondPerByte, ()>;
type ResponseHandler = XcmPallet;
type AssetTrap = XcmPallet;
type AssetLocker = ();
type AssetExchanger = ();
type AssetClaims = XcmPallet;
type SubscriptionService = XcmPallet;
type PalletInstancesInfo = AllPalletsWithSystem;
type MaxAssetsIntoHolding = MaxAssetsIntoHolding;
type FeeManager = ();
type MessageExporter = ();
type UniversalAliases = Nothing;
type CallDispatcher = RuntimeCall;
type SafeCallFilter = Everything;
type Aliasers = Nothing;
type TransactionalProcessor = ();
type HrmpNewChannelOpenRequestHandler = ();
type HrmpChannelAcceptedHandler = ();
type HrmpChannelClosingHandler = ();
type XcmRecorder = XcmPallet;
}
/// Converts a local signed origin into an XCM location. Forms the basis for local origins
/// sending/executing XCMs.
pub type LocalOriginToLocation = SignedToAccountId32<RuntimeOrigin, AccountId, KusamaNetwork>;
impl pallet_xcm::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type UniversalLocation = UniversalLocation;
type SendXcmOrigin = xcm_builder::EnsureXcmOrigin<RuntimeOrigin, LocalOriginToLocation>;
type XcmRouter = TestXcmRouter;
// Anyone can execute XCM messages locally...
type ExecuteXcmOrigin = xcm_builder::EnsureXcmOrigin<RuntimeOrigin, LocalOriginToLocation>;
type XcmExecuteFilter = Nothing;
type XcmExecutor = XcmExecutor<XcmConfig>;
type XcmTeleportFilter = Everything;
type XcmReserveTransferFilter = Everything;
type Weigher = FixedWeightBounds<BaseXcmWeight, RuntimeCall, MaxInstructions>;
type RuntimeCall = RuntimeCall;
type RuntimeOrigin = RuntimeOrigin;
const VERSION_DISCOVERY_QUEUE_SIZE: u32 = 100;
type AdvertisedXcmVersion = pallet_xcm::CurrentXcmVersion;
type TrustedLockers = ();
type SovereignAccountOf = ();
type Currency = Balances;
type CurrencyMatcher = IsConcrete<KsmLocation>;
type MaxLockers = frame_support::traits::ConstU32<8>;
type MaxRemoteLockConsumers = frame_support::traits::ConstU32<0>;
type RemoteLockConsumerIdentifier = ();
type WeightInfo = pallet_xcm::TestWeightInfo;
type AdminOrigin = EnsureRoot<AccountId>;
// Aliasing is disabled: xcm_executor::Config::Aliasers is set to `Nothing`.
type AuthorizedAliasConsideration = Disabled;
}
impl origin::Config for Runtime {}
type Block = frame_system::mocking::MockBlock<Runtime>;
construct_runtime!(
pub enum Runtime
{
System: frame_system,
Balances: pallet_balances,
ParasOrigin: origin,
XcmPallet: pallet_xcm,
}
);
pub fn kusama_like_with_balances(balances: Vec<(AccountId, Balance)>) -> sp_io::TestExternalities {
let mut t = frame_system::GenesisConfig::<Runtime>::default().build_storage().unwrap();
pallet_balances::GenesisConfig::<Runtime> { balances, ..Default::default() }
.assimilate_storage(&mut t)
.unwrap();
let mut ext = sp_io::TestExternalities::new(t);
ext.execute_with(|| System::set_block_number(1));
ext
}
+432
View File
@@ -0,0 +1,432 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Pezkuwi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pezkuwi. If not, see <http://www.gnu.org/licenses/>.
mod mock;
use mock::{
kusama_like_with_balances, AccountId, Balance, Balances, BaseXcmWeight, System, XcmConfig,
CENTS,
};
use pezkuwi_teyrchain_primitives::primitives::Id as ParaId;
use sp_runtime::traits::AccountIdConversion;
use xcm::latest::{prelude::*, Error::UntrustedTeleportLocation};
use xcm_executor::XcmExecutor;
use xcm_simulator::fake_message_hash;
pub const ALICE: AccountId = AccountId::new([0u8; 32]);
pub const PARA_ID: u32 = 2000;
pub const INITIAL_BALANCE: u128 = 100_000_000_000;
pub const REGISTER_AMOUNT: Balance = 10 * CENTS;
// Construct a `BuyExecution` order.
fn buy_execution<C>() -> Instruction<C> {
BuyExecution { fees: (Here, REGISTER_AMOUNT).into(), weight_limit: Unlimited }
}
/// Scenario:
/// A teyrchain transfers funds on the relay-chain to another teyrchain's account.
///
/// Asserts that the teyrchain accounts are updated as expected.
#[test]
fn withdraw_and_deposit_works() {
let para_acc: AccountId = ParaId::from(PARA_ID).into_account_truncating();
let balances = vec![(ALICE, INITIAL_BALANCE), (para_acc.clone(), INITIAL_BALANCE)];
kusama_like_with_balances(balances).execute_with(|| {
let other_para_id = 3000;
let amount = REGISTER_AMOUNT;
let weight = BaseXcmWeight::get() * 3;
let message = Xcm(vec![
WithdrawAsset((Here, amount).into()),
buy_execution(),
DepositAsset {
assets: AllCounted(1).into(),
beneficiary: Teyrchain(other_para_id).into(),
},
]);
let mut hash = fake_message_hash(&message);
let r = XcmExecutor::<XcmConfig>::prepare_and_execute(
Teyrchain(PARA_ID),
message,
&mut hash,
weight,
Weight::zero(),
);
assert_eq!(r, Outcome::Complete { used: weight });
let other_para_acc: AccountId = ParaId::from(other_para_id).into_account_truncating();
assert_eq!(Balances::free_balance(para_acc), INITIAL_BALANCE - amount);
assert_eq!(Balances::free_balance(other_para_acc), amount);
});
}
/// Scenario:
/// Alice simply wants to transfer funds to Bob's account via XCM.
///
/// Asserts that the balances are updated correctly and the correct events are fired.
#[test]
fn transfer_asset_works() {
let bob = AccountId::new([1u8; 32]);
let balances = vec![(ALICE, INITIAL_BALANCE), (bob.clone(), INITIAL_BALANCE)];
kusama_like_with_balances(balances).execute_with(|| {
let amount = REGISTER_AMOUNT;
let weight = BaseXcmWeight::get();
let message = Xcm(vec![TransferAsset {
assets: (Here, amount).into(),
beneficiary: AccountId32 { network: None, id: bob.clone().into() }.into(),
}]);
let mut hash = fake_message_hash(&message);
// Use `prepare_and_execute` here to pass through the barrier
let r = XcmExecutor::<XcmConfig>::prepare_and_execute(
AccountId32 { network: None, id: ALICE.into() },
message,
&mut hash,
weight,
weight,
);
System::assert_last_event(
pallet_balances::Event::Transfer { from: ALICE, to: bob.clone(), amount }.into(),
);
assert_eq!(r, Outcome::Complete { used: weight });
assert_eq!(Balances::free_balance(ALICE), INITIAL_BALANCE - amount);
assert_eq!(Balances::free_balance(bob), INITIAL_BALANCE + amount);
});
}
/// Scenario:
/// A teyrchain wants to be notified that a transfer worked correctly.
/// It includes a `QueryHolding` order after the deposit to get notified on success.
/// This somewhat abuses `QueryHolding` as an indication of execution success. It works because
/// order execution halts on error (so no `QueryResponse` will be sent if the previous order
/// failed). The inner response sent due to the query is not used.
///
/// Asserts that the balances are updated correctly and the expected XCM is sent.
#[test]
fn report_holding_works() {
use xcm::opaque::latest::prelude::*;
let para_acc: AccountId = ParaId::from(PARA_ID).into_account_truncating();
let balances = vec![(ALICE, INITIAL_BALANCE), (para_acc.clone(), INITIAL_BALANCE)];
kusama_like_with_balances(balances).execute_with(|| {
let other_para_id = 3000;
let amount = REGISTER_AMOUNT;
let weight = BaseXcmWeight::get() * 4;
let response_info = QueryResponseInfo {
destination: Teyrchain(PARA_ID).into(),
query_id: 1234,
max_weight: Weight::from_parts(1_000_000_000, 1_000_000_000),
};
let message = Xcm(vec![
WithdrawAsset((Here, amount).into()),
buy_execution(),
DepositAsset {
assets: AllCounted(1).into(),
beneficiary: OnlyChild.into(), // invalid destination
},
// is not triggered because the deposit fails
ReportHolding { response_info: response_info.clone(), assets: All.into() },
]);
let mut hash = fake_message_hash(&message);
let r = XcmExecutor::<XcmConfig>::prepare_and_execute(
Teyrchain(PARA_ID),
message,
&mut hash,
weight,
Weight::zero(),
);
assert_eq!(
r,
Outcome::Incomplete {
used: weight - BaseXcmWeight::get(),
error: InstructionError {
index: 2,
error: XcmError::FailedToTransactAsset("AccountIdConversionFailed")
},
}
);
// there should be no query response sent for the failed deposit
assert_eq!(mock::sent_xcm(), vec![]);
assert_eq!(Balances::free_balance(para_acc.clone()), INITIAL_BALANCE - amount);
// now do a successful transfer
let message = Xcm(vec![
WithdrawAsset((Here, amount).into()),
buy_execution(),
DepositAsset {
assets: AllCounted(1).into(),
beneficiary: Teyrchain(other_para_id).into(),
},
// used to get a notification in case of success
ReportHolding { response_info: response_info.clone(), assets: AllCounted(1).into() },
]);
let mut hash = fake_message_hash(&message);
let r = XcmExecutor::<XcmConfig>::prepare_and_execute(
Teyrchain(PARA_ID),
message,
&mut hash,
weight,
Weight::zero(),
);
assert_eq!(r, Outcome::Complete { used: weight });
let other_para_acc: AccountId = ParaId::from(other_para_id).into_account_truncating();
assert_eq!(Balances::free_balance(other_para_acc), amount);
assert_eq!(Balances::free_balance(para_acc), INITIAL_BALANCE - 2 * amount);
let expected_msg = Xcm(vec![
QueryResponse {
query_id: response_info.query_id,
response: Response::Assets(vec![].into()),
max_weight: response_info.max_weight,
querier: Some(Here.into()),
},
SetTopic(hash.into()),
]);
assert_eq!(mock::sent_xcm(), vec![(Teyrchain(PARA_ID).into(), expected_msg, hash,)]);
});
}
/// Scenario:
/// A teyrchain wants to move KSM from Kusama to Asset Hub.
/// The teyrchain sends an XCM to withdraw funds combined with a teleport to the destination.
///
/// This way of moving funds from a relay to a teyrchain will only work for trusted chains.
/// Reserve based transfer should be used to move KSM to a community teyrchain.
///
/// Asserts that the balances are updated accordingly and the correct XCM is sent.
#[test]
fn teleport_to_asset_hub_works() {
use xcm::opaque::latest::prelude::*;
let para_acc: AccountId = ParaId::from(PARA_ID).into_account_truncating();
let balances = vec![(ALICE, INITIAL_BALANCE), (para_acc.clone(), INITIAL_BALANCE)];
kusama_like_with_balances(balances).execute_with(|| {
let asset_hub_id = 1000;
let other_para_id = 3000;
let amount = REGISTER_AMOUNT;
let teleport_effects = vec![
buy_execution(), // unchecked mock value
DepositAsset {
assets: AllCounted(1).into(),
beneficiary: (Parent, Teyrchain(PARA_ID)).into(),
},
];
let weight = BaseXcmWeight::get() * 3;
// teleports are not allowed to other chains, in the absence of trust from their side
let message = Xcm(vec![
WithdrawAsset((Here, amount).into()),
buy_execution(),
InitiateTeleport {
assets: All.into(),
dest: Teyrchain(other_para_id).into(),
xcm: Xcm(teleport_effects.clone()),
},
]);
let mut hash = fake_message_hash(&message);
let r = XcmExecutor::<XcmConfig>::prepare_and_execute(
Teyrchain(PARA_ID),
message,
&mut hash,
weight,
Weight::zero(),
);
assert_eq!(
r,
Outcome::Incomplete {
used: weight,
error: InstructionError { index: 2, error: UntrustedTeleportLocation },
}
);
// teleports are allowed from asset hub to kusama.
let message = Xcm(vec![
WithdrawAsset((Here, amount).into()),
buy_execution(),
InitiateTeleport {
assets: All.into(),
dest: Teyrchain(asset_hub_id).into(),
xcm: Xcm(teleport_effects.clone()),
},
]);
let mut hash = fake_message_hash(&message);
let r = XcmExecutor::<XcmConfig>::prepare_and_execute(
Teyrchain(PARA_ID),
message,
&mut hash,
weight,
Weight::zero(),
);
assert_eq!(r, Outcome::Complete { used: weight });
// 2 * amount because of the other teleport above
assert_eq!(Balances::free_balance(para_acc), INITIAL_BALANCE - 2 * amount);
let expected_msg = Xcm(vec![ReceiveTeleportedAsset((Parent, amount).into()), ClearOrigin]
.into_iter()
.chain(teleport_effects.clone().into_iter())
.chain([SetTopic(hash.into())])
.collect());
assert_eq!(mock::sent_xcm(), vec![(Teyrchain(asset_hub_id).into(), expected_msg, hash,)]);
});
}
/// Scenario:
/// A teyrchain wants to move KSM from Kusama to the teyrchain.
/// It withdraws funds and then deposits them into the reserve account of the destination chain.
/// to the destination.
///
/// Asserts that the balances are updated accordingly and the correct XCM is sent.
#[test]
fn reserve_based_transfer_works() {
use xcm::opaque::latest::prelude::*;
let para_acc: AccountId = ParaId::from(PARA_ID).into_account_truncating();
let balances = vec![(ALICE, INITIAL_BALANCE), (para_acc.clone(), INITIAL_BALANCE)];
kusama_like_with_balances(balances).execute_with(|| {
let other_para_id = 3000;
let amount = REGISTER_AMOUNT;
let transfer_effects = vec![
buy_execution(), // unchecked mock value
DepositAsset {
assets: AllCounted(1).into(),
beneficiary: (Parent, Teyrchain(PARA_ID)).into(),
},
];
let message = Xcm(vec![
WithdrawAsset((Here, amount).into()),
buy_execution(),
DepositReserveAsset {
assets: AllCounted(1).into(),
dest: Teyrchain(other_para_id).into(),
xcm: Xcm(transfer_effects.clone()),
},
]);
let mut hash = fake_message_hash(&message);
let weight = BaseXcmWeight::get() * 3;
let r = XcmExecutor::<XcmConfig>::prepare_and_execute(
Teyrchain(PARA_ID),
message,
&mut hash,
weight,
Weight::zero(),
);
assert_eq!(r, Outcome::Complete { used: weight });
assert_eq!(Balances::free_balance(para_acc), INITIAL_BALANCE - amount);
let expected_msg = Xcm(vec![ReserveAssetDeposited((Parent, amount).into()), ClearOrigin]
.into_iter()
.chain(transfer_effects.into_iter())
.chain([SetTopic(hash.into())])
.collect());
assert_eq!(mock::sent_xcm(), vec![(Teyrchain(other_para_id).into(), expected_msg, hash,)]);
});
}
/// Scenario:
/// A recursive XCM that triggers itself via `SetAppendix`.
/// The execution should fail due to inner filter.
#[test]
fn recursive_xcm_execution_fail() {
use crate::mock::*;
use frame_support::traits::{Everything, Nothing, ProcessMessageError};
use staging_xcm_builder::*;
use std::ops::ControlFlow;
use xcm::opaque::latest::prelude::*;
use xcm_executor::traits::{DenyExecution, Properties, ShouldExecute};
// Dummy filter to allow all
struct AllowAll;
impl ShouldExecute for AllowAll {
fn should_execute<RuntimeCall>(
_: &Location,
_: &mut [Instruction<RuntimeCall>],
_: Weight,
_: &mut Properties,
) -> Result<(), ProcessMessageError> {
Ok(())
}
}
// Dummy filter which denies `ClearOrigin`
struct DenyClearOrigin;
impl DenyExecution for DenyClearOrigin {
fn deny_execution<RuntimeCall>(
_: &Location,
instructions: &mut [Instruction<RuntimeCall>],
_: Weight,
_: &mut Properties,
) -> Result<(), ProcessMessageError> {
instructions.matcher().match_next_inst_while(
|_| true,
|inst| match inst {
ClearOrigin => Err(ProcessMessageError::Unsupported),
_ => Ok(ControlFlow::Continue(())),
},
)?;
Ok(())
}
}
struct XcmTestConfig;
impl xcm_executor::Config for XcmTestConfig {
type RuntimeCall = RuntimeCall;
type XcmSender = TestXcmRouter;
type XcmEventEmitter = ();
type AssetTransactor = LocalAssetTransactor;
type OriginConverter = ();
type IsReserve = ();
type IsTeleporter = TrustedTeleporters;
type UniversalLocation = UniversalLocation;
type Barrier = DenyThenTry<DenyRecursively<DenyClearOrigin>, AllowAll>;
type Weigher = FixedWeightBounds<BaseXcmWeight, RuntimeCall, MaxInstructions>;
type Trader = FixedRateOfFungible<KsmPerSecondPerByte, ()>;
type ResponseHandler = XcmPallet;
type AssetTrap = XcmPallet;
type AssetLocker = ();
type AssetExchanger = ();
type AssetClaims = XcmPallet;
type SubscriptionService = XcmPallet;
type PalletInstancesInfo = AllPalletsWithSystem;
type MaxAssetsIntoHolding = MaxAssetsIntoHolding;
type FeeManager = ();
type MessageExporter = ();
type UniversalAliases = Nothing;
type CallDispatcher = RuntimeCall;
type SafeCallFilter = Everything;
type Aliasers = Nothing;
type TransactionalProcessor = ();
type HrmpNewChannelOpenRequestHandler = ();
type HrmpChannelAcceptedHandler = ();
type HrmpChannelClosingHandler = ();
type XcmRecorder = XcmPallet;
}
let para_acc: AccountId = ParaId::from(PARA_ID).into_account_truncating();
let balances = vec![(ALICE, INITIAL_BALANCE), (para_acc.clone(), INITIAL_BALANCE)];
let origin = Teyrchain(PARA_ID);
let message = Xcm(vec![SetAppendix(Xcm(vec![SetAppendix(Xcm(vec![ClearOrigin]))]))]);
let mut hash = fake_message_hash(&message);
let weight = BaseXcmWeight::get() * 3;
kusama_like_with_balances(balances).execute_with(|| {
let outcome = XcmExecutor::<XcmTestConfig>::prepare_and_execute(
origin,
message,
&mut hash,
weight,
Weight::zero(),
);
assert_eq!(
outcome,
Outcome::Incomplete {
used: Weight::from_parts(3000000000, 3072),
error: InstructionError { index: 0, error: XcmError::Barrier },
}
);
});
}