Skip to content

Commit 95f3977

Browse files
michalkucharczykseadandabkchr
authored
asset-hub-rococo: genesis config presets added (#3996)
Gensis config presets moved from `polkadot-parachain` binary into `asset-hub-rococo` runtime. relates to: #3944 --------- Co-authored-by: Dónal Murray <[email protected]> Co-authored-by: Bastian Köcher <[email protected]>
1 parent d34f687 commit 95f3977

File tree

9 files changed

+268
-131
lines changed

9 files changed

+268
-131
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright (C) Parity Technologies (UK) Ltd.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
//! Some common helpers for declaring runtime's presets
17+
// note: copied from: cumulus/polkadot-parachain/src/chain_spec/mod.rs
18+
19+
use crate::{AccountId, Signature};
20+
#[cfg(not(feature = "std"))]
21+
use alloc::format;
22+
use sp_core::{Pair, Public};
23+
use sp_runtime::traits::{IdentifyAccount, Verify};
24+
25+
/// Helper function to generate a crypto pair from seed.
26+
pub fn get_from_seed<TPublic: Public>(seed: &str) -> <TPublic::Pair as Pair>::Public {
27+
TPublic::Pair::from_string(&format!("//{seed}"), None)
28+
.expect("static values are valid; qed")
29+
.public()
30+
}
31+
32+
type AccountPublic = <Signature as Verify>::Signer;
33+
34+
/// Helper function to generate an account id from seed.
35+
pub fn get_account_id_from_seed<TPublic: Public>(seed: &str) -> AccountId
36+
where
37+
AccountPublic: From<<TPublic::Pair as Pair>::Public>,
38+
{
39+
AccountPublic::from(get_from_seed::<TPublic>(seed)).into_account()
40+
}
41+
42+
/// Generate collator keys from seed.
43+
///
44+
/// This function's return type must always match the session keys of the chain in tuple format.
45+
pub fn get_collator_keys_from_seed<AuraId: Public>(seed: &str) -> <AuraId::Pair as Pair>::Public {
46+
get_from_seed::<AuraId>(seed)
47+
}

cumulus/parachains/common/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
extern crate alloc;
1919

20+
pub mod genesis_config_helpers;
2021
pub mod impls;
2122
pub mod message_queue;
2223
pub mod xcm_config;

cumulus/parachains/runtimes/assets/asset-hub-rococo/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ codec = { features = ["derive", "max-encoded-len"], workspace = true }
1414
hex-literal = { workspace = true, default-features = true }
1515
log = { workspace = true }
1616
scale-info = { features = ["derive"], workspace = true }
17+
serde_json = { features = ["alloc"], workspace = true }
1718

1819
# Substrate
1920
frame-benchmarking = { optional = true, workspace = true }
@@ -230,6 +231,7 @@ std = [
230231
"primitive-types/std",
231232
"rococo-runtime-constants/std",
232233
"scale-info/std",
234+
"serde_json/std",
233235
"snowbridge-router-primitives/std",
234236
"sp-api/std",
235237
"sp-block-builder/std",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
// Copyright (C) Parity Technologies (UK) Ltd.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
//! # Asset Hub Rococo Runtime genesis config presets
17+
18+
use alloc::{vec, vec::Vec};
19+
use cumulus_primitives_core::ParaId;
20+
use hex_literal::hex;
21+
use parachains_common::{genesis_config_helpers::*, AccountId, AuraId, Balance as AssetHubBalance};
22+
use sp_core::{crypto::UncheckedInto, sr25519};
23+
use sp_genesis_builder::PresetId;
24+
use testnet_parachains_constants::rococo::xcm_version::SAFE_XCM_VERSION;
25+
26+
const ASSET_HUB_ROCOCO_ED: AssetHubBalance = crate::ExistentialDeposit::get();
27+
28+
/// Generate the session keys from individual elements.
29+
///
30+
/// The input must be a tuple of individual keys (a single arg for now since we have just one key).
31+
pub fn asset_hub_rococo_session_keys(keys: AuraId) -> crate::SessionKeys {
32+
crate::SessionKeys { aura: keys }
33+
}
34+
35+
fn asset_hub_rococo_genesis(
36+
invulnerables: Vec<(AccountId, AuraId)>,
37+
endowed_accounts: Vec<AccountId>,
38+
endowment: AssetHubBalance,
39+
id: ParaId,
40+
) -> serde_json::Value {
41+
serde_json::json!({
42+
"balances": crate::BalancesConfig {
43+
balances: endowed_accounts
44+
.iter()
45+
.cloned()
46+
.map(|k| (k, endowment))
47+
.collect(),
48+
},
49+
"parachainInfo": crate::ParachainInfoConfig {
50+
parachain_id: id,
51+
..Default::default()
52+
},
53+
"collatorSelection": crate::CollatorSelectionConfig {
54+
invulnerables: invulnerables.iter().cloned().map(|(acc, _)| acc).collect(),
55+
candidacy_bond: ASSET_HUB_ROCOCO_ED * 16,
56+
..Default::default()
57+
},
58+
"session": crate::SessionConfig {
59+
keys: invulnerables
60+
.into_iter()
61+
.map(|(acc, aura)| {
62+
(
63+
acc.clone(), // account id
64+
acc, // validator id
65+
asset_hub_rococo_session_keys(aura), // session keys
66+
)
67+
})
68+
.collect(),
69+
..Default::default()
70+
},
71+
"polkadotXcm": crate::PolkadotXcmConfig {
72+
safe_xcm_version: Some(SAFE_XCM_VERSION),
73+
..Default::default()
74+
}
75+
})
76+
}
77+
78+
/// Encapsulates names of predefined presets.
79+
mod preset_names {
80+
pub const PRESET_DEVELOPMENT: &str = "development";
81+
pub const PRESET_LOCAL: &str = "local";
82+
pub const PRESET_GENESIS: &str = "genesis";
83+
}
84+
85+
/// Provides the JSON representation of predefined genesis config for given `id`.
86+
pub fn get_preset(id: &PresetId) -> Option<Vec<u8>> {
87+
use preset_names::*;
88+
let patch = match id.try_into() {
89+
Ok(PRESET_GENESIS) => asset_hub_rococo_genesis(
90+
// initial collators.
91+
vec![
92+
// E8XC6rTJRsioKCp6KMy6zd24ykj4gWsusZ3AkSeyavpVBAG
93+
(
94+
hex!("44cb62d1d6cdd2fff2a5ef3bb7ef827be5b3e117a394ecaa634d8dd9809d5608").into(),
95+
hex!("44cb62d1d6cdd2fff2a5ef3bb7ef827be5b3e117a394ecaa634d8dd9809d5608")
96+
.unchecked_into(),
97+
),
98+
// G28iWEybndgGRbhfx83t7Q42YhMPByHpyqWDUgeyoGF94ri
99+
(
100+
hex!("9864b85e23aa4506643db9879c3dbbeabaa94d269693a4447f537dd6b5893944").into(),
101+
hex!("9864b85e23aa4506643db9879c3dbbeabaa94d269693a4447f537dd6b5893944")
102+
.unchecked_into(),
103+
),
104+
// G839e2eMiq7UXbConsY6DS1XDAYG2XnQxAmLuRLGGQ3Px9c
105+
(
106+
hex!("9ce5741ee2f1ac3bdedbde9f3339048f4da2cb88ddf33a0977fa0b4cf86e2948").into(),
107+
hex!("9ce5741ee2f1ac3bdedbde9f3339048f4da2cb88ddf33a0977fa0b4cf86e2948")
108+
.unchecked_into(),
109+
),
110+
// GLao4ukFUW6qhexuZowdFrKa2NLCfnEjZMftSXXfvGv1vvt
111+
(
112+
hex!("a676ed15f5a325eab49ed8d5f8c00f3f814b19bb58cda14ad10894c078dd337f").into(),
113+
hex!("a676ed15f5a325eab49ed8d5f8c00f3f814b19bb58cda14ad10894c078dd337f")
114+
.unchecked_into(),
115+
),
116+
],
117+
Vec::new(),
118+
ASSET_HUB_ROCOCO_ED * 524_288,
119+
1000.into(),
120+
),
121+
Ok(PRESET_LOCAL) => asset_hub_rococo_genesis(
122+
// initial collators.
123+
vec![
124+
(
125+
get_account_id_from_seed::<sr25519::Public>("Alice"),
126+
get_collator_keys_from_seed::<AuraId>("Alice"),
127+
),
128+
(
129+
get_account_id_from_seed::<sr25519::Public>("Bob"),
130+
get_collator_keys_from_seed::<AuraId>("Bob"),
131+
),
132+
],
133+
vec![
134+
get_account_id_from_seed::<sr25519::Public>("Alice"),
135+
get_account_id_from_seed::<sr25519::Public>("Bob"),
136+
get_account_id_from_seed::<sr25519::Public>("Charlie"),
137+
get_account_id_from_seed::<sr25519::Public>("Dave"),
138+
get_account_id_from_seed::<sr25519::Public>("Eve"),
139+
get_account_id_from_seed::<sr25519::Public>("Ferdie"),
140+
get_account_id_from_seed::<sr25519::Public>("Alice//stash"),
141+
get_account_id_from_seed::<sr25519::Public>("Bob//stash"),
142+
get_account_id_from_seed::<sr25519::Public>("Charlie//stash"),
143+
get_account_id_from_seed::<sr25519::Public>("Dave//stash"),
144+
get_account_id_from_seed::<sr25519::Public>("Eve//stash"),
145+
get_account_id_from_seed::<sr25519::Public>("Ferdie//stash"),
146+
],
147+
testnet_parachains_constants::rococo::currency::UNITS * 1_000_000,
148+
1000.into(),
149+
),
150+
Ok(PRESET_DEVELOPMENT) => asset_hub_rococo_genesis(
151+
// initial collators.
152+
vec![(
153+
get_account_id_from_seed::<sr25519::Public>("Alice"),
154+
get_collator_keys_from_seed::<AuraId>("Alice"),
155+
)],
156+
vec![
157+
get_account_id_from_seed::<sr25519::Public>("Alice"),
158+
get_account_id_from_seed::<sr25519::Public>("Bob"),
159+
get_account_id_from_seed::<sr25519::Public>("Alice//stash"),
160+
get_account_id_from_seed::<sr25519::Public>("Bob//stash"),
161+
],
162+
testnet_parachains_constants::rococo::currency::UNITS * 1_000_000,
163+
1000.into(),
164+
),
165+
Err(_) | Ok(_) => return None,
166+
};
167+
168+
Some(
169+
serde_json::to_string(&patch)
170+
.expect("serialization to json is expected to work. qed.")
171+
.into_bytes(),
172+
)
173+
}
174+
175+
/// List of supported presets.
176+
pub fn preset_names() -> Vec<PresetId> {
177+
use preset_names::*;
178+
vec![
179+
PresetId::from(PRESET_GENESIS),
180+
PresetId::from(PRESET_DEVELOPMENT),
181+
PresetId::from(PRESET_LOCAL),
182+
]
183+
}

cumulus/parachains/runtimes/assets/asset-hub-rococo/src/lib.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#[cfg(feature = "std")]
2525
include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));
2626

27+
mod genesis_config_presets;
2728
mod weights;
2829
pub mod xcm_config;
2930

@@ -40,6 +41,7 @@ use cumulus_pallet_parachain_system::RelayNumberMonotonicallyIncreases;
4041
use cumulus_primitives_core::AggregateMessageOrigin;
4142
use sp_api::impl_runtime_apis;
4243
use sp_core::{crypto::KeyTypeId, OpaqueMetadata};
44+
use sp_genesis_builder::PresetId;
4345
use sp_runtime::{
4446
create_runtime_str, generic, impl_opaque_keys,
4547
traits::{AccountIdConversion, BlakeTwo256, Block as BlockT, Saturating, Verify},
@@ -1769,12 +1771,12 @@ impl_runtime_apis! {
17691771
build_state::<RuntimeGenesisConfig>(config)
17701772
}
17711773

1772-
fn get_preset(id: &Option<sp_genesis_builder::PresetId>) -> Option<Vec<u8>> {
1773-
get_preset::<RuntimeGenesisConfig>(id, |_| None)
1774+
fn get_preset(id: &Option<PresetId>) -> Option<Vec<u8>> {
1775+
get_preset::<RuntimeGenesisConfig>(id, &genesis_config_presets::get_preset)
17741776
}
17751777

1776-
fn preset_names() -> Vec<sp_genesis_builder::PresetId> {
1777-
vec![]
1778+
fn preset_names() -> Vec<PresetId> {
1779+
genesis_config_presets::preset_names()
17781780
}
17791781
}
17801782
}

cumulus/parachains/runtimes/constants/src/rococo.rs

+5
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,8 @@ pub mod snowbridge {
161161
pub EthereumNetwork: NetworkId = NetworkId::Ethereum { chain_id: 11155111 };
162162
}
163163
}
164+
165+
pub mod xcm_version {
166+
/// The default XCM version to set in genesis config.
167+
pub const SAFE_XCM_VERSION: u32 = xcm::prelude::XCM_VERSION;
168+
}

0 commit comments

Comments
 (0)