ldk_node/
graph.rs

1// This file is Copyright its original authors, visible in version control history.
2//
3// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5// http://opensource.org/licenses/MIT>, at your option. You may not use this file except in
6// accordance with one or both of these licenses.
7
8//! Objects for querying the network graph.
9
10use crate::types::Graph;
11
12use lightning::routing::gossip::NodeId;
13
14#[cfg(feature = "uniffi")]
15use lightning::ln::msgs::SocketAddress;
16#[cfg(feature = "uniffi")]
17use lightning::routing::gossip::RoutingFees;
18
19#[cfg(not(feature = "uniffi"))]
20use lightning::routing::gossip::{ChannelInfo, NodeInfo};
21
22use std::sync::Arc;
23
24/// Represents the network as nodes and channels between them.
25pub struct NetworkGraph {
26	inner: Arc<Graph>,
27}
28
29impl NetworkGraph {
30	pub(crate) fn new(inner: Arc<Graph>) -> Self {
31		Self { inner }
32	}
33
34	/// Returns the list of channels in the graph
35	pub fn list_channels(&self) -> Vec<u64> {
36		self.inner.read_only().channels().unordered_keys().map(|c| *c).collect()
37	}
38
39	/// Returns information on a channel with the given id.
40	pub fn channel(&self, short_channel_id: u64) -> Option<ChannelInfo> {
41		self.inner.read_only().channels().get(&short_channel_id).cloned().map(|c| c.into())
42	}
43
44	/// Returns the list of nodes in the graph
45	pub fn list_nodes(&self) -> Vec<NodeId> {
46		self.inner.read_only().nodes().unordered_keys().map(|n| *n).collect()
47	}
48
49	/// Returns information on a node with the given id.
50	pub fn node(&self, node_id: &NodeId) -> Option<NodeInfo> {
51		self.inner.read_only().nodes().get(node_id).cloned().map(|n| n.into())
52	}
53}
54
55/// Details about a channel (both directions).
56///
57/// Received within a channel announcement.
58///
59/// This is a simplified version of LDK's `ChannelInfo` for bindings.
60#[cfg(feature = "uniffi")]
61#[derive(Clone, Debug, PartialEq, Eq)]
62pub struct ChannelInfo {
63	/// Source node of the first direction of a channel
64	pub node_one: NodeId,
65	/// Details about the first direction of a channel
66	pub one_to_two: Option<ChannelUpdateInfo>,
67	/// Source node of the second direction of a channel
68	pub node_two: NodeId,
69	/// Details about the second direction of a channel
70	pub two_to_one: Option<ChannelUpdateInfo>,
71	/// The channel capacity as seen on-chain, if chain lookup is available.
72	pub capacity_sats: Option<u64>,
73}
74
75#[cfg(feature = "uniffi")]
76impl From<lightning::routing::gossip::ChannelInfo> for ChannelInfo {
77	fn from(value: lightning::routing::gossip::ChannelInfo) -> Self {
78		Self {
79			node_one: value.node_one,
80			one_to_two: value.one_to_two.map(|u| u.into()),
81			node_two: value.node_two,
82			two_to_one: value.two_to_one.map(|u| u.into()),
83			capacity_sats: value.capacity_sats,
84		}
85	}
86}
87
88/// Details about one direction of a channel as received within a `ChannelUpdate`.
89///
90/// This is a simplified version of LDK's `ChannelUpdateInfo` for bindings.
91#[cfg(feature = "uniffi")]
92#[derive(Clone, Debug, PartialEq, Eq)]
93pub struct ChannelUpdateInfo {
94	/// When the last update to the channel direction was issued.
95	/// Value is opaque, as set in the announcement.
96	pub last_update: u32,
97	/// Whether the channel can be currently used for payments (in this one direction).
98	pub enabled: bool,
99	/// The difference in CLTV values that you must have when routing through this channel.
100	pub cltv_expiry_delta: u16,
101	/// The minimum value, which must be relayed to the next hop via the channel
102	pub htlc_minimum_msat: u64,
103	/// The maximum value which may be relayed to the next hop via the channel.
104	pub htlc_maximum_msat: u64,
105	/// Fees charged when the channel is used for routing
106	pub fees: RoutingFees,
107}
108
109#[cfg(feature = "uniffi")]
110impl From<lightning::routing::gossip::ChannelUpdateInfo> for ChannelUpdateInfo {
111	fn from(value: lightning::routing::gossip::ChannelUpdateInfo) -> Self {
112		Self {
113			last_update: value.last_update,
114			enabled: value.enabled,
115			cltv_expiry_delta: value.cltv_expiry_delta,
116			htlc_minimum_msat: value.htlc_minimum_msat,
117			htlc_maximum_msat: value.htlc_maximum_msat,
118			fees: value.fees,
119		}
120	}
121}
122
123/// Details about a node in the network, known from the network announcement.
124///
125/// This is a simplified version of LDK's `NodeInfo` for bindings.
126#[cfg(feature = "uniffi")]
127#[derive(Clone, Debug, PartialEq, Eq)]
128pub struct NodeInfo {
129	/// All valid channels a node has announced
130	pub channels: Vec<u64>,
131	/// More information about a node from node_announcement.
132	/// Optional because we store a Node entry after learning about it from
133	/// a channel announcement, but before receiving a node announcement.
134	pub announcement_info: Option<NodeAnnouncementInfo>,
135}
136
137#[cfg(feature = "uniffi")]
138impl From<lightning::routing::gossip::NodeInfo> for NodeInfo {
139	fn from(value: lightning::routing::gossip::NodeInfo) -> Self {
140		Self {
141			channels: value.channels,
142			announcement_info: value.announcement_info.map(|a| a.into()),
143		}
144	}
145}
146
147/// Information received in the latest node_announcement from this node.
148///
149/// This is a simplified version of LDK's `NodeAnnouncementInfo` for bindings.
150#[cfg(feature = "uniffi")]
151#[derive(Clone, Debug, PartialEq, Eq)]
152pub struct NodeAnnouncementInfo {
153	/// When the last known update to the node state was issued.
154	/// Value is opaque, as set in the announcement.
155	pub last_update: u32,
156	/// Moniker assigned to the node.
157	/// May be invalid or malicious (eg control chars),
158	/// should not be exposed to the user.
159	pub alias: String,
160	/// List of addresses on which this node is reachable
161	pub addresses: Vec<SocketAddress>,
162}
163
164#[cfg(feature = "uniffi")]
165impl From<lightning::routing::gossip::NodeAnnouncementInfo> for NodeAnnouncementInfo {
166	fn from(value: lightning::routing::gossip::NodeAnnouncementInfo) -> Self {
167		Self {
168			last_update: value.last_update(),
169			alias: value.alias().to_string(),
170			addresses: value.addresses().iter().cloned().collect(),
171		}
172	}
173}