Add cmd line options to allow for bootstrap nodes

This commit is contained in:
Paul Hauner 2018-08-02 22:38:54 +10:00
parent b10b422b2a
commit 44bcd1b40e
4 changed files with 33 additions and 26 deletions

View File

@ -13,7 +13,7 @@ pub mod utils;
use std::path::PathBuf;
use slog::Drain;
use clap::{ Arg, App, SubCommand};
use clap::{ Arg, App };
use p2p::config::NetworkConfig;
use p2p::floodsub;
use p2p::state::NetworkState;
@ -29,21 +29,35 @@ fn main() {
.author("Sigma Prime <paul@sigmaprime.io>")
.about("Eth 2.0 Client")
.arg(Arg::with_name("datadir")
.long("datadir")
.value_name("DIR")
.help("Data directory for keys and databases."))
.subcommand(SubCommand::with_name("generate-keys"))
.about("Generates a new set of random keys for p2p dev.")
.help("Data directory for keys and databases.")
.takes_value(true))
.arg(Arg::with_name("port")
.long("port")
.value_name("PORT")
.help("Network listen port for p2p connections.")
.takes_value(true))
.get_matches();
let mut config = NetworkConfig::default();
// Custom datadir
if let Some(dir) = matches.value_of("datadir") {
config.data_dir = PathBuf::from(dir.to_string());
}
// Custom listen port
if let Some(port) = matches.value_of("port") {
config.listen_multiaddr =
NetworkConfig::multiaddr_on_port(&port.to_string());
}
info!(log, ""; "data_dir" => &config.data_dir.to_str());
if let Some(_) = matches.subcommand_matches("generate-keys") {
// keys::generate_keys(&log).expect("Failed to generate keys");
} else {
let state = NetworkState::new(config, &log).expect("setup failed");
let mut state = NetworkState::new(config, &log).expect("setup failed");
floodsub::listen(state, &log);
}
info!(log, "Exiting.");

View File

@ -20,9 +20,14 @@ impl NetworkConfig {
};
Self {
data_dir,
listen_multiaddr: "/ip4/0.0.0.0/tcp/0"
.parse::<Multiaddr>().unwrap()
listen_multiaddr: NetworkConfig::multiaddr_on_port("0")
}
}
/// Return a TCP multiaddress on 0.0.0.0 for a given port.
pub fn multiaddr_on_port(port: &str) -> Multiaddr {
return format!("/ip4/0.0.0.0/tcp/{}", port)
.parse::<Multiaddr>().unwrap()
}
}

View File

@ -42,8 +42,6 @@ pub fn listen(state: NetworkState, log: &Logger)
let peer_store = state.peer_store;
let peer_id = state.peer_id;
let listen_multiaddr = state.listen_multiaddr;
info!(log, "Local PeerId: {:?}", peer_id);
let mut core = tokio_core::reactor::Core::new().expect("tokio failure.");
let listened_addrs = Arc::new(RwLock::new(vec![]));
@ -116,7 +114,7 @@ pub fn listen(state: NetworkState, log: &Logger)
.listen_on(listen_multiaddr)
.expect("Failed to listen on multiaddr");
info!(log, "Listening on: {:?}", actual_addr);
info!(log, "libp2p listening"; "listen_addr" => actual_addr.to_string());
let (kad_ctl, kad_init) = kad_ctl_proto.start(
swarm_ctl.clone(),
@ -145,10 +143,12 @@ pub fn listen(state: NetworkState, log: &Logger)
.and_then(move |()| kad_ctl.find_node(peer_id.clone()))
.for_each(move |peers| {
let local_hash = U512::from(polling_peer_id.hash());
info!(log, "Peer discovery results:");
for peer in peers {
let peer_hash = U512::from(peer.hash());
let _distance = 512 - (local_hash ^ peer_hash).leading_zeros();
let distance = 512 - (local_hash ^ peer_hash).leading_zeros();
info!(log, "Discovered peer";
"distance" => distance,
"peer_id" => peer.to_base58());
let peer_addr = AddrComponent::P2P(peer.into_bytes()).into();
let dial_result = swarm_ctl.dial(
peer_addr,

View File

@ -46,7 +46,8 @@ impl NetworkState {
let base = JsonPeerstore::new(path)?;
Arc::new(base)
};
let listen_multiaddr = "/ip4/0.0.0.0/tcp/0".parse::<Multiaddr>()?;
info!(log, "Loaded peerstore"; "peer_count" => &peer_store.peers().count());
let listen_multiaddr = config.listen_multiaddr.clone();
Ok(Self {
config: config,
seckey,
@ -65,19 +66,6 @@ impl NetworkState {
.add_addr(multiaddr, Duration::from_secs(duration_secs));
}
// TODO: this shouldn't be hard-coded; distribute with peers json.
pub fn add_sigp_peer(&mut self) {
let peer_id = {
let b58 = "Qmajfeei87f8V5N7SQwPw3wr57M1dNcGNwhTqf72v73E7U";
b58.parse::<PeerId>().unwrap()
};
let multiaddr = {
let string = "/dns/lh.sigp.io/tcp/10101";
string.parse::<Multiaddr>().unwrap()
};
self.add_peer(peer_id, multiaddr, 3600 * 24 * 356);
}
/// Instantiate a SecretKey from a .pem file on disk.
pub fn load_secret_key_from_pem_file(config: &NetworkConfig, curve: &Secp256k1)
-> Result<SecretKey, Box<Error>>