Skip to content

Commit

Permalink
refactor(gossipsub): revise symbol naming to follow conventions (#3303)
Browse files Browse the repository at this point in the history
Changes regarding the  #2217
  • Loading branch information
StemCll authored Jan 27, 2023
1 parent e2b3c11 commit ab59af4
Show file tree
Hide file tree
Showing 18 changed files with 639 additions and 633 deletions.
26 changes: 12 additions & 14 deletions examples/gossipsub-chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,6 @@
use async_std::io;
use futures::{prelude::*, select};
use libp2p::gossipsub::MessageId;
use libp2p::gossipsub::{
Gossipsub, GossipsubEvent, GossipsubMessage, IdentTopic as Topic, MessageAuthenticity,
ValidationMode,
};
use libp2p::{
gossipsub, identity, mdns, swarm::NetworkBehaviour, swarm::SwarmEvent, PeerId, Swarm,
};
Expand All @@ -73,31 +68,34 @@ async fn main() -> Result<(), Box<dyn Error>> {
// We create a custom network behaviour that combines Gossipsub and Mdns.
#[derive(NetworkBehaviour)]
struct MyBehaviour {
gossipsub: Gossipsub,
gossipsub: gossipsub::Behaviour,
mdns: mdns::async_io::Behaviour,
}

// To content-address message, we can take the hash of message and use it as an ID.
let message_id_fn = |message: &GossipsubMessage| {
let message_id_fn = |message: &gossipsub::Message| {
let mut s = DefaultHasher::new();
message.data.hash(&mut s);
MessageId::from(s.finish().to_string())
gossipsub::MessageId::from(s.finish().to_string())
};

// Set a custom gossipsub configuration
let gossipsub_config = gossipsub::GossipsubConfigBuilder::default()
let gossipsub_config = gossipsub::ConfigBuilder::default()
.heartbeat_interval(Duration::from_secs(10)) // This is set to aid debugging by not cluttering the log space
.validation_mode(ValidationMode::Strict) // This sets the kind of message validation. The default is Strict (enforce message signing)
.validation_mode(gossipsub::ValidationMode::Strict) // This sets the kind of message validation. The default is Strict (enforce message signing)
.message_id_fn(message_id_fn) // content-address messages. No two messages of the same content will be propagated.
.build()
.expect("Valid config");

// build a gossipsub network behaviour
let mut gossipsub = Gossipsub::new(MessageAuthenticity::Signed(local_key), gossipsub_config)
.expect("Correct configuration");
let mut gossipsub = gossipsub::Behaviour::new(
gossipsub::MessageAuthenticity::Signed(local_key),
gossipsub_config,
)
.expect("Correct configuration");

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

// subscribes to our topic
gossipsub.subscribe(&topic)?;
Expand Down Expand Up @@ -140,7 +138,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
swarm.behaviour_mut().gossipsub.remove_explicit_peer(&peer_id);
}
},
SwarmEvent::Behaviour(MyBehaviourEvent::Gossipsub(GossipsubEvent::Message {
SwarmEvent::Behaviour(MyBehaviourEvent::Gossipsub(gossipsub::Event::Message {
propagation_source: peer_id,
message_id: id,
message,
Expand Down
19 changes: 9 additions & 10 deletions examples/ipfs-private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ use either::Either;
use futures::{prelude::*, select};
use libp2p::{
core::{muxing::StreamMuxerBox, transport, transport::upgrade::Version},
gossipsub::{self, Gossipsub, GossipsubConfigBuilder, GossipsubEvent, MessageAuthenticity},
identify, identity,
gossipsub, identify, identity,
multiaddr::Protocol,
noise, ping,
pnet::{PnetConfig, PreSharedKey},
Expand Down Expand Up @@ -150,19 +149,19 @@ async fn main() -> Result<(), Box<dyn Error>> {
#[derive(NetworkBehaviour)]
#[behaviour(out_event = "MyBehaviourEvent")]
struct MyBehaviour {
gossipsub: Gossipsub,
gossipsub: gossipsub::Behaviour,
identify: identify::Behaviour,
ping: ping::Behaviour,
}

enum MyBehaviourEvent {
Gossipsub(GossipsubEvent),
Gossipsub(gossipsub::Event),
Identify(identify::Event),
Ping(ping::Event),
}

impl From<GossipsubEvent> for MyBehaviourEvent {
fn from(event: GossipsubEvent) -> Self {
impl From<gossipsub::Event> for MyBehaviourEvent {
fn from(event: gossipsub::Event) -> Self {
MyBehaviourEvent::Gossipsub(event)
}
}
Expand All @@ -181,13 +180,13 @@ async fn main() -> Result<(), Box<dyn Error>> {

// Create a Swarm to manage peers and events
let mut swarm = {
let gossipsub_config = GossipsubConfigBuilder::default()
let gossipsub_config = gossipsub::ConfigBuilder::default()
.max_transmit_size(262144)
.build()
.expect("valid config");
let mut behaviour = MyBehaviour {
gossipsub: Gossipsub::new(
MessageAuthenticity::Signed(local_key.clone()),
gossipsub: gossipsub::Behaviour::new(
gossipsub::MessageAuthenticity::Signed(local_key.clone()),
gossipsub_config,
)
.expect("Valid configuration"),
Expand Down Expand Up @@ -236,7 +235,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
SwarmEvent::Behaviour(MyBehaviourEvent::Identify(event)) => {
println!("identify: {event:?}");
}
SwarmEvent::Behaviour(MyBehaviourEvent::Gossipsub(GossipsubEvent::Message {
SwarmEvent::Behaviour(MyBehaviourEvent::Gossipsub(gossipsub::Event::Message {
propagation_source: peer_id,
message_id: id,
message,
Expand Down
6 changes: 3 additions & 3 deletions misc/metrics/src/gossipsub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ impl Metrics {
}
}

impl super::Recorder<libp2p_gossipsub::GossipsubEvent> for Metrics {
fn record(&self, event: &libp2p_gossipsub::GossipsubEvent) {
if let libp2p_gossipsub::GossipsubEvent::Message { .. } = event {
impl super::Recorder<libp2p_gossipsub::Event> for Metrics {
fn record(&self, event: &libp2p_gossipsub::Event) {
if let libp2p_gossipsub::Event::Message { .. } = event {
self.messages.inc();
}
}
Expand Down
4 changes: 2 additions & 2 deletions misc/metrics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ impl Recorder<libp2p_dcutr::Event> for Metrics {

#[cfg(feature = "gossipsub")]
#[cfg(not(target_os = "unknown"))]
impl Recorder<libp2p_gossipsub::GossipsubEvent> for Metrics {
fn record(&self, event: &libp2p_gossipsub::GossipsubEvent) {
impl Recorder<libp2p_gossipsub::Event> for Metrics {
fn record(&self, event: &libp2p_gossipsub::Event) {
self.gossipsub.record(event)
}
}
Expand Down
8 changes: 8 additions & 0 deletions protocols/gossipsub/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,16 @@

- Initialize `ProtocolConfig` via `GossipsubConfig`. See [PR 3381].

- Rename types as per [discussion 2174].
`Gossipsub` has been renamed to `Behaviour`.
The `Gossipsub` prefix has been removed from various types like `GossipsubConfig` or `GossipsubMessage`.
It is preferred to import the gossipsub protocol as a module (`use libp2p::gossipsub;`), and refer to its types via `gossipsub::`.
For example: `gossipsub::Behaviour` or `gossipsub::RawMessage`. See [PR 3303].

[PR 3207]: https://github.com/libp2p/rust-libp2p/pull/3207/
[PR 3303]: https://github.com/libp2p/rust-libp2p/pull/3303/
[PR 3381]: https://github.com/libp2p/rust-libp2p/pull/3381/
[discussion 2174]: https://github.com/libp2p/rust-libp2p/discussions/2174

# 0.43.0

Expand Down
Loading

0 comments on commit ab59af4

Please sign in to comment.