Skip to content

Commit 97ac065

Browse files
committed
Improve routing-related documentation
1 parent ec8984e commit 97ac065

File tree

7 files changed

+61
-38
lines changed

7 files changed

+61
-38
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: 45 additions & 22 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,6 +39,9 @@ pub struct NetGraphMsgHandler {
3539

3640
impl NetGraphMsgHandler {
3741
/// Creates a new tracker of the actual state of the network of channels and nodes.
42+
/// Chain monitor is used to make sure announced channels exist on-chain,
43+
/// channel data is correct, and that the announcement is signed with
44+
/// channel owners' keys.
3845
pub fn new(chain_monitor: Arc<ChainWatchInterface>, logger: Arc<Logger>) -> Self {
3946
NetGraphMsgHandler {
4047
secp_ctx: Secp256k1::verification_only(),
@@ -48,7 +55,9 @@ impl NetGraphMsgHandler {
4855
}
4956
}
5057

51-
/// Get network addresses by node id
58+
/// Get network addresses by node id.
59+
/// Returns None if the requested node is completely unknown,
60+
/// or if node announcement for the node was never received.
5261
pub fn get_addresses(&self, pubkey: &PublicKey) -> Option<Vec<NetAddress>> {
5362
let network = self.network_graph.read().unwrap();
5463
if let Some(node) = network.get_nodes().get(pubkey) {
@@ -199,13 +208,15 @@ impl RoutingMessageHandler for NetGraphMsgHandler {
199208
}
200209

201210
#[derive(PartialEq, Debug)]
202-
/// Details regarding one direction of a channel
211+
/// Details about one direction of a channel. Received
212+
/// within a channel update.
203213
pub struct DirectionalChannelInfo {
204-
/// When the last update to the channel direction was issued
214+
/// When the last update to the channel direction was issued.
215+
/// Value is opaque, as set in the announcement.
205216
pub last_update: u32,
206-
/// Whether the channel can be currently used for payments
217+
/// Whether the channel can be currently used for payments (in this one direction).
207218
pub enabled: bool,
208-
/// The difference in CLTV values between the source and the destination node of the channel
219+
/// The difference in CLTV values that you must have when routing through this channel.
209220
pub cltv_expiry_delta: u16,
210221
/// The minimum value, which must be relayed to the next hop via the channel
211222
pub htlc_minimum_msat: u64,
@@ -232,7 +243,8 @@ impl_writeable!(DirectionalChannelInfo, 0, {
232243
});
233244

234245
#[derive(PartialEq)]
235-
/// Details regarding a channel (both directions)
246+
/// Details about a channel (both directions).
247+
/// Received within a channel announcement.
236248
pub struct ChannelInfo {
237249
/// Protocol features of a channel communicated during its announcement
238250
pub features: ChannelFeatures,
@@ -245,8 +257,9 @@ pub struct ChannelInfo {
245257
/// Details about the second direction of a channel
246258
pub two_to_one: Option<DirectionalChannelInfo>,
247259
/// An initial announcement of the channel
248-
//this is cached here so we can send out it later if required by initial routing sync
249-
//keep an eye on this to see if the extra memory is a problem
260+
/// Mostly redundant with the data we store in fields explicitly.
261+
/// Everything else is useful only for sending out for initial routing sync.
262+
/// Not stored if contains excess data to prevent DoS.
250263
pub announcement_message: Option<msgs::ChannelAnnouncement>,
251264
}
252265

@@ -271,9 +284,10 @@ impl_writeable!(ChannelInfo, 0, {
271284
/// Fees for routing via a given channel or a node
272285
#[derive(Eq, PartialEq, Copy, Clone, Debug)]
273286
pub struct RoutingFees {
274-
/// Flat routing fee
287+
/// Flat routing fee in satoshis
275288
pub base_msat: u32,
276-
/// Liquidity-based routing fee
289+
/// Liquidity-based routing fee in millionths of a routed amount.
290+
/// In other words, 10000 is 1%.
277291
pub proportional_millionths: u32,
278292
}
279293

@@ -301,17 +315,21 @@ impl Writeable for RoutingFees {
301315
pub struct NodeAnnouncementInfo {
302316
/// Protocol features the node announced support for
303317
pub features: NodeFeatures,
304-
/// When the last known update to the node state was issued
318+
/// When the last known update to the node state was issued.
319+
/// Value is opaque, as set in the announcement.
305320
pub last_update: u32,
306321
/// Color assigned to the node
307322
pub rgb: [u8; 3],
308-
/// Moniker assigned to the node
323+
/// Moniker assigned to the node.
324+
/// May be invalid or malicious (eg control chars),
325+
/// should not be exposed to the user.
309326
pub alias: [u8; 32],
310327
/// Internet-level addresses via which one can connect to the node
311328
pub addresses: Vec<NetAddress>,
312329
/// An initial announcement of the node
313-
// this is cached here so we can send out it later if required by initial routing sync
314-
// keep an eye on this to see if the extra memory is a problem
330+
/// Mostly redundant with the data we store in fields explicitly.
331+
/// Everything else is useful only for sending out for initial routing sync.
332+
/// Not stored if contains excess data to prevent DoS.
315333
pub announcement_message: Option<msgs::NodeAnnouncement>
316334
}
317335

@@ -359,14 +377,17 @@ impl Readable for NodeAnnouncementInfo {
359377
}
360378

361379
#[derive(PartialEq)]
362-
/// Details regarding a node in the network
380+
/// Details about a node in the network, known from the network announcement.
363381
pub struct NodeInfo {
364382
/// All valid channels a node has announced
365383
pub channels: Vec<u64>,
366-
/// Lowest fees enabling routing via any of the known channels to a node
384+
/// Lowest fees enabling routing via any of the known channels to a node.
385+
/// The two fields (flat and proportional fee) are independent,
386+
/// meaning they don't have to refer to the same channel.
367387
pub lowest_inbound_channel_fees: Option<RoutingFees>,
368-
/// More information about a node from node_announcement
369-
/// Optional because we may have a NodeInfo entry before having received the announcement
388+
/// More information about a node from node_announcement.
389+
/// Optional because we store a Node entry after learning about it from
390+
/// a channel announcement, but before receiving a node announcement.
370391
pub announcement_info: Option<NodeAnnouncementInfo>
371392
}
372393

@@ -505,10 +526,12 @@ impl NetworkGraph {
505526
}
506527
}
507528

508-
/// For a new or already known (from previous announcement) channel, store or update channel info,
509-
/// after making sure it corresponds to a real transaction on-chain.
529+
/// For a new or already known (from previous announcement) channel, store or update channel info.
510530
/// Also store nodes (if not stored yet) the channel is between, and make node aware of this channel.
511531
/// Announcement signatures are checked here.
532+
/// Checking utxo on-chain is useful if we receive an update for already known channel id,
533+
/// which is probably result of a reorg. In that case, we update channel info only if the
534+
/// utxo was checked, otherwise stick to the existing update, to prevent DoS risks.
512535
fn update_channel_from_announcement(&mut self, msg: &msgs::ChannelAnnouncement, checked_utxo: bool, secp_ctx: &Secp256k1<secp256k1::VerifyOnly>) -> Result<bool, LightningError> {
513536
let msg_hash = hash_to_message!(&Sha256dHash::hash(&msg.contents.encode()[..])[..]);
514537
secp_verify_sig!(secp_ctx, &msg_hash, &msg.node_signature_1, &msg.contents.node_id_1);
@@ -604,7 +627,7 @@ impl NetworkGraph {
604627
}
605628
}
606629

607-
/// For an already known (from announcement) channel, update info regarding one of the directions of a channel.
630+
/// For an already known (from announcement) channel, update info about one of the directions of a channel.
608631
/// Announcement signatures are checked here.
609632
fn update_channel(&mut self, msg: &msgs::ChannelUpdate, secp_ctx: &Secp256k1<secp256k1::VerifyOnly>) -> Result<bool, LightningError> {
610633
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)