Skip to content

Commit

Permalink
XCMv4 (paritytech#1230)
Browse files Browse the repository at this point in the history
# 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>
  • Loading branch information
franciscoaguirre and KiChjang authored Jan 16, 2024
1 parent 41af9f2 commit 52f5bc9
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 128 deletions.
121 changes: 63 additions & 58 deletions substrate/frame/assets/src/benchmarking.rs

Large diffs are not rendered by default.

6 changes: 1 addition & 5 deletions substrate/frame/assets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,11 +259,7 @@ pub mod pallet {
/// This type includes the `From<Self::AssetId>` bound, since tightly coupled pallets may
/// want to convert an `AssetId` into a parameter for calling dispatchable functions
/// directly.
type AssetIdParameter: Parameter
+ Copy
+ From<Self::AssetId>
+ Into<Self::AssetId>
+ MaxEncodedLen;
type AssetIdParameter: Parameter + From<Self::AssetId> + Into<Self::AssetId> + MaxEncodedLen;

/// The currency mechanism.
type Currency: ReservableCurrency<Self::AccountId>;
Expand Down
8 changes: 4 additions & 4 deletions substrate/frame/contracts/mock-network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -67,20 +67,20 @@ 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()
}

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() },
)
Expand Down
27 changes: 20 additions & 7 deletions substrate/frame/contracts/mock-network/src/mocks/msg_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))),
Expand Down Expand Up @@ -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(_) => {
Expand All @@ -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));
},
Expand Down
62 changes: 28 additions & 34 deletions substrate/frame/contracts/mock-network/src/parachain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand All @@ -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 {
Expand All @@ -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
}
}
Expand All @@ -211,7 +210,7 @@ pub type ForeignAssetsTransactor = FungiblesAdapter<
ConvertedConcreteId<
AssetIdForAssets,
Balance,
FromMultiLocationToAsset<MultiLocation, AssetIdForAssets>,
FromLocationToAsset<Location, AssetIdForAssets>,
JustTry,
>,
SovereignAccountOf,
Expand All @@ -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 { .. }]))
}
}

Expand All @@ -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>);
Expand Down Expand Up @@ -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
}
Expand Down
12 changes: 6 additions & 6 deletions substrate/frame/contracts/mock-network/src/relay_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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(_)]))
}
}

Expand Down
25 changes: 14 additions & 11 deletions substrate/frame/contracts/mock-network/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>;
Expand Down Expand Up @@ -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(),
Expand All @@ -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,
Expand All @@ -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.
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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);
Expand All @@ -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(),
Expand Down
4 changes: 2 additions & 2 deletions substrate/frame/contracts/src/wasm/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2169,11 +2169,11 @@ pub mod env {
msg_len: u32,
output_ptr: u32,
) -> Result<ReturnErrorCode, TrapReason> {
use xcm::{VersionedMultiLocation, VersionedXcm};
use xcm::{VersionedLocation, VersionedXcm};
use xcm_builder::{SendController, SendControllerWeightInfo};

ctx.charge_gas(RuntimeCosts::CopyFromContract(msg_len))?;
let dest: VersionedMultiLocation = ctx.read_sandbox_memory_as(memory, dest_ptr)?;
let dest: VersionedLocation = ctx.read_sandbox_memory_as(memory, dest_ptr)?;

let message: VersionedXcm<()> =
ctx.read_sandbox_memory_as_unbounded(memory, msg_ptr, msg_len)?;
Expand Down
2 changes: 1 addition & 1 deletion substrate/frame/support/src/traits/tokens/pay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use sp_std::fmt::Debug;
use super::{fungible, fungibles, Balance, Preservation::Expendable};

/// Can be implemented by `PayFromAccount` using a `fungible` impl, but can also be implemented with
/// XCM/MultiAsset and made generic over assets.
/// XCM/Asset and made generic over assets.
pub trait Pay {
/// The type by which we measure units of the currency in which we make payments.
type Balance: Balance;
Expand Down

0 comments on commit 52f5bc9

Please sign in to comment.