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

@ -40,8 +40,8 @@ use libp2p_identity::PeerId;
use libp2p_swarm::{
behaviour::{AddressChange, ConnectionClosed, ConnectionEstablished, FromSwarm},
dial_opts::DialOpts,
ConnectionDenied, ConnectionId, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler,
PollParameters, THandler, THandlerInEvent, THandlerOutEvent,
ConnectionDenied, ConnectionId, NetworkBehaviour, NotifyHandler, PollParameters, THandler,
THandlerInEvent, THandlerOutEvent, ToSwarm,
};
use wasm_timer::Instant;
@ -218,7 +218,7 @@ pub struct Behaviour<D = IdentityTransform, F = AllowAllSubscriptionFilter> {
config: Config,
/// Events that need to be yielded to the outside when polling.
events: VecDeque<NetworkBehaviourAction<Event, HandlerIn>>,
events: VecDeque<ToSwarm<Event, HandlerIn>>,
/// Pools non-urgent control messages between heartbeats.
control_pool: HashMap<PeerId, Vec<ControlAction>>,
@ -1133,7 +1133,7 @@ where
if !self.peer_topics.contains_key(peer_id) {
// Connect to peer
debug!("Connecting to explicit peer {:?}", peer_id);
self.events.push_back(NetworkBehaviourAction::Dial {
self.events.push_back(ToSwarm::Dial {
opts: DialOpts::peer_id(*peer_id).build(),
});
}
@ -1632,7 +1632,7 @@ where
self.px_peers.insert(peer_id);
// dial peer
self.events.push_back(NetworkBehaviourAction::Dial {
self.events.push_back(ToSwarm::Dial {
opts: DialOpts::peer_id(peer_id).build(),
});
}
@ -1818,7 +1818,7 @@ where
if self.mesh.contains_key(&message.topic) {
debug!("Sending received message to user");
self.events
.push_back(NetworkBehaviourAction::GenerateEvent(Event::Message {
.push_back(ToSwarm::GenerateEvent(Event::Message {
propagation_source: *propagation_source,
message_id: msg_id.clone(),
message,
@ -1994,12 +1994,10 @@ where
}
}
// generates a subscription event to be polled
application_event.push(NetworkBehaviourAction::GenerateEvent(
Event::Subscribed {
peer_id: *propagation_source,
topic: topic_hash.clone(),
},
));
application_event.push(ToSwarm::GenerateEvent(Event::Subscribed {
peer_id: *propagation_source,
topic: topic_hash.clone(),
}));
}
SubscriptionAction::Unsubscribe => {
if peer_list.remove(propagation_source) {
@ -2014,12 +2012,10 @@ where
subscribed_topics.remove(topic_hash);
unsubscribed_peers.push((*propagation_source, topic_hash.clone()));
// generate an unsubscribe event to be polled
application_event.push(NetworkBehaviourAction::GenerateEvent(
Event::Unsubscribed {
peer_id: *propagation_source,
topic: topic_hash.clone(),
},
));
application_event.push(ToSwarm::GenerateEvent(Event::Unsubscribed {
peer_id: *propagation_source,
topic: topic_hash.clone(),
}));
}
}
@ -2890,12 +2886,11 @@ where
let messages = self.fragment_message(message)?;
for message in messages {
self.events
.push_back(NetworkBehaviourAction::NotifyHandler {
peer_id,
event: HandlerIn::Message(message),
handler: NotifyHandler::Any,
})
self.events.push_back(ToSwarm::NotifyHandler {
peer_id,
event: HandlerIn::Message(message),
handler: NotifyHandler::Any,
})
}
Ok(())
}
@ -3150,12 +3145,11 @@ where
for topic in topics {
if let Some(mesh_peers) = self.mesh.get(topic) {
if mesh_peers.contains(&peer_id) {
self.events
.push_back(NetworkBehaviourAction::NotifyHandler {
peer_id,
event: HandlerIn::JoinedMesh,
handler: NotifyHandler::One(connections.connections[0]),
});
self.events.push_back(ToSwarm::NotifyHandler {
peer_id,
event: HandlerIn::JoinedMesh,
handler: NotifyHandler::One(connections.connections[0]),
});
break;
}
}
@ -3338,11 +3332,10 @@ where
"Peer does not support gossipsub protocols. {}",
propagation_source
);
self.events.push_back(NetworkBehaviourAction::GenerateEvent(
Event::GossipsubNotSupported {
self.events
.push_back(ToSwarm::GenerateEvent(Event::GossipsubNotSupported {
peer_id: propagation_source,
},
));
}));
} else if let Some(conn) = self.connected_peers.get_mut(&propagation_source) {
// Only change the value if the old value is Floodsub (the default set in
// `NetworkBehaviour::on_event` with FromSwarm::ConnectionEstablished).
@ -3450,7 +3443,7 @@ where
&mut self,
cx: &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);
}
@ -3499,7 +3492,7 @@ fn peer_added_to_mesh(
new_topics: Vec<&TopicHash>,
mesh: &HashMap<TopicHash, BTreeSet<PeerId>>,
known_topics: Option<&BTreeSet<TopicHash>>,
events: &mut VecDeque<NetworkBehaviourAction<Event, HandlerIn>>,
events: &mut VecDeque<ToSwarm<Event, HandlerIn>>,
connections: &HashMap<PeerId, PeerConnections>,
) {
// Ensure there is an active connection
@ -3525,7 +3518,7 @@ fn peer_added_to_mesh(
}
}
// This is the first mesh the peer has joined, inform the handler
events.push_back(NetworkBehaviourAction::NotifyHandler {
events.push_back(ToSwarm::NotifyHandler {
peer_id,
event: HandlerIn::JoinedMesh,
handler: NotifyHandler::One(connection_id),
@ -3540,7 +3533,7 @@ fn peer_removed_from_mesh(
old_topic: &TopicHash,
mesh: &HashMap<TopicHash, BTreeSet<PeerId>>,
known_topics: Option<&BTreeSet<TopicHash>>,
events: &mut VecDeque<NetworkBehaviourAction<Event, HandlerIn>>,
events: &mut VecDeque<ToSwarm<Event, HandlerIn>>,
connections: &HashMap<PeerId, PeerConnections>,
) {
// Ensure there is an active connection
@ -3564,7 +3557,7 @@ fn peer_removed_from_mesh(
}
}
// The peer is not in any other mesh, inform the handler
events.push_back(NetworkBehaviourAction::NotifyHandler {
events.push_back(ToSwarm::NotifyHandler {
peer_id,
event: HandlerIn::LeftMesh,
handler: NotifyHandler::One(*connection_id),