From f92d2be7b49a9c82550b60a0635cb184e02543e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Bierlein?= Date: Fri, 26 Apr 2024 18:50:51 +0200 Subject: [PATCH] Actually select peer randomly --- src/p2p.rs | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/src/p2p.rs b/src/p2p.rs index afbd0a6..5747652 100644 --- a/src/p2p.rs +++ b/src/p2p.rs @@ -1,10 +1,6 @@ -use crate::config::Config; -use crate::controller::{P2PEvent, SwarmCommand}; -use crate::storage::QueryOptions; -use crate::types::{ - claims_topic_hashes, InclusionClaim, MintpoolNodeInfo, PeerInclusionClaim, Premint, - PremintName, PremintTypes, -}; +use std::hash::Hasher; +use std::time::Duration; + use eyre::WrapErr; use futures_ticker::Ticker; use libp2p::core::ConnectedPoint; @@ -20,12 +16,19 @@ use libp2p::swarm::{ConnectionId, NetworkBehaviour, NetworkInfo, SwarmEvent}; use libp2p::{ gossipsub, kad, noise, request_response, tcp, yamux, Multiaddr, PeerId, StreamProtocol, }; +use rand::prelude::SliceRandom; use serde::{Deserialize, Serialize}; use sha256::digest; -use std::hash::Hasher; -use std::time::Duration; use tokio::select; +use crate::config::Config; +use crate::controller::{P2PEvent, SwarmCommand}; +use crate::storage::QueryOptions; +use crate::types::{ + claims_topic_hashes, InclusionClaim, MintpoolNodeInfo, PeerInclusionClaim, Premint, + PremintName, PremintTypes, +}; + #[derive(NetworkBehaviour)] pub struct MintpoolBehaviour { gossipsub: gossipsub::Behaviour, @@ -575,17 +578,12 @@ impl SwarmController { async fn do_sync(&mut self, query: QueryOptions) { // select random peer - let peer_id = self - .swarm - .behaviour_mut() - .kad - .kbuckets() - .flat_map(|x| { - x.iter() - .map(|x| x.node.key.preimage().clone()) - .collect::>() - }) - .next(); + let state = self.make_network_state(); + + let peer_id = state + .gossipsub_peers + .choose(&mut rand::thread_rng()) + .cloned(); if let Some(peer_id) = peer_id { let id = self