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

Commit c22e1c4

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 50a2c88 commit c22e1c4

File tree

4 files changed

+207
-85
lines changed

4 files changed

+207
-85
lines changed

Diff for: Cargo.lock

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: xcm/Cargo.toml

+2
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.39" }
21+
hex = "0.4.3"
22+
hex-literal = "0.3.4"
2123

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

Diff for: xcm/src/lib.rs

+20-85
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ pub mod latest {
4141
mod double_encoded;
4242
pub use double_encoded::DoubleEncoded;
4343

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

@@ -73,6 +76,7 @@ pub trait TryAs<T> {
7376

7477
macro_rules! versioned_type {
7578
($(#[$attr:meta])* pub enum $n:ident {
79+
$(#[$index3:meta])+
7680
V3($v3:ty),
7781
}) => {
7882
#[derive(Derivative, Encode, Decode, TypeInfo)]
@@ -86,7 +90,7 @@ macro_rules! versioned_type {
8690
#[codec(decode_bound())]
8791
$(#[$attr])*
8892
pub enum $n {
89-
#[codec(index = 0)]
93+
$(#[$index3])*
9094
V3($v3),
9195
}
9296
impl $n {
@@ -131,7 +135,9 @@ macro_rules! versioned_type {
131135
};
132136

133137
($(#[$attr:meta])* pub enum $n:ident {
138+
$(#[$index2:meta])+
134139
V2($v2:ty),
140+
$(#[$index3:meta])+
135141
V3($v3:ty),
136142
}) => {
137143
#[derive(Derivative, Encode, Decode, TypeInfo)]
@@ -145,9 +151,9 @@ macro_rules! versioned_type {
145151
#[codec(decode_bound())]
146152
$(#[$attr])*
147153
pub enum $n {
148-
#[codec(index = 0)]
154+
$(#[$index2])*
149155
V2($v2),
150-
#[codec(index = 1)]
156+
$(#[$index3])*
151157
V3($v3),
152158
}
153159
impl $n {
@@ -216,101 +222,22 @@ macro_rules! versioned_type {
216222
}
217223
}
218224
};
219-
220-
($(#[$attr:meta])* pub enum $n:ident {
221-
V2($v2:ty),
222-
V3($v3:ty),
223-
}) => {
224-
#[derive(Derivative, Encode, Decode, TypeInfo)]
225-
#[derivative(Clone(bound = ""), Eq(bound = ""), PartialEq(bound = ""), Debug(bound = ""))]
226-
#[codec(encode_bound())]
227-
#[codec(decode_bound())]
228-
$(#[$attr])*
229-
pub enum $n {
230-
#[codec(index = 1)]
231-
V2($v2),
232-
#[codec(index = 2)]
233-
V3($v3),
234-
}
235-
impl $n {
236-
pub fn try_as<T>(&self) -> Result<&T, ()> where Self: TryAs<T> {
237-
<Self as TryAs<T>>::try_as(&self)
238-
}
239-
}
240-
impl TryAs<$v2> for $n {
241-
fn try_as(&self) -> Result<&$v2, ()> {
242-
match &self {
243-
Self::V2(ref x) => Ok(x),
244-
_ => Err(()),
245-
}
246-
}
247-
}
248-
impl TryAs<$v3> for $n {
249-
fn try_as(&self) -> Result<&$v3, ()> {
250-
match &self {
251-
Self::V3(ref x) => Ok(x),
252-
_ => Err(()),
253-
}
254-
}
255-
}
256-
impl IntoVersion for $n {
257-
fn into_version(self, n: Version) -> Result<Self, ()> {
258-
Ok(match n {
259-
2 => Self::V2(self.try_into()?),
260-
3 => Self::V3(self.try_into()?),
261-
_ => return Err(()),
262-
})
263-
}
264-
}
265-
impl From<$v2> for $n {
266-
fn from(x: $v2) -> Self {
267-
$n::V2(x)
268-
}
269-
}
270-
impl<T: Into<$v3>> From<T> for $n {
271-
fn from(x: T) -> Self {
272-
$n::V3(x.into())
273-
}
274-
}
275-
impl TryFrom<$n> for $v2 {
276-
type Error = ();
277-
fn try_from(x: $n) -> Result<Self, ()> {
278-
use $n::*;
279-
match x {
280-
V2(x) => Ok(x),
281-
V3(x) => x.try_into(),
282-
}
283-
}
284-
}
285-
impl TryFrom<$n> for $v3 {
286-
type Error = ();
287-
fn try_from(x: $n) -> Result<Self, ()> {
288-
use $n::*;
289-
match x {
290-
V2(x) => x.try_into(),
291-
V3(x) => Ok(x),
292-
}
293-
}
294-
}
295-
impl MaxEncodedLen for $n {
296-
fn max_encoded_len() -> usize {
297-
<$v3>::max_encoded_len()
298-
}
299-
}
300-
}
301225
}
302226

303227
versioned_type! {
304228
/// A single version's `Response` value, together with its version code.
305229
pub enum VersionedAssetId {
230+
#[codec(index = 3)]
306231
V3(v3::AssetId),
307232
}
308233
}
309234

310235
versioned_type! {
311236
/// A single version's `Response` value, together with its version code.
312237
pub enum VersionedResponse {
238+
#[codec(index = 2)]
313239
V2(v2::Response),
240+
#[codec(index = 3)]
314241
V3(v3::Response),
315242
}
316243
}
@@ -319,31 +246,39 @@ versioned_type! {
319246
/// A single `MultiLocation` value, together with its version code.
320247
#[derive(Ord, PartialOrd)]
321248
pub enum VersionedMultiLocation {
249+
#[codec(index = 1)] // v2 is same as v1 and therefore re-using the v1 index
322250
V2(v2::MultiLocation),
251+
#[codec(index = 3)]
323252
V3(v3::MultiLocation),
324253
}
325254
}
326255

327256
versioned_type! {
328257
/// A single `InteriorMultiLocation` value, together with its version code.
329258
pub enum VersionedInteriorMultiLocation {
259+
#[codec(index = 2)] // while this is same as v1::Junctions, VersionedInteriorMultiLocation is introduced in v3
330260
V2(v2::InteriorMultiLocation),
261+
#[codec(index = 3)]
331262
V3(v3::InteriorMultiLocation),
332263
}
333264
}
334265

335266
versioned_type! {
336267
/// A single `MultiAsset` value, together with its version code.
337268
pub enum VersionedMultiAsset {
269+
#[codec(index = 1)] // v2 is same as v1 and therefore re-using the v1 index
338270
V2(v2::MultiAsset),
271+
#[codec(index = 3)]
339272
V3(v3::MultiAsset),
340273
}
341274
}
342275

343276
versioned_type! {
344277
/// A single `MultiAssets` value, together with its version code.
345278
pub enum VersionedMultiAssets {
279+
#[codec(index = 1)] // v2 is same as v1 and therefore re-using the v1 index
346280
V2(v2::MultiAssets),
281+
#[codec(index = 3)]
347282
V3(v3::MultiAssets),
348283
}
349284
}

Diff for: xcm/src/tests.rs

+183
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)