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