2019-03-06 12:31:08 +00:00
|
|
|
use crate::NetworkConfig;
|
|
|
|
use libp2p::gossipsub::GossipsubEvent;
|
|
|
|
use libp2p::PeerId;
|
|
|
|
use libp2p::{build_tcp_ws_secio_mplex_yamux, core, secio, Transport};
|
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 {
|
|
|
|
/// This node's PeerId.
|
|
|
|
peer_id: PeerId,
|
|
|
|
}
|
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;
|
|
|
|
let peer_id = local_private_key.to_peer_id();
|
|
|
|
debug!(log, "Local peer id: {:?}", peer_id);
|
|
|
|
|
|
|
|
// Set up the transport
|
|
|
|
let transport = build_transport(local_private_key);
|
|
|
|
|
|
|
|
//let transport = libp2p::
|
|
|
|
|
|
|
|
Service { peer_id }
|
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)
|
|
|
|
}
|