swarm-derive/: Make event_process = false the default (#2214)

Co-authored-by: Max Inden <mail@max-inden.de>
This commit is contained in:
Thomas Eizinger
2021-09-14 23:28:08 +10:00
committed by GitHub
parent b79fd02f0b
commit e83e1b3d0b
10 changed files with 72 additions and 39 deletions

View File

@ -87,6 +87,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
// requires the implementations of `NetworkBehaviourEventProcess` for // requires the implementations of `NetworkBehaviourEventProcess` for
// the events of each behaviour. // the events of each behaviour.
#[derive(NetworkBehaviour)] #[derive(NetworkBehaviour)]
#[behaviour(event_process = true)]
struct MyBehaviour { struct MyBehaviour {
floodsub: Floodsub, floodsub: Floodsub,
mdns: Mdns, mdns: Mdns,

View File

@ -55,7 +55,7 @@ use libp2p::{
floodsub::{self, Floodsub, FloodsubEvent}, floodsub::{self, Floodsub, FloodsubEvent},
identity, identity,
mdns::{Mdns, MdnsConfig, MdnsEvent}, mdns::{Mdns, MdnsConfig, MdnsEvent},
swarm::{NetworkBehaviourEventProcess, SwarmEvent}, swarm::SwarmEvent,
Multiaddr, NetworkBehaviour, PeerId, Swarm, Multiaddr, NetworkBehaviour, PeerId, Swarm,
}; };
use std::{ use std::{
@ -83,6 +83,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
// Use the derive to generate delegating NetworkBehaviour impl and require the // Use the derive to generate delegating NetworkBehaviour impl and require the
// NetworkBehaviourEventProcess implementations below. // NetworkBehaviourEventProcess implementations below.
#[derive(NetworkBehaviour)] #[derive(NetworkBehaviour)]
#[behaviour(out_event = "OutEvent")]
struct MyBehaviour { struct MyBehaviour {
floodsub: Floodsub, floodsub: Floodsub,
mdns: Mdns, mdns: Mdns,
@ -93,36 +94,21 @@ async fn main() -> Result<(), Box<dyn Error>> {
ignored_member: bool, ignored_member: bool,
} }
impl NetworkBehaviourEventProcess<FloodsubEvent> for MyBehaviour { #[derive(Debug)]
// Called when `floodsub` produces an event. enum OutEvent {
fn inject_event(&mut self, message: FloodsubEvent) { Floodsub(FloodsubEvent),
if let FloodsubEvent::Message(message) = message { Mdns(MdnsEvent),
println!(
"Received: '{:?}' from {:?}",
String::from_utf8_lossy(&message.data),
message.source
);
} }
impl From<MdnsEvent> for OutEvent {
fn from(v: MdnsEvent) -> Self {
Self::Mdns(v)
} }
} }
impl NetworkBehaviourEventProcess<MdnsEvent> for MyBehaviour { impl From<FloodsubEvent> for OutEvent {
// Called when `mdns` produces an event. fn from(v: FloodsubEvent) -> Self {
fn inject_event(&mut self, event: MdnsEvent) { Self::Floodsub(v)
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);
}
}
}
}
} }
} }
@ -166,11 +152,41 @@ async fn main() -> Result<(), Box<dyn Error>> {
} }
loop { loop {
match swarm.poll_next_unpin(cx) { match swarm.poll_next_unpin(cx) {
Poll::Ready(Some(event)) => { Poll::Ready(Some(SwarmEvent::NewListenAddr { address, .. })) => {
if let SwarmEvent::NewListenAddr { address, .. } = event {
println!("Listening on {:?}", 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::Ready(None) => return Poll::Ready(Ok(())),
Poll::Pending => break, Poll::Pending => break,
} }

View File

@ -71,6 +71,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
// We create a custom network behaviour that combines Kademlia and mDNS. // We create a custom network behaviour that combines Kademlia and mDNS.
#[derive(NetworkBehaviour)] #[derive(NetworkBehaviour)]
#[behaviour(event_process = true)]
struct MyBehaviour { struct MyBehaviour {
kademlia: Kademlia<MemoryStore>, kademlia: Kademlia<MemoryStore>,
mdns: Mdns, mdns: Mdns,

View File

@ -582,7 +582,7 @@ mod network {
} }
#[derive(NetworkBehaviour)] #[derive(NetworkBehaviour)]
#[behaviour(event_process = false, out_event = "ComposedEvent")] #[behaviour(out_event = "ComposedEvent")]
struct ComposedBehaviour { struct ComposedBehaviour {
request_response: RequestResponse<FileExchangeCodec>, request_response: RequestResponse<FileExchangeCodec>,
kademlia: Kademlia<MemoryStore>, kademlia: Kademlia<MemoryStore>,

View File

@ -163,6 +163,7 @@ fn main() -> Result<(), Box<dyn Error>> {
// We create a custom network behaviour that combines gossipsub, ping and identify. // We create a custom network behaviour that combines gossipsub, ping and identify.
#[derive(NetworkBehaviour)] #[derive(NetworkBehaviour)]
#[behaviour(event_process = true)]
struct MyBehaviour { struct MyBehaviour {
gossipsub: Gossipsub, gossipsub: Gossipsub,
identify: Identify, identify: Identify,

View File

@ -209,7 +209,7 @@ fn get_client_listen_address(opt: &Opt) -> String {
} }
#[derive(NetworkBehaviour)] #[derive(NetworkBehaviour)]
#[behaviour(out_event = "Event", event_process = false)] #[behaviour(out_event = "Event")]
struct Behaviour { struct Behaviour {
relay: Relay, relay: Relay,
ping: ping::Behaviour, ping: ping::Behaviour,

View File

@ -1129,7 +1129,11 @@ fn yield_incoming_connection_through_correct_listener() {
} }
#[derive(NetworkBehaviour)] #[derive(NetworkBehaviour)]
#[behaviour(out_event = "CombinedEvent", poll_method = "poll")] #[behaviour(
out_event = "CombinedEvent",
poll_method = "poll",
event_process = true
)]
struct CombinedBehaviour { struct CombinedBehaviour {
relay: Relay, relay: Relay,
ping: ping::Behaviour, ping: ping::Behaviour,
@ -1195,6 +1199,7 @@ impl NetworkBehaviourEventProcess<()> for CombinedBehaviour {
} }
#[derive(NetworkBehaviour)] #[derive(NetworkBehaviour)]
#[behaviour(event_process = true)]
struct CombinedKeepAliveBehaviour { struct CombinedKeepAliveBehaviour {
relay: Relay, relay: Relay,
keep_alive: DummyBehaviour, keep_alive: DummyBehaviour,

View File

@ -2,6 +2,8 @@
- Update to latest `libp2p-swarm` changes (see [PR 2191]). - 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 [PR 2191]: https://github.com/libp2p/rust-libp2p/pull/2191
# 0.24.0 [2021-07-12] # 0.24.0 [2021-07-12]

View File

@ -71,7 +71,7 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> TokenStream {
// Whether or not we require the `NetworkBehaviourEventProcess` trait to be implemented. // Whether or not we require the `NetworkBehaviourEventProcess` trait to be implemented.
let event_process = { 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_items in ast.attrs.iter().filter_map(get_meta_items) {
for meta_item in meta_items { for meta_item in meta_items {

View File

@ -38,6 +38,7 @@ fn empty() {
fn one_field() { fn one_field() {
#[allow(dead_code)] #[allow(dead_code)]
#[derive(NetworkBehaviour)] #[derive(NetworkBehaviour)]
#[behaviour(event_process = true)]
struct Foo { struct Foo {
ping: libp2p::ping::Ping, ping: libp2p::ping::Ping,
} }
@ -56,6 +57,7 @@ fn one_field() {
fn two_fields() { fn two_fields() {
#[allow(dead_code)] #[allow(dead_code)]
#[derive(NetworkBehaviour)] #[derive(NetworkBehaviour)]
#[behaviour(event_process = true)]
struct Foo { struct Foo {
ping: libp2p::ping::Ping, ping: libp2p::ping::Ping,
identify: libp2p::identify::Identify, identify: libp2p::identify::Identify,
@ -79,6 +81,7 @@ fn two_fields() {
fn three_fields() { fn three_fields() {
#[allow(dead_code)] #[allow(dead_code)]
#[derive(NetworkBehaviour)] #[derive(NetworkBehaviour)]
#[behaviour(event_process = true)]
struct Foo { struct Foo {
ping: libp2p::ping::Ping, ping: libp2p::ping::Ping,
identify: libp2p::identify::Identify, identify: libp2p::identify::Identify,
@ -109,6 +112,7 @@ fn three_fields() {
fn three_fields_non_last_ignored() { fn three_fields_non_last_ignored() {
#[allow(dead_code)] #[allow(dead_code)]
#[derive(NetworkBehaviour)] #[derive(NetworkBehaviour)]
#[behaviour(event_process = true)]
struct Foo { struct Foo {
ping: libp2p::ping::Ping, ping: libp2p::ping::Ping,
#[behaviour(ignore)] #[behaviour(ignore)]
@ -134,7 +138,7 @@ fn three_fields_non_last_ignored() {
fn custom_polling() { fn custom_polling() {
#[allow(dead_code)] #[allow(dead_code)]
#[derive(NetworkBehaviour)] #[derive(NetworkBehaviour)]
#[behaviour(poll_method = "foo")] #[behaviour(poll_method = "foo", event_process = true)]
struct Foo { struct Foo {
ping: libp2p::ping::Ping, ping: libp2p::ping::Ping,
identify: libp2p::identify::Identify, identify: libp2p::identify::Identify,
@ -173,7 +177,7 @@ fn custom_polling() {
fn custom_event_no_polling() { fn custom_event_no_polling() {
#[allow(dead_code)] #[allow(dead_code)]
#[derive(NetworkBehaviour)] #[derive(NetworkBehaviour)]
#[behaviour(out_event = "Vec<String>")] #[behaviour(out_event = "Vec<String>", event_process = true)]
struct Foo { struct Foo {
ping: libp2p::ping::Ping, ping: libp2p::ping::Ping,
identify: libp2p::identify::Identify, identify: libp2p::identify::Identify,
@ -197,7 +201,7 @@ fn custom_event_no_polling() {
fn custom_event_and_polling() { fn custom_event_and_polling() {
#[allow(dead_code)] #[allow(dead_code)]
#[derive(NetworkBehaviour)] #[derive(NetworkBehaviour)]
#[behaviour(poll_method = "foo", out_event = "String")] #[behaviour(poll_method = "foo", out_event = "String", event_process = true)]
struct Foo { struct Foo {
ping: libp2p::ping::Ping, ping: libp2p::ping::Ping,
identify: libp2p::identify::Identify, identify: libp2p::identify::Identify,
@ -236,6 +240,7 @@ fn custom_event_and_polling() {
fn where_clause() { fn where_clause() {
#[allow(dead_code)] #[allow(dead_code)]
#[derive(NetworkBehaviour)] #[derive(NetworkBehaviour)]
#[behaviour(event_process = true)]
struct Foo<T: Copy> { struct Foo<T: Copy> {
ping: libp2p::ping::Ping, ping: libp2p::ping::Ping,
bar: T, bar: T,
@ -248,12 +253,14 @@ fn nested_derives_with_import() {
#[allow(dead_code)] #[allow(dead_code)]
#[derive(NetworkBehaviour)] #[derive(NetworkBehaviour)]
#[behaviour(event_process = true)]
struct Foo { struct Foo {
ping: libp2p::ping::Ping, ping: libp2p::ping::Ping,
} }
#[allow(dead_code)] #[allow(dead_code)]
#[derive(NetworkBehaviour)] #[derive(NetworkBehaviour)]
#[behaviour(event_process = true)]
struct Bar { struct Bar {
foo: Foo, foo: Foo,
} }
@ -293,7 +300,7 @@ fn event_process_false() {
#[allow(dead_code)] #[allow(dead_code)]
#[derive(NetworkBehaviour)] #[derive(NetworkBehaviour)]
#[behaviour(out_event = "BehaviourOutEvent", event_process = false)] #[behaviour(out_event = "BehaviourOutEvent")]
struct Foo { struct Foo {
ping: libp2p::ping::Ping, ping: libp2p::ping::Ping,
identify: libp2p::identify::Identify, identify: libp2p::identify::Identify,