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:
Michal Kucharczyk
2024-04-04 20:30:54 +02:00
committed by GitHub
parent 9d052b7e09
commit f910a15c1c
47 changed files with 1460 additions and 661 deletions
+63 -14
View File
@@ -40,15 +40,26 @@ use std::{
sync::Arc,
};
#[derive(Serialize, Deserialize, Clone)]
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
enum GenesisBuildAction {
enum GenesisBuildAction<EHF> {
Patch(json::Value),
Full(json::Value),
NamedPreset(String, PhantomData<EHF>),
}
impl<EHF> Clone for GenesisBuildAction<EHF> {
fn clone(&self) -> Self {
match self {
Self::Patch(ref p) => Self::Patch(p.clone()),
Self::Full(ref f) => Self::Full(f.clone()),
Self::NamedPreset(ref p, _) => Self::NamedPreset(p.clone(), Default::default()),
}
}
}
#[allow(deprecated)]
enum GenesisSource<G> {
enum GenesisSource<G, EHF> {
File(PathBuf),
Binary(Cow<'static, [u8]>),
/// factory function + code
@@ -56,10 +67,10 @@ enum GenesisSource<G> {
Factory(Arc<dyn Fn() -> G + Send + Sync>, Vec<u8>),
Storage(Storage),
/// build action + code
GenesisBuilderApi(GenesisBuildAction, Vec<u8>),
GenesisBuilderApi(GenesisBuildAction<EHF>, Vec<u8>),
}
impl<G> Clone for GenesisSource<G> {
impl<G, EHF> Clone for GenesisSource<G, EHF> {
fn clone(&self) -> Self {
match *self {
Self::File(ref path) => Self::File(path.clone()),
@@ -71,7 +82,7 @@ impl<G> Clone for GenesisSource<G> {
}
}
impl<G: RuntimeGenesis> GenesisSource<G> {
impl<G: RuntimeGenesis, EHF: HostFunctions> GenesisSource<G, EHF> {
fn resolve(&self) -> Result<Genesis<G>, String> {
/// helper container for deserializing genesis from the JSON file (ChainSpec JSON file is
/// also supported here)
@@ -118,6 +129,13 @@ impl<G: RuntimeGenesis> GenesisSource<G> {
json_blob: RuntimeGenesisConfigJson::Patch(patch.clone()),
code: code.clone(),
})),
Self::GenesisBuilderApi(GenesisBuildAction::NamedPreset(name, _), code) => {
let patch = RuntimeCaller::<EHF>::new(&code[..]).get_named_preset(Some(name))?;
Ok(Genesis::RuntimeGenesis(RuntimeGenesisInner {
json_blob: RuntimeGenesisConfigJson::Patch(patch),
code: code.clone(),
}))
},
}
}
}
@@ -331,13 +349,13 @@ pub struct ChainSpecBuilder<G, E = NoExtension, EHF = ()> {
name: String,
id: String,
chain_type: ChainType,
genesis_build_action: GenesisBuildAction,
genesis_build_action: GenesisBuildAction<EHF>,
boot_nodes: Option<Vec<MultiaddrWithPeerId>>,
telemetry_endpoints: Option<TelemetryEndpoints>,
protocol_id: Option<String>,
fork_id: Option<String>,
properties: Option<Properties>,
_genesis: PhantomData<(G, EHF)>,
_genesis: PhantomData<G>,
}
impl<G, E, EHF> ChainSpecBuilder<G, E, EHF> {
@@ -425,6 +443,13 @@ impl<G, E, EHF> ChainSpecBuilder<G, E, EHF> {
self
}
/// Sets the name of runtime-provided JSON patch for runtime's GenesisConfig.
pub fn with_genesis_config_preset_name(mut self, name: &str) -> Self {
self.genesis_build_action =
GenesisBuildAction::NamedPreset(name.to_string(), Default::default());
self
}
/// Sets the full runtime's GenesisConfig JSON.
pub fn with_genesis_config(mut self, config: json::Value) -> Self {
self.genesis_build_action = GenesisBuildAction::Full(config);
@@ -463,7 +488,7 @@ impl<G, E, EHF> ChainSpecBuilder<G, E, EHF> {
/// runtime is using the non-standard host function during genesis state creation.
pub struct ChainSpec<G, E = NoExtension, EHF = ()> {
client_spec: ClientSpec<E>,
genesis: GenesisSource<G>,
genesis: GenesisSource<G, EHF>,
_host_functions: PhantomData<EHF>,
}
@@ -1009,7 +1034,30 @@ mod tests {
assert!(raw_chain_spec.is_ok());
}
#[docify::export]
#[test]
fn generate_chain_spec_with_named_preset_works() {
sp_tracing::try_init_simple();
let output: ChainSpec<()> = ChainSpec::builder(
substrate_test_runtime::wasm_binary_unwrap().into(),
Default::default(),
)
.with_name("TestName")
.with_id("test_id")
.with_chain_type(ChainType::Local)
.with_genesis_config_preset_name("staging")
.build();
let actual = output.as_json(false).unwrap();
let expected =
from_str::<Value>(include_str!("../res/substrate_test_runtime_from_named_preset.json"))
.unwrap();
//wasm blob may change overtime so let's zero it. Also ensure it is there:
let actual = zeroize_code_key_in_json(false, actual.as_str());
assert_eq!(actual, expected);
}
#[test]
fn generate_chain_spec_with_patch_works() {
let output = ChainSpec::<()>::builder(
@@ -1089,6 +1137,8 @@ mod tests {
#[test]
fn chain_spec_as_json_fails_with_invalid_config() {
let expected_error_message =
include_str!("../res/chain_spec_as_json_fails_with_invalid_config.err");
let j =
include_str!("../../../test-utils/runtime/res/default_genesis_config_invalid_2.json");
let output = ChainSpec::<()>::builder(
@@ -1101,10 +1151,9 @@ mod tests {
.with_genesis_config(from_str(j).unwrap())
.build();
assert_eq!(
output.as_json(true),
Err("Invalid JSON blob: unknown field `babex`, expected one of `system`, `babe`, `substrateTest`, `balances` at line 1 column 8".to_string())
);
let result = output.as_json(true);
assert_eq!(result.err().unwrap(), expected_error_message);
}
#[test]
@@ -26,7 +26,7 @@ use sp_core::{
storage::Storage,
traits::{CallContext, CodeExecutor, Externalities, FetchRuntimeCode, RuntimeCode},
};
use sp_genesis_builder::Result as BuildResult;
use sp_genesis_builder::{PresetId, Result as BuildResult};
use sp_state_machine::BasicExternalities;
use std::borrow::Cow;
@@ -84,30 +84,46 @@ where
/// Returns a json representation of the default `RuntimeGenesisConfig` provided by the
/// `runtime`.
///
/// Calls [`GenesisBuilder::create_default_config`](sp_genesis_builder::GenesisBuilder::create_default_config) in the `runtime`.
/// Calls [`GenesisBuilder::get_preset`](sp_genesis_builder::GenesisBuilder::get_preset) in the
/// `runtime` with `None` argument.
pub fn get_default_config(&self) -> core::result::Result<Value, String> {
let mut t = BasicExternalities::new_empty();
let call_result = self
.call(&mut t, "GenesisBuilder_create_default_config", &[])
.map_err(|e| format!("wasm call error {e}"))?;
let default_config = Vec::<u8>::decode(&mut &call_result[..])
.map_err(|e| format!("scale codec error: {e}"))?;
Ok(from_slice(&default_config[..]).expect("returned value is json. qed."))
self.get_named_preset(None)
}
/// Builds `RuntimeGenesisConfig` from given json blob and returns the genesis state.
/// Returns a JSON blob representation of the builtin `GenesisConfig` identified by `id`.
///
/// Calls [`GenesisBuilder::build_config`](sp_genesis_builder::GenesisBuilder::build_config)
/// Calls [`GenesisBuilder::get_preset`](sp_genesis_builder::GenesisBuilder::get_preset)
/// provided by the `runtime`.
pub fn get_named_preset(&self, id: Option<&String>) -> core::result::Result<Value, String> {
let mut t = BasicExternalities::new_empty();
let call_result = self
.call(&mut t, "GenesisBuilder_get_preset", &id.encode())
.map_err(|e| format!("wasm call error {e}"))?;
let named_preset = Option::<Vec<u8>>::decode(&mut &call_result[..])
.map_err(|e| format!("scale codec error: {e}"))?;
if let Some(named_preset) = named_preset {
Ok(from_slice(&named_preset[..]).expect("returned value is json. qed."))
} else {
Err(format!("The preset with name {id:?} is not available."))
}
}
/// Calls [`sp_genesis_builder::GenesisBuilder::build_state`] provided by runtime.
pub fn get_storage_for_config(&self, config: Value) -> core::result::Result<Storage, String> {
let mut ext = BasicExternalities::new_empty();
let json_pretty_str = serde_json::to_string_pretty(&config)
.map_err(|e| format!("json to string failed: {e}"))?;
let call_result = self
.call(&mut ext, "GenesisBuilder_build_config", &config.to_string().encode())
.call(&mut ext, "GenesisBuilder_build_state", &json_pretty_str.encode())
.map_err(|e| format!("wasm call error {e}"))?;
BuildResult::decode(&mut &call_result[..])
.map_err(|e| format!("scale codec error: {e}"))??;
.map_err(|e| format!("scale codec error: {e}"))?
.map_err(|e| format!("{e} for blob:\n{}", json_pretty_str))?;
Ok(ext.into_storages())
}
@@ -137,6 +153,25 @@ where
crate::json_patch::merge(&mut config, patch);
self.get_storage_for_config(config)
}
pub fn get_storage_for_named_preset(
&self,
name: Option<&String>,
) -> core::result::Result<Storage, String> {
self.get_storage_for_patch(self.get_named_preset(name)?)
}
pub fn preset_names(&self) -> core::result::Result<Vec<PresetId>, String> {
let mut t = BasicExternalities::new_empty();
let call_result = self
.call(&mut t, "GenesisBuilder_preset_names", &vec![])
.map_err(|e| format!("wasm call error {e}"))?;
let preset_names = Vec::<PresetId>::decode(&mut &call_result[..])
.map_err(|e| format!("scale codec error: {e}"))?;
Ok(preset_names)
}
}
#[cfg(test)]
@@ -144,6 +179,17 @@ mod tests {
use super::*;
use serde_json::{from_str, json};
pub use sp_consensus_babe::{AllowedSlots, BabeEpochConfiguration};
pub use sp_genesis_builder::PresetId;
#[test]
fn list_presets_works() {
sp_tracing::try_init_simple();
let presets =
<GenesisConfigBuilderRuntimeCaller>::new(substrate_test_runtime::wasm_binary_unwrap())
.preset_names()
.unwrap();
assert_eq!(presets, vec![PresetId::from("foobar"), PresetId::from("staging"),]);
}
#[test]
fn get_default_config_works() {
@@ -155,6 +201,17 @@ mod tests {
assert_eq!(from_str::<Value>(expected).unwrap(), config);
}
#[test]
fn get_named_preset_works() {
sp_tracing::try_init_simple();
let config =
<GenesisConfigBuilderRuntimeCaller>::new(substrate_test_runtime::wasm_binary_unwrap())
.get_named_preset(Some(&"foobar".to_string()))
.unwrap();
let expected = r#"{"foo":"bar"}"#;
assert_eq!(from_str::<Value>(expected).unwrap(), config);
}
#[test]
fn get_storage_for_patch_works() {
let patch = json!({
+2 -2
View File
@@ -181,7 +181,7 @@
//! the node needs to interact with the runtime.
//!
//! This interaction involves passing the runtime genesis config JSON blob to the runtime using the
//! [`sp_genesis_builder::GenesisBuilder::build_config`] function. During this operation, the
//! [`sp_genesis_builder::GenesisBuilder::build_state`] function. During this operation, the
//! runtime converts the JSON representation of the genesis config into [`sp_io::storage`] items. It
//! is a crucial step for computing the storage root hash, which is a key component in determining
//! the genesis hash.
@@ -325,7 +325,7 @@ mod chain_spec;
mod extension;
mod genesis_block;
mod genesis_config_builder;
mod json_patch;
pub mod json_patch;
pub use self::{
chain_spec::{