Node listens on default port and connects to bootnodes.

This commit is contained in:
Age Manning
2019-03-08 11:07:30 +11:00
parent 9f13731d6d
commit 3c51769428
6 changed files with 61 additions and 38 deletions

View File

@@ -6,6 +6,8 @@ mod behaviour;
mod network_config;
mod service;
pub use libp2p::multiaddr;
pub use libp2p::Multiaddr;
pub use libp2p::{
gossipsub::{GossipsubConfig, GossipsubConfigBuilder},
PeerId,

View File

@@ -1,20 +1,20 @@
use libp2p::gossipsub::{GossipsubConfig, GossipsubConfigBuilder};
use libp2p::secio;
use libp2p::Multiaddr;
use std::fmt;
use std::net::IpAddr;
#[derive(Clone)]
/// Network configuration for lighthouse.
pub struct NetworkConfig {
//TODO: stubbing networking initial params, change in the future
/// IP address to listen on.
pub listen_addresses: Vec<IpAddr>,
pub listen_addresses: Vec<Multiaddr>,
/// Listen port UDP/TCP.
pub listen_port: u16,
/// Gossipsub configuration parameters.
pub gs_config: GossipsubConfig,
/// List of nodes to initially connect to.
pub boot_nodes: Vec<IpAddr>,
pub boot_nodes: Vec<Multiaddr>,
/// Peer key related to this nodes PeerId.
pub local_private_key: secio::SecioKeyPair,
/// Client version
@@ -24,21 +24,15 @@ pub struct NetworkConfig {
impl Default for NetworkConfig {
/// Generate a default network configuration.
fn default() -> Self {
// hard-coded defaults
let bootnodes = ["127.0.0.1"];
let default_port = 9000;
// TODO: Currently using ed25519 key pairs. Wire protocol specifies RSA. Waiting for this
// PR to be merged to generate RSA keys: https://github.com/briansmith/ring/pull/733
NetworkConfig {
listen_addresses: vec!["127.0.0.1".parse().expect("correct IP address")],
listen_port: default_port,
listen_addresses: vec!["/ip4/127.0.0.1/tcp/9000"
.parse()
.expect("is a correct multi-address")],
listen_port: 9000,
gs_config: GossipsubConfigBuilder::new().build(),
boot_nodes: bootnodes
.iter()
.map(|s| s.parse().expect("Bootnodes must be IP addresses"))
.collect(),
boot_nodes: Vec::new(),
local_private_key: secio::SecioKeyPair::ed25519_generated().unwrap(),
client_version: version::version(),
}

View File

@@ -5,12 +5,12 @@ use libp2p::core::{
muxing::StreamMuxerBox,
nodes::Substream,
transport::boxed::Boxed,
upgrade::{InboundUpgrade, InboundUpgradeExt, OutboundUpgrade, OutboundUpgradeExt},
upgrade::{InboundUpgradeExt, OutboundUpgradeExt},
};
use libp2p::{build_tcp_ws_secio_mplex_yamux, core, secio, Transport};
use libp2p::multiaddr::Protocol;
use libp2p::{core, secio, Transport};
use libp2p::{PeerId, Swarm};
use slog::debug;
use std::error;
use slog::{debug, info, warn};
use std::io::{Error, ErrorKind};
use std::time::Duration;
@@ -21,9 +21,6 @@ pub struct Service {
/// This node's PeerId.
local_peer_id: PeerId,
}
//Swarm<impl std::clone::Clone+libp2p_core::transport::Transport, behaviour::Behaviour<libp2p_core::muxing::SubstreamRef<std::sync::Arc<impl std::marker::Send+std::marker::Sync+libp2p_core::muxing::StreamMuxer>>>>
//swarm: Swarm<Boxed<(PeerId, StreamMuxerBox), IoError>, Behaviour<TMessage, Substream<StreamMuxerBox>>>,
impl Service {
pub fn new(config: NetworkConfig, log: slog::Logger) -> Self {
@@ -33,14 +30,37 @@ impl Service {
let local_peer_id = local_private_key.to_peer_id();
debug!(log, "Local peer id: {:?}", local_peer_id);
// Set up the transport
let transport = build_transport(local_private_key);
// Set up gossipsub routing
let behaviour = Behaviour::new(local_peer_id.clone(), config.gs_config);
// Set up Topology
let topology = local_peer_id.clone();
let mut swarm = {
// Set up the transport
let transport = build_transport(local_private_key);
// Set up gossipsub routing
let behaviour = Behaviour::new(local_peer_id.clone(), config.gs_config);
// Set up Topology
let topology = local_peer_id.clone();
Swarm::new(transport, behaviour, topology)
};
let swarm = Swarm::new(transport, behaviour, topology);
// listen on all addresses
for address in &config.listen_addresses {
match Swarm::listen_on(&mut swarm, address.clone()) {
Ok(mut listen_addr) => {
listen_addr.append(Protocol::P2p(local_peer_id.clone().into()));
info!(log, "Listening on: {}", listen_addr);
}
Err(err) => warn!(log, "Cannot listen on: {} : {:?}", address, err),
};
}
// connect to boot nodes - these are currently stored as multiadders
// Once we have discovery, can set to peerId
for bootnode in config.boot_nodes {
match Swarm::dial_addr(&mut swarm, bootnode.clone()) {
Ok(()) => debug!(log, "Dialing bootnode: {}", bootnode),
Err(err) => debug!(
log,
"Could not connect to bootnode: {} error: {:?}", bootnode, err
),
};
}
Service {
local_peer_id,