2019-03-17 12:14:28 +00:00
|
|
|
use crate::rpc::{RPCEvent, RPCMessage, Rpc};
|
2019-03-21 01:41:05 +00:00
|
|
|
use crate::NetworkConfig;
|
2019-03-07 05:17:06 +00:00
|
|
|
use futures::prelude::*;
|
|
|
|
use libp2p::{
|
2019-03-21 01:41:05 +00:00
|
|
|
core::{
|
|
|
|
swarm::{NetworkBehaviourAction, NetworkBehaviourEventProcess},
|
|
|
|
PublicKey,
|
|
|
|
},
|
|
|
|
gossipsub::{Gossipsub, GossipsubEvent},
|
|
|
|
identify::{protocol::IdentifyInfo, Identify, IdentifyEvent},
|
2019-03-21 02:42:02 +00:00
|
|
|
ping::{Ping, PingEvent},
|
2019-03-07 05:17:06 +00:00
|
|
|
tokio_io::{AsyncRead, AsyncWrite},
|
|
|
|
NetworkBehaviour, PeerId,
|
|
|
|
};
|
2019-03-21 02:34:37 +00:00
|
|
|
use slog::{debug, o};
|
2019-03-13 04:37:44 +00:00
|
|
|
use types::Topic;
|
2019-03-07 05:17:06 +00:00
|
|
|
|
|
|
|
/// Builds the network behaviour for the libp2p Swarm.
|
|
|
|
/// Implements gossipsub message routing.
|
|
|
|
#[derive(NetworkBehaviour)]
|
2019-03-12 06:28:11 +00:00
|
|
|
#[behaviour(out_event = "BehaviourEvent", poll_method = "poll")]
|
2019-03-07 05:17:06 +00:00
|
|
|
pub struct Behaviour<TSubstream: AsyncRead + AsyncWrite> {
|
2019-03-21 01:41:05 +00:00
|
|
|
/// The routing pub-sub mechanism for eth2.
|
2019-03-07 05:17:06 +00:00
|
|
|
gossipsub: Gossipsub<TSubstream>,
|
|
|
|
// TODO: Add Kademlia for peer discovery
|
|
|
|
/// The events generated by this behaviour to be consumed in the swarm poll.
|
2019-03-14 15:48:09 +00:00
|
|
|
serenity_rpc: Rpc<TSubstream>,
|
2019-03-21 01:41:05 +00:00
|
|
|
/// Allows discovery of IP addresses for peers on the network.
|
|
|
|
identify: Identify<TSubstream>,
|
2019-03-21 02:42:02 +00:00
|
|
|
/// Keep regular connection to peers and disconnect if absent.
|
|
|
|
// TODO: Keepalive, likely remove this later.
|
|
|
|
// TODO: Make the ping time customizeable.
|
|
|
|
ping: Ping<TSubstream>,
|
2019-03-07 05:17:06 +00:00
|
|
|
#[behaviour(ignore)]
|
2019-03-12 06:28:11 +00:00
|
|
|
events: Vec<BehaviourEvent>,
|
2019-03-21 02:34:37 +00:00
|
|
|
/// Logger for behaviour actions.
|
2019-03-21 02:42:02 +00:00
|
|
|
#[behaviour(ignore)]
|
2019-03-21 02:34:37 +00:00
|
|
|
log: slog::Logger,
|
2019-03-07 05:17:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Implement the NetworkBehaviourEventProcess trait so that we can derive NetworkBehaviour for Behaviour
|
|
|
|
impl<TSubstream: AsyncRead + AsyncWrite> NetworkBehaviourEventProcess<GossipsubEvent>
|
|
|
|
for Behaviour<TSubstream>
|
|
|
|
{
|
|
|
|
fn inject_event(&mut self, event: GossipsubEvent) {
|
2019-03-12 06:28:11 +00:00
|
|
|
match event {
|
|
|
|
GossipsubEvent::Message(message) => {
|
|
|
|
let gs_message = String::from_utf8_lossy(&message.data);
|
|
|
|
// TODO: Remove this type - debug only
|
|
|
|
self.events
|
|
|
|
.push(BehaviourEvent::Message(gs_message.to_string()))
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
2019-03-07 05:17:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-17 12:14:28 +00:00
|
|
|
impl<TSubstream: AsyncRead + AsyncWrite> NetworkBehaviourEventProcess<RPCMessage>
|
2019-03-14 15:48:09 +00:00
|
|
|
for Behaviour<TSubstream>
|
|
|
|
{
|
2019-03-17 12:14:28 +00:00
|
|
|
fn inject_event(&mut self, event: RPCMessage) {
|
|
|
|
match event {
|
|
|
|
RPCMessage::PeerDialed(peer_id) => {
|
|
|
|
self.events.push(BehaviourEvent::PeerDialed(peer_id))
|
|
|
|
}
|
2019-03-19 01:47:36 +00:00
|
|
|
RPCMessage::RPC(peer_id, rpc_event) => {
|
|
|
|
self.events.push(BehaviourEvent::RPC(peer_id, rpc_event))
|
|
|
|
}
|
2019-03-17 12:14:28 +00:00
|
|
|
}
|
2019-03-14 15:48:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-21 01:41:05 +00:00
|
|
|
impl<TSubstream: AsyncRead + AsyncWrite> NetworkBehaviourEventProcess<IdentifyEvent>
|
|
|
|
for Behaviour<TSubstream>
|
|
|
|
{
|
|
|
|
fn inject_event(&mut self, event: IdentifyEvent) {
|
|
|
|
match event {
|
|
|
|
IdentifyEvent::Identified {
|
|
|
|
peer_id, mut info, ..
|
|
|
|
} => {
|
|
|
|
if info.listen_addrs.len() > 20 {
|
2019-03-21 02:34:37 +00:00
|
|
|
debug!(
|
|
|
|
self.log,
|
|
|
|
"More than 20 peers have been identified, truncating"
|
|
|
|
);
|
2019-03-21 01:41:05 +00:00
|
|
|
info.listen_addrs.truncate(20);
|
|
|
|
}
|
|
|
|
self.events.push(BehaviourEvent::Identified(peer_id, info));
|
|
|
|
}
|
|
|
|
IdentifyEvent::Error { .. } => {}
|
|
|
|
IdentifyEvent::SendBack { .. } => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-21 02:42:02 +00:00
|
|
|
impl<TSubstream: AsyncRead + AsyncWrite> NetworkBehaviourEventProcess<PingEvent>
|
|
|
|
for Behaviour<TSubstream>
|
|
|
|
{
|
|
|
|
fn inject_event(&mut self, _event: PingEvent) {
|
|
|
|
// not interested in ping responses at the moment.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-07 05:17:06 +00:00
|
|
|
impl<TSubstream: AsyncRead + AsyncWrite> Behaviour<TSubstream> {
|
2019-03-21 01:41:05 +00:00
|
|
|
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();
|
2019-03-21 02:34:37 +00:00
|
|
|
let behaviour_log = log.new(o!());
|
2019-03-21 01:41:05 +00:00
|
|
|
|
2019-03-07 05:17:06 +00:00
|
|
|
Behaviour {
|
2019-03-21 01:41:05 +00:00
|
|
|
gossipsub: Gossipsub::new(local_peer_id, net_conf.gs_config.clone()),
|
2019-03-17 12:14:28 +00:00
|
|
|
serenity_rpc: Rpc::new(log),
|
2019-03-21 01:41:05 +00:00
|
|
|
identify: Identify::new(
|
|
|
|
identify_config.version,
|
|
|
|
identify_config.user_agent,
|
|
|
|
local_public_key,
|
|
|
|
),
|
2019-03-21 02:42:02 +00:00
|
|
|
ping: Ping::new(),
|
2019-03-07 05:17:06 +00:00
|
|
|
events: Vec::new(),
|
2019-03-21 02:34:37 +00:00
|
|
|
log: behaviour_log,
|
2019-03-07 05:17:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-12 06:28:11 +00:00
|
|
|
/// Consumes the events list when polled.
|
|
|
|
fn poll<TBehaviourIn>(
|
|
|
|
&mut self,
|
|
|
|
) -> Async<NetworkBehaviourAction<TBehaviourIn, BehaviourEvent>> {
|
2019-03-07 05:17:06 +00:00
|
|
|
if !self.events.is_empty() {
|
|
|
|
return Async::Ready(NetworkBehaviourAction::GenerateEvent(self.events.remove(0)));
|
|
|
|
}
|
|
|
|
|
|
|
|
Async::NotReady
|
|
|
|
}
|
|
|
|
}
|
2019-03-12 06:28:11 +00:00
|
|
|
|
2019-03-18 12:34:44 +00:00
|
|
|
/// Implements the combined behaviour for the libp2p service.
|
2019-03-12 06:28:11 +00:00
|
|
|
impl<TSubstream: AsyncRead + AsyncWrite> Behaviour<TSubstream> {
|
2019-03-18 12:34:44 +00:00
|
|
|
/// Subscribes to a gossipsub topic.
|
2019-03-13 04:37:44 +00:00
|
|
|
pub fn subscribe(&mut self, topic: Topic) -> bool {
|
|
|
|
self.gossipsub.subscribe(topic)
|
|
|
|
}
|
|
|
|
|
2019-03-18 12:34:44 +00:00
|
|
|
/// Sends an RPC Request/Response via the RPC protocol.
|
|
|
|
pub fn send_rpc(&mut self, peer_id: PeerId, rpc_event: RPCEvent) {
|
|
|
|
self.serenity_rpc.send_rpc(peer_id, rpc_event);
|
2019-03-12 06:28:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The types of events than can be obtained from polling the behaviour.
|
|
|
|
pub enum BehaviourEvent {
|
2019-03-19 01:47:36 +00:00
|
|
|
RPC(PeerId, RPCEvent),
|
2019-03-17 12:14:28 +00:00
|
|
|
PeerDialed(PeerId),
|
2019-03-21 01:41:05 +00:00
|
|
|
Identified(PeerId, IdentifyInfo),
|
2019-03-12 06:28:11 +00:00
|
|
|
// TODO: This is a stub at the moment
|
|
|
|
Message(String),
|
|
|
|
}
|