Skip to content

Commit db4bb53

Browse files
authored
Location conversion tests for relay and parachains (#487)
Added location convertion tests. Polkadot sdk [PR](paritytech/polkadot-sdk#5765) Fixes #488
1 parent 22e8e89 commit db4bb53

14 files changed

Lines changed: 1205 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## [Unreleased]
4+
5+
### Added
6+
7+
- Location conversion tests for relays and parachains ([polkadot-fellows/runtimes#487](https://github.com/polkadot-fellows/runtimes/pull/487))
8+
39
Changelog for the runtimes governed by the Polkadot Fellowship.
410

511
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// Copyright (C) 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 polkadot_primitives::AccountId;
18+
use sp_core::crypto::Ss58Codec;
19+
use staging_kusama_runtime::xcm_config::SovereignAccountOf;
20+
use xcm::prelude::*;
21+
use xcm_runtime_apis::conversions::LocationToAccountHelper;
22+
23+
const ALICE: [u8; 32] = [1u8; 32];
24+
25+
#[test]
26+
fn location_conversion_works() {
27+
// the purpose of hardcoded values is to catch an unintended location conversion logic change.
28+
struct TestCase {
29+
description: &'static str,
30+
location: Location,
31+
expected_account_id_str: &'static str,
32+
}
33+
34+
let test_cases = vec![
35+
// DescribeTerminus
36+
TestCase {
37+
description: "DescribeTerminus Child",
38+
location: Location::new(0, [Parachain(1111)]),
39+
expected_account_id_str: "5Ec4AhP4h37t7TFsAZ4HhFq6k92usAAJDUC3ADSZ4H4Acru3",
40+
},
41+
// DescribePalletTerminal
42+
TestCase {
43+
description: "DescribePalletTerminal Child",
44+
location: Location::new(0, [Parachain(1111), PalletInstance(50)]),
45+
expected_account_id_str: "5FjEBrKn3STAFsZpQF4jzwxUYHNGnNgzdZqSQfTzeJ82XKp6",
46+
},
47+
// DescribeAccountId32Terminal
48+
TestCase {
49+
description: "DescribeAccountId32Terminal Child",
50+
location: Location::new(
51+
0,
52+
[
53+
Parachain(1111),
54+
Junction::AccountId32 { network: None, id: AccountId::from(ALICE).into() },
55+
],
56+
),
57+
expected_account_id_str: "5D6CDyPd9Mya81xFN3nChiKqLvUzd8zS9fwKhfCW6FtJKjS2",
58+
},
59+
// DescribeAccountKey20Terminal
60+
TestCase {
61+
description: "DescribeAccountKey20Terminal Sibling",
62+
location: Location::new(
63+
0,
64+
[Parachain(1111), AccountKey20 { network: None, key: [123u8; 20] }],
65+
),
66+
expected_account_id_str: "5DEZsy7tsnNXB7ehLGkF8b4EUqfLQWqEzGiy2RrneC8uRNMK",
67+
},
68+
// DescribeTreasuryVoiceTerminal
69+
TestCase {
70+
description: "DescribeTreasuryVoiceTerminal Child",
71+
location: Location::new(
72+
0,
73+
[Parachain(1111), Plurality { id: BodyId::Treasury, part: BodyPart::Voice }],
74+
),
75+
expected_account_id_str: "5GenE4vJgHvwYVcD6b4nBvH5HNY4pzpVHWoqwFpNMFT7a2oX",
76+
},
77+
// DescribeBodyTerminal
78+
TestCase {
79+
description: "DescribeBodyTerminal Child",
80+
location: Location::new(
81+
0,
82+
[Parachain(1111), Plurality { id: BodyId::Unit, part: BodyPart::Voice }],
83+
),
84+
expected_account_id_str: "5DPgGBFTTYm1dGbtB1VWHJ3T3ScvdrskGGx6vSJZNP1WNStV",
85+
},
86+
];
87+
88+
for tc in test_cases {
89+
let expected =
90+
AccountId::from_string(tc.expected_account_id_str).expect("Invalid AccountId string");
91+
92+
let got = LocationToAccountHelper::<AccountId, SovereignAccountOf>::convert_location(
93+
tc.location.into(),
94+
)
95+
.unwrap();
96+
97+
assert_eq!(got, expected, "{}", tc.description);
98+
}
99+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// Copyright (C) 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 polkadot_primitives::AccountId;
18+
use polkadot_runtime::xcm_config::SovereignAccountOf;
19+
use sp_core::crypto::Ss58Codec;
20+
use xcm::prelude::*;
21+
use xcm_runtime_apis::conversions::LocationToAccountHelper;
22+
23+
const ALICE: [u8; 32] = [1u8; 32];
24+
25+
#[test]
26+
fn location_conversion_works() {
27+
// the purpose of hardcoded values is to catch an unintended location conversion logic change.
28+
struct TestCase {
29+
description: &'static str,
30+
location: Location,
31+
expected_account_id_str: &'static str,
32+
}
33+
34+
let test_cases = vec![
35+
// DescribeTerminus
36+
TestCase {
37+
description: "DescribeTerminus Child",
38+
location: Location::new(0, [Parachain(1111)]),
39+
expected_account_id_str: "5Ec4AhP4h37t7TFsAZ4HhFq6k92usAAJDUC3ADSZ4H4Acru3",
40+
},
41+
// DescribePalletTerminal
42+
TestCase {
43+
description: "DescribePalletTerminal Child",
44+
location: Location::new(0, [Parachain(1111), PalletInstance(50)]),
45+
expected_account_id_str: "5FjEBrKn3STAFsZpQF4jzwxUYHNGnNgzdZqSQfTzeJ82XKp6",
46+
},
47+
// DescribeAccountId32Terminal
48+
TestCase {
49+
description: "DescribeAccountId32Terminal Child",
50+
location: Location::new(
51+
0,
52+
[
53+
Parachain(1111),
54+
Junction::AccountId32 { network: None, id: AccountId::from(ALICE).into() },
55+
],
56+
),
57+
expected_account_id_str: "5D6CDyPd9Mya81xFN3nChiKqLvUzd8zS9fwKhfCW6FtJKjS2",
58+
},
59+
// DescribeAccountKey20Terminal
60+
TestCase {
61+
description: "DescribeAccountKey20Terminal Child",
62+
location: Location::new(
63+
0,
64+
[Parachain(1111), AccountKey20 { network: None, key: [123u8; 20] }],
65+
),
66+
expected_account_id_str: "5DEZsy7tsnNXB7ehLGkF8b4EUqfLQWqEzGiy2RrneC8uRNMK",
67+
},
68+
// DescribeTreasuryVoiceTerminal
69+
TestCase {
70+
description: "DescribeTreasuryVoiceTerminal Child",
71+
location: Location::new(
72+
0,
73+
[Parachain(1111), Plurality { id: BodyId::Treasury, part: BodyPart::Voice }],
74+
),
75+
expected_account_id_str: "5GenE4vJgHvwYVcD6b4nBvH5HNY4pzpVHWoqwFpNMFT7a2oX",
76+
},
77+
// DescribeBodyTerminal
78+
TestCase {
79+
description: "DescribeBodyTerminal Child",
80+
location: Location::new(
81+
0,
82+
[Parachain(1111), Plurality { id: BodyId::Unit, part: BodyPart::Voice }],
83+
),
84+
expected_account_id_str: "5DPgGBFTTYm1dGbtB1VWHJ3T3ScvdrskGGx6vSJZNP1WNStV",
85+
},
86+
];
87+
88+
for tc in test_cases {
89+
let expected =
90+
AccountId::from_string(tc.expected_account_id_str).expect("Invalid AccountId string");
91+
92+
let got = LocationToAccountHelper::<AccountId, SovereignAccountOf>::convert_location(
93+
tc.location.into(),
94+
)
95+
.unwrap();
96+
97+
assert_eq!(got, expected, "{}", tc.description);
98+
}
99+
}

system-parachains/asset-hubs/asset-hub-kusama/tests/tests.rs

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ use frame_support::{assert_ok, traits::fungibles::InspectEnumerable};
3737
use parachains_common::{AccountId, AssetIdForTrustBackedAssets, AuraId, Balance};
3838
use parachains_runtimes_test_utils::SlotDurations;
3939
use sp_consensus_aura::SlotDuration;
40+
use sp_core::crypto::Ss58Codec;
4041
use sp_runtime::traits::MaybeEquivalence;
4142
use sp_std::ops::Mul;
4243
use system_parachains_constants::kusama::{
@@ -45,6 +46,7 @@ use system_parachains_constants::kusama::{
4546
use xcm::latest::prelude::{Assets as XcmAssets, *};
4647
use xcm_builder::WithLatestLocationConverter;
4748
use xcm_executor::traits::{ConvertLocation, JustTry};
49+
use xcm_runtime_apis::conversions::LocationToAccountHelper;
4850

4951
const ALICE: [u8; 32] = [1u8; 32];
5052
const SOME_ASSET_ADMIN: [u8; 32] = [5u8; 32];
@@ -1303,3 +1305,104 @@ pub mod remove_when_updated_to_stable2409 {
13031305
})
13041306
}
13051307
}
1308+
1309+
#[test]
1310+
fn location_conversion_works() {
1311+
let alice_32 =
1312+
AccountId32 { network: None, id: polkadot_core_primitives::AccountId::from(ALICE).into() };
1313+
let bob_20 = AccountKey20 { network: None, key: [123u8; 20] };
1314+
1315+
// the purpose of hardcoded values is to catch an unintended location conversion logic change.
1316+
struct TestCase {
1317+
description: &'static str,
1318+
location: Location,
1319+
expected_account_id_str: &'static str,
1320+
}
1321+
1322+
let test_cases = vec![
1323+
// DescribeTerminus
1324+
TestCase {
1325+
description: "DescribeTerminus Parent",
1326+
location: Location::new(1, Here),
1327+
expected_account_id_str: "5Dt6dpkWPwLaH4BBCKJwjiWrFVAGyYk3tLUabvyn4v7KtESG",
1328+
},
1329+
TestCase {
1330+
description: "DescribeTerminus Sibling",
1331+
location: Location::new(1, [Parachain(1111)]),
1332+
expected_account_id_str: "5Eg2fnssmmJnF3z1iZ1NouAuzciDaaDQH7qURAy3w15jULDk",
1333+
},
1334+
// DescribePalletTerminal
1335+
TestCase {
1336+
description: "DescribePalletTerminal Parent",
1337+
location: Location::new(1, [PalletInstance(50)]),
1338+
expected_account_id_str: "5CnwemvaAXkWFVwibiCvf2EjqwiqBi29S5cLLydZLEaEw6jZ",
1339+
},
1340+
TestCase {
1341+
description: "DescribePalletTerminal Sibling",
1342+
location: Location::new(1, [Parachain(1111), PalletInstance(50)]),
1343+
expected_account_id_str: "5GFBgPjpEQPdaxEnFirUoa51u5erVx84twYxJVuBRAT2UP2g",
1344+
},
1345+
// DescribeAccountId32Terminal
1346+
TestCase {
1347+
description: "DescribeAccountId32Terminal Parent",
1348+
location: Location::new(1, [alice_32]),
1349+
expected_account_id_str: "5DN5SGsuUG7PAqFL47J9meViwdnk9AdeSWKFkcHC45hEzVz4",
1350+
},
1351+
TestCase {
1352+
description: "DescribeAccountId32Terminal Sibling",
1353+
location: Location::new(1, [Parachain(1111), alice_32]),
1354+
expected_account_id_str: "5DGRXLYwWGce7wvm14vX1Ms4Vf118FSWQbJkyQigY2pfm6bg",
1355+
},
1356+
// DescribeAccountKey20Terminal
1357+
TestCase {
1358+
description: "DescribeAccountKey20Terminal Parent",
1359+
location: Location::new(1, [bob_20]),
1360+
expected_account_id_str: "5CJeW9bdeos6EmaEofTUiNrvyVobMBfWbdQvhTe6UciGjH2n",
1361+
},
1362+
TestCase {
1363+
description: "DescribeAccountKey20Terminal Sibling",
1364+
location: Location::new(1, [Parachain(1111), bob_20]),
1365+
expected_account_id_str: "5CE6V5AKH8H4rg2aq5KMbvaVUDMumHKVPPQEEDMHPy3GmJQp",
1366+
},
1367+
// DescribeTreasuryVoiceTerminal
1368+
TestCase {
1369+
description: "DescribeTreasuryVoiceTerminal Parent",
1370+
location: Location::new(1, [Plurality { id: BodyId::Treasury, part: BodyPart::Voice }]),
1371+
expected_account_id_str: "5CUjnE2vgcUCuhxPwFoQ5r7p1DkhujgvMNDHaF2bLqRp4D5F",
1372+
},
1373+
TestCase {
1374+
description: "DescribeTreasuryVoiceTerminal Sibling",
1375+
location: Location::new(
1376+
1,
1377+
[Parachain(1111), Plurality { id: BodyId::Treasury, part: BodyPart::Voice }],
1378+
),
1379+
expected_account_id_str: "5G6TDwaVgbWmhqRUKjBhRRnH4ry9L9cjRymUEmiRsLbSE4gB",
1380+
},
1381+
// DescribeBodyTerminal
1382+
TestCase {
1383+
description: "DescribeBodyTerminal Parent",
1384+
location: Location::new(1, [Plurality { id: BodyId::Unit, part: BodyPart::Voice }]),
1385+
expected_account_id_str: "5EBRMTBkDisEXsaN283SRbzx9Xf2PXwUxxFCJohSGo4jYe6B",
1386+
},
1387+
TestCase {
1388+
description: "DescribeBodyTerminal Sibling",
1389+
location: Location::new(
1390+
1,
1391+
[Parachain(1111), Plurality { id: BodyId::Unit, part: BodyPart::Voice }],
1392+
),
1393+
expected_account_id_str: "5DBoExvojy8tYnHgLL97phNH975CyT45PWTZEeGoBZfAyRMH",
1394+
},
1395+
];
1396+
1397+
for tc in test_cases {
1398+
let expected = polkadot_core_primitives::AccountId::from_string(tc.expected_account_id_str)
1399+
.expect("Invalid AccountId string");
1400+
1401+
let got = LocationToAccountHelper::<polkadot_core_primitives::AccountId, LocationToAccountId>::convert_location(
1402+
tc.location.into(),
1403+
)
1404+
.unwrap();
1405+
1406+
assert_eq!(got, expected, "{}", tc.description);
1407+
}
1408+
}

0 commit comments

Comments
 (0)