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)));
{
-3
View File
@@ -43,9 +43,6 @@ use substrate_network::{message, generic_message};
use substrate_network::specialization::Specialization;
use substrate_network::StatusMessage as GenericFullStatus;
/// Demo protocol id.
pub const PROTOCOL_ID: ::substrate_network::ProtocolId = *b"dot";
type FullStatus = GenericFullStatus<Block>;
/// Specialization of the network service for the node protocol.
+3 -2
View File
@@ -120,6 +120,7 @@ pub fn staging_testnet_config() -> ChainSpec<GenesisConfig> {
staging_testnet_config_genesis,
boot_nodes,
Some(STAGING_TELEMETRY_URL.into()),
None,
)
}
@@ -214,7 +215,7 @@ fn development_config_genesis() -> GenesisConfig {
/// Development config (single validator Alice)
pub fn development_config() -> ChainSpec<GenesisConfig> {
ChainSpec::from_genesis("Development", "development", development_config_genesis, vec![], None)
ChainSpec::from_genesis("Development", "development", development_config_genesis, vec![], None, None)
}
fn local_testnet_genesis() -> GenesisConfig {
@@ -226,5 +227,5 @@ fn local_testnet_genesis() -> GenesisConfig {
/// Local testnet config (multivalidator Alice + Bob)
pub fn local_testnet_config() -> ChainSpec<GenesisConfig> {
ChainSpec::from_genesis("Local Testnet", "local_testnet", local_testnet_genesis, vec![], None)
ChainSpec::from_genesis("Local Testnet", "local_testnet", local_testnet_genesis, vec![], None, None)
}
-2
View File
@@ -102,8 +102,6 @@ impl service::ServiceFactory for Factory {
type Genesis = GenesisConfig;
type Configuration = CustomConfiguration;
const NETWORK_PROTOCOL_ID: network::ProtocolId = ::node_network::PROTOCOL_ID;
fn build_full_transaction_pool(config: TransactionPoolOptions, client: Arc<service::FullClient<Self>>)
-> Result<TransactionPool<service::FullClient<Self>>, Error>
{