2019-03-12 06:28:11 +00:00
|
|
|
use crate::behaviour::{Behaviour, BehaviourEvent};
|
|
|
|
use crate::error;
|
2019-03-08 01:15:57 +00:00
|
|
|
use crate::multiaddr::Protocol;
|
2019-03-17 12:14:28 +00:00
|
|
|
use crate::rpc::RPCEvent;
|
2019-03-06 12:31:08 +00:00
|
|
|
use crate::NetworkConfig;
|
2019-03-07 05:17:06 +00:00
|
|
|
use futures::prelude::*;
|
2019-03-12 06:28:11 +00:00
|
|
|
use futures::Stream;
|
2019-03-07 05:17:06 +00:00
|
|
|
use libp2p::core::{
|
|
|
|
muxing::StreamMuxerBox,
|
|
|
|
nodes::Substream,
|
|
|
|
transport::boxed::Boxed,
|
2019-03-08 00:07:30 +00:00
|
|
|
upgrade::{InboundUpgradeExt, OutboundUpgradeExt},
|
2019-03-07 05:17:06 +00:00
|
|
|
};
|
2019-03-08 00:07:30 +00:00
|
|
|
use libp2p::{core, secio, Transport};
|
2019-03-07 05:17:06 +00:00
|
|
|
use libp2p::{PeerId, Swarm};
|
2019-03-13 04:37:44 +00:00
|
|
|
use slog::{debug, info, trace, warn};
|
2019-03-07 05:17:06 +00:00
|
|
|
use std::io::{Error, ErrorKind};
|
|
|
|
use std::time::Duration;
|
2019-03-14 14:50:59 +00:00
|
|
|
use types::TopicBuilder;
|
2019-03-04 07:31:01 +00:00
|
|
|
|
|
|
|
/// The configuration and state of the libp2p components for the beacon node.
|
2019-03-06 12:31:08 +00:00
|
|
|
pub struct Service {
|
2019-03-07 00:43:55 +00:00
|
|
|
/// The libp2p Swarm handler.
|
2019-03-12 06:28:11 +00:00
|
|
|
//TODO: Make this private
|
|
|
|
pub swarm: Swarm<Boxed<(PeerId, StreamMuxerBox), Error>, Behaviour<Substream<StreamMuxerBox>>>,
|
2019-03-06 12:31:08 +00:00
|
|
|
/// This node's PeerId.
|
2019-03-07 00:43:55 +00:00
|
|
|
local_peer_id: PeerId,
|
2019-03-12 06:28:11 +00:00
|
|
|
/// The libp2p logger handle.
|
|
|
|
pub log: slog::Logger,
|
2019-03-06 12:31:08 +00:00
|
|
|
}
|
2019-03-04 07:31:01 +00:00
|
|
|
|
|
|
|
impl Service {
|
2019-03-12 06:28:11 +00:00
|
|
|
pub fn new(config: NetworkConfig, log: slog::Logger) -> error::Result<Self> {
|
2019-03-06 12:31:08 +00:00
|
|
|
debug!(log, "Libp2p Service starting");
|
|
|
|
|
|
|
|
let local_private_key = config.local_private_key;
|
2019-03-07 00:43:55 +00:00
|
|
|
let local_peer_id = local_private_key.to_peer_id();
|
2019-03-13 04:37:44 +00:00
|
|
|
info!(log, "Local peer id: {:?}", local_peer_id);
|
2019-03-06 12:31:08 +00:00
|
|
|
|
2019-03-08 00:07:30 +00:00
|
|
|
let mut swarm = {
|
|
|
|
// Set up the transport
|
|
|
|
let transport = build_transport(local_private_key);
|
|
|
|
// Set up gossipsub routing
|
2019-03-17 12:14:28 +00:00
|
|
|
let behaviour = Behaviour::new(local_peer_id.clone(), config.gs_config, &log);
|
2019-03-08 00:07:30 +00:00
|
|
|
// Set up Topology
|
|
|
|
let topology = local_peer_id.clone();
|
|
|
|
Swarm::new(transport, behaviour, topology)
|
|
|
|
};
|
2019-03-06 12:31:08 +00:00
|
|
|
|
2019-03-08 00:07:30 +00:00
|
|
|
// 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),
|
|
|
|
};
|
|
|
|
}
|
2019-03-12 06:28:11 +00:00
|
|
|
// connect to boot nodes - these are currently stored as multiaddrs
|
2019-03-08 00:07:30 +00:00
|
|
|
// 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
|
|
|
|
),
|
|
|
|
};
|
|
|
|
}
|
2019-03-06 12:31:08 +00:00
|
|
|
|
2019-03-13 04:37:44 +00:00
|
|
|
// subscribe to default gossipsub topics
|
|
|
|
let mut subscribed_topics = vec![];
|
|
|
|
for topic in config.topics {
|
|
|
|
let t = TopicBuilder::new(topic.to_string()).build();
|
|
|
|
match swarm.subscribe(t) {
|
|
|
|
true => {
|
|
|
|
trace!(log, "Subscribed to topic: {:?}", topic);
|
|
|
|
subscribed_topics.push(topic);
|
|
|
|
}
|
|
|
|
false => warn!(log, "Could not subscribe to topic: {:?}", topic),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
info!(log, "Subscribed to topics: {:?}", subscribed_topics);
|
|
|
|
|
2019-03-12 06:28:11 +00:00
|
|
|
Ok(Service {
|
2019-03-07 00:43:55 +00:00
|
|
|
local_peer_id,
|
|
|
|
swarm,
|
2019-03-12 06:28:11 +00:00
|
|
|
log,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Stream for Service {
|
|
|
|
type Item = Libp2pEvent;
|
|
|
|
type Error = crate::error::Error;
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
|
|
|
|
loop {
|
|
|
|
// TODO: Currently only gossipsub events passed here.
|
|
|
|
// Build a type for more generic events
|
|
|
|
match self.swarm.poll() {
|
|
|
|
Ok(Async::Ready(Some(BehaviourEvent::Message(m)))) => {
|
|
|
|
// TODO: Stub here for debugging
|
|
|
|
debug!(self.log, "Message received: {}", m);
|
|
|
|
return Ok(Async::Ready(Some(Libp2pEvent::Message(m))));
|
|
|
|
}
|
2019-03-17 10:49:56 +00:00
|
|
|
Ok(Async::Ready(Some(BehaviourEvent::RPC(event)))) => {
|
|
|
|
return Ok(Async::Ready(Some(Libp2pEvent::RPC(event))));
|
|
|
|
}
|
2019-03-17 12:14:28 +00:00
|
|
|
Ok(Async::Ready(Some(BehaviourEvent::PeerDialed(peer_id)))) => {
|
|
|
|
return Ok(Async::Ready(Some(Libp2pEvent::PeerDialed(peer_id))));
|
|
|
|
}
|
2019-03-12 06:28:11 +00:00
|
|
|
Ok(Async::Ready(None)) => unreachable!("Swarm stream shouldn't end"),
|
|
|
|
Ok(Async::NotReady) => break,
|
|
|
|
_ => break,
|
|
|
|
}
|
2019-03-07 00:43:55 +00:00
|
|
|
}
|
2019-03-12 06:28:11 +00:00
|
|
|
Ok(Async::NotReady)
|
2019-03-04 07:31:01 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-06 12:31:08 +00:00
|
|
|
|
|
|
|
/// The implementation supports TCP/IP, WebSockets over TCP/IP, secio as the encryption layer, and
|
|
|
|
/// mplex or yamux as the multiplexing layer.
|
|
|
|
fn build_transport(
|
|
|
|
local_private_key: secio::SecioKeyPair,
|
2019-03-07 05:17:06 +00:00
|
|
|
) -> Boxed<(PeerId, StreamMuxerBox), Error> {
|
2019-03-06 12:31:08 +00:00
|
|
|
// TODO: The Wire protocol currently doesn't specify encryption and this will need to be customised
|
|
|
|
// in the future.
|
2019-03-07 05:17:06 +00:00
|
|
|
let transport = libp2p::tcp::TcpConfig::new();
|
|
|
|
let transport = libp2p::dns::DnsConfig::new(transport);
|
|
|
|
#[cfg(feature = "libp2p-websocket")]
|
|
|
|
let transport = {
|
|
|
|
let trans_clone = transport.clone();
|
|
|
|
transport.or_transport(websocket::WsConfig::new(trans_clone))
|
|
|
|
};
|
|
|
|
transport
|
|
|
|
.with_upgrade(secio::SecioConfig::new(local_private_key))
|
|
|
|
.and_then(move |out, endpoint| {
|
|
|
|
let peer_id = out.remote_key.into_peer_id();
|
|
|
|
let peer_id2 = peer_id.clone();
|
|
|
|
let upgrade = core::upgrade::SelectUpgrade::new(
|
|
|
|
libp2p::yamux::Config::default(),
|
|
|
|
libp2p::mplex::MplexConfig::new(),
|
|
|
|
)
|
|
|
|
// TODO: use a single `.map` instead of two maps
|
|
|
|
.map_inbound(move |muxer| (peer_id, muxer))
|
|
|
|
.map_outbound(move |muxer| (peer_id2, muxer));
|
2019-03-07 00:43:55 +00:00
|
|
|
|
2019-03-07 05:17:06 +00:00
|
|
|
core::upgrade::apply(out.stream, upgrade, endpoint)
|
|
|
|
.map(|(id, muxer)| (id, core::muxing::StreamMuxerBox::new(muxer)))
|
|
|
|
})
|
|
|
|
.with_timeout(Duration::from_secs(20))
|
|
|
|
.map_err(|err| Error::new(ErrorKind::Other, err))
|
|
|
|
.boxed()
|
2019-03-07 00:43:55 +00:00
|
|
|
}
|
2019-03-12 06:28:11 +00:00
|
|
|
|
|
|
|
/// Events that can be obtained from polling the Libp2p Service.
|
|
|
|
pub enum Libp2pEvent {
|
2019-03-17 10:49:56 +00:00
|
|
|
// We have received an RPC event on the swarm
|
2019-03-17 12:14:28 +00:00
|
|
|
RPC(RPCEvent),
|
|
|
|
PeerDialed(PeerId),
|
2019-03-12 06:28:11 +00:00
|
|
|
Message(String),
|
|
|
|
}
|