Skip to content
Snippets Groups Projects
Verified Commit 699224c8 authored by Michal Kucharczyk's avatar Michal Kucharczyk
Browse files

GenesisBuilder: api: PresetId added

parent c186869f
No related merge requests found
......@@ -23,7 +23,7 @@ extern crate alloc;
use alloc::vec::Vec;
use frame_support::traits::BuildGenesisConfig;
use sp_genesis_builder::Result as BuildResult;
use sp_genesis_builder::{Result as BuildResult, PresetId};
use sp_runtime::format_runtime_string;
/// Build `GenesisConfig` from a JSON blob not using any defaults and store it in the storage. For
......@@ -39,7 +39,7 @@ pub fn build_state<GC: BuildGenesisConfig>(json: Vec<u8>) -> BuildResult {
///
/// No named presets are supported. For more info refer to
/// [`sp_genesis_builder::GenesisBuilder::get_preset`].
pub fn get_preset<GC>(id: Option<Vec<u8>>) -> Option<Vec<u8>>
pub fn get_preset<GC>(id: Option<PresetId>) -> Option<Vec<u8>>
where
GC: BuildGenesisConfig + Default,
{
......
......@@ -16,6 +16,9 @@ workspace = true
targets = ["x86_64-unknown-linux-gnu"]
[dependencies]
codec = { package = "parity-scale-codec", version = "3.6.1", default-features = false, features = ["bytes"] }
scale-info = { version = "2.10.0", default-features = false, features = ["derive"] }
sp-api = { path = "../api", default-features = false }
sp-runtime = { path = "../runtime", default-features = false }
serde_json = { features = ["alloc", "arbitrary_precision"], workspace = true }
......
......@@ -43,10 +43,30 @@
//! genesis block.
extern crate alloc;
use alloc::vec::Vec;
use codec::{Decode, Encode};
use scale_info::TypeInfo;
/// The result type alias, used in build methods. `Err` contains formatted error message.
pub type Result = core::result::Result<(), sp_runtime::RuntimeString>;
/// Wrapper representing preset id.
#[derive(Decode, Encode, Debug, TypeInfo)]
pub struct PresetId(Vec<u8>);
impl From<&str> for PresetId {
fn from(v: &str) -> Self {
Self(v.as_bytes().to_vec())
}
}
impl<'a> TryFrom<&'a PresetId> for &'a str {
type Error = core::str::Utf8Error;
fn try_from(v: &'a PresetId) -> core::result::Result<&'a str, Self::Error> {
core::str::from_utf8(&v.0)
}
}
sp_api::decl_runtime_apis! {
/// API to interact with RuntimeGenesisConfig for the runtime
pub trait GenesisBuilder {
......@@ -59,7 +79,7 @@ sp_api::decl_runtime_apis! {
///
/// Please note that provided json blob must contain all `RuntimeGenesisConfig` fields, no
/// defaults will be used.
fn build_state(json: alloc::vec::Vec<u8>) -> Result;
fn build_state(json: Vec<u8>) -> Result;
/// Returns a JSON blob representation of the built-in `RuntimeGenesisConfig` identified by
/// `id`.
......@@ -71,12 +91,12 @@ sp_api::decl_runtime_apis! {
/// Otherwise function returns a JSON representation of the built-in, named
/// `RuntimeGenesisConfig` preset identified by `id`, or `None` if such preset does not
/// exists. Returned `Vec<u8>` contains bytes of JSON blob.
fn get_preset(id: Option<alloc::vec::Vec<u8>>) -> Option<alloc::vec::Vec<u8>>;
fn get_preset(id: Option<PresetId>) -> Option<Vec<u8>>;
/// Returns a list of names for available builtin `RuntimeGenesisConfig` presets.
///
/// The presets from the list can be queried with [`GenesisBuilder::get_preset`] method. If
/// no named presets are provided by the runtime the list is empty.
fn preset_names() -> alloc::vec::Vec<sp_runtime::RuntimeString>;
fn preset_names() -> Vec<PresetId>;
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment