mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-12 17:01:09 +00:00
Allow creating a ChainSpec from a Vec<u8> (#3233)
* Allow creating a ChainSpec from a Vec<u8> * Line widths * Embedded -> Binary * Remove from_embedded
This commit is contained in:
committed by
Bastian Köcher
parent
7658eb6b88
commit
20eb52d6bb
@@ -16,6 +16,7 @@
|
||||
|
||||
//! Substrate chain configurations.
|
||||
|
||||
use std::borrow::Cow;
|
||||
use std::collections::HashMap;
|
||||
use std::fs::File;
|
||||
use std::path::PathBuf;
|
||||
@@ -29,7 +30,7 @@ use tel::TelemetryEndpoints;
|
||||
|
||||
enum GenesisSource<G> {
|
||||
File(PathBuf),
|
||||
Embedded(&'static [u8]),
|
||||
Binary(Cow<'static, [u8]>),
|
||||
Factory(fn() -> G),
|
||||
}
|
||||
|
||||
@@ -37,7 +38,7 @@ impl<G: RuntimeGenesis> Clone for GenesisSource<G> {
|
||||
fn clone(&self) -> Self {
|
||||
match *self {
|
||||
GenesisSource::File(ref path) => GenesisSource::File(path.clone()),
|
||||
GenesisSource::Embedded(d) => GenesisSource::Embedded(d),
|
||||
GenesisSource::Binary(ref d) => GenesisSource::Binary(d.clone()),
|
||||
GenesisSource::Factory(f) => GenesisSource::Factory(f),
|
||||
}
|
||||
}
|
||||
@@ -50,14 +51,16 @@ impl<G: RuntimeGenesis> GenesisSource<G> {
|
||||
genesis: Genesis<G>,
|
||||
}
|
||||
|
||||
match *self {
|
||||
GenesisSource::File(ref path) => {
|
||||
match self {
|
||||
GenesisSource::File(path) => {
|
||||
let file = File::open(path).map_err(|e| format!("Error opening spec file: {}", e))?;
|
||||
let genesis: GenesisContainer<G> = json::from_reader(file).map_err(|e| format!("Error parsing spec file: {}", e))?;
|
||||
let genesis: GenesisContainer<G> =
|
||||
json::from_reader(file).map_err(|e| format!("Error parsing spec file: {}", e))?;
|
||||
Ok(genesis.genesis)
|
||||
},
|
||||
GenesisSource::Embedded(buf) => {
|
||||
let genesis: GenesisContainer<G> = json::from_reader(buf).map_err(|e| format!("Error parsing embedded file: {}", e))?;
|
||||
GenesisSource::Binary(buf) => {
|
||||
let genesis: GenesisContainer<G> =
|
||||
json::from_reader(buf.as_ref()).map_err(|e| format!("Error parsing embedded file: {}", e))?;
|
||||
Ok(genesis.genesis)
|
||||
},
|
||||
GenesisSource::Factory(f) => Ok(Genesis::Runtime(f())),
|
||||
@@ -158,11 +161,12 @@ impl<G: RuntimeGenesis> ChainSpec<G> {
|
||||
}
|
||||
|
||||
/// Parse json content into a `ChainSpec`
|
||||
pub fn from_embedded(json: &'static [u8]) -> Result<Self, String> {
|
||||
let spec = json::from_slice(json).map_err(|e| format!("Error parsing spec file: {}", e))?;
|
||||
pub fn from_json_bytes(json: impl Into<Cow<'static, [u8]>>) -> Result<Self, String> {
|
||||
let json = json.into();
|
||||
let spec = json::from_slice(json.as_ref()).map_err(|e| format!("Error parsing spec file: {}", e))?;
|
||||
Ok(ChainSpec {
|
||||
spec,
|
||||
genesis: GenesisSource::Embedded(json),
|
||||
genesis: GenesisSource::Binary(json),
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -39,7 +39,7 @@ pub type ChainSpec = substrate_service::ChainSpec<GenesisConfig>;
|
||||
|
||||
/// Flaming Fir testnet generator
|
||||
pub fn flaming_fir_config() -> Result<ChainSpec, String> {
|
||||
ChainSpec::from_embedded(include_bytes!("../res/flaming-fir.json"))
|
||||
ChainSpec::from_json_bytes(&include_bytes!("../res/flaming-fir.json")[..])
|
||||
}
|
||||
|
||||
fn session_keys(ed_key: ed25519::Public, sr_key: sr25519::Public) -> SessionKeys {
|
||||
|
||||
Reference in New Issue
Block a user