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

Allow gossipsub Topic construction from any string #1645

Closed
Closed
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion examples/gossipsub-chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ fn main() -> Result<(), Box<dyn Error>> {
let transport = libp2p::build_development_transport(local_key)?;

// Create a Gossipsub topic
let topic = Topic::new("test-net".into());
let topic = Topic::new("test-net");

// Create a Swarm to manage peers and events
let mut swarm = {
Expand Down
2 changes: 1 addition & 1 deletion examples/ipfs-private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ fn main() -> Result<(), Box<dyn Error>> {
let transport = build_transport(local_key.clone(), psk);

// Create a Gosspipsub topic
let gossipsub_topic = gossipsub::Topic::new("chat".into());
let gossipsub_topic = gossipsub::Topic::new("chat");

// We create a custom network behaviour that combines gossipsub, ping and identify.
#[derive(NetworkBehaviour)]
Expand Down
2 changes: 1 addition & 1 deletion protocols/gossipsub/src/behaviour/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ mod tests {
let mut gs: Gossipsub = Gossipsub::new(PeerId::random(), 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").no_hash().clone();
let mut peers = vec![];
for _ in 0..20 {
peers.push(PeerId::random())
Expand Down
20 changes: 10 additions & 10 deletions protocols/gossipsub/src/mcache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,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").no_hash().clone();
let topic2_hash = Topic::new("topic2").no_hash().clone();

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

Expand All @@ -192,8 +192,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").no_hash().clone();
let topic2_hash = Topic::new("topic2").no_hash().clone();

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

Expand Down Expand Up @@ -239,8 +239,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").no_hash().clone();
let topic2_hash = Topic::new("topic2").no_hash().clone();

// Build the message
for i in 0..10 {
Expand All @@ -263,8 +263,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").no_hash().clone();
let topic2_hash = Topic::new("topic2").no_hash().clone();
// Build the message
for i in 0..10 {
let m = gen_testm(i, vec![topic1_hash.clone(), topic2_hash.clone()]);
Expand All @@ -289,8 +289,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").no_hash().clone();
let topic2_hash = Topic::new("topic2").no_hash().clone();
// Build the message
for i in 0..10 {
let m = gen_testm(i, vec![topic1_hash.clone(), topic2_hash.clone()]);
Expand Down
4 changes: 2 additions & 2 deletions protocols/gossipsub/src/topic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ pub struct Topic {
}

impl Topic {
pub fn new(topic: String) -> Self {
Topic { topic }
pub fn new(topic: impl Into<String>) -> Self {
Topic { topic: topic.into() }
}

/// Creates a `TopicHash` by SHA256 hashing the topic then base64 encoding the
Expand Down
2 changes: 1 addition & 1 deletion protocols/gossipsub/tests/smoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,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 Down