From f74ebc8d5372d0a2490c65558d87f30b9e27b1b5 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Thu, 2 Sep 2021 16:08:26 +1000 Subject: [PATCH] Make event_process = false the default --- examples/chat-tokio.rs | 1 + examples/chat.rs | 78 +++++++++++++++---------- examples/distributed-key-value-store.rs | 1 + examples/file-sharing.rs | 2 +- examples/ipfs-private.rs | 1 + protocols/relay/examples/relay.rs | 2 +- protocols/relay/tests/lib.rs | 7 ++- swarm-derive/CHANGELOG.md | 2 + swarm-derive/src/lib.rs | 2 +- swarm-derive/tests/test.rs | 15 +++-- 10 files changed, 72 insertions(+), 39 deletions(-) diff --git a/examples/chat-tokio.rs b/examples/chat-tokio.rs index 202b5b39156..e5238e785e8 100644 --- a/examples/chat-tokio.rs +++ b/examples/chat-tokio.rs @@ -87,6 +87,7 @@ async fn main() -> Result<(), Box> { // requires the implementations of `NetworkBehaviourEventProcess` for // the events of each behaviour. #[derive(NetworkBehaviour)] + #[behaviour(event_process = true)] struct MyBehaviour { floodsub: Floodsub, mdns: Mdns, diff --git a/examples/chat.rs b/examples/chat.rs index 18ef72b96b8..7343732f8c4 100644 --- a/examples/chat.rs +++ b/examples/chat.rs @@ -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::{ @@ -83,6 +83,7 @@ async fn main() -> Result<(), Box> { // 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, @@ -93,36 +94,21 @@ async fn main() -> Result<(), Box> { ignored_member: bool, } - impl NetworkBehaviourEventProcess 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 for OutEvent { + fn from(v: MdnsEvent) -> Self { + Self::Mdns(v) } } - impl NetworkBehaviourEventProcess 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 for OutEvent { + fn from(v: FloodsubEvent) -> Self { + Self::Floodsub(v) } } @@ -166,11 +152,41 @@ async fn main() -> Result<(), Box> { } 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, } diff --git a/examples/distributed-key-value-store.rs b/examples/distributed-key-value-store.rs index 2e5fa5a8531..cea3ca2a56b 100644 --- a/examples/distributed-key-value-store.rs +++ b/examples/distributed-key-value-store.rs @@ -71,6 +71,7 @@ async fn main() -> Result<(), Box> { // We create a custom network behaviour that combines Kademlia and mDNS. #[derive(NetworkBehaviour)] + #[behaviour(event_process = true)] struct MyBehaviour { kademlia: Kademlia, mdns: Mdns, diff --git a/examples/file-sharing.rs b/examples/file-sharing.rs index 2bcdbe8719f..65ba87feaef 100644 --- a/examples/file-sharing.rs +++ b/examples/file-sharing.rs @@ -582,7 +582,7 @@ mod network { } #[derive(NetworkBehaviour)] - #[behaviour(event_process = false, out_event = "ComposedEvent")] + #[behaviour(out_event = "ComposedEvent")] struct ComposedBehaviour { request_response: RequestResponse, kademlia: Kademlia, diff --git a/examples/ipfs-private.rs b/examples/ipfs-private.rs index f3c7afd496c..8c58ab5f882 100644 --- a/examples/ipfs-private.rs +++ b/examples/ipfs-private.rs @@ -164,6 +164,7 @@ fn main() -> Result<(), Box> { // We create a custom network behaviour that combines gossipsub, ping and identify. #[derive(NetworkBehaviour)] + #[behaviour(event_process = true)] struct MyBehaviour { gossipsub: Gossipsub, identify: Identify, diff --git a/protocols/relay/examples/relay.rs b/protocols/relay/examples/relay.rs index 5b8de71702b..b6cea26d891 100644 --- a/protocols/relay/examples/relay.rs +++ b/protocols/relay/examples/relay.rs @@ -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, diff --git a/protocols/relay/tests/lib.rs b/protocols/relay/tests/lib.rs index 29a0cfd35ed..561ec6d0f31 100644 --- a/protocols/relay/tests/lib.rs +++ b/protocols/relay/tests/lib.rs @@ -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, @@ -1195,6 +1199,7 @@ impl NetworkBehaviourEventProcess<()> for CombinedBehaviour { } #[derive(NetworkBehaviour)] +#[behaviour(event_process = true)] struct CombinedKeepAliveBehaviour { relay: Relay, keep_alive: DummyBehaviour, diff --git a/swarm-derive/CHANGELOG.md b/swarm-derive/CHANGELOG.md index 335d0fb28e8..dc31bd83453 100644 --- a/swarm-derive/CHANGELOG.md +++ b/swarm-derive/CHANGELOG.md @@ -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] diff --git a/swarm-derive/src/lib.rs b/swarm-derive/src/lib.rs index eef08d15c07..b80d3d6a811 100644 --- a/swarm-derive/src/lib.rs +++ b/swarm-derive/src/lib.rs @@ -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 { diff --git a/swarm-derive/tests/test.rs b/swarm-derive/tests/test.rs index ef457c83776..829c9b71e34 100644 --- a/swarm-derive/tests/test.rs +++ b/swarm-derive/tests/test.rs @@ -38,6 +38,7 @@ fn empty() { fn one_field() { #[allow(dead_code)] #[derive(NetworkBehaviour)] + #[behaviour(event_process = true)] struct Foo { ping: libp2p::ping::Ping, } @@ -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, @@ -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, @@ -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)] @@ -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, @@ -173,7 +177,7 @@ fn custom_polling() { fn custom_event_no_polling() { #[allow(dead_code)] #[derive(NetworkBehaviour)] - #[behaviour(out_event = "Vec")] + #[behaviour(out_event = "Vec", event_process = true)] struct Foo { ping: libp2p::ping::Ping, identify: libp2p::identify::Identify, @@ -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, @@ -236,6 +240,7 @@ fn custom_event_and_polling() { fn where_clause() { #[allow(dead_code)] #[derive(NetworkBehaviour)] + #[behaviour(event_process = true)] struct Foo { ping: libp2p::ping::Ping, bar: T, @@ -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, } @@ -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,