Skip to content

Commit

Permalink
chore(genesis): move development genesis config to runtime
Browse files Browse the repository at this point in the history
Move Development Genesis Config to the Runtime to
support decoupling of client and runtime dependencies.

When running `chain-spec-builder`, the following command:

```
chain-spec-builder list-presets –runtime-wasm-path ./target/debug/wbuild/frequency-runtime/frequency_runtime.wasm
```

outputs:
```
{“presets”:[“development”]}
```

issue-2149
issue-2142

#2149
  • Loading branch information
enddynayn committed Sep 5, 2024
1 parent c7859c5 commit 7a032a9
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 141 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

143 changes: 4 additions & 139 deletions node/service/src/chain_spec/frequency_dev.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
#![allow(missing_docs)]
use common_primitives::node::AccountId;
use common_runtime::constants::{
currency::EXISTENTIAL_DEPOSIT, FREQUENCY_LOCAL_TOKEN, TOKEN_DECIMALS,
};
use cumulus_primitives_core::ParaId;
use frequency_runtime::{AuraId, CouncilConfig, Ss58Prefix, SudoConfig, TechnicalCommitteeConfig};
use common_runtime::constants::{FREQUENCY_LOCAL_TOKEN, TOKEN_DECIMALS};
use frequency_runtime::Ss58Prefix;
use sc_service::ChainType;
use sp_core::sr25519;
use sp_runtime::traits::AccountIdConversion;
/// Specialized `ChainSpec` for the normal parachain runtime.
pub type ChainSpec = sc_service::GenericChainSpec<Extensions>;

use super::{get_account_id_from_seed, get_collator_keys_from_seed, get_properties, Extensions};
use super::{get_properties, Extensions};

/// Generates the chain spec for a development (no relay)
pub fn development_config() -> ChainSpec {
Expand All @@ -31,135 +25,6 @@ pub fn development_config() -> ChainSpec {
.with_properties(properties)
.with_chain_type(ChainType::Development)
.with_protocol_id("dev")
.with_genesis_config(development_genesis(
development_invulnerables(),
development_root(),
development_endowed_accounts(),
development_council_members(),
development_technical_committee_members(),
// ParaId
1000.into(),
))
.with_genesis_config_preset_name("development")
.build()
}

/// Generate the session keys from individual elements.
///
/// The input must be a tuple of individual keys (a single arg for now since we have just one key).
fn template_session_keys(keys: AuraId) -> frequency_runtime::SessionKeys {
frequency_runtime::SessionKeys { aura: keys }
}

#[allow(clippy::unwrap_used)]
fn load_genesis_schemas() -> Vec<frequency_runtime::pallet_schemas::GenesisSchema> {
serde_json::from_slice(include_bytes!("../../../../resources/genesis-schemas.json")).unwrap()
}

#[allow(clippy::unwrap_used)]
fn development_genesis(
invulnerables: Vec<(AccountId, AuraId)>,
root_key: AccountId,
endowed_accounts: Vec<AccountId>,
council_members: Vec<AccountId>,
technical_committee_members: Vec<AccountId>,
id: ParaId,
) -> serde_json::Value {
let genesis = frequency_runtime::RuntimeGenesisConfig {
system: Default::default(),
balances: frequency_runtime::BalancesConfig {
balances: endowed_accounts.iter().cloned().map(|k| (k, 1 << 60)).collect(),
},
parachain_info: frequency_runtime::ParachainInfoConfig {
parachain_id: id,
..Default::default()
},
collator_selection: frequency_runtime::CollatorSelectionConfig {
invulnerables: invulnerables.iter().cloned().map(|(acc, _)| acc).collect(),
candidacy_bond: EXISTENTIAL_DEPOSIT * 16,
..Default::default()
},
session: frequency_runtime::SessionConfig {
keys: invulnerables
.into_iter()
.map(|(acc, aura)| {
(
acc.clone(), // account id
acc, // validator id
template_session_keys(aura), // session keys
)
})
.collect(),
},
// no need to pass anything to aura, in fact it will panic if we do. Session will take care
// of this.
aura: Default::default(),
aura_ext: Default::default(),
#[cfg(any(not(feature = "frequency-no-relay"), feature = "frequency-lint-check"))]
parachain_system: Default::default(),
sudo: SudoConfig {
// Assign network admin rights.
key: Some(root_key),
},
schemas: frequency_runtime::pallet_schemas::GenesisConfig {
initial_schemas: load_genesis_schemas(),
..Default::default()
},
time_release: Default::default(),
democracy: Default::default(),
treasury: Default::default(),
council: CouncilConfig { phantom: Default::default(), members: council_members },
technical_committee: TechnicalCommitteeConfig {
phantom: Default::default(),
members: technical_committee_members,
},
};

serde_json::to_value(&genesis).unwrap()
}

fn development_endowed_accounts() -> Vec<AccountId> {
vec![
get_account_id_from_seed::<sr25519::Public>("Alice"),
get_account_id_from_seed::<sr25519::Public>("Bob"),
get_account_id_from_seed::<sr25519::Public>("Charlie"),
get_account_id_from_seed::<sr25519::Public>("Dave"),
get_account_id_from_seed::<sr25519::Public>("Eve"),
get_account_id_from_seed::<sr25519::Public>("Ferdie"),
get_account_id_from_seed::<sr25519::Public>("Alice//stash"),
get_account_id_from_seed::<sr25519::Public>("Bob//stash"),
get_account_id_from_seed::<sr25519::Public>("Charlie//stash"),
get_account_id_from_seed::<sr25519::Public>("Dave//stash"),
get_account_id_from_seed::<sr25519::Public>("Eve//stash"),
get_account_id_from_seed::<sr25519::Public>("Ferdie//stash"),
common_runtime::constants::TREASURY_PALLET_ID.into_account_truncating(),
]
}

fn development_invulnerables() -> Vec<(AccountId, AuraId)> {
vec![
(
get_account_id_from_seed::<sr25519::Public>("Alice"),
get_collator_keys_from_seed("Alice"),
),
(get_account_id_from_seed::<sr25519::Public>("Bob"), get_collator_keys_from_seed("Bob")),
]
}

fn development_root() -> AccountId {
get_account_id_from_seed::<sr25519::Public>("Alice")
}

fn development_council_members() -> Vec<AccountId> {
vec![
get_account_id_from_seed::<sr25519::Public>("Alice"),
get_account_id_from_seed::<sr25519::Public>("Charlie"),
get_account_id_from_seed::<sr25519::Public>("Eve"),
]
}

fn development_technical_committee_members() -> Vec<AccountId> {
vec![
get_account_id_from_seed::<sr25519::Public>("Bob"),
get_account_id_from_seed::<sr25519::Public>("Dave"),
]
}
5 changes: 5 additions & 0 deletions runtime/frequency/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ log = { workspace = true }
scale-info = { workspace = true, features = ["derive"] }

serde = { workspace = true, optional = true, features = ["derive"] }
serde_json = { workspace = true, optional = true, features = ["alloc"] }

smallvec = { workspace = true }
# Substrate
frame-benchmarking = { workspace = true, optional = true }
Expand Down Expand Up @@ -85,6 +87,7 @@ pallet-passkey = { path = "../../pallets/passkey", default-features = false }
system-runtime-api = { path = '../system-runtime-api', default-features = false }
# Polkadot
polkadot-parachain-primitives = { workspace = true }
# polkadot-primitives = { workspace = true }
polkadot-runtime-common = { workspace = true }
# Cumulus
cumulus-pallet-aura-ext = { workspace = true }
Expand Down Expand Up @@ -148,8 +151,10 @@ std = [
"parity-scale-codec/std",
"polkadot-parachain-primitives/std",
"polkadot-runtime-common/std",
# "polkadot-primitives/std",
"scale-info/std",
"serde/std",
"serde_json/std",
"sp-api/std",
"sp-block-builder/std",
"sp-consensus-aura/std",
Expand Down
14 changes: 12 additions & 2 deletions runtime/frequency/src/apis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ use sp_runtime::{
use sp_std::prelude::Vec;
use sp_version::RuntimeVersion;

// Genesis preset configurations.
use crate::genesis_config_presets;

use super::{
AccountId, Balance, Block, Executive, InherentDataExt, Runtime, RuntimeCall,
RuntimeGenesisConfig, SessionKeys, System, TransactionPayment, VERSION,
Expand Down Expand Up @@ -149,11 +152,18 @@ impl_runtime_apis! {
}

fn get_preset(id: &Option<sp_genesis_builder::PresetId>) -> Option<Vec<u8>> {
get_preset::<RuntimeGenesisConfig>(id, |_| None)
// if let Some(id) = id {
// genesis_config_presets::get_preset(id)
// } else {
// get_preset::<RuntimeGenesisConfig>(&None)
// }
get_preset::<RuntimeGenesisConfig>(id, &genesis_config_presets::get_preset)
}

fn preset_names() -> Vec<sp_genesis_builder::PresetId> {
Default::default()
vec![
sp_genesis_builder::PresetId::from("development"),
]
}
}

Expand Down
157 changes: 157 additions & 0 deletions runtime/frequency/src/development_genesis.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
use common_primitives::node::Verify;
use cumulus_primitives_core::ParaId;
use sp_core::{sr25519, Pair, Public};
use sp_runtime::traits::IdentifyAccount;
#[cfg(not(feature = "std"))]
use sp_std::alloc::format;

use crate::*;
// Generate the session keys from individual elements.
//
// The input must be a tuple of individual keys (a single arg for now since we have just one key).
fn template_session_keys(keys: AuraId) -> SessionKeys {
SessionKeys { aura: keys }
}

#[allow(clippy::unwrap_used)]
fn load_genesis_schemas() -> Vec<crate::pallet_schemas::GenesisSchema> {
serde_json::from_slice(include_bytes!("../../../resources/genesis-schemas.json")).unwrap()
}

type AccountPublic = <Signature as Verify>::Signer;

#[allow(clippy::unwrap_used)]
pub fn development_genesis(
invulnerables: Vec<(AccountId, AuraId)>,
root_key: AccountId,
endowed_accounts: Vec<AccountId>,
council_members: Vec<AccountId>,
technical_committee_members: Vec<AccountId>,
id: ParaId,
) -> serde_json::Value {
let genesis = RuntimeGenesisConfig {
system: Default::default(),
balances: BalancesConfig {
balances: endowed_accounts.iter().cloned().map(|k| (k, 1 << 60)).collect(),
},
parachain_info: ParachainInfoConfig { parachain_id: id, ..Default::default() },
collator_selection: CollatorSelectionConfig {
invulnerables: invulnerables.iter().cloned().map(|(acc, _)| acc).collect(),
candidacy_bond: EXISTENTIAL_DEPOSIT * 16,
..Default::default()
},
session: SessionConfig {
keys: invulnerables
.into_iter()
.map(|(acc, aura)| {
(
acc.clone(), // account id
acc, // validator id
template_session_keys(aura), // session keys
)
})
.collect(),
},
// no need to pass anything to aura, in fact it will panic if we do. Session will take care
// of this.
aura: Default::default(),
aura_ext: Default::default(),
#[cfg(any(not(feature = "frequency-no-relay"), feature = "frequency-lint-check"))]
parachain_system: Default::default(),
sudo: SudoConfig {
// Assign network admin rights.
key: Some(root_key),
},
schemas: crate::pallet_schemas::GenesisConfig {
initial_schemas: load_genesis_schemas(),
..Default::default()
},
time_release: Default::default(),
democracy: Default::default(),
treasury: Default::default(),
council: CouncilConfig { phantom: Default::default(), members: council_members },
technical_committee: TechnicalCommitteeConfig {
phantom: Default::default(),
members: technical_committee_members,
},
};

serde_json::to_value(&genesis).unwrap()
}

pub fn development_endowed_accounts() -> Vec<AccountId> {
vec![
get_account_id_from_seed::<sr25519::Public>("Alice"),
get_account_id_from_seed::<sr25519::Public>("Bob"),
get_account_id_from_seed::<sr25519::Public>("Charlie"),
get_account_id_from_seed::<sr25519::Public>("Dave"),
get_account_id_from_seed::<sr25519::Public>("Eve"),
get_account_id_from_seed::<sr25519::Public>("Ferdie"),
get_account_id_from_seed::<sr25519::Public>("Alice//stash"),
get_account_id_from_seed::<sr25519::Public>("Bob//stash"),
get_account_id_from_seed::<sr25519::Public>("Charlie//stash"),
get_account_id_from_seed::<sr25519::Public>("Dave//stash"),
get_account_id_from_seed::<sr25519::Public>("Eve//stash"),
get_account_id_from_seed::<sr25519::Public>("Ferdie//stash"),
common_runtime::constants::TREASURY_PALLET_ID.into_account_truncating(),
]
}

pub fn development_invulnerables() -> Vec<(AccountId, AuraId)> {
vec![
(
get_account_id_from_seed::<sr25519::Public>("Alice"),
get_collator_keys_from_seed("Alice"),
),
(get_account_id_from_seed::<sr25519::Public>("Bob"), get_collator_keys_from_seed("Bob")),
]
}

pub fn development_root() -> AccountId {
get_account_id_from_seed::<sr25519::Public>("Alice")
}

pub fn development_council_members() -> Vec<AccountId> {
vec![
get_account_id_from_seed::<sr25519::Public>("Alice"),
get_account_id_from_seed::<sr25519::Public>("Charlie"),
get_account_id_from_seed::<sr25519::Public>("Eve"),
]
}

pub fn development_technical_committee_members() -> Vec<AccountId> {
vec![
get_account_id_from_seed::<sr25519::Public>("Bob"),
get_account_id_from_seed::<sr25519::Public>("Dave"),
]
}

/// Helper function to generate a crypto pair from seed
pub fn get_from_seed<TPublic: Public>(seed: &str) -> <TPublic::Pair as Pair>::Public {
TPublic::Pair::from_string(&format!("//{}", seed), None)
.expect("static values are valid; qed")
.public()
}

/// Helper function to generate an account ID from seed
pub fn get_account_id_from_seed<TPublic: Public>(seed: &str) -> AccountId
where
AccountPublic: From<<TPublic::Pair as Pair>::Public>,
{
AccountPublic::from(get_from_seed::<TPublic>(seed)).into_account()
}

/// Generate collator keys from seed.
///
/// This function's return type must always match the session keys of the chain in tuple format.
pub fn get_collator_keys_from_seed(seed: &str) -> AuraId {
get_public_from_seed::<AuraId>(seed)
}

#[allow(clippy::expect_used)]
/// Helper function to generate a crypto pair from seed
pub fn get_public_from_seed<TPublic: Public>(seed: &str) -> <TPublic::Pair as Pair>::Public {
TPublic::Pair::from_string(&format!("//{}", seed), None)
.expect("static values are valid; qed")
.public()
}
Loading

0 comments on commit 7a032a9

Please sign in to comment.