@@ -1284,9 +1284,9 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {
1284
1284
1285
1285
/// Connect to a node on the peer-to-peer network.
1286
1286
///
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.
1288
1288
pub fn connect (
1289
- & self , node_id : PublicKey , address : NetAddress , permanently : bool ,
1289
+ & self , node_id : PublicKey , address : NetAddress , persist : bool ,
1290
1290
) -> Result < ( ) , Error > {
1291
1291
let rt_lock = self . runtime . read ( ) . unwrap ( ) ;
1292
1292
if rt_lock. is_none ( ) {
@@ -1311,7 +1311,7 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {
1311
1311
1312
1312
log_info ! ( self . logger, "Connected to peer {}@{}. " , peer_info. node_id, peer_info. address) ;
1313
1313
1314
- if permanently {
1314
+ if persist {
1315
1315
self . peer_store . add_peer ( peer_info) ?;
1316
1316
}
1317
1317
@@ -1833,17 +1833,43 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {
1833
1833
1834
1834
/// Retrieves a list of known peers.
1835
1835
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 {
1842
1863
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
1847
1873
}
1848
1874
1849
1875
/// Creates a digital ECDSA signature of a message with the node's secret key.
0 commit comments