From 926f82882f75abdb946dcb78c653a93cef1a3af1 Mon Sep 17 00:00:00 2001 From: Michal Kucharczyk <1728078+michalkucharczyk@users.noreply.github.com> Date: Fri, 12 May 2023 15:46:04 +0200 Subject: [PATCH 01/28] GenesisConfigBuilder: preliminary API proposal --- Cargo.toml | 1 + client/genesis-builder/Cargo.toml | 24 ++++++++++++++++ client/genesis-builder/README.md | 3 ++ client/genesis-builder/src/lib.rs | 46 +++++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+) create mode 100644 client/genesis-builder/Cargo.toml create mode 100644 client/genesis-builder/README.md create mode 100644 client/genesis-builder/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index 7563a4a643286..f6f2a2a8d76ab 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -40,6 +40,7 @@ members = [ "client/executor/runtime-test", "client/executor/wasmi", "client/executor/wasmtime", + "client/genesis-builder", "client/informant", "client/keystore", "client/merkle-mountain-range", diff --git a/client/genesis-builder/Cargo.toml b/client/genesis-builder/Cargo.toml new file mode 100644 index 0000000000000..0a54d32ecd640 --- /dev/null +++ b/client/genesis-builder/Cargo.toml @@ -0,0 +1,24 @@ +[package] +name = "sc-genesis-builder" +version = "0.1.0-dev" +authors = ["Parity Technologies "] +edition = "2021" +license = "GPL-3.0-or-later WITH Classpath-exception-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 = "5.0.0", default-features = false, path = "../../primitives/std" } + +[features] +default = [ "std" ] +std = [ + "sp-api/std", + "sp-std/std", +] diff --git a/client/genesis-builder/README.md b/client/genesis-builder/README.md new file mode 100644 index 0000000000000..08caeecc7965d --- /dev/null +++ b/client/genesis-builder/README.md @@ -0,0 +1,3 @@ +Substrate genesis builder + +License: GPL-3.0-or-later WITH Classpath-exception-2.0 diff --git a/client/genesis-builder/src/lib.rs b/client/genesis-builder/src/lib.rs new file mode 100644 index 0000000000000..fb675d4e354f5 --- /dev/null +++ b/client/genesis-builder/src/lib.rs @@ -0,0 +1,46 @@ +// This file is part of Substrate. + +// Copyright (C) Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +//! 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 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. + +#![cfg_attr(not(feature = "std"), no_std)] + +sp_api::decl_runtime_apis! { + /// API to interact with GenesisConfig for the runtime + pub trait GenesisBuilder { + /// Instantiate default `GenesisConfig` and put it to storage. Typically this will be done by means of `GenesisBuild::build` function. + fn build_default_config(); + + // fn default_config_as_json() -> serde_json::Result>; + /// Instantiate default `GenesisConfig` and serializes it to json blob. + fn default_config_as_json() -> sp_std::vec::Vec; + + /// Deserialize the `GenesisConfig` from given json blob and put it into the storage. + fn build_genesis_config_from_json(json: sp_std::vec::Vec); + } +} From f5b4cbe06ce6b541a100143ab83efac8d283fb13 Mon Sep 17 00:00:00 2001 From: Michal Kucharczyk <1728078+michalkucharczyk@users.noreply.github.com> Date: Fri, 12 May 2023 15:51:37 +0200 Subject: [PATCH 02/28] fmt --- client/genesis-builder/src/lib.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/client/genesis-builder/src/lib.rs b/client/genesis-builder/src/lib.rs index fb675d4e354f5..d7327be9d8c8a 100644 --- a/client/genesis-builder/src/lib.rs +++ b/client/genesis-builder/src/lib.rs @@ -20,13 +20,15 @@ //! //! 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 +//! - put the GenesisConfig struct into the storage. Internally this operation calls +//! `GenesisBuild::build` function //! for all runtime pallets, which is typically by pallet's author. -//! - deserialize the GenesisConfig from given json blob and put GenesisConfig into the state storage. Allows to build +//! - 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. +//! 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. #![cfg_attr(not(feature = "std"), no_std)] From d24fbba64b6f454d93e354700d7e24a11ae81286 Mon Sep 17 00:00:00 2001 From: Michal Kucharczyk <1728078+michalkucharczyk@users.noreply.github.com> Date: Fri, 12 May 2023 15:52:17 +0200 Subject: [PATCH 03/28] comment removed --- client/genesis-builder/src/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/client/genesis-builder/src/lib.rs b/client/genesis-builder/src/lib.rs index d7327be9d8c8a..4d12ee7d81412 100644 --- a/client/genesis-builder/src/lib.rs +++ b/client/genesis-builder/src/lib.rs @@ -38,7 +38,6 @@ sp_api::decl_runtime_apis! { /// Instantiate default `GenesisConfig` and put it to storage. Typically this will be done by means of `GenesisBuild::build` function. fn build_default_config(); - // fn default_config_as_json() -> serde_json::Result>; /// Instantiate default `GenesisConfig` and serializes it to json blob. fn default_config_as_json() -> sp_std::vec::Vec; From 139341a53678829d41505f8480a79729e71d1554 Mon Sep 17 00:00:00 2001 From: Michal Kucharczyk <1728078+michalkucharczyk@users.noreply.github.com> Date: Mon, 15 May 2023 20:44:50 +0200 Subject: [PATCH 04/28] build_default_config removed --- client/genesis-builder/src/lib.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/client/genesis-builder/src/lib.rs b/client/genesis-builder/src/lib.rs index 4d12ee7d81412..d439e178bfcf0 100644 --- a/client/genesis-builder/src/lib.rs +++ b/client/genesis-builder/src/lib.rs @@ -35,9 +35,6 @@ sp_api::decl_runtime_apis! { /// API to interact with GenesisConfig for the runtime pub trait GenesisBuilder { - /// Instantiate default `GenesisConfig` and put it to storage. Typically this will be done by means of `GenesisBuild::build` function. - fn build_default_config(); - /// Instantiate default `GenesisConfig` and serializes it to json blob. fn default_config_as_json() -> sp_std::vec::Vec; From 587770b9a1a51fa314732d55f12866a4b8710dfb Mon Sep 17 00:00:00 2001 From: Michal Kucharczyk <1728078+michalkucharczyk@users.noreply.github.com> Date: Tue, 16 May 2023 21:43:10 +0200 Subject: [PATCH 05/28] Update client/genesis-builder/src/lib.rs --- client/genesis-builder/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/genesis-builder/src/lib.rs b/client/genesis-builder/src/lib.rs index d439e178bfcf0..0f1074123d597 100644 --- a/client/genesis-builder/src/lib.rs +++ b/client/genesis-builder/src/lib.rs @@ -22,7 +22,7 @@ //! - 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 by pallet's author. +//! 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. From 78f467b8d54e14061c3c967c501084d025ef068b Mon Sep 17 00:00:00 2001 From: Michal Kucharczyk <1728078+michalkucharczyk@users.noreply.github.com> Date: Tue, 16 May 2023 21:47:45 +0200 Subject: [PATCH 06/28] config -> gensis_config --- client/genesis-builder/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/genesis-builder/src/lib.rs b/client/genesis-builder/src/lib.rs index 0f1074123d597..8cf08ad1fa6f8 100644 --- a/client/genesis-builder/src/lib.rs +++ b/client/genesis-builder/src/lib.rs @@ -36,7 +36,7 @@ sp_api::decl_runtime_apis! { /// API to interact with GenesisConfig for the runtime pub trait GenesisBuilder { /// Instantiate default `GenesisConfig` and serializes it to json blob. - fn default_config_as_json() -> sp_std::vec::Vec; + fn default_genesis_config_as_json() -> sp_std::vec::Vec; /// Deserialize the `GenesisConfig` from given json blob and put it into the storage. fn build_genesis_config_from_json(json: sp_std::vec::Vec); From 75c1f73f8b2830f0f4e50669502d5afade264fd1 Mon Sep 17 00:00:00 2001 From: Michal Kucharczyk <1728078+michalkucharczyk@users.noreply.github.com> Date: Wed, 17 May 2023 15:02:15 +0200 Subject: [PATCH 07/28] GenesisConfigBuilder: helper added --- client/genesis-builder/Cargo.toml | 3 ++ client/genesis-builder/src/api.rs | 42 ++++++++++++++++++++++++++++ client/genesis-builder/src/helper.rs | 40 ++++++++++++++++++++++++++ client/genesis-builder/src/lib.rs | 26 ++--------------- 4 files changed, 87 insertions(+), 24 deletions(-) create mode 100644 client/genesis-builder/src/api.rs create mode 100644 client/genesis-builder/src/helper.rs diff --git a/client/genesis-builder/Cargo.toml b/client/genesis-builder/Cargo.toml index 0a54d32ecd640..cc47743d45968 100644 --- a/client/genesis-builder/Cargo.toml +++ b/client/genesis-builder/Cargo.toml @@ -15,10 +15,13 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] sp-api = { version = "4.0.0-dev", default-features = false, path = "../../primitives/api" } sp-std = { version = "5.0.0", default-features = false, path = "../../primitives/std" } +serde_json = { version = "1.0.85", default-features = false, features = ["alloc"] } +frame-support = { version = "4.0.0-dev", default-features = false, path = "../../frame/support" } [features] default = [ "std" ] std = [ "sp-api/std", "sp-std/std", + "serde_json/std", ] diff --git a/client/genesis-builder/src/api.rs b/client/genesis-builder/src/api.rs new file mode 100644 index 0000000000000..5fb2f6eb192c1 --- /dev/null +++ b/client/genesis-builder/src/api.rs @@ -0,0 +1,42 @@ +// This file is part of Substrate. + +// Copyright (C) Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +//! 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 { + /// Instantiate default `GenesisConfig` and serializes it to json blob. + fn default_genesis_config_as_json() -> sp_std::vec::Vec; + + /// Deserialize the `GenesisConfig` from given json blob and put it into the storage. + fn build_genesis_config_from_json(json: sp_std::vec::Vec); + } +} diff --git a/client/genesis-builder/src/helper.rs b/client/genesis-builder/src/helper.rs new file mode 100644 index 0000000000000..48fd66b809319 --- /dev/null +++ b/client/genesis-builder/src/helper.rs @@ -0,0 +1,40 @@ +// This file is part of Substrate. + +// Copyright (C) Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +//! Generic implementation of genesis config builder + +use frame_support::traits::GenesisBuild; + +pub struct GenesisBuilder(sp_std::marker::PhantomData<(R, GC)>); + +impl GenesisBuilder +where + GC: Default + GenesisBuild, +{ + pub fn default_genesis_config_as_json() -> sp_std::vec::Vec { + serde_json::to_string(&GC::default()) + .expect("serialization to json is expected to work. qed.") + .into_bytes() + } + + pub fn build_genesis_config_from_json(json: sp_std::vec::Vec) { + let gc = serde_json::from_slice::(&json) + .expect("provided json blob is expected to be valid. qed."); + >::build(&gc); + } +} diff --git a/client/genesis-builder/src/lib.rs b/client/genesis-builder/src/lib.rs index 8cf08ad1fa6f8..d0b87aa070dc5 100644 --- a/client/genesis-builder/src/lib.rs +++ b/client/genesis-builder/src/lib.rs @@ -16,29 +16,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -//! 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. - #![cfg_attr(not(feature = "std"), no_std)] -sp_api::decl_runtime_apis! { - /// API to interact with GenesisConfig for the runtime - pub trait GenesisBuilder { - /// Instantiate default `GenesisConfig` and serializes it to json blob. - fn default_genesis_config_as_json() -> sp_std::vec::Vec; - - /// Deserialize the `GenesisConfig` from given json blob and put it into the storage. - fn build_genesis_config_from_json(json: sp_std::vec::Vec); - } -} +pub mod api; +pub mod helper; From 57567767071fd08f2bfa2de546d8d2ee8c31bf85 Mon Sep 17 00:00:00 2001 From: Michal Kucharczyk <1728078+michalkucharczyk@users.noreply.github.com> Date: Thu, 18 May 2023 07:30:20 +0200 Subject: [PATCH 08/28] moved to primitives --- {client => primitives}/genesis-builder/Cargo.toml | 0 {client => primitives}/genesis-builder/README.md | 0 {client => primitives}/genesis-builder/src/api.rs | 0 {client => primitives}/genesis-builder/src/helper.rs | 0 {client => primitives}/genesis-builder/src/lib.rs | 0 5 files changed, 0 insertions(+), 0 deletions(-) rename {client => primitives}/genesis-builder/Cargo.toml (100%) rename {client => primitives}/genesis-builder/README.md (100%) rename {client => primitives}/genesis-builder/src/api.rs (100%) rename {client => primitives}/genesis-builder/src/helper.rs (100%) rename {client => primitives}/genesis-builder/src/lib.rs (100%) diff --git a/client/genesis-builder/Cargo.toml b/primitives/genesis-builder/Cargo.toml similarity index 100% rename from client/genesis-builder/Cargo.toml rename to primitives/genesis-builder/Cargo.toml diff --git a/client/genesis-builder/README.md b/primitives/genesis-builder/README.md similarity index 100% rename from client/genesis-builder/README.md rename to primitives/genesis-builder/README.md diff --git a/client/genesis-builder/src/api.rs b/primitives/genesis-builder/src/api.rs similarity index 100% rename from client/genesis-builder/src/api.rs rename to primitives/genesis-builder/src/api.rs diff --git a/client/genesis-builder/src/helper.rs b/primitives/genesis-builder/src/helper.rs similarity index 100% rename from client/genesis-builder/src/helper.rs rename to primitives/genesis-builder/src/helper.rs diff --git a/client/genesis-builder/src/lib.rs b/primitives/genesis-builder/src/lib.rs similarity index 100% rename from client/genesis-builder/src/lib.rs rename to primitives/genesis-builder/src/lib.rs From 1b3f8abd3f29d2a2c6c2029a3d842cf7df519431 Mon Sep 17 00:00:00 2001 From: Michal Kucharczyk <1728078+michalkucharczyk@users.noreply.github.com> Date: Thu, 18 May 2023 07:32:27 +0200 Subject: [PATCH 09/28] licesne changed to apache-2.0 --- primitives/genesis-builder/Cargo.toml | 2 +- primitives/genesis-builder/src/api.rs | 25 +++++++++++----------- primitives/genesis-builder/src/helper.rs | 27 ++++++++++++------------ primitives/genesis-builder/src/lib.rs | 25 +++++++++++----------- 4 files changed, 38 insertions(+), 41 deletions(-) diff --git a/primitives/genesis-builder/Cargo.toml b/primitives/genesis-builder/Cargo.toml index cc47743d45968..eae04cc3a2dc5 100644 --- a/primitives/genesis-builder/Cargo.toml +++ b/primitives/genesis-builder/Cargo.toml @@ -3,7 +3,7 @@ name = "sc-genesis-builder" version = "0.1.0-dev" authors = ["Parity Technologies "] edition = "2021" -license = "GPL-3.0-or-later WITH Classpath-exception-2.0" +license = "Apache-2.0" homepage = "https://substrate.io" repository = "https://github.com/paritytech/substrate/" description = "Substrate GenesisConfig builder API" diff --git a/primitives/genesis-builder/src/api.rs b/primitives/genesis-builder/src/api.rs index 5fb2f6eb192c1..66e198c634519 100644 --- a/primitives/genesis-builder/src/api.rs +++ b/primitives/genesis-builder/src/api.rs @@ -1,20 +1,19 @@ // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. -// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 +// SPDX-License-Identifier: Apache-2.0 -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. - -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. - -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . +// 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. //! Substrate genesis config builder //! diff --git a/primitives/genesis-builder/src/helper.rs b/primitives/genesis-builder/src/helper.rs index 48fd66b809319..592b1944d8a27 100644 --- a/primitives/genesis-builder/src/helper.rs +++ b/primitives/genesis-builder/src/helper.rs @@ -1,20 +1,19 @@ // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. -// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 - -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. - -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. - -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . +// 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. //! Generic implementation of genesis config builder diff --git a/primitives/genesis-builder/src/lib.rs b/primitives/genesis-builder/src/lib.rs index d0b87aa070dc5..6572c93141e1d 100644 --- a/primitives/genesis-builder/src/lib.rs +++ b/primitives/genesis-builder/src/lib.rs @@ -1,20 +1,19 @@ // This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. -// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 +// SPDX-License-Identifier: Apache-2.0 -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. - -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. - -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . +// 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)] From 0b3e08476640e9de543368aa8378742638cac806 Mon Sep 17 00:00:00 2001 From: Michal Kucharczyk <1728078+michalkucharczyk@users.noreply.github.com> Date: Thu, 18 May 2023 07:48:05 +0200 Subject: [PATCH 10/28] Cargo.toml: name/path to genesis-builder updated --- Cargo.toml | 2 +- primitives/genesis-builder/Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f6f2a2a8d76ab..c4e06e81d8717 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -40,7 +40,6 @@ members = [ "client/executor/runtime-test", "client/executor/wasmi", "client/executor/wasmtime", - "client/genesis-builder", "client/informant", "client/keystore", "client/merkle-mountain-range", @@ -202,6 +201,7 @@ members = [ "primitives/database", "primitives/debug-derive", "primitives/externalities", + "primitives/genesis-builder", "primitives/inherents", "primitives/io", "primitives/keyring", diff --git a/primitives/genesis-builder/Cargo.toml b/primitives/genesis-builder/Cargo.toml index eae04cc3a2dc5..35c3e89e05093 100644 --- a/primitives/genesis-builder/Cargo.toml +++ b/primitives/genesis-builder/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "sc-genesis-builder" +name = "sp-genesis-builder" version = "0.1.0-dev" authors = ["Parity Technologies "] edition = "2021" From fcd2ee997dca21cf9c4f5dd6b4d403e58ace9f18 Mon Sep 17 00:00:00 2001 From: Michal Kucharczyk <1728078+michalkucharczyk@users.noreply.github.com> Date: Thu, 18 May 2023 13:10:52 +0200 Subject: [PATCH 11/28] helper removed --- primitives/genesis-builder/Cargo.toml | 1 - primitives/genesis-builder/src/api.rs | 41 ------------------------ primitives/genesis-builder/src/helper.rs | 39 ---------------------- primitives/genesis-builder/src/lib.rs | 26 +++++++++++++-- 4 files changed, 24 insertions(+), 83 deletions(-) delete mode 100644 primitives/genesis-builder/src/api.rs delete mode 100644 primitives/genesis-builder/src/helper.rs diff --git a/primitives/genesis-builder/Cargo.toml b/primitives/genesis-builder/Cargo.toml index 35c3e89e05093..644a5dc6ac229 100644 --- a/primitives/genesis-builder/Cargo.toml +++ b/primitives/genesis-builder/Cargo.toml @@ -16,7 +16,6 @@ targets = ["x86_64-unknown-linux-gnu"] sp-api = { version = "4.0.0-dev", default-features = false, path = "../../primitives/api" } sp-std = { version = "5.0.0", default-features = false, path = "../../primitives/std" } serde_json = { version = "1.0.85", default-features = false, features = ["alloc"] } -frame-support = { version = "4.0.0-dev", default-features = false, path = "../../frame/support" } [features] default = [ "std" ] diff --git a/primitives/genesis-builder/src/api.rs b/primitives/genesis-builder/src/api.rs deleted file mode 100644 index 66e198c634519..0000000000000 --- a/primitives/genesis-builder/src/api.rs +++ /dev/null @@ -1,41 +0,0 @@ -// 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. - -//! 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 { - /// Instantiate default `GenesisConfig` and serializes it to json blob. - fn default_genesis_config_as_json() -> sp_std::vec::Vec; - - /// Deserialize the `GenesisConfig` from given json blob and put it into the storage. - fn build_genesis_config_from_json(json: sp_std::vec::Vec); - } -} diff --git a/primitives/genesis-builder/src/helper.rs b/primitives/genesis-builder/src/helper.rs deleted file mode 100644 index 592b1944d8a27..0000000000000 --- a/primitives/genesis-builder/src/helper.rs +++ /dev/null @@ -1,39 +0,0 @@ -// 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. - -//! Generic implementation of genesis config builder - -use frame_support::traits::GenesisBuild; - -pub struct GenesisBuilder(sp_std::marker::PhantomData<(R, GC)>); - -impl GenesisBuilder -where - GC: Default + GenesisBuild, -{ - pub fn default_genesis_config_as_json() -> sp_std::vec::Vec { - serde_json::to_string(&GC::default()) - .expect("serialization to json is expected to work. qed.") - .into_bytes() - } - - pub fn build_genesis_config_from_json(json: sp_std::vec::Vec) { - let gc = serde_json::from_slice::(&json) - .expect("provided json blob is expected to be valid. qed."); - >::build(&gc); - } -} diff --git a/primitives/genesis-builder/src/lib.rs b/primitives/genesis-builder/src/lib.rs index 6572c93141e1d..9e8b929bbe424 100644 --- a/primitives/genesis-builder/src/lib.rs +++ b/primitives/genesis-builder/src/lib.rs @@ -17,5 +17,27 @@ #![cfg_attr(not(feature = "std"), no_std)] -pub mod api; -pub mod helper; +//! 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 { + /// Instantiate default `GenesisConfig` and serializes it to json blob. + fn default_genesis_config_as_json() -> sp_std::vec::Vec; + + /// Deserialize the `GenesisConfig` from given json blob and put it into the storage. + fn build_genesis_config_from_json(json: sp_std::vec::Vec); + } +} From 5ae93f6f5480bce41ee91e2d573818762f4f7b62 Mon Sep 17 00:00:00 2001 From: Michal Kucharczyk <1728078+michalkucharczyk@users.noreply.github.com> Date: Tue, 30 May 2023 13:47:59 +0200 Subject: [PATCH 12/28] sp-sd version bumped --- primitives/genesis-builder/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/primitives/genesis-builder/Cargo.toml b/primitives/genesis-builder/Cargo.toml index 644a5dc6ac229..e8616379ce689 100644 --- a/primitives/genesis-builder/Cargo.toml +++ b/primitives/genesis-builder/Cargo.toml @@ -14,7 +14,7 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] sp-api = { version = "4.0.0-dev", default-features = false, path = "../../primitives/api" } -sp-std = { version = "5.0.0", default-features = false, path = "../../primitives/std" } +sp-std = { version = "6.0.0", default-features = false, path = "../../primitives/std" } serde_json = { version = "1.0.85", default-features = false, features = ["alloc"] } [features] From cc91d46176e8504689d906e5a089c92e6e0e65b5 Mon Sep 17 00:00:00 2001 From: Michal Kucharczyk <1728078+michalkucharczyk@users.noreply.github.com> Date: Wed, 31 May 2023 17:53:00 +0200 Subject: [PATCH 13/28] sp-std bump --- primitives/genesis-builder/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/primitives/genesis-builder/Cargo.toml b/primitives/genesis-builder/Cargo.toml index e8616379ce689..518e8e4e2f1fa 100644 --- a/primitives/genesis-builder/Cargo.toml +++ b/primitives/genesis-builder/Cargo.toml @@ -14,7 +14,7 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] sp-api = { version = "4.0.0-dev", default-features = false, path = "../../primitives/api" } -sp-std = { version = "6.0.0", default-features = false, path = "../../primitives/std" } +sp-std = { version = "8.0.0", default-features = false, path = "../../primitives/std" } serde_json = { version = "1.0.85", default-features = false, features = ["alloc"] } [features] From 679a970e93b09aaccabba32976ad03ad137949a6 Mon Sep 17 00:00:00 2001 From: Michal Kucharczyk <1728078+michalkucharczyk@users.noreply.github.com> Date: Mon, 5 Jun 2023 12:30:03 +0200 Subject: [PATCH 14/28] naming + new function --- primitives/genesis-builder/src/lib.rs | 45 ++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/primitives/genesis-builder/src/lib.rs b/primitives/genesis-builder/src/lib.rs index 9e8b929bbe424..5d31168746e89 100644 --- a/primitives/genesis-builder/src/lib.rs +++ b/primitives/genesis-builder/src/lib.rs @@ -34,10 +34,47 @@ sp_api::decl_runtime_apis! { /// API to interact with GenesisConfig for the runtime pub trait GenesisBuilder { - /// Instantiate default `GenesisConfig` and serializes it to json blob. - fn default_genesis_config_as_json() -> sp_std::vec::Vec; + /// 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` containing the JSON representation of the default `GenesisConfig`. + fn get_default_as_json() -> sp_std::vec::Vec; - /// Deserialize the `GenesisConfig` from given json blob and put it into the storage. - fn build_genesis_config_from_json(json: sp_std::vec::Vec); + /// 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` representing the JSON blob containing the `GenesisConfig`. + fn build_from_json(json: sp_std::vec::Vec); + + /// 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` representing the JSON patch to be applied to the `GenesisConfig`. + fn build_from_patch_json(patch_json: Vec); } } From aa1f77c44889f0e986c4cf1a80bdf0af417222bc Mon Sep 17 00:00:00 2001 From: Michal Kucharczyk <1728078+michalkucharczyk@users.noreply.github.com> Date: Tue, 6 Jun 2023 15:55:48 +0200 Subject: [PATCH 15/28] fix --- primitives/genesis-builder/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/primitives/genesis-builder/src/lib.rs b/primitives/genesis-builder/src/lib.rs index 5d31168746e89..7ee05385e7473 100644 --- a/primitives/genesis-builder/src/lib.rs +++ b/primitives/genesis-builder/src/lib.rs @@ -74,7 +74,7 @@ sp_api::decl_runtime_apis! { /// /// # Arguments /// - /// * `patch_json` - A `Vec` representing the JSON patch to be applied to the `GenesisConfig`. + /// * `patch_json` - A `Vec` representing the JSON patch to be applied to the default `GenesisConfig`. fn build_from_patch_json(patch_json: Vec); } } From 43a5d41a7227bc1686f89deccb8cdd07613dc01d Mon Sep 17 00:00:00 2001 From: Michal Kucharczyk <1728078+michalkucharczyk@users.noreply.github.com> Date: Tue, 6 Jun 2023 17:11:00 +0200 Subject: [PATCH 16/28] build_from_patch_json -> build_with_patch --- primitives/genesis-builder/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/primitives/genesis-builder/src/lib.rs b/primitives/genesis-builder/src/lib.rs index 7ee05385e7473..4cb8ecaa6a064 100644 --- a/primitives/genesis-builder/src/lib.rs +++ b/primitives/genesis-builder/src/lib.rs @@ -75,6 +75,6 @@ sp_api::decl_runtime_apis! { /// # Arguments /// /// * `patch_json` - A `Vec` representing the JSON patch to be applied to the default `GenesisConfig`. - fn build_from_patch_json(patch_json: Vec); + fn build_with_patch(patch_json: Vec); } } From 82bb745fa50884d68b13b9033de1dbe61a9c39f0 Mon Sep 17 00:00:00 2001 From: Michal Kucharczyk <1728078+michalkucharczyk@users.noreply.github.com> Date: Tue, 6 Jun 2023 17:39:23 +0200 Subject: [PATCH 17/28] fix --- primitives/genesis-builder/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/primitives/genesis-builder/src/lib.rs b/primitives/genesis-builder/src/lib.rs index 4cb8ecaa6a064..d3b23d1079833 100644 --- a/primitives/genesis-builder/src/lib.rs +++ b/primitives/genesis-builder/src/lib.rs @@ -75,6 +75,6 @@ sp_api::decl_runtime_apis! { /// # Arguments /// /// * `patch_json` - A `Vec` representing the JSON patch to be applied to the default `GenesisConfig`. - fn build_with_patch(patch_json: Vec); + fn build_with_patch(patch_json: sp_std::vec::Vec); } } From f9926bd132e94f92529ff07c1ffa87ccf8415073 Mon Sep 17 00:00:00 2001 From: Michal Kucharczyk <1728078+michalkucharczyk@users.noreply.github.com> Date: Tue, 6 Jun 2023 19:57:42 +0200 Subject: [PATCH 18/28] Cargo.lock updated --- Cargo.lock | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index fd12cb510933e..0ee912c77ca34 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11279,6 +11279,15 @@ dependencies = [ "sp-storage", ] +[[package]] +name = "sp-genesis-builder" +version = "0.1.0-dev" +dependencies = [ + "serde_json", + "sp-api", + "sp-std", +] + [[package]] name = "sp-inherents" version = "4.0.0-dev" From 706f3f2ba2de9888d361b179edcd816f926d89d0 Mon Sep 17 00:00:00 2001 From: Michal Kucharczyk <1728078+michalkucharczyk@users.noreply.github.com> Date: Wed, 7 Jun 2023 13:20:46 +0200 Subject: [PATCH 19/28] readme: license updated --- primitives/genesis-builder/README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/primitives/genesis-builder/README.md b/primitives/genesis-builder/README.md index 08caeecc7965d..4a842c95e358e 100644 --- a/primitives/genesis-builder/README.md +++ b/primitives/genesis-builder/README.md @@ -1,3 +1,5 @@ -Substrate genesis builder +Substrate genesis builder. -License: GPL-3.0-or-later WITH Classpath-exception-2.0 +Refer to the module doc for more details. + +License: Apache-2.0 From b35c443a6a822cbf09a3fdd3ca8ec971babeb980 Mon Sep 17 00:00:00 2001 From: Michal Kucharczyk <1728078+michalkucharczyk@users.noreply.github.com> Date: Wed, 7 Jun 2023 15:37:45 +0200 Subject: [PATCH 20/28] Update primitives/genesis-builder/src/lib.rs Co-authored-by: Davide Galassi --- primitives/genesis-builder/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/primitives/genesis-builder/src/lib.rs b/primitives/genesis-builder/src/lib.rs index d3b23d1079833..c13bb1c284b5c 100644 --- a/primitives/genesis-builder/src/lib.rs +++ b/primitives/genesis-builder/src/lib.rs @@ -28,7 +28,7 @@ //! storage. Allows to build //! customized configuration. //! -//! Providing externalities with empty storage and putting GenesisConfig into storage allows to +//! 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! { From 14a251bbfeff89e18f28ea1525c22ee69f0e0685 Mon Sep 17 00:00:00 2001 From: Michal Kucharczyk <1728078+michalkucharczyk@users.noreply.github.com> Date: Wed, 7 Jun 2023 15:37:55 +0200 Subject: [PATCH 21/28] Update primitives/genesis-builder/src/lib.rs Co-authored-by: Davide Galassi --- primitives/genesis-builder/src/lib.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/primitives/genesis-builder/src/lib.rs b/primitives/genesis-builder/src/lib.rs index c13bb1c284b5c..2ebb169d639b5 100644 --- a/primitives/genesis-builder/src/lib.rs +++ b/primitives/genesis-builder/src/lib.rs @@ -22,11 +22,10 @@ //! 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. +//! `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. From 2435e4278a96ba14f0437503a178c5817d0841ca Mon Sep 17 00:00:00 2001 From: Michal Kucharczyk <1728078+michalkucharczyk@users.noreply.github.com> Date: Wed, 7 Jun 2023 15:38:09 +0200 Subject: [PATCH 22/28] Update primitives/genesis-builder/Cargo.toml Co-authored-by: Davide Galassi --- primitives/genesis-builder/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/primitives/genesis-builder/Cargo.toml b/primitives/genesis-builder/Cargo.toml index 518e8e4e2f1fa..35b0279bf31f0 100644 --- a/primitives/genesis-builder/Cargo.toml +++ b/primitives/genesis-builder/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sp-genesis-builder" -version = "0.1.0-dev" +version = "0.1.0" authors = ["Parity Technologies "] edition = "2021" license = "Apache-2.0" From 8b62fcffa78aae5829fa4595943a479c82d2ca8a Mon Sep 17 00:00:00 2001 From: Michal Kucharczyk <1728078+michalkucharczyk@users.noreply.github.com> Date: Wed, 7 Jun 2023 15:43:07 +0200 Subject: [PATCH 23/28] Cargo.lock updated --- Cargo.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 0ee912c77ca34..05f47fdf9b5ac 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11281,7 +11281,7 @@ dependencies = [ [[package]] name = "sp-genesis-builder" -version = "0.1.0-dev" +version = "0.1.0" dependencies = [ "serde_json", "sp-api", From 254776103e172630efd200cee13aecbc2a032e70 Mon Sep 17 00:00:00 2001 From: Michal Kucharczyk <1728078+michalkucharczyk@users.noreply.github.com> Date: Thu, 8 Jun 2023 09:05:01 +0200 Subject: [PATCH 24/28] removed redundant function --- primitives/genesis-builder/src/lib.rs | 32 ++++++--------------------- 1 file changed, 7 insertions(+), 25 deletions(-) diff --git a/primitives/genesis-builder/src/lib.rs b/primitives/genesis-builder/src/lib.rs index 2ebb169d639b5..b6cedec1fa1ae 100644 --- a/primitives/genesis-builder/src/lib.rs +++ b/primitives/genesis-builder/src/lib.rs @@ -22,8 +22,8 @@ //! 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. +//! `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. //! @@ -35,25 +35,10 @@ sp_api::decl_runtime_apis! { 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` containing the JSON representation of the default `GenesisConfig`. + /// This function instantiates the default `GenesisConfig` struct for the runtime and serializes it into a JSON + /// blob. It returns a `Vec` containing the JSON representation of the default `GenesisConfig`. fn get_default_as_json() -> sp_std::vec::Vec; - /// 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` representing the JSON blob containing the `GenesisConfig`. - fn build_from_json(json: sp_std::vec::Vec); - /// 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. @@ -62,18 +47,15 @@ sp_api::decl_runtime_apis! { /// 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. + /// Any errors encountered during this process should be logged. /// /// 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` representing the JSON patch to be applied to the default `GenesisConfig`. - fn build_with_patch(patch_json: sp_std::vec::Vec); + /// Please note the patch may contain full `GenesisConfig`. + fn build_config(patch: sp_std::vec::Vec); } } From a1b64de9c8e1ded549e0ec61b1afe84f7373eac7 Mon Sep 17 00:00:00 2001 From: Michal Kucharczyk <1728078+michalkucharczyk@users.noreply.github.com> Date: Wed, 21 Jun 2023 14:26:18 +0200 Subject: [PATCH 25/28] GenesisConfigBuilder API: no_defaults function added --- primitives/genesis-builder/Cargo.toml | 1 + primitives/genesis-builder/src/lib.rs | 20 ++++++++++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/primitives/genesis-builder/Cargo.toml b/primitives/genesis-builder/Cargo.toml index 35b0279bf31f0..de543f0a74acf 100644 --- a/primitives/genesis-builder/Cargo.toml +++ b/primitives/genesis-builder/Cargo.toml @@ -14,6 +14,7 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] sp-api = { version = "4.0.0-dev", default-features = false, path = "../../primitives/api" } +sp-runtime = { version = "24.0.0", default-features = false, path = "../../primitives/runtime" } sp-std = { version = "8.0.0", default-features = false, path = "../../primitives/std" } serde_json = { version = "1.0.85", default-features = false, features = ["alloc"] } diff --git a/primitives/genesis-builder/src/lib.rs b/primitives/genesis-builder/src/lib.rs index b6cedec1fa1ae..66c642cdefe7b 100644 --- a/primitives/genesis-builder/src/lib.rs +++ b/primitives/genesis-builder/src/lib.rs @@ -30,6 +30,9 @@ //! 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. +/// The result type alias, used in build methods. `Err` contains formatted error message. +pub type Result = core::result::Result<(), sp_runtime::RuntimeString>; + sp_api::decl_runtime_apis! { /// API to interact with GenesisConfig for the runtime pub trait GenesisBuilder { @@ -46,8 +49,8 @@ sp_api::decl_runtime_apis! { /// 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. - /// Any errors encountered during this process should be logged. + /// If the provided JSON patch is incorrect or the deserialization fails the error will be returned. + /// Method may panic if creation of json representation of default config fails. /// /// 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. @@ -55,7 +58,16 @@ sp_api::decl_runtime_apis! { /// 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. /// - /// Please note the patch may contain full `GenesisConfig`. - fn build_config(patch: sp_std::vec::Vec); + /// Please note that the patch may contain full `GenesisConfig`. + fn build_config(patch: sp_std::vec::Vec) -> Result; + + /// Build `GenesisConfig` from a JSON blob not using any defaults and store it in the storage. + /// + /// This function deserializes the full `GenesisConfig` from the given JSON blob and puts it into the storage. + /// If the provided JSON blob is incorrect or incomplete or the deserialization fails, an error is returned. + /// It is recommended to log any errors encountered during the process. + /// + /// Please note that provided json blob must contain all `GenesisConfig` fields, no defaults will be used. + fn build_config_no_defaults(json: sp_std::vec::Vec) -> Result; } } From 750ddb2502735e695813b9e65f24311a02c6f11d Mon Sep 17 00:00:00 2001 From: Michal Kucharczyk <1728078+michalkucharczyk@users.noreply.github.com> Date: Wed, 21 Jun 2023 15:09:25 +0200 Subject: [PATCH 26/28] Cargo.lock updated --- Cargo.lock | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.lock b/Cargo.lock index 05f47fdf9b5ac..da646875ac798 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11285,6 +11285,7 @@ version = "0.1.0" dependencies = [ "serde_json", "sp-api", + "sp-runtime", "sp-std", ] From 4c615f0d5ea1e7cdef9a10658886446273e1b699 Mon Sep 17 00:00:00 2001 From: Michal Kucharczyk <1728078+michalkucharczyk@users.noreply.github.com> Date: Mon, 26 Jun 2023 09:29:45 +0200 Subject: [PATCH 27/28] GenesisConfigBuilder API: patching fn removed --- primitives/genesis-builder/src/lib.rs | 25 +++---------------------- 1 file changed, 3 insertions(+), 22 deletions(-) diff --git a/primitives/genesis-builder/src/lib.rs b/primitives/genesis-builder/src/lib.rs index 66c642cdefe7b..e002cd3aa6f70 100644 --- a/primitives/genesis-builder/src/lib.rs +++ b/primitives/genesis-builder/src/lib.rs @@ -36,30 +36,11 @@ pub type Result = core::result::Result<(), sp_runtime::RuntimeString>; sp_api::decl_runtime_apis! { /// API to interact with GenesisConfig for the runtime pub trait GenesisBuilder { - /// Get the default `GenesisConfig` as a JSON blob. + /// Creates the default `GenesisConfig` and returns it as a JSON blob. /// /// This function instantiates the default `GenesisConfig` struct for the runtime and serializes it into a JSON /// blob. It returns a `Vec` containing the JSON representation of the default `GenesisConfig`. - fn get_default_as_json() -> sp_std::vec::Vec; - - /// 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 the error will be returned. - /// Method may panic if creation of json representation of default config fails. - /// - /// 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. - /// - /// Please note that the patch may contain full `GenesisConfig`. - fn build_config(patch: sp_std::vec::Vec) -> Result; + fn create_default_config() -> sp_std::vec::Vec; /// Build `GenesisConfig` from a JSON blob not using any defaults and store it in the storage. /// @@ -68,6 +49,6 @@ sp_api::decl_runtime_apis! { /// It is recommended to log any errors encountered during the process. /// /// Please note that provided json blob must contain all `GenesisConfig` fields, no defaults will be used. - fn build_config_no_defaults(json: sp_std::vec::Vec) -> Result; + fn build_config(json: sp_std::vec::Vec) -> Result; } } From b02e21e0048995ff92d828c66484bfb781b5dd40 Mon Sep 17 00:00:00 2001 From: Michal Kucharczyk <1728078+michalkucharczyk@users.noreply.github.com> Date: Mon, 26 Jun 2023 12:56:47 +0200 Subject: [PATCH 28/28] trigger CI job