diff --git a/beacon_node/libp2p/src/behaviour.rs b/beacon_node/libp2p/src/behaviour.rs index be12011dd..be49abb94 100644 --- a/beacon_node/libp2p/src/behaviour.rs +++ b/beacon_node/libp2p/src/behaviour.rs @@ -5,6 +5,7 @@ use libp2p::{ tokio_io::{AsyncRead, AsyncWrite}, NetworkBehaviour, PeerId, }; +use types::Topic; /// Builds the network behaviour for the libp2p Swarm. /// Implements gossipsub message routing. @@ -57,6 +58,10 @@ impl Behaviour { } impl Behaviour { + pub fn subscribe(&mut self, topic: Topic) -> bool { + self.gossipsub.subscribe(topic) + } + pub fn send_message(&self, message: String) { // TODO: Encode and send via gossipsub diff --git a/beacon_node/libp2p/src/network_config.rs b/beacon_node/libp2p/src/network_config.rs index ecc36da33..176892bb0 100644 --- a/beacon_node/libp2p/src/network_config.rs +++ b/beacon_node/libp2p/src/network_config.rs @@ -2,7 +2,6 @@ use crate::Multiaddr; use libp2p::gossipsub::{GossipsubConfig, GossipsubConfigBuilder}; use libp2p::secio; use std::fmt; -use types::Topic; #[derive(Clone)] /// Network configuration for lighthouse. @@ -20,6 +19,8 @@ pub struct NetworkConfig { 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 { @@ -37,6 +38,7 @@ impl Default for NetworkConfig { boot_nodes: Vec::new(), local_private_key: secio::SecioKeyPair::secp256k1_generated().unwrap(), client_version: version::version(), + topics: vec![String::from("beacon_chain")], } } } diff --git a/beacon_node/libp2p/src/service.rs b/beacon_node/libp2p/src/service.rs index dceb62511..26154beb6 100644 --- a/beacon_node/libp2p/src/service.rs +++ b/beacon_node/libp2p/src/service.rs @@ -12,9 +12,10 @@ use libp2p::core::{ }; use libp2p::{core, secio, Transport}; use libp2p::{PeerId, Swarm}; -use slog::{debug, info, warn}; +use slog::{debug, info, trace, warn}; use std::io::{Error, ErrorKind}; use std::time::Duration; +use types::{Topic, TopicBuilder}; /// The configuration and state of the libp2p components for the beacon node. pub struct Service { @@ -33,7 +34,7 @@ impl Service { let local_private_key = config.local_private_key; let local_peer_id = local_private_key.to_peer_id(); - debug!(log, "Local peer id: {:?}", local_peer_id); + info!(log, "Local peer id: {:?}", local_peer_id); let mut swarm = { // Set up the transport @@ -67,6 +68,20 @@ impl Service { }; } + // 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); + Ok(Service { local_peer_id, swarm, diff --git a/eth2/types/src/lib.rs b/eth2/types/src/lib.rs index 47235a15b..2cf61cfec 100644 --- a/eth2/types/src/lib.rs +++ b/eth2/types/src/lib.rs @@ -73,6 +73,6 @@ pub type AttesterMap = HashMap<(u64, u64), Vec>; pub type ProposerMap = HashMap; pub use bls::{AggregatePublicKey, AggregateSignature, Keypair, PublicKey, Signature}; -pub use libp2p::floodsub::Topic; +pub use libp2p::floodsub::{Topic, TopicBuilder}; pub use libp2p::multiaddr; pub use libp2p::Multiaddr;