Add logging to libp2p behaviour

This commit is contained in:
Age Manning 2019-03-21 13:34:37 +11:00
parent 71dca8af36
commit 7f976124df
No known key found for this signature in database
GPG Key ID: 05EED64B79E06A93

View File

@ -11,6 +11,7 @@ use libp2p::{
tokio_io::{AsyncRead, AsyncWrite}, tokio_io::{AsyncRead, AsyncWrite},
NetworkBehaviour, PeerId, NetworkBehaviour, PeerId,
}; };
use slog::{debug, o};
use types::Topic; use types::Topic;
/// Builds the network behaviour for the libp2p Swarm. /// Builds the network behaviour for the libp2p Swarm.
@ -27,6 +28,9 @@ pub struct Behaviour<TSubstream: AsyncRead + AsyncWrite> {
identify: Identify<TSubstream>, identify: Identify<TSubstream>,
#[behaviour(ignore)] #[behaviour(ignore)]
events: Vec<BehaviourEvent>, events: Vec<BehaviourEvent>,
#[behaviour(ignore)]
/// Logger for behaviour actions.
log: slog::Logger,
} }
// Implement the NetworkBehaviourEventProcess trait so that we can derive NetworkBehaviour for Behaviour // Implement the NetworkBehaviourEventProcess trait so that we can derive NetworkBehaviour for Behaviour
@ -70,6 +74,10 @@ impl<TSubstream: AsyncRead + AsyncWrite> NetworkBehaviourEventProcess<IdentifyEv
peer_id, mut info, .. peer_id, mut info, ..
} => { } => {
if info.listen_addrs.len() > 20 { if info.listen_addrs.len() > 20 {
debug!(
self.log,
"More than 20 peers have been identified, truncating"
);
info.listen_addrs.truncate(20); info.listen_addrs.truncate(20);
} }
self.events.push(BehaviourEvent::Identified(peer_id, info)); self.events.push(BehaviourEvent::Identified(peer_id, info));
@ -84,6 +92,7 @@ impl<TSubstream: AsyncRead + AsyncWrite> Behaviour<TSubstream> {
pub fn new(local_public_key: PublicKey, net_conf: &NetworkConfig, log: &slog::Logger) -> Self { 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 local_peer_id = local_public_key.clone().into_peer_id();
let identify_config = net_conf.identify_config.clone(); let identify_config = net_conf.identify_config.clone();
let behaviour_log = log.new(o!());
Behaviour { Behaviour {
gossipsub: Gossipsub::new(local_peer_id, net_conf.gs_config.clone()), gossipsub: Gossipsub::new(local_peer_id, net_conf.gs_config.clone()),
@ -94,6 +103,7 @@ impl<TSubstream: AsyncRead + AsyncWrite> Behaviour<TSubstream> {
local_public_key, local_public_key,
), ),
events: Vec::new(), events: Vec::new(),
log: behaviour_log,
} }
} }