feat(swarm): rename NetworkBehaviourAction to ToSwarm

Resolves #3123.

Pull-Request: #3658.
This commit is contained in:
Thomas Eizinger
2023-03-24 14:43:49 +01:00
committed by GitHub
parent 7ffa63b656
commit dcbc04e89e
32 changed files with 698 additions and 807 deletions

View File

@ -30,8 +30,8 @@ use libp2p_core::{Endpoint, Multiaddr};
use libp2p_identity::PeerId;
use libp2p_swarm::behaviour::{ConnectionClosed, ConnectionEstablished, FromSwarm};
use libp2p_swarm::{
dial_opts::DialOpts, ConnectionDenied, ConnectionId, NetworkBehaviour, NetworkBehaviourAction,
NotifyHandler, OneShotHandler, PollParameters, THandler, THandlerInEvent, THandlerOutEvent,
dial_opts::DialOpts, ConnectionDenied, ConnectionId, NetworkBehaviour, NotifyHandler,
OneShotHandler, PollParameters, THandler, THandlerInEvent, THandlerOutEvent, ToSwarm,
};
use log::warn;
use smallvec::SmallVec;
@ -42,7 +42,7 @@ use std::{collections::VecDeque, iter};
/// Network behaviour that handles the floodsub protocol.
pub struct Floodsub {
/// Events that need to be yielded to the outside when polling.
events: VecDeque<NetworkBehaviourAction<FloodsubEvent, FloodsubRpc>>,
events: VecDeque<ToSwarm<FloodsubEvent, FloodsubRpc>>,
config: FloodsubConfig,
@ -87,23 +87,22 @@ impl Floodsub {
// Send our topics to this node if we're already connected to it.
if self.connected_peers.contains_key(&peer_id) {
for topic in self.subscribed_topics.iter().cloned() {
self.events
.push_back(NetworkBehaviourAction::NotifyHandler {
peer_id,
handler: NotifyHandler::Any,
event: FloodsubRpc {
messages: Vec::new(),
subscriptions: vec![FloodsubSubscription {
topic,
action: FloodsubSubscriptionAction::Subscribe,
}],
},
});
self.events.push_back(ToSwarm::NotifyHandler {
peer_id,
handler: NotifyHandler::Any,
event: FloodsubRpc {
messages: Vec::new(),
subscriptions: vec![FloodsubSubscription {
topic,
action: FloodsubSubscriptionAction::Subscribe,
}],
},
});
}
}
if self.target_peers.insert(peer_id) {
self.events.push_back(NetworkBehaviourAction::Dial {
self.events.push_back(ToSwarm::Dial {
opts: DialOpts::peer_id(peer_id).build(),
});
}
@ -124,18 +123,17 @@ impl Floodsub {
}
for peer in self.connected_peers.keys() {
self.events
.push_back(NetworkBehaviourAction::NotifyHandler {
peer_id: *peer,
handler: NotifyHandler::Any,
event: FloodsubRpc {
messages: Vec::new(),
subscriptions: vec![FloodsubSubscription {
topic: topic.clone(),
action: FloodsubSubscriptionAction::Subscribe,
}],
},
});
self.events.push_back(ToSwarm::NotifyHandler {
peer_id: *peer,
handler: NotifyHandler::Any,
event: FloodsubRpc {
messages: Vec::new(),
subscriptions: vec![FloodsubSubscription {
topic: topic.clone(),
action: FloodsubSubscriptionAction::Subscribe,
}],
},
});
}
self.subscribed_topics.push(topic);
@ -156,18 +154,17 @@ impl Floodsub {
self.subscribed_topics.remove(pos);
for peer in self.connected_peers.keys() {
self.events
.push_back(NetworkBehaviourAction::NotifyHandler {
peer_id: *peer,
handler: NotifyHandler::Any,
event: FloodsubRpc {
messages: Vec::new(),
subscriptions: vec![FloodsubSubscription {
topic: topic.clone(),
action: FloodsubSubscriptionAction::Unsubscribe,
}],
},
});
self.events.push_back(ToSwarm::NotifyHandler {
peer_id: *peer,
handler: NotifyHandler::Any,
event: FloodsubRpc {
messages: Vec::new(),
subscriptions: vec![FloodsubSubscription {
topic: topic.clone(),
action: FloodsubSubscriptionAction::Unsubscribe,
}],
},
});
}
true
@ -233,9 +230,10 @@ impl Floodsub {
);
}
if self.config.subscribe_local_messages {
self.events.push_back(NetworkBehaviourAction::GenerateEvent(
FloodsubEvent::Message(message.clone()),
));
self.events
.push_back(ToSwarm::GenerateEvent(FloodsubEvent::Message(
message.clone(),
)));
}
}
// Don't publish the message if we have to check subscriptions
@ -259,15 +257,14 @@ impl Floodsub {
continue;
}
self.events
.push_back(NetworkBehaviourAction::NotifyHandler {
peer_id: *peer_id,
handler: NotifyHandler::Any,
event: FloodsubRpc {
subscriptions: Vec::new(),
messages: vec![message.clone()],
},
});
self.events.push_back(ToSwarm::NotifyHandler {
peer_id: *peer_id,
handler: NotifyHandler::Any,
event: FloodsubRpc {
subscriptions: Vec::new(),
messages: vec![message.clone()],
},
});
}
}
@ -287,18 +284,17 @@ impl Floodsub {
// We need to send our subscriptions to the newly-connected node.
if self.target_peers.contains(&peer_id) {
for topic in self.subscribed_topics.iter().cloned() {
self.events
.push_back(NetworkBehaviourAction::NotifyHandler {
peer_id,
handler: NotifyHandler::Any,
event: FloodsubRpc {
messages: Vec::new(),
subscriptions: vec![FloodsubSubscription {
topic,
action: FloodsubSubscriptionAction::Subscribe,
}],
},
});
self.events.push_back(ToSwarm::NotifyHandler {
peer_id,
handler: NotifyHandler::Any,
event: FloodsubRpc {
messages: Vec::new(),
subscriptions: vec![FloodsubSubscription {
topic,
action: FloodsubSubscriptionAction::Subscribe,
}],
},
});
}
}
@ -324,7 +320,7 @@ impl Floodsub {
// We can be disconnected by the remote in case of inactivity for example, so we always
// try to reconnect.
if self.target_peers.contains(&peer_id) {
self.events.push_back(NetworkBehaviourAction::Dial {
self.events.push_back(ToSwarm::Dial {
opts: DialOpts::peer_id(peer_id).build(),
});
}
@ -377,12 +373,11 @@ impl NetworkBehaviour for Floodsub {
if !remote_peer_topics.contains(&subscription.topic) {
remote_peer_topics.push(subscription.topic.clone());
}
self.events.push_back(NetworkBehaviourAction::GenerateEvent(
FloodsubEvent::Subscribed {
self.events
.push_back(ToSwarm::GenerateEvent(FloodsubEvent::Subscribed {
peer_id: propagation_source,
topic: subscription.topic,
},
));
}));
}
FloodsubSubscriptionAction::Unsubscribe => {
if let Some(pos) = remote_peer_topics
@ -391,12 +386,11 @@ impl NetworkBehaviour for Floodsub {
{
remote_peer_topics.remove(pos);
}
self.events.push_back(NetworkBehaviourAction::GenerateEvent(
FloodsubEvent::Unsubscribed {
self.events
.push_back(ToSwarm::GenerateEvent(FloodsubEvent::Unsubscribed {
peer_id: propagation_source,
topic: subscription.topic,
},
));
}));
}
}
}
@ -427,8 +421,7 @@ impl NetworkBehaviour for Floodsub {
.any(|t| message.topics.iter().any(|u| t == u))
{
let event = FloodsubEvent::Message(message.clone());
self.events
.push_back(NetworkBehaviourAction::GenerateEvent(event));
self.events.push_back(ToSwarm::GenerateEvent(event));
}
// Propagate the message to everyone else who is subscribed to any of the topics.
@ -465,12 +458,11 @@ impl NetworkBehaviour for Floodsub {
}
for (peer_id, rpc) in rpcs_to_dispatch {
self.events
.push_back(NetworkBehaviourAction::NotifyHandler {
peer_id,
handler: NotifyHandler::Any,
event: rpc,
});
self.events.push_back(ToSwarm::NotifyHandler {
peer_id,
handler: NotifyHandler::Any,
event: rpc,
});
}
}
@ -478,7 +470,7 @@ impl NetworkBehaviour for Floodsub {
&mut self,
_: &mut Context<'_>,
_: &mut impl PollParameters,
) -> Poll<NetworkBehaviourAction<Self::OutEvent, THandlerInEvent<Self>>> {
) -> Poll<ToSwarm<Self::OutEvent, THandlerInEvent<Self>>> {
if let Some(event) = self.events.pop_front() {
return Poll::Ready(event);
}