Adds basic structure for swarm behaviour and topology.

This commit is contained in:
Age Manning 2019-03-07 11:43:55 +11:00
parent ac639c6427
commit e8e4c4ab9b
No known key found for this signature in database
GPG Key ID: 05EED64B79E06A93
2 changed files with 27 additions and 7 deletions

View File

@ -9,3 +9,4 @@ edition = "2018"
libp2p = { git = "https://github.com/SigP/rust-libp2p", branch = "gossipsub" } libp2p = { git = "https://github.com/SigP/rust-libp2p", branch = "gossipsub" }
slog = "2.4.1" slog = "2.4.1"
version = { path = "../version" } version = { path = "../version" }
tokio = "0.1.16"

View File

@ -1,14 +1,17 @@
use crate::NetworkConfig; use crate::NetworkConfig;
use libp2p::gossipsub::GossipsubEvent; use libp2p::core::{muxing::StreamMuxer, nodes::Substream};
use libp2p::PeerId; use libp2p::gossipsub::{Gossipsub, GossipsubConfig, GossipsubEvent};
use libp2p::{build_tcp_ws_secio_mplex_yamux, core, secio, Transport}; use libp2p::{build_tcp_ws_secio_mplex_yamux, core, secio, Transport};
use libp2p::{core::swarm::NetworkBehaviour, PeerId, Swarm};
use slog::debug; use slog::debug;
use std::error; use std::error;
/// The configuration and state of the libp2p components for the beacon node. /// The configuration and state of the libp2p components for the beacon node.
pub struct Service { pub struct Service {
/// The libp2p Swarm handler.
swarm: String,
/// This node's PeerId. /// This node's PeerId.
peer_id: PeerId, local_peer_id: PeerId,
} }
impl Service { impl Service {
@ -16,15 +19,22 @@ impl Service {
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;
let peer_id = local_private_key.to_peer_id(); let local_peer_id = local_private_key.to_peer_id();
debug!(log, "Local peer id: {:?}", peer_id); debug!(log, "Local peer id: {:?}", local_peer_id);
// 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
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. // in the future.
build_tcp_ws_secio_mplex_yamux(local_private_key) build_tcp_ws_secio_mplex_yamux(local_private_key)
} }
/// Builds the network behaviour for the libp2p Swarm.
fn build_behaviour<TSubstream>(
local_peer_id: PeerId,
config: GossipsubConfig,
) -> impl NetworkBehaviour {
// TODO: Add Kademlia/Peer discovery
Gossipsub::new(local_peer_id, config)
}