Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed swarm event tracking bug #401

Merged
merged 1 commit into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 5 additions & 19 deletions src/network/p2p/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ impl Command for StartListening {
struct AddAddress {
peer_id: PeerId,
peer_addr: Multiaddr,
response_sender: Option<oneshot::Sender<Result<()>>>,
}

impl Command for AddAddress {
Expand All @@ -123,19 +122,10 @@ impl Command for AddAddress {
.kademlia
.add_address(&self.peer_id, self.peer_addr.clone());

// insert response channel into Swarm Events pending map
entries.insert_swarm_event(self.peer_id, self.response_sender.take().unwrap());
Ok(())
}

fn abort(&mut self, error: Report) {
// TODO: consider what to do if this results with None
self.response_sender
.take()
.unwrap()
.send(Err(error))
.expect("AddAddress receiver dropped");
}
fn abort(&mut self, _error: Report) {}
}

struct Bootstrap {
Expand Down Expand Up @@ -452,14 +442,10 @@ impl Client {
}

pub async fn add_address(&self, peer_id: PeerId, peer_addr: Multiaddr) -> Result<()> {
self.execute_sync(|response_sender| {
Box::new(AddAddress {
peer_id,
peer_addr,
response_sender: Some(response_sender),
})
})
.await
self.command_sender
.send(Box::new(AddAddress { peer_id, peer_addr }))
.await
.context("failed to add address to the routing table")
}

pub async fn dial_peer(&self, peer_id: PeerId, peer_address: Multiaddr) -> Result<()> {
Expand Down
9 changes: 3 additions & 6 deletions src/network/p2p/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ struct BootstrapState {
pub struct EventLoop {
swarm: Swarm<Behaviour>,
command_receiver: CommandReceiver,
// Tracking Kademlia events
pending_kad_queries: HashMap<QueryId, QueryChannel>,
pending_kad_routing: HashMap<PeerId, oneshot::Sender<Result<()>>>,
// Tracking swarm events (i.e. peer dialing)
pending_swarm_events: HashMap<PeerId, oneshot::Sender<Result<()>>>,
relay: RelayState,
bootstrap: BootstrapState,
Expand Down Expand Up @@ -126,7 +127,6 @@ impl EventLoop {
swarm,
command_receiver,
pending_kad_queries: Default::default(),
pending_kad_routing: Default::default(),
pending_swarm_events: Default::default(),
relay: RelayState {
id: PeerId::random(),
Expand Down Expand Up @@ -197,9 +197,6 @@ impl EventLoop {
..
} => {
trace!("Routing updated. Peer: {peer:?}. is_new_peer: {is_new_peer:?}. Addresses: {addresses:#?}. Old peer: {old_peer:#?}");
if let Some(ch) = self.pending_kad_routing.remove(&peer) {
_ = ch.send(Ok(()));
}
},
kad::Event::RoutablePeer { peer, address } => {
trace!("RoutablePeer. Peer: {peer:?}. Address: {address:?}");
Expand Down Expand Up @@ -507,7 +504,7 @@ impl EventLoop {
if let Err(err) = command.run(EventLoopEntries::new(
&mut self.swarm,
&mut self.pending_kad_queries,
&mut self.pending_kad_routing,
&mut self.pending_swarm_events,
&mut self.active_blocks,
)) {
command.abort(eyre!(err));
Expand Down
Loading