chain-spec: getting ready for native-runtime-free world (#1256)

This PR prepares chains specs for _native-runtime-free_  world.

This PR has following changes:
- `substrate`:
  - adds support for:
- JSON based `GenesisConfig` to `ChainSpec` allowing interaction with
runtime `GenesisBuilder` API.
- interacting with arbitrary runtime wasm blob to[
`chain-spec-builder`](https://github.com/paritytech/substrate/blob/3ef576eaeb3f42610e85daecc464961cf1295570/bin/utils/chain-spec-builder/src/lib.rs#L46)
command line util,
- removes
[`code`](https://github.com/paritytech/substrate/blob/3ef576eaeb3f42610e85daecc464961cf1295570/frame/system/src/lib.rs#L660)
from `system_pallet`
  - adds `code` to the `ChainSpec`
- deprecates
[`ChainSpec::from_genesis`](https://github.com/paritytech/substrate/blob/3ef576eaeb3f42610e85daecc464961cf1295570/client/chain-spec/src/chain_spec.rs#L263),
but also changes the signature of this method extending it with `code`
argument.
[`ChainSpec::builder()`](https://github.com/paritytech/substrate/blob/20bee680ed098be7239cf7a6b804cd4de267983e/client/chain-spec/src/chain_spec.rs#L507)
should be used instead.
- `polkadot`:
- all references to `RuntimeGenesisConfig` in `node/service` are
removed,
- all
`(kusama|polkadot|versi|rococo|wococo)_(staging|dev)_genesis_config`
functions now return the JSON patch for default runtime `GenesisConfig`,
  - `ChainSpecBuilder` is used, `ChainSpec::from_genesis` is removed,

- `cumulus`:
  - `ChainSpecBuilder` is used, `ChainSpec::from_genesis` is removed,
- _JSON_ patch configuration used instead of `RuntimeGenesisConfig
struct` in all chain specs.
  
---------

Co-authored-by: command-bot <>
Co-authored-by: Javier Viola <javier@parity.io>
Co-authored-by: Davide Galassi <davxy@datawok.net>
Co-authored-by: Francisco Aguirre <franciscoaguirreperez@gmail.com>
Co-authored-by: Kevin Krone <kevin@parity.io>
Co-authored-by: Bastian Köcher <git@kchr.de>
This commit is contained in:
Michal Kucharczyk
2023-11-05 15:19:23 +01:00
committed by GitHub
parent c46a7dbb61
commit 8ba7a6aba8
90 changed files with 4833 additions and 3059 deletions
+1
View File
@@ -21,6 +21,7 @@ clap = { version = "4.0.9", features = ["derive"] }
futures = { version = "0.3.21", features = ["thread-pool"] }
futures-timer = "3.0.1"
jsonrpsee = { version = "0.16.2", features = ["server"] }
serde_json = "1.0.107"
sc-cli = { path = "../../../client/cli" }
sc-executor = { path = "../../../client/executor" }
+15 -26
View File
@@ -15,12 +15,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use runtime::{BalancesConfig, RuntimeGenesisConfig, SudoConfig, SystemConfig, WASM_BINARY};
use runtime::{BalancesConfig, SudoConfig, WASM_BINARY};
use sc_service::{ChainType, Properties};
use serde_json::{json, Value};
use sp_keyring::AccountKeyring;
/// This is a specialization of the general Substrate ChainSpec type.
pub type ChainSpec = sc_service::GenericChainSpec<RuntimeGenesisConfig>;
pub type ChainSpec = sc_service::GenericChainSpec<()>;
fn props() -> Properties {
let mut properties = Properties::new();
@@ -30,37 +31,25 @@ fn props() -> Properties {
}
pub fn development_config() -> Result<ChainSpec, String> {
let wasm_binary = WASM_BINARY.ok_or_else(|| "Development wasm not available".to_string())?;
Ok(ChainSpec::from_genesis(
"Development",
"dev",
ChainType::Development,
move || testnet_genesis(wasm_binary),
vec![],
None,
None,
None,
Some(props()),
None,
))
Ok(ChainSpec::builder(WASM_BINARY.expect("Development wasm not available"), Default::default())
.with_name("Development")
.with_id("dev")
.with_chain_type(ChainType::Development)
.with_genesis_config_patch(testnet_genesis())
.with_properties(props())
.build())
}
/// Configure initial storage state for FRAME pallets.
fn testnet_genesis(wasm_binary: &[u8]) -> RuntimeGenesisConfig {
fn testnet_genesis() -> Value {
use frame::traits::Get;
use runtime::interface::{Balance, MinimumBalance};
let endowment = <MinimumBalance as Get<Balance>>::get().max(1) * 1000;
let balances = AccountKeyring::iter()
.map(|a| (a.to_account_id(), endowment))
.collect::<Vec<_>>();
RuntimeGenesisConfig {
system: SystemConfig {
// Add Wasm runtime to storage.
code: wasm_binary.to_vec(),
_config: Default::default(),
},
balances: BalancesConfig { balances },
sudo: SudoConfig { key: Some(AccountKeyring::Alice.to_account_id()) },
..Default::default()
}
json!({
"balances": BalancesConfig { balances },
"sudo": SudoConfig { key: Some(AccountKeyring::Alice.to_account_id()) },
})
}
+4
View File
@@ -9,6 +9,7 @@ scale-info = { version = "2.6.0", default-features = false }
# this is a frame-based runtime, thus importing `frame` with runtime feature enabled.
frame = { path = "../../../frame", default-features = false, features = ["runtime", "experimental"] }
frame-support = { path = "../../../frame/support", default-features = false}
# pallets that we want to use
pallet-balances = { path = "../../../frame/balances", default-features = false }
@@ -17,6 +18,9 @@ pallet-timestamp = { path = "../../../frame/timestamp", default-features = false
pallet-transaction-payment = { path = "../../../frame/transaction-payment", default-features = false }
pallet-transaction-payment-rpc-runtime-api = { path = "../../../frame/transaction-payment/rpc/runtime-api", default-features = false }
# genesis builder that allows us to interacto with runtime genesis config
sp-genesis-builder = { path = "../../../primitives/genesis-builder", default-features = false}
[build-dependencies]
substrate-wasm-builder = { path = "../../../utils/wasm-builder", optional = true }
+11
View File
@@ -31,6 +31,7 @@ use frame::{
prelude::*,
},
};
use frame_support::genesis_builder_helper::{build_config, create_default_config};
#[runtime_version]
pub const VERSION: RuntimeVersion = RuntimeVersion {
@@ -210,6 +211,16 @@ impl_runtime_apis! {
TransactionPayment::length_to_fee(length)
}
}
impl sp_genesis_builder::GenesisBuilder<Block> for Runtime {
fn create_default_config() -> Vec<u8> {
create_default_config::<RuntimeGenesisConfig>()
}
fn build_config(config: Vec<u8>) -> sp_genesis_builder::Result {
build_config::<RuntimeGenesisConfig>(config)
}
}
}
/// Some re-exports that the node side code needs to know. Some are useful in this context as well.
@@ -19,6 +19,7 @@ name = "node-template"
[dependencies]
clap = { version = "4.4.6", features = ["derive"] }
futures = { version = "0.3.21", features = ["thread-pool"]}
serde_json = "1.0.85"
sc-cli = { path = "../../../client/cli" }
sp-core = { path = "../../../primitives/core" }
@@ -1,7 +1,4 @@
use node_template_runtime::{
AccountId, AuraConfig, BalancesConfig, GrandpaConfig, RuntimeGenesisConfig, Signature,
SudoConfig, SystemConfig, WASM_BINARY,
};
use node_template_runtime::{AccountId, RuntimeGenesisConfig, Signature, WASM_BINARY};
use sc_service::ChainType;
use sp_consensus_aura::sr25519::AuthorityId as AuraId;
use sp_consensus_grandpa::AuthorityId as GrandpaId;
@@ -37,122 +34,84 @@ pub fn authority_keys_from_seed(s: &str) -> (AuraId, GrandpaId) {
}
pub fn development_config() -> Result<ChainSpec, String> {
let wasm_binary = WASM_BINARY.ok_or_else(|| "Development wasm not available".to_string())?;
Ok(ChainSpec::from_genesis(
// Name
"Development",
// ID
"dev",
ChainType::Development,
move || {
testnet_genesis(
wasm_binary,
// Initial PoA authorities
vec![authority_keys_from_seed("Alice")],
// Sudo account
get_account_id_from_seed::<sr25519::Public>("Alice"),
// Pre-funded accounts
vec![
get_account_id_from_seed::<sr25519::Public>("Alice"),
get_account_id_from_seed::<sr25519::Public>("Bob"),
get_account_id_from_seed::<sr25519::Public>("Alice//stash"),
get_account_id_from_seed::<sr25519::Public>("Bob//stash"),
],
true,
)
},
// Bootnodes
vec![],
// Telemetry
None,
// Protocol ID
None,
None,
// Properties
None,
// Extensions
Ok(ChainSpec::builder(
WASM_BINARY.ok_or_else(|| "Development wasm not available".to_string())?,
None,
)
.with_name("Development")
.with_id("dev")
.with_chain_type(ChainType::Development)
.with_genesis_config_patch(testnet_genesis(
// Initial PoA authorities
vec![authority_keys_from_seed("Alice")],
// Sudo account
get_account_id_from_seed::<sr25519::Public>("Alice"),
// Pre-funded accounts
vec![
get_account_id_from_seed::<sr25519::Public>("Alice"),
get_account_id_from_seed::<sr25519::Public>("Bob"),
get_account_id_from_seed::<sr25519::Public>("Alice//stash"),
get_account_id_from_seed::<sr25519::Public>("Bob//stash"),
],
true,
))
.build())
}
pub fn local_testnet_config() -> Result<ChainSpec, String> {
let wasm_binary = WASM_BINARY.ok_or_else(|| "Development wasm not available".to_string())?;
Ok(ChainSpec::from_genesis(
// Name
"Local Testnet",
// ID
"local_testnet",
ChainType::Local,
move || {
testnet_genesis(
wasm_binary,
// Initial PoA authorities
vec![authority_keys_from_seed("Alice"), authority_keys_from_seed("Bob")],
// Sudo account
get_account_id_from_seed::<sr25519::Public>("Alice"),
// Pre-funded accounts
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"),
],
true,
)
},
// Bootnodes
vec![],
// Telemetry
None,
// Protocol ID
None,
// Properties
None,
None,
// Extensions
Ok(ChainSpec::builder(
WASM_BINARY.ok_or_else(|| "Development wasm not available".to_string())?,
None,
)
.with_name("Local Testnet")
.with_id("local_testnet")
.with_chain_type(ChainType::Local)
.with_genesis_config_patch(testnet_genesis(
// Initial PoA authorities
vec![authority_keys_from_seed("Alice"), authority_keys_from_seed("Bob")],
// Sudo account
get_account_id_from_seed::<sr25519::Public>("Alice"),
// Pre-funded accounts
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"),
],
true,
))
.build())
}
/// Configure initial storage state for FRAME modules.
fn testnet_genesis(
wasm_binary: &[u8],
initial_authorities: Vec<(AuraId, GrandpaId)>,
root_key: AccountId,
endowed_accounts: Vec<AccountId>,
_enable_println: bool,
) -> RuntimeGenesisConfig {
RuntimeGenesisConfig {
system: SystemConfig {
// Add Wasm runtime to storage.
code: wasm_binary.to_vec(),
..Default::default()
},
balances: BalancesConfig {
) -> serde_json::Value {
serde_json::json!({
"balances": {
// Configure endowed accounts with initial balance of 1 << 60.
balances: endowed_accounts.iter().cloned().map(|k| (k, 1 << 60)).collect(),
"balances": endowed_accounts.iter().cloned().map(|k| (k, 1u64 << 60)).collect::<Vec<_>>(),
},
aura: AuraConfig {
authorities: initial_authorities.iter().map(|x| (x.0.clone())).collect(),
"aura": {
"authorities": initial_authorities.iter().map(|x| (x.0.clone())).collect::<Vec<_>>(),
},
grandpa: GrandpaConfig {
authorities: initial_authorities.iter().map(|x| (x.1.clone(), 1)).collect(),
..Default::default()
"grandpa": {
"authorities": initial_authorities.iter().map(|x| (x.1.clone(), 1)).collect::<Vec<_>>(),
},
sudo: SudoConfig {
"sudo": {
// Assign network admin rights.
key: Some(root_key),
"key": Some(root_key),
},
transaction_payment: Default::default(),
}
})
}
@@ -14,7 +14,7 @@ targets = ["x86_64-unknown-linux-gnu"]
[dependencies]
codec = { package = "parity-scale-codec", version = "3.6.1", default-features = false, features = ["derive"] }
scale-info = { version = "2.10.0", default-features = false, features = ["derive"] }
scale-info = { version = "2.10.0", default-features = false, features = ["derive", "serde"] }
pallet-aura = { path = "../../../frame/aura", default-features = false}
pallet-balances = { path = "../../../frame/balances", default-features = false}
@@ -28,17 +28,18 @@ pallet-transaction-payment = { path = "../../../frame/transaction-payment", defa
frame-executive = { path = "../../../frame/executive", default-features = false}
sp-api = { path = "../../../primitives/api", default-features = false}
sp-block-builder = { path = "../../../primitives/block-builder", default-features = false}
sp-consensus-aura = { path = "../../../primitives/consensus/aura", default-features = false}
sp-consensus-grandpa = { path = "../../../primitives/consensus/grandpa", default-features = false}
sp-core = { path = "../../../primitives/core", default-features = false}
sp-consensus-aura = { path = "../../../primitives/consensus/aura", default-features = false, features = ["serde"] }
sp-consensus-grandpa = { path = "../../../primitives/consensus/grandpa", default-features = false, features = ["serde"] }
sp-core = { path = "../../../primitives/core", default-features = false, features = ["serde"]}
sp-inherents = { path = "../../../primitives/inherents", default-features = false}
sp-offchain = { path = "../../../primitives/offchain", default-features = false}
sp-runtime = { path = "../../../primitives/runtime", default-features = false}
sp-runtime = { path = "../../../primitives/runtime", default-features = false, features = ["serde"] }
sp-session = { path = "../../../primitives/session", default-features = false}
sp-std = { path = "../../../primitives/std", default-features = false}
sp-storage = { path = "../../../primitives/storage", default-features = false}
sp-transaction-pool = { path = "../../../primitives/transaction-pool", default-features = false}
sp-version = { path = "../../../primitives/version", default-features = false}
sp-version = { path = "../../../primitives/version", default-features = false, features = ["serde"] }
serde_json = { version = "1.0.85", default-features = false, features = ["alloc"] }
sp-genesis-builder = { default-features = false, path = "../../../primitives/genesis-builder" }
# Used for the node template's RPCs
@@ -75,6 +76,7 @@ std = [
"pallet-transaction-payment-rpc-runtime-api/std",
"pallet-transaction-payment/std",
"scale-info/std",
"serde_json/std",
"sp-api/std",
"sp-block-builder/std",
"sp-consensus-aura/std",
-1
View File
@@ -124,7 +124,6 @@ futures = "0.3.21"
tempfile = "3.1.0"
assert_cmd = "2.0.2"
nix = { version = "0.26.1", features = ["signal"] }
serde_json = "1.0"
regex = "1.6.0"
platforms = "3.0"
soketto = "0.7.1"
+109 -137
View File
@@ -20,10 +20,7 @@
use grandpa_primitives::AuthorityId as GrandpaId;
use kitchensink_runtime::{
constants::currency::*, wasm_binary_unwrap, BabeConfig, BalancesConfig, Block, CouncilConfig,
DemocracyConfig, ElectionsConfig, ImOnlineConfig, IndicesConfig, MaxNominations,
NominationPoolsConfig, SessionConfig, SessionKeys, SocietyConfig, StakerStatus, StakingConfig,
SudoConfig, SystemConfig, TechnicalCommitteeConfig,
constants::currency::*, wasm_binary_unwrap, Block, MaxNominations, SessionKeys, StakerStatus,
};
use pallet_im_online::sr25519::AuthorityId as ImOnlineId;
use sc_chain_spec::ChainSpecExtension;
@@ -45,6 +42,8 @@ pub use node_primitives::{AccountId, Balance, Signature};
type AccountPublic = <Signature as Verify>::Signer;
const STAGING_TELEMETRY_URL: &str = "wss://telemetry.polkadot.io/submit/";
const ENDOWMENT: Balance = 10_000_000 * DOLLARS;
const STASH: Balance = ENDOWMENT / 1000;
/// Node `ChainSpec` extensions.
///
@@ -78,7 +77,11 @@ fn session_keys(
SessionKeys { grandpa, babe, im_online, authority_discovery, mixnet }
}
fn staging_testnet_config_genesis() -> RuntimeGenesisConfig {
fn configure_accounts_for_staging_testnet() -> (
Vec<(AccountId, AccountId, GrandpaId, BabeId, ImOnlineId, AuthorityDiscoveryId, MixnetId)>,
AccountId,
Vec<AccountId>,
) {
#[rustfmt::skip]
// stash, controller, session-key
// generated with secret:
@@ -190,28 +193,27 @@ fn staging_testnet_config_genesis() -> RuntimeGenesisConfig {
);
let endowed_accounts: Vec<AccountId> = vec![root_key.clone()];
(initial_authorities, root_key, endowed_accounts)
}
fn staging_testnet_config_genesis() -> serde_json::Value {
let (initial_authorities, root_key, endowed_accounts) =
configure_accounts_for_staging_testnet();
testnet_genesis(initial_authorities, vec![], root_key, Some(endowed_accounts))
}
/// Staging testnet config.
pub fn staging_testnet_config() -> ChainSpec {
let boot_nodes = vec![];
ChainSpec::from_genesis(
"Staging Testnet",
"staging_testnet",
ChainType::Live,
staging_testnet_config_genesis,
boot_nodes,
Some(
ChainSpec::builder(wasm_binary_unwrap(), Default::default())
.with_name("Staging Testnet")
.with_id("staging_testnet")
.with_chain_type(ChainType::Live)
.with_genesis_config_patch(staging_testnet_config_genesis())
.with_telemetry_endpoints(
TelemetryEndpoints::new(vec![(STAGING_TELEMETRY_URL.to_string(), 0)])
.expect("Staging telemetry url is valid; qed"),
),
None,
None,
None,
Default::default(),
)
)
.build()
}
/// Helper function to generate a crypto pair from seed.
@@ -244,8 +246,7 @@ pub fn authority_keys_from_seed(
)
}
/// Helper function to create RuntimeGenesisConfig for testing.
pub fn testnet_genesis(
fn configure_accounts(
initial_authorities: Vec<(
AccountId,
AccountId,
@@ -256,9 +257,14 @@ pub fn testnet_genesis(
MixnetId,
)>,
initial_nominators: Vec<AccountId>,
root_key: AccountId,
endowed_accounts: Option<Vec<AccountId>>,
) -> RuntimeGenesisConfig {
stash: Balance,
) -> (
Vec<(AccountId, AccountId, GrandpaId, BabeId, ImOnlineId, AuthorityDiscoveryId, MixnetId)>,
Vec<AccountId>,
usize,
Vec<(AccountId, AccountId, Balance, StakerStatus<AccountId>)>,
) {
let mut endowed_accounts: Vec<AccountId> = endowed_accounts.unwrap_or_else(|| {
vec![
get_account_id_from_seed::<sr25519::Public>("Alice"),
@@ -290,7 +296,7 @@ pub fn testnet_genesis(
let mut rng = rand::thread_rng();
let stakers = initial_authorities
.iter()
.map(|x| (x.0.clone(), x.0.clone(), STASH, StakerStatus::Validator))
.map(|x| (x.0.clone(), x.0.clone(), stash, StakerStatus::Validator))
.chain(initial_nominators.iter().map(|x| {
use rand::{seq::SliceRandom, Rng};
let limit = (MaxNominations::get() as usize).min(initial_authorities.len());
@@ -301,23 +307,39 @@ pub fn testnet_genesis(
.into_iter()
.map(|choice| choice.0.clone())
.collect::<Vec<_>>();
(x.clone(), x.clone(), STASH, StakerStatus::Nominator(nominations))
(x.clone(), x.clone(), stash, StakerStatus::Nominator(nominations))
}))
.collect::<Vec<_>>();
let num_endowed_accounts = endowed_accounts.len();
const ENDOWMENT: Balance = 10_000_000 * DOLLARS;
const STASH: Balance = ENDOWMENT / 1000;
(initial_authorities, endowed_accounts, num_endowed_accounts, stakers)
}
RuntimeGenesisConfig {
system: SystemConfig { code: wasm_binary_unwrap().to_vec(), ..Default::default() },
balances: BalancesConfig {
balances: endowed_accounts.iter().cloned().map(|x| (x, ENDOWMENT)).collect(),
/// Helper function to create RuntimeGenesisConfig json patch for testing.
pub fn testnet_genesis(
initial_authorities: Vec<(
AccountId,
AccountId,
GrandpaId,
BabeId,
ImOnlineId,
AuthorityDiscoveryId,
MixnetId,
)>,
initial_nominators: Vec<AccountId>,
root_key: AccountId,
endowed_accounts: Option<Vec<AccountId>>,
) -> serde_json::Value {
let (initial_authorities, endowed_accounts, num_endowed_accounts, stakers) =
configure_accounts(initial_authorities, initial_nominators, endowed_accounts, STASH);
serde_json::json!({
"balances": {
"balances": endowed_accounts.iter().cloned().map(|x| (x, ENDOWMENT)).collect::<Vec<_>>(),
},
indices: IndicesConfig { indices: vec![] },
session: SessionConfig {
keys: initial_authorities
"session": {
"keys": initial_authorities
.iter()
.map(|x| {
(
@@ -334,67 +356,45 @@ pub fn testnet_genesis(
})
.collect::<Vec<_>>(),
},
staking: StakingConfig {
validator_count: initial_authorities.len() as u32,
minimum_validator_count: initial_authorities.len() as u32,
invulnerables: initial_authorities.iter().map(|x| x.0.clone()).collect(),
slash_reward_fraction: Perbill::from_percent(10),
stakers,
..Default::default()
"staking": {
"validatorCount": initial_authorities.len() as u32,
"minimumValidatorCount": initial_authorities.len() as u32,
"invulnerables": initial_authorities.iter().map(|x| x.0.clone()).collect::<Vec<_>>(),
"slashRewardFraction": Perbill::from_percent(10),
"stakers": stakers.clone(),
},
democracy: DemocracyConfig::default(),
elections: ElectionsConfig {
members: endowed_accounts
"elections": {
"members": endowed_accounts
.iter()
.take((num_endowed_accounts + 1) / 2)
.cloned()
.map(|member| (member, STASH))
.collect(),
.collect::<Vec<_>>(),
},
council: CouncilConfig::default(),
technical_committee: TechnicalCommitteeConfig {
members: endowed_accounts
"technicalCommittee": {
"members": endowed_accounts
.iter()
.take((num_endowed_accounts + 1) / 2)
.cloned()
.collect(),
phantom: Default::default(),
.collect::<Vec<_>>(),
},
sudo: SudoConfig { key: Some(root_key) },
babe: BabeConfig {
epoch_config: Some(kitchensink_runtime::BABE_GENESIS_EPOCH_CONFIG),
..Default::default()
"sudo": { "key": Some(root_key.clone()) },
"babe": {
"epochConfig": Some(kitchensink_runtime::BABE_GENESIS_EPOCH_CONFIG),
},
im_online: ImOnlineConfig { keys: vec![] },
authority_discovery: Default::default(),
grandpa: Default::default(),
technical_membership: Default::default(),
treasury: Default::default(),
society: SocietyConfig { pot: 0 },
vesting: Default::default(),
assets: pallet_assets::GenesisConfig {
"society": { "pot": 0 },
"assets": {
// This asset is used by the NIS pallet as counterpart currency.
assets: vec![(9, get_account_id_from_seed::<sr25519::Public>("Alice"), true, 1)],
..Default::default()
"assets": vec![(9, get_account_id_from_seed::<sr25519::Public>("Alice"), true, 1)],
},
pool_assets: Default::default(),
transaction_storage: Default::default(),
transaction_payment: Default::default(),
alliance: Default::default(),
safe_mode: Default::default(),
tx_pause: Default::default(),
alliance_motion: Default::default(),
nomination_pools: NominationPoolsConfig {
min_create_bond: 10 * DOLLARS,
min_join_bond: 1 * DOLLARS,
..Default::default()
"nominationPools": {
"minCreateBond": 10 * DOLLARS,
"minJoinBond": 1 * DOLLARS,
},
glutton: Default::default(),
mixnet: Default::default(),
}
})
}
fn development_config_genesis() -> RuntimeGenesisConfig {
fn development_config_genesis_json() -> serde_json::Value {
testnet_genesis(
vec![authority_keys_from_seed("Alice")],
vec![],
@@ -405,21 +405,15 @@ fn development_config_genesis() -> RuntimeGenesisConfig {
/// Development config (single validator Alice).
pub fn development_config() -> ChainSpec {
ChainSpec::from_genesis(
"Development",
"dev",
ChainType::Development,
development_config_genesis,
vec![],
None,
None,
None,
None,
Default::default(),
)
ChainSpec::builder(wasm_binary_unwrap(), Default::default())
.with_name("Development")
.with_id("dev")
.with_chain_type(ChainType::Development)
.with_genesis_config_patch(development_config_genesis_json())
.build()
}
fn local_testnet_genesis() -> RuntimeGenesisConfig {
fn local_testnet_genesis() -> serde_json::Value {
testnet_genesis(
vec![authority_keys_from_seed("Alice"), authority_keys_from_seed("Bob")],
vec![],
@@ -430,18 +424,12 @@ fn local_testnet_genesis() -> RuntimeGenesisConfig {
/// Local testnet config (multivalidator Alice + Bob).
pub fn local_testnet_config() -> ChainSpec {
ChainSpec::from_genesis(
"Local Testnet",
"local_testnet",
ChainType::Local,
local_testnet_genesis,
vec![],
None,
None,
None,
None,
Default::default(),
)
ChainSpec::builder(wasm_binary_unwrap(), Default::default())
.with_name("Local Testnet")
.with_id("local_testnet")
.with_chain_type(ChainType::Local)
.with_genesis_config_patch(local_testnet_genesis())
.build()
}
#[cfg(test)]
@@ -451,45 +439,29 @@ pub(crate) mod tests {
use sc_service_test;
use sp_runtime::BuildStorage;
fn local_testnet_genesis_instant_single() -> RuntimeGenesisConfig {
testnet_genesis(
vec![authority_keys_from_seed("Alice")],
vec![],
get_account_id_from_seed::<sr25519::Public>("Alice"),
None,
)
}
/// Local testnet config (single validator - Alice).
pub fn integration_test_config_with_single_authority() -> ChainSpec {
ChainSpec::from_genesis(
"Integration Test",
"test",
ChainType::Development,
local_testnet_genesis_instant_single,
vec![],
None,
None,
None,
None,
Default::default(),
)
ChainSpec::builder(wasm_binary_unwrap(), Default::default())
.with_name("Integration Test")
.with_id("test")
.with_chain_type(ChainType::Development)
.with_genesis_config_patch(testnet_genesis(
vec![authority_keys_from_seed("Alice")],
vec![],
get_account_id_from_seed::<sr25519::Public>("Alice"),
None,
))
.build()
}
/// Local testnet config (multivalidator Alice + Bob).
pub fn integration_test_config_with_two_authorities() -> ChainSpec {
ChainSpec::from_genesis(
"Integration Test",
"test",
ChainType::Development,
local_testnet_genesis,
vec![],
None,
None,
None,
None,
Default::default(),
)
ChainSpec::builder(wasm_binary_unwrap(), Default::default())
.with_name("Integration Test")
.with_id("test")
.with_chain_type(ChainType::Development)
.with_genesis_config_patch(local_testnet_genesis())
.build()
}
#[test]
+4 -3
View File
@@ -14,17 +14,17 @@ targets = ["x86_64-unknown-linux-gnu"]
[dependencies]
codec = { package = "parity-scale-codec", version = "3.6.1" }
scale-info = { version = "2.10.0", features = ["derive"] }
scale-info = { version = "2.10.0", features = ["derive", "serde"] }
frame-benchmarking = { path = "../../../frame/benchmarking" }
node-primitives = { path = "../primitives" }
kitchensink-runtime = { path = "../runtime" }
sc-executor = { path = "../../../client/executor" }
sp-core = { path = "../../../primitives/core" }
sp-core = { path = "../../../primitives/core", features=["serde"] }
sp-keystore = { path = "../../../primitives/keystore" }
sp-state-machine = { path = "../../../primitives/state-machine" }
sp-tracing = { path = "../../../primitives/tracing" }
sp-trie = { path = "../../../primitives/trie" }
sp-statement-store = { path = "../../../primitives/statement-store" }
sp-statement-store = { path = "../../../primitives/statement-store", features=["serde"] }
[dev-dependencies]
criterion = "0.4.0"
@@ -47,6 +47,7 @@ sp-consensus-babe = { path = "../../../primitives/consensus/babe" }
sp-externalities = { path = "../../../primitives/externalities" }
sp-keyring = { path = "../../../primitives/keyring" }
sp-runtime = { path = "../../../primitives/runtime" }
serde_json = "1.0.85"
[features]
stress-test = []
+1 -1
View File
@@ -190,7 +190,7 @@ fn bench_execute_block(c: &mut Criterion) {
for strategy in execution_methods {
group.bench_function(format!("{:?}", strategy), |b| {
let genesis_config = node_testing::genesis::config(Some(compact_code_unwrap()));
let genesis_config = node_testing::genesis::config();
let use_native = match strategy {
ExecutionMethod::Native => true,
ExecutionMethod::Wasm(..) => false,
@@ -857,3 +857,19 @@ fn should_import_block_with_test_client() {
futures::executor::block_on(client.import(BlockOrigin::Own, block)).unwrap();
}
#[test]
fn default_config_as_json_works() {
let mut t = new_test_ext(compact_code_unwrap());
let r = executor_call(&mut t, "GenesisBuilder_create_default_config", &vec![], false)
.0
.unwrap();
let r = Vec::<u8>::decode(&mut &r[..]).unwrap();
let json = String::from_utf8(r.into()).expect("returned value is json. qed.");
let expected = include_str!("res/default_genesis_config.json").to_string();
assert_eq!(
serde_json::from_str::<serde_json::Value>(&expected).unwrap(),
serde_json::from_str::<serde_json::Value>(&json).unwrap()
);
}
+1 -1
View File
@@ -124,7 +124,7 @@ pub fn executor_call(
pub fn new_test_ext(code: &[u8]) -> TestExternalities<BlakeTwo256> {
let ext = TestExternalities::new_with_code(
code,
node_testing::genesis::config(Some(code)).build_storage().unwrap(),
node_testing::genesis::config().build_storage().unwrap(),
);
ext
}
@@ -0,0 +1,108 @@
{
"system": {},
"babe": {
"authorities": [],
"epochConfig": null
},
"indices": {
"indices": []
},
"balances": {
"balances": []
},
"transactionPayment": {
"multiplier": "1000000000000000000"
},
"staking": {
"validatorCount": 0,
"minimumValidatorCount": 0,
"invulnerables": [],
"forceEra": "NotForcing",
"slashRewardFraction": 0,
"canceledPayout": 0,
"stakers": [],
"minNominatorBond": 0,
"minValidatorBond": 0,
"maxValidatorCount": null,
"maxNominatorCount": null
},
"session": {
"keys": []
},
"democracy": {},
"council": {
"members": []
},
"technicalCommittee": {
"members": []
},
"elections": {
"members": []
},
"technicalMembership": {
"members": []
},
"grandpa": {
"authorities": []
},
"treasury": {},
"sudo": {
"key": null
},
"imOnline": {
"keys": []
},
"authorityDiscovery": {
"keys": []
},
"society": {
"pot": 0
},
"vesting": {
"vesting": []
},
"glutton": {
"compute": "0",
"storage": "0",
"trashDataCount": 0
},
"assets": {
"assets": [],
"metadata": [],
"accounts": []
},
"poolAssets": {
"assets": [],
"metadata": [],
"accounts": []
},
"transactionStorage": {
"byteFee": 10,
"entryFee": 1000,
"storagePeriod": 100800
},
"allianceMotion": {
"members": []
},
"alliance": {
"fellows": [],
"allies": []
},
"mixnet": {
"mixnodes": []
},
"nominationPools": {
"minJoinBond": 0,
"minCreateBond": 0,
"maxPools": 16,
"maxMembersPerPool": 32,
"maxMembers": 512,
"globalMaxCommission": null
},
"txPause": {
"paused": []
},
"safeMode": {
"enteredUntil": null
}
}
+11 -9
View File
@@ -20,33 +20,34 @@ codec = { package = "parity-scale-codec", version = "3.6.1", default-features =
"derive",
"max-encoded-len",
] }
scale-info = { version = "2.10.0", default-features = false, features = ["derive"] }
scale-info = { version = "2.10.0", default-features = false, features = ["derive", "serde"] }
static_assertions = "1.1.0"
log = { version = "0.4.17", default-features = false }
serde_json = { version = "1.0.85", default-features = false, features = ["alloc", "arbitrary_precision"] }
# pallet-asset-conversion: turn on "num-traits" feature
primitive-types = { version = "0.12.0", default-features = false, features = ["codec", "scale-info", "num-traits"] }
# primitives
sp-authority-discovery = { path = "../../../primitives/authority-discovery", default-features = false}
sp-consensus-babe = { path = "../../../primitives/consensus/babe", default-features = false}
sp-consensus-grandpa = { path = "../../../primitives/consensus/grandpa", default-features = false}
sp-authority-discovery = { path = "../../../primitives/authority-discovery", default-features = false, features=["serde"] }
sp-consensus-babe = { path = "../../../primitives/consensus/babe", default-features = false, features=["serde"] }
sp-consensus-grandpa = { path = "../../../primitives/consensus/grandpa", default-features = false, features=["serde"] }
sp-block-builder = { path = "../../../primitives/block-builder", default-features = false}
sp-genesis-builder = { default-features = false, path = "../../../primitives/genesis-builder" }
sp-inherents = { path = "../../../primitives/inherents", default-features = false}
node-primitives = { path = "../primitives", default-features = false}
sp-mixnet = { path = "../../../primitives/mixnet", default-features = false }
sp-offchain = { path = "../../../primitives/offchain", default-features = false}
sp-core = { path = "../../../primitives/core", default-features = false}
sp-core = { path = "../../../primitives/core", default-features = false, features=["serde"] }
sp-std = { path = "../../../primitives/std", default-features = false}
sp-api = { path = "../../../primitives/api", default-features = false}
sp-runtime = { path = "../../../primitives/runtime", default-features = false}
sp-staking = { path = "../../../primitives/staking", default-features = false}
sp-runtime = { path = "../../../primitives/runtime", default-features = false, features=["serde"] }
sp-staking = { path = "../../../primitives/staking", default-features = false, features=["serde"] }
sp-storage = { path = "../../../primitives/storage", default-features = false}
sp-session = { path = "../../../primitives/session", default-features = false}
sp-transaction-pool = { path = "../../../primitives/transaction-pool", default-features = false}
sp-statement-store = { path = "../../../primitives/statement-store", default-features = false}
sp-version = { path = "../../../primitives/version", default-features = false}
sp-statement-store = { path = "../../../primitives/statement-store", default-features = false, features=["serde"] }
sp-version = { path = "../../../primitives/version", default-features = false, features=["serde"] }
sp-io = { path = "../../../primitives/io", default-features = false}
# frame dependencies
@@ -230,6 +231,7 @@ std = [
"pallet-whitelist/std",
"primitive-types/std",
"scale-info/std",
"serde_json/std",
"sp-api/std",
"sp-authority-discovery/std",
"sp-block-builder/std",
+15 -7
View File
@@ -398,7 +398,7 @@ impl BenchDb {
let client_config = sc_service::ClientConfig::default();
let genesis_block_builder = sc_service::GenesisBlockBuilder::new(
&keyring.generate_genesis(),
keyring.as_storage_builder(),
!client_config.no_genesis,
backend.clone(),
executor.clone(),
@@ -590,12 +590,20 @@ impl BenchKeyring {
}
}
/// Generate genesis with accounts from this keyring endowed with some balance.
pub fn generate_genesis(&self) -> kitchensink_runtime::RuntimeGenesisConfig {
crate::genesis::config_endowed(
Some(kitchensink_runtime::wasm_binary_unwrap()),
self.collect_account_ids(),
)
/// Generate genesis with accounts from this keyring endowed with some balance and
/// kitchensink_runtime code blob.
pub fn as_storage_builder(&self) -> &dyn sp_runtime::BuildStorage {
self
}
}
impl sp_runtime::BuildStorage for BenchKeyring {
fn assimilate_storage(&self, storage: &mut sp_core::storage::Storage) -> Result<(), String> {
storage.top.insert(
sp_core::storage::well_known_keys::CODE.to_vec(),
kitchensink_runtime::wasm_binary_unwrap().into(),
);
crate::genesis::config_endowed(self.collect_account_ids()).assimilate_storage(storage)
}
}
+6 -1
View File
@@ -42,7 +42,12 @@ pub struct GenesisParameters;
impl substrate_test_client::GenesisInit for GenesisParameters {
fn genesis_storage(&self) -> Storage {
crate::genesis::config(None).build_storage().unwrap()
let mut storage = crate::genesis::config().build_storage().unwrap();
storage.top.insert(
sp_core::storage::well_known_keys::CODE.to_vec(),
kitchensink_runtime::wasm_binary_unwrap().into(),
);
storage
}
}
+7 -11
View File
@@ -20,22 +20,21 @@
use crate::keyring::*;
use kitchensink_runtime::{
constants::currency::*, wasm_binary_unwrap, AccountId, AssetsConfig, BabeConfig,
BalancesConfig, GluttonConfig, GrandpaConfig, IndicesConfig, RuntimeGenesisConfig,
SessionConfig, SocietyConfig, StakerStatus, StakingConfig, SystemConfig,
BABE_GENESIS_EPOCH_CONFIG,
constants::currency::*, AccountId, AssetsConfig, BabeConfig, BalancesConfig, GluttonConfig,
GrandpaConfig, IndicesConfig, RuntimeGenesisConfig, SessionConfig, SocietyConfig, StakerStatus,
StakingConfig, BABE_GENESIS_EPOCH_CONFIG,
};
use sp_keyring::{Ed25519Keyring, Sr25519Keyring};
use sp_runtime::Perbill;
/// Create genesis runtime configuration for tests.
pub fn config(code: Option<&[u8]>) -> RuntimeGenesisConfig {
config_endowed(code, Default::default())
pub fn config() -> RuntimeGenesisConfig {
config_endowed(Default::default())
}
/// Create genesis runtime configuration for tests with some extra
/// endowed accounts.
pub fn config_endowed(code: Option<&[u8]>, extra_endowed: Vec<AccountId>) -> RuntimeGenesisConfig {
pub fn config_endowed(extra_endowed: Vec<AccountId>) -> RuntimeGenesisConfig {
let mut endowed = vec![
(alice(), 111 * DOLLARS),
(bob(), 100 * DOLLARS),
@@ -48,10 +47,7 @@ pub fn config_endowed(code: Option<&[u8]>, extra_endowed: Vec<AccountId>) -> Run
endowed.extend(extra_endowed.into_iter().map(|endowed| (endowed, 100 * DOLLARS)));
RuntimeGenesisConfig {
system: SystemConfig {
code: code.map(|x| x.to_vec()).unwrap_or_else(|| wasm_binary_unwrap().to_vec()),
..Default::default()
},
system: Default::default(),
indices: IndicesConfig { indices: vec![] },
balances: BalancesConfig { balances: endowed },
session: SessionConfig {
@@ -23,8 +23,12 @@ crate-type = ["rlib"]
ansi_term = "0.12.1"
clap = { version = "4.4.6", features = ["derive"] }
rand = "0.8"
kitchensink-runtime = { version = "3.0.0-dev", path = "../../node/runtime" }
log = "0.4.17"
node-cli = { package = "staging-node-cli", path = "../../node/cli" }
sc-chain-spec = { path = "../../../client/chain-spec" }
sc-keystore = { path = "../../../client/keystore" }
serde_json = "1.0.100"
sp-core = { path = "../../../primitives/core" }
sp-keystore = { path = "../../../primitives/keystore" }
sp-tracing = { version = "10.0.0", path = "../../../primitives/tracing" }
@@ -17,28 +17,40 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use chain_spec_builder::{
generate_authority_keys_and_store, generate_chain_spec, print_seeds, ChainSpecBuilder,
generate_authority_keys_and_store, generate_chain_spec, generate_chain_spec_for_runtime,
print_seeds, ChainSpecBuilder, ChainSpecBuilderCmd, EditCmd, GenerateCmd, NewCmd, VerifyCmd,
};
use clap::Parser;
use node_cli::chain_spec;
use rand::{distributions::Alphanumeric, rngs::OsRng, Rng};
use sc_chain_spec::{update_code_in_json_chain_spec, GenericChainSpec};
use sp_core::{crypto::Ss58Codec, sr25519};
use staging_chain_spec_builder as chain_spec_builder;
use std::fs;
fn main() -> Result<(), String> {
#[cfg(build_type = "debug")]
println!(
"The chain spec builder builds a chain specification that includes a Substrate runtime \
compiled as WASM. To ensure proper functioning of the included runtime compile (or run) \
the chain spec builder binary in `--release` mode.\n",
);
sp_tracing::try_init_simple();
let builder = ChainSpecBuilder::parse();
let chain_spec_path = builder.chain_spec_path().to_path_buf();
#[cfg(build_type = "debug")]
if matches!(builder.command, ChainSpecBuilderCmd::Generate(_) | ChainSpecBuilderCmd::New(_)) {
println!(
"The chain spec builder builds a chain specification that includes a Substrate runtime \
compiled as WASM. To ensure proper functioning of the included runtime compile (or run) \
the chain spec builder binary in `--release` mode.\n",
);
}
let (authority_seeds, nominator_accounts, endowed_accounts, sudo_account) = match builder {
ChainSpecBuilder::Generate { authorities, nominators, endowed, keystore_path, .. } => {
let chain_spec_path = builder.chain_spec_path.to_path_buf();
let mut write_chain_spec = true;
let chain_spec_json = match builder.command {
ChainSpecBuilderCmd::Generate(GenerateCmd {
authorities,
nominators,
endowed,
keystore_path,
}) => {
let authorities = authorities.max(1);
let rand_str = || -> String {
OsRng.sample_iter(&Alphanumeric).take(32).map(char::from).collect()
@@ -72,19 +84,58 @@ fn main() -> Result<(), String> {
let sudo_account =
chain_spec::get_account_id_from_seed::<sr25519::Public>(&sudo_seed).to_ss58check();
(authority_seeds, nominator_accounts, endowed_accounts, sudo_account)
generate_chain_spec(authority_seeds, nominator_accounts, endowed_accounts, sudo_account)
},
ChainSpecBuilder::New {
ChainSpecBuilderCmd::New(NewCmd {
authority_seeds,
nominator_accounts,
endowed_accounts,
sudo_account,
..
} => (authority_seeds, nominator_accounts, endowed_accounts, sudo_account),
};
}) =>
generate_chain_spec(authority_seeds, nominator_accounts, endowed_accounts, sudo_account),
ChainSpecBuilderCmd::Runtime(cmd) => generate_chain_spec_for_runtime(&cmd),
ChainSpecBuilderCmd::Edit(EditCmd {
ref input_chain_spec,
ref runtime_wasm_path,
convert_to_raw,
}) => {
let chain_spec = GenericChainSpec::<()>::from_json_file(input_chain_spec.clone())?;
let json =
generate_chain_spec(authority_seeds, nominator_accounts, endowed_accounts, sudo_account)?;
let mut chain_spec_json =
serde_json::from_str::<serde_json::Value>(&chain_spec.as_json(convert_to_raw)?)
.map_err(|e| format!("Conversion to json failed: {e}"))?;
if let Some(path) = runtime_wasm_path {
update_code_in_json_chain_spec(
&mut chain_spec_json,
&fs::read(path.as_path())
.map_err(|e| format!("Wasm blob file could not be read: {e}"))?[..],
);
}
fs::write(chain_spec_path, json).map_err(|err| err.to_string())
serde_json::to_string_pretty(&chain_spec_json)
.map_err(|e| format!("to pretty failed: {e}"))
},
ChainSpecBuilderCmd::Verify(VerifyCmd { ref input_chain_spec, ref runtime_wasm_path }) => {
write_chain_spec = false;
let chain_spec = GenericChainSpec::<()>::from_json_file(input_chain_spec.clone())?;
let mut chain_spec_json =
serde_json::from_str::<serde_json::Value>(&chain_spec.as_json(true)?)
.map_err(|e| format!("Conversion to json failed: {e}"))?;
if let Some(path) = runtime_wasm_path {
update_code_in_json_chain_spec(
&mut chain_spec_json,
&fs::read(path.as_path())
.map_err(|e| format!("Wasm blob file could not be read: {e}"))?[..],
);
};
serde_json::to_string_pretty(&chain_spec_json)
.map_err(|e| format!("to pretty failed: {e}"))
},
}?;
if write_chain_spec {
fs::write(chain_spec_path, chain_spec_json).map_err(|err| err.to_string())
} else {
Ok(())
}
}
+215 -94
View File
@@ -29,96 +29,169 @@
//! [`sc-chain-spec`]: ../sc_chain_spec/index.html
//! [`node-cli`]: ../node_cli/index.html
use std::path::{Path, PathBuf};
use std::{
fs,
path::{Path, PathBuf},
};
use ansi_term::Style;
use clap::Parser;
use clap::{Parser, Subcommand};
use sc_chain_spec::GenesisConfigBuilderRuntimeCaller;
use node_cli::chain_spec::{self, AccountId};
use sc_keystore::LocalKeystore;
use serde_json::Value;
use sp_core::crypto::{ByteArray, Ss58Codec};
use sp_keystore::KeystorePtr;
/// A utility to easily create a testnet chain spec definition with a given set
/// of authorities and endowed accounts and/or generate random accounts.
#[derive(Parser)]
#[derive(Debug, Parser)]
#[command(rename_all = "kebab-case")]
pub enum ChainSpecBuilder {
/// Create a new chain spec with the given authorities, endowed and sudo
/// accounts.
New {
/// Authority key seed.
#[arg(long, short, required = true)]
authority_seeds: Vec<String>,
/// Active nominators (SS58 format), each backing a random subset of the aforementioned
/// authorities.
#[arg(long, short, default_value = "0")]
nominator_accounts: Vec<String>,
/// Endowed account address (SS58 format).
#[arg(long, short)]
endowed_accounts: Vec<String>,
/// Sudo account address (SS58 format).
#[arg(long, short)]
sudo_account: String,
/// The path where the chain spec should be saved.
#[arg(long, short, default_value = "./chain_spec.json")]
chain_spec_path: PathBuf,
},
/// Create a new chain spec with the given number of authorities and endowed
/// accounts. Random keys will be generated as required.
Generate {
/// The number of authorities.
#[arg(long, short)]
authorities: usize,
/// The number of nominators backing the aforementioned authorities.
///
/// Will nominate a random subset of `authorities`.
#[arg(long, short, default_value_t = 0)]
nominators: usize,
/// The number of endowed accounts.
#[arg(long, short, default_value_t = 0)]
endowed: usize,
/// The path where the chain spec should be saved.
#[arg(long, short, default_value = "./chain_spec.json")]
chain_spec_path: PathBuf,
/// Path to use when saving generated keystores for each authority.
///
/// At this path, a new folder will be created for each authority's
/// keystore named `auth-$i` where `i` is the authority index, i.e.
/// `auth-0`, `auth-1`, etc.
#[arg(long, short)]
keystore_path: Option<PathBuf>,
},
pub struct ChainSpecBuilder {
#[command(subcommand)]
pub command: ChainSpecBuilderCmd,
/// The path where the chain spec should be saved.
#[arg(long, short, default_value = "./chain_spec.json")]
pub chain_spec_path: PathBuf,
}
impl ChainSpecBuilder {
/// Returns the path where the chain spec should be saved.
pub fn chain_spec_path(&self) -> &Path {
match self {
ChainSpecBuilder::New { chain_spec_path, .. } => chain_spec_path.as_path(),
ChainSpecBuilder::Generate { chain_spec_path, .. } => chain_spec_path.as_path(),
}
}
#[derive(Debug, Subcommand)]
#[command(rename_all = "kebab-case")]
pub enum ChainSpecBuilderCmd {
New(NewCmd),
Generate(GenerateCmd),
Runtime(RuntimeCmd),
Edit(EditCmd),
Verify(VerifyCmd),
}
fn genesis_constructor(
authority_seeds: &[String],
nominator_accounts: &[AccountId],
endowed_accounts: &[AccountId],
sudo_account: &AccountId,
) -> chain_spec::RuntimeGenesisConfig {
let authorities = authority_seeds
.iter()
.map(AsRef::as_ref)
.map(chain_spec::authority_keys_from_seed)
.collect::<Vec<_>>();
/// Create a new chain spec with the given authorities, endowed and sudo
/// accounts. Only works for kitchen-sink runtime
#[derive(Parser, Debug)]
#[command(rename_all = "kebab-case")]
pub struct NewCmd {
/// Authority key seed.
#[arg(long, short, required = true)]
pub authority_seeds: Vec<String>,
/// Active nominators (SS58 format), each backing a random subset of the aforementioned
/// authorities.
#[arg(long, short, default_value = "0")]
pub nominator_accounts: Vec<String>,
/// Endowed account address (SS58 format).
#[arg(long, short)]
pub endowed_accounts: Vec<String>,
/// Sudo account address (SS58 format).
#[arg(long, short)]
pub sudo_account: String,
}
chain_spec::testnet_genesis(
authorities,
nominator_accounts.to_vec(),
sudo_account.clone(),
Some(endowed_accounts.to_vec()),
)
/// Create a new chain spec with the given number of authorities and endowed
/// accounts. Random keys will be generated as required.
#[derive(Parser, Debug)]
pub struct GenerateCmd {
/// The number of authorities.
#[arg(long, short)]
pub authorities: usize,
/// The number of nominators backing the aforementioned authorities.
///
/// Will nominate a random subset of `authorities`.
#[arg(long, short, default_value_t = 0)]
pub nominators: usize,
/// The number of endowed accounts.
#[arg(long, short, default_value_t = 0)]
pub endowed: usize,
/// Path to use when saving generated keystores for each authority.
///
/// At this path, a new folder will be created for each authority's
/// keystore named `auth-$i` where `i` is the authority index, i.e.
/// `auth-0`, `auth-1`, etc.
#[arg(long, short)]
pub keystore_path: Option<PathBuf>,
}
/// Create a new chain spec by interacting with the provided runtime wasm blob.
#[derive(Parser, Debug)]
pub struct RuntimeCmd {
/// The name of chain
#[arg(long, short = 'n', default_value = "Custom")]
chain_name: String,
/// The chain id
#[arg(long, short = 'i', default_value = "custom")]
chain_id: String,
/// The path to runtime wasm blob
#[arg(long, short)]
runtime_wasm_path: PathBuf,
/// Export chainspec as raw storage
#[arg(long, short = 's')]
raw_storage: bool,
/// Verify the genesis config. This silently generates the raw storage from genesis config. Any
/// errors will be reported.
#[arg(long, short = 'v')]
verify: bool,
#[command(subcommand)]
action: GenesisBuildAction,
}
#[derive(Subcommand, Debug, Clone)]
enum GenesisBuildAction {
Patch(PatchCmd),
Full(FullCmd),
Default(DefaultCmd),
}
/// Patches the runtime's default genesis config with provided patch.
#[derive(Parser, Debug, Clone)]
struct PatchCmd {
/// The path to the runtime genesis config patch.
#[arg(long, short)]
patch_path: PathBuf,
}
/// Build the genesis config for runtime using provided json file. No defaults will be used.
#[derive(Parser, Debug, Clone)]
struct FullCmd {
/// The path to the full runtime genesis config json file.
#[arg(long, short)]
config_path: PathBuf,
}
/// Gets the default genesis config for the runtime and uses it in ChainSpec. Please note that
/// default genesis config may not be valid. For some runtimes initial values should be added there
/// (e.g. session keys, babe epoch).
#[derive(Parser, Debug, Clone)]
struct DefaultCmd {
/// If provided stores the default genesis config json file at given path (in addition to
/// chain-spec).
#[arg(long, short)]
default_config_path: Option<PathBuf>,
}
/// Edits provided input chain spec. Input can be converted into raw storage chain-spec. The code
/// can be updated with the runtime provided in the command line.
#[derive(Parser, Debug, Clone)]
pub struct EditCmd {
/// Chain spec to be edited
#[arg(long, short)]
pub input_chain_spec: PathBuf,
/// The path to new runtime wasm blob to be stored into chain-spec
#[arg(long, short = 'r')]
pub runtime_wasm_path: Option<PathBuf>,
/// Convert genesis spec to raw format
#[arg(long, short = 's')]
pub convert_to_raw: bool,
}
/// Verifies provided input chain spec. If the runtime is provided verification is performed against
/// new runtime.
#[derive(Parser, Debug, Clone)]
pub struct VerifyCmd {
/// Chain spec to be edited
#[arg(long, short)]
pub input_chain_spec: PathBuf,
/// The path to new runtime wasm blob to be stored into chain-spec
#[arg(long, short = 'r')]
pub runtime_wasm_path: Option<PathBuf>,
}
/// Generate the chain spec using the given seeds and accounts.
@@ -145,27 +218,24 @@ pub fn generate_chain_spec(
let sudo_account = parse_account(sudo_account)?;
let chain_spec = chain_spec::ChainSpec::from_genesis(
"Custom",
"custom",
sc_chain_spec::ChainType::Live,
move || {
genesis_constructor(
&authority_seeds,
&nominator_accounts,
&endowed_accounts,
&sudo_account,
)
},
vec![],
None,
None,
None,
None,
Default::default(),
);
let authorities = authority_seeds
.iter()
.map(AsRef::as_ref)
.map(chain_spec::authority_keys_from_seed)
.collect::<Vec<_>>();
chain_spec.as_json(false)
chain_spec::ChainSpec::builder(kitchensink_runtime::wasm_binary_unwrap(), Default::default())
.with_name("Custom")
.with_id("custom")
.with_chain_type(sc_chain_spec::ChainType::Live)
.with_genesis_config_patch(chain_spec::testnet_genesis(
authorities,
nominator_accounts,
sudo_account,
Some(endowed_accounts),
))
.build()
.as_json(false)
}
/// Generate the authority keys and store them in the given `keystore_path`.
@@ -241,3 +311,54 @@ pub fn print_seeds(
println!("{}", header.paint("Sudo seed"));
println!("//{}", sudo_seed);
}
/// Processes `RuntimeCmd` and returns JSON version of `ChainSpec`
pub fn generate_chain_spec_for_runtime(cmd: &RuntimeCmd) -> Result<String, String> {
let code = fs::read(cmd.runtime_wasm_path.as_path())
.map_err(|e| format!("wasm blob shall be readable {e}"))?;
let builder = chain_spec::ChainSpec::builder(&code[..], Default::default())
.with_name(&cmd.chain_name[..])
.with_id(&cmd.chain_id[..])
.with_chain_type(sc_chain_spec::ChainType::Live);
let builder = match cmd.action {
GenesisBuildAction::Patch(PatchCmd { ref patch_path }) => {
let patch = fs::read(patch_path.as_path())
.map_err(|e| format!("patch file {patch_path:?} shall be readable: {e}"))?;
builder.with_genesis_config_patch(serde_json::from_slice::<Value>(&patch[..]).map_err(
|e| format!("patch file {patch_path:?} shall contain a valid json: {e}"),
)?)
},
GenesisBuildAction::Full(FullCmd { ref config_path }) => {
let config = fs::read(config_path.as_path())
.map_err(|e| format!("config file {config_path:?} shall be readable: {e}"))?;
builder.with_genesis_config(serde_json::from_slice::<Value>(&config[..]).map_err(
|e| format!("config file {config_path:?} shall contain a valid json: {e}"),
)?)
},
GenesisBuildAction::Default(DefaultCmd { ref default_config_path }) => {
let caller = GenesisConfigBuilderRuntimeCaller::new(&code[..]);
let default_config = caller
.get_default_config()
.map_err(|e| format!("getting default config from runtime should work: {e}"))?;
default_config_path.clone().map(|path| {
fs::write(path.as_path(), serde_json::to_string_pretty(&default_config).unwrap())
.map_err(|err| err.to_string())
});
builder.with_genesis_config(default_config)
},
};
let chain_spec = builder.build();
match (cmd.verify, cmd.raw_storage) {
(_, true) => chain_spec.as_json(true),
(true, false) => {
chain_spec.as_json(true)?;
println!("Genesis config verification: OK");
chain_spec.as_json(false)
},
(false, false) => chain_spec.as_json(false),
}
}