2019-03-06 12:31:08 +00:00
|
|
|
use crate::NetworkConfig;
|
2019-03-07 00:43:55 +00:00
|
|
|
use libp2p::core::{muxing::StreamMuxer, nodes::Substream};
|
|
|
|
use libp2p::gossipsub::{Gossipsub, GossipsubConfig, GossipsubEvent};
|
2019-03-06 12:31:08 +00:00
|
|
|
use libp2p::{build_tcp_ws_secio_mplex_yamux, core, secio, Transport};
|
2019-03-07 00:43:55 +00:00
|
|
|
use libp2p::{core::swarm::NetworkBehaviour, PeerId, Swarm};
|
2019-03-04 07:31:01 +00:00
|
|
|
use slog::debug;
|
2019-03-06 12:31:08 +00:00
|
|
|
use std::error;
|
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.
|
|
|
|
swarm: String,
|
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-06 12:31:08 +00:00
|
|
|
}
|
2019-03-04 07:31:01 +00:00
|
|
|
|
|
|
|
impl Service {
|
2019-03-06 12:31:08 +00:00
|
|
|
pub fn new(config: NetworkConfig, log: slog::Logger) -> Self {
|
|
|
|
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();
|
|
|
|
debug!(log, "Local peer id: {:?}", local_peer_id);
|
2019-03-06 12:31:08 +00:00
|
|
|
|
|
|
|
// Set up the transport
|
|
|
|
let transport = build_transport(local_private_key);
|
2019-03-07 00:43:55 +00:00
|
|
|
// Set up gossipsub routing
|
|
|
|
let behaviour = build_behaviour(local_peer_id, config.gs_config);
|
|
|
|
// Set up Topology
|
|
|
|
let topology = local_peer_id;
|
2019-03-06 12:31:08 +00:00
|
|
|
|
2019-03-07 00:43:55 +00:00
|
|
|
let swarm = Swarm::new(transport, behaviour, topology);
|
2019-03-06 12:31:08 +00:00
|
|
|
|
2019-03-07 00:43:55 +00:00
|
|
|
Service {
|
|
|
|
local_peer_id,
|
|
|
|
swarm,
|
|
|
|
}
|
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,
|
|
|
|
) -> impl Transport<
|
|
|
|
Output = (
|
|
|
|
PeerId,
|
|
|
|
impl core::muxing::StreamMuxer<OutboundSubstream = impl Send, Substream = impl Send>
|
|
|
|
+ Send
|
|
|
|
+ Sync,
|
|
|
|
),
|
|
|
|
Error = impl error::Error + Send,
|
|
|
|
Listener = impl Send,
|
|
|
|
Dial = impl Send,
|
|
|
|
ListenerUpgrade = impl Send,
|
|
|
|
> + Clone {
|
|
|
|
// TODO: The Wire protocol currently doesn't specify encryption and this will need to be customised
|
|
|
|
// in the future.
|
|
|
|
build_tcp_ws_secio_mplex_yamux(local_private_key)
|
|
|
|
}
|
2019-03-07 00:43:55 +00:00
|
|
|
|
|
|
|
/// Builds the network behaviour for the libp2p Swarm.
|
|
|
|
fn build_behaviour<TSubstream>(
|
|
|
|
local_peer_id: PeerId,
|
|
|
|
config: GossipsubConfig,
|
|
|
|
) -> impl NetworkBehaviour {
|
|
|
|
// TODO: Add Kademlia/Peer discovery
|
|
|
|
Gossipsub::new(local_peer_id, config)
|
|
|
|
}
|