Skip to content

Commit

Permalink
Debug instance for the gossipsub behaviour (#1673)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
rklaehn committed Jul 29, 2020
1 parent 967f396 commit 9662929
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 4 deletions.
1 change: 1 addition & 0 deletions protocols/gossipsub/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ base64 = "0.11.0"
lru = "0.4.3"
smallvec = "1.1.0"
prost = "0.6.1"
hex_fmt = "0.3.0"

[dev-dependencies]
async-std = "1.6.2"
Expand Down
1 change: 1 addition & 0 deletions protocols/gossipsub/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ use wasm_timer::{Instant, Interval};

mod tests;

#[derive(Debug)]
/// Network behaviour that handles the gossipsub protocol.
pub struct Gossipsub {
/// Configuration providing gossipsub performance parameters.
Expand Down
6 changes: 5 additions & 1 deletion protocols/gossipsub/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,11 @@ impl GossipsubConfigBuilder {
impl std::fmt::Debug for GossipsubConfig {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
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_gossip", &self.history_gossip);
let _ = builder.field("mesh_n", &self.mesh_n);
Expand Down
12 changes: 11 additions & 1 deletion protocols/gossipsub/src/mcache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

use crate::protocol::{GossipsubMessage, MessageId};
use crate::topic::TopicHash;
use std::collections::HashMap;
use std::{collections::HashMap, fmt};

/// CacheEntry stored in the history.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
Expand All @@ -40,6 +40,16 @@ pub struct MessageCache {
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.
impl MessageCache {
pub fn new(
Expand Down
15 changes: 13 additions & 2 deletions protocols/gossipsub/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand All @@ -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 {
Expand Down

0 comments on commit 9662929

Please sign in to comment.