Skip to content

Commit

Permalink
Fix reconnection to peers from peers_exchange
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeyboyko0791 committed Nov 27, 2020
1 parent 54d206f commit 392f559
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
10 changes: 6 additions & 4 deletions mm2src/mm2_libp2p/src/atomicdex_behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,13 +470,15 @@ fn maintain_connection_to_relays(swarm: &mut AtomicDexSwarm, bootstrap_addresses
.choose_multiple(&mut thread_rng(), connect_bootstrap_num)
{
if let Err(e) = libp2p::Swarm::dial_addr(swarm, (*addr).clone()) {
error!("Addr {} dial error {}", addr, e);
error!("Bootstrap addr {} dial error {}", addr, e);
}
}
}
for peer in to_connect {
if let Err(e) = libp2p::Swarm::dial(swarm, &peer) {
error!("Peer {} dial error {}", peer, e);
for (peer, addresses) in to_connect {
for addr in addresses {
if let Err(e) = libp2p::Swarm::dial_addr(swarm, addr.clone()) {
error!("Peer {} address {} dial error {}", peer, addr, e);
}
}
}
}
Expand Down
20 changes: 12 additions & 8 deletions mm2src/mm2_libp2p/src/peers_exchange.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,19 @@ impl PeersExchange {
}
}

pub fn get_random_peers(&self, num: usize, mut filter: impl FnMut(&PeerId) -> bool) -> Vec<PeerId> {
pub fn get_random_peers(
&mut self,
num: usize,
mut filter: impl FnMut(&PeerId) -> bool,
) -> HashMap<PeerId, Vec<Multiaddr>> {
let mut result = HashMap::with_capacity(num);
let mut rng = thread_rng();
self.known_peers
.iter()
.filter(|peer| filter(*peer))
.collect::<Vec<_>>()
.choose_multiple(&mut rng, num)
.map(|peer| (*peer).clone())
.collect()
let peer_ids = self.known_peers.iter().filter(|peer| filter(*peer)).collect::<Vec<_>>();
for peer_id in peer_ids.choose_multiple(&mut rng, num) {
let addresses = self.request_response.addresses_of_peer(*peer_id);
result.insert((*peer_id).clone(), addresses);
}
result
}

pub fn is_known_peer(&self, peer: &PeerId) -> bool { self.known_peers.contains(peer) }
Expand Down

0 comments on commit 392f559

Please sign in to comment.