@@ -1019,9 +1019,9 @@ impl Node {
10191019
10201020 /// Connect to a node on the peer-to-peer network.
10211021 ///
1022- /// If `permanently ` is set to `true`, we'll remember the peer and reconnect to it on restart.
1022+ /// If `persist ` is set to `true`, we'll remember the peer and reconnect to it on restart.
10231023 pub fn connect (
1024- & self , node_id : PublicKey , address : NetAddress , permanently : bool ,
1024+ & self , node_id : PublicKey , address : NetAddress , persist : bool ,
10251025 ) -> Result < ( ) , Error > {
10261026 let rt_lock = self . runtime . read ( ) . unwrap ( ) ;
10271027 if rt_lock. is_none ( ) {
@@ -1052,7 +1052,7 @@ impl Node {
10521052
10531053 log_info ! ( self . logger, "Connected to peer {}@{}. " , peer_info. node_id, peer_info. address) ;
10541054
1055- if permanently {
1055+ if persist {
10561056 self . peer_store . add_peer ( peer_info) ?;
10571057 }
10581058
@@ -1550,17 +1550,43 @@ impl Node {
15501550
15511551 /// Retrieves a list of known peers.
15521552 pub fn list_peers ( & self ) -> Vec < PeerDetails > {
1553- let active_connected_peers: Vec < PublicKey > =
1554- self . peer_manager . get_peer_node_ids ( ) . iter ( ) . map ( |p| p. 0 ) . collect ( ) ;
1555- self . peer_store
1556- . list_peers ( )
1557- . iter ( )
1558- . map ( |p| PeerDetails {
1553+ let mut peers = Vec :: new ( ) ;
1554+
1555+ // First add all connected peers, preferring to list the connected address if available.
1556+ let connected_peers = self . peer_manager . get_peer_node_ids ( ) ;
1557+ for ( node_id, con_addr_opt) in connected_peers {
1558+ let stored_peer = self . peer_store . get_peer ( & node_id) ;
1559+ let stored_addr_opt = stored_peer. as_ref ( ) . map ( |p| p. address . clone ( ) ) ;
1560+ let address = match ( con_addr_opt, stored_addr_opt) {
1561+ ( Some ( con_addr) , Some ( _stored_addr) ) => NetAddress ( con_addr) ,
1562+ ( None , Some ( stored_addr) ) => stored_addr,
1563+ ( Some ( con_addr) , None ) => NetAddress ( con_addr) ,
1564+ ( None , None ) => continue ,
1565+ } ;
1566+
1567+ let is_persisted = stored_peer. is_some ( ) ;
1568+ let is_connected = true ;
1569+ let details = PeerDetails { node_id, address, is_persisted, is_connected } ;
1570+ peers. push ( details) ;
1571+ }
1572+
1573+ // Now add all known-but-offline peers, too.
1574+ for p in self . peer_store . list_peers ( ) {
1575+ if peers. iter ( ) . find ( |d| d. node_id == p. node_id ) . is_some ( ) {
1576+ continue ;
1577+ }
1578+
1579+ let details = PeerDetails {
15591580 node_id : p. node_id ,
1560- address : p. address . clone ( ) ,
1561- is_connected : active_connected_peers. contains ( & p. node_id ) ,
1562- } )
1563- . collect ( )
1581+ address : p. address ,
1582+ is_persisted : true ,
1583+ is_connected : false ,
1584+ } ;
1585+
1586+ peers. push ( details) ;
1587+ }
1588+
1589+ peers
15641590 }
15651591
15661592 /// Creates a digital ECDSA signature of a message with the node's secret key.
0 commit comments