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
+99 -26
View File
@@ -27,11 +27,14 @@ pub mod extrinsic;
pub mod genesismap;
pub mod substrate_test_pallet;
use alloc::boxed::Box;
#[cfg(not(feature = "std"))]
use alloc::{vec, vec::Vec};
use codec::{Decode, Encode};
use frame_support::{
construct_runtime, derive_impl,
dispatch::DispatchClass,
genesis_builder_helper::{build_config, create_default_config},
genesis_builder_helper::{build_state, get_preset},
parameter_types,
traits::{ConstU32, ConstU64},
weights::{
@@ -44,10 +47,8 @@ use frame_system::{
CheckNonce, CheckWeight,
};
use scale_info::TypeInfo;
use alloc::boxed::Box;
#[cfg(not(feature = "std"))]
use alloc::{vec, vec::Vec};
use sp_application_crypto::Ss58Codec;
use sp_keyring::AccountKeyring;
use sp_application_crypto::{ecdsa, ed25519, sr25519, RuntimeAppPublic};
use sp_core::{OpaqueMetadata, RuntimeDebug};
@@ -57,8 +58,10 @@ use sp_trie::{
};
use trie_db::{Trie, TrieMut};
use serde_json::json;
use sp_api::{decl_runtime_apis, impl_runtime_apis};
pub use sp_core::hash::H256;
use sp_genesis_builder::PresetId;
use sp_inherents::{CheckInherentsResult, InherentData};
use sp_runtime::{
create_runtime_str, impl_opaque_keys,
@@ -93,7 +96,7 @@ pub mod wasm_binary_logging_disabled {
#[cfg(feature = "std")]
pub fn wasm_binary_unwrap() -> &'static [u8] {
WASM_BINARY.expect(
"Development wasm binary is not available. Testing is only supported with the flag \
"Development wasm binary is not available. Testing is only supported with the flag
disabled.",
)
}
@@ -102,7 +105,7 @@ pub fn wasm_binary_unwrap() -> &'static [u8] {
#[cfg(feature = "std")]
pub fn wasm_binary_logging_disabled_unwrap() -> &'static [u8] {
wasm_binary_logging_disabled::WASM_BINARY.expect(
"Development wasm binary is not available. Testing is only supported with the flag \
"Development wasm binary is not available. Testing is only supported with the flag
disabled.",
)
}
@@ -725,12 +728,42 @@ impl_runtime_apis! {
}
impl sp_genesis_builder::GenesisBuilder<Block> for Runtime {
fn create_default_config() -> Vec<u8> {
create_default_config::<RuntimeGenesisConfig>()
fn build_state(config: Vec<u8>) -> sp_genesis_builder::Result {
build_state::<RuntimeGenesisConfig>(config)
}
fn build_config(config: Vec<u8>) -> sp_genesis_builder::Result {
build_config::<RuntimeGenesisConfig>(config)
fn get_preset(name: &Option<PresetId>) -> Option<Vec<u8>> {
get_preset::<RuntimeGenesisConfig>(name, |name| {
let patch = match name.try_into() {
Ok("staging") => {
let endowed_accounts: Vec<AccountId> = vec![
AccountKeyring::Bob.public().into(),
AccountKeyring::Charlie.public().into(),
];
json!({
"balances": {
"balances": endowed_accounts.into_iter().map(|k| (k, 10 * currency::DOLLARS)).collect::<Vec<_>>(),
},
"substrateTest": {
"authorities": [
AccountKeyring::Alice.public().to_ss58check(),
AccountKeyring::Ferdie.public().to_ss58check()
],
}
})
},
Ok("foobar") => json!({"foo":"bar"}),
_ => return None,
};
Some(serde_json::to_string(&patch)
.expect("serialization to json is expected to work. qed.")
.into_bytes())
})
}
fn preset_names() -> Vec<PresetId> {
vec![PresetId::from("foobar"), PresetId::from("staging")]
}
}
}
@@ -836,7 +869,6 @@ fn test_witness(proof: StorageProof, root: crate::Hash) {
pub mod storage_key_generator {
use super::*;
use sp_core::Pair;
use sp_keyring::AccountKeyring;
/// Generate hex string without prefix
pub(super) fn hex<T>(x: T) -> String
@@ -1026,7 +1058,6 @@ mod tests {
use sp_api::{ApiExt, ProvideRuntimeApi};
use sp_consensus::BlockOrigin;
use sp_core::{storage::well_known_keys::HEAP_PAGES, traits::CallContext};
use sp_keyring::AccountKeyring;
use sp_runtime::{
traits::{Hash as _, SignedExtension},
transaction_validity::{InvalidTransaction, ValidTransaction},
@@ -1177,7 +1208,7 @@ mod tests {
fn check_substrate_check_signed_extension_works() {
sp_tracing::try_init_simple();
new_test_ext().execute_with(|| {
let x = sp_keyring::AccountKeyring::Alice.into();
let x = AccountKeyring::Alice.into();
let info = DispatchInfo::default();
let len = 0_usize;
assert_eq!(
@@ -1242,7 +1273,7 @@ mod tests {
let default_minimal_json = r#"{"system":{},"babe":{"authorities":[],"epochConfig":{"c": [ 3, 10 ],"allowed_slots":"PrimaryAndSecondaryPlainSlots"}},"substrateTest":{"authorities":[]},"balances":{"balances":[]}}"#;
let mut t = BasicExternalities::new_empty();
executor_call(&mut t, "GenesisBuilder_build_config", &default_minimal_json.encode())
executor_call(&mut t, "GenesisBuilder_build_state", &default_minimal_json.encode())
.unwrap();
let mut keys = t.into_storages().top.keys().cloned().map(hex).collect::<Vec<String>>();
@@ -1290,21 +1321,60 @@ mod tests {
fn default_config_as_json_works() {
sp_tracing::try_init_simple();
let mut t = BasicExternalities::new_empty();
let r = executor_call(&mut t, "GenesisBuilder_create_default_config", &vec![]).unwrap();
let r = Vec::<u8>::decode(&mut &r[..]).unwrap();
let r = executor_call(&mut t, "GenesisBuilder_get_preset", &None::<&PresetId>.encode())
.unwrap();
let r = Option::<Vec<u8>>::decode(&mut &r[..])
.unwrap()
.expect("default config is there");
let json = String::from_utf8(r.into()).expect("returned value is json. qed.");
let expected = r#"{"system":{},"babe":{"authorities":[],"epochConfig":{"c":[1,4],"allowed_slots":"PrimaryAndSecondaryVRFSlots"}},"substrateTest":{"authorities":[]},"balances":{"balances":[]}}"#;
assert_eq!(expected.to_string(), json);
}
#[test]
fn preset_names_listing_works() {
sp_tracing::try_init_simple();
let mut t = BasicExternalities::new_empty();
let r = executor_call(&mut t, "GenesisBuilder_preset_names", &vec![]).unwrap();
let r = Vec::<PresetId>::decode(&mut &r[..]).unwrap();
assert_eq!(r, vec![PresetId::from("foobar"), PresetId::from("staging"),]);
log::info!("r: {:#?}", r);
}
#[test]
fn named_config_works() {
sp_tracing::try_init_simple();
let f = |cfg_name: &str, expected: &str| {
let mut t = BasicExternalities::new_empty();
let name = cfg_name.to_string();
let r = executor_call(
&mut t,
"GenesisBuilder_get_preset",
&Some(name.as_bytes()).encode(),
)
.unwrap();
let r = Option::<Vec<u8>>::decode(&mut &r[..]).unwrap();
let json =
String::from_utf8(r.unwrap().into()).expect("returned value is json. qed.");
log::info!("json: {:#?}", json);
assert_eq!(expected.to_string(), json);
};
f("foobar", r#"{"foo":"bar"}"#);
f(
"staging",
r#"{"balances":{"balances":[["5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty",1000000000000000],["5FLSigC9HGRKVhB9FiEo4Y3koPsNmBmLJbpXg2mp1hXcS59Y",1000000000000000]]},"substrateTest":{"authorities":["5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY","5CiPPseXPECbkjWCa6MnjNokrgYjMqmKndv2rSnekmSK2DjL"]}}"#,
);
}
#[test]
fn build_config_from_json_works() {
sp_tracing::try_init_simple();
let j = include_str!("../res/default_genesis_config.json");
let mut t = BasicExternalities::new_empty();
let r = executor_call(&mut t, "GenesisBuilder_build_config", &j.encode()).unwrap();
let r = executor_call(&mut t, "GenesisBuilder_build_state", &j.encode()).unwrap();
let r = BuildResult::decode(&mut &r[..]);
assert!(r.is_ok());
@@ -1323,7 +1393,7 @@ mod tests {
sp_tracing::try_init_simple();
let j = include_str!("../res/default_genesis_config_invalid.json");
let mut t = BasicExternalities::new_empty();
let r = executor_call(&mut t, "GenesisBuilder_build_config", &j.encode()).unwrap();
let r = executor_call(&mut t, "GenesisBuilder_build_state", &j.encode()).unwrap();
let r = BuildResult::decode(&mut &r[..]).unwrap();
log::info!("result: {:#?}", r);
assert_eq!(r, Err(
@@ -1338,7 +1408,7 @@ mod tests {
sp_tracing::try_init_simple();
let j = include_str!("../res/default_genesis_config_invalid_2.json");
let mut t = BasicExternalities::new_empty();
let r = executor_call(&mut t, "GenesisBuilder_build_config", &j.encode()).unwrap();
let r = executor_call(&mut t, "GenesisBuilder_build_state", &j.encode()).unwrap();
let r = BuildResult::decode(&mut &r[..]).unwrap();
assert_eq!(r, Err(
sp_runtime::RuntimeString::Owned(
@@ -1353,7 +1423,7 @@ mod tests {
let j = include_str!("../res/default_genesis_config_incomplete.json");
let mut t = BasicExternalities::new_empty();
let r = executor_call(&mut t, "GenesisBuilder_build_config", &j.encode()).unwrap();
let r = executor_call(&mut t, "GenesisBuilder_build_state", &j.encode()).unwrap();
let r =
core::result::Result::<(), sp_runtime::RuntimeString>::decode(&mut &r[..]).unwrap();
assert_eq!(
@@ -1388,8 +1458,11 @@ mod tests {
sp_tracing::try_init_simple();
let mut t = BasicExternalities::new_empty();
let r = executor_call(&mut t, "GenesisBuilder_create_default_config", &vec![]).unwrap();
let r = Vec::<u8>::decode(&mut &r[..]).unwrap();
let r = executor_call(&mut t, "GenesisBuilder_get_preset", &None::<&PresetId>.encode())
.unwrap();
let r = Option::<Vec<u8>>::decode(&mut &r[..])
.unwrap()
.expect("default config is there");
let mut default_config: serde_json::Value =
serde_json::from_slice(&r[..]).expect("returned value is json. qed.");
@@ -1418,7 +1491,7 @@ mod tests {
let mut t = BasicExternalities::new_empty();
executor_call(
&mut t,
"GenesisBuilder_build_config",
"GenesisBuilder_build_state",
&default_config.to_string().encode(),
)
.unwrap();
@@ -1436,8 +1509,8 @@ mod tests {
let authority_key_vec =
Vec::<sp_core::sr25519::Public>::decode(&mut &value[..]).unwrap();
assert_eq!(authority_key_vec.len(), 2);
assert_eq!(authority_key_vec[0], sp_keyring::AccountKeyring::Ferdie.public());
assert_eq!(authority_key_vec[1], sp_keyring::AccountKeyring::Alice.public());
assert_eq!(authority_key_vec[0], AccountKeyring::Ferdie.public());
assert_eq!(authority_key_vec[1], AccountKeyring::Alice.public());
//Babe|Authorities
let value: Vec<u8> = get_from_storage(