Skip to content

Commit a7432c8

Browse files
michalkucharczykpepovioladavxyfranciscoaguirrewentelteefje
authored
chain-spec: getting ready for native-runtime-free world (paritytech#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]>
1 parent 7a1dd0d commit a7432c8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+2530
-736
lines changed

substrate/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ subkey = { path = "bin/utils/subkey" }
1717
chain-spec-builder = { package = "staging-chain-spec-builder", path = "bin/utils/chain-spec-builder" }
1818

1919
sc-service = { path = "client/service" }
20+
sc-chain-spec = { path = "client/chain-spec" }
2021
sc-cli = { path = "client/cli" }
2122
sc-consensus-aura = { path = "client/consensus/aura" }
2223
sc-consensus-babe = { path = "client/consensus/babe" }

substrate/bin/minimal/node/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ clap = { version = "4.0.9", features = ["derive"] }
2121
futures = { version = "0.3.21", features = ["thread-pool"] }
2222
futures-timer = "3.0.1"
2323
jsonrpsee = { version = "0.16.2", features = ["server"] }
24+
serde_json = "1.0.107"
2425

2526
sc-cli = { path = "../../../client/cli" }
2627
sc-executor = { path = "../../../client/executor" }

substrate/bin/minimal/node/src/chain_spec.rs

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515
// See the License for the specific language governing permissions and
1616
// limitations under the License.
1717

18-
use runtime::{BalancesConfig, RuntimeGenesisConfig, SudoConfig, SystemConfig, WASM_BINARY};
18+
use runtime::{BalancesConfig, SudoConfig, WASM_BINARY};
1919
use sc_service::{ChainType, Properties};
20+
use serde_json::{json, Value};
2021
use sp_keyring::AccountKeyring;
2122

2223
/// This is a specialization of the general Substrate ChainSpec type.
23-
pub type ChainSpec = sc_service::GenericChainSpec<RuntimeGenesisConfig>;
24+
pub type ChainSpec = sc_service::GenericChainSpec<()>;
2425

2526
fn props() -> Properties {
2627
let mut properties = Properties::new();
@@ -30,37 +31,25 @@ fn props() -> Properties {
3031
}
3132

3233
pub fn development_config() -> Result<ChainSpec, String> {
33-
let wasm_binary = WASM_BINARY.ok_or_else(|| "Development wasm not available".to_string())?;
34-
Ok(ChainSpec::from_genesis(
35-
"Development",
36-
"dev",
37-
ChainType::Development,
38-
move || testnet_genesis(wasm_binary),
39-
vec![],
40-
None,
41-
None,
42-
None,
43-
Some(props()),
44-
None,
45-
))
34+
Ok(ChainSpec::builder(WASM_BINARY.expect("Development wasm not available"), Default::default())
35+
.with_name("Development")
36+
.with_id("dev")
37+
.with_chain_type(ChainType::Development)
38+
.with_genesis_config_patch(testnet_genesis())
39+
.with_properties(props())
40+
.build())
4641
}
4742

4843
/// Configure initial storage state for FRAME pallets.
49-
fn testnet_genesis(wasm_binary: &[u8]) -> RuntimeGenesisConfig {
44+
fn testnet_genesis() -> Value {
5045
use frame::traits::Get;
5146
use runtime::interface::{Balance, MinimumBalance};
5247
let endowment = <MinimumBalance as Get<Balance>>::get().max(1) * 1000;
5348
let balances = AccountKeyring::iter()
5449
.map(|a| (a.to_account_id(), endowment))
5550
.collect::<Vec<_>>();
56-
RuntimeGenesisConfig {
57-
system: SystemConfig {
58-
// Add Wasm runtime to storage.
59-
code: wasm_binary.to_vec(),
60-
_config: Default::default(),
61-
},
62-
balances: BalancesConfig { balances },
63-
sudo: SudoConfig { key: Some(AccountKeyring::Alice.to_account_id()) },
64-
..Default::default()
65-
}
51+
json!({
52+
"balances": BalancesConfig { balances },
53+
"sudo": SudoConfig { key: Some(AccountKeyring::Alice.to_account_id()) },
54+
})
6655
}

substrate/bin/minimal/runtime/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ scale-info = { version = "2.6.0", default-features = false }
99

1010
# this is a frame-based runtime, thus importing `frame` with runtime feature enabled.
1111
frame = { path = "../../../frame", default-features = false, features = ["runtime", "experimental"] }
12+
frame-support = { path = "../../../frame/support", default-features = false}
1213

1314
# pallets that we want to use
1415
pallet-balances = { path = "../../../frame/balances", default-features = false }
@@ -17,6 +18,9 @@ pallet-timestamp = { path = "../../../frame/timestamp", default-features = false
1718
pallet-transaction-payment = { path = "../../../frame/transaction-payment", default-features = false }
1819
pallet-transaction-payment-rpc-runtime-api = { path = "../../../frame/transaction-payment/rpc/runtime-api", default-features = false }
1920

21+
# genesis builder that allows us to interacto with runtime genesis config
22+
sp-genesis-builder = { path = "../../../primitives/genesis-builder", default-features = false}
23+
2024

2125
[build-dependencies]
2226
substrate-wasm-builder = { path = "../../../utils/wasm-builder", optional = true }

substrate/bin/minimal/runtime/src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ use frame::{
3131
prelude::*,
3232
},
3333
};
34+
use frame_support::genesis_builder_helper::{build_config, create_default_config};
3435

3536
#[runtime_version]
3637
pub const VERSION: RuntimeVersion = RuntimeVersion {
@@ -210,6 +211,16 @@ impl_runtime_apis! {
210211
TransactionPayment::length_to_fee(length)
211212
}
212213
}
214+
215+
impl sp_genesis_builder::GenesisBuilder<Block> for Runtime {
216+
fn create_default_config() -> Vec<u8> {
217+
create_default_config::<RuntimeGenesisConfig>()
218+
}
219+
220+
fn build_config(config: Vec<u8>) -> sp_genesis_builder::Result {
221+
build_config::<RuntimeGenesisConfig>(config)
222+
}
223+
}
213224
}
214225

215226
/// Some re-exports that the node side code needs to know. Some are useful in this context as well.

substrate/bin/node-template/node/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ name = "node-template"
1919
[dependencies]
2020
clap = { version = "4.4.6", features = ["derive"] }
2121
futures = { version = "0.3.21", features = ["thread-pool"]}
22+
serde_json = "1.0.85"
2223

2324
sc-cli = { path = "../../../client/cli" }
2425
sp-core = { path = "../../../primitives/core" }
Lines changed: 60 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
use node_template_runtime::{
2-
AccountId, AuraConfig, BalancesConfig, GrandpaConfig, RuntimeGenesisConfig, Signature,
3-
SudoConfig, SystemConfig, WASM_BINARY,
4-
};
1+
use node_template_runtime::{AccountId, RuntimeGenesisConfig, Signature, WASM_BINARY};
52
use sc_service::ChainType;
63
use sp_consensus_aura::sr25519::AuthorityId as AuraId;
74
use sp_consensus_grandpa::AuthorityId as GrandpaId;
@@ -37,122 +34,84 @@ pub fn authority_keys_from_seed(s: &str) -> (AuraId, GrandpaId) {
3734
}
3835

3936
pub fn development_config() -> Result<ChainSpec, String> {
40-
let wasm_binary = WASM_BINARY.ok_or_else(|| "Development wasm not available".to_string())?;
41-
42-
Ok(ChainSpec::from_genesis(
43-
// Name
44-
"Development",
45-
// ID
46-
"dev",
47-
ChainType::Development,
48-
move || {
49-
testnet_genesis(
50-
wasm_binary,
51-
// Initial PoA authorities
52-
vec![authority_keys_from_seed("Alice")],
53-
// Sudo account
54-
get_account_id_from_seed::<sr25519::Public>("Alice"),
55-
// Pre-funded accounts
56-
vec![
57-
get_account_id_from_seed::<sr25519::Public>("Alice"),
58-
get_account_id_from_seed::<sr25519::Public>("Bob"),
59-
get_account_id_from_seed::<sr25519::Public>("Alice//stash"),
60-
get_account_id_from_seed::<sr25519::Public>("Bob//stash"),
61-
],
62-
true,
63-
)
64-
},
65-
// Bootnodes
66-
vec![],
67-
// Telemetry
68-
None,
69-
// Protocol ID
70-
None,
71-
None,
72-
// Properties
73-
None,
74-
// Extensions
37+
Ok(ChainSpec::builder(
38+
WASM_BINARY.ok_or_else(|| "Development wasm not available".to_string())?,
7539
None,
40+
)
41+
.with_name("Development")
42+
.with_id("dev")
43+
.with_chain_type(ChainType::Development)
44+
.with_genesis_config_patch(testnet_genesis(
45+
// Initial PoA authorities
46+
vec![authority_keys_from_seed("Alice")],
47+
// Sudo account
48+
get_account_id_from_seed::<sr25519::Public>("Alice"),
49+
// Pre-funded accounts
50+
vec![
51+
get_account_id_from_seed::<sr25519::Public>("Alice"),
52+
get_account_id_from_seed::<sr25519::Public>("Bob"),
53+
get_account_id_from_seed::<sr25519::Public>("Alice//stash"),
54+
get_account_id_from_seed::<sr25519::Public>("Bob//stash"),
55+
],
56+
true,
7657
))
58+
.build())
7759
}
7860

7961
pub fn local_testnet_config() -> Result<ChainSpec, String> {
80-
let wasm_binary = WASM_BINARY.ok_or_else(|| "Development wasm not available".to_string())?;
81-
82-
Ok(ChainSpec::from_genesis(
83-
// Name
84-
"Local Testnet",
85-
// ID
86-
"local_testnet",
87-
ChainType::Local,
88-
move || {
89-
testnet_genesis(
90-
wasm_binary,
91-
// Initial PoA authorities
92-
vec![authority_keys_from_seed("Alice"), authority_keys_from_seed("Bob")],
93-
// Sudo account
94-
get_account_id_from_seed::<sr25519::Public>("Alice"),
95-
// Pre-funded accounts
96-
vec![
97-
get_account_id_from_seed::<sr25519::Public>("Alice"),
98-
get_account_id_from_seed::<sr25519::Public>("Bob"),
99-
get_account_id_from_seed::<sr25519::Public>("Charlie"),
100-
get_account_id_from_seed::<sr25519::Public>("Dave"),
101-
get_account_id_from_seed::<sr25519::Public>("Eve"),
102-
get_account_id_from_seed::<sr25519::Public>("Ferdie"),
103-
get_account_id_from_seed::<sr25519::Public>("Alice//stash"),
104-
get_account_id_from_seed::<sr25519::Public>("Bob//stash"),
105-
get_account_id_from_seed::<sr25519::Public>("Charlie//stash"),
106-
get_account_id_from_seed::<sr25519::Public>("Dave//stash"),
107-
get_account_id_from_seed::<sr25519::Public>("Eve//stash"),
108-
get_account_id_from_seed::<sr25519::Public>("Ferdie//stash"),
109-
],
110-
true,
111-
)
112-
},
113-
// Bootnodes
114-
vec![],
115-
// Telemetry
116-
None,
117-
// Protocol ID
118-
None,
119-
// Properties
120-
None,
121-
None,
122-
// Extensions
62+
Ok(ChainSpec::builder(
63+
WASM_BINARY.ok_or_else(|| "Development wasm not available".to_string())?,
12364
None,
65+
)
66+
.with_name("Local Testnet")
67+
.with_id("local_testnet")
68+
.with_chain_type(ChainType::Local)
69+
.with_genesis_config_patch(testnet_genesis(
70+
// Initial PoA authorities
71+
vec![authority_keys_from_seed("Alice"), authority_keys_from_seed("Bob")],
72+
// Sudo account
73+
get_account_id_from_seed::<sr25519::Public>("Alice"),
74+
// Pre-funded accounts
75+
vec![
76+
get_account_id_from_seed::<sr25519::Public>("Alice"),
77+
get_account_id_from_seed::<sr25519::Public>("Bob"),
78+
get_account_id_from_seed::<sr25519::Public>("Charlie"),
79+
get_account_id_from_seed::<sr25519::Public>("Dave"),
80+
get_account_id_from_seed::<sr25519::Public>("Eve"),
81+
get_account_id_from_seed::<sr25519::Public>("Ferdie"),
82+
get_account_id_from_seed::<sr25519::Public>("Alice//stash"),
83+
get_account_id_from_seed::<sr25519::Public>("Bob//stash"),
84+
get_account_id_from_seed::<sr25519::Public>("Charlie//stash"),
85+
get_account_id_from_seed::<sr25519::Public>("Dave//stash"),
86+
get_account_id_from_seed::<sr25519::Public>("Eve//stash"),
87+
get_account_id_from_seed::<sr25519::Public>("Ferdie//stash"),
88+
],
89+
true,
12490
))
91+
.build())
12592
}
12693

12794
/// Configure initial storage state for FRAME modules.
12895
fn testnet_genesis(
129-
wasm_binary: &[u8],
13096
initial_authorities: Vec<(AuraId, GrandpaId)>,
13197
root_key: AccountId,
13298
endowed_accounts: Vec<AccountId>,
13399
_enable_println: bool,
134-
) -> RuntimeGenesisConfig {
135-
RuntimeGenesisConfig {
136-
system: SystemConfig {
137-
// Add Wasm runtime to storage.
138-
code: wasm_binary.to_vec(),
139-
..Default::default()
140-
},
141-
balances: BalancesConfig {
100+
) -> serde_json::Value {
101+
serde_json::json!({
102+
"balances": {
142103
// Configure endowed accounts with initial balance of 1 << 60.
143-
balances: endowed_accounts.iter().cloned().map(|k| (k, 1 << 60)).collect(),
104+
"balances": endowed_accounts.iter().cloned().map(|k| (k, 1u64 << 60)).collect::<Vec<_>>(),
144105
},
145-
aura: AuraConfig {
146-
authorities: initial_authorities.iter().map(|x| (x.0.clone())).collect(),
106+
"aura": {
107+
"authorities": initial_authorities.iter().map(|x| (x.0.clone())).collect::<Vec<_>>(),
147108
},
148-
grandpa: GrandpaConfig {
149-
authorities: initial_authorities.iter().map(|x| (x.1.clone(), 1)).collect(),
150-
..Default::default()
109+
"grandpa": {
110+
"authorities": initial_authorities.iter().map(|x| (x.1.clone(), 1)).collect::<Vec<_>>(),
151111
},
152-
sudo: SudoConfig {
112+
"sudo": {
153113
// Assign network admin rights.
154-
key: Some(root_key),
114+
"key": Some(root_key),
155115
},
156-
transaction_payment: Default::default(),
157-
}
116+
})
158117
}

substrate/bin/node-template/runtime/Cargo.toml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ targets = ["x86_64-unknown-linux-gnu"]
1414

1515
[dependencies]
1616
codec = { package = "parity-scale-codec", version = "3.6.1", default-features = false, features = ["derive"] }
17-
scale-info = { version = "2.10.0", default-features = false, features = ["derive"] }
17+
scale-info = { version = "2.10.0", default-features = false, features = ["derive", "serde"] }
1818

1919
pallet-aura = { path = "../../../frame/aura", default-features = false}
2020
pallet-balances = { path = "../../../frame/balances", default-features = false}
@@ -28,17 +28,18 @@ pallet-transaction-payment = { path = "../../../frame/transaction-payment", defa
2828
frame-executive = { path = "../../../frame/executive", default-features = false}
2929
sp-api = { path = "../../../primitives/api", default-features = false}
3030
sp-block-builder = { path = "../../../primitives/block-builder", default-features = false}
31-
sp-consensus-aura = { path = "../../../primitives/consensus/aura", default-features = false}
32-
sp-consensus-grandpa = { path = "../../../primitives/consensus/grandpa", default-features = false}
33-
sp-core = { path = "../../../primitives/core", default-features = false}
31+
sp-consensus-aura = { path = "../../../primitives/consensus/aura", default-features = false, features = ["serde"] }
32+
sp-consensus-grandpa = { path = "../../../primitives/consensus/grandpa", default-features = false, features = ["serde"] }
33+
sp-core = { path = "../../../primitives/core", default-features = false, features = ["serde"]}
3434
sp-inherents = { path = "../../../primitives/inherents", default-features = false}
3535
sp-offchain = { path = "../../../primitives/offchain", default-features = false}
36-
sp-runtime = { path = "../../../primitives/runtime", default-features = false}
36+
sp-runtime = { path = "../../../primitives/runtime", default-features = false, features = ["serde"] }
3737
sp-session = { path = "../../../primitives/session", default-features = false}
3838
sp-std = { path = "../../../primitives/std", default-features = false}
3939
sp-storage = { path = "../../../primitives/storage", default-features = false}
4040
sp-transaction-pool = { path = "../../../primitives/transaction-pool", default-features = false}
41-
sp-version = { path = "../../../primitives/version", default-features = false}
41+
sp-version = { path = "../../../primitives/version", default-features = false, features = ["serde"] }
42+
serde_json = { version = "1.0.85", default-features = false, features = ["alloc"] }
4243
sp-genesis-builder = { default-features = false, path = "../../../primitives/genesis-builder" }
4344

4445
# Used for the node template's RPCs
@@ -75,6 +76,7 @@ std = [
7576
"pallet-transaction-payment-rpc-runtime-api/std",
7677
"pallet-transaction-payment/std",
7778
"scale-info/std",
79+
"serde_json/std",
7880
"sp-api/std",
7981
"sp-block-builder/std",
8082
"sp-consensus-aura/std",

substrate/bin/node/cli/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ futures = "0.3.21"
124124
tempfile = "3.1.0"
125125
assert_cmd = "2.0.2"
126126
nix = { version = "0.26.1", features = ["signal"] }
127-
serde_json = "1.0"
128127
regex = "1.6.0"
129128
platforms = "3.0"
130129
soketto = "0.7.1"

0 commit comments

Comments
 (0)