* cargo fmt

* Create benchmarks for XCM instructions introduced in v3 (#4564)

* Create benchmarks for BurnAsset and ExpectAsset

* Add benchmarks for ExpectOrigin and ExpectError

* Add benchmarks for QueryPallet and ExpectPallet

* Add benchmarks for ReportTransactStatus and ClearTransactStatus

* cargo fmt

* Use AllPalletsWithSystem in mocks

* Update XCM generic benchmarks for westend

* Remove default impls for some XCM weight functions

* Fix compilation error

* Add weight_args helper attribute

* Remove manually written XcmWeightInfo

* Parse trailing comma

* Revert "Add weight_args helper attribute"

This reverts commit 3b7c47a6182e1b9227036c38b406d494c3fcf6fd.

* Fixes

* Fixes

* XCM v3: Introduce querier field into `QueryReponse` (#4732)

* Introduce querier field into QueryReponse

* Convert &Option<MultiLocation> to Option<&MultiLocation>

&Option<T> is almost always never quite useful, most of the time it
still gets converted to an Option<&T> via `as_ref`, so we should simply
make functions that accept Option<&T> instead.

* Fix tests

* cargo fmt

* Fix benchmarks

* Appease spellchecker

* Fix test

* Fix tests

* Fix test

* Fix mock

* Fixes

* Fix tests

* Add test for response queriers

* Update xcm/pallet-xcm/src/lib.rs

* Test for non-existence of querier

Co-authored-by: Keith Yeung <kungfukeith11@gmail.com>

* Fixes

* Fixes

* Add `starts_with` function to `MultiLocation` and `Junctions` (#4835)

* add matches_prefix function to MultiLocation and Junctions

* rename matches_prefix to starts_with

* remove unnecessary main in doc comment

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Make use of starts_with in match_and_split

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
Co-authored-by: Keith Yeung <kungfukeith11@gmail.com>

* XCM v3: Bridge infrastructure (#4681)

* XCM bridge infrastructure

* Missing bit of cherry-pick

* Revamped XCM proc macros; new NetworkIds

* Fixes

* Formatting

* ExportMessage instruction and config type

* Add MessageExporter definitions

* Formatting

* Missing files

* Fixes

* Initial bridging config API

* Allow for two-stage XCM execution

* Update xcm/src/v3/mod.rs

Co-authored-by: Keith Yeung <kungfukeith11@gmail.com>

* XCM crate building again

* Initial bridging primitive

* Docs

* Docs

* More work

* More work

* Merge branch 'gav-xcm-v3' into gav-xcm-v3-bridging

* Make build

* WithComputedOrigin and SovereignPaidRemoteExporter

* Remove TODOs

* Slim bridge API and tests.

* Fixes

* More work

* First bridge test passing

* Formatting

* Another test

* Next round of bridging tests

* Repot tests

* Cleanups

* Paid bridging

* Formatting

* Tests

* Spelling

* Formatting

* Fees and refactoring

* Fixes

* Formatting

* Refactor SendXcm to become two-phase

* Fix tests

* Refactoring of SendXcm and ExportXcm complete

* Formatting

* Rename CannotReachDestination -> NotApplicable

* Remove XCM v0

* Minor grumbles

* Formatting

* Formatting

* Fixes

* Fixes

* Cleanup XCM config

* Fee handling

* Fixes

* Formatting

* Fixes

* Bump

Co-authored-by: Keith Yeung <kungfukeith11@gmail.com>

* Bump Substrate

* XCM v3: `ExchangeAsset` and Remote-locking (#4945)

* Asset Exchange and Locks

* Make sure XCM typers impl MaxEncodedLen

* Basic implementation for locks

* Bump Substrate

* Missing files

* Use new API

* Introduce  instruction

* Big refactor

* Docs

* Remove deprecated struct

* Remove deprecated struct

* Repot XCM builder tests

* ExchangeAsset test

* Exchange tests

* Locking tests

* Locking tests

* Fixes and tests

* Fixes

* Formatting

* Spelling

* Add simulator test for remote locking

* Fix tests

* Bump

* XCM v3: Support for non-fungibles (#4950)

* NFT support and a test

* New files.

* Integration tests for sending NFTs

* Formatting

* Broken Cargo features

* Use 2021 edition

* Fixes

* Formatting

* Formatting

* Update xcm/xcm-builder/src/asset_conversion.rs

Co-authored-by: Keith Yeung <kungfukeith11@gmail.com>

* Update xcm/xcm-builder/src/nonfungibles_adapter.rs

Co-authored-by: Keith Yeung <kungfukeith11@gmail.com>

* Update xcm/xcm-executor/src/lib.rs

Co-authored-by: Keith Yeung <kungfukeith11@gmail.com>

* Fixes

* Fixes

* Fixes

* Formatting

* Fixes

Co-authored-by: Bastian Köcher <info@kchr.de>
Co-authored-by: Keith Yeung <kungfukeith11@gmail.com>

* XCM v3: Context & ID hash (#4756)

* send_xcm returns message hash

* cargo fmt

* Create topic register and instructions

* Fix weights

* Use tabs

* Sketch out XcmContext

* Fix doc test

* Add the XCM context as a parameter to executor trait fns

* Fixes

* Add XcmContext parameter

* Revert adding context as an arg to SendXcm trait methods

* Revert adding context argument to ConvertOrigin trait methods

* cargo fmt

* Do not change the API of XcmExecutor::execute

* Fixes

* Fixes

* Fixes

* Fixes

* Remove convenience method

* Fixes

* Fixes

* cargo fmt

* Fixes

* Add benchmarks for XCM topic instructions

* cargo run --quiet --profile=production  --features=runtime-benchmarks -- benchmark --chain=westend-dev --steps=50 --repeat=20 --pallet=pallet_xcm_benchmarks::generic --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --header=./file_header.txt --template=./xcm/pallet-xcm-benchmarks/template.hbs --output=./runtime/westend/src/weights/xcm/pallet_xcm_benchmarks_generic.rs

* Remove context argument on FilterAssetLocation

* Fixes

* Remove unused import

* Fixes

* Fixes

* Fixes

* Accept XCM hash parameter in ExecuteXcm trait methods

* cargo fmt

* Properly enable sp-io/std

* Fixes

* default-features = false

* Fixes

* Fixes

* Fixes

* Make XcmContext optional in withdraw_asset

* Fixes

* Fixes

* Fixes

* Modify tests to check for the correct XCM hash

* Small refactor

* cargo fmt

* Check for expected hash in xcm-builder unit tests

* Add doc comment for the optionality of the XCM context in withdraw_asset

* Update xcm/src/v3/traits.rs

* Update xcm/src/v3/traits.rs

* Store XcmContext and avoid rebuilding

* Use ref for XcmContext

* Formatting

* Fix incorrect hash CC @KiChjang

* Refactor and make clear fake hashes

* Fixes

* Fixes

* Fixes

* Fix broken hashing

* Docs

* Fixes

* Fixes

* Fixes

* Formatting

* Fixes

* Fixes

* Fixes

* Remove unknowable hash

* Formatting

* Use message hash for greater identifiability

* Formatting

* Fixes

* Formatting

Co-authored-by: Keith Yeung <kungfukeith11@gmail.com>
Co-authored-by: Parity Bot <admin@parity.io>

* Fixes

* Fixes

* Fixes

* Fixes

* Formatting

* Fixes

* Formatting

* Fixes

* Fixes

* Formatting

* Formatting

* Remove horrible names

* Bump

* Remove InvertLocation trait (#5092)

* Remove InvertLocation trait

* Remove unneeded functions

* Formatting

* Fixes

* Remove XCMv1 (#5094)

* Remove XCMv1

* Remove XCMv1

* Formatting

* Fixes

* Fixes

* Formatting

* derive serialize/deserialize for xcm primitives (#5036)

* derive serialize/deserialize for xcm primitives

* derive serialize/deserialize for xcm primitives

* update v3

* update v2

Co-authored-by: Gav Wood <gavin@parity.io>

* Update lock

* Fixes

* Add benchmarks for the ExchangeAsset instruction

* `AliasOrigin` instruction stub (#5122)

* AliasOrigin instruction stub

* Fixes

* Fixes

* Update substrate

* Fixes

* Ensure same array length before using copy_from_slice

* Fixes

* Add benchmarks for the UniversalOrigin instruction

* Remove unused import

* Remove unused import

* Add benchmarks for SetFeesMode instruction

* Add benchmarks for asset (un)locking instructions

* Leave AliasOrigin unbenchmarked

* Fixes after merge

* cargo fmt

* Fixes

* Fixes

* Set TrustedReserves to None on both Kusama and Westend

* Remove extraneous reserve_asset_deposited benchmark

* Fix universal_origin benchmark

* cargo run --quiet --profile=production  --features=runtime-benchmarks -- benchmark pallet --chain=westend-dev --steps=50 --repeat=20 --pallet=pallet_xcm_benchmarks::generic --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --header=./file_header.txt --template=./xcm/pallet-xcm-benchmarks/template.hbs --output=./runtime/westend/src/weights/xcm/pallet_xcm_benchmarks_generic.rs

* Don't rely on skipped benchmark functions

* Fixes

* cargo run --quiet --profile=production  --features=runtime-benchmarks -- benchmark pallet --chain=kusama-dev --steps=50 --repeat=20 --pallet=pallet_xcm_benchmarks::generic --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --header=./file_header.txt --template=./xcm/pallet-xcm-benchmarks/template.hbs --output=./runtime/kusama/src/weights/xcm/pallet_xcm_benchmarks_generic.rs

* Fix unused variables

* Fixes

* Spelling

* Fixes

* Fix codec index of VersionedXcm

* Allows to customize how calls are dispatched from XCM (#5657)

* CallDispatcher trait

* fmt

* unused import

* fix test-runtime

* remove JustDispatch type

* fix typo in test-runtime

* missing CallDispatcher

* more missing CallDispatcher

* Update comment `NoteAssetLocked` -> `NoteUnlockable`

* Fixes

* Fixes

* Adjust MultiAssets weights based on new wild card variants

* Fixes

* Fixes

* Fixes

* Fixes

* Fixes

* Some late fixes for XCMv3 (#5237)

* Maximise chances that trapped assets can be reclaimed

* Do origin check as part of ExportMessage for security

* Formatting

* Fixes

* Cleanup export XCM APIs

* Formatting

* Update xcm/src/v3/junctions.rs

* UnpaidExecution instruction and associated barrier.

* Tighten barriers (ClearOrigin/QueryResponse)

* Allow only 1 ClearOrigin instruction in AllowTopLevelPaidExecutionFrom

* Bi-directional teleport accounting

* Revert other fix

* Build fixes]

* Tests build

* Benchmark fixes

Co-authored-by: Keith Yeung <kungfukeith11@gmail.com>

* Update Substrate

* Re-export `pub` stuff from universal_exports.rs + removed unecessary clone (#6145)

* Re-export `pub` stuff from universal_exports.rs

* Removed unnecessary clone

* Use 2D weights in XCM v3 (#6134)

* Depend upon sp-core instead of sp-runtime

* Make sp-io a dev-dependency

* Use 2D weights in XCM v3

* cargo fmt

* Add XCM pallet migration to runtimes

* Use from_parts

* cargo fmt

* Fixes

* cargo fmt

* Remove XCMWeight import

* Fixes

* Fixes

* Fixes

* Fixes

* Use translate in migration

* Increase max upward message size in tests

* Fix doc test

* Remove most uses of from_ref_time

* cargo fmt

* Fixes

* Fixes

* Add extrinsic benchmarking to XCM pallet

* cargo fmt

* Fixes

* Use old syntax

* cargo fmt

* Fixes

* Remove hardcoded weights

* Add XCM pallet to benchmarks

* Use successful origin

* Fix weird type parameter compilation issue

* Fixes

* ".git/.scripts/bench-bot.sh" runtime westend-dev pallet_xcm

* ".git/.scripts/bench-bot.sh" runtime rococo-dev pallet_xcm

* ".git/.scripts/bench-bot.sh" runtime kusama-dev pallet_xcm

* ".git/.scripts/bench-bot.sh" runtime polkadot-dev pallet_xcm

* Use benchmarked XCM pallet weights

* Fixes

* Fixes

* Use override instead of skip

* Fixes

* Fixes

* Fixes

* Fixes

* ".git/.scripts/bench-bot.sh" runtime polkadot-dev pallet_xcm

* Fixes

* ".git/.scripts/bench-bot.sh" runtime polkadot-dev pallet_xcm

* ".git/.scripts/bench-bot.sh" runtime westend-dev pallet_xcm

Co-authored-by: command-bot <>

* Replace Weight::MAX with 100b weight units

* Add test to ensure all_gte in barriers is correct

* Update xcm/src/v3/junction.rs

Co-authored-by: asynchronous rob <rphmeier@gmail.com>

* Add more weight tests

* cargo fmt

* Create thread_local in XCM executor to limit recursion depth (#6304)

* Create thread_local in XCM executor to limit recursion depth

* Add unit test for recursion limit

* Fix statefulness in tests

* Remove panic

* Use defer and environmental macro

* Fix the implementation

* Use nicer interface

* Change ThisNetwork to AnyNetwork

* Move recursion check up to top level

* cargo fmt

* Update comment

Co-authored-by: Bastian Köcher <info@kchr.de>

* Add upper limit on the number of overweight messages in the queue (#6298)

* Add upper limit on the number of ovwerweight messages in the queue

* Add newline

* Introduce whitelist for Transact and limit UMP processing to 10 messages per block (#6280)

* Add SafeCallFilter to XcmConfig

* Limit UMP to receive 10 messages every block

* Place 10 message limit on processing instead of receiving

* Always increment the message_processed count whenever a message is processed

* Add as_derivative to the Transact whitelist

* cargo fmt

* Fixes

* Update xcm/xcm-builder/src/universal_exports.rs

Co-authored-by: Branislav Kontur <bkontur@gmail.com>

* Fixes

* Fixes

* Remove topic register and instead use the topic field in XcmContext

* Derive some common traits for DispatchBlobError

* Fixes

* cargo fmt

* Fixes

* Fixes

* Fix comments

* Fixes

* Introduce WithOriginFilter and apply it as the CallDispatcher for runtimes

* Fixes

* Appease clippy and fixes

* Fixes

* Fix more clippy issues

* Fixes

* ".git/.scripts/bench-bot.sh" runtime polkadot-dev pallet_xcm

* ".git/.scripts/bench-bot.sh" runtime westend-dev pallet_xcm

* ".git/.scripts/bench-bot.sh" runtime westend-dev pallet_xcm

* Add benchmark function for ExportMessage

* Fix comment

* Add upper limit to DownwardMessageQueues size

* Add max size check for queue in can_queue_downward_message

* Fixes

* Make Transact runtime call configurable

* Return Weight::MAX when there is no successful send XCM origin

* Update substrate

* Fixes

* Fixes

* Remove ExportMessage benchmark

* Remove assertion on Transact instruction benchmark

* Make reachable destination configurable in XCM pallet benchmarks

* Fixes

* Fixes

* Remove cfg attribute in fuzzer

* Fixes

* Remove cfg attribute for XCM pallet in test runtime

* Fixes

* Use ReachableDest where possible

* Fixes

* Add benchmark for UnpaidExecution

* Update substrate

* Ensure benchmark functions pass filters

* Add runtime-benchmarks feature to fuzzer

* Ensure FixedRateOfFungible accounts for proof size weights

* cargo fmt

* Whitelist remark_with_event when runtime-benchmarks feature is enabled

* Use remark_with_event for Transact benchmarks

* Fix Cargo.lock

* Allow up to 3 DescendOrigin instructions before UnpaidExecution

* cargo fmt

* Edit code comment

* Check check_origin for unpaid execution privilege

* Fixes

* Small nits for xcm-v3 (#6408)

* Add possibility to skip benchmark for export_message

* ".git/.scripts/bench-bot.sh" xcm westend-dev pallet_xcm_benchmarks::generic

* Revert

* ".git/.scripts/bench-bot.sh" xcm westend-dev pallet_xcm_benchmarks::generic

* Add HaulBlobError to `fn haul_blob`

* ".git/.scripts/bench-bot.sh" xcm westend-dev pallet_xcm_benchmarks::generic

Co-authored-by: command-bot <>

* Revert changes to UnpaidExecution

* Change AllowUnpaidExecutionFrom to be explicit

* Fix log text

* cargo fmt

* Add benchmarks for XCM pallet version migration (#6448)

* Add benchmarks for XCM pallet version migration

* cargo fmt

* Fixes

* Fixes

* Fixes

* ".git/.scripts/bench-bot.sh" runtime westend-dev pallet_xcm

* ".git/.scripts/bench-bot.sh" runtime kusama-dev pallet_xcm

* ".git/.scripts/bench-bot.sh" runtime rococo-dev pallet_xcm

* ".git/.scripts/bench-bot.sh" runtime polkadot-dev pallet_xcm

* Fix benchmarks

* Fix benchmarks

* ".git/.scripts/bench-bot.sh" runtime westend-dev pallet_xcm

* ".git/.scripts/bench-bot.sh" runtime kusama-dev pallet_xcm

* ".git/.scripts/bench-bot.sh" runtime rococo-dev pallet_xcm

* ".git/.scripts/bench-bot.sh" runtime polkadot-dev pallet_xcm

Co-authored-by: command-bot <>

* Merge remote-tracking branch 'origin/master' into gav-xcm-v3

* Fixes

* Fix comments (#6470)

* Specify Ethereum networks by their chain id (#6286)

Co-authored-by: Squirrel <gilescope@gmail.com>

* Use  for Kusama

* Use WithComputedOrigin for Polkadot, Rococo and Westend

* Update lock

* Fix warning

* Update xcm/pallet-xcm/src/tests.rs

Co-authored-by: Squirrel <gilescope@gmail.com>

* Update runtime/parachains/src/ump/migration.rs

Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Update xcm/pallet-xcm/src/migration.rs

Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Fixes

* cargo fmt

* Typo

* Update xcm/src/v3/mod.rs

Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Docs

* Docs

* Docs

* Docs

* Docs

* Update xcm/src/v3/multiasset.rs

Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Add tests for MultiAssets::from_sorted_and_deduplicated

* Fail gracefully when same instance NFTs are detected during push

* Update Substrate to fix benchmarks

* Apply suggestions from code review

* Update runtime/kusama/src/xcm_config.rs

* Rename arguments

* Attempt to fix benchmark

* ".git/.scripts/commands/bench/bench.sh" runtime polkadot-dev runtime_parachains::ump

* Use actual weights for UMP pallet in Polkadot

* ".git/.scripts/commands/bench/bench.sh" runtime kusama-dev runtime_parachains::ump

* ".git/.scripts/commands/bench/bench.sh" runtime westend-dev runtime_parachains::ump

* ".git/.scripts/commands/bench/bench.sh" runtime rococo-dev runtime_parachains::ump

Co-authored-by: Keith Yeung <kungfukeith11@gmail.com>
Co-authored-by: Alexander Popiak <alexander.popiak@parity.io>
Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
Co-authored-by: Bastian Köcher <info@kchr.de>
Co-authored-by: Parity Bot <admin@parity.io>
Co-authored-by: stanly-johnson <stanlyjohnson@outlook.com>
Co-authored-by: nanocryk <6422796+nanocryk@users.noreply.github.com>
Co-authored-by: Branislav Kontur <bkontur@gmail.com>
Co-authored-by: asynchronous rob <rphmeier@gmail.com>
Co-authored-by: command-bot <>
Co-authored-by: Vincent Geddes <vincent.geddes@hey.com>
Co-authored-by: Squirrel <gilescope@gmail.com>
Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>
This commit is contained in:
Gavin Wood
2023-01-17 04:04:34 -03:00
committed by GitHub
parent 2952ad6f44
commit 1a1bfd2af9
155 changed files with 19234 additions and 8436 deletions
@@ -0,0 +1,469 @@
// Copyright 2022 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Polkadot 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.
// Polkadot 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 Polkadot. 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 hash = fake_message_hash(&message);
let r =
XcmExecutor::<TestConfig>::execute_xcm(Parent, message, hash, Weight::from_parts(50, 50));
assert_eq!(r, Outcome::Complete(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 hash = fake_message_hash(&message);
let r =
XcmExecutor::<TestConfig>::execute_xcm(Parent, message, hash, Weight::from_parts(50, 50));
assert_eq!(r, Outcome::Complete(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 hash = fake_message_hash(&message);
let r =
XcmExecutor::<TestConfig>::execute_xcm(Parent, message, hash, Weight::from_parts(50, 50));
assert_eq!(r, Outcome::Incomplete(Weight::from_parts(40, 40), 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 hash = fake_message_hash(&message);
let weight_limit = Weight::from_parts(50, 50);
let r = XcmExecutor::<TestConfig>::execute_xcm(Parent, message, hash, weight_limit);
assert_eq!(r, Outcome::Complete(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![X1(Parachain(1)).into()]);
// Child parachain #1 owns 1000 tokens held by us in reserve.
add_asset(Parachain(1), (Here, 1000));
// They want to transfer 100 of them to their sibling parachain #2
let message = Xcm(vec![TransferAsset {
assets: (Here, 100u128).into(),
beneficiary: X1(AccountIndex64 { index: 3, network: None }).into(),
}]);
let hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::execute_xcm(
Parachain(1),
message,
hash,
Weight::from_parts(50, 50),
);
assert_eq!(r, Outcome::Complete(Weight::from_parts(10, 10)));
assert_eq!(
asset_list(AccountIndex64 { index: 3, network: None }),
vec![(Here, 100u128).into()]
);
assert_eq!(asset_list(Parachain(1)), vec![(Here, 900u128).into()]);
assert_eq!(sent_xcm(), vec![]);
}
#[test]
fn reserve_transfer_should_work() {
AllowUnpaidFrom::set(vec![X1(Parachain(1)).into()]);
// Child parachain #1 owns 1000 tokens held by us in reserve.
add_asset(Parachain(1), (Here, 1000));
// The remote account owned by gav.
let three: MultiLocation = X1(AccountIndex64 { index: 3, network: None }).into();
// They want to transfer 100 of our native asset from sovereign account of parachain #1 into #2
// and let them know to hand it to account #3.
let message = Xcm(vec![TransferReserveAsset {
assets: (Here, 100u128).into(),
dest: Parachain(2).into(),
xcm: Xcm::<()>(vec![DepositAsset {
assets: AllCounted(1).into(),
beneficiary: three.clone(),
}]),
}]);
let hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::execute_xcm(
Parachain(1),
message,
hash,
Weight::from_parts(50, 50),
);
assert_eq!(r, Outcome::Complete(Weight::from_parts(10, 10)));
let expected_msg = Xcm::<()>(vec![
ReserveAssetDeposited((Parent, 100u128).into()),
ClearOrigin,
DepositAsset { assets: AllCounted(1).into(), beneficiary: three },
]);
let expected_hash = fake_message_hash(&expected_msg);
assert_eq!(asset_list(Parachain(2)), vec![(Here, 100).into()]);
assert_eq!(sent_xcm(), vec![(Parachain(2).into(), expected_msg, expected_hash)]);
}
#[test]
fn burn_should_work() {
// we'll let them have message execution for free.
AllowUnpaidFrom::set(vec![X1(Parachain(1)).into()]);
// Child parachain #1 owns 1000 tokens held by us in reserve.
add_asset(Parachain(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: Parachain(1).into() },
]);
let hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::execute_xcm(
Parachain(1),
message,
hash,
Weight::from_parts(50, 50),
);
assert_eq!(r, Outcome::Complete(Weight::from_parts(30, 30)));
assert_eq!(asset_list(Parachain(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: Parachain(1).into() },
]);
let hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::execute_xcm(
Parachain(1),
message,
hash,
Weight::from_parts(50, 50),
);
assert_eq!(r, Outcome::Complete(Weight::from_parts(30, 30)));
assert_eq!(asset_list(Parachain(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![X1(Parachain(1)).into(), X1(Parachain(2)).into()]);
// Child parachain #1 owns 1000 tokens held by us in reserve.
add_asset(Parachain(1), (Here, 1000));
// They want to transfer 100 of them to their sibling parachain #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 hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::execute_xcm(
Parachain(1),
message,
hash,
Weight::from_parts(20, 20),
);
assert_eq!(r, Outcome::Complete(Weight::from_parts(25, 25)));
assert_eq!(asset_list(Parachain(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 hash = fake_message_hash(&message);
let old_trapped_assets = TrappedAssets::get();
let r = XcmExecutor::<TestConfig>::execute_xcm(
Parachain(1),
message,
hash,
Weight::from_parts(20, 20),
);
assert_eq!(r, Outcome::Incomplete(Weight::from_parts(10, 10), XcmError::UnknownClaim));
assert_eq!(asset_list(Parachain(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 hash = fake_message_hash(&message);
let old_trapped_assets = TrappedAssets::get();
let r = XcmExecutor::<TestConfig>::execute_xcm(
Parachain(2),
message,
hash,
Weight::from_parts(20, 20),
);
assert_eq!(r, Outcome::Incomplete(Weight::from_parts(10, 10), XcmError::UnknownClaim));
assert_eq!(asset_list(Parachain(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 hash = fake_message_hash(&message);
let old_trapped_assets = TrappedAssets::get();
let r = XcmExecutor::<TestConfig>::execute_xcm(
Parachain(1),
message,
hash,
Weight::from_parts(20, 20),
);
assert_eq!(r, Outcome::Incomplete(Weight::from_parts(10, 10), XcmError::UnknownClaim));
assert_eq!(asset_list(Parachain(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 hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::execute_xcm(
Parachain(1),
message,
hash,
Weight::from_parts(20, 20),
);
assert_eq!(r, Outcome::Complete(Weight::from_parts(20, 20)));
assert_eq!(asset_list(Parachain(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 hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::execute_xcm(
Parachain(1),
message,
hash,
Weight::from_parts(20, 20),
);
assert_eq!(r, Outcome::Incomplete(Weight::from_parts(10, 10), XcmError::UnknownClaim));
}
#[test]
fn max_assets_limit_should_work() {
// we'll let them have message execution for free.
AllowUnpaidFrom::set(vec![X1(Parachain(1)).into()]);
// Child parachain #1 owns 1000 tokens held by us in reserve.
add_asset(Parachain(1), ([1u8; 32], 1000u128));
add_asset(Parachain(1), ([2u8; 32], 1000u128));
add_asset(Parachain(1), ([3u8; 32], 1000u128));
add_asset(Parachain(1), ([4u8; 32], 1000u128));
add_asset(Parachain(1), ([5u8; 32], 1000u128));
add_asset(Parachain(1), ([6u8; 32], 1000u128));
add_asset(Parachain(1), ([7u8; 32], 1000u128));
add_asset(Parachain(1), ([8u8; 32], 1000u128));
add_asset(Parachain(1), ([9u8; 32], 1000u128));
// Attempt to withdraw 8 (=2x4)different assets. This will succeed.
let message = Xcm(vec![
WithdrawAsset(([1u8; 32], 100u128).into()),
WithdrawAsset(([2u8; 32], 100u128).into()),
WithdrawAsset(([3u8; 32], 100u128).into()),
WithdrawAsset(([4u8; 32], 100u128).into()),
WithdrawAsset(([5u8; 32], 100u128).into()),
WithdrawAsset(([6u8; 32], 100u128).into()),
WithdrawAsset(([7u8; 32], 100u128).into()),
WithdrawAsset(([8u8; 32], 100u128).into()),
]);
let hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::execute_xcm(
Parachain(1),
message,
hash,
Weight::from_parts(100, 100),
);
assert_eq!(r, Outcome::Complete(Weight::from_parts(85, 85)));
// Attempt to withdraw 9 different assets will fail.
let message = Xcm(vec![
WithdrawAsset(([1u8; 32], 100u128).into()),
WithdrawAsset(([2u8; 32], 100u128).into()),
WithdrawAsset(([3u8; 32], 100u128).into()),
WithdrawAsset(([4u8; 32], 100u128).into()),
WithdrawAsset(([5u8; 32], 100u128).into()),
WithdrawAsset(([6u8; 32], 100u128).into()),
WithdrawAsset(([7u8; 32], 100u128).into()),
WithdrawAsset(([8u8; 32], 100u128).into()),
WithdrawAsset(([9u8; 32], 100u128).into()),
]);
let hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::execute_xcm(
Parachain(1),
message,
hash,
Weight::from_parts(100, 100),
);
assert_eq!(r, Outcome::Incomplete(Weight::from_parts(95, 95), 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(([1u8; 32], 100u128).into()),
WithdrawAsset(([2u8; 32], 100u128).into()),
WithdrawAsset(([3u8; 32], 100u128).into()),
WithdrawAsset(([4u8; 32], 100u128).into()),
WithdrawAsset(([1u8; 32], 100u128).into()),
WithdrawAsset(([2u8; 32], 100u128).into()),
WithdrawAsset(([3u8; 32], 100u128).into()),
WithdrawAsset(([4u8; 32], 100u128).into()),
WithdrawAsset(([5u8; 32], 100u128).into()),
WithdrawAsset(([6u8; 32], 100u128).into()),
WithdrawAsset(([7u8; 32], 100u128).into()),
WithdrawAsset(([8u8; 32], 100u128).into()),
]);
let hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::execute_xcm(
Parachain(1),
message,
hash,
Weight::from_parts(200, 200),
);
assert_eq!(r, Outcome::Complete(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(([1u8; 32], 100u128).into()),
WithdrawAsset(([2u8; 32], 100u128).into()),
WithdrawAsset(([3u8; 32], 100u128).into()),
WithdrawAsset(([4u8; 32], 100u128).into()),
WithdrawAsset(([5u8; 32], 100u128).into()),
WithdrawAsset(([6u8; 32], 100u128).into()),
WithdrawAsset(([7u8; 32], 100u128).into()),
WithdrawAsset(([8u8; 32], 100u128).into()),
WithdrawAsset(([1u8; 32], 100u128).into()),
WithdrawAsset(([2u8; 32], 100u128).into()),
WithdrawAsset(([3u8; 32], 100u128).into()),
WithdrawAsset(([4u8; 32], 100u128).into()),
]);
let hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::execute_xcm(
Parachain(1),
message,
hash,
Weight::from_parts(200, 200),
);
assert_eq!(r, Outcome::Incomplete(Weight::from_parts(95, 95), 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(MultiAssets::from(vec![
([1u8; 32], 100u128).into(),
([2u8; 32], 100u128).into(),
([3u8; 32], 100u128).into(),
([4u8; 32], 100u128).into(),
([5u8; 32], 100u128).into(),
([6u8; 32], 100u128).into(),
([7u8; 32], 100u128).into(),
([8u8; 32], 100u128).into(),
])),
WithdrawAsset(([1u8; 32], 100u128).into()),
]);
let hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::execute_xcm(
Parachain(1),
message,
hash,
Weight::from_parts(200, 200),
);
assert_eq!(r, Outcome::Incomplete(Weight::from_parts(25, 25), XcmError::HoldingWouldOverflow));
}
@@ -0,0 +1,278 @@
// Copyright 2022 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Polkadot 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.
// Polkadot 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 Polkadot. If not, see <http://www.gnu.org/licenses/>.
use super::*;
#[test]
fn take_weight_credit_barrier_should_work() {
let mut message =
Xcm::<()>(vec![TransferAsset { assets: (Parent, 100).into(), beneficiary: Here.into() }]);
let mut weight_credit = Weight::from_parts(10, 10);
let r = TakeWeightCredit::should_execute(
&Parent.into(),
message.inner_mut(),
Weight::from_parts(10, 10),
&mut weight_credit,
);
assert_eq!(r, Ok(()));
assert_eq!(weight_credit, Weight::zero());
let r = TakeWeightCredit::should_execute(
&Parent.into(),
message.inner_mut(),
Weight::from_parts(10, 10),
&mut weight_credit,
);
assert_eq!(r, Err(()));
assert_eq!(weight_credit, Weight::zero());
}
#[test]
fn computed_origin_should_work() {
let mut message = Xcm::<()>(vec![
UniversalOrigin(GlobalConsensus(Kusama)),
DescendOrigin(Parachain(100).into()),
DescendOrigin(PalletInstance(69).into()),
WithdrawAsset((Parent, 100).into()),
BuyExecution {
fees: (Parent, 100).into(),
weight_limit: Limited(Weight::from_parts(100, 100)),
},
TransferAsset { assets: (Parent, 100).into(), beneficiary: Here.into() },
]);
AllowPaidFrom::set(vec![(
Parent,
Parent,
GlobalConsensus(Kusama),
Parachain(100),
PalletInstance(69),
)
.into()]);
let r = AllowTopLevelPaidExecutionFrom::<IsInVec<AllowPaidFrom>>::should_execute(
&Parent.into(),
message.inner_mut(),
Weight::from_parts(100, 100),
&mut Weight::zero(),
);
assert_eq!(r, Err(()));
let r = WithComputedOrigin::<
AllowTopLevelPaidExecutionFrom<IsInVec<AllowPaidFrom>>,
ExecutorUniversalLocation,
ConstU32<2>,
>::should_execute(
&Parent.into(),
message.inner_mut(),
Weight::from_parts(100, 100),
&mut Weight::zero(),
);
assert_eq!(r, Err(()));
let r = WithComputedOrigin::<
AllowTopLevelPaidExecutionFrom<IsInVec<AllowPaidFrom>>,
ExecutorUniversalLocation,
ConstU32<5>,
>::should_execute(
&Parent.into(),
message.inner_mut(),
Weight::from_parts(100, 100),
&mut Weight::zero(),
);
assert_eq!(r, Ok(()));
}
#[test]
fn allow_unpaid_should_work() {
let mut message =
Xcm::<()>(vec![TransferAsset { assets: (Parent, 100).into(), beneficiary: Here.into() }]);
AllowUnpaidFrom::set(vec![Parent.into()]);
let r = AllowUnpaidExecutionFrom::<IsInVec<AllowUnpaidFrom>>::should_execute(
&Parachain(1).into(),
message.inner_mut(),
Weight::from_parts(10, 10),
&mut Weight::zero(),
);
assert_eq!(r, Err(()));
let r = AllowUnpaidExecutionFrom::<IsInVec<AllowUnpaidFrom>>::should_execute(
&Parent.into(),
message.inner_mut(),
Weight::from_parts(10, 10),
&mut Weight::zero(),
);
assert_eq!(r, Ok(()));
}
#[test]
fn allow_explicit_unpaid_should_work() {
let mut bad_message1 =
Xcm::<()>(vec![TransferAsset { assets: (Parent, 100).into(), beneficiary: Here.into() }]);
let mut bad_message2 = Xcm::<()>(vec![
UnpaidExecution {
weight_limit: Limited(Weight::from_parts(10, 10)),
check_origin: Some(Parent.into()),
},
TransferAsset { assets: (Parent, 100).into(), beneficiary: Here.into() },
]);
let mut good_message = Xcm::<()>(vec![
UnpaidExecution {
weight_limit: Limited(Weight::from_parts(20, 20)),
check_origin: Some(Parent.into()),
},
TransferAsset { assets: (Parent, 100).into(), beneficiary: Here.into() },
]);
AllowExplicitUnpaidFrom::set(vec![Parent.into()]);
let r = AllowExplicitUnpaidExecutionFrom::<IsInVec<AllowExplicitUnpaidFrom>>::should_execute(
&Parachain(1).into(),
good_message.inner_mut(),
Weight::from_parts(20, 20),
&mut Weight::zero(),
);
assert_eq!(r, Err(()));
let r = AllowExplicitUnpaidExecutionFrom::<IsInVec<AllowExplicitUnpaidFrom>>::should_execute(
&Parent.into(),
bad_message1.inner_mut(),
Weight::from_parts(20, 20),
&mut Weight::zero(),
);
assert_eq!(r, Err(()));
let r = AllowExplicitUnpaidExecutionFrom::<IsInVec<AllowExplicitUnpaidFrom>>::should_execute(
&Parent.into(),
bad_message2.inner_mut(),
Weight::from_parts(20, 20),
&mut Weight::zero(),
);
assert_eq!(r, Err(()));
let r = AllowExplicitUnpaidExecutionFrom::<IsInVec<AllowExplicitUnpaidFrom>>::should_execute(
&Parent.into(),
good_message.inner_mut(),
Weight::from_parts(20, 20),
&mut Weight::zero(),
);
assert_eq!(r, Ok(()));
let mut message_with_different_weight_parts = Xcm::<()>(vec![
UnpaidExecution {
weight_limit: Limited(Weight::from_parts(20, 10)),
check_origin: Some(Parent.into()),
},
TransferAsset { assets: (Parent, 100).into(), beneficiary: Here.into() },
]);
let r = AllowExplicitUnpaidExecutionFrom::<IsInVec<AllowExplicitUnpaidFrom>>::should_execute(
&Parent.into(),
message_with_different_weight_parts.inner_mut(),
Weight::from_parts(20, 20),
&mut Weight::zero(),
);
assert_eq!(r, Err(()));
let r = AllowExplicitUnpaidExecutionFrom::<IsInVec<AllowExplicitUnpaidFrom>>::should_execute(
&Parent.into(),
message_with_different_weight_parts.inner_mut(),
Weight::from_parts(10, 10),
&mut Weight::zero(),
);
assert_eq!(r, Ok(()));
}
#[test]
fn allow_paid_should_work() {
AllowPaidFrom::set(vec![Parent.into()]);
let mut message =
Xcm::<()>(vec![TransferAsset { assets: (Parent, 100).into(), beneficiary: Here.into() }]);
let r = AllowTopLevelPaidExecutionFrom::<IsInVec<AllowPaidFrom>>::should_execute(
&Parachain(1).into(),
message.inner_mut(),
Weight::from_parts(10, 10),
&mut Weight::zero(),
);
assert_eq!(r, Err(()));
let fees = (Parent, 1).into();
let mut underpaying_message = Xcm::<()>(vec![
ReserveAssetDeposited((Parent, 100).into()),
BuyExecution { fees, weight_limit: Limited(Weight::from_parts(20, 20)) },
DepositAsset { assets: AllCounted(1).into(), beneficiary: Here.into() },
]);
let r = AllowTopLevelPaidExecutionFrom::<IsInVec<AllowPaidFrom>>::should_execute(
&Parent.into(),
underpaying_message.inner_mut(),
Weight::from_parts(30, 30),
&mut Weight::zero(),
);
assert_eq!(r, Err(()));
let fees = (Parent, 1).into();
let mut paying_message = Xcm::<()>(vec![
ReserveAssetDeposited((Parent, 100).into()),
BuyExecution { fees, weight_limit: Limited(Weight::from_parts(30, 30)) },
DepositAsset { assets: AllCounted(1).into(), beneficiary: Here.into() },
]);
let r = AllowTopLevelPaidExecutionFrom::<IsInVec<AllowPaidFrom>>::should_execute(
&Parachain(1).into(),
paying_message.inner_mut(),
Weight::from_parts(30, 30),
&mut Weight::zero(),
);
assert_eq!(r, Err(()));
let r = AllowTopLevelPaidExecutionFrom::<IsInVec<AllowPaidFrom>>::should_execute(
&Parent.into(),
paying_message.inner_mut(),
Weight::from_parts(30, 30),
&mut Weight::zero(),
);
assert_eq!(r, Ok(()));
let fees = (Parent, 1).into();
let mut paying_message_with_different_weight_parts = Xcm::<()>(vec![
WithdrawAsset((Parent, 100).into()),
BuyExecution { fees, weight_limit: Limited(Weight::from_parts(20, 10)) },
DepositAsset { assets: AllCounted(1).into(), beneficiary: Here.into() },
]);
let r = AllowTopLevelPaidExecutionFrom::<IsInVec<AllowPaidFrom>>::should_execute(
&Parent.into(),
paying_message_with_different_weight_parts.inner_mut(),
Weight::from_parts(20, 20),
&mut Weight::zero(),
);
assert_eq!(r, Err(()));
let r = AllowTopLevelPaidExecutionFrom::<IsInVec<AllowPaidFrom>>::should_execute(
&Parent.into(),
paying_message_with_different_weight_parts.inner_mut(),
Weight::from_parts(10, 10),
&mut Weight::zero(),
);
assert_eq!(r, Ok(()))
}
+106
View File
@@ -0,0 +1,106 @@
// Copyright 2022 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Polkadot 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.
// Polkadot 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 Polkadot. 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(Parachain(1)), Ok(1001));
assert_eq!(to_account(Parachain(50)), Ok(1050));
assert_eq!(to_account((Parent, Parachain(1))), Ok(2001));
assert_eq!(to_account((Parent, Parachain(50))), Ok(2050));
assert_eq!(
to_account(MultiLocation::new(0, X1(AccountIndex64 { index: 1, network: None }))),
Ok(1),
);
assert_eq!(
to_account(MultiLocation::new(0, X1(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),
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: X1(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: X1(AccountIndex64 { index: 3, network: None }).into(),
}])),
// First xfer always works ok
TransferAsset {
assets: (Here, 1u128).into(),
beneficiary: X1(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: X1(AccountIndex64 { index: 3, network: None }).into(),
},
]);
// Weight limit of 70 is needed.
let limit = <TestConfig as Config>::Weigher::weight(&mut message).unwrap();
assert_eq!(limit, Weight::from_parts(70, 70));
let hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::execute_xcm(Here, message.clone(), hash, limit);
assert_eq!(r, Outcome::Complete(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>::execute_xcm(Here, message, hash, limit);
assert_eq!(r, Outcome::Complete(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,112 @@
// Copyright 2021 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Polkadot 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.
// Polkadot 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 Polkadot. If not, see <http://www.gnu.org/licenses/>.
//! This test is when we're sending an XCM from a parachain which hosts a bridge to another
//! network's bridge parachain. The destination of the XCM is within the global consensus of the
//! remote side of the bridge.
use super::*;
parameter_types! {
pub UniversalLocation: Junctions = X2(GlobalConsensus(Local::get()), Parachain(1));
pub RemoteUniversalLocation: Junctions = X2(GlobalConsensus(Remote::get()), Parachain(1));
}
type TheBridge =
TestBridge<BridgeBlobDispatcher<TestRemoteIncomingRouter, RemoteUniversalLocation>>;
type Router = UnpaidLocalExporter<HaulBlobExporter<TheBridge, Remote, Price>, UniversalLocation>;
/// ```nocompile
/// local | remote
/// |
/// GlobalConsensus(Local::get()) | GlobalConsensus(Remote::get())
/// |
/// |
/// |
/// |
/// Parachain(1) ===> Parachain(1)
/// ```
#[test]
fn sending_to_bridged_chain_works() {
let msg = Xcm(vec![Trap(1)]);
let dest = (Parent, Parent, Remote::get(), Parachain(1)).into();
assert_eq!(send_xcm::<Router>(dest, msg).unwrap().1, (Here, 100).into());
assert_eq!(TheBridge::service(), 1);
assert_eq!(
take_received_remote_messages(),
vec![(
Here.into(),
Xcm(vec![
UniversalOrigin(Local::get().into()),
DescendOrigin(Parachain(1).into()),
Trap(1),
])
)]
);
}
/// ```nocompile
/// local | remote
/// |
/// GlobalConsensus(Local::get()) | GlobalConsensus(Remote::get())
/// |
/// |
/// |
/// |
/// Parachain(1) ===> Parachain(1) ==> Parachain(1000)
/// ```
#[test]
fn sending_to_parachain_of_bridged_chain_works() {
let msg = Xcm(vec![Trap(1)]);
let dest = (Parent, Parent, Remote::get(), Parachain(1000)).into();
assert_eq!(send_xcm::<Router>(dest, msg).unwrap().1, (Here, 100).into());
assert_eq!(TheBridge::service(), 1);
let expected = vec![(
(Parent, Parachain(1000)).into(),
Xcm(vec![
UniversalOrigin(Local::get().into()),
DescendOrigin(Parachain(1).into()),
Trap(1),
]),
)];
assert_eq!(take_received_remote_messages(), expected);
}
/// ```nocompile
/// local | remote
/// |
/// GlobalConsensus(Local::get()) | GlobalConsensus(Remote::get())
/// | /\
/// | ||
/// | ||
/// | ||
/// Parachain(1) ===> Parachain(1)
/// ```
#[test]
fn sending_to_relay_chain_of_bridged_chain_works() {
let msg = Xcm(vec![Trap(1)]);
let dest = (Parent, Parent, Remote::get()).into();
assert_eq!(send_xcm::<Router>(dest, msg).unwrap().1, (Here, 100).into());
assert_eq!(TheBridge::service(), 1);
let expected = vec![(
Parent.into(),
Xcm(vec![
UniversalOrigin(Local::get().into()),
DescendOrigin(Parachain(1).into()),
Trap(1),
]),
)];
assert_eq!(take_received_remote_messages(), expected);
}
@@ -0,0 +1,70 @@
// Copyright 2021 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Polkadot 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.
// Polkadot 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 Polkadot. 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 = X1(GlobalConsensus(Local::get()));
pub RemoteUniversalLocation: Junctions = X1(GlobalConsensus(Remote::get()));
}
type TheBridge =
TestBridge<BridgeBlobDispatcher<TestRemoteIncomingRouter, RemoteUniversalLocation>>;
type Router = UnpaidLocalExporter<HaulBlobExporter<TheBridge, Remote, Price>, UniversalLocation>;
/// ```nocompile
/// local | remote
/// |
/// GlobalConsensus(Local::get()) ========> GlobalConsensus(Remote::get())
/// |
/// ```
#[test]
fn sending_to_bridged_chain_works() {
let msg = Xcm(vec![Trap(1)]);
assert_eq!(
send_xcm::<Router>((Parent, Remote::get()).into(), msg).unwrap().1,
(Here, 100).into()
);
assert_eq!(TheBridge::service(), 1);
assert_eq!(
take_received_remote_messages(),
vec![(Here.into(), Xcm(vec![UniversalOrigin(Local::get().into()), Trap(1)]))]
);
}
/// ```nocompile
/// local | remote
/// |
/// GlobalConsensus(Local::get()) ========> GlobalConsensus(Remote::get())
/// | ||
/// | ||
/// | ||
/// | \/
/// | Parachain(1000)
/// ```
#[test]
fn sending_to_parachain_of_bridged_chain_works() {
let msg = Xcm(vec![Trap(1)]);
let dest = (Parent, Remote::get(), Parachain(1000)).into();
assert_eq!(send_xcm::<Router>(dest, msg).unwrap().1, (Here, 100).into());
assert_eq!(TheBridge::service(), 1);
let expected =
vec![(Parachain(1000).into(), Xcm(vec![UniversalOrigin(Local::get().into()), Trap(1)]))];
assert_eq!(take_received_remote_messages(), expected);
}
@@ -0,0 +1,194 @@
// Copyright 2021 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Polkadot 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.
// Polkadot 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 Polkadot. If not, see <http://www.gnu.org/licenses/>.
//! Tests specific to the bridging primitives
use super::mock::*;
use crate::universal_exports::*;
use frame_support::{parameter_types, traits::Get};
use std::{cell::RefCell, marker::PhantomData};
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;
parameter_types! {
pub Local: NetworkId = ByGenesis([0; 32]);
pub Remote: NetworkId = ByGenesis([1; 32]);
pub Price: MultiAssets = MultiAssets::from((Here, 100u128));
}
std::thread_local! {
static BRIDGE_TRAFFIC: RefCell<Vec<Vec<u8>>> = RefCell::new(Vec::new());
}
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<(MultiLocation, Xcm<()>)>> = RefCell::new(Vec::new());
}
struct TestRemoteIncomingRouter;
impl SendXcm for TestRemoteIncomingRouter {
type Ticket = (MultiLocation, Xcm<()>);
fn validate(
dest: &mut Option<MultiLocation>,
msg: &mut Option<Xcm<()>>,
) -> SendResult<(MultiLocation, Xcm<()>)> {
let pair = (dest.take().unwrap(), msg.take().unwrap());
Ok((pair, MultiAssets::new()))
}
fn deliver(pair: (MultiLocation, Xcm<()>)) -> Result<XcmHash, SendError> {
let hash = fake_message_hash(&pair.1);
REMOTE_INCOMING_XCM.with(|q| q.borrow_mut().push(pair));
Ok(hash)
}
}
fn take_received_remote_messages() -> Vec<(MultiLocation, 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: &InteriorMultiLocation,
d: &InteriorMultiLocation,
m: &Xcm<()>,
) -> Result<MultiAssets, SendError> {
Ok(validate_export::<RemoteExporter>(n, c, s.clone(), d.clone(), m.clone())?.1)
}
fn deliver<RemoteExporter: ExportXcm>(
n: NetworkId,
c: u32,
s: InteriorMultiLocation,
d: InteriorMultiLocation,
m: Xcm<()>,
) -> Result<XcmHash, SendError> {
export_xcm::<RemoteExporter>(n, c, s, d, m).map(|(hash, _)| hash)
}
impl<Local: Get<Junctions>, Remote: Get<Junctions>, RemoteExporter: ExportXcm> SendXcm
for UnpaidExecutingRouter<Local, Remote, RemoteExporter>
{
type Ticket = Xcm<()>;
fn validate(
destination: &mut Option<MultiLocation>,
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, MultiAssets::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>);
// The we execute it:
let hash = fake_message_hash(&message);
let outcome = XcmExecutor::<TestConfig>::execute_xcm(
origin,
message.into(),
hash,
Weight::from_parts(2_000_000_000_000, 2_000_000_000_000),
);
match outcome {
Outcome::Complete(..) => Ok(hash),
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<MultiLocation>,
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, MultiAssets::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 hash = fake_message_hash(&message);
let outcome = XcmExecutor::<TestConfig>::execute_xcm(
origin,
message.into(),
hash,
Weight::from_parts(2_000_000_000_000, 2_000_000_000_000),
);
match outcome {
Outcome::Complete(..) => Ok(hash),
Outcome::Incomplete(..) => Err(Transport("Error executing")),
Outcome::Error(..) => Err(Transport("Unable to execute")),
}
}
}
@@ -0,0 +1,124 @@
// Copyright 2021 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Polkadot 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.
// Polkadot 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 Polkadot. If not, see <http://www.gnu.org/licenses/>.
//! This test is when we're sending an XCM from a parachain 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 parachain for use of the bridge. This is expressed
//! under the standard XCM weight and the weight pricing.
use super::*;
parameter_types! {
pub UniversalLocation: Junctions = X2(GlobalConsensus(Local::get()), Parachain(100));
pub RelayUniversalLocation: Junctions = X1(GlobalConsensus(Local::get()));
pub RemoteUniversalLocation: Junctions = X1(GlobalConsensus(Remote::get()));
pub static BridgeTable: Vec<(NetworkId, MultiLocation, Option<MultiAsset>)>
= vec![(Remote::get(), MultiLocation::parent(), Some((Parent, 200u128).into()))];
// ^^^ 100 to use the bridge (export) and 100 for the remote execution weight (5 instructions
// x (10 + 10) weight each).
}
type TheBridge =
TestBridge<BridgeBlobDispatcher<TestRemoteIncomingRouter, RemoteUniversalLocation>>;
type RelayExporter = HaulBlobExporter<TheBridge, Remote, Price>;
type LocalInnerRouter = ExecutingRouter<UniversalLocation, RelayUniversalLocation, RelayExporter>;
type LocalBridgeRouter = SovereignPaidRemoteExporter<
NetworkExportTable<BridgeTable>,
LocalInnerRouter,
UniversalLocation,
>;
type LocalRouter = (LocalInnerRouter, LocalBridgeRouter);
/// ```nocompile
/// local | remote
/// |
/// GlobalConsensus(Local::get()) ========> GlobalConsensus(Remote::get())
/// /\ |
/// || |
/// || |
/// || |
/// Parachain(100) |
/// ```
#[test]
fn sending_to_bridged_chain_works() {
let dest: MultiLocation = (Parent, Parent, Remote::get()).into();
// Routing won't work if we don't have enough funds.
assert_eq!(
send_xcm::<LocalRouter>(dest.clone(), Xcm(vec![Trap(1)])),
Err(SendError::Transport("Error executing")),
);
// Initialize the local relay so that our parachain has funds to pay for export.
add_asset(Parachain(100), (Here, 1000u128));
let msg = Xcm(vec![Trap(1)]);
assert_eq!(send_xcm::<LocalRouter>(dest, msg).unwrap().1, (Parent, 200u128).into());
assert_eq!(TheBridge::service(), 1);
assert_eq!(
take_received_remote_messages(),
vec![(
Here.into(),
Xcm(vec![
UniversalOrigin(Local::get().into()),
DescendOrigin(Parachain(100).into()),
Trap(1),
])
)]
);
// The export cost 50 ref time and 50 proof size weight units (and thus 100 units of balance).
assert_eq!(asset_list(Parachain(100)), vec![(Here, 800u128).into()]);
}
/// ```nocompile
/// local | remote
/// |
/// GlobalConsensus(Local::get()) ========> GlobalConsensus(Remote::get())
/// /\ | ||
/// || | ||
/// || | ||
/// || | \/
/// Parachain(100) | Parachain(100)
/// ```
#[test]
fn sending_to_parachain_of_bridged_chain_works() {
let dest: MultiLocation = (Parent, Parent, Remote::get(), Parachain(100)).into();
// Routing won't work if we don't have enough funds.
assert_eq!(
send_xcm::<LocalRouter>(dest.clone(), Xcm(vec![Trap(1)])),
Err(SendError::Transport("Error executing")),
);
// Initialize the local relay so that our parachain has funds to pay for export.
add_asset(Parachain(100), (Here, 1000u128));
let msg = Xcm(vec![Trap(1)]);
assert_eq!(send_xcm::<LocalRouter>(dest, msg).unwrap().1, (Parent, 200u128).into());
assert_eq!(TheBridge::service(), 1);
let expected = vec![(
Parachain(100).into(),
Xcm(vec![
UniversalOrigin(Local::get().into()),
DescendOrigin(Parachain(100).into()),
Trap(1),
]),
)];
assert_eq!(take_received_remote_messages(), expected);
// The export cost 50 ref time and 50 proof size weight units (and thus 100 units of balance).
assert_eq!(asset_list(Parachain(100)), vec![(Here, 800u128).into()]);
}
@@ -0,0 +1,124 @@
// Copyright 2021 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Polkadot 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.
// Polkadot 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 Polkadot. If not, see <http://www.gnu.org/licenses/>.
//! This test is when we're sending an XCM from a parachain whose sibling parachain hosts a
//! bridge to a parachain 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 = X2(GlobalConsensus(Local::get()), Parachain(1000));
pub ParaBridgeUniversalLocation: Junctions = X2(GlobalConsensus(Local::get()), Parachain(1));
pub RemoteParaBridgeUniversalLocation: Junctions = X2(GlobalConsensus(Remote::get()), Parachain(1));
pub BridgeTable: Vec<(NetworkId, MultiLocation, Option<MultiAsset>)>
= vec![(Remote::get(), (Parent, Parachain(1)).into(), None)];
}
type TheBridge =
TestBridge<BridgeBlobDispatcher<TestRemoteIncomingRouter, RemoteParaBridgeUniversalLocation>>;
type RelayExporter = HaulBlobExporter<TheBridge, Remote, ()>;
type LocalInnerRouter =
UnpaidExecutingRouter<UniversalLocation, ParaBridgeUniversalLocation, RelayExporter>;
type LocalBridgingRouter =
UnpaidRemoteExporter<NetworkExportTable<BridgeTable>, LocalInnerRouter, UniversalLocation>;
type LocalRouter = (LocalInnerRouter, LocalBridgingRouter);
/// ```nocompile
/// local | remote
/// |
/// GlobalConsensus(Local::get()) | GlobalConsensus(Remote::get())
/// |
/// |
/// |
/// |
/// Parachain(1000) ===> Parachain(1) ===> Parachain(1)
/// ```
#[test]
fn sending_to_bridged_chain_works() {
let msg = Xcm(vec![Trap(1)]);
assert_eq!(
send_xcm::<LocalRouter>((Parent, Parent, Remote::get(), Parachain(1)).into(), msg)
.unwrap()
.1,
MultiAssets::new()
);
assert_eq!(TheBridge::service(), 1);
assert_eq!(
take_received_remote_messages(),
vec![(
Here.into(),
Xcm(vec![
UniversalOrigin(Local::get().into()),
DescendOrigin(Parachain(1000).into()),
Trap(1)
])
)]
);
}
/// ```nocompile
/// local | remote
/// |
/// GlobalConsensus(Local::get()) | GlobalConsensus(Remote::get())
/// |
/// |
/// |
/// |
/// Parachain(1000) ===> Parachain(1) ===> Parachain(1) ===> Parachain(1000)
/// ```
#[test]
fn sending_to_sibling_of_bridged_chain_works() {
let msg = Xcm(vec![Trap(1)]);
let dest = (Parent, Parent, Remote::get(), Parachain(1000)).into();
assert_eq!(send_xcm::<LocalRouter>(dest, msg).unwrap().1, MultiAssets::new());
assert_eq!(TheBridge::service(), 1);
let expected = vec![(
(Parent, Parachain(1000)).into(),
Xcm(vec![
UniversalOrigin(Local::get().into()),
DescendOrigin(Parachain(1000).into()),
Trap(1),
]),
)];
assert_eq!(take_received_remote_messages(), expected);
}
/// ```nocompile
/// local | remote
/// |
/// GlobalConsensus(Local::get()) | GlobalConsensus(Remote::get())
/// | /\
/// | ||
/// | ||
/// | ||
/// Parachain(1000) ===> Parachain(1) ===> Parachain(1)
/// ```
#[test]
fn sending_to_relay_of_bridged_chain_works() {
let msg = Xcm(vec![Trap(1)]);
let dest = (Parent, Parent, Remote::get()).into();
assert_eq!(send_xcm::<LocalRouter>(dest, msg).unwrap().1, MultiAssets::new());
assert_eq!(TheBridge::service(), 1);
let expected = vec![(
Parent.into(),
Xcm(vec![
UniversalOrigin(Local::get().into()),
DescendOrigin(Parachain(1000).into()),
Trap(1),
]),
)];
assert_eq!(take_received_remote_messages(), expected);
}
@@ -0,0 +1,106 @@
// Copyright 2021 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Polkadot 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.
// Polkadot 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 Polkadot. If not, see <http://www.gnu.org/licenses/>.
//! This test is when we're sending an XCM from a relay-chain whose child parachain hosts a
//! bridge to a parachain 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 = X1(GlobalConsensus(Local::get()));
pub ParaBridgeUniversalLocation: Junctions = X2(GlobalConsensus(Local::get()), Parachain(1));
pub RemoteParaBridgeUniversalLocation: Junctions = X2(GlobalConsensus(Remote::get()), Parachain(1));
pub BridgeTable: Vec<(NetworkId, MultiLocation, Option<MultiAsset>)>
= vec![(Remote::get(), Parachain(1).into(), None)];
}
type TheBridge =
TestBridge<BridgeBlobDispatcher<TestRemoteIncomingRouter, RemoteParaBridgeUniversalLocation>>;
type RelayExporter = HaulBlobExporter<TheBridge, Remote, ()>;
type LocalInnerRouter =
UnpaidExecutingRouter<UniversalLocation, ParaBridgeUniversalLocation, RelayExporter>;
type LocalBridgingRouter =
UnpaidRemoteExporter<NetworkExportTable<BridgeTable>, LocalInnerRouter, UniversalLocation>;
type LocalRouter = (LocalInnerRouter, LocalBridgingRouter);
/// ```nocompile
/// local | remote
/// |
/// GlobalConsensus(Local::get()) | GlobalConsensus(Remote::get())
/// || |
/// || |
/// || |
/// \/ |
/// Parachain(1) ===> Parachain(1)
/// ```
#[test]
fn sending_to_bridged_chain_works() {
let msg = Xcm(vec![Trap(1)]);
assert_eq!(
send_xcm::<LocalRouter>((Parent, Remote::get(), Parachain(1)).into(), msg)
.unwrap()
.1,
MultiAssets::new()
);
assert_eq!(TheBridge::service(), 1);
assert_eq!(
take_received_remote_messages(),
vec![(Here.into(), Xcm(vec![UniversalOrigin(Local::get().into()), Trap(1)]))]
);
}
/// ```nocompile
/// local | remote
/// |
/// GlobalConsensus(Local::get()) | GlobalConsensus(Remote::get())
/// || |
/// || |
/// || |
/// \/ |
/// Parachain(1) ===> Parachain(1) ===> Parachain(1000)
/// ```
#[test]
fn sending_to_sibling_of_bridged_chain_works() {
let msg = Xcm(vec![Trap(1)]);
let dest = (Parent, Remote::get(), Parachain(1000)).into();
assert_eq!(send_xcm::<LocalRouter>(dest, msg).unwrap().1, MultiAssets::new());
assert_eq!(TheBridge::service(), 1);
let expected = vec![(
(Parent, Parachain(1000)).into(),
Xcm(vec![UniversalOrigin(Local::get().into()), Trap(1)]),
)];
assert_eq!(take_received_remote_messages(), expected);
}
/// ```nocompile
/// local | remote
/// |
/// GlobalConsensus(Local::get()) | GlobalConsensus(Remote::get())
/// || | /\
/// || | ||
/// || | ||
/// \/ | ||
/// Parachain(1) ===> Parachain(1)
/// ```
#[test]
fn sending_to_relay_of_bridged_chain_works() {
let msg = Xcm(vec![Trap(1)]);
let dest = (Parent, Remote::get()).into();
assert_eq!(send_xcm::<LocalRouter>(dest, msg).unwrap().1, MultiAssets::new());
assert_eq!(TheBridge::service(), 1);
let expected = vec![(Parent.into(), Xcm(vec![UniversalOrigin(Local::get().into()), Trap(1)]))];
assert_eq!(take_received_remote_messages(), expected);
}
@@ -0,0 +1,95 @@
// Copyright 2021 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Polkadot 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.
// Polkadot 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 Polkadot. If not, see <http://www.gnu.org/licenses/>.
//! This test is when we're sending an XCM from a parachain 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 = X2(GlobalConsensus(Local::get()), Parachain(1000));
pub RelayUniversalLocation: Junctions = X1(GlobalConsensus(Local::get()));
pub RemoteUniversalLocation: Junctions = X1(GlobalConsensus(Remote::get()));
pub BridgeTable: Vec<(NetworkId, MultiLocation, Option<MultiAsset>)>
= vec![(Remote::get(), MultiLocation::parent(), None)];
}
type TheBridge =
TestBridge<BridgeBlobDispatcher<TestRemoteIncomingRouter, RemoteUniversalLocation>>;
type RelayExporter = HaulBlobExporter<TheBridge, Remote, ()>;
type LocalInnerRouter =
UnpaidExecutingRouter<UniversalLocation, RelayUniversalLocation, RelayExporter>;
type LocalBridgeRouter =
UnpaidRemoteExporter<NetworkExportTable<BridgeTable>, LocalInnerRouter, UniversalLocation>;
type LocalRouter = (LocalInnerRouter, LocalBridgeRouter);
/// ```nocompile
/// local | remote
/// |
/// GlobalConsensus(Local::get()) ========> GlobalConsensus(Remote::get())
/// /\ |
/// || |
/// || |
/// || |
/// Parachain(1000) |
/// ```
#[test]
fn sending_to_bridged_chain_works() {
let msg = Xcm(vec![Trap(1)]);
assert_eq!(
send_xcm::<LocalRouter>((Parent, Parent, Remote::get()).into(), msg).unwrap().1,
MultiAssets::new()
);
assert_eq!(TheBridge::service(), 1);
assert_eq!(
take_received_remote_messages(),
vec![(
Here.into(),
Xcm(vec![
UniversalOrigin(Local::get().into()),
DescendOrigin(Parachain(1000).into()),
Trap(1)
])
)]
);
}
/// ```nocompile
/// local | remote
/// |
/// GlobalConsensus(Local::get()) ========> GlobalConsensus(Remote::get())
/// /\ | ||
/// || | ||
/// || | ||
/// || | \/
/// Parachain(1000) | Parachain(1000)
/// ```
#[test]
fn sending_to_parachain_of_bridged_chain_works() {
let msg = Xcm(vec![Trap(1)]);
let dest = (Parent, Parent, Remote::get(), Parachain(1000)).into();
assert_eq!(send_xcm::<LocalRouter>(dest, msg).unwrap().1, MultiAssets::new());
assert_eq!(TheBridge::service(), 1);
let expected = vec![(
Parachain(1000).into(),
Xcm(vec![
UniversalOrigin(Local::get().into()),
DescendOrigin(Parachain(1000).into()),
Trap(1),
]),
)];
assert_eq!(take_received_remote_messages(), expected);
}
@@ -0,0 +1,187 @@
// Copyright 2022 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Polkadot 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.
// Polkadot 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 Polkadot. If not, see <http://www.gnu.org/licenses/>.
use super::*;
#[test]
fn expect_pallet_should_work() {
AllowUnpaidFrom::set(vec![X1(Parachain(1)).into()]);
// They want to transfer 100 of our native asset from sovereign account of parachain #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 hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::execute_xcm(
Parachain(1),
message,
hash,
Weight::from_parts(50, 50),
);
assert_eq!(r, Outcome::Complete(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 hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::execute_xcm(
Parachain(1),
message,
hash,
Weight::from_parts(50, 50),
);
assert_eq!(r, Outcome::Complete(Weight::from_parts(10, 10)));
}
#[test]
fn expect_pallet_should_fail_correctly() {
AllowUnpaidFrom::set(vec![X1(Parachain(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 hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::execute_xcm(
Parachain(1),
message,
hash,
Weight::from_parts(50, 50),
);
assert_eq!(r, Outcome::Incomplete(Weight::from_parts(10, 10), 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 hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::execute_xcm(
Parachain(1),
message,
hash,
Weight::from_parts(50, 50),
);
assert_eq!(r, Outcome::Incomplete(Weight::from_parts(10, 10), 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 hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::execute_xcm(
Parachain(1),
message,
hash,
Weight::from_parts(50, 50),
);
assert_eq!(r, Outcome::Incomplete(Weight::from_parts(10, 10), 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 hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::execute_xcm(
Parachain(1),
message,
hash,
Weight::from_parts(50, 50),
);
assert_eq!(r, Outcome::Incomplete(Weight::from_parts(10, 10), 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 hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::execute_xcm(
Parachain(1),
message,
hash,
Weight::from_parts(50, 50),
);
assert_eq!(r, Outcome::Incomplete(Weight::from_parts(10, 10), 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 hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::execute_xcm(
Parachain(1),
message,
hash,
Weight::from_parts(50, 50),
);
assert_eq!(r, Outcome::Incomplete(Weight::from_parts(10, 10), 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 hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::execute_xcm(
Parachain(1),
message,
hash,
Weight::from_parts(50, 50),
);
assert_eq!(r, Outcome::Incomplete(Weight::from_parts(10, 10), 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 hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::execute_xcm(
Parachain(1),
message,
hash,
Weight::from_parts(50, 50),
);
assert_eq!(r, Outcome::Incomplete(Weight::from_parts(10, 10), XcmError::VersionIncompatible));
}
@@ -0,0 +1,234 @@
// Copyright 2022 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Polkadot 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.
// Polkadot 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 Polkadot. If not, see <http://www.gnu.org/licenses/>.
use super::*;
use LockTraceItem::*;
#[test]
fn lock_roundtrip_should_work() {
// Account #3 and Parachain #1 can execute for free
AllowUnpaidFrom::set(vec![(3u64,).into(), (Parent, Parachain(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 Parachain #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, Parachain(1)).into() },
]);
let hash = fake_message_hash(&message);
let r =
XcmExecutor::<TestConfig>::execute_xcm((3u64,), message, hash, Weight::from_parts(50, 50));
assert_eq!(r, Outcome::Complete(Weight::from_parts(40, 40)));
assert_eq!(asset_list((3u64,)), vec![(Parent, 990u128).into()]);
let expected_msg = Xcm::<()>(vec![NoteUnlockable {
owner: (Parent, Parachain(42), 3u64).into(),
asset: (Parent, 100u128).into(),
}]);
let expected_hash = fake_message_hash(&expected_msg);
assert_eq!(sent_xcm(), vec![((Parent, Parachain(1)).into(), expected_msg, expected_hash)]);
assert_eq!(
take_lock_trace(),
vec![Lock {
asset: (Parent, 100u128).into(),
owner: (3u64,).into(),
unlocker: (Parent, Parachain(1)).into(),
}]
);
// Now we'll unlock it.
let message =
Xcm(vec![UnlockAsset { asset: (Parent, 100u128).into(), target: (3u64,).into() }]);
let hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::execute_xcm(
(Parent, Parachain(1)),
message,
hash,
Weight::from_parts(50, 50),
);
assert_eq!(r, Outcome::Complete(Weight::from_parts(10, 10)));
}
#[test]
fn auto_fee_paying_should_work() {
// Account #3 and Parachain #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 Parachain #1.
let message = Xcm(vec![
SetFeesMode { jit_withdraw: true },
LockAsset { asset: (Parent, 100u128).into(), unlocker: (Parent, Parachain(1)).into() },
]);
let hash = fake_message_hash(&message);
let r =
XcmExecutor::<TestConfig>::execute_xcm((3u64,), message, hash, Weight::from_parts(50, 50));
assert_eq!(r, Outcome::Complete(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, Parachain(1)).into()]);
// #3 wants to lock 100 of the native parent tokens to be unlocked only by parachain ../#1,
// but they don't have any.
let message = Xcm(vec![LockAsset {
asset: (Parent, 100u128).into(),
unlocker: (Parent, Parachain(1)).into(),
}]);
let hash = fake_message_hash(&message);
let r =
XcmExecutor::<TestConfig>::execute_xcm((3u64,), message, hash, Weight::from_parts(50, 50));
assert_eq!(r, Outcome::Incomplete(Weight::from_parts(10, 10), 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 parachain ../#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, Parachain(1)).into(),
}]);
let hash = fake_message_hash(&message);
let r =
XcmExecutor::<TestConfig>::execute_xcm((3u64,), message, hash, Weight::from_parts(50, 50));
assert_eq!(r, Outcome::Incomplete(Weight::from_parts(10, 10), 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, Parachain(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 Parachain #1 that Account #3 has locked funds which we can unlock.
let message =
Xcm(vec![NoteUnlockable { asset: (Parent, 100u128).into(), owner: (3u64,).into() }]);
let hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::execute_xcm(
(Parent, Parachain(1)),
message,
hash,
Weight::from_parts(50, 50),
);
assert_eq!(r, Outcome::Complete(Weight::from_parts(10, 10)));
assert_eq!(
take_lock_trace(),
vec![Note {
asset: (Parent, 100u128).into(),
owner: (3u64,).into(),
locker: (Parent, Parachain(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, Parachain(1)).into() },
]);
let hash = fake_message_hash(&message);
let r =
XcmExecutor::<TestConfig>::execute_xcm((3u64,), message, hash, Weight::from_parts(50, 50));
assert_eq!(r, Outcome::Complete(Weight::from_parts(40, 40)));
assert_eq!(asset_list((3u64,)), vec![(Parent, 990u128).into()]);
let expected_msg =
Xcm::<()>(vec![UnlockAsset { target: (3u64,).into(), asset: (Parent, 100u128).into() }]);
let expected_hash = fake_message_hash(&expected_msg);
assert_eq!(sent_xcm(), vec![((Parent, Parachain(1)).into(), expected_msg, expected_hash)]);
assert_eq!(
take_lock_trace(),
vec![Reduce {
asset: (Parent, 100u128).into(),
owner: (3u64,).into(),
locker: (Parent, Parachain(1)).into(),
}]
);
}
#[test]
fn remote_unlock_should_fail_correctly() {
// Account #3 can execute for free
AllowUnpaidFrom::set(vec![(3u64,).into(), (Parent, Parachain(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 parachain.
// 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, Parachain(1)).into(),
}]);
let hash = fake_message_hash(&message);
let r =
XcmExecutor::<TestConfig>::execute_xcm((3u64,), message, hash, Weight::from_parts(50, 50));
assert_eq!(r, Outcome::Incomplete(Weight::from_parts(10, 10), XcmError::LockError));
assert_eq!(sent_xcm(), vec![]);
assert_eq!(take_lock_trace(), vec![]);
// We have been told by Parachain #1 that Account #3 has locked funds which we can unlock.
let message =
Xcm(vec![NoteUnlockable { asset: (Parent, 100u128).into(), owner: (3u64,).into() }]);
let hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::execute_xcm(
(Parent, Parachain(1)),
message,
hash,
Weight::from_parts(50, 50),
);
assert_eq!(r, Outcome::Complete(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 parachain.
// 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, Parachain(1)).into(),
}]);
let hash = fake_message_hash(&message);
let r =
XcmExecutor::<TestConfig>::execute_xcm((3u64,), message, hash, Weight::from_parts(50, 50));
assert_eq!(r, Outcome::Incomplete(Weight::from_parts(10, 10), XcmError::NotHoldingFees));
assert_eq!(sent_xcm(), vec![]);
assert_eq!(take_lock_trace(), vec![]);
}
+656
View File
@@ -0,0 +1,656 @@
// Copyright 2020 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Polkadot 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.
// Polkadot 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 Polkadot. If not, see <http://www.gnu.org/licenses/>.
use crate::{barriers::AllowSubscriptionsFrom, test_utils::*};
pub use crate::{
AllowExplicitUnpaidExecutionFrom, AllowKnownQueryResponses, AllowTopLevelPaidExecutionFrom,
AllowUnpaidExecutionFrom, FixedRateOfFungible, FixedWeightBounds, TakeWeightCredit,
};
use frame_support::traits::{ContainsPair, Everything};
pub use frame_support::{
dispatch::{
DispatchError, DispatchInfo, DispatchResultWithPostInfo, Dispatchable, GetDispatchInfo,
Parameter, PostDispatchInfo,
},
ensure, parameter_types,
sp_runtime::DispatchErrorWithPostInfo,
traits::{Contains, Get, IsInVec},
};
pub use parity_scale_codec::{Decode, Encode};
pub use sp_io::hashing::blake2_256;
pub use sp_std::{
cell::RefCell,
collections::{btree_map::BTreeMap, btree_set::BTreeSet},
fmt::Debug,
marker::PhantomData,
};
pub use xcm::latest::{prelude::*, Weight};
pub use xcm_executor::{
traits::{
AssetExchange, AssetLock, ConvertOrigin, Enact, ExportXcm, FeeManager, FeeReason,
LockError, OnResponse, TransactAsset,
},
Assets, Config,
};
pub enum TestOrigin {
Root,
Relay,
Signed(u64),
Parachain(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, Eq, PartialEq, Clone, Copy, scale_info::TypeInfo)]
pub enum TestCall {
OnlyRoot(Weight, Option<Weight>),
OnlyParachain(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::OnlyParachain(_, maybe_actual, _) |
TestCall::Any(_, maybe_actual) => maybe_actual,
};
post_info.actual_weight = maybe_actual;
if match (&origin, &self) {
(TestOrigin::Parachain(i), TestCall::OnlyParachain(_, _, Some(j))) => i == j,
(TestOrigin::Signed(i), TestCall::OnlySigned(_, _, Some(j))) => i == j,
(TestOrigin::Root, TestCall::OnlyRoot(..)) |
(TestOrigin::Parachain(_), TestCall::OnlyParachain(_, _, 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 weight = *match self {
TestCall::OnlyRoot(estimate, ..) |
TestCall::OnlyParachain(estimate, ..) |
TestCall::OnlySigned(estimate, ..) |
TestCall::Any(estimate, ..) => estimate,
};
DispatchInfo { weight, ..Default::default() }
}
}
thread_local! {
pub static SENT_XCM: RefCell<Vec<(MultiLocation, Xcm<()>, XcmHash)>> = RefCell::new(Vec::new());
pub static EXPORTED_XCM: RefCell<
Vec<(NetworkId, u32, InteriorMultiLocation, InteriorMultiLocation, Xcm<()>, XcmHash)>
> = RefCell::new(Vec::new());
pub static EXPORTER_OVERRIDE: RefCell<Option<(
fn(
NetworkId,
u32,
&InteriorMultiLocation,
&InteriorMultiLocation,
&Xcm<()>,
) -> Result<MultiAssets, SendError>,
fn(
NetworkId,
u32,
InteriorMultiLocation,
InteriorMultiLocation,
Xcm<()>,
) -> Result<XcmHash, SendError>,
)>> = RefCell::new(None);
pub static SEND_PRICE: RefCell<MultiAssets> = RefCell::new(MultiAssets::new());
}
pub fn sent_xcm() -> Vec<(MultiLocation, opaque::Xcm, XcmHash)> {
SENT_XCM.with(|q| (*q.borrow()).clone())
}
pub fn set_send_price(p: impl Into<MultiAsset>) {
SEND_PRICE.with(|l| l.replace(p.into().into()));
}
pub fn exported_xcm(
) -> Vec<(NetworkId, u32, InteriorMultiLocation, InteriorMultiLocation, opaque::Xcm, XcmHash)> {
EXPORTED_XCM.with(|q| (*q.borrow()).clone())
}
pub fn set_exporter_override(
price: fn(
NetworkId,
u32,
&InteriorMultiLocation,
&InteriorMultiLocation,
&Xcm<()>,
) -> Result<MultiAssets, SendError>,
deliver: fn(
NetworkId,
u32,
InteriorMultiLocation,
InteriorMultiLocation,
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 TestMessageSender;
impl SendXcm for TestMessageSender {
type Ticket = (MultiLocation, Xcm<()>, XcmHash);
fn validate(
dest: &mut Option<MultiLocation>,
msg: &mut Option<Xcm<()>>,
) -> SendResult<(MultiLocation, Xcm<()>, XcmHash)> {
let msg = msg.take().unwrap();
let hash = fake_message_hash(&msg);
let triplet = (dest.take().unwrap(), msg, hash);
Ok((triplet, SEND_PRICE.with(|l| l.borrow().clone())))
}
fn deliver(triplet: (MultiLocation, Xcm<()>, XcmHash)) -> Result<XcmHash, SendError> {
let hash = triplet.2;
SENT_XCM.with(|q| q.borrow_mut().push(triplet));
Ok(hash)
}
}
pub struct TestMessageExporter;
impl ExportXcm for TestMessageExporter {
type Ticket = (NetworkId, u32, InteriorMultiLocation, InteriorMultiLocation, Xcm<()>, XcmHash);
fn validate(
network: NetworkId,
channel: u32,
uni_src: &mut Option<InteriorMultiLocation>,
dest: &mut Option<InteriorMultiLocation>,
msg: &mut Option<Xcm<()>>,
) -> SendResult<(NetworkId, u32, InteriorMultiLocation, InteriorMultiLocation, Xcm<()>, XcmHash)>
{
let (s, d, m) = (uni_src.take().unwrap(), dest.take().unwrap(), msg.take().unwrap());
let r: Result<MultiAssets, SendError> = EXPORTER_OVERRIDE.with(|e| {
if let Some((ref f, _)) = &*e.borrow() {
f(network, channel, &s, &d, &m)
} else {
Ok(MultiAssets::new())
}
});
let h = fake_message_hash(&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, InteriorMultiLocation, InteriorMultiLocation, 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<MultiLocation, Assets>> = RefCell::new(BTreeMap::new());
}
pub fn assets(who: impl Into<MultiLocation>) -> Assets {
ASSETS.with(|a| a.borrow().get(&who.into()).cloned()).unwrap_or_default()
}
pub fn asset_list(who: impl Into<MultiLocation>) -> Vec<MultiAsset> {
MultiAssets::from(assets(who)).into_inner()
}
pub fn add_asset(who: impl Into<MultiLocation>, what: impl Into<MultiAsset>) {
ASSETS.with(|a| a.borrow_mut().entry(who.into()).or_insert(Assets::new()).subsume(what.into()));
}
pub struct TestAssetTransactor;
impl TransactAsset for TestAssetTransactor {
fn deposit_asset(
what: &MultiAsset,
who: &MultiLocation,
_context: &XcmContext,
) -> Result<(), XcmError> {
add_asset(who.clone(), what.clone());
Ok(())
}
fn withdraw_asset(
what: &MultiAsset,
who: &MultiLocation,
_maybe_context: Option<&XcmContext>,
) -> Result<Assets, 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<MultiLocation>) -> Result<u64, MultiLocation> {
Ok(match l.into() {
// Siblings at 2000+id
MultiLocation { parents: 1, interior: X1(Parachain(id)) } => 2000 + id as u64,
// Accounts are their number
MultiLocation { parents: 0, interior: X1(AccountIndex64 { index, .. }) } => index,
// Children at 1000+id
MultiLocation { parents: 0, interior: X1(Parachain(id)) } => 1000 + id as u64,
// Self at 3000
MultiLocation { parents: 0, interior: Here } => 3000,
// Parent at 3001
MultiLocation { parents: 1, interior: Here } => 3001,
l => {
// 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(Polkadot)) => 4001,
_ => return Err(l),
}
},
})
}
pub struct TestOriginConverter;
impl ConvertOrigin<TestOrigin> for TestOriginConverter {
fn convert_origin(
origin: impl Into<MultiLocation>,
kind: OriginKind,
) -> Result<TestOrigin, MultiLocation> {
use OriginKind::*;
match (kind, origin.into()) {
(Superuser, _) => Ok(TestOrigin::Root),
(SovereignAccount, l) => Ok(TestOrigin::Signed(to_account(l)?)),
(Native, MultiLocation { parents: 0, interior: X1(Parachain(id)) }) =>
Ok(TestOrigin::Parachain(id)),
(Native, MultiLocation { parents: 1, interior: Here }) => Ok(TestOrigin::Relay),
(Native, MultiLocation { parents: 0, interior: X1(AccountIndex64 { index, .. }) }) =>
Ok(TestOrigin::Signed(index)),
(_, origin) => Err(origin),
}
}
}
thread_local! {
pub static IS_RESERVE: RefCell<BTreeMap<MultiLocation, Vec<MultiAssetFilter>>> = RefCell::new(BTreeMap::new());
pub static IS_TELEPORTER: RefCell<BTreeMap<MultiLocation, Vec<MultiAssetFilter>>> = RefCell::new(BTreeMap::new());
pub static UNIVERSAL_ALIASES: RefCell<BTreeSet<(MultiLocation, Junction)>> = RefCell::new(BTreeSet::new());
}
pub fn add_reserve(from: MultiLocation, asset: MultiAssetFilter) {
IS_RESERVE.with(|r| r.borrow_mut().entry(from).or_default().push(asset));
}
#[allow(dead_code)]
pub fn add_teleporter(from: MultiLocation, asset: MultiAssetFilter) {
IS_TELEPORTER.with(|r| r.borrow_mut().entry(from).or_default().push(asset));
}
pub fn add_universal_alias(bridge: impl Into<MultiLocation>, 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<MultiAsset, MultiLocation> for TestIsReserve {
fn contains(asset: &MultiAsset, origin: &MultiLocation) -> 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<MultiAsset, MultiLocation> for TestIsTeleporter {
fn contains(asset: &MultiAsset, origin: &MultiLocation) -> 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<(MultiLocation, Junction)> for TestUniversalAliases {
fn contains(t: &(MultiLocation, Junction)) -> bool {
UNIVERSAL_ALIASES.with(|r| r.borrow().contains(t))
}
}
pub enum ResponseSlot {
Expecting(MultiLocation),
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: &MultiLocation,
query_id: u64,
_querier: Option<&MultiLocation>,
) -> bool {
QUERIES.with(|q| match q.borrow().get(&query_id) {
Some(ResponseSlot::Expecting(ref l)) => l == origin,
_ => false,
})
}
fn on_response(
_origin: &MultiLocation,
query_id: u64,
_querier: Option<&MultiLocation>,
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: MultiLocation) {
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,
})
})
}
parameter_types! {
pub static ExecutorUniversalLocation: InteriorMultiLocation
= (ByGenesis([0; 32]), Parachain(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<MultiLocation> = vec![];
pub static AllowUnpaidFrom: Vec<MultiLocation> = vec![];
pub static AllowPaidFrom: Vec<MultiLocation> = vec![];
pub static AllowSubsFrom: Vec<MultiLocation> = 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 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<&MultiLocation>, r: FeeReason) -> bool {
IS_WAIVED.with(|l| l.borrow().contains(&r))
}
fn handle_fee(_: MultiAssets) {}
}
#[derive(Clone, Eq, PartialEq, Debug)]
pub enum LockTraceItem {
Lock { unlocker: MultiLocation, asset: MultiAsset, owner: MultiLocation },
Unlock { unlocker: MultiLocation, asset: MultiAsset, owner: MultiLocation },
Note { locker: MultiLocation, asset: MultiAsset, owner: MultiLocation },
Reduce { locker: MultiLocation, asset: MultiAsset, owner: MultiLocation },
}
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<(MultiLocation, MultiLocation), Assets>> = RefCell::new(BTreeMap::new());
pub static ALLOWED_REQUEST_UNLOCKS: RefCell<BTreeMap<(MultiLocation, MultiLocation), Assets>> = 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<MultiLocation>,
asset: impl Into<MultiAsset>,
owner: impl Into<MultiLocation>,
) {
ALLOWED_UNLOCKS.with(|l| {
l.borrow_mut()
.entry((owner.into(), unlocker.into()))
.or_default()
.subsume(asset.into())
});
}
pub fn disallow_unlock(
unlocker: impl Into<MultiLocation>,
asset: impl Into<MultiAsset>,
owner: impl Into<MultiLocation>,
) {
ALLOWED_UNLOCKS.with(|l| {
l.borrow_mut()
.entry((owner.into(), unlocker.into()))
.or_default()
.saturating_take(asset.into().into())
});
}
pub fn unlock_allowed(unlocker: &MultiLocation, asset: &MultiAsset, owner: &MultiLocation) -> 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<MultiLocation>,
asset: impl Into<MultiAsset>,
owner: impl Into<MultiLocation>,
) {
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<MultiLocation>,
asset: impl Into<MultiAsset>,
owner: impl Into<MultiLocation>,
) {
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: &MultiLocation,
asset: &MultiAsset,
owner: &MultiLocation,
) -> 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: MultiLocation,
asset: MultiAsset,
owner: MultiLocation,
) -> Result<Self::LockTicket, LockError> {
ensure!(assets(owner.clone()).contains_asset(&asset), LockError::AssetNotOwned);
Ok(TestTicket(LockTraceItem::Lock { unlocker, asset, owner }))
}
fn prepare_unlock(
unlocker: MultiLocation,
asset: MultiAsset,
owner: MultiLocation,
) -> Result<Self::UnlockTicket, LockError> {
ensure!(unlock_allowed(&unlocker, &asset, &owner), LockError::NotLocked);
Ok(TestTicket(LockTraceItem::Unlock { unlocker, asset, owner }))
}
fn note_unlockable(
locker: MultiLocation,
asset: MultiAsset,
owner: MultiLocation,
) -> 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: MultiLocation,
asset: MultiAsset,
owner: MultiLocation,
) -> 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<Assets> = RefCell::new(Assets::new());
}
pub fn set_exchange_assets(assets: impl Into<MultiAssets>) {
EXCHANGE_ASSETS.with(|a| a.replace(assets.into().into()));
}
pub fn exchange_assets() -> MultiAssets {
EXCHANGE_ASSETS.with(|a| a.borrow().clone().into())
}
pub struct TestAssetExchange;
impl AssetExchange for TestAssetExchange {
fn exchange_asset(
_origin: Option<&MultiLocation>,
give: Assets,
want: &MultiAssets,
maximal: bool,
) -> Result<Assets, Assets> {
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, Assets::new())
} else {
have.saturating_take(want.clone().into())
};
have.subsume_assets(give);
EXCHANGE_ASSETS.with(|l| l.replace(have));
Ok(get)
}
}
pub struct TestConfig;
impl Config for TestConfig {
type RuntimeCall = TestCall;
type XcmSender = TestMessageSender;
type AssetTransactor = TestAssetTransactor;
type OriginConverter = TestOriginConverter;
type IsReserve = TestIsReserve;
type IsTeleporter = TestIsTeleporter;
type UniversalLocation = ExecutorUniversalLocation;
type Barrier = TestBarrier;
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;
}
pub fn fungible_multi_asset(location: MultiLocation, amount: u128) -> MultiAsset {
(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)
}
+39
View File
@@ -0,0 +1,39 @@
// Copyright 2022 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Polkadot 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.
// Polkadot 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 Polkadot. If not, see <http://www.gnu.org/licenses/>.
use super::{test_utils::*, *};
use core::convert::TryInto;
use frame_support::{
assert_err,
traits::{ConstU32, ContainsPair},
weights::constants::{WEIGHT_PROOF_SIZE_PER_MB, WEIGHT_REF_TIME_PER_SECOND},
};
use xcm_executor::{traits::prelude::*, Config, XcmExecutor};
mod mock;
use mock::*;
mod assets;
mod barriers;
mod basic;
mod bridging;
mod expecting;
mod locking;
mod origins;
mod querying;
mod transacting;
mod version_subscriptions;
mod weight;
@@ -0,0 +1,140 @@
// Copyright 2022 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Polkadot 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.
// Polkadot 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 Polkadot. If not, see <http://www.gnu.org/licenses/>.
use super::*;
#[test]
fn universal_origin_should_work() {
AllowUnpaidFrom::set(vec![X1(Parachain(1)).into(), X1(Parachain(2)).into()]);
clear_universal_aliases();
// Parachain 1 may represent Kusama to us
add_universal_alias(Parachain(1), Kusama);
// Parachain 2 may represent Polkadot to us
add_universal_alias(Parachain(2), Polkadot);
let message = Xcm(vec![
UniversalOrigin(GlobalConsensus(Kusama)),
TransferAsset { assets: (Parent, 100u128).into(), beneficiary: Here.into() },
]);
let hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::execute_xcm(
Parachain(2),
message,
hash,
Weight::from_parts(50, 50),
);
assert_eq!(r, Outcome::Incomplete(Weight::from_parts(10, 10), XcmError::InvalidLocation));
let message = Xcm(vec![
UniversalOrigin(GlobalConsensus(Kusama)),
TransferAsset { assets: (Parent, 100u128).into(), beneficiary: Here.into() },
]);
let hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::execute_xcm(
Parachain(1),
message,
hash,
Weight::from_parts(50, 50),
);
assert_eq!(r, Outcome::Incomplete(Weight::from_parts(20, 20), 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 hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::execute_xcm(
Parachain(1),
message,
hash,
Weight::from_parts(50, 50),
);
assert_eq!(r, Outcome::Complete(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 Parachain #1 have message execution for free.
AllowUnpaidFrom::set(vec![X1(Parachain(1)).into()]);
// Local parachain #1 issues a transfer asset on Polkadot Relay-chain, transfering 100 Planck to
// Polkadot parachain #2.
let expected_message = Xcm(vec![TransferAsset {
assets: (Here, 100u128).into(),
beneficiary: Parachain(2).into(),
}]);
let expected_hash = fake_message_hash(&expected_message);
let message = Xcm(vec![ExportMessage {
network: Polkadot,
destination: Here,
xcm: expected_message.clone(),
}]);
let hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::execute_xcm(
Parachain(1),
message,
hash,
Weight::from_parts(50, 50),
);
assert_eq!(r, Outcome::Complete(Weight::from_parts(10, 10)));
let uni_src = (ByGenesis([0; 32]), Parachain(42), Parachain(1)).into();
assert_eq!(
exported_xcm(),
vec![(Polkadot, 403611790, uni_src, Here, expected_message, expected_hash)]
);
}
#[test]
fn unpaid_execution_should_work() {
// Bridge chain (assumed to be Relay) lets Parachain #1 have message execution for free.
AllowUnpaidFrom::set(vec![X1(Parachain(1)).into()]);
// Bridge chain (assumed to be Relay) lets Parachain #2 have message execution for free if it
// asks.
AllowExplicitUnpaidFrom::set(vec![X1(Parachain(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(Parachain(2).into()),
}]);
let hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::execute_xcm(
Parachain(1),
message.clone(),
hash,
Weight::from_parts(50, 50),
);
assert_eq!(r, Outcome::Incomplete(Weight::from_parts(10, 10), XcmError::BadOrigin));
let r = XcmExecutor::<TestConfig>::execute_xcm(
Parachain(2),
message.clone(),
hash,
Weight::from_parts(50, 50),
);
assert_eq!(r, Outcome::Error(XcmError::Barrier));
let message = Xcm(vec![UnpaidExecution {
weight_limit: Limited(Weight::from_parts(10, 10)),
check_origin: Some(Parachain(2).into()),
}]);
let r = XcmExecutor::<TestConfig>::execute_xcm(
Parachain(2),
message.clone(),
hash,
Weight::from_parts(50, 50),
);
assert_eq!(r, Outcome::Complete(Weight::from_parts(10, 10)));
}
@@ -0,0 +1,119 @@
// Copyright 2022 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Polkadot 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.
// Polkadot 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 Polkadot. If not, see <http://www.gnu.org/licenses/>.
use super::*;
#[test]
fn pallet_query_should_work() {
AllowUnpaidFrom::set(vec![X1(Parachain(1)).into()]);
// They want to transfer 100 of our native asset from sovereign account of parachain #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: Parachain(1).into(),
query_id: 1,
max_weight: Weight::from_parts(50, 50),
},
}]);
let hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::execute_xcm(
Parachain(1),
message,
hash,
Weight::from_parts(50, 50),
);
assert_eq!(r, Outcome::Complete(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![].try_into().unwrap()),
querier: Some(Here.into()),
}]);
let expected_hash = fake_message_hash(&expected_msg);
assert_eq!(sent_xcm(), vec![(Parachain(1).into(), expected_msg, expected_hash)]);
}
#[test]
fn pallet_query_with_results_should_work() {
AllowUnpaidFrom::set(vec![X1(Parachain(1)).into()]);
// They want to transfer 100 of our native asset from sovereign account of parachain #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: Parachain(1).into(),
query_id: 1,
max_weight: Weight::from_parts(50, 50),
},
}]);
let hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::execute_xcm(
Parachain(1),
message,
hash,
Weight::from_parts(50, 50),
);
assert_eq!(r, Outcome::Complete(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()),
}]);
let expected_hash = fake_message_hash(&expected_msg);
assert_eq!(sent_xcm(), vec![(Parachain(1).into(), expected_msg, expected_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 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>::execute_xcm(Parent, message.clone(), hash, weight_limit);
assert_eq!(r, Outcome::Complete(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>::execute_xcm(Parent, message.clone(), hash, weight_limit);
assert_eq!(r, Outcome::Error(XcmError::Barrier));
}
@@ -0,0 +1,188 @@
// Copyright 2022 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Polkadot 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.
// Polkadot 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 Polkadot. 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,
require_weight_at_most: Weight::from_parts(50, 50),
call: TestCall::Any(Weight::from_parts(50, 50), None).encode().into(),
}]);
let hash = fake_message_hash(&message);
let weight_limit = Weight::from_parts(60, 60);
let r = XcmExecutor::<TestConfig>::execute_xcm(Parent, message, hash, weight_limit);
assert_eq!(r, Outcome::Complete(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,
require_weight_at_most: Weight::from_parts(40, 40),
call: TestCall::Any(Weight::from_parts(50, 50), None).encode().into(),
}]);
let hash = fake_message_hash(&message);
let weight_limit = Weight::from_parts(60, 60);
let r = XcmExecutor::<TestConfig>::execute_xcm(Parent, message, hash, weight_limit);
assert_eq!(r, Outcome::Incomplete(Weight::from_parts(50, 50), XcmError::MaxWeightInvalid));
}
#[test]
fn transacting_should_refund_weight() {
AllowUnpaidFrom::set(vec![Parent.into()]);
let message = Xcm::<TestCall>(vec![Transact {
origin_kind: OriginKind::Native,
require_weight_at_most: Weight::from_parts(50, 50),
call: TestCall::Any(Weight::from_parts(50, 50), Some(Weight::from_parts(30, 30)))
.encode()
.into(),
}]);
let hash = fake_message_hash(&message);
let weight_limit = Weight::from_parts(60, 60);
let r = XcmExecutor::<TestConfig>::execute_xcm(Parent, message, hash, weight_limit);
assert_eq!(r, Outcome::Complete(Weight::from_parts(40, 40)));
}
#[test]
fn paid_transacting_should_refund_payment_for_unused_weight() {
let one: MultiLocation = 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,
require_weight_at_most: Weight::from_parts(50, 50),
// call estimated at 50 but only takes 10.
call: TestCall::Any(Weight::from_parts(50, 50), Some(Weight::from_parts(10, 10)))
.encode()
.into(),
},
RefundSurplus,
DepositAsset { assets: AllCounted(1).into(), beneficiary: one.clone() },
]);
let hash = fake_message_hash(&message);
let weight_limit = Weight::from_parts(100, 100);
let r = XcmExecutor::<TestConfig>::execute_xcm(origin, message, hash, weight_limit);
assert_eq!(r, Outcome::Complete(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,
require_weight_at_most: Weight::from_parts(50, 50),
call: TestCall::Any(Weight::from_parts(50, 50), None).encode().into(),
},
ReportTransactStatus(QueryResponseInfo {
destination: Parent.into(),
query_id: 42,
max_weight: Weight::from_parts(5000, 5000),
}),
]);
let hash = fake_message_hash(&message);
let weight_limit = Weight::from_parts(70, 70);
let r = XcmExecutor::<TestConfig>::execute_xcm(Parent, message, hash, weight_limit);
assert_eq!(r, Outcome::Complete(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()),
}]);
let expected_hash = fake_message_hash(&expected_msg);
assert_eq!(sent_xcm(), vec![(Parent.into(), expected_msg, expected_hash)]);
}
#[test]
fn report_failed_transact_status_should_work() {
AllowUnpaidFrom::set(vec![Parent.into()]);
let message = Xcm::<TestCall>(vec![
Transact {
origin_kind: OriginKind::Native,
require_weight_at_most: Weight::from_parts(50, 50),
call: TestCall::OnlyRoot(Weight::from_parts(50, 50), None).encode().into(),
},
ReportTransactStatus(QueryResponseInfo {
destination: Parent.into(),
query_id: 42,
max_weight: Weight::from_parts(5000, 5000),
}),
]);
let hash = fake_message_hash(&message);
let weight_limit = Weight::from_parts(70, 70);
let r = XcmExecutor::<TestConfig>::execute_xcm(Parent, message, hash, weight_limit);
assert_eq!(r, Outcome::Complete(Weight::from_parts(70, 70)));
let expected_msg = Xcm(vec![QueryResponse {
response: Response::DispatchResult(MaybeErrorCode::Error(vec![2])),
query_id: 42,
max_weight: Weight::from_parts(5000, 5000),
querier: Some(Here.into()),
}]);
let expected_hash = fake_message_hash(&expected_msg);
assert_eq!(sent_xcm(), vec![(Parent.into(), expected_msg, expected_hash)]);
}
#[test]
fn clear_transact_status_should_work() {
AllowUnpaidFrom::set(vec![Parent.into()]);
let message = Xcm::<TestCall>(vec![
Transact {
origin_kind: OriginKind::Native,
require_weight_at_most: Weight::from_parts(50, 50),
call: TestCall::OnlyRoot(Weight::from_parts(50, 50), None).encode().into(),
},
ClearTransactStatus,
ReportTransactStatus(QueryResponseInfo {
destination: Parent.into(),
query_id: 42,
max_weight: Weight::from_parts(5000, 5000),
}),
]);
let hash = fake_message_hash(&message);
let weight_limit = Weight::from_parts(80, 80);
let r = XcmExecutor::<TestConfig>::execute_xcm(Parent, message, hash, weight_limit);
assert_eq!(r, Outcome::Complete(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()),
}]);
let expected_hash = fake_message_hash(&expected_msg);
assert_eq!(sent_xcm(), vec![(Parent.into(), expected_msg, expected_hash)]);
}
@@ -0,0 +1,149 @@
// Copyright 2022 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Polkadot 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.
// Polkadot 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 Polkadot. If not, see <http://www.gnu.org/licenses/>.
use super::*;
#[test]
fn simple_version_subscriptions_should_work() {
AllowSubsFrom::set(vec![Parent.into()]);
let origin = Parachain(1000);
let message = Xcm::<TestCall>(vec![
SetAppendix(Xcm(vec![])),
SubscribeVersion { query_id: 42, max_response_weight: Weight::from_parts(5000, 5000) },
]);
let hash = fake_message_hash(&message);
let weight_limit = Weight::from_parts(20, 20);
let r = XcmExecutor::<TestConfig>::execute_xcm(origin, message, hash, weight_limit);
assert_eq!(r, Outcome::Error(XcmError::Barrier));
let origin = Parachain(1000);
let message = Xcm::<TestCall>(vec![SubscribeVersion {
query_id: 42,
max_response_weight: Weight::from_parts(5000, 5000),
}]);
let hash = fake_message_hash(&message);
let weight_limit = Weight::from_parts(10, 10);
let r = XcmExecutor::<TestConfig>::execute_xcm(origin, message.clone(), hash, weight_limit);
assert_eq!(r, Outcome::Error(XcmError::Barrier));
let r = XcmExecutor::<TestConfig>::execute_xcm(Parent, message, hash, weight_limit);
assert_eq!(r, Outcome::Complete(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 = Parachain(1000);
let message = Xcm::<TestCall>(vec![
DescendOrigin(X1(AccountIndex64 { index: 1, network: None })),
SubscribeVersion { query_id: 42, max_response_weight: Weight::from_parts(5000, 5000) },
]);
let hash = fake_message_hash(&message);
let weight_limit = Weight::from_parts(20, 20);
let r = XcmExecutor::<TestConfig>::execute_xcm_in_credit(
origin.clone(),
message,
hash,
weight_limit,
weight_limit,
);
assert_eq!(r, Outcome::Incomplete(Weight::from_parts(20, 20), XcmError::BadOrigin));
let message = Xcm::<TestCall>(vec![
SetAppendix(Xcm(vec![])),
SubscribeVersion { query_id: 42, max_response_weight: Weight::from_parts(5000, 5000) },
]);
let hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::execute_xcm_in_credit(
origin,
message,
hash,
weight_limit,
weight_limit,
);
assert_eq!(r, Outcome::Complete(Weight::from_parts(20, 20)));
assert_eq!(
SubscriptionRequests::get(),
vec![(Parachain(1000).into(), Some((42, Weight::from_parts(5000, 5000))))]
);
}
#[test]
fn simple_version_unsubscriptions_should_work() {
AllowSubsFrom::set(vec![Parent.into()]);
let origin = Parachain(1000);
let message = Xcm::<TestCall>(vec![SetAppendix(Xcm(vec![])), UnsubscribeVersion]);
let hash = fake_message_hash(&message);
let weight_limit = Weight::from_parts(20, 20);
let r = XcmExecutor::<TestConfig>::execute_xcm(origin, message, hash, weight_limit);
assert_eq!(r, Outcome::Error(XcmError::Barrier));
let origin = Parachain(1000);
let message = Xcm::<TestCall>(vec![UnsubscribeVersion]);
let hash = fake_message_hash(&message);
let weight_limit = Weight::from_parts(10, 10);
let r = XcmExecutor::<TestConfig>::execute_xcm(origin, message.clone(), hash, weight_limit);
assert_eq!(r, Outcome::Error(XcmError::Barrier));
let r = XcmExecutor::<TestConfig>::execute_xcm(Parent, message, hash, weight_limit);
assert_eq!(r, Outcome::Complete(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 = Parachain(1000);
// Not allowed to do it when origin has been changed.
let message = Xcm::<TestCall>(vec![
DescendOrigin(X1(AccountIndex64 { index: 1, network: None })),
UnsubscribeVersion,
]);
let hash = fake_message_hash(&message);
let weight_limit = Weight::from_parts(20, 20);
let r = XcmExecutor::<TestConfig>::execute_xcm_in_credit(
origin.clone(),
message,
hash,
weight_limit,
weight_limit,
);
assert_eq!(r, Outcome::Incomplete(Weight::from_parts(20, 20), XcmError::BadOrigin));
// Fine to do it when origin is untouched.
let message = Xcm::<TestCall>(vec![SetAppendix(Xcm(vec![])), UnsubscribeVersion]);
let hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::execute_xcm_in_credit(
origin,
message,
hash,
weight_limit,
weight_limit,
);
assert_eq!(r, Outcome::Complete(Weight::from_parts(20, 20)));
assert_eq!(SubscriptionRequests::get(), vec![(Parachain(1000).into(), None)]);
assert_eq!(sent_xcm(), vec![]);
}
@@ -0,0 +1,183 @@
// Copyright 2022 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Polkadot 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.
// Polkadot 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 Polkadot. 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();
// 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()),
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()),
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()),
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()),
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: X1(AccountIndex64 { index: 3, network: None }).into(),
},
// Second xfer results in error third message and after
TransferAsset {
assets: (Here, 2u128).into(),
beneficiary: X1(AccountIndex64 { index: 3, network: None }).into(),
},
// Third xfer results in error second message and after
TransferAsset {
assets: (Here, 4u128).into(),
beneficiary: X1(AccountIndex64 { index: 3, network: None }).into(),
},
]);
// Weight limit of 70 is needed.
let limit = <TestConfig as Config>::Weigher::weight(&mut message).unwrap();
assert_eq!(limit, Weight::from_parts(30, 30));
let hash = fake_message_hash(&message);
let r = XcmExecutor::<TestConfig>::execute_xcm(Here, message.clone(), hash, limit);
assert_eq!(r, Outcome::Complete(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>::execute_xcm(Here, message.clone(), hash, limit);
assert_eq!(r, Outcome::Incomplete(Weight::from_parts(30, 30), 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>::execute_xcm(Here, message.clone(), hash, limit);
assert_eq!(r, Outcome::Incomplete(Weight::from_parts(20, 20), 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>::execute_xcm(Here, message, hash, limit);
assert_eq!(r, Outcome::Incomplete(Weight::from_parts(10, 10), 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() {
MaxInstructions::set(3);
let mut message = Xcm(vec![ClearOrigin; 4]);
// 4 instructions are too many.
assert_eq!(<TestConfig as Config>::Weigher::weight(&mut message), Err(()));
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), Err(()));
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), Err(()));
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),
Ok(Weight::from_parts(30, 30))
);
}
#[test]
fn weight_trader_tuple_should_work() {
let para_1: MultiLocation = Parachain(1).into();
let para_2: MultiLocation = Parachain(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) =
(Parachain(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();
// trader one buys weight
assert_eq!(
traders.buy_weight(Weight::from_parts(5, 5), fungible_multi_asset(Here.into(), 10).into()),
Ok(vec![].into()),
);
// trader one refunds
assert_eq!(
traders.refund_weight(Weight::from_parts(2, 2)),
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, 10).into()),
Ok(vec![].into()),
);
// trader two refunds
assert_eq!(
traders.refund_weight(Weight::from_parts(2, 2)),
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()),
XcmError::TooExpensive,
);
// and no refund
assert_eq!(traders.refund_weight(Weight::from_parts(2, 2)), None);
}