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
// the events of each behaviour.
#[derive(NetworkBehaviour)]
#[behaviour(event_process = true)]
struct MyBehaviour {
floodsub: Floodsub,
mdns: Mdns,

View File

@ -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<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,
@ -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)
}
}
@ -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,
}

View File

@ -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,

View File

@ -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>,

View File

@ -163,6 +163,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,

View File

@ -209,7 +209,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::Behaviour,

View File

@ -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::Behaviour,
@ -1195,6 +1199,7 @@ impl NetworkBehaviourEventProcess<()> for CombinedBehaviour {
}
#[derive(NetworkBehaviour)]
#[behaviour(event_process = true)]
struct CombinedKeepAliveBehaviour {
relay: Relay,
keep_alive: DummyBehaviour,

View File

@ -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]

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.
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 {

View File

@ -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<String>")]
#[behaviour(out_event = "Vec<String>", 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<T: Copy> {
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,