@@ -734,112 +734,83 @@ impl PeerNetworkManager {
734734
735735 /// Start peer connection maintenance loop
736736 async fn start_maintenance_loop ( & self ) {
737- let pool = self . pool . clone ( ) ;
738- let discovery = self . discovery . clone ( ) ;
739- let network = self . network ;
740- let shutdown_token = self . shutdown_token . clone ( ) ;
741- let addrv2_handler = self . addrv2_handler . clone ( ) ;
742- let peer_store = self . peer_store . clone ( ) ;
743- let reputation_manager = self . reputation_manager . clone ( ) ;
744- let initial_peers = self . initial_peers . clone ( ) ;
745- let connected_peer_count = self . connected_peer_count . clone ( ) ;
746-
747- // Check if we're in exclusive mode (explicit flag or peers configured)
748- let exclusive_mode = self . exclusive_mode ;
749-
750- // Clone self for peer callback
751- let connect_fn = {
752- let this = self . clone ( ) ;
753- move |addr| {
754- let this = this. clone ( ) ;
755- async move { this. connect_to_peer ( addr) . await }
756- }
757- } ;
758-
737+ let this = self . clone ( ) ;
759738 let mut tasks = self . tasks . lock ( ) . await ;
760739 tasks. spawn ( async move {
761740 // Periodic DNS discovery check (only active in non-exclusive mode)
762741 let mut dns_interval = time:: interval ( DNS_DISCOVERY_DELAY ) ;
763742
764- while !shutdown_token. is_cancelled ( ) {
743+ while !this . shutdown_token . is_cancelled ( ) {
765744 // Clean up disconnected peers
766- pool. cleanup_disconnected ( ) . await ;
745+ this . pool . cleanup_disconnected ( ) . await ;
767746
768- let count = pool. peer_count ( ) . await ;
747+ let count = this . pool . peer_count ( ) . await ;
769748 log:: debug!( "Connected peers: {}" , count) ;
770749 // Keep the cached counter in sync with actual pool count
771- connected_peer_count. store ( count, Ordering :: Relaxed ) ;
772- if exclusive_mode {
750+ this . connected_peer_count . store ( count, Ordering :: Relaxed ) ;
751+ if this . exclusive_mode {
773752 // In exclusive mode, only reconnect to originally specified peers
774- for addr in initial_peers. iter ( ) {
775- if !pool. is_connected ( addr) . await && !pool. is_connecting ( addr) . await {
753+ for addr in this. initial_peers . iter ( ) {
754+ if !this. pool . is_connected ( addr) . await
755+ && !this. pool . is_connecting ( addr) . await
756+ {
776757 log:: info!( "Reconnecting to exclusive peer: {}" , addr) ;
777- tokio:: select! {
778- _= connect_fn( * addr) => { } ,
779- _ = shutdown_token. cancelled( ) => {
780- log:: info!( "Maintenance loop shutting down during connection attempt (exclusive)" ) ;
781- break ;
782- }
783- }
758+ this. connect_to_peer ( * addr) . await ;
784759 }
785760 }
786761 } else {
787762 // Normal mode: try to maintain minimum peer count with discovery
788763 if count < MIN_PEERS {
789764 // Try known addresses first, sorted by reputation
790- let known = addrv2_handler. get_known_addresses ( ) . await ;
765+ let known = this . addrv2_handler . get_known_addresses ( ) . await ;
791766 let needed = TARGET_PEERS . saturating_sub ( count) ;
792767 // Select best peers based on reputation
793- let best_peers = reputation_manager. select_best_peers ( known, needed * 2 ) . await ;
768+ let best_peers =
769+ this. reputation_manager . select_best_peers ( known, needed * 2 ) . await ;
794770 let mut attempted = 0 ;
795771
796772 for addr in best_peers {
797- if !pool. is_connected ( & addr) . await && !pool. is_connecting ( & addr) . await {
798- tokio:: select! {
799- _= connect_fn( addr) => { } ,
800- _ = shutdown_token. cancelled( ) => {
801- log:: info!( "Maintenance loop shutting down during connection attempt (min peers)" ) ;
802- break ;
803- }
804- }
773+ if !this. pool . is_connected ( & addr) . await
774+ && !this. pool . is_connecting ( & addr) . await
775+ {
776+ this. connect_to_peer ( addr) . await ;
805777 attempted += 1 ;
806778 if attempted >= needed {
807779 break ;
808780 }
809781 }
810782 }
811-
812783 }
813784 }
814785
815786 // Send ping to all peers if needed
816- for ( addr, peer) in pool. get_all_peers ( ) . await {
787+ for ( addr, peer) in this . pool . get_all_peers ( ) . await {
817788 let mut peer_guard = peer. write ( ) . await ;
818789 if peer_guard. should_ping ( ) {
819790 if let Err ( e) = peer_guard. send_ping ( ) . await {
820791 log:: error!( "Failed to ping {}: {}" , addr, e) ;
821792 // Update reputation for ping failure
822- reputation_manager. update_reputation (
823- addr,
824- misbehavior_scores:: TIMEOUT ,
825- "Ping failed" ,
826- ) . await ;
793+ this. reputation_manager
794+ . update_reputation ( addr, misbehavior_scores:: TIMEOUT , "Ping failed" )
795+ . await ;
827796 }
828797 }
829798 peer_guard. cleanup_old_pings ( ) ;
830799 }
831800
832801 // Only save known peers if not in exclusive mode
833- if !exclusive_mode {
834- let addresses = addrv2_handler. get_addresses_for_peer ( MAX_ADDR_TO_STORE ) . await ;
802+ if !this. exclusive_mode {
803+ let addresses =
804+ this. addrv2_handler . get_addresses_for_peer ( MAX_ADDR_TO_STORE ) . await ;
835805 if !addresses. is_empty ( ) {
836- if let Err ( e) = peer_store. save_peers ( & addresses) . await {
806+ if let Err ( e) = this . peer_store . save_peers ( & addresses) . await {
837807 log:: warn!( "Failed to save peers: {}" , e) ;
838808 }
839809 }
840810
841811 // Save reputation data periodically
842- if let Err ( e) = reputation_manager. save_to_storage ( & * peer_store) . await {
812+ if let Err ( e) = this. reputation_manager . save_to_storage ( & * this. peer_store ) . await
813+ {
843814 log:: warn!( "Failed to save reputation data: {}" , e) ;
844815 }
845816 }
@@ -848,19 +819,19 @@ impl PeerNetworkManager {
848819 _ = time:: sleep( MAINTENANCE_INTERVAL ) => {
849820 log:: debug!( "Maintenance interval elapsed" ) ;
850821 }
851- _ = dns_interval. tick( ) , if !exclusive_mode => {
852- let count = pool. peer_count( ) . await ;
822+ _ = dns_interval. tick( ) , if !this . exclusive_mode => {
823+ let count = this . pool. peer_count( ) . await ;
853824 if count >= MIN_PEERS {
854825 continue ;
855826 }
856827 log:: info!( "DNS discovery triggered ({} peers), discovering" , count) ;
857- let dns_peers = discovery. discover_peers( network) . await
828+ let dns_peers = this . discovery. discover_peers( this . network) . await
858829 . iter( )
859830 . map( |addr| AddrV2Message :: new( * addr, ServiceFlags :: NETWORK ) )
860831 . collect( ) ;
861- addrv2_handler. handle_addrv2( dns_peers) . await ;
832+ this . addrv2_handler. handle_addrv2( dns_peers) . await ;
862833 }
863- _ = shutdown_token. cancelled( ) => {
834+ _ = this . shutdown_token. cancelled( ) => {
864835 log:: info!( "Maintenance loop shutting down" ) ;
865836 break ;
866837 }
0 commit comments