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
@@ -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);
}