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

GenesisBuilder runtime API #14131

Merged
merged 32 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
926f828
GenesisConfigBuilder: preliminary API proposal
michalkucharczyk May 12, 2023
f5b4cbe
fmt
michalkucharczyk May 12, 2023
d24fbba
comment removed
michalkucharczyk May 12, 2023
139341a
build_default_config removed
michalkucharczyk May 15, 2023
587770b
Update client/genesis-builder/src/lib.rs
michalkucharczyk May 16, 2023
78f467b
config -> gensis_config
michalkucharczyk May 16, 2023
75c1f73
GenesisConfigBuilder: helper added
michalkucharczyk May 17, 2023
5756776
moved to primitives
michalkucharczyk May 18, 2023
1b3f8ab
licesne changed to apache-2.0
michalkucharczyk May 18, 2023
0b3e084
Cargo.toml: name/path to genesis-builder updated
michalkucharczyk May 18, 2023
fcd2ee9
helper removed
michalkucharczyk May 18, 2023
bed8d05
Merge remote-tracking branch 'origin/master' into mku-genesis-builder…
michalkucharczyk May 30, 2023
5ae93f6
sp-sd version bumped
michalkucharczyk May 30, 2023
77c6504
Merge remote-tracking branch 'origin/master' into mku-genesis-builder…
May 31, 2023
cc91d46
sp-std bump
michalkucharczyk May 31, 2023
679a970
naming + new function
michalkucharczyk Jun 5, 2023
aa1f77c
fix
michalkucharczyk Jun 6, 2023
43a5d41
build_from_patch_json -> build_with_patch
michalkucharczyk Jun 6, 2023
82bb745
fix
michalkucharczyk Jun 6, 2023
8ca9b44
Merge remote-tracking branch 'origin/master' into mku-genesis-builder…
Jun 6, 2023
f9926bd
Cargo.lock updated
michalkucharczyk Jun 6, 2023
706f3f2
readme: license updated
michalkucharczyk Jun 7, 2023
b35c443
Update primitives/genesis-builder/src/lib.rs
michalkucharczyk Jun 7, 2023
14a251b
Update primitives/genesis-builder/src/lib.rs
michalkucharczyk Jun 7, 2023
2435e42
Update primitives/genesis-builder/Cargo.toml
michalkucharczyk Jun 7, 2023
8b62fcf
Cargo.lock updated
michalkucharczyk Jun 7, 2023
2547761
removed redundant function
michalkucharczyk Jun 8, 2023
a1b64de
GenesisConfigBuilder API: no_defaults function added
michalkucharczyk Jun 21, 2023
750ddb2
Cargo.lock updated
michalkucharczyk Jun 21, 2023
cfbd0d2
Merge remote-tracking branch 'origin/master' into mku-genesis-builder…
Jun 21, 2023
4c615f0
GenesisConfigBuilder API: patching fn removed
michalkucharczyk Jun 26, 2023
b02e21e
trigger CI job
michalkucharczyk Jun 26, 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
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ members = [
"primitives/database",
"primitives/debug-derive",
"primitives/externalities",
"primitives/genesis-builder",
"primitives/inherents",
"primitives/io",
"primitives/keyring",
Expand Down
26 changes: 26 additions & 0 deletions primitives/genesis-builder/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[package]
name = "sp-genesis-builder"
version = "0.1.0-dev"
authors = ["Parity Technologies <[email protected]>"]
edition = "2021"
license = "Apache-2.0"
homepage = "https://substrate.io"
repository = "https://github.com/paritytech/substrate/"
description = "Substrate GenesisConfig builder API"
readme = "README.md"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
sp-api = { version = "4.0.0-dev", default-features = false, path = "../../primitives/api" }
sp-std = { version = "8.0.0", default-features = false, path = "../../primitives/std" }
serde_json = { version = "1.0.85", default-features = false, features = ["alloc"] }

[features]
default = [ "std" ]
std = [
"sp-api/std",
"sp-std/std",
"serde_json/std",
]
5 changes: 5 additions & 0 deletions primitives/genesis-builder/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Substrate genesis builder.

Refer to the module doc for more details.

License: Apache-2.0
80 changes: 80 additions & 0 deletions primitives/genesis-builder/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// This file is part of Substrate.

// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#![cfg_attr(not(feature = "std"), no_std)]

//! Substrate genesis config builder
//!
//! This Runtime API allows to construct `GenesisConfig`, in particular:
//! - serialize the runtime default `GenesisConfig` struct into json format,
//! - put the GenesisConfig struct into the storage. Internally this operation calls
//! `GenesisBuild::build` function
//! for all runtime pallets, which is typically provided by pallet's author.
//! - deserialize the GenesisConfig from given json blob and put GenesisConfig into the state
//! storage. Allows to build
//! customized configuration.
//!
//! Providing externalities with empty storage and putting GenesisConfig into storage allows to
//! catch and build the raw storage of `GenesisConfig` which is the foundation for genesis block.

sp_api::decl_runtime_apis! {
/// API to interact with GenesisConfig for the runtime
pub trait GenesisBuilder {
/// Get the default `GenesisConfig` as a JSON blob.
///
/// This function instantiates the default `GenesisConfig` struct for the runtime and serializes it into
/// a JSON blob.
///
/// # Returns
///
/// A `Vec<u8>` containing the JSON representation of the default `GenesisConfig`.
fn get_default_as_json() -> sp_std::vec::Vec<u8>;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
fn get_default_as_json() -> sp_std::vec::Vec<u8>;
fn create_default_config() -> sp_std::vec::Vec<u8>;


/// Build `GenesisConfig` from a JSON blob and store it in the storage.
///
/// This function deserializes the `GenesisConfig` from the given JSON blob and puts it into the storage.
/// If the provided JSON blob is incorrect or the deserialization fails, this method will panic.
/// It is recommended to log any errors encountered during the process.
///
/// # Arguments
///
/// * `json` - A `Vec<u8>` representing the JSON blob containing the `GenesisConfig`.
fn build_from_json(json: sp_std::vec::Vec<u8>);

/// Patch default `GenesisConfig` using given JSON patch and store it in the storage.
///
/// This function generates the `GenesisConfig` for the runtime by applying a provided JSON patch.
/// The patch modifies the default `GenesisConfig` allowing customization of the specific keys.
/// The resulting `GenesisConfig` is then deserialized from the patched JSON representation and
/// stored in the storage.
///
/// If the provided JSON patch is incorrect or the deserialization fails, this method will panic.
/// It is recommended to log any errors encountered during the process.
///
/// The patching process modifies the default `GenesisConfig` according to the following rules:
///
/// 1. Existing keys in the default configuration will be overridden by the corresponding values in the patch.
/// 2. If a key exists in the patch but not in the default configuration, it will be added to the resulting `GenesisConfig`.
/// 3. Keys in the default configuration that have null values in the patch will be removed from the resulting
/// `GenesisConfig`. This is helpful for changing enum variant value.
///
/// # Arguments
///
/// * `patch_json` - A `Vec<u8>` representing the JSON patch to be applied to the default `GenesisConfig`.
fn build_with_patch(patch_json: sp_std::vec::Vec<u8>);
}
}