Peer finding which won't get stuck on one specific network

This commit is contained in:
Luke Parker 2024-03-22 23:47:43 -04:00
parent 35b58a45bd
commit f11a08c436
No known key found for this signature in database

View file

@ -322,6 +322,7 @@ impl LibP2p {
to_dial_send.send(addr).unwrap(); to_dial_send.send(addr).unwrap();
}; };
let mut to_retry = vec![];
while let Some(network) = pending_p2p_connections_recv.recv().await { while let Some(network) = pending_p2p_connections_recv.recv().await {
if let Ok(mut nodes) = serai.p2p_validators(network).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 // If there's an insufficient amount of nodes known, connect to all yet add it
@ -332,11 +333,11 @@ impl LibP2p {
network, network,
nodes.len() nodes.len()
); );
pending_p2p_connections_send.send(network).unwrap(); to_retry.push(network);
for node in nodes { for node in nodes {
connect(node); connect(node);
} }
break; continue;
} }
// Randomly select up to 5 // 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 // Sleep 60 seconds before moving to the next iteration
tokio::time::sleep(core::time::Duration::from_secs(60)).await; tokio::time::sleep(core::time::Duration::from_secs(60)).await;
@ -432,6 +436,9 @@ impl LibP2p {
log::debug!("dialing to peer in connection ID {}", &connection_id); log::debug!("dialing to peer in connection ID {}", &connection_id);
} }
Some(SwarmEvent::ConnectionEstablished { peer_id, connection_id, .. }) => { Some(SwarmEvent::ConnectionEstablished { peer_id, connection_id, .. }) => {
if &peer_id == swarm.local_peer_id() {
swarm.close_connection(connection_id);
} else {
log::debug!( log::debug!(
"connection established to peer {} in connection ID {}", "connection established to peer {} in connection ID {}",
&peer_id, &peer_id,
@ -439,6 +446,7 @@ impl LibP2p {
); );
swarm.behaviour_mut().gossipsub.add_explicit_peer(&peer_id) swarm.behaviour_mut().gossipsub.add_explicit_peer(&peer_id)
} }
}
Some(SwarmEvent::Behaviour(BehaviorEvent::Gossipsub( Some(SwarmEvent::Behaviour(BehaviorEvent::Gossipsub(
GsEvent::Message { propagation_source, message, .. }, GsEvent::Message { propagation_source, message, .. },
))) => { ))) => {