Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

chain-spec: support for json config/patch (and GenesisBuilder API) #14562

Open
wants to merge 53 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
52d6a4f
frame::system: code removed
michalkucharczyk Jun 27, 2023
e333e16
chain-spec: mod genesis -> genesis_block
michalkucharczyk Jul 1, 2023
ea2e252
chain-spec: genesis_config_builder (wasm based)
michalkucharczyk Jul 6, 2023
b4f5267
chain-spec: support for code and config patching
michalkucharczyk Jul 7, 2023
baacd30
chain-spec: runtime caller added
michalkucharczyk Jul 9, 2023
b40193f
chain-spec: doc + minor improvements
michalkucharczyk Jul 10, 2023
dc6c927
chain-spec: set_code added
michalkucharczyk Jul 11, 2023
ae9b2a8
chain-spec-builder: from_genesis removed
michalkucharczyk Jul 6, 2023
e166fec
chain-spec-builder: boot_nodes call optional
michalkucharczyk Jul 6, 2023
c200e26
chain-spec-builder: support for runtime wasm blobs
michalkucharczyk Jul 11, 2023
e64b7d1
bin/node: GenesisBuilder implementation added
michalkucharczyk May 24, 2023
5d450a4
bin/node-template: serde enabled in runtime deps
michalkucharczyk May 25, 2023
b945d4d
bin/node/runtime: arbitrary_precision feature added to serde_json
michalkucharczyk Jul 11, 2023
ae9415f
bin/node: fixes
michalkucharczyk Jun 20, 2023
98a9a58
bin/node: from_genesis moved to ChainSpecBuilder
michalkucharczyk Jul 6, 2023
f49362e
bin/node: boot_nodes call optional
michalkucharczyk Jul 6, 2023
f62284d
bin/node: GenesisBuilder helper struct removed
michalkucharczyk Jul 6, 2023
12dac2c
bin/node: frame_system::code removed
michalkucharczyk Jul 6, 2023
ab4ae6b
node-template: frame_system::code removed
michalkucharczyk Jul 6, 2023
fc55a8d
client/cli: from_genesis - code arg added
michalkucharczyk Jul 6, 2023
dbe0c8b
chain-spec: naming fixed
michalkucharczyk Jul 17, 2023
01f10b9
node-template: ChainSpecBuilder used
michalkucharczyk Jul 17, 2023
7015549
bin/node: ChainSpecBuilder used (tests)
michalkucharczyk Jul 17, 2023
86ab3d3
cli/cmd/insert_key: ChainSpecBuilder used (tests)
michalkucharczyk Jul 17, 2023
1418558
cli/runner: ChainSpecBuilder used (tests)
michalkucharczyk Jul 17, 2023
2faefc1
ChainSpec: note corrected
michalkucharczyk Jul 17, 2023
ef29d30
Merge remote-tracking branch 'origin/master' into mku-chain-spec-supp…
michalkucharczyk Jul 20, 2023
fc1c393
chain-spec-builder: after-merge cleanup
michalkucharczyk Jul 20, 2023
4e9732c
Merge remote-tracking branch 'origin/master' into mku-chain-spec-supp…
Jul 24, 2023
bbfabb5
Cargo.lock updated
michalkucharczyk Jul 24, 2023
ce2b20a
test-runtime: frame_system::code removed
michalkucharczyk Jul 6, 2023
b214805
node-bench: code addded to genesis storage
michalkucharczyk Jul 25, 2023
1fd42a0
node-testing: code added to genesis storage
michalkucharczyk Jul 25, 2023
b77b45e
bin/node: executor test fix
michalkucharczyk Jul 25, 2023
a445c68
Merge remote-tracking branch 'origin/master' into mku-chain-spec-supp…
michalkucharczyk Jul 25, 2023
f869b0e
Cargo.lock updated
michalkucharczyk Jul 25, 2023
e41a161
chainspec::code field is better handled for raw specs
michalkucharczyk Jul 31, 2023
991f9fa
code field improvements
michalkucharczyk Jul 31, 2023
0258f52
doc + naming
michalkucharczyk Jul 31, 2023
0215340
Merge remote-tracking branch 'origin/master' into mku-chain-spec-supp…
Aug 1, 2023
20bee68
doc improvements
michalkucharczyk Aug 1, 2023
0bf081e
improvements
michalkucharczyk Aug 1, 2023
67a77ad
test-runtime: json files moved to res dir
michalkucharczyk Aug 1, 2023
da79f96
Cargo.toml fixes
michalkucharczyk Aug 1, 2023
2bd3fab
".git/.scripts/commands/fmt/fmt.sh"
Aug 1, 2023
d788003
Cargo.lock updated
michalkucharczyk Aug 1, 2023
298305b
minor fixes
michalkucharczyk Aug 1, 2023
97e5b1b
Apply suggestions from code review
michalkucharczyk Aug 2, 2023
5f60a4b
cleanup
michalkucharczyk Aug 2, 2023
0f58273
".git/.scripts/commands/fmt/fmt.sh"
Aug 2, 2023
1096d36
Merge remote-tracking branch 'origin/master' into mku-chain-spec-supp…
Aug 2, 2023
e1c7e11
removing unneeded refs to runtime PalletGenesisConfig types
michalkucharczyk Aug 3, 2023
fc33dfb
Merge branch 'master' into mku-chain-spec-support-for-genesis-builder…
michalkucharczyk Aug 4, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 74 additions & 53 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions bin/node-template/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ sp-inherents = { version = "4.0.0-dev", path = "../../../primitives/inherents" }
sp-keyring = { version = "24.0.0", path = "../../../primitives/keyring" }
frame-system = { version = "4.0.0-dev", path = "../../../frame/system" }
pallet-transaction-payment = { version = "4.0.0-dev", default-features = false, path = "../../../frame/transaction-payment" }
serde_json = "1.0.85"

# These dependencies are used for the node template's RPCs
jsonrpsee = { version = "0.16.2", features = ["server"] }
Expand Down
163 changes: 60 additions & 103 deletions 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,82 @@ 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
Ok(ChainSpec::builder()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really related (feel free to ignore), but I'd rename the functions retuning the ChainSpec with something like development_chain_spec()

.with_name("Development")
.with_id("dev")
.with_chain_type(ChainType::Development)
.with_extensions(None)
.with_code(WASM_BINARY.ok_or_else(|| "Development wasm not available".to_string())?)
.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"),
// 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
None,
))
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
Ok(ChainSpec::builder()
.with_name("Local Testnet")
.with_id("local_testnet")
.with_chain_type(ChainType::Local)
.with_extensions(None)
.with_code(WASM_BINARY.ok_or_else(|| "Development wasm not available".to_string())?)
.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"),
// 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
None,
))
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!({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By using serde_json::Value we are loosing the static type checking advantages we can have when using the types defined by the native runtime (i.e. RuntimeGenesisConfig).

(Maybe I'm going to say something stupid, not 100% confident with metadata) but isn't possible to have some sanity check using the runtime metadata?
Maybe for this we have to investigate (scale_info::meta_type::<Runtime>())

"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(),
}
})
}
20 changes: 13 additions & 7 deletions bin/node-template/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +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.5.0", default-features = false, features = ["derive"] }

scale-info = { version = "2.5.0", default-features = false, features = ["derive", "serde"] }
pallet-aura = { version = "4.0.0-dev", default-features = false, path = "../../../frame/aura" }
pallet-balances = { version = "4.0.0-dev", default-features = false, path = "../../../frame/balances" }
frame-support = { version = "4.0.0-dev", default-features = false, path = "../../../frame/support" }
Expand All @@ -28,16 +27,18 @@ pallet-transaction-payment = { version = "4.0.0-dev", default-features = false,
frame-executive = { version = "4.0.0-dev", default-features = false, path = "../../../frame/executive" }
sp-api = { version = "4.0.0-dev", default-features = false, path = "../../../primitives/api" }
sp-block-builder = { version = "4.0.0-dev", default-features = false, path = "../../../primitives/block-builder"}
sp-consensus-aura = { version = "0.10.0-dev", default-features = false, path = "../../../primitives/consensus/aura" }
sp-consensus-grandpa = { version = "4.0.0-dev", default-features = false, path = "../../../primitives/consensus/grandpa" }
sp-core = { version = "21.0.0", default-features = false, path = "../../../primitives/core" }
sp-consensus-aura = { version = "0.10.0-dev", default-features = false, path = "../../../primitives/consensus/aura", features = ["serde"] }
sp-consensus-grandpa = { version = "4.0.0-dev", default-features = false, path = "../../../primitives/consensus/grandpa", features = ["serde"] }
sp-core = { version = "21.0.0", default-features = false, path = "../../../primitives/core", features=["serde"] }
sp-inherents = { version = "4.0.0-dev", default-features = false, path = "../../../primitives/inherents"}
sp-offchain = { version = "4.0.0-dev", default-features = false, path = "../../../primitives/offchain" }
sp-runtime = { version = "24.0.0", default-features = false, path = "../../../primitives/runtime" }
sp-runtime = { version = "24.0.0", default-features = false, path = "../../../primitives/runtime", features = ["serde"] }
sp-session = { version = "4.0.0-dev", default-features = false, path = "../../../primitives/session" }
sp-std = { version = "8.0.0", default-features = false, path = "../../../primitives/std" }
sp-transaction-pool = { version = "4.0.0-dev", default-features = false, path = "../../../primitives/transaction-pool" }
sp-version = { version = "22.0.0", default-features = false, path = "../../../primitives/version" }
sp-version = { version = "22.0.0", default-features = false, path = "../../../primitives/version", features=["serde"] }
serde_json = { version = "1.0.85", default-features = false, features = ["alloc"] }
sp-genesis-builder = { version = "0.1.0-dev", default-features = false, path = "../../../primitives/genesis-builder" }

# Used for the node template's RPCs
frame-system-rpc-runtime-api = { version = "4.0.0-dev", default-features = false, path = "../../../frame/system/rpc/runtime-api/" }
Expand Down Expand Up @@ -87,6 +88,8 @@ std = [
"sp-transaction-pool/std",
"sp-version/std",
"substrate-wasm-builder",
"serde_json/std",
"sp-genesis-builder/std",
]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
Expand Down Expand Up @@ -115,3 +118,6 @@ try-runtime = [
"sp-runtime/try-runtime"
]
experimental = ["pallet-aura/experimental"]

#Enabling this flag will disable GenesisBuilder API implementation in runtime.
disable-genesis-builder = []
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing newline before EOF

13 changes: 13 additions & 0 deletions bin/node-template/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ use sp_std::prelude::*;
use sp_version::NativeVersion;
use sp_version::RuntimeVersion;

#[cfg(not(feature = "disable-genesis-builder"))]
use frame_support::genesis_builder_helper::{build_config, create_default_config};
// A few exports that help ease life for downstream crates.
pub use frame_support::{
construct_runtime, parameter_types,
Expand Down Expand Up @@ -571,4 +573,15 @@ impl_runtime_apis! {
Executive::try_execute_block(block, state_root_check, signature_check, select).expect("execute-block failed")
}
}

#[cfg(not(feature = "disable-genesis-builder"))]
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)
}
}
}
1 change: 0 additions & 1 deletion bin/node/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,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