Adds Identify protocol and cleans up network config
This commit is contained in:
parent
437a0505c9
commit
7ec37939c8
@ -1,8 +1,13 @@
|
|||||||
use crate::rpc::{RPCEvent, RPCMessage, Rpc};
|
use crate::rpc::{RPCEvent, RPCMessage, Rpc};
|
||||||
|
use crate::NetworkConfig;
|
||||||
use futures::prelude::*;
|
use futures::prelude::*;
|
||||||
use libp2p::{
|
use libp2p::{
|
||||||
core::swarm::{NetworkBehaviourAction, NetworkBehaviourEventProcess},
|
core::{
|
||||||
gossipsub::{Gossipsub, GossipsubConfig, GossipsubEvent},
|
swarm::{NetworkBehaviourAction, NetworkBehaviourEventProcess},
|
||||||
|
PublicKey,
|
||||||
|
},
|
||||||
|
gossipsub::{Gossipsub, GossipsubEvent},
|
||||||
|
identify::{protocol::IdentifyInfo, Identify, IdentifyEvent},
|
||||||
tokio_io::{AsyncRead, AsyncWrite},
|
tokio_io::{AsyncRead, AsyncWrite},
|
||||||
NetworkBehaviour, PeerId,
|
NetworkBehaviour, PeerId,
|
||||||
};
|
};
|
||||||
@ -13,10 +18,13 @@ use types::Topic;
|
|||||||
#[derive(NetworkBehaviour)]
|
#[derive(NetworkBehaviour)]
|
||||||
#[behaviour(out_event = "BehaviourEvent", poll_method = "poll")]
|
#[behaviour(out_event = "BehaviourEvent", poll_method = "poll")]
|
||||||
pub struct Behaviour<TSubstream: AsyncRead + AsyncWrite> {
|
pub struct Behaviour<TSubstream: AsyncRead + AsyncWrite> {
|
||||||
|
/// The routing pub-sub mechanism for eth2.
|
||||||
gossipsub: Gossipsub<TSubstream>,
|
gossipsub: Gossipsub<TSubstream>,
|
||||||
// TODO: Add Kademlia for peer discovery
|
// TODO: Add Kademlia for peer discovery
|
||||||
/// The events generated by this behaviour to be consumed in the swarm poll.
|
/// The events generated by this behaviour to be consumed in the swarm poll.
|
||||||
serenity_rpc: Rpc<TSubstream>,
|
serenity_rpc: Rpc<TSubstream>,
|
||||||
|
/// Allows discovery of IP addresses for peers on the network.
|
||||||
|
identify: Identify<TSubstream>,
|
||||||
#[behaviour(ignore)]
|
#[behaviour(ignore)]
|
||||||
events: Vec<BehaviourEvent>,
|
events: Vec<BehaviourEvent>,
|
||||||
}
|
}
|
||||||
@ -53,11 +61,38 @@ impl<TSubstream: AsyncRead + AsyncWrite> NetworkBehaviourEventProcess<RPCMessage
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<TSubstream: AsyncRead + AsyncWrite> NetworkBehaviourEventProcess<IdentifyEvent>
|
||||||
|
for Behaviour<TSubstream>
|
||||||
|
{
|
||||||
|
fn inject_event(&mut self, event: IdentifyEvent) {
|
||||||
|
match event {
|
||||||
|
IdentifyEvent::Identified {
|
||||||
|
peer_id, mut info, ..
|
||||||
|
} => {
|
||||||
|
if info.listen_addrs.len() > 20 {
|
||||||
|
info.listen_addrs.truncate(20);
|
||||||
|
}
|
||||||
|
self.events.push(BehaviourEvent::Identified(peer_id, info));
|
||||||
|
}
|
||||||
|
IdentifyEvent::Error { .. } => {}
|
||||||
|
IdentifyEvent::SendBack { .. } => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<TSubstream: AsyncRead + AsyncWrite> Behaviour<TSubstream> {
|
impl<TSubstream: AsyncRead + AsyncWrite> Behaviour<TSubstream> {
|
||||||
pub fn new(local_peer_id: PeerId, gs_config: GossipsubConfig, log: &slog::Logger) -> Self {
|
pub fn new(local_public_key: PublicKey, net_conf: &NetworkConfig, log: &slog::Logger) -> Self {
|
||||||
|
let local_peer_id = local_public_key.clone().into_peer_id();
|
||||||
|
let identify_config = net_conf.identify_config.clone();
|
||||||
|
|
||||||
Behaviour {
|
Behaviour {
|
||||||
gossipsub: Gossipsub::new(local_peer_id, gs_config),
|
gossipsub: Gossipsub::new(local_peer_id, net_conf.gs_config.clone()),
|
||||||
serenity_rpc: Rpc::new(log),
|
serenity_rpc: Rpc::new(log),
|
||||||
|
identify: Identify::new(
|
||||||
|
identify_config.version,
|
||||||
|
identify_config.user_agent,
|
||||||
|
local_public_key,
|
||||||
|
),
|
||||||
events: Vec::new(),
|
events: Vec::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -91,6 +126,7 @@ impl<TSubstream: AsyncRead + AsyncWrite> Behaviour<TSubstream> {
|
|||||||
pub enum BehaviourEvent {
|
pub enum BehaviourEvent {
|
||||||
RPC(PeerId, RPCEvent),
|
RPC(PeerId, RPCEvent),
|
||||||
PeerDialed(PeerId),
|
PeerDialed(PeerId),
|
||||||
|
Identified(PeerId, IdentifyInfo),
|
||||||
// TODO: This is a stub at the moment
|
// TODO: This is a stub at the moment
|
||||||
Message(String),
|
Message(String),
|
||||||
}
|
}
|
||||||
|
@ -3,16 +3,16 @@
|
|||||||
///
|
///
|
||||||
/// This crate builds and manages the libp2p services required by the beacon node.
|
/// This crate builds and manages the libp2p services required by the beacon node.
|
||||||
pub mod behaviour;
|
pub mod behaviour;
|
||||||
|
mod config;
|
||||||
pub mod error;
|
pub mod error;
|
||||||
mod network_config;
|
|
||||||
pub mod rpc;
|
pub mod rpc;
|
||||||
mod service;
|
mod service;
|
||||||
|
|
||||||
|
pub use config::Config as NetworkConfig;
|
||||||
pub use libp2p::{
|
pub use libp2p::{
|
||||||
gossipsub::{GossipsubConfig, GossipsubConfigBuilder},
|
gossipsub::{GossipsubConfig, GossipsubConfigBuilder},
|
||||||
PeerId,
|
PeerId,
|
||||||
};
|
};
|
||||||
pub use network_config::NetworkConfig;
|
|
||||||
pub use rpc::{HelloMessage, RPCEvent};
|
pub use rpc::{HelloMessage, RPCEvent};
|
||||||
pub use service::Libp2pEvent;
|
pub use service::Libp2pEvent;
|
||||||
pub use service::Service;
|
pub use service::Service;
|
||||||
|
@ -1,59 +0,0 @@
|
|||||||
use crate::Multiaddr;
|
|
||||||
use libp2p::gossipsub::{GossipsubConfig, GossipsubConfigBuilder};
|
|
||||||
use libp2p::secio;
|
|
||||||
use std::fmt;
|
|
||||||
|
|
||||||
#[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<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<Multiaddr>,
|
|
||||||
/// Peer key related to this nodes PeerId.
|
|
||||||
pub local_private_key: secio::SecioKeyPair,
|
|
||||||
/// Client version
|
|
||||||
pub client_version: String,
|
|
||||||
/// List of topics to subscribe to as strings
|
|
||||||
pub topics: Vec<String>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for NetworkConfig {
|
|
||||||
/// Generate a default network configuration.
|
|
||||||
fn default() -> Self {
|
|
||||||
// TODO: Currently using secp256k1 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!["/ip4/127.0.0.1/tcp/9000"
|
|
||||||
.parse()
|
|
||||||
.expect("is a correct multi-address")],
|
|
||||||
listen_port: 9000,
|
|
||||||
gs_config: GossipsubConfigBuilder::new().build(),
|
|
||||||
boot_nodes: Vec::new(),
|
|
||||||
local_private_key: secio::SecioKeyPair::secp256k1_generated().unwrap(),
|
|
||||||
client_version: version::version(),
|
|
||||||
topics: vec![String::from("beacon_chain")],
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl NetworkConfig {
|
|
||||||
pub fn new(boot_nodes: Vec<Multiaddr>) -> Self {
|
|
||||||
let mut conf = NetworkConfig::default();
|
|
||||||
conf.boot_nodes = boot_nodes;
|
|
||||||
|
|
||||||
conf
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl fmt::Debug for NetworkConfig {
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
write!(f, "NetworkConfig: listen_addresses: {:?}, listen_port: {:?}, gs_config: {:?}, boot_nodes: {:?}, local_private_key: <Secio-PubKey {:?}>, client_version: {:?}", self.listen_addresses, self.listen_port, self.gs_config, self.boot_nodes, self.local_private_key.to_public_key(), self.client_version)
|
|
||||||
}
|
|
||||||
}
|
|
@ -33,7 +33,8 @@ impl Service {
|
|||||||
pub fn new(config: NetworkConfig, log: slog::Logger) -> error::Result<Self> {
|
pub fn new(config: NetworkConfig, log: slog::Logger) -> error::Result<Self> {
|
||||||
debug!(log, "Libp2p Service starting");
|
debug!(log, "Libp2p Service starting");
|
||||||
|
|
||||||
let local_private_key = config.local_private_key;
|
let local_private_key = config.local_private_key.clone();
|
||||||
|
let local_public_key = local_private_key.to_public_key();
|
||||||
let local_peer_id = local_private_key.to_peer_id();
|
let local_peer_id = local_private_key.to_peer_id();
|
||||||
info!(log, "Local peer id: {:?}", local_peer_id);
|
info!(log, "Local peer id: {:?}", local_peer_id);
|
||||||
|
|
||||||
@ -41,7 +42,7 @@ impl Service {
|
|||||||
// Set up the transport
|
// Set up the transport
|
||||||
let transport = build_transport(local_private_key);
|
let transport = build_transport(local_private_key);
|
||||||
// Set up gossipsub routing
|
// Set up gossipsub routing
|
||||||
let behaviour = Behaviour::new(local_peer_id.clone(), config.gs_config, &log);
|
let behaviour = Behaviour::new(local_public_key.clone(), &config, &log);
|
||||||
// Set up Topology
|
// Set up Topology
|
||||||
let topology = local_peer_id.clone();
|
let topology = local_peer_id.clone();
|
||||||
Swarm::new(transport, behaviour, topology)
|
Swarm::new(transport, behaviour, topology)
|
||||||
|
Loading…
Reference in New Issue
Block a user