Skip to content

Commit

Permalink
chain-spec: getting ready for native-runtime-free world (paritytech…
Browse files Browse the repository at this point in the history
…#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 <[email protected]>
Co-authored-by: Davide Galassi <[email protected]>
Co-authored-by: Francisco Aguirre <[email protected]>
Co-authored-by: Kevin Krone <[email protected]>
Co-authored-by: Bastian Köcher <[email protected]>
  • Loading branch information
6 people authored Nov 5, 2023
1 parent 7a1dd0d commit a7432c8
Show file tree
Hide file tree
Showing 47 changed files with 2,530 additions and 736 deletions.
1 change: 1 addition & 0 deletions substrate/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ subkey = { path = "bin/utils/subkey" }
chain-spec-builder = { package = "staging-chain-spec-builder", path = "bin/utils/chain-spec-builder" }

sc-service = { path = "client/service" }
sc-chain-spec = { path = "client/chain-spec" }
sc-cli = { path = "client/cli" }
sc-consensus-aura = { path = "client/consensus/aura" }
sc-consensus-babe = { path = "client/consensus/babe" }
Expand Down
1 change: 1 addition & 0 deletions substrate/bin/minimal/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
41 changes: 15 additions & 26 deletions substrate/bin/minimal/node/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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 changes: 4 additions & 0 deletions substrate/bin/minimal/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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 }
Expand Down
11 changes: 11 additions & 0 deletions substrate/bin/minimal/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions substrate/bin/node-template/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
161 changes: 60 additions & 101 deletions substrate/bin/node-template/node/src/chain_spec.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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 changes: 8 additions & 6 deletions substrate/bin/node-template/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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
Expand Down Expand Up @@ -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",
Expand Down
1 change: 0 additions & 1 deletion substrate/bin/node/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading

0 comments on commit a7432c8

Please sign in to comment.