From 7ca598f7f6b8e337b9524a0e57ee69888cf6f094 Mon Sep 17 00:00:00 2001 From: Daniel Shiposha Date: Mon, 27 May 2024 21:02:49 +0000 Subject: [PATCH] fix: use ChainSpecBuilder --- --- a/Cargo.lock +++ b/Cargo.lock @@ -6380,6 +6380,7 @@ "sp-block-builder", "sp-consensus-aura", "sp-core", + "sp-genesis-builder", "sp-inherents", "sp-io", "sp-offchain", @@ -10261,6 +10262,7 @@ "sp-block-builder", "sp-consensus-aura", "sp-core", + "sp-genesis-builder", "sp-inherents", "sp-io", "sp-offchain", @@ -15134,6 +15136,7 @@ "sp-block-builder", "sp-consensus-aura", "sp-core", + "sp-genesis-builder", "sp-inherents", "sp-io", "sp-offchain", --- a/Cargo.toml +++ b/Cargo.toml @@ -181,6 +181,7 @@ sp-trie = { default-features = false, version = "32.0.0" } sp-version = { default-features = false, version = "32.0.0" } sp-weights = { default-features = false, version = "30.0.0" } +sp-genesis-builder = { default-features = false, version = "0.10.0" } staging-parachain-info = { default-features = false, version = "0.10.0" } staging-xcm = { default-features = false, version = "10.0.0" } staging-xcm-builder = { default-features = false, version = "10.0.0" } @@ -217,4 +218,5 @@ log = { version = "0.4.20", default-features = false } num_enum = { version = "0.7.0", default-features = false } serde = { default-features = false, features = ['derive'], version = "1.0.188" } +serde_json = "1" smallvec = "1.11.1" --- a/node/cli/src/chain_spec.rs +++ b/node/cli/src/chain_spec.rs @@ -14,8 +14,6 @@ // You should have received a copy of the GNU General Public License // along with Unique Network. If not, see . -use std::collections::BTreeMap; - use default_runtime::WASM_BINARY; #[cfg(all(not(feature = "unique-runtime"), not(feature = "quartz-runtime")))] pub use opal_runtime as default_runtime; @@ -24,7 +22,7 @@ use sc_chain_spec::{ChainSpecExtension, ChainSpecGroup}; use sc_service::ChainType; use serde::{Deserialize, Serialize}; -use serde_json::map::Map; +use serde_json::{json, map::Map}; use sp_core::{sr25519, Pair, Public}; use sp_runtime::traits::{IdentifyAccount, Verify}; #[cfg(feature = "unique-runtime")] @@ -142,184 +140,102 @@ AccountPublic::from(get_from_seed::(seed)).into_account() } -#[cfg(not(feature = "unique-runtime"))] -macro_rules! testnet_genesis { - ( - $runtime:path, - $root_key:expr, - $initial_invulnerables:expr, - $endowed_accounts:expr, - $id:expr - ) => {{ - use $runtime::*; - - RuntimeGenesisConfig { - system: Default::default(), - balances: BalancesConfig { - balances: $endowed_accounts - .iter() - .cloned() - // 1e13 UNQ - .map(|k| (k, 1 << 100)) - .collect(), - }, - sudo: SudoConfig { - key: Some($root_key), - }, - - vesting: VestingConfig { vesting: vec![] }, - parachain_info: ParachainInfoConfig { - parachain_id: $id.into(), - ..Default::default() - }, - collator_selection: CollatorSelectionConfig { - invulnerables: $initial_invulnerables - .iter() - .cloned() - .map(|(acc, _)| acc) - .collect(), - }, - session: SessionConfig { - keys: $initial_invulnerables - .into_iter() - .map(|(acc, aura)| { - ( - acc.clone(), // account id - acc, // validator id - SessionKeys { aura }, // session keys - ) - }) - .collect(), - }, - evm: EVMConfig { - accounts: BTreeMap::new(), - ..Default::default() - }, - ..Default::default() +pub fn test_config(chain_id: &str, relay_chain: &str) -> DefaultChainSpec { + DefaultChainSpec::builder( + WASM_BINARY.expect("WASM binary was not build, please build it!"), + Extensions { + relay_chain: relay_chain.into(), + para_id: PARA_ID, + }, + ) + .with_id(&format!( + "{}_{}", + default_runtime::VERSION.spec_name, + chain_id + )) + .with_name(&format!( + "{}{}", + default_runtime::VERSION.spec_name.to_uppercase(), + if cfg!(feature = "unique-runtime") { + "" + } else { + " by UNIQUE" } - }}; + )) + .with_properties(chain_properties()) + .with_chain_type(ChainType::Development) + .with_genesis_config_patch(genesis_patch()) + .build() } -#[cfg(feature = "unique-runtime")] -macro_rules! testnet_genesis { - ( - $runtime:path, - $root_key:expr, - $initial_invulnerables:expr, - $endowed_accounts:expr, - $id:expr - ) => {{ - use $runtime::*; +fn genesis_patch() -> serde_json::Value { + use default_runtime::*; - RuntimeGenesisConfig { - system: Default::default(), - balances: BalancesConfig { - balances: $endowed_accounts - .iter() - .cloned() - // 1e13 UNQ - .map(|k| (k, 1 << 100)) - .collect(), - }, - sudo: SudoConfig { - key: Some($root_key), - }, - vesting: VestingConfig { vesting: vec![] }, - parachain_info: ParachainInfoConfig { - parachain_id: $id.into(), - ..Default::default() - }, - aura: AuraConfig { - authorities: $initial_invulnerables - .into_iter() - .map(|(_, aura)| aura) - .collect(), - }, - evm: EVMConfig { - accounts: BTreeMap::new(), - ..Default::default() - }, - ..Default::default() - } - }}; -} + let invulnerables = ["Alice", "Bob"]; -pub fn development_config() -> DefaultChainSpec { - let mut properties = Map::new(); - properties.insert("tokenSymbol".into(), default_runtime::TOKEN_SYMBOL.into()); - properties.insert("tokenDecimals".into(), default_runtime::DECIMALS.into()); - properties.insert( - "ss58Format".into(), - default_runtime::SS58Prefix::get().into(), - ); + #[allow(unused_mut)] + let mut patch = json!({ + "parachainInfo": { + "parachainId": PARA_ID, + }, - DefaultChainSpec::from_genesis( - // Name - format!( - "{}{}", - default_runtime::VERSION.spec_name.to_uppercase(), - if cfg!(feature = "unique-runtime") { - "" - } else { - " by UNIQUE" - } - ) - .as_str(), - // ID - format!("{}_dev", default_runtime::VERSION.spec_name).as_str(), - ChainType::Local, - move || { - testnet_genesis!( - default_runtime, - // Sudo account - get_account_id_from_seed::("Alice"), - [ + "aura": { + "authorities": invulnerables.into_iter() + .map(|name| get_from_seed::(name)) + .collect::>(), + }, + + "session": { + "keys": invulnerables.into_iter() + .map(|name| { + let account = get_account_id_from_seed::(name); + let aura = get_from_seed::(name); + ( - get_account_id_from_seed::("Alice"), - get_from_seed::("Alice"), - ), - ( - get_account_id_from_seed::("Bob"), - get_from_seed::("Bob"), - ), - ], - // Pre-funded accounts - vec![ - get_account_id_from_seed::("Alice"), - get_account_id_from_seed::("Bob"), - get_account_id_from_seed::("Charlie"), - get_account_id_from_seed::("Dave"), - get_account_id_from_seed::("Eve"), - get_account_id_from_seed::("Ferdie"), - get_account_id_from_seed::("Alice//stash"), - get_account_id_from_seed::("Bob//stash"), - get_account_id_from_seed::("Charlie//stash"), - get_account_id_from_seed::("Dave//stash"), - get_account_id_from_seed::("Eve//stash"), - get_account_id_from_seed::("Ferdie//stash"), - ], - PARA_ID - ) + /* account id: */ account.clone(), + /* validator id: */ account, + /* session keys: */ SessionKeys { aura }, + ) + }) + .collect::>() + }, + + "sudo": { + "key": get_account_id_from_seed::("Alice"), }, - // Bootnodes - vec![], - // Telemetry - None, - // Protocol ID - None, - None, - // Properties - Some(properties), - // Extensions - Extensions { - relay_chain: "rococo-dev".into(), - para_id: PARA_ID, + + "balances": { + "balances": &[ + get_account_id_from_seed::("Alice"), + get_account_id_from_seed::("Bob"), + get_account_id_from_seed::("Charlie"), + get_account_id_from_seed::("Dave"), + get_account_id_from_seed::("Eve"), + get_account_id_from_seed::("Ferdie"), + get_account_id_from_seed::("Alice//stash"), + get_account_id_from_seed::("Bob//stash"), + get_account_id_from_seed::("Charlie//stash"), + get_account_id_from_seed::("Dave//stash"), + get_account_id_from_seed::("Eve//stash"), + get_account_id_from_seed::("Ferdie//stash"), + ].into_iter() + .map(|k| (k, /* ~1.2e+12 UNQ */ 1u128 << 100)) + .collect::>(), }, - WASM_BINARY.expect("WASM binary was not build, please build it!"), - ) + }); + + #[cfg(feature = "unique-runtime")] + { + patch + .as_object_mut() + .expect("the genesis patch is always an object; qed") + .remove("session"); + } + + patch } -pub fn local_testnet_config() -> DefaultChainSpec { +fn chain_properties() -> sc_chain_spec::Properties { let mut properties = Map::new(); properties.insert("tokenSymbol".into(), default_runtime::TOKEN_SYMBOL.into()); properties.insert("tokenDecimals".into(), default_runtime::DECIMALS.into()); @@ -328,68 +244,5 @@ default_runtime::SS58Prefix::get().into(), ); - DefaultChainSpec::from_genesis( - // Name - format!( - "{}{}", - default_runtime::VERSION.impl_name.to_uppercase(), - if cfg!(feature = "unique-runtime") { - "" - } else { - " by UNIQUE" - } - ) - .as_str(), - // ID - format!("{}_local", default_runtime::VERSION.spec_name).as_str(), - ChainType::Local, - move || { - testnet_genesis!( - default_runtime, - // Sudo account - get_account_id_from_seed::("Alice"), - [ - ( - get_account_id_from_seed::("Alice"), - get_from_seed::("Alice"), - ), - ( - get_account_id_from_seed::("Bob"), - get_from_seed::("Bob"), - ), - ], - // Pre-funded accounts - vec![ - get_account_id_from_seed::("Alice"), - get_account_id_from_seed::("Bob"), - get_account_id_from_seed::("Charlie"), - get_account_id_from_seed::("Dave"), - get_account_id_from_seed::("Eve"), - get_account_id_from_seed::("Ferdie"), - get_account_id_from_seed::("Alice//stash"), - get_account_id_from_seed::("Bob//stash"), - get_account_id_from_seed::("Charlie//stash"), - get_account_id_from_seed::("Dave//stash"), - get_account_id_from_seed::("Eve//stash"), - get_account_id_from_seed::("Ferdie//stash"), - ], - PARA_ID - ) - }, - // Bootnodes - vec![], - // Telemetry - None, - // Protocol ID - None, - None, - // Properties - Some(properties), - // Extensions - Extensions { - relay_chain: "westend-local".into(), - para_id: PARA_ID, - }, - WASM_BINARY.expect("WASM binary was not build, please build it!"), - ) + properties } --- a/node/cli/src/command.rs +++ b/node/cli/src/command.rs @@ -49,9 +49,7 @@ use crate::{ chain_spec::{self, RuntimeIdentification, ServiceId, ServiceIdentification}, cli::{Cli, RelayChainCli, Subcommand}, - service::{ - new_partial, start_dev_node, start_node, OpalRuntimeExecutor, ParachainHostFunctions, - }, + service::{new_partial, start_dev_node, start_node, OpalRuntimeExecutor}, }; macro_rules! no_runtime_err { @@ -65,8 +63,8 @@ fn load_spec(id: &str) -> std::result::Result, String> { Ok(match id { - "dev" => Box::new(chain_spec::development_config()), - "" | "local" => Box::new(chain_spec::local_testnet_config()), + "dev" => Box::new(chain_spec::test_config("dev", "rococo-dev")), + "" | "local" => Box::new(chain_spec::test_config("local", "westend-local")), path => { let path = std::path::PathBuf::from(path); #[allow(clippy::redundant_clone)] @@ -352,6 +350,8 @@ use frame_benchmarking_cli::{BenchmarkCmd, SUBSTRATE_REFERENCE_HARDWARE}; use polkadot_cli::Block; + use crate::service::ParachainHostFunctions; + type Header = ::Header; type Hasher =
::Hashing; @@ -403,7 +403,7 @@ use std::{future::Future, pin::Pin}; use polkadot_cli::Block; - use sc_executor::{sp_wasm_interface::ExtendedHostFunctions, NativeExecutionDispatch}; + use sc_executor::NativeExecutionDispatch; use try_runtime_cli::block_building_info::timestamp_with_aura_info; let runner = cli.create_runner(cmd)?; --- a/runtime/common/runtime_apis.rs +++ b/runtime/common/runtime_apis.rs @@ -43,6 +43,7 @@ ApplyExtrinsicResult, DispatchError, ExtrinsicInclusionMode, }; use frame_support::{ + genesis_builder_helper::{build_config, create_default_config}, pallet_prelude::Weight, traits::OnFinalize, }; @@ -710,6 +711,16 @@ ) } } + + impl sp_genesis_builder::GenesisBuilder for Runtime { + fn create_default_config() -> Vec { + create_default_config::() + } + + fn build_config(config: Vec) -> sp_genesis_builder::Result { + build_config::(config) + } + } } } } --- a/runtime/opal/Cargo.toml +++ b/runtime/opal/Cargo.toml @@ -146,6 +146,7 @@ 'sp-storage/std', 'sp-transaction-pool/std', 'sp-version/std', + 'sp-genesis-builder/std', 'staging-parachain-info/std', 'staging-xcm-builder/std', 'staging-xcm-executor/std', @@ -295,6 +296,7 @@ sp-storage = { workspace = true } sp-transaction-pool = { workspace = true } sp-version = { workspace = true } +sp-genesis-builder = { workspace = true } staging-parachain-info = { workspace = true } staging-xcm = { workspace = true } staging-xcm-builder = { workspace = true } --- a/runtime/quartz/Cargo.toml +++ b/runtime/quartz/Cargo.toml @@ -145,6 +145,7 @@ 'sp-std/std', 'sp-transaction-pool/std', 'sp-version/std', + 'sp-genesis-builder/std', 'staging-parachain-info/std', 'staging-xcm-builder/std', 'staging-xcm-executor/std', @@ -283,6 +284,7 @@ sp-storage = { workspace = true } sp-transaction-pool = { workspace = true } sp-version = { workspace = true } +sp-genesis-builder = { workspace = true } staging-parachain-info = { workspace = true } staging-xcm = { workspace = true } staging-xcm-builder = { workspace = true } --- a/runtime/unique/Cargo.toml +++ b/runtime/unique/Cargo.toml @@ -143,6 +143,7 @@ 'sp-std/std', 'sp-transaction-pool/std', 'sp-version/std', + 'sp-genesis-builder/std', 'staging-parachain-info/std', 'staging-xcm-builder/std', 'staging-xcm-executor/std', @@ -287,6 +288,7 @@ sp-storage = { workspace = true } sp-transaction-pool = { workspace = true } sp-version = { workspace = true } +sp-genesis-builder = { workspace = true } staging-parachain-info = { workspace = true } staging-xcm = { workspace = true } staging-xcm-builder = { workspace = true } -- gitstuff