Skip to content

Commit

Permalink
chore: check if reservation exist prior to emitting event
Browse files Browse the repository at this point in the history
  • Loading branch information
dariusc93 committed Nov 13, 2023
1 parent b121cd6 commit 17e7a8e
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions protocols/relay/src/priv_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use libp2p_swarm::{
dummy, ConnectionDenied, ConnectionHandler, ConnectionId, DialFailure, NetworkBehaviour,
NotifyHandler, Stream, THandler, THandlerInEvent, THandlerOutEvent, ToSwarm,
};
use std::collections::{hash_map, HashMap, VecDeque};
use std::collections::{hash_map, HashMap, HashSet, VecDeque};
use std::io::{Error, ErrorKind, IoSlice};
use std::pin::Pin;
use std::task::{Context, Poll};
Expand Down Expand Up @@ -82,6 +82,8 @@ pub struct Behaviour {

relay_connection_addr: HashMap<ConnectionId, Multiaddr>,

reservation: HashSet<Multiaddr>,

/// Queue of actions to return when polled.
queued_actions: VecDeque<ToSwarm<Event, Either<handler::In, Void>>>,

Expand All @@ -96,6 +98,7 @@ pub fn new(local_peer_id: PeerId) -> (Transport, Behaviour) {
from_transport,
directly_connected_peers: Default::default(),
relay_connection_addr: Default::default(),
reservation: Default::default(),
queued_actions: Default::default(),
pending_handler_commands: Default::default(),
};
Expand Down Expand Up @@ -135,8 +138,10 @@ impl Behaviour {
.with(Protocol::P2pCircuit)
.with(Protocol::P2p(self.local_peer_id));

self.queued_actions
.push_back(ToSwarm::ExternalAddrExpired(addr));
if self.reservation.remove(&addr) {
self.queued_actions
.push_back(ToSwarm::ExternalAddrExpired(addr));
}
}
}
}
Expand Down Expand Up @@ -232,15 +237,15 @@ impl NetworkBehaviour for Behaviour {

let event = match handler_event {
handler::Event::ReservationReqAccepted { renewal, limit } => {
if !renewal {
let addr = self
.relay_connection_addr
.get(&connection)
.cloned()
.expect("Relay connection exist")
.with(Protocol::P2pCircuit)
.with(Protocol::P2p(self.local_peer_id));
let addr = self
.relay_connection_addr
.get(&connection)
.cloned()
.expect("Relay connection exist")
.with(Protocol::P2pCircuit)
.with(Protocol::P2p(self.local_peer_id));

if !renewal && self.reservation.insert(addr.clone()) {
self.queued_actions
.push_back(ToSwarm::ExternalAddrConfirmed(addr));
}
Expand Down

0 comments on commit 17e7a8e

Please sign in to comment.