mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-15 12:41:07 +00:00
XCMv4 (#1230)
# 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>
This commit is contained in:
committed by
GitHub
parent
ec7bfae00a
commit
8428f678fe
@@ -24,7 +24,7 @@ mod tests;
|
||||
|
||||
use crate::primitives::{AccountId, UNITS};
|
||||
use sp_runtime::BuildStorage;
|
||||
use xcm::latest::{prelude::*, MultiLocation};
|
||||
use xcm::latest::prelude::*;
|
||||
use xcm_executor::traits::ConvertLocation;
|
||||
use xcm_simulator::{decl_test_network, decl_test_parachain, decl_test_relay_chain, TestExt};
|
||||
|
||||
@@ -67,12 +67,12 @@ decl_test_network! {
|
||||
}
|
||||
|
||||
pub fn relay_sovereign_account_id() -> AccountId {
|
||||
let location: MultiLocation = (Parent,).into();
|
||||
let location: Location = (Parent,).into();
|
||||
parachain::SovereignAccountOf::convert_location(&location).unwrap()
|
||||
}
|
||||
|
||||
pub fn parachain_sovereign_account_id(para: u32) -> AccountId {
|
||||
let location: MultiLocation = (Parachain(para),).into();
|
||||
let location: Location = (Parachain(para),).into();
|
||||
relay_chain::SovereignAccountOf::convert_location(&location).unwrap()
|
||||
}
|
||||
|
||||
@@ -80,7 +80,7 @@ pub fn parachain_account_sovereign_account_id(
|
||||
para: u32,
|
||||
who: sp_runtime::AccountId32,
|
||||
) -> AccountId {
|
||||
let location: MultiLocation = (
|
||||
let location: Location = (
|
||||
Parachain(para),
|
||||
AccountId32 { network: Some(relay_chain::RelayNetwork::get()), id: who.into() },
|
||||
)
|
||||
|
||||
@@ -96,16 +96,23 @@ pub mod pallet {
|
||||
max_weight: Weight,
|
||||
) -> Result<Weight, XcmError> {
|
||||
let hash = Encode::using_encoded(&xcm, T::Hashing::hash);
|
||||
let message_hash = Encode::using_encoded(&xcm, sp_io::hashing::blake2_256);
|
||||
let mut message_hash = Encode::using_encoded(&xcm, sp_io::hashing::blake2_256);
|
||||
let (result, event) = match Xcm::<T::RuntimeCall>::try_from(xcm) {
|
||||
Ok(xcm) => {
|
||||
let location = (Parent, Parachain(sender.into()));
|
||||
match T::XcmExecutor::execute_xcm(location, xcm, message_hash, max_weight) {
|
||||
Outcome::Error(e) => (Err(e), Event::Fail(Some(hash), e)),
|
||||
Outcome::Complete(w) => (Ok(w), Event::Success(Some(hash))),
|
||||
match T::XcmExecutor::prepare_and_execute(
|
||||
location,
|
||||
xcm,
|
||||
&mut message_hash,
|
||||
max_weight,
|
||||
Weight::zero(),
|
||||
) {
|
||||
Outcome::Error { error } => (Err(error), Event::Fail(Some(hash), error)),
|
||||
Outcome::Complete { used } => (Ok(used), Event::Success(Some(hash))),
|
||||
// As far as the caller is concerned, this was dispatched without error, so
|
||||
// we just report the weight used.
|
||||
Outcome::Incomplete(w, e) => (Ok(w), Event::Fail(Some(hash), e)),
|
||||
Outcome::Incomplete { used, error } =>
|
||||
(Ok(used), Event::Fail(Some(hash), error)),
|
||||
}
|
||||
},
|
||||
Err(()) => (Err(XcmError::UnhandledXcmVersion), Event::BadVersion(Some(hash))),
|
||||
@@ -146,7 +153,7 @@ pub mod pallet {
|
||||
limit: Weight,
|
||||
) -> Weight {
|
||||
for (_i, (_sent_at, data)) in iter.enumerate() {
|
||||
let id = sp_io::hashing::blake2_256(&data[..]);
|
||||
let mut id = sp_io::hashing::blake2_256(&data[..]);
|
||||
let maybe_versioned = VersionedXcm::<T::RuntimeCall>::decode(&mut &data[..]);
|
||||
match maybe_versioned {
|
||||
Err(_) => {
|
||||
@@ -155,7 +162,13 @@ pub mod pallet {
|
||||
Ok(versioned) => match Xcm::try_from(versioned) {
|
||||
Err(()) => Self::deposit_event(Event::UnsupportedVersion(id)),
|
||||
Ok(x) => {
|
||||
let outcome = T::XcmExecutor::execute_xcm(Parent, x.clone(), id, limit);
|
||||
let outcome = T::XcmExecutor::prepare_and_execute(
|
||||
Parent,
|
||||
x.clone(),
|
||||
&mut id,
|
||||
limit,
|
||||
Weight::zero(),
|
||||
);
|
||||
<ReceivedDmp<T>>::append(x);
|
||||
Self::deposit_event(Event::ExecutedDownward(id, outcome));
|
||||
},
|
||||
|
||||
@@ -143,10 +143,10 @@ parameter_types! {
|
||||
}
|
||||
|
||||
parameter_types! {
|
||||
pub const KsmLocation: MultiLocation = MultiLocation::parent();
|
||||
pub const TokenLocation: MultiLocation = Here.into_location();
|
||||
pub const KsmLocation: Location = Location::parent();
|
||||
pub const TokenLocation: Location = Here.into_location();
|
||||
pub const RelayNetwork: NetworkId = ByGenesis([0; 32]);
|
||||
pub UniversalLocation: InteriorMultiLocation = Parachain(MsgQueue::parachain_id().into()).into();
|
||||
pub UniversalLocation: InteriorLocation = Parachain(MsgQueue::parachain_id().into()).into();
|
||||
}
|
||||
|
||||
pub type XcmOriginToCallOrigin = (
|
||||
@@ -158,13 +158,13 @@ pub type XcmOriginToCallOrigin = (
|
||||
|
||||
parameter_types! {
|
||||
pub const XcmInstructionWeight: Weight = Weight::from_parts(1_000, 1_000);
|
||||
pub TokensPerSecondPerMegabyte: (AssetId, u128, u128) = (Concrete(Parent.into()), 1_000_000_000_000, 1024 * 1024);
|
||||
pub TokensPerSecondPerMegabyte: (AssetId, u128, u128) = (AssetId(Parent.into()), 1_000_000_000_000, 1024 * 1024);
|
||||
pub const MaxInstructions: u32 = 100;
|
||||
pub const MaxAssetsIntoHolding: u32 = 64;
|
||||
pub ForeignPrefix: MultiLocation = (Parent,).into();
|
||||
pub ForeignPrefix: Location = (Parent,).into();
|
||||
pub CheckingAccount: AccountId = PolkadotXcm::check_account();
|
||||
pub TrustedLockPairs: (MultiLocation, MultiAssetFilter) =
|
||||
(Parent.into(), Wild(AllOf { id: Concrete(Parent.into()), fun: WildFungible }));
|
||||
pub TrustedLockPairs: (Location, AssetFilter) =
|
||||
(Parent.into(), Wild(AllOf { id: AssetId(Parent.into()), fun: WildFungible }));
|
||||
}
|
||||
|
||||
pub fn estimate_message_fee(number_of_instructions: u64) -> u128 {
|
||||
@@ -188,20 +188,19 @@ pub fn estimate_fee_for_weight(weight: Weight) -> u128 {
|
||||
pub type LocalBalancesTransactor =
|
||||
XcmCurrencyAdapter<Balances, IsConcrete<TokenLocation>, SovereignAccountOf, AccountId, ()>;
|
||||
|
||||
pub struct FromMultiLocationToAsset<MultiLocation, AssetId>(PhantomData<(MultiLocation, AssetId)>);
|
||||
impl MaybeEquivalence<MultiLocation, AssetIdForAssets>
|
||||
for FromMultiLocationToAsset<MultiLocation, AssetIdForAssets>
|
||||
pub struct FromLocationToAsset<Location, AssetId>(PhantomData<(Location, AssetId)>);
|
||||
impl MaybeEquivalence<Location, AssetIdForAssets>
|
||||
for FromLocationToAsset<Location, AssetIdForAssets>
|
||||
{
|
||||
fn convert(value: &MultiLocation) -> Option<AssetIdForAssets> {
|
||||
match *value {
|
||||
MultiLocation { parents: 1, interior: Here } => Some(0 as AssetIdForAssets),
|
||||
MultiLocation { parents: 1, interior: X1(Parachain(para_id)) } =>
|
||||
Some(para_id as AssetIdForAssets),
|
||||
fn convert(value: &Location) -> Option<AssetIdForAssets> {
|
||||
match value.unpack() {
|
||||
(1, []) => Some(0 as AssetIdForAssets),
|
||||
(1, [Parachain(para_id)]) => Some(*para_id as AssetIdForAssets),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn convert_back(_id: &AssetIdForAssets) -> Option<MultiLocation> {
|
||||
fn convert_back(_id: &AssetIdForAssets) -> Option<Location> {
|
||||
None
|
||||
}
|
||||
}
|
||||
@@ -211,7 +210,7 @@ pub type ForeignAssetsTransactor = FungiblesAdapter<
|
||||
ConvertedConcreteId<
|
||||
AssetIdForAssets,
|
||||
Balance,
|
||||
FromMultiLocationToAsset<MultiLocation, AssetIdForAssets>,
|
||||
FromLocationToAsset<Location, AssetIdForAssets>,
|
||||
JustTry,
|
||||
>,
|
||||
SovereignAccountOf,
|
||||
@@ -224,18 +223,15 @@ pub type ForeignAssetsTransactor = FungiblesAdapter<
|
||||
pub type AssetTransactors = (LocalBalancesTransactor, ForeignAssetsTransactor);
|
||||
|
||||
pub struct ParentRelay;
|
||||
impl Contains<MultiLocation> for ParentRelay {
|
||||
fn contains(location: &MultiLocation) -> bool {
|
||||
impl Contains<Location> for ParentRelay {
|
||||
fn contains(location: &Location) -> bool {
|
||||
location.contains_parents_only(1)
|
||||
}
|
||||
}
|
||||
pub struct ThisParachain;
|
||||
impl Contains<MultiLocation> for ThisParachain {
|
||||
fn contains(location: &MultiLocation) -> bool {
|
||||
matches!(
|
||||
location,
|
||||
MultiLocation { parents: 0, interior: Junctions::X1(Junction::AccountId32 { .. }) }
|
||||
)
|
||||
impl Contains<Location> for ThisParachain {
|
||||
fn contains(location: &Location) -> bool {
|
||||
matches!(location.unpack(), (0, [Junction::AccountId32 { .. }]))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -251,12 +247,12 @@ pub type Barrier = (
|
||||
);
|
||||
|
||||
parameter_types! {
|
||||
pub NftCollectionOne: MultiAssetFilter
|
||||
= Wild(AllOf { fun: WildNonFungible, id: Concrete((Parent, GeneralIndex(1)).into()) });
|
||||
pub NftCollectionOneForRelay: (MultiAssetFilter, MultiLocation)
|
||||
pub NftCollectionOne: AssetFilter
|
||||
= Wild(AllOf { fun: WildNonFungible, id: AssetId((Parent, GeneralIndex(1)).into()) });
|
||||
pub NftCollectionOneForRelay: (AssetFilter, Location)
|
||||
= (NftCollectionOne::get(), Parent.into());
|
||||
pub RelayNativeAsset: MultiAssetFilter = Wild(AllOf { fun: WildFungible, id: Concrete((Parent, Here).into()) });
|
||||
pub RelayNativeAssetForRelay: (MultiAssetFilter, MultiLocation) = (RelayNativeAsset::get(), Parent.into());
|
||||
pub RelayNativeAsset: AssetFilter = Wild(AllOf { fun: WildFungible, id: AssetId((Parent, Here).into()) });
|
||||
pub RelayNativeAssetForRelay: (AssetFilter, Location) = (RelayNativeAsset::get(), Parent.into());
|
||||
}
|
||||
pub type TrustedTeleporters =
|
||||
(xcm_builder::Case<NftCollectionOneForRelay>, xcm_builder::Case<RelayNativeAssetForRelay>);
|
||||
@@ -298,10 +294,8 @@ impl mock_msg_queue::Config for Runtime {
|
||||
pub type LocalOriginToLocation = SignedToAccountId32<RuntimeOrigin, AccountId, RelayNetwork>;
|
||||
|
||||
pub struct TrustedLockerCase<T>(PhantomData<T>);
|
||||
impl<T: Get<(MultiLocation, MultiAssetFilter)>> ContainsPair<MultiLocation, MultiAsset>
|
||||
for TrustedLockerCase<T>
|
||||
{
|
||||
fn contains(origin: &MultiLocation, asset: &MultiAsset) -> bool {
|
||||
impl<T: Get<(Location, AssetFilter)>> ContainsPair<Location, Asset> for TrustedLockerCase<T> {
|
||||
fn contains(origin: &Location, asset: &Asset) -> bool {
|
||||
let (o, a) = T::get();
|
||||
a.matches(asset) && &o == origin
|
||||
}
|
||||
|
||||
@@ -107,8 +107,8 @@ impl configuration::Config for Runtime {
|
||||
|
||||
parameter_types! {
|
||||
pub RelayNetwork: NetworkId = ByGenesis([0; 32]);
|
||||
pub const TokenLocation: MultiLocation = Here.into_location();
|
||||
pub UniversalLocation: InteriorMultiLocation = Here;
|
||||
pub const TokenLocation: Location = Here.into_location();
|
||||
pub UniversalLocation: InteriorLocation = Here;
|
||||
pub UnitWeightCost: u64 = 1_000;
|
||||
}
|
||||
|
||||
@@ -134,15 +134,15 @@ type LocalOriginConverter = (
|
||||
parameter_types! {
|
||||
pub const XcmInstructionWeight: Weight = Weight::from_parts(1_000, 1_000);
|
||||
pub TokensPerSecondPerMegabyte: (AssetId, u128, u128) =
|
||||
(Concrete(TokenLocation::get()), 1_000_000_000_000, 1024 * 1024);
|
||||
(AssetId(TokenLocation::get()), 1_000_000_000_000, 1024 * 1024);
|
||||
pub const MaxInstructions: u32 = 100;
|
||||
pub const MaxAssetsIntoHolding: u32 = 64;
|
||||
}
|
||||
|
||||
pub struct ChildrenParachains;
|
||||
impl Contains<MultiLocation> for ChildrenParachains {
|
||||
fn contains(location: &MultiLocation) -> bool {
|
||||
matches!(location, MultiLocation { parents: 0, interior: X1(Parachain(_)) })
|
||||
impl Contains<Location> for ChildrenParachains {
|
||||
fn contains(location: &Location) -> bool {
|
||||
matches!(location.unpack(), (0, [Parachain(_)]))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -31,7 +31,7 @@ use frame_support::{
|
||||
use pallet_balances::{BalanceLock, Reasons};
|
||||
use pallet_contracts::{Code, CollectEvents, DebugInfo, Determinism};
|
||||
use pallet_contracts_fixtures::compile_module;
|
||||
use xcm::{v3::prelude::*, VersionedMultiLocation, VersionedXcm};
|
||||
use xcm::{v4::prelude::*, VersionedLocation, VersionedXcm};
|
||||
use xcm_simulator::TestExt;
|
||||
|
||||
type ParachainContracts = pallet_contracts::Pallet<parachain::Runtime>;
|
||||
@@ -82,7 +82,7 @@ fn test_xcm_execute() {
|
||||
let amount: u128 = 10 * CENTS;
|
||||
|
||||
// The XCM used to transfer funds to Bob.
|
||||
let message: xcm_simulator::Xcm<()> = Xcm(vec![
|
||||
let message: Xcm<()> = Xcm(vec![
|
||||
WithdrawAsset(vec![(Here, amount).into()].into()),
|
||||
DepositAsset {
|
||||
assets: All.into(),
|
||||
@@ -96,7 +96,7 @@ fn test_xcm_execute() {
|
||||
0,
|
||||
Weight::MAX,
|
||||
None,
|
||||
VersionedXcm::V3(message).encode(),
|
||||
VersionedXcm::V4(message).encode(),
|
||||
DebugInfo::UnsafeDebug,
|
||||
CollectEvents::UnsafeCollect,
|
||||
Determinism::Enforced,
|
||||
@@ -106,7 +106,7 @@ fn test_xcm_execute() {
|
||||
|
||||
let mut data = &result.data[..];
|
||||
let outcome = Outcome::decode(&mut data).expect("Failed to decode xcm_execute Outcome");
|
||||
assert_matches!(outcome, Outcome::Complete(_));
|
||||
assert_matches!(outcome, Outcome::Complete { .. });
|
||||
|
||||
// Check if the funds are subtracted from the account of Alice and added to the account of
|
||||
// Bob.
|
||||
@@ -137,7 +137,7 @@ fn test_xcm_execute_filtered_call() {
|
||||
0,
|
||||
Weight::MAX,
|
||||
None,
|
||||
VersionedXcm::V3(message).encode(),
|
||||
VersionedXcm::V4(message).encode(),
|
||||
DebugInfo::UnsafeDebug,
|
||||
CollectEvents::UnsafeCollect,
|
||||
Determinism::Enforced,
|
||||
@@ -178,7 +178,7 @@ fn test_xcm_execute_reentrant_call() {
|
||||
0,
|
||||
Weight::MAX,
|
||||
None,
|
||||
VersionedXcm::V3(message).encode(),
|
||||
VersionedXcm::V4(message).encode(),
|
||||
DebugInfo::UnsafeDebug,
|
||||
CollectEvents::UnsafeCollect,
|
||||
Determinism::Enforced,
|
||||
@@ -188,7 +188,10 @@ fn test_xcm_execute_reentrant_call() {
|
||||
|
||||
let mut data = &result.data[..];
|
||||
let outcome = Outcome::decode(&mut data).expect("Failed to decode xcm_execute Outcome");
|
||||
assert_matches!(outcome, Outcome::Incomplete(_, XcmError::ExpectationFalse));
|
||||
assert_matches!(
|
||||
outcome,
|
||||
Outcome::Incomplete { used: _, error: XcmError::ExpectationFalse }
|
||||
);
|
||||
|
||||
// Funds should not change hands as the XCM transact failed.
|
||||
assert_eq!(ParachainBalances::free_balance(BOB), INITIAL_BALANCE);
|
||||
@@ -203,15 +206,15 @@ fn test_xcm_send() {
|
||||
|
||||
// Send XCM instructions through the contract, to lock some funds on the relay chain.
|
||||
ParaA::execute_with(|| {
|
||||
let dest = MultiLocation::from(Parent);
|
||||
let dest = VersionedMultiLocation::V3(dest);
|
||||
let dest = Location::from(Parent);
|
||||
let dest = VersionedLocation::V4(dest);
|
||||
|
||||
let message: xcm_simulator::Xcm<()> = Xcm(vec![
|
||||
let message: Xcm<()> = Xcm(vec![
|
||||
WithdrawAsset((Here, fee).into()),
|
||||
BuyExecution { fees: (Here, fee).into(), weight_limit: WeightLimit::Unlimited },
|
||||
LockAsset { asset: (Here, 5 * CENTS).into(), unlocker: (Parachain(1)).into() },
|
||||
]);
|
||||
let message = VersionedXcm::V3(message);
|
||||
let message = VersionedXcm::V4(message);
|
||||
let exec = ParachainContracts::bare_call(
|
||||
ALICE,
|
||||
contract_addr.clone(),
|
||||
|
||||
Reference in New Issue
Block a user