@@ -1063,9 +1063,9 @@ impl Node {
1063
1063
1064
1064
/// Connect to a node on the peer-to-peer network.
1065
1065
///
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.
1067
1067
pub fn connect (
1068
- & self , node_id : PublicKey , address : NetAddress , permanently : bool ,
1068
+ & self , node_id : PublicKey , address : NetAddress , persist : bool ,
1069
1069
) -> Result < ( ) , Error > {
1070
1070
let rt_lock = self . runtime . read ( ) . unwrap ( ) ;
1071
1071
if rt_lock. is_none ( ) {
@@ -1096,7 +1096,7 @@ impl Node {
1096
1096
1097
1097
log_info ! ( self . logger, "Connected to peer {}@{}. " , peer_info. node_id, peer_info. address) ;
1098
1098
1099
- if permanently {
1099
+ if persist {
1100
1100
self . peer_store . add_peer ( peer_info) ?;
1101
1101
}
1102
1102
@@ -1594,17 +1594,43 @@ impl Node {
1594
1594
1595
1595
/// Retrieves a list of known peers.
1596
1596
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 {
1603
1624
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
1608
1634
}
1609
1635
1610
1636
/// Creates a digital ECDSA signature of a message with the node's secret key.
0 commit comments