-
Notifications
You must be signed in to change notification settings - Fork 690
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
dastansam
wants to merge
8
commits into
paritytech:master
Choose a base branch
from
dastansam:versioned_type_xcm
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
61a62b7
Use `versioned_type!` for `VersionedXcm`
dastansam 37c8a6b
Add prdoc
dastansam 794b59c
Merge branch 'master' into versioned_type_xcm
dastansam 6cf04ba
Try resolve comments
dastansam bda5a4c
Update prdoc/pr_4378.prdoc
bkchr a28429d
Merge branch 'master' into versioned_type_xcm
bkchr 06b9211
Update prdoc/pr_4378.prdoc
franciscoaguirre bcc1db2
Update prdoc/pr_4378.prdoc
franciscoaguirre File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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()) | ||
} | ||
} | ||
}; | ||
(@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), | ||
|
@@ -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])+ | ||
|
@@ -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), | ||
|
@@ -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(), | ||
|
@@ -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) => { | ||
|
@@ -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 { | ||
|
@@ -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] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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>), | ||
} | ||
} | ||
|
||
|
@@ -476,66 +469,6 @@ impl<C> VersionedXcm<C> { | |
} | ||
} | ||
|
||
impl<RuntimeCall> From<v2::Xcm<RuntimeCall>> for VersionedXcm<RuntimeCall> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 `stagin-xcm` are created using `versioned_type!` macro, except for `VersionedXcm`. | ||
franciscoaguirre marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This PR adds changes `versioned_type` macro so that it can be used for `VersionedXcm` as well. This is done by adding optional | ||
franciscoaguirre marked this conversation as resolved.
Show resolved
Hide resolved
|
||
generic type param to an enum that is passed to the macro. | ||
|
||
crates: | ||
- name: staging-xcm | ||
bump: patch |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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