From f11a08c43668be3ecec512fa2f0a2240ed4a487c Mon Sep 17 00:00:00 2001 From: Luke Parker Date: Fri, 22 Mar 2024 23:47:43 -0400 Subject: [PATCH] Peer finding which won't get stuck on one specific network --- coordinator/src/p2p.rs | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/coordinator/src/p2p.rs b/coordinator/src/p2p.rs index 8fe609a0..f64834ee 100644 --- a/coordinator/src/p2p.rs +++ b/coordinator/src/p2p.rs @@ -322,6 +322,7 @@ impl LibP2p { to_dial_send.send(addr).unwrap(); }; + let mut to_retry = vec![]; while let Some(network) = pending_p2p_connections_recv.recv().await { if let Ok(mut nodes) = serai.p2p_validators(network).await { // If there's an insufficient amount of nodes known, connect to all yet add it @@ -332,11 +333,11 @@ impl LibP2p { network, nodes.len() ); - pending_p2p_connections_send.send(network).unwrap(); + to_retry.push(network); for node in nodes { connect(node); } - break; + continue; } // Randomly select up to 5 @@ -351,6 +352,9 @@ impl LibP2p { } } } + for to_retry in to_retry { + pending_p2p_connections_send.send(to_retry).unwrap(); + } } // Sleep 60 seconds before moving to the next iteration tokio::time::sleep(core::time::Duration::from_secs(60)).await; @@ -432,12 +436,16 @@ impl LibP2p { log::debug!("dialing to peer in connection ID {}", &connection_id); } Some(SwarmEvent::ConnectionEstablished { peer_id, connection_id, .. }) => { - log::debug!( - "connection established to peer {} in connection ID {}", - &peer_id, - &connection_id, - ); - swarm.behaviour_mut().gossipsub.add_explicit_peer(&peer_id) + if &peer_id == swarm.local_peer_id() { + swarm.close_connection(connection_id); + } else { + log::debug!( + "connection established to peer {} in connection ID {}", + &peer_id, + &connection_id, + ); + swarm.behaviour_mut().gossipsub.add_explicit_peer(&peer_id) + } } Some(SwarmEvent::Behaviour(BehaviorEvent::Gossipsub( GsEvent::Message { propagation_source, message, .. },