Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 2bbbf8f

Browse files
xlccoderobe
authored andcommitted
remove duplicated arm and fix version index (#6884)
* remove duplicated arm * annotate the version index * add tests * fmt
1 parent 256a9d2 commit 2bbbf8f

File tree

4 files changed

+207
-85
lines changed

4 files changed

+207
-85
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

xcm/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ xcm-procedural = { path = "procedural" }
1818

1919
[dev-dependencies]
2020
sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.40" }
21+
hex = "0.4.3"
22+
hex-literal = "0.3.4"
2123

2224
[features]
2325
default = ["std"]

xcm/src/lib.rs

Lines changed: 20 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ pub mod latest {
4242
mod double_encoded;
4343
pub use double_encoded::DoubleEncoded;
4444

45+
#[cfg(test)]
46+
mod tests;
47+
4548
/// Maximum nesting level for XCM decoding.
4649
pub const MAX_XCM_DECODE_DEPTH: u32 = 8;
4750

@@ -157,6 +160,7 @@ pub trait TryAs<T> {
157160

158161
macro_rules! versioned_type {
159162
($(#[$attr:meta])* pub enum $n:ident {
163+
$(#[$index3:meta])+
160164
V3($v3:ty),
161165
}) => {
162166
#[derive(Derivative, Encode, Decode, TypeInfo)]
@@ -170,7 +174,7 @@ macro_rules! versioned_type {
170174
#[codec(decode_bound())]
171175
$(#[$attr])*
172176
pub enum $n {
173-
#[codec(index = 0)]
177+
$(#[$index3])*
174178
V3($v3),
175179
}
176180
impl $n {
@@ -215,7 +219,9 @@ macro_rules! versioned_type {
215219
};
216220

217221
($(#[$attr:meta])* pub enum $n:ident {
222+
$(#[$index2:meta])+
218223
V2($v2:ty),
224+
$(#[$index3:meta])+
219225
V3($v3:ty),
220226
}) => {
221227
#[derive(Derivative, Encode, Decode, TypeInfo)]
@@ -229,9 +235,9 @@ macro_rules! versioned_type {
229235
#[codec(decode_bound())]
230236
$(#[$attr])*
231237
pub enum $n {
232-
#[codec(index = 0)]
238+
$(#[$index2])*
233239
V2($v2),
234-
#[codec(index = 1)]
240+
$(#[$index3])*
235241
V3($v3),
236242
}
237243
impl $n {
@@ -300,101 +306,22 @@ macro_rules! versioned_type {
300306
}
301307
}
302308
};
303-
304-
($(#[$attr:meta])* pub enum $n:ident {
305-
V2($v2:ty),
306-
V3($v3:ty),
307-
}) => {
308-
#[derive(Derivative, Encode, Decode, TypeInfo)]
309-
#[derivative(Clone(bound = ""), Eq(bound = ""), PartialEq(bound = ""), Debug(bound = ""))]
310-
#[codec(encode_bound())]
311-
#[codec(decode_bound())]
312-
$(#[$attr])*
313-
pub enum $n {
314-
#[codec(index = 1)]
315-
V2($v2),
316-
#[codec(index = 2)]
317-
V3($v3),
318-
}
319-
impl $n {
320-
pub fn try_as<T>(&self) -> Result<&T, ()> where Self: TryAs<T> {
321-
<Self as TryAs<T>>::try_as(&self)
322-
}
323-
}
324-
impl TryAs<$v2> for $n {
325-
fn try_as(&self) -> Result<&$v2, ()> {
326-
match &self {
327-
Self::V2(ref x) => Ok(x),
328-
_ => Err(()),
329-
}
330-
}
331-
}
332-
impl TryAs<$v3> for $n {
333-
fn try_as(&self) -> Result<&$v3, ()> {
334-
match &self {
335-
Self::V3(ref x) => Ok(x),
336-
_ => Err(()),
337-
}
338-
}
339-
}
340-
impl IntoVersion for $n {
341-
fn into_version(self, n: Version) -> Result<Self, ()> {
342-
Ok(match n {
343-
2 => Self::V2(self.try_into()?),
344-
3 => Self::V3(self.try_into()?),
345-
_ => return Err(()),
346-
})
347-
}
348-
}
349-
impl From<$v2> for $n {
350-
fn from(x: $v2) -> Self {
351-
$n::V2(x)
352-
}
353-
}
354-
impl<T: Into<$v3>> From<T> for $n {
355-
fn from(x: T) -> Self {
356-
$n::V3(x.into())
357-
}
358-
}
359-
impl TryFrom<$n> for $v2 {
360-
type Error = ();
361-
fn try_from(x: $n) -> Result<Self, ()> {
362-
use $n::*;
363-
match x {
364-
V2(x) => Ok(x),
365-
V3(x) => x.try_into(),
366-
}
367-
}
368-
}
369-
impl TryFrom<$n> for $v3 {
370-
type Error = ();
371-
fn try_from(x: $n) -> Result<Self, ()> {
372-
use $n::*;
373-
match x {
374-
V2(x) => x.try_into(),
375-
V3(x) => Ok(x),
376-
}
377-
}
378-
}
379-
impl MaxEncodedLen for $n {
380-
fn max_encoded_len() -> usize {
381-
<$v3>::max_encoded_len()
382-
}
383-
}
384-
}
385309
}
386310

387311
versioned_type! {
388312
/// A single version's `Response` value, together with its version code.
389313
pub enum VersionedAssetId {
314+
#[codec(index = 3)]
390315
V3(v3::AssetId),
391316
}
392317
}
393318

394319
versioned_type! {
395320
/// A single version's `Response` value, together with its version code.
396321
pub enum VersionedResponse {
322+
#[codec(index = 2)]
397323
V2(v2::Response),
324+
#[codec(index = 3)]
398325
V3(v3::Response),
399326
}
400327
}
@@ -403,31 +330,39 @@ versioned_type! {
403330
/// A single `MultiLocation` value, together with its version code.
404331
#[derive(Ord, PartialOrd)]
405332
pub enum VersionedMultiLocation {
333+
#[codec(index = 1)] // v2 is same as v1 and therefore re-using the v1 index
406334
V2(v2::MultiLocation),
335+
#[codec(index = 3)]
407336
V3(v3::MultiLocation),
408337
}
409338
}
410339

411340
versioned_type! {
412341
/// A single `InteriorMultiLocation` value, together with its version code.
413342
pub enum VersionedInteriorMultiLocation {
343+
#[codec(index = 2)] // while this is same as v1::Junctions, VersionedInteriorMultiLocation is introduced in v3
414344
V2(v2::InteriorMultiLocation),
345+
#[codec(index = 3)]
415346
V3(v3::InteriorMultiLocation),
416347
}
417348
}
418349

419350
versioned_type! {
420351
/// A single `MultiAsset` value, together with its version code.
421352
pub enum VersionedMultiAsset {
353+
#[codec(index = 1)] // v2 is same as v1 and therefore re-using the v1 index
422354
V2(v2::MultiAsset),
355+
#[codec(index = 3)]
423356
V3(v3::MultiAsset),
424357
}
425358
}
426359

427360
versioned_type! {
428361
/// A single `MultiAssets` value, together with its version code.
429362
pub enum VersionedMultiAssets {
363+
#[codec(index = 1)] // v2 is same as v1 and therefore re-using the v1 index
430364
V2(v2::MultiAssets),
365+
#[codec(index = 3)]
431366
V3(v3::MultiAssets),
432367
}
433368
}

xcm/src/tests.rs

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
// Copyright 2021 Parity Technologies (UK) Ltd.
2+
// This file is part of Polkadot.
3+
4+
// Polkadot is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
9+
// Polkadot is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
14+
// You should have received a copy of the GNU General Public License
15+
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
16+
17+
use crate::*;
18+
use alloc::vec;
19+
20+
#[test]
21+
fn encode_decode_versioned_asset_id_v3() {
22+
let asset_id = VersionedAssetId::V3(v3::AssetId::Abstract([1; 32]));
23+
let encoded = asset_id.encode();
24+
25+
assert_eq!(
26+
encoded,
27+
hex_literal::hex!("03010101010101010101010101010101010101010101010101010101010101010101"),
28+
"encode format changed"
29+
);
30+
assert_eq!(encoded[0], 3, "bad version number");
31+
32+
let decoded = VersionedAssetId::decode(&mut &encoded[..]).unwrap();
33+
assert_eq!(asset_id, decoded);
34+
}
35+
36+
#[test]
37+
fn encode_decode_versioned_response_v2() {
38+
let response = VersionedResponse::V2(v2::Response::Null);
39+
let encoded = response.encode();
40+
41+
assert_eq!(encoded, hex_literal::hex!("0200"), "encode format changed");
42+
assert_eq!(encoded[0], 2, "bad version number");
43+
44+
let decoded = VersionedResponse::decode(&mut &encoded[..]).unwrap();
45+
assert_eq!(response, decoded);
46+
}
47+
48+
#[test]
49+
fn encode_decode_versioned_response_v3() {
50+
let response = VersionedResponse::V3(v3::Response::Null);
51+
let encoded = response.encode();
52+
53+
assert_eq!(encoded, hex_literal::hex!("0300"), "encode format changed");
54+
assert_eq!(encoded[0], 3, "bad version number");
55+
56+
let decoded = VersionedResponse::decode(&mut &encoded[..]).unwrap();
57+
assert_eq!(response, decoded);
58+
}
59+
60+
#[test]
61+
fn encode_decode_versioned_multi_location_v2() {
62+
let location = VersionedMultiLocation::V2(v2::MultiLocation::new(0, v2::Junctions::Here));
63+
let encoded = location.encode();
64+
65+
assert_eq!(encoded, hex_literal::hex!("010000"), "encode format changed");
66+
assert_eq!(encoded[0], 1, "bad version number"); // this is introduced in v1
67+
68+
let decoded = VersionedMultiLocation::decode(&mut &encoded[..]).unwrap();
69+
assert_eq!(location, decoded);
70+
}
71+
72+
#[test]
73+
fn encode_decode_versioned_multi_location_v3() {
74+
let location = VersionedMultiLocation::V3(v3::MultiLocation::new(0, v3::Junctions::Here));
75+
let encoded = location.encode();
76+
77+
assert_eq!(encoded, hex_literal::hex!("030000"), "encode format changed");
78+
assert_eq!(encoded[0], 3, "bad version number");
79+
80+
let decoded = VersionedMultiLocation::decode(&mut &encoded[..]).unwrap();
81+
assert_eq!(location, decoded);
82+
}
83+
84+
#[test]
85+
fn encode_decode_versioned_interior_multi_location_v2() {
86+
let location = VersionedInteriorMultiLocation::V2(v2::InteriorMultiLocation::Here);
87+
let encoded = location.encode();
88+
89+
assert_eq!(encoded, hex_literal::hex!("0200"), "encode format changed");
90+
assert_eq!(encoded[0], 2, "bad version number");
91+
92+
let decoded = VersionedInteriorMultiLocation::decode(&mut &encoded[..]).unwrap();
93+
assert_eq!(location, decoded);
94+
}
95+
96+
#[test]
97+
fn encode_decode_versioned_interior_multi_location_v3() {
98+
let location = VersionedInteriorMultiLocation::V3(v3::InteriorMultiLocation::Here);
99+
let encoded = location.encode();
100+
101+
assert_eq!(encoded, hex_literal::hex!("0300"), "encode format changed");
102+
assert_eq!(encoded[0], 3, "bad version number");
103+
104+
let decoded = VersionedInteriorMultiLocation::decode(&mut &encoded[..]).unwrap();
105+
assert_eq!(location, decoded);
106+
}
107+
108+
#[test]
109+
fn encode_decode_versioned_multi_asset_v2() {
110+
let asset = VersionedMultiAsset::V2(v2::MultiAsset::from(((0, v2::Junctions::Here), 1)));
111+
let encoded = asset.encode();
112+
113+
assert_eq!(encoded, hex_literal::hex!("010000000004"), "encode format changed");
114+
assert_eq!(encoded[0], 1, "bad version number");
115+
116+
let decoded = VersionedMultiAsset::decode(&mut &encoded[..]).unwrap();
117+
assert_eq!(asset, decoded);
118+
}
119+
120+
#[test]
121+
fn encode_decode_versioned_multi_asset_v3() {
122+
let asset = VersionedMultiAsset::V3(v3::MultiAsset::from((v3::MultiLocation::default(), 1)));
123+
let encoded = asset.encode();
124+
125+
assert_eq!(encoded, hex_literal::hex!("030000000004"), "encode format changed");
126+
assert_eq!(encoded[0], 3, "bad version number");
127+
128+
let decoded = VersionedMultiAsset::decode(&mut &encoded[..]).unwrap();
129+
assert_eq!(asset, decoded);
130+
}
131+
132+
#[test]
133+
fn encode_decode_versioned_multi_assets_v2() {
134+
let assets = VersionedMultiAssets::V2(v2::MultiAssets::from(vec![v2::MultiAsset::from((
135+
(0, v2::Junctions::Here),
136+
1,
137+
))]));
138+
let encoded = assets.encode();
139+
140+
assert_eq!(encoded, hex_literal::hex!("01040000000004"), "encode format changed");
141+
assert_eq!(encoded[0], 1, "bad version number");
142+
143+
let decoded = VersionedMultiAssets::decode(&mut &encoded[..]).unwrap();
144+
assert_eq!(assets, decoded);
145+
}
146+
147+
#[test]
148+
fn encode_decode_versioned_multi_assets_v3() {
149+
let assets = VersionedMultiAssets::V3(v3::MultiAssets::from(vec![
150+
(v3::MultiAsset::from((v3::MultiLocation::default(), 1))),
151+
]));
152+
let encoded = assets.encode();
153+
154+
assert_eq!(encoded, hex_literal::hex!("03040000000004"), "encode format changed");
155+
assert_eq!(encoded[0], 3, "bad version number");
156+
157+
let decoded = VersionedMultiAssets::decode(&mut &encoded[..]).unwrap();
158+
assert_eq!(assets, decoded);
159+
}
160+
161+
#[test]
162+
fn encode_decode_versioned_xcm_v2() {
163+
let xcm = VersionedXcm::V2(v2::Xcm::<()>::new());
164+
let encoded = xcm.encode();
165+
166+
assert_eq!(encoded, hex_literal::hex!("0200"), "encode format changed");
167+
assert_eq!(encoded[0], 2, "bad version number");
168+
169+
let decoded = VersionedXcm::decode(&mut &encoded[..]).unwrap();
170+
assert_eq!(xcm, decoded);
171+
}
172+
173+
#[test]
174+
fn encode_decode_versioned_xcm_v3() {
175+
let xcm = VersionedXcm::V3(v3::Xcm::<()>::new());
176+
let encoded = xcm.encode();
177+
178+
assert_eq!(encoded, hex_literal::hex!("0300"), "encode format changed");
179+
assert_eq!(encoded[0], 3, "bad version number");
180+
181+
let decoded = VersionedXcm::decode(&mut &encoded[..]).unwrap();
182+
assert_eq!(xcm, decoded);
183+
}

0 commit comments

Comments
 (0)