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