mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-29 11:27:58 +00:00
GenesisConfig presets for runtime (#2714)
The runtime now can provide a number of predefined presets of `RuntimeGenesisConfig` struct. This presets are intended to be used in different deployments, e.g.: `local`, `staging`, etc, and should be included into the corresponding chain-specs. Having `GenesisConfig` presets in runtime allows to fully decouple node from runtime types (the problem is described in #1984). **Summary of changes:** - The `GenesisBuilder` API was adjusted to enable this functionality (and provide better naming - #150): ```rust fn preset_names() -> Vec<PresetId>; fn get_preset(id: Option<PresetId>) -> Option<serde_json::Value>; //`None` means default fn build_state(value: serde_json::Value); pub struct PresetId(Vec<u8>); ``` - **Breaking change**: Old `create_default_config` method was removed, `build_config` was renamed to `build_state`. As a consequence a node won't be able to interact with genesis config for older runtimes. The cleanup was made for sake of API simplicity. Also IMO maintaining compatibility with old API is not so crucial. - Reference implementation was provided for `substrate-test-runtime` and `rococo` runtimes. For rococo new [`genesis_configs_presets`](https://github.com/paritytech/polkadot-sdk/blob/3b41d66b97c5ff0ec4a1989da5ffd8b9f3f588e3/polkadot/runtime/rococo/src/genesis_config_presets.rs#L530) module was added and is used in `GenesisBuilder` [_presets-related_](https://github.com/paritytech/polkadot-sdk/blob/3b41d66b97c5ff0ec4a1989da5ffd8b9f3f588e3/polkadot/runtime/rococo/src/lib.rs#L2462-L2485) methods. - The `chain-spec-builder` util was also improved and allows to ([_doc_](https://github.com/paritytech/polkadot-sdk/blob/3b41d66b97c5ff0ec4a1989da5ffd8b9f3f588e3/substrate/bin/utils/chain-spec-builder/src/lib.rs#L19)): - list presets provided by given runtime (`list-presets`), - display preset or default config provided by the runtime (`display-preset`), - build chain-spec using named preset (`create ... named-preset`), - The `ChainSpecBuilder` is extended with [`with_genesis_config_preset_name`](https://github.com/paritytech/polkadot-sdk/blob/3b41d66b97c5ff0ec4a1989da5ffd8b9f3f588e3/substrate/client/chain-spec/src/chain_spec.rs#L447) method which allows to build chain-spec using named preset provided by the runtime. Sample usage on the node side [here](https://github.com/paritytech/polkadot-sdk/blob/2caffaae803e08a3d5b46c860e8016da023ff4ce/polkadot/node/service/src/chain_spec.rs#L404). Implementation of #1984. fixes: #150 part of: #25 --------- Co-authored-by: Sebastian Kunert <skunert49@gmail.com> Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
This commit is contained in:
committed by
GitHub
parent
9d052b7e09
commit
f910a15c1c
@@ -19,26 +19,39 @@
|
||||
//!
|
||||
//! Provides common logic. For more info refer to [`sp_genesis_builder::GenesisBuilder`].
|
||||
|
||||
extern crate alloc;
|
||||
|
||||
use alloc::vec::Vec;
|
||||
use frame_support::traits::BuildGenesisConfig;
|
||||
use sp_genesis_builder::Result as BuildResult;
|
||||
use sp_genesis_builder::{PresetId, Result as BuildResult};
|
||||
use sp_runtime::format_runtime_string;
|
||||
|
||||
/// Get the default `GenesisConfig` as a JSON blob. For more info refer to
|
||||
/// [`sp_genesis_builder::GenesisBuilder::create_default_config`]
|
||||
pub fn create_default_config<GC>() -> sp_std::vec::Vec<u8>
|
||||
where
|
||||
GC: BuildGenesisConfig + Default,
|
||||
{
|
||||
serde_json::to_string(&GC::default())
|
||||
.expect("serialization to json is expected to work. qed.")
|
||||
.into_bytes()
|
||||
}
|
||||
|
||||
/// Build `GenesisConfig` from a JSON blob not using any defaults and store it in the storage. For
|
||||
/// more info refer to [`sp_genesis_builder::GenesisBuilder::build_config`].
|
||||
pub fn build_config<GC: BuildGenesisConfig>(json: sp_std::vec::Vec<u8>) -> BuildResult {
|
||||
/// more info refer to [`sp_genesis_builder::GenesisBuilder::build_state`].
|
||||
pub fn build_state<GC: BuildGenesisConfig>(json: Vec<u8>) -> BuildResult {
|
||||
let gc = serde_json::from_slice::<GC>(&json)
|
||||
.map_err(|e| format_runtime_string!("Invalid JSON blob: {}", e))?;
|
||||
<GC as BuildGenesisConfig>::build(&gc);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Get the default `GenesisConfig` as a JSON blob if `name` is None.
|
||||
///
|
||||
/// Query of named presets is delegetaed to provided `preset_for_name` closure. For more info refer
|
||||
/// to [`sp_genesis_builder::GenesisBuilder::get_preset`].
|
||||
pub fn get_preset<GC>(
|
||||
name: &Option<PresetId>,
|
||||
preset_for_name: impl FnOnce(&sp_genesis_builder::PresetId) -> Option<sp_std::vec::Vec<u8>>,
|
||||
) -> Option<Vec<u8>>
|
||||
where
|
||||
GC: BuildGenesisConfig + Default,
|
||||
{
|
||||
name.as_ref().map_or(
|
||||
Some(
|
||||
serde_json::to_string(&GC::default())
|
||||
.expect("serialization to json is expected to work. qed.")
|
||||
.into_bytes(),
|
||||
),
|
||||
preset_for_name,
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user