Rename ChainSpec field (#4471)

* initial rename

* nitpick: add space in "chain spec"

* Add comment to client spec.
This commit is contained in:
Joshy Orndorff
2019-12-20 15:50:18 -05:00
committed by Bastian Köcher
parent e6b8a69656
commit f6cbf4421f
+23 -22
View File
@@ -80,7 +80,7 @@ impl<'a, G: RuntimeGenesis, E> BuildStorage for &'a ChainSpec<G, E> {
let child_info = ChildInfo::resolve_child_info( let child_info = ChildInfo::resolve_child_info(
child_content.child_type, child_content.child_type,
child_content.child_info.as_slice(), child_content.child_info.as_slice(),
).expect("chainspec contains correct content").to_owned(); ).expect("chain spec contains correct content").to_owned();
( (
sk.0, sk.0,
StorageChild { StorageChild {
@@ -129,10 +129,11 @@ enum Genesis<G> {
Raw(RawGenesis), Raw(RawGenesis),
} }
/// A configuration of a client. Does not include runtime storage initialization.
#[derive(Serialize, Deserialize, Clone, Debug)] #[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
#[serde(deny_unknown_fields)] #[serde(deny_unknown_fields)]
struct ChainSpecFile<E> { struct ClientSpec<E> {
pub name: String, pub name: String,
pub id: String, pub id: String,
pub boot_nodes: Vec<String>, pub boot_nodes: Vec<String>,
@@ -157,14 +158,14 @@ pub type NoExtension = Option<()>;
/// A configuration of a chain. Can be used to build a genesis block. /// A configuration of a chain. Can be used to build a genesis block.
pub struct ChainSpec<G, E = NoExtension> { pub struct ChainSpec<G, E = NoExtension> {
spec: ChainSpecFile<E>, client_spec: ClientSpec<E>,
genesis: GenesisSource<G>, genesis: GenesisSource<G>,
} }
impl<G, E: Clone> Clone for ChainSpec<G, E> { impl<G, E: Clone> Clone for ChainSpec<G, E> {
fn clone(&self) -> Self { fn clone(&self) -> Self {
ChainSpec { ChainSpec {
spec: self.spec.clone(), client_spec: self.client_spec.clone(),
genesis: self.genesis.clone(), genesis: self.genesis.clone(),
} }
} }
@@ -173,44 +174,44 @@ impl<G, E: Clone> Clone for ChainSpec<G, E> {
impl<G, E> ChainSpec<G, E> { impl<G, E> ChainSpec<G, E> {
/// A list of bootnode addresses. /// A list of bootnode addresses.
pub fn boot_nodes(&self) -> &[String] { pub fn boot_nodes(&self) -> &[String] {
&self.spec.boot_nodes &self.client_spec.boot_nodes
} }
/// Spec name. /// Spec name.
pub fn name(&self) -> &str { pub fn name(&self) -> &str {
&self.spec.name &self.client_spec.name
} }
/// Spec id. /// Spec id.
pub fn id(&self) -> &str { pub fn id(&self) -> &str {
&self.spec.id &self.client_spec.id
} }
/// Telemetry endpoints (if any) /// Telemetry endpoints (if any)
pub fn telemetry_endpoints(&self) -> &Option<TelemetryEndpoints> { pub fn telemetry_endpoints(&self) -> &Option<TelemetryEndpoints> {
&self.spec.telemetry_endpoints &self.client_spec.telemetry_endpoints
} }
/// Network protocol id. /// Network protocol id.
pub fn protocol_id(&self) -> Option<&str> { pub fn protocol_id(&self) -> Option<&str> {
self.spec.protocol_id.as_ref().map(String::as_str) self.client_spec.protocol_id.as_ref().map(String::as_str)
} }
/// Additional loosly-typed properties of the chain. /// Additional loosly-typed properties of the chain.
/// ///
/// Returns an empty JSON object if 'properties' not defined in config /// Returns an empty JSON object if 'properties' not defined in config
pub fn properties(&self) -> Properties { pub fn properties(&self) -> Properties {
self.spec.properties.as_ref().unwrap_or(&json::map::Map::new()).clone() self.client_spec.properties.as_ref().unwrap_or(&json::map::Map::new()).clone()
} }
/// Add a bootnode to the list. /// Add a bootnode to the list.
pub fn add_boot_node(&mut self, addr: Multiaddr) { pub fn add_boot_node(&mut self, addr: Multiaddr) {
self.spec.boot_nodes.push(addr.to_string()) self.client_spec.boot_nodes.push(addr.to_string())
} }
/// Returns a reference to defined chain spec extensions. /// Returns a reference to defined chain spec extensions.
pub fn extensions(&self) -> &E { pub fn extensions(&self) -> &E {
&self.spec.extensions &self.client_spec.extensions
} }
/// Create hardcoded spec. /// Create hardcoded spec.
@@ -224,7 +225,7 @@ impl<G, E> ChainSpec<G, E> {
properties: Option<Properties>, properties: Option<Properties>,
extensions: E, extensions: E,
) -> Self { ) -> Self {
let spec = ChainSpecFile { let client_spec = ClientSpec {
name: name.to_owned(), name: name.to_owned(),
id: id.to_owned(), id: id.to_owned(),
boot_nodes: boot_nodes, boot_nodes: boot_nodes,
@@ -237,7 +238,7 @@ impl<G, E> ChainSpec<G, E> {
}; };
ChainSpec { ChainSpec {
spec, client_spec,
genesis: GenesisSource::Factory(Rc::new(constructor)), genesis: GenesisSource::Factory(Rc::new(constructor)),
} }
} }
@@ -247,10 +248,10 @@ impl<G, E: serde::de::DeserializeOwned> ChainSpec<G, E> {
/// Parse json content into a `ChainSpec` /// Parse json content into a `ChainSpec`
pub fn from_json_bytes(json: impl Into<Cow<'static, [u8]>>) -> Result<Self, String> { pub fn from_json_bytes(json: impl Into<Cow<'static, [u8]>>) -> Result<Self, String> {
let json = json.into(); let json = json.into();
let spec = json::from_slice(json.as_ref()) let client_spec = json::from_slice(json.as_ref())
.map_err(|e| format!("Error parsing spec file: {}", e))?; .map_err(|e| format!("Error parsing spec file: {}", e))?;
Ok(ChainSpec { Ok(ChainSpec {
spec, client_spec,
genesis: GenesisSource::Binary(json), genesis: GenesisSource::Binary(json),
}) })
} }
@@ -259,10 +260,10 @@ impl<G, E: serde::de::DeserializeOwned> ChainSpec<G, E> {
pub fn from_json_file(path: PathBuf) -> Result<Self, String> { pub fn from_json_file(path: PathBuf) -> Result<Self, String> {
let file = File::open(&path) let file = File::open(&path)
.map_err(|e| format!("Error opening spec file: {}", e))?; .map_err(|e| format!("Error opening spec file: {}", e))?;
let spec = json::from_reader(file) let client_spec = json::from_reader(file)
.map_err(|e| format!("Error parsing spec file: {}", e))?; .map_err(|e| format!("Error parsing spec file: {}", e))?;
Ok(ChainSpec { Ok(ChainSpec {
spec, client_spec,
genesis: GenesisSource::File(path), genesis: GenesisSource::File(path),
}) })
} }
@@ -274,7 +275,7 @@ impl<G: RuntimeGenesis, E: serde::Serialize> ChainSpec<G, E> {
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
struct Container<G, E> { struct Container<G, E> {
#[serde(flatten)] #[serde(flatten)]
spec: ChainSpecFile<E>, client_spec: ClientSpec<E>,
genesis: Genesis<G>, genesis: Genesis<G>,
}; };
@@ -304,11 +305,11 @@ impl<G: RuntimeGenesis, E: serde::Serialize> ChainSpec<G, E> {
}, },
(_, genesis) => genesis, (_, genesis) => genesis,
}; };
let spec = Container { let container = Container {
spec: self.spec, client_spec: self.client_spec,
genesis, genesis,
}; };
json::to_string_pretty(&spec) json::to_string_pretty(&container)
.map_err(|e| format!("Error generating spec json: {}", e)) .map_err(|e| format!("Error generating spec json: {}", e))
} }
} }