Skip to content

Commit 562870d

Browse files
parachain-template: genesis config presets added (#4739)
Gensis config presets moved from `parachain-template-node` binary into `parachain-template-runtime` runtime. cc: @PierreBesson
1 parent 95f3977 commit 562870d

File tree

8 files changed

+196
-134
lines changed

8 files changed

+196
-134
lines changed

Cargo.lock

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

prdoc/pr_4739.prdoc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
title: parachain-template - genesis config presets added
2+
3+
doc:
4+
- audience: Node Dev
5+
description: |
6+
- common DEV_RUNTIME_PRESET ("development") const added to sp-genesis-builder.
7+
- parachain-templates are now using presets.
8+
9+
crates:
10+
- name: sp-genesis-builder
11+
bump: minor
12+
- name: parachain-template-node
13+
bump: patch
14+
- name: parachain-template-runtime
15+
bump: patch

substrate/primitives/genesis-builder/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ pub type Result = core::result::Result<(), sp_runtime::RuntimeString>;
6262
/// The type representing preset ID.
6363
pub type PresetId = sp_runtime::RuntimeString;
6464

65+
/// The default `development` preset used to communicate with the runtime via
66+
/// [`GenesisBuilder`] interface.
67+
pub const DEV_RUNTIME_PRESET: &'static str = "development";
68+
6569
sp_api::decl_runtime_apis! {
6670
/// API to interact with RuntimeGenesisConfig for the runtime
6771
pub trait GenesisBuilder {
Lines changed: 2 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,11 @@
1-
use cumulus_primitives_core::ParaId;
21
use parachain_template_runtime as runtime;
3-
use runtime::{AccountId, AuraId, Signature, EXISTENTIAL_DEPOSIT};
42
use sc_chain_spec::{ChainSpecExtension, ChainSpecGroup};
53
use sc_service::ChainType;
64
use serde::{Deserialize, Serialize};
7-
use sp_core::{sr25519, Pair, Public};
8-
use sp_runtime::traits::{IdentifyAccount, Verify};
95

106
/// Specialized `ChainSpec` for the normal parachain runtime.
117
pub type ChainSpec = sc_service::GenericChainSpec<Extensions>;
128

13-
/// The default XCM version to set in genesis config.
14-
const SAFE_XCM_VERSION: u32 = xcm::prelude::XCM_VERSION;
15-
16-
/// Helper function to generate a crypto pair from seed
17-
pub fn get_from_seed<TPublic: Public>(seed: &str) -> <TPublic::Pair as Pair>::Public {
18-
TPublic::Pair::from_string(&format!("//{}", seed), None)
19-
.expect("static values are valid; qed")
20-
.public()
21-
}
22-
239
/// The extensions for the [`ChainSpec`].
2410
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ChainSpecGroup, ChainSpecExtension)]
2511
pub struct Extensions {
@@ -38,30 +24,6 @@ impl Extensions {
3824
}
3925
}
4026

41-
type AccountPublic = <Signature as Verify>::Signer;
42-
43-
/// Generate collator keys from seed.
44-
///
45-
/// This function's return type must always match the session keys of the chain in tuple format.
46-
pub fn get_collator_keys_from_seed(seed: &str) -> AuraId {
47-
get_from_seed::<AuraId>(seed)
48-
}
49-
50-
/// Helper function to generate an account ID from seed
51-
pub fn get_account_id_from_seed<TPublic: Public>(seed: &str) -> AccountId
52-
where
53-
AccountPublic: From<<TPublic::Pair as Pair>::Public>,
54-
{
55-
AccountPublic::from(get_from_seed::<TPublic>(seed)).into_account()
56-
}
57-
58-
/// Generate the session keys from individual elements.
59-
///
60-
/// The input must be a tuple of individual keys (a single arg for now since we have just one key).
61-
pub fn template_session_keys(keys: AuraId) -> runtime::SessionKeys {
62-
runtime::SessionKeys { aura: keys }
63-
}
64-
6527
pub fn development_config() -> ChainSpec {
6628
// Give your base currency a unit name and decimal places
6729
let mut properties = sc_chain_spec::Properties::new();
@@ -80,35 +42,7 @@ pub fn development_config() -> ChainSpec {
8042
.with_name("Development")
8143
.with_id("dev")
8244
.with_chain_type(ChainType::Development)
83-
.with_genesis_config_patch(testnet_genesis(
84-
// initial collators.
85-
vec![
86-
(
87-
get_account_id_from_seed::<sr25519::Public>("Alice"),
88-
get_collator_keys_from_seed("Alice"),
89-
),
90-
(
91-
get_account_id_from_seed::<sr25519::Public>("Bob"),
92-
get_collator_keys_from_seed("Bob"),
93-
),
94-
],
95-
vec![
96-
get_account_id_from_seed::<sr25519::Public>("Alice"),
97-
get_account_id_from_seed::<sr25519::Public>("Bob"),
98-
get_account_id_from_seed::<sr25519::Public>("Charlie"),
99-
get_account_id_from_seed::<sr25519::Public>("Dave"),
100-
get_account_id_from_seed::<sr25519::Public>("Eve"),
101-
get_account_id_from_seed::<sr25519::Public>("Ferdie"),
102-
get_account_id_from_seed::<sr25519::Public>("Alice//stash"),
103-
get_account_id_from_seed::<sr25519::Public>("Bob//stash"),
104-
get_account_id_from_seed::<sr25519::Public>("Charlie//stash"),
105-
get_account_id_from_seed::<sr25519::Public>("Dave//stash"),
106-
get_account_id_from_seed::<sr25519::Public>("Eve//stash"),
107-
get_account_id_from_seed::<sr25519::Public>("Ferdie//stash"),
108-
],
109-
get_account_id_from_seed::<sr25519::Public>("Alice"),
110-
1000.into(),
111-
))
45+
.with_genesis_config_preset_name("development")
11246
.build()
11347
}
11448

@@ -131,72 +65,8 @@ pub fn local_testnet_config() -> ChainSpec {
13165
.with_name("Local Testnet")
13266
.with_id("local_testnet")
13367
.with_chain_type(ChainType::Local)
134-
.with_genesis_config_patch(testnet_genesis(
135-
// initial collators.
136-
vec![
137-
(
138-
get_account_id_from_seed::<sr25519::Public>("Alice"),
139-
get_collator_keys_from_seed("Alice"),
140-
),
141-
(
142-
get_account_id_from_seed::<sr25519::Public>("Bob"),
143-
get_collator_keys_from_seed("Bob"),
144-
),
145-
],
146-
vec![
147-
get_account_id_from_seed::<sr25519::Public>("Alice"),
148-
get_account_id_from_seed::<sr25519::Public>("Bob"),
149-
get_account_id_from_seed::<sr25519::Public>("Charlie"),
150-
get_account_id_from_seed::<sr25519::Public>("Dave"),
151-
get_account_id_from_seed::<sr25519::Public>("Eve"),
152-
get_account_id_from_seed::<sr25519::Public>("Ferdie"),
153-
get_account_id_from_seed::<sr25519::Public>("Alice//stash"),
154-
get_account_id_from_seed::<sr25519::Public>("Bob//stash"),
155-
get_account_id_from_seed::<sr25519::Public>("Charlie//stash"),
156-
get_account_id_from_seed::<sr25519::Public>("Dave//stash"),
157-
get_account_id_from_seed::<sr25519::Public>("Eve//stash"),
158-
get_account_id_from_seed::<sr25519::Public>("Ferdie//stash"),
159-
],
160-
get_account_id_from_seed::<sr25519::Public>("Alice"),
161-
1000.into(),
162-
))
68+
.with_genesis_config_preset_name("local_testnet")
16369
.with_protocol_id("template-local")
16470
.with_properties(properties)
16571
.build()
16672
}
167-
168-
fn testnet_genesis(
169-
invulnerables: Vec<(AccountId, AuraId)>,
170-
endowed_accounts: Vec<AccountId>,
171-
root: AccountId,
172-
id: ParaId,
173-
) -> serde_json::Value {
174-
serde_json::json!({
175-
"balances": {
176-
"balances": endowed_accounts.iter().cloned().map(|k| (k, 1u64 << 60)).collect::<Vec<_>>(),
177-
},
178-
"parachainInfo": {
179-
"parachainId": id,
180-
},
181-
"collatorSelection": {
182-
"invulnerables": invulnerables.iter().cloned().map(|(acc, _)| acc).collect::<Vec<_>>(),
183-
"candidacyBond": EXISTENTIAL_DEPOSIT * 16,
184-
},
185-
"session": {
186-
"keys": invulnerables
187-
.into_iter()
188-
.map(|(acc, aura)| {
189-
(
190-
acc.clone(), // account id
191-
acc, // validator id
192-
template_session_keys(aura), // session keys
193-
)
194-
})
195-
.collect::<Vec<_>>(),
196-
},
197-
"polkadotXcm": {
198-
"safeXcmVersion": Some(SAFE_XCM_VERSION),
199-
},
200-
"sudo": { "key": Some(root) }
201-
})
202-
}

templates/parachain/runtime/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ scale-info = { features = [
2727
], workspace = true }
2828
smallvec = { workspace = true, default-features = true }
2929
docify = { workspace = true }
30+
serde_json = { workspace = true, default-features = false }
3031

3132
# Local
3233
pallet-parachain-template = { workspace = true }
@@ -126,6 +127,7 @@ std = [
126127
"polkadot-parachain-primitives/std",
127128
"polkadot-runtime-common/std",
128129
"scale-info/std",
130+
"serde_json/std",
129131
"sp-api/std",
130132
"sp-block-builder/std",
131133
"sp-consensus-aura/std",

templates/parachain/runtime/src/apis.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,11 +295,11 @@ impl_runtime_apis! {
295295
}
296296

297297
fn get_preset(id: &Option<sp_genesis_builder::PresetId>) -> Option<Vec<u8>> {
298-
get_preset::<RuntimeGenesisConfig>(id, |_| None)
298+
get_preset::<RuntimeGenesisConfig>(id, crate::genesis_config_presets::get_preset)
299299
}
300300

301301
fn preset_names() -> Vec<sp_genesis_builder::PresetId> {
302-
Default::default()
302+
crate::genesis_config_presets::preset_names()
303303
}
304304
}
305305
}

0 commit comments

Comments
 (0)