refactor(identify): simplify reply handling

A `ConnectionHandler` is bound to a single peer. There is no need to embed the `PeerId` in the event that we report to the behaviour, it already knows which peer the event relates to.

Pull-Request: #3895.
This commit is contained in:
Thomas Eizinger
2023-05-10 03:51:47 +02:00
committed by GitHub
parent 5a867c86b4
commit 541eba306b
2 changed files with 7 additions and 11 deletions

View File

@ -300,9 +300,9 @@ impl NetworkBehaviour for Behaviour {
score: AddressScore::Finite(1), score: AddressScore::Finite(1),
}); });
} }
handler::Event::Identification(peer) => { handler::Event::Identification => {
self.events self.events
.push_back(ToSwarm::GenerateEvent(Event::Sent { peer_id: peer })); .push_back(ToSwarm::GenerateEvent(Event::Sent { peer_id }));
} }
handler::Event::IdentificationPushed => { handler::Event::IdentificationPushed => {
self.events self.events

View File

@ -55,7 +55,7 @@ pub struct Handler {
>, >,
/// Pending identification replies, awaiting being sent. /// Pending identification replies, awaiting being sent.
pending_replies: FuturesUnordered<BoxFuture<'static, Result<PeerId, UpgradeError>>>, pending_replies: FuturesUnordered<BoxFuture<'static, Result<(), UpgradeError>>>,
/// Future that fires when we need to identify the node again. /// Future that fires when we need to identify the node again.
trigger_next_identify: Delay, trigger_next_identify: Delay,
@ -96,7 +96,7 @@ pub enum Event {
/// We obtained identification information from the remote. /// We obtained identification information from the remote.
Identified(Info), Identified(Info),
/// We replied to an identification request from the remote. /// We replied to an identification request from the remote.
Identification(PeerId), Identification,
/// We actively pushed our identification information to the remote. /// We actively pushed our identification information to the remote.
IdentificationPushed, IdentificationPushed,
/// Failed to identify the remote, or to reply to an identification request. /// Failed to identify the remote, or to reply to an identification request.
@ -144,14 +144,10 @@ impl Handler {
) { ) {
match output { match output {
future::Either::Left(substream) => { future::Either::Left(substream) => {
let peer_id = self.remote_peer_id;
let info = self.build_info(); let info = self.build_info();
self.pending_replies.push(Box::pin(async move { self.pending_replies
crate::protocol::send(substream, info).await?; .push(crate::protocol::send(substream, info).boxed());
Ok(peer_id)
}));
} }
future::Either::Right(fut) => { future::Either::Right(fut) => {
if self.inbound_identify_push.replace(fut).is_some() { if self.inbound_identify_push.replace(fut).is_some() {
@ -320,7 +316,7 @@ impl ConnectionHandler for Handler {
// Check for pending replies to send. // Check for pending replies to send.
if let Poll::Ready(Some(result)) = self.pending_replies.poll_next_unpin(cx) { if let Poll::Ready(Some(result)) = self.pending_replies.poll_next_unpin(cx) {
let event = result let event = result
.map(Event::Identification) .map(|()| Event::Identification)
.unwrap_or_else(|err| Event::IdentificationError(StreamUpgradeError::Apply(err))); .unwrap_or_else(|err| Event::IdentificationError(StreamUpgradeError::Apply(err)));
return Poll::Ready(ConnectionHandlerEvent::Custom(event)); return Poll::Ready(ConnectionHandlerEvent::Custom(event));