Skip to content

Commit 542f71f

Browse files
committed
Return connection state of peer in list_peers
1 parent 17a91d3 commit 542f71f

File tree

4 files changed

+32
-5
lines changed

4 files changed

+32
-5
lines changed

bindings/ldk_node.udl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ interface Node {
5151
[Throws=NodeError]
5252
Invoice receive_variable_amount_payment([ByRef]string description, u32 expiry_secs);
5353
PaymentInfo? payment_info([ByRef]PaymentHash payment_hash);
54-
sequence<PublicKey> list_peers();
54+
sequence<PeerDetails> list_peers();
5555
sequence<ChannelDetails> list_channels();
5656
};
5757

@@ -135,6 +135,11 @@ dictionary ChannelDetails {
135135
u16? cltv_expiry_delta;
136136
};
137137

138+
dictionary PeerDetails {
139+
PublicKey node_id;
140+
boolean is_connected;
141+
};
142+
138143
[Custom]
139144
typedef string SocketAddr;
140145

src/lib.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ pub use payment_store::{PaymentDirection, PaymentInfo, PaymentStatus};
102102
use peer_store::{PeerInfo, PeerInfoStorage};
103103
use types::{
104104
ChainMonitor, ChannelDetails, ChannelManager, GossipSync, KeysManager, Network, NetworkGraph,
105-
OnionMessenger, OutPoint, PeerManager, Scorer,
105+
OnionMessenger, OutPoint, PeerDetails, PeerManager, Scorer,
106106
};
107107
pub use types::{ChannelId, UserChannelId};
108108
use wallet::Wallet;
@@ -1343,8 +1343,17 @@ impl Node {
13431343
}
13441344

13451345
/// List node's connected peers.
1346-
pub fn list_peers(&self) -> Vec<PublicKey> {
1347-
self.peer_manager.get_peer_node_ids().iter().map(|(pubkey, _)| *pubkey).collect::<Vec<_>>()
1346+
pub fn list_peers(&self) -> Vec<PeerDetails> {
1347+
let active_connected_peers: Vec<PublicKey> =
1348+
self.peer_manager.get_peer_node_ids().iter().map(|p| p.0).collect();
1349+
self.peer_store
1350+
.list_peers()
1351+
.iter()
1352+
.map(|p| PeerDetails {
1353+
node_id: p.pubkey,
1354+
is_connected: active_connected_peers.contains(&p.pubkey),
1355+
})
1356+
.collect()
13481357
}
13491358
}
13501359

src/test/functional_tests.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ fn channel_full_cycle() {
3737
.connect_open_channel(&node_b.node_id(), &node_b.listening_address().unwrap(), 50000, true)
3838
.unwrap();
3939

40-
assert_eq!(node_a.list_peers(), [node_b.node_id()]);
40+
assert_eq!(
41+
node_a.list_peers().iter().map(|p| p.node_id).collect::<Vec<_>>(),
42+
[node_b.node_id()]
43+
);
4144

4245
let funding_txo = loop {
4346
let details = node_a.channel_manager.list_channels();

src/types.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,3 +431,13 @@ impl From<LdkOutpoint> for OutPoint {
431431
OutPoint { txid: value.txid.to_string(), index: value.index }
432432
}
433433
}
434+
435+
/// Details about peers the user is connected to. As returned by [`Node::list_peers`].
436+
///
437+
/// [`Node::list_peers`]: [`crate::Node::list_peers`]
438+
pub struct PeerDetails {
439+
/// Our peer's node ID.
440+
pub node_id: PublicKey,
441+
/// Indicates whether or not the user is currently has an active connection with the peer.
442+
pub is_connected: bool,
443+
}

0 commit comments

Comments
 (0)