Ensure MultiLocation always has a canonical representation (#3404)

* MultiAsset TWO

* Ensure MultiLocation always has a canonical representation

* Remove v1 module

* Draft next MultiAsset API.

* Implement custom encoding/decoding scheme for MultiLocation

* Properly implement IntoIterator for Junctions

* Implement TryFrom<MultiLocation> for Junctions

* Fix spelling mistakes

* Fix tests in xcm-executor

* XCM core builds

* XCM Executor builds

* XCM Builder builds

* Fix xcm-builder tests and compilation

* Make pallet-xcm compile

* Use MultiLocation::default()

* Make polkadot-runtime-common compile

* Make rococo-runtime compile

* Change return type of parent_count to u8

* Change MAX_MULTILOCATION_LENGTH to 255

* Make kusama-runtime compile

* Fix logic in pallet-xcm

* Use MultiLocation::empty()

* Fix logic in location_conversion

* Fix logic in origin_conversion.rs

* Make westend-runtime compile

* Rename prefixes and suffixes variables

* Rename non_parent to interior

* Rename non_parent to interior

* Add test for encode/decode roundtrip and fix decode algorithm

* API changes making their way throughout

* Some TODOs

* Further build fixes

* Rename non_parent/junctions to interior

* Basic compile builds

* First test fixed

* All executor tests fixed

* Typo

* Optimize subsume_assets and add test

* Optimize checked_sub

* XCM Builder first test fixed

* Fix builder tests

* Fix doc test

* Make xcm-simulator compile

* Make xcm-simulator-example compile

* Make spellcheck happy

* cargo fmt

* fix some doc tests

* spelling

* named fields for AllOf

* Fix subtle bug where Null is treated as an identifier

* Add FIXME comment awaiting for const generics eval stabilization

* Update xcm/src/v0/multiasset.rs

Co-authored-by: Alexander Popiak <alexander.popiak@parity.io>

* Update xcm/src/v0/multiasset.rs

Co-authored-by: Alexander Popiak <alexander.popiak@parity.io>

* Update xcm/src/v0/multiasset.rs

Co-authored-by: Alexander Popiak <alexander.popiak@parity.io>

* Update xcm/src/v0/multiasset.rs

Co-authored-by: Alexander Popiak <alexander.popiak@parity.io>

* Reformat

* Move to XCM version 1

* Spelling

* warnings

* Replace some more v0->v1s

* warnings

* format

* Add max_assets param

* building

* test fixes

* tests

* another test

* final test

* Update rustdocs and add debug_assert where sensible

* Revert debug_assert in const fn len()

* tests

* Rename Null -> Here

* Introduce

* More ergonomics

* More ergonomics

* test fix

* test fixes

* docs

* BuyExecution includes

* Fix XCM extrinsics

* fmt

* Make Vec<MultiAsset>/MultiAssets conversions safe

* More MultiAssets conversion safety

* spelling

* fix doc test

* Apply suggestions from code review

Co-authored-by: Amar Singh <asinghchrony@protonmail.com>

* Apply suggestions from code review

Co-authored-by: Amar Singh <asinghchrony@protonmail.com>

* fmt

* Add v0, remove VersionedMultiAsset

* Remove VersionedMultiLocation

* Update xcm/src/v1/order.rs

Co-authored-by: Amar Singh <asinghchrony@protonmail.com>

* Update xcm/src/v1/mod.rs

Co-authored-by: Amar Singh <asinghchrony@protonmail.com>

* XCM v0 backwards compatibility

* Full compatibility

* fmt

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

* Update xcm/src/v0/order.rs

Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>

* Tweaks to versioning system

* Fixes

* fmt

* Fix pallet-xcm tests

* fix

* Substitute with_parent with with_parents_const

* Rename argument name from a to m

* Rename split_last to split_last_interior

* Allow adding multiple parents in MultiLocation

* Rename pop_parent to dec_parent

* Ensure relay chain XCM sender receives a MultiLocation without any parents

* Block only when MultiLocation destination length is 8

* Cargo fmt

* Remove reverse iterators, implement DoubleEndedIterator and add tests

* Fix iter_rev lifetime requirements

* Cargo fmt

* Add an into() method for Junctions for conciseness in const context

* Ensure parent count is 0 while executing who in RelayedFrom

* Appease spellchecker

* Use and_then instead of repeated map_err

* Remove custom codec indices for v1 Junctions

* Add convenience 'contains_parents_only' method to MultiLocation

* Fix merge conflict

* Use more convenience methods

* Remove with_parachain_interior

* Prefer matching against tuple instead of using match guards

* Match against tuple instead of using more match guards

* Update encode/decode test for MultiLocation

* Minor tweaks

* Fixes

* Fixes

* Fixes

* Fix MultiLocation

* Add deprecation note for iter_rev and into_iter_rev

* Update some rustdocs

* cargo fmt

* Fix xcm-executor unit tests

* Fix compilation and unit tests in xcm-builder

* cargo fmt

* Fix tests in xcm-simulator-example

* Publicize MultiLocation fields

* Match on the MultiLocation struct directly in xcm-builder

* Do not dereference undereferenceable types

* Add convenience MultiLocation conversions for tuples

* Use clearer import paths

* Remove unused dependency

* fix junction + response

* Import from latest opaque xcm module

* Update xcm/src/v1/mod.rs

* better comment

* Fix ownership transfer

* Fix merge

* Fix merge

* cargo fmt

* Fix merge

* Fix merge

* Fix integration test

* More readable Parent syntax

* cleanup

* cleanup

* cleanup

* cleanup

* cleanup

* cleanup

* cleanup

* cleanup

* cargo fmt

* Fixes

* Fix doc test

Co-authored-by: Gav Wood <gavin@parity.io>
Co-authored-by: Alexander Popiak <alexander.popiak@parity.io>
Co-authored-by: Amar Singh <asinghchrony@protonmail.com>
Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>
This commit is contained in:
Keith Yeung
2021-08-09 20:55:40 +02:00
committed by GitHub
parent 276dc1981e
commit 1556873060
36 changed files with 1683 additions and 952 deletions
+78 -62
View File
@@ -20,35 +20,41 @@ use xcm_executor::{traits::*, Config, XcmExecutor};
#[test]
fn basic_setup_works() {
add_reserve(X1(Parent), Wild((X1(Parent), WildFungible).into()));
add_reserve(Parent.into(), Wild((Parent, WildFungible).into()));
assert!(<TestConfig as Config>::IsReserve::filter_asset_location(
&(X1(Parent), 100).into(),
&X1(Parent),
&(Parent, 100).into(),
&Parent.into(),
));
assert_eq!(to_account(X1(Parachain(1))), Ok(1001));
assert_eq!(to_account(X1(Parachain(50))), Ok(1050));
assert_eq!(to_account(X2(Parent, Parachain(1))), Ok(2001));
assert_eq!(to_account(X2(Parent, Parachain(50))), Ok(2050));
assert_eq!(to_account(X1(AccountIndex64 { index: 1, network: Any })), Ok(1));
assert_eq!(to_account(X1(AccountIndex64 { index: 42, network: Any })), Ok(42));
assert_eq!(to_account(Here), Ok(3000));
assert_eq!(to_account(X1(Parachain(1)).into()), Ok(1001));
assert_eq!(to_account(X1(Parachain(50)).into()), Ok(1050));
assert_eq!(to_account(MultiLocation::new(1, X1(Parachain(1)))), Ok(2001));
assert_eq!(to_account(MultiLocation::new(1, X1(Parachain(50)))), Ok(2050));
assert_eq!(
to_account(MultiLocation::new(0, X1(AccountIndex64 { index: 1, network: Any }))),
Ok(1),
);
assert_eq!(
to_account(MultiLocation::new(0, X1(AccountIndex64 { index: 42, network: Any }))),
Ok(42),
);
assert_eq!(to_account(Here.into()), Ok(3000));
}
#[test]
fn weigher_should_work() {
let mut message = opaque::Xcm::ReserveAssetDeposited {
assets: (X1(Parent), 100).into(),
assets: (Parent, 100).into(),
effects: vec![
Order::BuyExecution {
fees: (X1(Parent), 1).into(),
fees: (Parent, 1).into(),
weight: 0,
debt: 30,
halt_on_error: true,
orders: vec![],
instructions: vec![],
},
Order::DepositAsset { assets: All.into(), max_assets: 1, beneficiary: Here },
Order::DepositAsset { assets: All.into(), max_assets: 1, beneficiary: Here.into() },
],
}
.into();
@@ -58,16 +64,26 @@ fn weigher_should_work() {
#[test]
fn take_weight_credit_barrier_should_work() {
let mut message =
opaque::Xcm::TransferAsset { assets: (X1(Parent), 100).into(), beneficiary: Here };
opaque::Xcm::TransferAsset { assets: (Parent, 100).into(), beneficiary: Here.into() };
let mut weight_credit = 10;
let r =
TakeWeightCredit::should_execute(&X1(Parent), true, &mut message, 10, &mut weight_credit);
let r = TakeWeightCredit::should_execute(
&Parent.into(),
true,
&mut message,
10,
&mut weight_credit,
);
assert_eq!(r, Ok(()));
assert_eq!(weight_credit, 0);
let r =
TakeWeightCredit::should_execute(&X1(Parent), true, &mut message, 10, &mut weight_credit);
let r = TakeWeightCredit::should_execute(
&Parent.into(),
true,
&mut message,
10,
&mut weight_credit,
);
assert_eq!(r, Err(()));
assert_eq!(weight_credit, 0);
}
@@ -75,12 +91,12 @@ fn take_weight_credit_barrier_should_work() {
#[test]
fn allow_unpaid_should_work() {
let mut message =
opaque::Xcm::TransferAsset { assets: (X1(Parent), 100).into(), beneficiary: Here };
opaque::Xcm::TransferAsset { assets: (Parent, 100).into(), beneficiary: Here.into() };
AllowUnpaidFrom::set(vec![X1(Parent)]);
AllowUnpaidFrom::set(vec![Parent.into()]);
let r = AllowUnpaidExecutionFrom::<IsInVec<AllowUnpaidFrom>>::should_execute(
&X1(Parachain(1)),
&Parachain(1).into(),
true,
&mut message,
10,
@@ -89,7 +105,7 @@ fn allow_unpaid_should_work() {
assert_eq!(r, Err(()));
let r = AllowUnpaidExecutionFrom::<IsInVec<AllowUnpaidFrom>>::should_execute(
&X1(Parent),
&Parent.into(),
true,
&mut message,
10,
@@ -100,13 +116,13 @@ fn allow_unpaid_should_work() {
#[test]
fn allow_paid_should_work() {
AllowPaidFrom::set(vec![X1(Parent)]);
AllowPaidFrom::set(vec![Parent.into()]);
let mut message =
opaque::Xcm::TransferAsset { assets: (X1(Parent), 100).into(), beneficiary: Here };
opaque::Xcm::TransferAsset { assets: (Parent, 100).into(), beneficiary: Here.into() };
let r = AllowTopLevelPaidExecutionFrom::<IsInVec<AllowPaidFrom>>::should_execute(
&X1(Parachain(1)),
&Parachain(1).into(),
true,
&mut message,
10,
@@ -114,9 +130,9 @@ fn allow_paid_should_work() {
);
assert_eq!(r, Err(()));
let fees = (X1(Parent), 1).into();
let fees = (Parent, 1).into();
let mut underpaying_message = opaque::Xcm::ReserveAssetDeposited {
assets: (X1(Parent), 100).into(),
assets: (Parent, 100).into(),
effects: vec![
Order::BuyExecution {
fees,
@@ -126,12 +142,12 @@ fn allow_paid_should_work() {
orders: vec![],
instructions: vec![],
},
Order::DepositAsset { assets: All.into(), max_assets: 1, beneficiary: Here },
Order::DepositAsset { assets: All.into(), max_assets: 1, beneficiary: Here.into() },
],
};
let r = AllowTopLevelPaidExecutionFrom::<IsInVec<AllowPaidFrom>>::should_execute(
&X1(Parent),
&Parent.into(),
true,
&mut underpaying_message,
30,
@@ -139,9 +155,9 @@ fn allow_paid_should_work() {
);
assert_eq!(r, Err(()));
let fees = (X1(Parent), 1).into();
let fees = (Parent, 1).into();
let mut paying_message = opaque::Xcm::ReserveAssetDeposited {
assets: (X1(Parent), 100).into(),
assets: (Parent, 100).into(),
effects: vec![
Order::BuyExecution {
fees,
@@ -151,12 +167,12 @@ fn allow_paid_should_work() {
orders: vec![],
instructions: vec![],
},
Order::DepositAsset { assets: All.into(), max_assets: 1, beneficiary: Here },
Order::DepositAsset { assets: All.into(), max_assets: 1, beneficiary: Here.into() },
],
};
let r = AllowTopLevelPaidExecutionFrom::<IsInVec<AllowPaidFrom>>::should_execute(
&X1(Parachain(1)),
&Parachain(1).into(),
true,
&mut paying_message,
30,
@@ -165,7 +181,7 @@ fn allow_paid_should_work() {
assert_eq!(r, Err(()));
let r = AllowTopLevelPaidExecutionFrom::<IsInVec<AllowPaidFrom>>::should_execute(
&X1(Parent),
&Parent.into(),
true,
&mut paying_message,
30,
@@ -176,14 +192,14 @@ fn allow_paid_should_work() {
#[test]
fn paying_reserve_deposit_should_work() {
AllowPaidFrom::set(vec![X1(Parent)]);
add_reserve(X1(Parent), (Parent, WildFungible).into());
AllowPaidFrom::set(vec![Parent.into()]);
add_reserve(Parent.into(), (Parent, WildFungible).into());
WeightPrice::set((Parent.into(), 1_000_000_000_000));
let origin = X1(Parent);
let fees = (X1(Parent), 30).into();
let origin = Parent.into();
let fees = (Parent, 30).into();
let message = Xcm::<TestCall>::ReserveAssetDeposited {
assets: (X1(Parent), 100).into(),
assets: (Parent, 100).into(),
effects: vec![
Order::<TestCall>::BuyExecution {
fees,
@@ -196,28 +212,28 @@ fn paying_reserve_deposit_should_work() {
Order::<TestCall>::DepositAsset {
assets: All.into(),
max_assets: 1,
beneficiary: Here,
beneficiary: Here.into(),
},
],
};
let weight_limit = 50;
let r = XcmExecutor::<TestConfig>::execute_xcm(origin, message, weight_limit);
assert_eq!(r, Outcome::Complete(30));
assert_eq!(assets(3000), vec![(X1(Parent), 70).into()]);
assert_eq!(assets(3000), vec![(Parent, 70).into()]);
}
#[test]
fn transfer_should_work() {
// we'll let them have message execution for free.
AllowUnpaidFrom::set(vec![X1(Parachain(1))]);
AllowUnpaidFrom::set(vec![X1(Parachain(1)).into()]);
// Child parachain #1 owns 1000 tokens held by us in reserve.
add_asset(1001, (Here, 1000).into());
// They want to transfer 100 of them to their sibling parachain #2
let r = XcmExecutor::<TestConfig>::execute_xcm(
X1(Parachain(1)),
Parachain(1).into(),
Xcm::TransferAsset {
assets: (Here, 100).into(),
beneficiary: X1(AccountIndex64 { index: 3, network: Any }),
beneficiary: X1(AccountIndex64 { index: 3, network: Any }).into(),
},
50,
);
@@ -229,19 +245,19 @@ fn transfer_should_work() {
#[test]
fn reserve_transfer_should_work() {
AllowUnpaidFrom::set(vec![X1(Parachain(1))]);
AllowUnpaidFrom::set(vec![X1(Parachain(1)).into()]);
// Child parachain #1 owns 1000 tokens held by us in reserve.
add_asset(1001, (Here, 1000).into());
// The remote account owned by gav.
let three = X1(AccountIndex64 { index: 3, network: Any });
let three: MultiLocation = X1(AccountIndex64 { index: 3, network: Any }).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 r = XcmExecutor::<TestConfig>::execute_xcm(
X1(Parachain(1)),
Parachain(1).into(),
Xcm::TransferReserveAsset {
assets: (Here, 100).into(),
dest: X1(Parachain(2)),
dest: Parachain(2).into(),
effects: vec![Order::DepositAsset {
assets: All.into(),
max_assets: 1,
@@ -256,9 +272,9 @@ fn reserve_transfer_should_work() {
assert_eq!(
sent_xcm(),
vec![(
X1(Parachain(2)),
Parachain(2).into(),
Xcm::ReserveAssetDeposited {
assets: (X1(Parent), 100).into(),
assets: (Parent, 100).into(),
effects: vec![Order::DepositAsset {
assets: All.into(),
max_assets: 1,
@@ -271,9 +287,9 @@ fn reserve_transfer_should_work() {
#[test]
fn transacting_should_work() {
AllowUnpaidFrom::set(vec![X1(Parent)]);
AllowUnpaidFrom::set(vec![Parent.into()]);
let origin = X1(Parent);
let origin = Parent.into();
let message = Xcm::<TestCall>::Transact {
origin_type: OriginKind::Native,
require_weight_at_most: 50,
@@ -286,9 +302,9 @@ fn transacting_should_work() {
#[test]
fn transacting_should_respect_max_weight_requirement() {
AllowUnpaidFrom::set(vec![X1(Parent)]);
AllowUnpaidFrom::set(vec![Parent.into()]);
let origin = X1(Parent);
let origin = Parent.into();
let message = Xcm::<TestCall>::Transact {
origin_type: OriginKind::Native,
require_weight_at_most: 40,
@@ -301,9 +317,9 @@ fn transacting_should_respect_max_weight_requirement() {
#[test]
fn transacting_should_refund_weight() {
AllowUnpaidFrom::set(vec![X1(Parent)]);
AllowUnpaidFrom::set(vec![Parent.into()]);
let origin = X1(Parent);
let origin = Parent.into();
let message = Xcm::<TestCall>::Transact {
origin_type: OriginKind::Native,
require_weight_at_most: 50,
@@ -316,15 +332,15 @@ fn transacting_should_refund_weight() {
#[test]
fn paid_transacting_should_refund_payment_for_unused_weight() {
let one = X1(AccountIndex64 { index: 1, network: Any });
let one: MultiLocation = X1(AccountIndex64 { index: 1, network: Any }).into();
AllowPaidFrom::set(vec![one.clone()]);
add_asset(1, (Parent, 100).into());
WeightPrice::set((Parent.into(), 1_000_000_000_000));
let origin = one.clone();
let fees = (X1(Parent), 100).into();
let fees = (Parent, 100).into();
let message = Xcm::<TestCall>::WithdrawAsset {
assets: (X1(Parent), 100).into(), // enough for 100 units of weight.
assets: (Parent, 100).into(), // enough for 100 units of weight.
effects: vec![
Order::<TestCall>::BuyExecution {
fees,
@@ -349,17 +365,17 @@ fn paid_transacting_should_refund_payment_for_unused_weight() {
let weight_limit = 100;
let r = XcmExecutor::<TestConfig>::execute_xcm(origin, message, weight_limit);
assert_eq!(r, Outcome::Complete(50));
assert_eq!(assets(1), vec![(X1(Parent), 50).into()]);
assert_eq!(assets(1), vec![(Parent, 50).into()]);
}
#[test]
fn prepaid_result_of_query_should_get_free_execution() {
let query_id = 33;
let origin = X1(Parent);
let origin: MultiLocation = Parent.into();
// We put this in manually here, but normally this would be done at the point of crafting the message.
expect_response(query_id, origin.clone());
let the_response = Response::Assets((X1(Parent), 100).into());
let the_response = Response::Assets((Parent, 100).into());
let message = Xcm::<TestCall>::QueryResponse { query_id, response: the_response.clone() };
let weight_limit = 10;