Add default topics and initial topic subscription

This commit is contained in:
Age Manning 2019-03-13 15:37:44 +11:00
parent 8ee3523abd
commit 23a8fbfc74
No known key found for this signature in database
GPG Key ID: 05EED64B79E06A93
4 changed files with 26 additions and 4 deletions

View File

@ -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<TSubstream: AsyncRead + AsyncWrite> Behaviour<TSubstream> {
}
impl<TSubstream: AsyncRead + AsyncWrite> Behaviour<TSubstream> {
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

View File

@ -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<String>,
}
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")],
}
}
}

View File

@ -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,

View File

@ -73,6 +73,6 @@ pub type AttesterMap = HashMap<(u64, u64), Vec<usize>>;
pub type ProposerMap = HashMap<u64, usize>;
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;