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

Public gossipsub api #1677

Merged
merged 7 commits into from
Aug 13, 2020
Merged
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: 2 additions & 0 deletions protocols/gossipsub/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# 0.21.0 [unreleased]

- Add public API to list topics and peers. [PR 1677](https://github.com/libp2p/rust-libp2p/pull/1677).

- Add message signing and extended privacy/validation configurations. [PR 1583](https://github.com/libp2p/rust-libp2p/pull/1583).

- `Debug` instance for `Gossipsub`. [PR 1673](https://github.com/libp2p/rust-libp2p/pull/1673).
Expand Down
27 changes: 23 additions & 4 deletions protocols/gossipsub/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,25 @@ impl Gossipsub {
}
}

/// Lists the hashes of the topics we are currently subscribed to.
pub fn topics(&self) -> impl Iterator<Item = &TopicHash> {
self.mesh.keys()
}

/// Lists peers for a certain topic hash.
pub fn peers(&self, topic_hash: &TopicHash) -> impl Iterator<Item = &PeerId> {
self.mesh.get(topic_hash).into_iter().map(|x| x.into_iter()).flatten()
}

/// Lists all peers for any topic.
pub fn all_peers(&self) -> impl Iterator<Item = &PeerId> {
let mut res = BTreeSet::new();
for peers in self.mesh.values() {
res.extend(peers);
}
res.into_iter()
}

/// Subscribe to a topic.
///
/// Returns true if the subscription worked. Returns false if we were already subscribed.
Expand Down Expand Up @@ -1508,10 +1527,10 @@ impl fmt::Debug for Gossipsub {
impl fmt::Debug for PublishConfig {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
PublishConfig::Signing { author, .. } => f.write_fmt(format_args!("PublishConfig::Signing({})", author)),
PublishConfig::Author(author) => f.write_fmt(format_args!("PublishConfig::Author({})", author)),
PublishConfig::RandomAuthor => f.write_fmt(format_args!("PublishConfig::RandomAuthor")),
PublishConfig::Anonymous => f.write_fmt(format_args!("PublishConfig::Anonymous")),
PublishConfig::Signing { author, .. } => f.write_fmt(format_args!("PublishConfig::Signing({})", author)),
PublishConfig::Author(author) => f.write_fmt(format_args!("PublishConfig::Author({})", author)),
PublishConfig::RandomAuthor => f.write_fmt(format_args!("PublishConfig::RandomAuthor")),
PublishConfig::Anonymous => f.write_fmt(format_args!("PublishConfig::Anonymous")),
}
}
}
Expand Down
23 changes: 23 additions & 0 deletions protocols/gossipsub/src/behaviour/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -895,4 +895,27 @@ mod tests {
// Peers should be removed to reach mesh_n
assert_eq!(gs.mesh.get(&topics[0]).unwrap().len(), config.mesh_n);
}

// Some very basic test of public api methods.
#[test]
fn test_public_api() {
rklaehn marked this conversation as resolved.
Show resolved Hide resolved
let (gs, peers, topic_hashes) =
build_and_inject_nodes(4, vec![String::from("topic1")], true);
let peers = peers.into_iter().collect::<BTreeSet<_>>();

assert_eq!(
gs.topics().cloned().collect::<Vec<_>>(), topic_hashes,
"Expected topics to match registered topic."
);

assert_eq!(
gs.peers(&TopicHash::from_raw("topic1")).cloned().collect::<BTreeSet<_>>(), peers,
"Expected peers for a registered topic to contain all peers."
);

assert_eq!(
gs.all_peers().cloned().collect::<BTreeSet<_>>(), peers,
"Expected all_peers to contain all peers."
);
}
}