diff --git a/beacon_node/eth2-libp2p/src/behaviour.rs b/beacon_node/eth2-libp2p/src/behaviour.rs index 78d0130025..a29484e2b1 100644 --- a/beacon_node/eth2-libp2p/src/behaviour.rs +++ b/beacon_node/eth2-libp2p/src/behaviour.rs @@ -1,8 +1,13 @@ use crate::rpc::{RPCEvent, RPCMessage, Rpc}; +use crate::NetworkConfig; use futures::prelude::*; use libp2p::{ - core::swarm::{NetworkBehaviourAction, NetworkBehaviourEventProcess}, - gossipsub::{Gossipsub, GossipsubConfig, GossipsubEvent}, + core::{ + swarm::{NetworkBehaviourAction, NetworkBehaviourEventProcess}, + PublicKey, + }, + gossipsub::{Gossipsub, GossipsubEvent}, + identify::{protocol::IdentifyInfo, Identify, IdentifyEvent}, tokio_io::{AsyncRead, AsyncWrite}, NetworkBehaviour, PeerId, }; @@ -13,10 +18,13 @@ use types::Topic; #[derive(NetworkBehaviour)] #[behaviour(out_event = "BehaviourEvent", poll_method = "poll")] pub struct Behaviour { + /// The routing pub-sub mechanism for eth2. gossipsub: Gossipsub, // TODO: Add Kademlia for peer discovery /// The events generated by this behaviour to be consumed in the swarm poll. serenity_rpc: Rpc, + /// Allows discovery of IP addresses for peers on the network. + identify: Identify, #[behaviour(ignore)] events: Vec, } @@ -53,11 +61,38 @@ impl NetworkBehaviourEventProcess NetworkBehaviourEventProcess + for Behaviour +{ + 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 Behaviour { - 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 { - gossipsub: Gossipsub::new(local_peer_id, gs_config), + gossipsub: Gossipsub::new(local_peer_id, net_conf.gs_config.clone()), serenity_rpc: Rpc::new(log), + identify: Identify::new( + identify_config.version, + identify_config.user_agent, + local_public_key, + ), events: Vec::new(), } } @@ -91,6 +126,7 @@ impl Behaviour { pub enum BehaviourEvent { RPC(PeerId, RPCEvent), PeerDialed(PeerId), + Identified(PeerId, IdentifyInfo), // TODO: This is a stub at the moment Message(String), } diff --git a/beacon_node/eth2-libp2p/src/lib.rs b/beacon_node/eth2-libp2p/src/lib.rs index f3e97355d7..f7a961bb2e 100644 --- a/beacon_node/eth2-libp2p/src/lib.rs +++ b/beacon_node/eth2-libp2p/src/lib.rs @@ -3,16 +3,16 @@ /// /// This crate builds and manages the libp2p services required by the beacon node. pub mod behaviour; +mod config; pub mod error; -mod network_config; pub mod rpc; mod service; +pub use config::Config as NetworkConfig; pub use libp2p::{ gossipsub::{GossipsubConfig, GossipsubConfigBuilder}, PeerId, }; -pub use network_config::NetworkConfig; pub use rpc::{HelloMessage, RPCEvent}; pub use service::Libp2pEvent; pub use service::Service; diff --git a/beacon_node/eth2-libp2p/src/network_config.rs b/beacon_node/eth2-libp2p/src/network_config.rs deleted file mode 100644 index 176892bb02..0000000000 --- a/beacon_node/eth2-libp2p/src/network_config.rs +++ /dev/null @@ -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, - /// 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, - /// 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, -} - -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) -> 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: , client_version: {:?}", self.listen_addresses, self.listen_port, self.gs_config, self.boot_nodes, self.local_private_key.to_public_key(), self.client_version) - } -} diff --git a/beacon_node/eth2-libp2p/src/service.rs b/beacon_node/eth2-libp2p/src/service.rs index e378cd634a..7a39378834 100644 --- a/beacon_node/eth2-libp2p/src/service.rs +++ b/beacon_node/eth2-libp2p/src/service.rs @@ -33,7 +33,8 @@ impl Service { pub fn new(config: NetworkConfig, log: slog::Logger) -> error::Result { 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(); info!(log, "Local peer id: {:?}", local_peer_id); @@ -41,7 +42,7 @@ impl Service { // 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, &log); + let behaviour = Behaviour::new(local_public_key.clone(), &config, &log); // Set up Topology let topology = local_peer_id.clone(); Swarm::new(transport, behaviour, topology)