@@ -1063,9 +1063,9 @@ impl Node {
10631063
10641064 /// Connect to a node on the peer-to-peer network.
10651065 ///
1066- /// If `permanently ` is set to `true`, we'll remember the peer and reconnect to it on restart.
1066+ /// If `persist ` is set to `true`, we'll remember the peer and reconnect to it on restart.
10671067 pub fn connect (
1068- & self , node_id : PublicKey , address : NetAddress , permanently : bool ,
1068+ & self , node_id : PublicKey , address : NetAddress , persist : bool ,
10691069 ) -> Result < ( ) , Error > {
10701070 let rt_lock = self . runtime . read ( ) . unwrap ( ) ;
10711071 if rt_lock. is_none ( ) {
@@ -1096,7 +1096,7 @@ impl Node {
10961096
10971097 log_info ! ( self . logger, "Connected to peer {}@{}. " , peer_info. node_id, peer_info. address) ;
10981098
1099- if permanently {
1099+ if persist {
11001100 self . peer_store . add_peer ( peer_info) ?;
11011101 }
11021102
@@ -1594,17 +1594,43 @@ impl Node {
15941594
15951595 /// Retrieves a list of known peers.
15961596 pub fn list_peers ( & self ) -> Vec < PeerDetails > {
1597- let active_connected_peers: Vec < PublicKey > =
1598- self . peer_manager . get_peer_node_ids ( ) . iter ( ) . map ( |p| p. 0 ) . collect ( ) ;
1599- self . peer_store
1600- . list_peers ( )
1601- . iter ( )
1602- . map ( |p| PeerDetails {
1597+ let mut peers = Vec :: new ( ) ;
1598+
1599+ // First add all connected peers, preferring to list the connected address if available.
1600+ let connected_peers = self . peer_manager . get_peer_node_ids ( ) ;
1601+ for ( node_id, con_addr_opt) in connected_peers {
1602+ let stored_peer = self . peer_store . get_peer ( & node_id) ;
1603+ let stored_addr_opt = stored_peer. as_ref ( ) . map ( |p| p. address . clone ( ) ) ;
1604+ let address = match ( con_addr_opt, stored_addr_opt) {
1605+ ( Some ( con_addr) , Some ( _stored_addr) ) => NetAddress ( con_addr) ,
1606+ ( None , Some ( stored_addr) ) => stored_addr,
1607+ ( Some ( con_addr) , None ) => NetAddress ( con_addr) ,
1608+ ( None , None ) => continue ,
1609+ } ;
1610+
1611+ let is_persisted = stored_peer. is_some ( ) ;
1612+ let is_connected = true ;
1613+ let details = PeerDetails { node_id, address, is_persisted, is_connected } ;
1614+ peers. push ( details) ;
1615+ }
1616+
1617+ // Now add all known-but-offline peers, too.
1618+ for p in self . peer_store . list_peers ( ) {
1619+ if peers. iter ( ) . find ( |d| d. node_id == p. node_id ) . is_some ( ) {
1620+ continue ;
1621+ }
1622+
1623+ let details = PeerDetails {
16031624 node_id : p. node_id ,
1604- address : p. address . clone ( ) ,
1605- is_connected : active_connected_peers. contains ( & p. node_id ) ,
1606- } )
1607- . collect ( )
1625+ address : p. address ,
1626+ is_persisted : true ,
1627+ is_connected : false ,
1628+ } ;
1629+
1630+ peers. push ( details) ;
1631+ }
1632+
1633+ peers
16081634 }
16091635
16101636 /// Creates a digital ECDSA signature of a message with the node's secret key.
0 commit comments