Skip to content

Commit

Permalink
Update topic hashing structure
Browse files Browse the repository at this point in the history
  • Loading branch information
AgeManning committed May 27, 2020
1 parent 58f61b4 commit 238e305
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 26 deletions.
8 changes: 4 additions & 4 deletions protocols/gossipsub/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ impl Gossipsub {
/// Returns true if we were subscribed to this topic.
pub fn unsubscribe<H: Hasher>(&mut self, topic: Topic<H>) -> bool {
debug!("Unsubscribing from topic: {}", topic);
let topic_hash = topic.hash();
let topic_hash = &topic.hash();

if self.mesh.get(topic_hash).is_none() {
debug!("Already unsubscribed from topic: {:?}", topic_hash);
Expand Down Expand Up @@ -218,8 +218,8 @@ impl Gossipsub {
}

/// Publishes a message to the network.
pub fn publish<H: Hasher>(&mut self, topic: &Topic<H>, data: impl Into<Vec<u8>>) {
self.publish_many(iter::once(topic.clone()), data)
pub fn publish<H: Hasher>(&mut self, topic: Topic<H>, data: impl Into<Vec<u8>>) {
self.publish_many(iter::once(topic), data)
}

/// Publishes a message with multiple topics to the network.
Expand All @@ -234,7 +234,7 @@ impl Gossipsub {
// To be interoperable with the go-implementation this is treated as a 64-bit
// big-endian uint.
sequence_number: rand::random(),
topics: topic.into_iter().map(|t| self.topic_hash(t)).collect(),
topics: topic.into_iter().map(|t| t.hash()).collect(),
signature: None, // signature will get created when being published
key: None,
};
Expand Down
9 changes: 5 additions & 4 deletions protocols/gossipsub/src/behaviour/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#[cfg(test)]
mod tests {
use super::super::*;
use crate::IdentTopic as Topic;

// helper functions for testing

Expand All @@ -44,7 +45,7 @@ mod tests {
for t in topics {
let topic = Topic::new(t);
gs.subscribe(topic.clone());
topic_hashes.push(topic.no_hash().clone());
topic_hashes.push(topic.hash().clone());
}

// build and connect peer_no random peers
Expand Down Expand Up @@ -314,7 +315,7 @@ mod tests {

// publish on topic
let publish_data = vec![0; 42];
gs.publish(&Topic::new(publish_topic), publish_data);
gs.publish(Topic::new(publish_topic), publish_data);

// Collect all publish messages
let publishes = gs
Expand Down Expand Up @@ -367,7 +368,7 @@ mod tests {

// Publish on unsubscribed topic
let publish_data = vec![0; 42];
gs.publish(&Topic::new(fanout_topic.clone()), publish_data);
gs.publish(Topic::new(fanout_topic.clone()), publish_data);

assert_eq!(
gs.fanout
Expand Down Expand Up @@ -552,7 +553,7 @@ mod tests {
let mut gs: Gossipsub = Gossipsub::new(key, gs_config);

// create a topic and fill it with some peers
let topic_hash = Topic::new("Test".into()).no_hash().clone();
let topic_hash = Topic::new("Test").hash().clone();
let mut peers = vec![];
for _ in 0..20 {
peers.push(PeerId::random())
Expand Down
2 changes: 2 additions & 0 deletions protocols/gossipsub/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,5 @@ pub use self::behaviour::{Gossipsub, GossipsubEvent, GossipsubRpc};
pub use self::config::{GossipsubConfig, GossipsubConfigBuilder};
pub use self::protocol::{GossipsubMessage, MessageId};
pub use self::topic::{Hasher, Topic, TopicHash};
pub type IdentTopic = Topic<self::topic::IdentityHash>;
pub type Sha256Topic = Topic<self::topic::Sha256Hash>;
22 changes: 11 additions & 11 deletions protocols/gossipsub/src/mcache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ impl MessageCache {
#[cfg(test)]
mod tests {
use super::*;
use crate::{Topic, TopicHash};
use crate::{IdentTopic as Topic, TopicHash};
use libp2p_core::PeerId;

fn gen_testm(x: u64, topics: Vec<TopicHash>) -> GossipsubMessage {
Expand Down Expand Up @@ -173,8 +173,8 @@ mod tests {
fn test_put_get_one() {
let mut mc = MessageCache::new_default(10, 15);

let topic1_hash = Topic::new("topic1".into()).no_hash().clone();
let topic2_hash = Topic::new("topic2".into()).no_hash().clone();
let topic1_hash = Topic::new("topic1").hash().clone();
let topic2_hash = Topic::new("topic2").hash().clone();

let m = gen_testm(10, vec![topic1_hash, topic2_hash]);

Expand All @@ -199,8 +199,8 @@ mod tests {
fn test_get_wrong() {
let mut mc = MessageCache::new_default(10, 15);

let topic1_hash = Topic::new("topic1".into()).no_hash().clone();
let topic2_hash = Topic::new("topic2".into()).no_hash().clone();
let topic1_hash = Topic::new("topic1").hash().clone();
let topic2_hash = Topic::new("topic2").hash().clone();

let m = gen_testm(10, vec![topic1_hash, topic2_hash]);

Expand Down Expand Up @@ -246,8 +246,8 @@ mod tests {
fn test_shift() {
let mut mc = MessageCache::new_default(1, 5);

let topic1_hash = Topic::new("topic1".into()).no_hash().clone();
let topic2_hash = Topic::new("topic2".into()).no_hash().clone();
let topic1_hash = Topic::new("topic1").hash().clone();
let topic2_hash = Topic::new("topic2").hash().clone();

// Build the message
for i in 0..10 {
Expand All @@ -270,8 +270,8 @@ mod tests {
fn test_empty_shift() {
let mut mc = MessageCache::new_default(1, 5);

let topic1_hash = Topic::new("topic1".into()).no_hash().clone();
let topic2_hash = Topic::new("topic2".into()).no_hash().clone();
let topic1_hash = Topic::new("topic1").hash().clone();
let topic2_hash = Topic::new("topic2").hash().clone();
// Build the message
for i in 0..10 {
let m = gen_testm(i, vec![topic1_hash.clone(), topic2_hash.clone()]);
Expand All @@ -296,8 +296,8 @@ mod tests {
fn test_remove_last_from_shift() {
let mut mc = MessageCache::new_default(4, 5);

let topic1_hash = Topic::new("topic1".into()).no_hash().clone();
let topic2_hash = Topic::new("topic2".into()).no_hash().clone();
let topic1_hash = Topic::new("topic1").hash().clone();
let topic2_hash = Topic::new("topic2").hash().clone();
// Build the message
for i in 0..10 {
let m = gen_testm(i, vec![topic1_hash.clone(), topic2_hash.clone()]);
Expand Down
2 changes: 1 addition & 1 deletion protocols/gossipsub/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ impl Decoder for GossipsubCodec {
}

/// A type for gossipsub message ids.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct MessageId(pub String);

impl std::fmt::Display for MessageId {
Expand Down
15 changes: 12 additions & 3 deletions protocols/gossipsub/src/topic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ use std::fmt;
/// A generic trait that can be extended for various hashing types for a topic.
pub trait Hasher {
/// The function that takes a topic string and creates a topic hash.
fn hash(topic_string: String) -> TopicHash {}
fn hash(topic_string: String) -> TopicHash;
}

/// A type for representing topics who use the identity hash.
#[derive(Debug, Clone)]
pub struct IdentityHash {}
impl Hasher for IdentityHash {
/// Creates a `TopicHash` as a raw string.
Expand All @@ -39,8 +40,9 @@ impl Hasher for IdentityHash {
}
}

#[derive(Debug, Clone)]
pub struct Sha256Hash {}
impl Hasher for IdentityHash {
impl Hasher for Sha256Hash {
/// Creates a `TopicHash` by SHA256 hashing the topic then base64 encoding the
/// hash.
fn hash(topic_string: String) -> TopicHash {
Expand Down Expand Up @@ -85,10 +87,17 @@ pub struct Topic<H: Hasher> {
phantom_data: std::marker::PhantomData<H>,
}

impl<H: Hasher> From<Topic<H>> for TopicHash {
fn from(topic: Topic<H>) -> TopicHash {
topic.hash()
}
}

impl<H: Hasher> Topic<H> {
pub fn new(topic: impl Into<String>) -> Self {
Topic {
topic: topic.into(),
phantom_data: std::marker::PhantomData,
}
}

Expand All @@ -97,7 +106,7 @@ impl<H: Hasher> Topic<H> {
}
}

impl<H> fmt::Display for Topic<H> {
impl<H: Hasher> fmt::Display for Topic<H> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.topic)
}
Expand Down
6 changes: 3 additions & 3 deletions protocols/gossipsub/tests/smoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use libp2p_core::{
identity, multiaddr::Protocol, muxing::StreamMuxerBox, transport::MemoryTransport, upgrade,
Multiaddr, Transport,
};
use libp2p_gossipsub::{Gossipsub, GossipsubConfig, GossipsubEvent, Topic};
use libp2p_gossipsub::{Gossipsub, GossipsubConfig, GossipsubEvent, IdentTopic as Topic};
use libp2p_plaintext::PlainText2Config;
use libp2p_swarm::Swarm;
use libp2p_yamux as yamux;
Expand Down Expand Up @@ -174,7 +174,7 @@ fn multi_hop_propagation() {
let number_nodes = graph.nodes.len();

// Subscribe each node to the same topic.
let topic = Topic::new("test-net".into());
let topic = Topic::new("test-net");
for (_addr, node) in &mut graph.nodes {
node.subscribe(topic.clone());
}
Expand All @@ -193,7 +193,7 @@ fn multi_hop_propagation() {
});

// Publish a single message.
graph.nodes[0].1.publish(&topic, vec![1, 2, 3]);
graph.nodes[0].1.publish(topic, vec![1, 2, 3]);

// Wait for all nodes to receive the published message.
let mut received_msgs = 0;
Expand Down

0 comments on commit 238e305

Please sign in to comment.