Default boot_nodes to local node in build-spec (#1181)

* Default to local bootnode with build-spec

* Rexport libp2p stuff via network crate

* Remove unused imports and fix where formatting

* Remove spurious blank lines

* Remove unnecessary else clause
This commit is contained in:
Andrew Jones
2018-12-01 08:47:40 +00:00
committed by Gav Wood
parent 2327b213e2
commit c36998aeaf
6 changed files with 51 additions and 9 deletions
+38 -5
View File
@@ -63,7 +63,10 @@ use service::{
ServiceFactory, FactoryFullConfiguration, RuntimeGenesis,
FactoryGenesis, PruningMode, ChainSpec,
};
use network::{Protocol, config::NonReservedPeerMode};
use network::{
Protocol, config::{NetworkConfiguration, NonReservedPeerMode},
multiaddr,
};
use primitives::H256;
use std::io::{Write, Read, stdin, stdout};
@@ -325,7 +328,8 @@ where
pub fn execute_default<'a, F, E>(
spec: ChainSpec<FactoryGenesis<F>>,
exit: E,
matches: &clap::ArgMatches<'a>
matches: &clap::ArgMatches<'a>,
config: &FactoryFullConfiguration<F>
) -> error::Result<Action<E>>
where
E: IntoExit,
@@ -339,7 +343,7 @@ where
fdlimit::raise_fd_limit();
if let Some(matches) = matches.subcommand_matches("build-spec") {
build_spec::<F>(matches, spec)?;
build_spec::<F>(matches, spec, config)?;
return Ok(Action::ExecutedInternally);
}
@@ -366,11 +370,40 @@ where
Ok(Action::RunService(exit))
}
fn build_spec<F>(matches: &clap::ArgMatches, spec: ChainSpec<FactoryGenesis<F>>) -> error::Result<()>
where F: ServiceFactory,
fn with_default_boot_node<F>(
spec: &ChainSpec<FactoryGenesis<F>>,
config: &NetworkConfiguration
) -> error::Result<ChainSpec<FactoryGenesis<F>>>
where
F: ServiceFactory
{
let mut spec = spec.clone();
if spec.boot_nodes().is_empty() {
let network_keys =
network::obtain_private_key(config)
.map_err(|err| format!("Error obtaining network key: {}", err))?;
let peer_id = network_keys.to_peer_id();
let addr = multiaddr![
Ip4([127, 0, 0, 1]),
Tcp(30333u16),
P2p(peer_id)
];
spec.add_boot_node(addr)
}
Ok(spec)
}
fn build_spec<F>(
matches: &clap::ArgMatches,
spec: ChainSpec<FactoryGenesis<F>>,
config: &FactoryFullConfiguration<F>
) -> error::Result<()>
where
F: ServiceFactory
{
info!("Building chain spec");
let raw = matches.is_present("raw");
let spec = with_default_boot_node::<F>(&spec, &config.network)?;
let json = service::chain_ops::build_spec::<FactoryGenesis<F>>(spec, raw)?;
print!("{}", json);
Ok(())
+2 -1
View File
@@ -53,7 +53,8 @@ mod transport;
pub use custom_proto::RegisteredProtocol;
pub use error::{Error, ErrorKind, DisconnectReason};
pub use libp2p::{Multiaddr, multiaddr::Protocol, PeerId};
pub use libp2p::{Multiaddr, multiaddr::{Protocol}, multiaddr, PeerId};
pub use secret::obtain_private_key;
pub use service_task::{start_service, Service, ServiceEvent};
pub use traits::{NetworkConfiguration, NodeIndex, NodeId, NonReservedPeerMode};
pub use traits::{ProtocolId, Secret, Severity};
+1 -1
View File
@@ -25,7 +25,7 @@ use NetworkConfiguration;
const SECRET_FILE: &str = "secret";
/// Obtains or generates the local private key using the configuration.
pub(crate) fn obtain_private_key(
pub fn obtain_private_key(
config: &NetworkConfiguration
) -> Result<secio::SecioKeyPair, IoError> {
if let Some(ref secret) = config.use_secret {
+4 -1
View File
@@ -68,7 +68,10 @@ pub use chain::Client as ClientHandle;
pub use service::{Service, FetchFuture, TransactionPool, ManageNetwork, SyncProvider, ExHashT};
pub use protocol::{ProtocolStatus, PeerInfo, Context};
pub use sync::{Status as SyncStatus, SyncState};
pub use network_libp2p::{NodeIndex, ProtocolId, Severity, Protocol};
pub use network_libp2p::{
NodeIndex, ProtocolId, Severity, Protocol, Multiaddr,
obtain_private_key, multiaddr,
};
pub use message::{generic as generic_message, RequestId, Status as StatusMessage};
pub use error::Error;
pub use on_demand::{OnDemand, OnDemandService, RemoteResponse};
+5
View File
@@ -23,6 +23,7 @@ use primitives::storage::{StorageKey, StorageData};
use runtime_primitives::{BuildStorage, StorageMap, ChildrenStorageMap};
use serde_json as json;
use components::RuntimeGenesis;
use network::Multiaddr;
enum GenesisSource<G> {
File(PathBuf),
@@ -139,6 +140,10 @@ impl<G: RuntimeGenesis> ChainSpec<G> {
self.spec.properties.as_ref().unwrap_or(&json::map::Map::new()).clone()
}
pub fn add_boot_node(&mut self, addr: Multiaddr) {
self.spec.boot_nodes.push(addr.to_string())
}
/// 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))?;
+1 -1
View File
@@ -135,7 +135,7 @@ pub fn run<I, T, E>(args: I, exit: E, version: cli::VersionInfo) -> error::Resul
config.roles = ServiceRoles::AUTHORITY;
}
match cli::execute_default::<service::Factory, _>(spec, exit, &matches)? {
match cli::execute_default::<service::Factory, _>(spec, exit, &matches, &config)? {
cli::Action::ExecutedInternally => (),
cli::Action::RunService(exit) => {
info!("Substrate Node");