Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debug instance for the gossipsub behaviour #1673

Merged
merged 7 commits into from
Jul 29, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
13 changes: 12 additions & 1 deletion protocols/gossipsub/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,19 @@ impl GossipsubConfigBuilder {

impl std::fmt::Debug for GossipsubConfig {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
struct StringOrBytes<'a>(&'a [u8]);

impl<'a> std::fmt::Debug for StringOrBytes<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if let Ok(text) = std::str::from_utf8(self.0) {
write!(f, "\"{}\"", text)
} else {
write!(f, "{}", hex_fmt::HexFmt(self.0))
}
}
}
rklaehn marked this conversation as resolved.
Show resolved Hide resolved
let mut builder = f.debug_struct("GossipsubConfig");
let _ = builder.field("protocol_id", &self.protocol_id);
let _ = builder.field("protocol_id", &StringOrBytes(&self.protocol_id));
rklaehn marked this conversation as resolved.
Show resolved Hide resolved
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 @@ extern crate fnv;

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", &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