@@ -1284,9 +1284,9 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {
12841284
12851285 /// Connect to a node on the peer-to-peer network.
12861286 ///
1287- /// If `permanently ` is set to `true`, we'll remember the peer and reconnect to it on restart.
1287+ /// If `persist ` is set to `true`, we'll remember the peer and reconnect to it on restart.
12881288 pub fn connect (
1289- & self , node_id : PublicKey , address : NetAddress , permanently : bool ,
1289+ & self , node_id : PublicKey , address : NetAddress , persist : bool ,
12901290 ) -> Result < ( ) , Error > {
12911291 let rt_lock = self . runtime . read ( ) . unwrap ( ) ;
12921292 if rt_lock. is_none ( ) {
@@ -1311,7 +1311,7 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {
13111311
13121312 log_info ! ( self . logger, "Connected to peer {}@{}. " , peer_info. node_id, peer_info. address) ;
13131313
1314- if permanently {
1314+ if persist {
13151315 self . peer_store . add_peer ( peer_info) ?;
13161316 }
13171317
@@ -1833,17 +1833,43 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {
18331833
18341834 /// Retrieves a list of known peers.
18351835 pub fn list_peers ( & self ) -> Vec < PeerDetails > {
1836- let active_connected_peers: Vec < PublicKey > =
1837- self . peer_manager . get_peer_node_ids ( ) . iter ( ) . map ( |p| p. 0 ) . collect ( ) ;
1838- self . peer_store
1839- . list_peers ( )
1840- . iter ( )
1841- . map ( |p| PeerDetails {
1836+ let mut peers = Vec :: new ( ) ;
1837+
1838+ // First add all connected peers, preferring to list the connected address if available.
1839+ let connected_peers = self . peer_manager . get_peer_node_ids ( ) ;
1840+ let connected_peers_len = connected_peers. len ( ) ;
1841+ for ( node_id, con_addr_opt) in connected_peers {
1842+ let stored_peer = self . peer_store . get_peer ( & node_id) ;
1843+ let stored_addr_opt = stored_peer. as_ref ( ) . map ( |p| p. address . clone ( ) ) ;
1844+ let address = match ( con_addr_opt, stored_addr_opt) {
1845+ ( Some ( con_addr) , _) => NetAddress ( con_addr) ,
1846+ ( None , Some ( stored_addr) ) => stored_addr,
1847+ ( None , None ) => continue ,
1848+ } ;
1849+
1850+ let is_persisted = stored_peer. is_some ( ) ;
1851+ let is_connected = true ;
1852+ let details = PeerDetails { node_id, address, is_persisted, is_connected } ;
1853+ peers. push ( details) ;
1854+ }
1855+
1856+ // Now add all known-but-offline peers, too.
1857+ for p in self . peer_store . list_peers ( ) {
1858+ if peers. iter ( ) . take ( connected_peers_len) . find ( |d| d. node_id == p. node_id ) . is_some ( ) {
1859+ continue ;
1860+ }
1861+
1862+ let details = PeerDetails {
18421863 node_id : p. node_id ,
1843- address : p. address . clone ( ) ,
1844- is_connected : active_connected_peers. contains ( & p. node_id ) ,
1845- } )
1846- . collect ( )
1864+ address : p. address ,
1865+ is_persisted : true ,
1866+ is_connected : false ,
1867+ } ;
1868+
1869+ peers. push ( details) ;
1870+ }
1871+
1872+ peers
18471873 }
18481874
18491875 /// Creates a digital ECDSA signature of a message with the node's secret key.
0 commit comments