Protocol ID configurable in the chain spec (#811)

* Protocol ID configurable in the chain spec

* Removed obsolete const
This commit is contained in:
Arkadiy Paronyan
2018-09-27 13:26:07 +02:00
committed by Gav Wood
parent 37102611d4
commit 0ab3b2de35
6 changed files with 22 additions and 12 deletions
+8 -1
View File
@@ -76,6 +76,7 @@ struct ChainSpecFile {
pub id: String,
pub boot_nodes: Vec<String>,
pub telemetry_url: Option<String>,
pub protocol_id: Option<String>,
}
/// A configuration of a chain. Can be used to build a genesis block.
@@ -101,6 +102,10 @@ impl<G: RuntimeGenesis> ChainSpec<G> {
self.spec.telemetry_url.as_ref().map(String::as_str)
}
pub fn protocol_id(&self) -> Option<&str> {
self.spec.protocol_id.as_ref().map(String::as_str)
}
/// 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))?;
@@ -126,7 +131,8 @@ impl<G: RuntimeGenesis> ChainSpec<G> {
id: &str,
constructor: fn() -> G,
boot_nodes: Vec<String>,
telemetry_url: Option<&str>
telemetry_url: Option<&str>,
protocol_id: Option<&str>,
) -> Self
{
let spec = ChainSpecFile {
@@ -134,6 +140,7 @@ impl<G: RuntimeGenesis> ChainSpec<G> {
id: id.to_owned(),
boot_nodes: boot_nodes,
telemetry_url: telemetry_url.map(str::to_owned),
protocol_id: protocol_id.map(str::to_owned),
};
ChainSpec {
spec,
-3
View File
@@ -131,9 +131,6 @@ pub trait ServiceFactory: 'static {
/// Other configuration for service members.
type Configuration: Default;
/// Network protocol id.
const NETWORK_PROTOCOL_ID: network::ProtocolId;
//TODO: replace these with a constructor trait. that TransactionPool implements.
/// Extrinsic pool constructor for the full client.
fn build_full_transaction_pool(config: TransactionPoolOptions, client: Arc<FullClient<Self>>)
+11 -1
View File
@@ -84,6 +84,8 @@ pub use components::{ServiceFactory, FullBackend, FullExecutor, LightBackend,
ComponentExHash, ComponentExtrinsic,
};
const DEFAULT_PROTOCOL_ID: &'static str = "sup";
/// Substrate service.
pub struct Service<Components: components::Components> {
client: Arc<ComponentClient<Components>>,
@@ -171,7 +173,15 @@ impl<Components> Service<Components>
specialization: network_protocol,
};
let network = network::Service::new(network_params, Components::Factory::NETWORK_PROTOCOL_ID)?;
let mut protocol_id = network::ProtocolId::default();
let protocol_id_full = config.chain_spec.protocol_id().unwrap_or(DEFAULT_PROTOCOL_ID).as_bytes();
if protocol_id_full.len() > protocol_id.len() {
warn!("Protocol ID truncated to {} chars", protocol_id.len());
}
let id_len = protocol_id_full.len().min(protocol_id.len());
&mut protocol_id[0..id_len].copy_from_slice(&protocol_id_full[0..id_len]);
let network = network::Service::new(network_params, protocol_id)?;
on_demand.map(|on_demand| on_demand.set_service_link(Arc::downgrade(&network)));
{