From 61a62b74e479e81bb27e57d6058cfaf0b0ac4c4d Mon Sep 17 00:00:00 2001 From: dastansam Date: Sat, 4 May 2024 15:07:23 +0200 Subject: [PATCH] Use `versioned_type!` for `VersionedXcm` --- polkadot/xcm/src/lib.rs | 144 ++++++++++------------------------------ 1 file changed, 35 insertions(+), 109 deletions(-) diff --git a/polkadot/xcm/src/lib.rs b/polkadot/xcm/src/lib.rs index 513dfe5501ba..784b94ff8b38 100644 --- a/polkadot/xcm/src/lib.rs +++ b/polkadot/xcm/src/lib.rs @@ -77,6 +77,15 @@ pub trait TryAs { } macro_rules! versioned_type { + // only impl `MaxEncodedLen` for enums without generic type parameters + (@internal $n:ident, $v3:ty, ) => { + impl MaxEncodedLen for $n { + fn max_encoded_len() -> usize { + <$v3>::max_encoded_len() + } + } + }; + (@internal $n:ident, $v3:ty, $t:ident) => {}; ($(#[$attr:meta])* pub enum $n:ident { $(#[$index3:meta])+ V3($v3:ty), @@ -176,7 +185,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])+ @@ -193,9 +202,10 @@ 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])* @@ -203,12 +213,12 @@ macro_rules! versioned_type { $(#[$index4])* V4($v4), } - impl $n { + impl$(<$($gen),+>)? $n $(<$($gen),+>)? { pub fn try_as(&self) -> Result<&T, ()> where Self: TryAs { >::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), @@ -216,7 +226,7 @@ macro_rules! versioned_type { } } } - 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), @@ -224,7 +234,7 @@ macro_rules! versioned_type { } } } - 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), @@ -232,7 +242,7 @@ macro_rules! versioned_type { } } } - impl IntoVersion for $n { + impl$(<$($gen),+>)? IntoVersion for $n $(<$($gen),+>)?{ fn into_version(self, n: Version) -> Result { Ok(match n { 1 | 2 => Self::V2(self.try_into()?), @@ -242,19 +252,19 @@ macro_rules! versioned_type { }) } } - impl From<$v2> for $n { + impl$(<$($gen),+>)? From<$v2> for $n $(<$($gen),+>)? { fn from(x: $v2) -> Self { $n::V2(x) } } - impl> From for $n { + impl<$($($gen,),+)? T: Into<$v4>> From 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 { + fn try_from(x: $n $(<$($gen),+>)?) -> Result { use $n::*; match x { V2(x) => Ok(x), @@ -266,9 +276,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 { + fn try_from(x: $n $(<$($gen),+>)?) -> Result { use $n::*; match x { V2(x) => x.try_into(), @@ -277,9 +287,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 { + fn try_from(x: $n $(<$($gen),+>)?) -> Result { use $n::*; match x { V2(x) => { @@ -291,12 +301,8 @@ macro_rules! versioned_type { } } } - impl MaxEncodedLen for $n { - fn max_encoded_len() -> usize { - <$v3>::max_encoded_len() - } - } - impl IdentifyVersion for $n { + versioned_type!(@internal $n, $v3, $($($gen),+)?); + impl$(<$($gen),+>)? IdentifyVersion for $n $(<$($gen),+>)? { fn identify_version(&self) -> Version { use $n::*; match self { @@ -416,40 +422,14 @@ 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 { - #[codec(index = 2)] - V2(v2::Xcm), - #[codec(index = 3)] - V3(v3::Xcm), - #[codec(index = 4)] - V4(v4::Xcm), -} - -impl IntoVersion for VersionedXcm { - fn into_version(self, n: Version) -> Result { - Ok(match n { - 2 => Self::V2(self.try_into()?), - 3 => Self::V3(self.try_into()?), - 4 => Self::V4(self.try_into()?), - _ => return Err(()), - }) - } -} - -impl IdentifyVersion for VersionedXcm { - fn identify_version(&self) -> Version { - match self { - Self::V2(_) => v2::VERSION, - Self::V3(_) => v3::VERSION, - Self::V4(_) => v4::VERSION, - } +versioned_type! { + pub enum VersionedXcm { + #[codec(index = 2)] + V2(v2::Xcm), + #[codec(index = 3)] + V3(v3::Xcm), + #[codec(index = 4)] + V4(v4::Xcm), } } @@ -470,66 +450,12 @@ impl VersionedXcm { } } -impl From> for VersionedXcm { - fn from(x: v2::Xcm) -> Self { - VersionedXcm::V2(x) - } -} - impl From> for VersionedXcm { fn from(x: v3::Xcm) -> Self { VersionedXcm::V3(x) } } -impl From> for VersionedXcm { - fn from(x: v4::Xcm) -> Self { - VersionedXcm::V4(x) - } -} - -impl TryFrom> for v2::Xcm { - type Error = (); - fn try_from(x: VersionedXcm) -> Result { - use VersionedXcm::*; - match x { - V2(x) => Ok(x), - V3(x) => x.try_into(), - V4(x) => { - let v3: v3::Xcm = x.try_into()?; - v3.try_into() - }, - } - } -} - -impl TryFrom> for v3::Xcm { - type Error = (); - fn try_from(x: VersionedXcm) -> Result { - use VersionedXcm::*; - match x { - V2(x) => x.try_into(), - V3(x) => Ok(x), - V4(x) => x.try_into(), - } - } -} - -impl TryFrom> for v4::Xcm { - type Error = (); - fn try_from(x: VersionedXcm) -> Result { - use VersionedXcm::*; - match x { - V2(x) => { - let v3: v3::Xcm = 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 {