From e8e4c4ab9baaa1334c662bc9ef2a5a3b04e65095 Mon Sep 17 00:00:00 2001 From: Age Manning Date: Thu, 7 Mar 2019 11:43:55 +1100 Subject: [PATCH] Adds basic structure for swarm behaviour and topology. --- beacon_node/libp2p/Cargo.toml | 1 + beacon_node/libp2p/src/service.rs | 33 ++++++++++++++++++++++++------- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/beacon_node/libp2p/Cargo.toml b/beacon_node/libp2p/Cargo.toml index b32eed1a6..fff7dc82d 100644 --- a/beacon_node/libp2p/Cargo.toml +++ b/beacon_node/libp2p/Cargo.toml @@ -9,3 +9,4 @@ edition = "2018" libp2p = { git = "https://github.com/SigP/rust-libp2p", branch = "gossipsub" } slog = "2.4.1" version = { path = "../version" } +tokio = "0.1.16" diff --git a/beacon_node/libp2p/src/service.rs b/beacon_node/libp2p/src/service.rs index 53c566187..528d24ce8 100644 --- a/beacon_node/libp2p/src/service.rs +++ b/beacon_node/libp2p/src/service.rs @@ -1,14 +1,17 @@ use crate::NetworkConfig; -use libp2p::gossipsub::GossipsubEvent; -use libp2p::PeerId; +use libp2p::core::{muxing::StreamMuxer, nodes::Substream}; +use libp2p::gossipsub::{Gossipsub, GossipsubConfig, GossipsubEvent}; use libp2p::{build_tcp_ws_secio_mplex_yamux, core, secio, Transport}; +use libp2p::{core::swarm::NetworkBehaviour, PeerId, Swarm}; use slog::debug; use std::error; /// The configuration and state of the libp2p components for the beacon node. pub struct Service { + /// The libp2p Swarm handler. + swarm: String, /// This node's PeerId. - peer_id: PeerId, + local_peer_id: PeerId, } impl Service { @@ -16,15 +19,22 @@ impl Service { 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); + let local_peer_id = local_private_key.to_peer_id(); + debug!(log, "Local peer id: {:?}", local_peer_id); // Set up the transport let transport = build_transport(local_private_key); + // Set up gossipsub routing + let behaviour = build_behaviour(local_peer_id, config.gs_config); + // Set up Topology + let topology = local_peer_id; - //let transport = libp2p:: + let swarm = Swarm::new(transport, behaviour, topology); - Service { peer_id } + Service { + local_peer_id, + swarm, + } } } @@ -48,3 +58,12 @@ fn build_transport( // in the future. build_tcp_ws_secio_mplex_yamux(local_private_key) } + +/// Builds the network behaviour for the libp2p Swarm. +fn build_behaviour( + local_peer_id: PeerId, + config: GossipsubConfig, +) -> impl NetworkBehaviour { + // TODO: Add Kademlia/Peer discovery + Gossipsub::new(local_peer_id, config) +}