Debug instance for the gossipsub behaviour (#1673)

* Add debug instances for MessageCache and GossipSub behaviour

* Manual impl of Debug for GossipsubMessage

* Add pretty printing of protocol_id in debug

* Use hex_fmt instead of hex

* Inline StringOrBytes helper struct

Since it is used only once here

* Limit data of gossipsub msg to 20 bytes

Otherwise they might become very large and useless for debugging
This commit is contained in:
Rüdiger Klaehn
2020-07-29 09:25:53 +02:00
committed by GitHub
parent 967f39656a
commit 9662929f38
5 changed files with 31 additions and 4 deletions

View File

@ -29,7 +29,7 @@ use futures::prelude::*;
use futures_codec::{Decoder, Encoder, Framed};
use libp2p_core::{InboundUpgrade, OutboundUpgrade, PeerId, UpgradeInfo};
use prost::Message as ProtobufMessage;
use std::{borrow::Cow, io, iter, pin::Pin};
use std::{borrow::Cow, fmt, io, iter, pin::Pin};
use unsigned_varint::codec;
/// Implementation of the `ConnectionUpgrade` for the Gossipsub protocol.
@ -336,7 +336,7 @@ impl Into<String> for MessageId {
}
/// A message received by the gossipsub system.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct GossipsubMessage {
/// Id of the peer that published this message.
pub source: PeerId,
@ -353,6 +353,17 @@ pub struct GossipsubMessage {
pub topics: Vec<TopicHash>,
}
impl fmt::Debug for GossipsubMessage {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("GossipsubMessage")
.field("data",&format_args!("{:<20}", &hex_fmt::HexFmt(&self.data)))
.field("source", &self.source)
.field("sequence_number", &self.sequence_number)
.field("topics", &self.topics)
.finish()
}
}
/// A subscription received by the gossipsub system.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct GossipsubSubscription {