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

Make event_process = false the default #2214

Merged
merged 2 commits into from
Sep 14, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 examples/chat-tokio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
// requires the implementations of `NetworkBehaviourEventProcess` for
// the events of each behaviour.
#[derive(NetworkBehaviour)]
#[behaviour(event_process = true)]
struct MyBehaviour {
floodsub: Floodsub,
mdns: Mdns,
Expand Down
78 changes: 47 additions & 31 deletions examples/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ use libp2p::{
floodsub::{self, Floodsub, FloodsubEvent},
identity,
mdns::{Mdns, MdnsConfig, MdnsEvent},
swarm::{NetworkBehaviourEventProcess, SwarmEvent},
swarm::SwarmEvent,
Multiaddr, NetworkBehaviour, PeerId, Swarm,
};
use std::{
Expand Down Expand Up @@ -83,6 +83,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
// Use the derive to generate delegating NetworkBehaviour impl and require the
// NetworkBehaviourEventProcess implementations below.
#[derive(NetworkBehaviour)]
#[behaviour(out_event = "OutEvent")]
struct MyBehaviour {
floodsub: Floodsub,
mdns: Mdns,
Expand All @@ -93,36 +94,21 @@ async fn main() -> Result<(), Box<dyn Error>> {
ignored_member: bool,
}

impl NetworkBehaviourEventProcess<FloodsubEvent> for MyBehaviour {
// Called when `floodsub` produces an event.
fn inject_event(&mut self, message: FloodsubEvent) {
if let FloodsubEvent::Message(message) = message {
println!(
"Received: '{:?}' from {:?}",
String::from_utf8_lossy(&message.data),
message.source
);
}
#[derive(Debug)]
enum OutEvent {
Floodsub(FloodsubEvent),
Mdns(MdnsEvent),
}

impl From<MdnsEvent> for OutEvent {
fn from(v: MdnsEvent) -> Self {
Self::Mdns(v)
}
}

impl NetworkBehaviourEventProcess<MdnsEvent> for MyBehaviour {
// Called when `mdns` produces an event.
fn inject_event(&mut self, event: MdnsEvent) {
match event {
MdnsEvent::Discovered(list) => {
for (peer, _) in list {
self.floodsub.add_node_to_partial_view(peer);
}
}
MdnsEvent::Expired(list) => {
for (peer, _) in list {
if !self.mdns.has_node(&peer) {
self.floodsub.remove_node_from_partial_view(&peer);
}
}
}
}
impl From<FloodsubEvent> for OutEvent {
fn from(v: FloodsubEvent) -> Self {
Self::Floodsub(v)
}
}

Expand Down Expand Up @@ -166,11 +152,41 @@ async fn main() -> Result<(), Box<dyn Error>> {
}
loop {
match swarm.poll_next_unpin(cx) {
Poll::Ready(Some(event)) => {
if let SwarmEvent::NewListenAddr { address, .. } = event {
println!("Listening on {:?}", address);
Poll::Ready(Some(SwarmEvent::NewListenAddr { address, .. })) => {
println!("Listening on {:?}", address);
}
Poll::Ready(Some(SwarmEvent::Behaviour(OutEvent::Floodsub(
FloodsubEvent::Message(message),
)))) => {
println!(
"Received: '{:?}' from {:?}",
String::from_utf8_lossy(&message.data),
message.source
);
}
Poll::Ready(Some(SwarmEvent::Behaviour(OutEvent::Mdns(
MdnsEvent::Discovered(list),
)))) => {
for (peer, _) in list {
swarm
.behaviour_mut()
.floodsub
.add_node_to_partial_view(peer);
}
}
Poll::Ready(Some(SwarmEvent::Behaviour(OutEvent::Mdns(MdnsEvent::Expired(
list,
))))) => {
for (peer, _) in list {
if !swarm.behaviour_mut().mdns.has_node(&peer) {
swarm
.behaviour_mut()
.floodsub
.remove_node_from_partial_view(&peer);
}
}
}
Poll::Ready(Some(_)) => {}
Poll::Ready(None) => return Poll::Ready(Ok(())),
Poll::Pending => break,
}
Expand Down
1 change: 1 addition & 0 deletions examples/distributed-key-value-store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ async fn main() -> Result<(), Box<dyn Error>> {

// We create a custom network behaviour that combines Kademlia and mDNS.
#[derive(NetworkBehaviour)]
#[behaviour(event_process = true)]
struct MyBehaviour {
kademlia: Kademlia<MemoryStore>,
mdns: Mdns,
Expand Down
2 changes: 1 addition & 1 deletion examples/file-sharing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ mod network {
}

#[derive(NetworkBehaviour)]
#[behaviour(event_process = false, out_event = "ComposedEvent")]
#[behaviour(out_event = "ComposedEvent")]
struct ComposedBehaviour {
request_response: RequestResponse<FileExchangeCodec>,
kademlia: Kademlia<MemoryStore>,
Expand Down
1 change: 1 addition & 0 deletions examples/ipfs-private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ fn main() -> Result<(), Box<dyn Error>> {

// We create a custom network behaviour that combines gossipsub, ping and identify.
#[derive(NetworkBehaviour)]
#[behaviour(event_process = true)]
struct MyBehaviour {
gossipsub: Gossipsub,
identify: Identify,
Expand Down
2 changes: 1 addition & 1 deletion protocols/relay/examples/relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ fn get_client_listen_address(opt: &Opt) -> String {
}

#[derive(NetworkBehaviour)]
#[behaviour(out_event = "Event", event_process = false)]
#[behaviour(out_event = "Event")]
struct Behaviour {
relay: Relay,
ping: Ping,
Expand Down
7 changes: 6 additions & 1 deletion protocols/relay/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1129,7 +1129,11 @@ fn yield_incoming_connection_through_correct_listener() {
}

#[derive(NetworkBehaviour)]
#[behaviour(out_event = "CombinedEvent", poll_method = "poll")]
#[behaviour(
out_event = "CombinedEvent",
poll_method = "poll",
event_process = true
)]
struct CombinedBehaviour {
relay: Relay,
ping: Ping,
Expand Down Expand Up @@ -1195,6 +1199,7 @@ impl NetworkBehaviourEventProcess<()> for CombinedBehaviour {
}

#[derive(NetworkBehaviour)]
#[behaviour(event_process = true)]
struct CombinedKeepAliveBehaviour {
relay: Relay,
keep_alive: DummyBehaviour,
Expand Down
2 changes: 2 additions & 0 deletions swarm-derive/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

- Update to latest `libp2p-swarm` changes (see [PR 2191]).

- Make `event_process = false` the default.

[PR 2191]: https://github.com/libp2p/rust-libp2p/pull/2191

# 0.24.0 [2021-07-12]
Expand Down
2 changes: 1 addition & 1 deletion swarm-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> TokenStream {

// Whether or not we require the `NetworkBehaviourEventProcess` trait to be implemented.
let event_process = {
let mut event_process = true; // Default to true for backwards compatibility
let mut event_process = false;

for meta_items in ast.attrs.iter().filter_map(get_meta_items) {
for meta_item in meta_items {
Expand Down
15 changes: 11 additions & 4 deletions swarm-derive/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ fn empty() {
fn one_field() {
#[allow(dead_code)]
#[derive(NetworkBehaviour)]
#[behaviour(event_process = true)]
struct Foo {
ping: libp2p::ping::Ping,
}
Expand All @@ -56,6 +57,7 @@ fn one_field() {
fn two_fields() {
#[allow(dead_code)]
#[derive(NetworkBehaviour)]
#[behaviour(event_process = true)]
struct Foo {
ping: libp2p::ping::Ping,
identify: libp2p::identify::Identify,
Expand All @@ -79,6 +81,7 @@ fn two_fields() {
fn three_fields() {
#[allow(dead_code)]
#[derive(NetworkBehaviour)]
#[behaviour(event_process = true)]
struct Foo {
ping: libp2p::ping::Ping,
identify: libp2p::identify::Identify,
Expand Down Expand Up @@ -109,6 +112,7 @@ fn three_fields() {
fn three_fields_non_last_ignored() {
#[allow(dead_code)]
#[derive(NetworkBehaviour)]
#[behaviour(event_process = true)]
struct Foo {
ping: libp2p::ping::Ping,
#[behaviour(ignore)]
Expand All @@ -134,7 +138,7 @@ fn three_fields_non_last_ignored() {
fn custom_polling() {
#[allow(dead_code)]
#[derive(NetworkBehaviour)]
#[behaviour(poll_method = "foo")]
#[behaviour(poll_method = "foo", event_process = true)]
struct Foo {
ping: libp2p::ping::Ping,
identify: libp2p::identify::Identify,
Expand Down Expand Up @@ -173,7 +177,7 @@ fn custom_polling() {
fn custom_event_no_polling() {
#[allow(dead_code)]
#[derive(NetworkBehaviour)]
#[behaviour(out_event = "Vec<String>")]
#[behaviour(out_event = "Vec<String>", event_process = true)]
struct Foo {
ping: libp2p::ping::Ping,
identify: libp2p::identify::Identify,
Expand All @@ -197,7 +201,7 @@ fn custom_event_no_polling() {
fn custom_event_and_polling() {
#[allow(dead_code)]
#[derive(NetworkBehaviour)]
#[behaviour(poll_method = "foo", out_event = "String")]
#[behaviour(poll_method = "foo", out_event = "String", event_process = true)]
struct Foo {
ping: libp2p::ping::Ping,
identify: libp2p::identify::Identify,
Expand Down Expand Up @@ -236,6 +240,7 @@ fn custom_event_and_polling() {
fn where_clause() {
#[allow(dead_code)]
#[derive(NetworkBehaviour)]
#[behaviour(event_process = true)]
struct Foo<T: Copy> {
ping: libp2p::ping::Ping,
bar: T,
Expand All @@ -248,12 +253,14 @@ fn nested_derives_with_import() {

#[allow(dead_code)]
#[derive(NetworkBehaviour)]
#[behaviour(event_process = true)]
struct Foo {
ping: libp2p::ping::Ping,
}

#[allow(dead_code)]
#[derive(NetworkBehaviour)]
#[behaviour(event_process = true)]
struct Bar {
foo: Foo,
}
Expand Down Expand Up @@ -293,7 +300,7 @@ fn event_process_false() {

#[allow(dead_code)]
#[derive(NetworkBehaviour)]
#[behaviour(out_event = "BehaviourOutEvent", event_process = false)]
#[behaviour(out_event = "BehaviourOutEvent")]
struct Foo {
ping: libp2p::ping::Ping,
identify: libp2p::identify::Identify,
Expand Down