Skip to content

Commit 07a7e34

Browse files
committed
Improve routing-related documentation
1 parent 7ec98e6 commit 07a7e34

File tree

7 files changed

+64
-41
lines changed

7 files changed

+64
-41
lines changed

ARCH.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ receive `ChannelMonitorUpdate`s from `ChannelManager` and persist them to disk b
1111
channel steps forward.
1212

1313
There are two additional important structures that you may use either on the same device
14-
as the `ChannelManager` or on a separate one. `Router` handles receiving channel and node
15-
announcements and calculates routes for sending payments. `PeerManager` handles the
16-
authenticated and encrypted communication protocol, monitoring for liveness of peers,
17-
routing messages to `ChannelManager` and `Router` instances directly, and receiving
18-
messages from them via the `EventsProvider` interface.
14+
as the `ChannelManager` or on a separate one. `NetGraphMsgHandler` handles receiving channel
15+
and node announcements, which are then used to calculate routes by `get_route` for sending payments.
16+
`PeerManager` handles the authenticated and encrypted communication protocol,
17+
monitoring for liveness of peers, routing messages to `ChannelManager` and `NetGraphMsgHandler`
18+
instances directly, and receiving messages from them via the `EventsProvider` interface.
1919

2020
These structs communicate with each other using a public API, so that you can easily add
2121
a proxy in between for special handling. Further, APIs for key generation, transaction
@@ -56,7 +56,7 @@ At a high level, some of the common interfaces fit together as follows:
5656
| ----------------------- ---------
5757
| | | Event |
5858
(as RoutingMessageHandler) v ---------
59-
\ ----------
60-
-----------------> | Router |
61-
----------
59+
\ --------------------
60+
-----------------> | NetGraphMsgHandler |
61+
--------------------
6262
```

lightning/src/ln/channelmanager.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//! responsible for tracking which channels are open, HTLCs are in flight and reestablishing those
55
//! upon reconnect to the relevant peer(s).
66
//!
7-
//! It does not manage routing logic (see routing::router for that) nor does it manage constructing
7+
//! It does not manage routing logic (see routing::router::get_route for that) nor does it manage constructing
88
//! on-chain transactions (it only monitors the chain to watch for any force-closes that might
99
//! imply it needs to fail HTLCs/payments/channels it manages).
1010

lightning/src/ln/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
//! High level lightning structs and impls live here.
22
//!
3-
//! You probably want to create a channelmanager::ChannelManager, and a router::Router first.
3+
//! You probably want to create a channelmanager::ChannelManager, and a routing::NetGraphMsgHandler first.
44
//! Then, you probably want to pass them both on to a peer_handler::PeerManager and use that to
55
//! create/manage connections and call get_and_clear_pending_events after each action, handling
66
//! them appropriately.
77
//!
88
//! When you want to open/close a channel or send a payment, call into your ChannelManager and when
99
//! you want to learn things about the network topology (eg get a route for sending a payment),
10-
//! call into your Router.
10+
//! call into your NetGraphMsgHandler.
1111
1212
pub mod channelmanager;
1313
pub mod channelmonitor;

lightning/src/ln/peer_handler.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! Instead of actually servicing sockets ourselves we require that you implement the
44
//! SocketDescriptor interface and use that to receive actions which you should perform on the
55
//! socket, and call into PeerManager with bytes read from the socket. The PeerManager will then
6-
//! call into the provided message handlers (probably a ChannelManager and Router) with messages
6+
//! call into the provided message handlers (probably a ChannelManager and NetGraphmsgHandler) with messages
77
//! they should handle, and encoding/sending response messages.
88
99
use bitcoin::secp256k1::key::{SecretKey,PublicKey};

lightning/src/routing/network_graph.rs

Lines changed: 48 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ use std::collections::BTreeMap;
2323
use std::collections::btree_map::Entry as BtreeEntry;
2424
use std;
2525

26-
/// Receives network updates from peers to track view of the network.
26+
/// Receives and validates network updates from peers,
27+
/// stores authentic and relevant data as a network graph.
28+
/// This network graph is then used for routing payments.
29+
/// Provides interface to help with initial routing sync by
30+
/// serving historical announcements.
2731
pub struct NetGraphMsgHandler {
2832
secp_ctx: Secp256k1<secp256k1::VerifyOnly>,
2933
/// Representation of the payment channel network
@@ -35,7 +39,10 @@ pub struct NetGraphMsgHandler {
3539

3640
impl NetGraphMsgHandler {
3741
/// Creates a new tracker of the actual state of the network of channels and nodes,
38-
/// assuming fresh Network Graph
42+
/// assuming a fresh network graph.
43+
/// Chain monitor is used to make sure announced channels exist on-chain,
44+
/// channel data is correct, and that the announcement is signed with
45+
/// channel owners' keys.
3946
pub fn new(chain_monitor: Arc<ChainWatchInterface>, logger: Arc<Logger>) -> Self {
4047
NetGraphMsgHandler {
4148
secp_ctx: Secp256k1::verification_only(),
@@ -61,7 +68,9 @@ impl NetGraphMsgHandler {
6168
}
6269
}
6370

64-
/// Get network addresses by node id
71+
/// Get network addresses by node id.
72+
/// Returns None if the requested node is completely unknown,
73+
/// or if node announcement for the node was never received.
6574
pub fn get_addresses(&self, pubkey: &PublicKey) -> Option<Vec<NetAddress>> {
6675
let network = self.network_graph.read().unwrap();
6776
if let Some(node) = network.get_nodes().get(pubkey) {
@@ -212,13 +221,15 @@ impl RoutingMessageHandler for NetGraphMsgHandler {
212221
}
213222

214223
#[derive(PartialEq, Debug)]
215-
/// Details regarding one direction of a channel
224+
/// Details about one direction of a channel. Received
225+
/// within a channel update.
216226
pub struct DirectionalChannelInfo {
217-
/// When the last update to the channel direction was issued
227+
/// When the last update to the channel direction was issued.
228+
/// Value is opaque, as set in the announcement.
218229
pub last_update: u32,
219-
/// Whether the channel can be currently used for payments
230+
/// Whether the channel can be currently used for payments (in this one direction).
220231
pub enabled: bool,
221-
/// The difference in CLTV values between the source and the destination node of the channel
232+
/// The difference in CLTV values that you must have when routing through this channel.
222233
pub cltv_expiry_delta: u16,
223234
/// The minimum value, which must be relayed to the next hop via the channel
224235
pub htlc_minimum_msat: u64,
@@ -245,7 +256,8 @@ impl_writeable!(DirectionalChannelInfo, 0, {
245256
});
246257

247258
#[derive(PartialEq)]
248-
/// Details regarding a channel (both directions)
259+
/// Details about a channel (both directions).
260+
/// Received within a channel announcement.
249261
pub struct ChannelInfo {
250262
/// Protocol features of a channel communicated during its announcement
251263
pub features: ChannelFeatures,
@@ -258,8 +270,9 @@ pub struct ChannelInfo {
258270
/// Details about the second direction of a channel
259271
pub two_to_one: Option<DirectionalChannelInfo>,
260272
/// An initial announcement of the channel
261-
//this is cached here so we can send out it later if required by initial routing sync
262-
//keep an eye on this to see if the extra memory is a problem
273+
/// Mostly redundant with the data we store in fields explicitly.
274+
/// Everything else is useful only for sending out for initial routing sync.
275+
/// Not stored if contains excess data to prevent DoS.
263276
pub announcement_message: Option<msgs::ChannelAnnouncement>,
264277
}
265278

@@ -284,9 +297,10 @@ impl_writeable!(ChannelInfo, 0, {
284297
/// Fees for routing via a given channel or a node
285298
#[derive(Eq, PartialEq, Copy, Clone, Debug)]
286299
pub struct RoutingFees {
287-
/// Flat routing fee
300+
/// Flat routing fee in satoshis
288301
pub base_msat: u32,
289-
/// Liquidity-based routing fee
302+
/// Liquidity-based routing fee in millionths of a routed amount.
303+
/// In other words, 10000 is 1%.
290304
pub proportional_millionths: u32,
291305
}
292306

@@ -314,17 +328,21 @@ impl Writeable for RoutingFees {
314328
pub struct NodeAnnouncementInfo {
315329
/// Protocol features the node announced support for
316330
pub features: NodeFeatures,
317-
/// When the last known update to the node state was issued
331+
/// When the last known update to the node state was issued.
332+
/// Value is opaque, as set in the announcement.
318333
pub last_update: u32,
319334
/// Color assigned to the node
320335
pub rgb: [u8; 3],
321-
/// Moniker assigned to the node
336+
/// Moniker assigned to the node.
337+
/// May be invalid or malicious (eg control chars),
338+
/// should not be exposed to the user.
322339
pub alias: [u8; 32],
323340
/// Internet-level addresses via which one can connect to the node
324341
pub addresses: Vec<NetAddress>,
325342
/// An initial announcement of the node
326-
// this is cached here so we can send out it later if required by initial routing sync
327-
// keep an eye on this to see if the extra memory is a problem
343+
/// Mostly redundant with the data we store in fields explicitly.
344+
/// Everything else is useful only for sending out for initial routing sync.
345+
/// Not stored if contains excess data to prevent DoS.
328346
pub announcement_message: Option<msgs::NodeAnnouncement>
329347
}
330348

@@ -372,14 +390,17 @@ impl Readable for NodeAnnouncementInfo {
372390
}
373391

374392
#[derive(PartialEq)]
375-
/// Details regarding a node in the network
393+
/// Details about a node in the network, known from the network announcement.
376394
pub struct NodeInfo {
377395
/// All valid channels a node has announced
378396
pub channels: Vec<u64>,
379-
/// Lowest fees enabling routing via any of the known channels to a node
397+
/// Lowest fees enabling routing via any of the known channels to a node.
398+
/// The two fields (flat and proportional fee) are independent,
399+
/// meaning they don't have to refer to the same channel.
380400
pub lowest_inbound_channel_fees: Option<RoutingFees>,
381-
/// More information about a node from node_announcement
382-
/// Optional because we may have a NodeInfo entry before having received the announcement
401+
/// More information about a node from node_announcement.
402+
/// Optional because we store a Node entry after learning about it from
403+
/// a channel announcement, but before receiving a node announcement.
383404
pub announcement_info: Option<NodeAnnouncementInfo>
384405
}
385406

@@ -483,9 +504,9 @@ impl std::fmt::Display for NetworkGraph {
483504
}
484505

485506
impl NetworkGraph {
486-
/// Returns all known valid channels
507+
/// Returns all known valid channels' short ids along with announced channel info.
487508
pub fn get_channels<'a>(&'a self) -> &'a BTreeMap<u64, ChannelInfo> { &self.channels }
488-
/// Returns all known nodes
509+
/// Returns all known nodes' public keys along with announced node info.
489510
pub fn get_nodes<'a>(&'a self) -> &'a BTreeMap<PublicKey, NodeInfo> { &self.nodes }
490511

491512
/// For an already known node (from channel announcements), update its stored properties from a given node announcement
@@ -520,9 +541,11 @@ impl NetworkGraph {
520541
}
521542
}
522543

523-
/// For a new or already known (from previous announcement) channel, store or update channel info,
524-
/// after making sure it corresponds to a real transaction on-chain.
544+
/// For a new or already known (from previous announcement) channel, store or update channel info.
525545
/// Also store nodes (if not stored yet) the channel is between, and make node aware of this channel.
546+
/// Checking utxo on-chain is useful if we receive an update for already known channel id,
547+
/// which is probably result of a reorg. In that case, we update channel info only if the
548+
/// utxo was checked, otherwise stick to the existing update, to prevent DoS risks.
526549
/// Announcement signatures are checked here only if Secp256k1 object is provided.
527550
fn update_channel_from_announcement(&mut self, msg: &msgs::ChannelAnnouncement, checked_utxo: bool, secp_ctx: Option<&Secp256k1<secp256k1::VerifyOnly>>) -> Result<bool, LightningError> {
528551
if let Some(sig_verifier) = secp_ctx {
@@ -621,7 +644,7 @@ impl NetworkGraph {
621644
}
622645
}
623646

624-
/// For an already known (from announcement) channel, update info regarding one of the directions of a channel.
647+
/// For an already known (from announcement) channel, update info about one of the directions of a channel.
625648
/// Announcement signatures are checked here only if Secp256k1 object is provided.
626649
fn update_channel(&mut self, msg: &msgs::ChannelUpdate, secp_ctx: Option<&Secp256k1<secp256k1::VerifyOnly>>) -> Result<bool, LightningError> {
627650
let dest_node_id;

lightning/src/routing/router.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! The top-level routing/network map tracking logic lives here.
22
//!
3-
//! You probably want to create a Router and use that as your RoutingMessageHandler and then
3+
//! You probably want to create a NetGraphMsgHandler and use that as your RoutingMessageHandler and then
44
//! interrogate it to get routes for your own payments.
55
66
use bitcoin::secp256k1::key::PublicKey;
@@ -151,7 +151,7 @@ struct DummyDirectionalChannelInfo {
151151
///
152152
/// If some channels aren't announced, it may be useful to fill in a first_hops with the
153153
/// results from a local ChannelManager::list_usable_channels() call. If it is filled in, our
154-
/// (this Router's) view of our local channels will be ignored, and only those in first_hops
154+
/// view of our local channels (from net_graph_msg_handler) will be ignored, and only those in first_hops
155155
/// will be used.
156156
///
157157
/// Panics if first_hops contains channels without short_channel_ids

lightning/src/util/events.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,9 +332,9 @@ pub enum MessageSendEvent {
332332
action: msgs::ErrorAction
333333
},
334334
/// When a payment fails we may receive updates back from the hop where it failed. In such
335-
/// cases this event is generated so that we can inform the router of this information.
335+
/// cases this event is generated so that we can inform the network graph of this information.
336336
PaymentFailureNetworkUpdate {
337-
/// The channel/node update which should be sent to router
337+
/// The channel/node update which should be sent to NetGraphMsgHandler
338338
update: msgs::HTLCFailChannelUpdate,
339339
}
340340
}

0 commit comments

Comments
 (0)