Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

staging-xcm: Use versioned_type! for VersionedXcm #4378

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
181 changes: 57 additions & 124 deletions polkadot/xcm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,36 @@ pub trait TryAs<T> {
}

macro_rules! versioned_type {
(@internal $n:ident, $v3:ty, $v4:ty,) => {
impl IntoVersion for $n {
fn into_version(self, n: Version) -> Result<Self, ()> {
Ok(match n {
1 | 2 => Self::V2(self.try_into()?),
3 => Self::V3(self.try_into()?),
4 => Self::V4(self.try_into()?),
_ => return Err(()),
})
}
}
impl MaxEncodedLen for $n {
fn max_encoded_len() -> usize {
<$v3>::max_encoded_len().max(<$v4>::max_encoded_len())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can also fix for the other implementation of MaxEncodedLen when there is only v3 and v4:
https://github.com/paritytech/polkadot-sdk/pull/4378/files#diff-8eb727f0e7d0dd42a8d45ab05f9450777ed6af6d22021b81d0c449e8a32aba78R200

}
}
};
(@internal $n:ident, $v3:ty, $v4:ty, $t:ident) => {
// `VersionedXcm` version 1 support is dropped
impl<$t> IntoVersion for $n<$t>{
fn into_version(self, n: Version) -> Result<Self, ()> {
Ok(match n {
2 => Self::V2(self.try_into()?),
3 => Self::V3(self.try_into()?),
4 => Self::V4(self.try_into()?),
_ => return Err(()),
})
}
}
};
($(#[$attr:meta])* pub enum $n:ident {
$(#[$index3:meta])+
V3($v3:ty),
Expand Down Expand Up @@ -181,7 +211,7 @@ macro_rules! versioned_type {
}
};

($(#[$attr:meta])* pub enum $n:ident {
($(#[$attr:meta])* pub enum $n:ident $(<$($gen:ident),*>)?{
$(#[$index2:meta])+
V2($v2:ty),
$(#[$index3:meta])+
Expand All @@ -198,68 +228,59 @@ macro_rules! versioned_type {
)]
#[codec(encode_bound())]
#[codec(decode_bound())]
$(#[scale_info(bounds(), skip_type_params($($gen)+))])?
#[scale_info(replace_segment("staging_xcm", "xcm"))]
$(#[$attr])*
pub enum $n {
pub enum $n<$($($gen),*)?>{
$(#[$index2])*
V2($v2),
$(#[$index3])*
V3($v3),
$(#[$index4])*
V4($v4),
}
impl $n {
impl<$($($gen),*)?> $n<$($($gen),*)?> {
pub fn try_as<T>(&self) -> Result<&T, ()> where Self: TryAs<T> {
<Self as TryAs<T>>::try_as(&self)
}
}
impl TryAs<$v2> for $n {
impl<$($($gen),*)?> TryAs<$v2> for $n <$($($gen),*)?>{
fn try_as(&self) -> Result<&$v2, ()> {
match &self {
Self::V2(ref x) => Ok(x),
_ => Err(()),
}
}
}
impl TryAs<$v3> for $n {
impl<$($($gen),*)?> TryAs<$v3> for $n <$($($gen),*)?>{
fn try_as(&self) -> Result<&$v3, ()> {
match &self {
Self::V3(ref x) => Ok(x),
_ => Err(()),
}
}
}
impl TryAs<$v4> for $n {
impl<$($($gen),*)?> TryAs<$v4> for $n<$($($gen),*)?> {
fn try_as(&self) -> Result<&$v4, ()> {
match &self {
Self::V4(ref x) => Ok(x),
_ => Err(()),
}
}
}
impl IntoVersion for $n {
fn into_version(self, n: Version) -> Result<Self, ()> {
Ok(match n {
1 | 2 => Self::V2(self.try_into()?),
3 => Self::V3(self.try_into()?),
4 => Self::V4(self.try_into()?),
_ => return Err(()),
})
}
}
impl From<$v2> for $n {
impl<$($($gen),*)?> From<$v2> for $n<$($($gen),*)?> {
fn from(x: $v2) -> Self {
$n::V2(x)
}
}
impl<T: Into<$v4>> From<T> for $n {
impl<$($($gen,),+)? T: Into<$v4>> From<T> for $n<$($($gen),*)?> {
fn from(x: T) -> Self {
$n::V4(x.into())
}
}
impl TryFrom<$n> for $v2 {
impl<$($($gen),*)?>TryFrom<$n<$($($gen),*)?>> for $v2 {
type Error = ();
fn try_from(x: $n) -> Result<Self, ()> {
fn try_from(x: $n<$($($gen),*)?>) -> Result<Self, ()> {
use $n::*;
match x {
V2(x) => Ok(x),
Expand All @@ -271,9 +292,9 @@ macro_rules! versioned_type {
}
}
}
impl TryFrom<$n> for $v3 {
impl<$($($gen),*)?> TryFrom<$n<$($($gen),*)?>> for $v3 {
type Error = ();
fn try_from(x: $n) -> Result<Self, ()> {
fn try_from(x: $n <$($($gen),*)?>) -> Result<Self, ()> {
use $n::*;
match x {
V2(x) => x.try_into(),
Expand All @@ -282,9 +303,9 @@ macro_rules! versioned_type {
}
}
}
impl TryFrom<$n> for $v4 {
impl<$($($gen),*)?> TryFrom<$n <$($($gen),*)?>> for $v4 {
type Error = ();
fn try_from(x: $n) -> Result<Self, ()> {
fn try_from(x: $n<$($($gen),*)?>) -> Result<Self, ()> {
use $n::*;
match x {
V2(x) => {
Expand All @@ -296,12 +317,10 @@ macro_rules! versioned_type {
}
}
}
impl MaxEncodedLen for $n {
fn max_encoded_len() -> usize {
<$v3>::max_encoded_len()
}
}
impl IdentifyVersion for $n {
// internal macro that handles some edge cases
versioned_type!(@internal $n, $v3, $v4, $($($gen),+)?);

impl<$($($gen),*)?> IdentifyVersion for $n<$($($gen),*)?>{
fn identify_version(&self) -> Version {
use $n::*;
match self {
Expand Down Expand Up @@ -421,41 +440,15 @@ versioned_type! {
#[deprecated(note = "Use `VersionedAssets` instead")]
pub type VersionedMultiAssets = VersionedAssets;

/// A single XCM message, together with its version code.
#[derive(Derivative, Encode, Decode, TypeInfo)]
#[derivative(Clone(bound = ""), Eq(bound = ""), PartialEq(bound = ""), Debug(bound = ""))]
#[codec(encode_bound())]
#[codec(decode_bound())]
#[scale_info(bounds(), skip_type_params(RuntimeCall))]
#[scale_info(replace_segment("staging_xcm", "xcm"))]
pub enum VersionedXcm<RuntimeCall> {
#[codec(index = 2)]
#[deprecated]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this deprecated seems removed by the PR.

V2(v2::Xcm<RuntimeCall>),
#[codec(index = 3)]
V3(v3::Xcm<RuntimeCall>),
#[codec(index = 4)]
V4(v4::Xcm<RuntimeCall>),
}

impl<C> IntoVersion for VersionedXcm<C> {
fn into_version(self, n: Version) -> Result<Self, ()> {
Ok(match n {
2 => Self::V2(self.try_into()?),
3 => Self::V3(self.try_into()?),
4 => Self::V4(self.try_into()?),
_ => return Err(()),
})
}
}

impl<C> IdentifyVersion for VersionedXcm<C> {
fn identify_version(&self) -> Version {
match self {
Self::V2(_) => v2::VERSION,
Self::V3(_) => v3::VERSION,
Self::V4(_) => v4::VERSION,
}
versioned_type! {
pub enum VersionedXcm<RuntimeCall> {
#[codec(index = 2)]
V2(v2::Xcm<RuntimeCall>),
#[codec(index = 3)]
V3(v3::Xcm<RuntimeCall>),
#[codec(index = 4)]
V4(v4::Xcm<RuntimeCall>),
}
}

Expand All @@ -476,66 +469,6 @@ impl<C> VersionedXcm<C> {
}
}

impl<RuntimeCall> From<v2::Xcm<RuntimeCall>> for VersionedXcm<RuntimeCall> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Beautiful

fn from(x: v2::Xcm<RuntimeCall>) -> Self {
VersionedXcm::V2(x)
}
}

impl<RuntimeCall> From<v3::Xcm<RuntimeCall>> for VersionedXcm<RuntimeCall> {
fn from(x: v3::Xcm<RuntimeCall>) -> Self {
VersionedXcm::V3(x)
}
}

impl<RuntimeCall> From<v4::Xcm<RuntimeCall>> for VersionedXcm<RuntimeCall> {
fn from(x: v4::Xcm<RuntimeCall>) -> Self {
VersionedXcm::V4(x)
}
}

impl<RuntimeCall> TryFrom<VersionedXcm<RuntimeCall>> for v2::Xcm<RuntimeCall> {
type Error = ();
fn try_from(x: VersionedXcm<RuntimeCall>) -> Result<Self, ()> {
use VersionedXcm::*;
match x {
V2(x) => Ok(x),
V3(x) => x.try_into(),
V4(x) => {
let v3: v3::Xcm<RuntimeCall> = x.try_into()?;
v3.try_into()
},
}
}
}

impl<Call> TryFrom<VersionedXcm<Call>> for v3::Xcm<Call> {
type Error = ();
fn try_from(x: VersionedXcm<Call>) -> Result<Self, ()> {
use VersionedXcm::*;
match x {
V2(x) => x.try_into(),
V3(x) => Ok(x),
V4(x) => x.try_into(),
}
}
}

impl<Call> TryFrom<VersionedXcm<Call>> for v4::Xcm<Call> {
type Error = ();
fn try_from(x: VersionedXcm<Call>) -> Result<Self, ()> {
use VersionedXcm::*;
match x {
V2(x) => {
let v3: v3::Xcm<Call> = x.try_into()?;
v3.try_into()
},
V3(x) => x.try_into(),
V4(x) => Ok(x),
}
}
}

/// Convert an `Xcm` datum into a `VersionedXcm`, based on a destination `Location` which will
/// interpret it.
pub trait WrapVersion {
Expand Down
15 changes: 15 additions & 0 deletions prdoc/pr_4378.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0
# See doc at https://raw.githubusercontent.com/paritytech/polkadot-sdk/master/prdoc/schema_user.json

title: "Use `versioned_type!` macro for `VersionedXcm`"

doc:
- audience: Runtime Dev
description: |
Currently, all other versioned types in the `staging-xcm` are created using `versioned_type!` macro, except for `VersionedXcm`.
This PR changes `versioned_type` macro so that it can be used for `VersionedXcm` as well. This is done by adding optional
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This PR changes `versioned_type` macro so that it can be used for `VersionedXcm` as well. This is done by adding optional
This PR changes `versioned_type` macro so that it can be used for `VersionedXcm` as well, reducing a lot of duplication. This is done by adding optional

generic type param to an enum that is passed to the macro.

crates:
- name: staging-xcm
bump: patch
Loading