diff --git a/protocols/gossipsub/Cargo.toml b/protocols/gossipsub/Cargo.toml index 165d1737..59ccb548 100644 --- a/protocols/gossipsub/Cargo.toml +++ b/protocols/gossipsub/Cargo.toml @@ -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" diff --git a/protocols/gossipsub/src/behaviour.rs b/protocols/gossipsub/src/behaviour.rs index a959cad9..4f1ae52a 100644 --- a/protocols/gossipsub/src/behaviour.rs +++ b/protocols/gossipsub/src/behaviour.rs @@ -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. diff --git a/protocols/gossipsub/src/config.rs b/protocols/gossipsub/src/config.rs index a01895ef..b00070a9 100644 --- a/protocols/gossipsub/src/config.rs +++ b/protocols/gossipsub/src/config.rs @@ -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); diff --git a/protocols/gossipsub/src/mcache.rs b/protocols/gossipsub/src/mcache.rs index 6c284ae7..52c0be08 100644 --- a/protocols/gossipsub/src/mcache.rs +++ b/protocols/gossipsub/src/mcache.rs @@ -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)] @@ -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( diff --git a/protocols/gossipsub/src/protocol.rs b/protocols/gossipsub/src/protocol.rs index 14a8c6dd..53a75063 100644 --- a/protocols/gossipsub/src/protocol.rs +++ b/protocols/gossipsub/src/protocol.rs @@ -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 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, } +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 {