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

@ -26,6 +26,7 @@ base64 = "0.11.0"
lru = "0.4.3" lru = "0.4.3"
smallvec = "1.1.0" smallvec = "1.1.0"
prost = "0.6.1" prost = "0.6.1"
hex_fmt = "0.3.0"
[dev-dependencies] [dev-dependencies]
async-std = "1.6.2" async-std = "1.6.2"

View File

@ -51,6 +51,7 @@ use wasm_timer::{Instant, Interval};
mod tests; mod tests;
#[derive(Debug)]
/// Network behaviour that handles the gossipsub protocol. /// Network behaviour that handles the gossipsub protocol.
pub struct Gossipsub { pub struct Gossipsub {
/// Configuration providing gossipsub performance parameters. /// Configuration providing gossipsub performance parameters.

View File

@ -232,7 +232,11 @@ impl GossipsubConfigBuilder {
impl std::fmt::Debug for GossipsubConfig { impl std::fmt::Debug for GossipsubConfig {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut builder = f.debug_struct("GossipsubConfig"); let mut builder = f.debug_struct("GossipsubConfig");
let _ = builder.field("protocol_id", &self.protocol_id); let _ = if let Ok(text) = std::str::from_utf8(&self.protocol_id) {
builder.field("protocol_id", &text)
} else {
builder.field("protocol_id", &hex_fmt::HexFmt(&self.protocol_id))
};
let _ = builder.field("history_length", &self.history_length); let _ = builder.field("history_length", &self.history_length);
let _ = builder.field("history_gossip", &self.history_gossip); let _ = builder.field("history_gossip", &self.history_gossip);
let _ = builder.field("mesh_n", &self.mesh_n); let _ = builder.field("mesh_n", &self.mesh_n);

View File

@ -22,7 +22,7 @@
use crate::protocol::{GossipsubMessage, MessageId}; use crate::protocol::{GossipsubMessage, MessageId};
use crate::topic::TopicHash; use crate::topic::TopicHash;
use std::collections::HashMap; use std::{collections::HashMap, fmt};
/// CacheEntry stored in the history. /// CacheEntry stored in the history.
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
@ -40,6 +40,16 @@ pub struct MessageCache {
msg_id: fn(&GossipsubMessage) -> MessageId, msg_id: fn(&GossipsubMessage) -> MessageId,
} }
impl fmt::Debug for MessageCache {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("MessageCache")
.field("msgs", &self.msgs)
.field("history", &self.history)
.field("gossip", &self.gossip)
.finish()
}
}
/// Implementation of the MessageCache. /// Implementation of the MessageCache.
impl MessageCache { impl MessageCache {
pub fn new( pub fn new(

View File

@ -29,7 +29,7 @@ use futures::prelude::*;
use futures_codec::{Decoder, Encoder, Framed}; use futures_codec::{Decoder, Encoder, Framed};
use libp2p_core::{InboundUpgrade, OutboundUpgrade, PeerId, UpgradeInfo}; use libp2p_core::{InboundUpgrade, OutboundUpgrade, PeerId, UpgradeInfo};
use prost::Message as ProtobufMessage; 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; use unsigned_varint::codec;
/// Implementation of the `ConnectionUpgrade` for the Gossipsub protocol. /// Implementation of the `ConnectionUpgrade` for the Gossipsub protocol.
@ -336,7 +336,7 @@ impl Into<String> for MessageId {
} }
/// A message received by the gossipsub system. /// A message received by the gossipsub system.
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Clone, PartialEq, Eq, Hash)]
pub struct GossipsubMessage { pub struct GossipsubMessage {
/// Id of the peer that published this message. /// Id of the peer that published this message.
pub source: PeerId, pub source: PeerId,
@ -353,6 +353,17 @@ pub struct GossipsubMessage {
pub topics: Vec<TopicHash>, 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. /// A subscription received by the gossipsub system.
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct GossipsubSubscription { pub struct GossipsubSubscription {