mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-07-22 18:25:41 +00:00
8428f678fe
# Note for reviewer
Most changes are just syntax changes necessary for the new version.
Most important files should be the ones under the `xcm` folder.
# Description
Added XCMv4.
## Removed `Multi` prefix
The following types have been renamed:
- MultiLocation -> Location
- MultiAsset -> Asset
- MultiAssets -> Assets
- InteriorMultiLocation -> InteriorLocation
- MultiAssetFilter -> AssetFilter
- VersionedMultiAsset -> VersionedAsset
- WildMultiAsset -> WildAsset
- VersionedMultiLocation -> VersionedLocation
In order to fix a name conflict, the `Assets` in `xcm-executor` were
renamed to `HoldingAssets`, as they represent assets in holding.
## Removed `Abstract` asset id
It was not being used anywhere and this simplifies the code.
Now assets are just constructed as follows:
```rust
let asset: Asset = (AssetId(Location::new(1, Here)), 100u128).into();
```
No need for specifying `Concrete` anymore.
## Outcome is now a named fields struct
Instead of
```rust
pub enum Outcome {
Complete(Weight),
Incomplete(Weight, Error),
Error(Error),
}
```
we now have
```rust
pub enum Outcome {
Complete { used: Weight },
Incomplete { used: Weight, error: Error },
Error { error: Error },
}
```
## Added Reanchorable trait
Now both locations and assets implement this trait, making it easier to
reanchor both.
## New syntax for building locations and junctions
Now junctions are built using the following methods:
```rust
let location = Location {
parents: 1,
interior: [Parachain(1000), PalletInstance(50), GeneralIndex(1984)].into()
};
```
or
```rust
let location = Location::new(1, [Parachain(1000), PalletInstance(50), GeneralIndex(1984)]);
```
And they are matched like so:
```rust
match location.unpack() {
(1, [Parachain(id)]) => ...
(0, Here) => ...,
(1, [_]) => ...,
}
```
This syntax is mandatory in v4, and has been also implemented for v2 and
v3 for easier migration.
This was needed to make all sizes smaller.
# TODO
- [x] Scaffold v4
- [x] Port github.com/paritytech/polkadot/pull/7236
- [x] Remove `Multi` prefix
- [x] Remove `Abstract` asset id
---------
Co-authored-by: command-bot <>
Co-authored-by: Keith Yeung <kungfukeith11@gmail.com>
244 lines
7.2 KiB
Rust
244 lines
7.2 KiB
Rust
// Copyright (C) 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();
|
|
let ctx = XcmContext { origin: None, message_id: XcmHash::default(), topic: None };
|
|
|
|
// supplies 100 unit of asset, 80 still remains after purchasing weight
|
|
assert_eq!(
|
|
trader.buy_weight(
|
|
Weight::from_parts(10, 10),
|
|
fungible_multi_asset(Here.into(), 100).into(),
|
|
&ctx,
|
|
),
|
|
Ok(fungible_multi_asset(Here.into(), 80).into()),
|
|
);
|
|
// should have nothing left, as 5 + 5 = 10, and we supplied 10 units of asset.
|
|
assert_eq!(
|
|
trader.buy_weight(
|
|
Weight::from_parts(5, 5),
|
|
fungible_multi_asset(Here.into(), 10).into(),
|
|
&ctx,
|
|
),
|
|
Ok(vec![].into()),
|
|
);
|
|
// should have 5 left, as there are no proof size components
|
|
assert_eq!(
|
|
trader.buy_weight(
|
|
Weight::from_parts(5, 0),
|
|
fungible_multi_asset(Here.into(), 10).into(),
|
|
&ctx,
|
|
),
|
|
Ok(fungible_multi_asset(Here.into(), 5).into()),
|
|
);
|
|
// not enough to purchase the combined weights
|
|
assert_err!(
|
|
trader.buy_weight(
|
|
Weight::from_parts(5, 5),
|
|
fungible_multi_asset(Here.into(), 5).into(),
|
|
&ctx,
|
|
),
|
|
XcmError::TooExpensive,
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn errors_should_return_unused_weight() {
|
|
// we'll let them have message execution for free.
|
|
AllowUnpaidFrom::set(vec![Here.into()]);
|
|
// We own 1000 of our tokens.
|
|
add_asset(Here, (Here, 11u128));
|
|
let mut message = Xcm(vec![
|
|
// First xfer results in an error on the last message only
|
|
TransferAsset {
|
|
assets: (Here, 1u128).into(),
|
|
beneficiary: [AccountIndex64 { index: 3, network: None }].into(),
|
|
},
|
|
// Second xfer results in error third message and after
|
|
TransferAsset {
|
|
assets: (Here, 2u128).into(),
|
|
beneficiary: [AccountIndex64 { index: 3, network: None }].into(),
|
|
},
|
|
// Third xfer results in error second message and after
|
|
TransferAsset {
|
|
assets: (Here, 4u128).into(),
|
|
beneficiary: [AccountIndex64 { index: 3, network: None }].into(),
|
|
},
|
|
]);
|
|
// Weight limit of 70 is needed.
|
|
let limit = <TestConfig as Config>::Weigher::weight(&mut message).unwrap();
|
|
assert_eq!(limit, Weight::from_parts(30, 30));
|
|
|
|
let mut hash = fake_message_hash(&message);
|
|
|
|
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
|
|
Here,
|
|
message.clone(),
|
|
&mut hash,
|
|
limit,
|
|
Weight::zero(),
|
|
);
|
|
assert_eq!(r, Outcome::Complete { used: Weight::from_parts(30, 30) });
|
|
assert_eq!(asset_list(AccountIndex64 { index: 3, network: None }), vec![(Here, 7u128).into()]);
|
|
assert_eq!(asset_list(Here), vec![(Here, 4u128).into()]);
|
|
assert_eq!(sent_xcm(), vec![]);
|
|
|
|
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
|
|
Here,
|
|
message.clone(),
|
|
&mut hash,
|
|
limit,
|
|
Weight::zero(),
|
|
);
|
|
assert_eq!(
|
|
r,
|
|
Outcome::Incomplete { used: Weight::from_parts(30, 30), error: XcmError::NotWithdrawable }
|
|
);
|
|
assert_eq!(asset_list(AccountIndex64 { index: 3, network: None }), vec![(Here, 10u128).into()]);
|
|
assert_eq!(asset_list(Here), vec![(Here, 1u128).into()]);
|
|
assert_eq!(sent_xcm(), vec![]);
|
|
|
|
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
|
|
Here,
|
|
message.clone(),
|
|
&mut hash,
|
|
limit,
|
|
Weight::zero(),
|
|
);
|
|
assert_eq!(
|
|
r,
|
|
Outcome::Incomplete { used: Weight::from_parts(20, 20), error: XcmError::NotWithdrawable }
|
|
);
|
|
assert_eq!(asset_list(AccountIndex64 { index: 3, network: None }), vec![(Here, 11u128).into()]);
|
|
assert_eq!(asset_list(Here), vec![]);
|
|
assert_eq!(sent_xcm(), vec![]);
|
|
|
|
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
|
|
Here,
|
|
message,
|
|
&mut hash,
|
|
limit,
|
|
Weight::zero(),
|
|
);
|
|
assert_eq!(
|
|
r,
|
|
Outcome::Incomplete { used: Weight::from_parts(10, 10), error: 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: Location = Parachain(1).into();
|
|
let para_2: Location = 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();
|
|
let ctx = XcmContext { origin: None, message_id: XcmHash::default(), topic: None };
|
|
|
|
// trader one buys weight
|
|
assert_eq!(
|
|
traders.buy_weight(
|
|
Weight::from_parts(5, 5),
|
|
fungible_multi_asset(Here.into(), 10).into(),
|
|
&ctx
|
|
),
|
|
Ok(vec![].into()),
|
|
);
|
|
// trader one refunds
|
|
assert_eq!(
|
|
traders.refund_weight(Weight::from_parts(2, 2), &ctx),
|
|
Some(fungible_multi_asset(Here.into(), 4))
|
|
);
|
|
|
|
let mut traders = Traders::new();
|
|
// trader one failed; trader two buys weight
|
|
assert_eq!(
|
|
traders.buy_weight(
|
|
Weight::from_parts(5, 5),
|
|
fungible_multi_asset(para_1.clone(), 10).into(),
|
|
&ctx
|
|
),
|
|
Ok(vec![].into()),
|
|
);
|
|
// trader two refunds
|
|
assert_eq!(
|
|
traders.refund_weight(Weight::from_parts(2, 2), &ctx),
|
|
Some(fungible_multi_asset(para_1, 4))
|
|
);
|
|
|
|
let mut traders = Traders::new();
|
|
// all traders fails
|
|
assert_err!(
|
|
traders.buy_weight(Weight::from_parts(5, 5), fungible_multi_asset(para_2, 10).into(), &ctx),
|
|
XcmError::TooExpensive,
|
|
);
|
|
// and no refund
|
|
assert_eq!(traders.refund_weight(Weight::from_parts(2, 2), &ctx), None);
|
|
}
|