@@ -23,7 +23,11 @@ use std::collections::BTreeMap;
23
23
use std:: collections:: btree_map:: Entry as BtreeEntry ;
24
24
use std;
25
25
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.
27
31
pub struct NetGraphMsgHandler {
28
32
secp_ctx : Secp256k1 < secp256k1:: VerifyOnly > ,
29
33
/// Representation of the payment channel network
@@ -35,7 +39,10 @@ pub struct NetGraphMsgHandler {
35
39
36
40
impl NetGraphMsgHandler {
37
41
/// 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.
39
46
pub fn new ( chain_monitor : Arc < ChainWatchInterface > , logger : Arc < Logger > ) -> Self {
40
47
NetGraphMsgHandler {
41
48
secp_ctx : Secp256k1 :: verification_only ( ) ,
@@ -61,7 +68,9 @@ impl NetGraphMsgHandler {
61
68
}
62
69
}
63
70
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.
65
74
pub fn get_addresses ( & self , pubkey : & PublicKey ) -> Option < Vec < NetAddress > > {
66
75
let network = self . network_graph . read ( ) . unwrap ( ) ;
67
76
if let Some ( node) = network. get_nodes ( ) . get ( pubkey) {
@@ -212,13 +221,15 @@ impl RoutingMessageHandler for NetGraphMsgHandler {
212
221
}
213
222
214
223
#[ 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.
216
226
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.
218
229
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).
220
231
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.
222
233
pub cltv_expiry_delta : u16 ,
223
234
/// The minimum value, which must be relayed to the next hop via the channel
224
235
pub htlc_minimum_msat : u64 ,
@@ -245,7 +256,8 @@ impl_writeable!(DirectionalChannelInfo, 0, {
245
256
} ) ;
246
257
247
258
#[ derive( PartialEq ) ]
248
- /// Details regarding a channel (both directions)
259
+ /// Details about a channel (both directions).
260
+ /// Received within a channel announcement.
249
261
pub struct ChannelInfo {
250
262
/// Protocol features of a channel communicated during its announcement
251
263
pub features : ChannelFeatures ,
@@ -258,8 +270,9 @@ pub struct ChannelInfo {
258
270
/// Details about the second direction of a channel
259
271
pub two_to_one : Option < DirectionalChannelInfo > ,
260
272
/// 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.
263
276
pub announcement_message : Option < msgs:: ChannelAnnouncement > ,
264
277
}
265
278
@@ -284,9 +297,10 @@ impl_writeable!(ChannelInfo, 0, {
284
297
/// Fees for routing via a given channel or a node
285
298
#[ derive( Eq , PartialEq , Copy , Clone , Debug ) ]
286
299
pub struct RoutingFees {
287
- /// Flat routing fee
300
+ /// Flat routing fee in satoshis
288
301
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%.
290
304
pub proportional_millionths : u32 ,
291
305
}
292
306
@@ -314,17 +328,21 @@ impl Writeable for RoutingFees {
314
328
pub struct NodeAnnouncementInfo {
315
329
/// Protocol features the node announced support for
316
330
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.
318
333
pub last_update : u32 ,
319
334
/// Color assigned to the node
320
335
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.
322
339
pub alias : [ u8 ; 32 ] ,
323
340
/// Internet-level addresses via which one can connect to the node
324
341
pub addresses : Vec < NetAddress > ,
325
342
/// 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.
328
346
pub announcement_message : Option < msgs:: NodeAnnouncement >
329
347
}
330
348
@@ -372,14 +390,17 @@ impl Readable for NodeAnnouncementInfo {
372
390
}
373
391
374
392
#[ derive( PartialEq ) ]
375
- /// Details regarding a node in the network
393
+ /// Details about a node in the network, known from the network announcement.
376
394
pub struct NodeInfo {
377
395
/// All valid channels a node has announced
378
396
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.
380
400
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.
383
404
pub announcement_info : Option < NodeAnnouncementInfo >
384
405
}
385
406
@@ -483,9 +504,9 @@ impl std::fmt::Display for NetworkGraph {
483
504
}
484
505
485
506
impl NetworkGraph {
486
- /// Returns all known valid channels
507
+ /// Returns all known valid channels' short ids along with announced channel info.
487
508
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.
489
510
pub fn get_nodes < ' a > ( & ' a self ) -> & ' a BTreeMap < PublicKey , NodeInfo > { & self . nodes }
490
511
491
512
/// For an already known node (from channel announcements), update its stored properties from a given node announcement
@@ -520,9 +541,11 @@ impl NetworkGraph {
520
541
}
521
542
}
522
543
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.
525
545
/// 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.
526
549
/// Announcement signatures are checked here only if Secp256k1 object is provided.
527
550
fn update_channel_from_announcement ( & mut self , msg : & msgs:: ChannelAnnouncement , checked_utxo : bool , secp_ctx : Option < & Secp256k1 < secp256k1:: VerifyOnly > > ) -> Result < bool , LightningError > {
528
551
if let Some ( sig_verifier) = secp_ctx {
@@ -621,7 +644,7 @@ impl NetworkGraph {
621
644
}
622
645
}
623
646
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.
625
648
/// Announcement signatures are checked here only if Secp256k1 object is provided.
626
649
fn update_channel ( & mut self , msg : & msgs:: ChannelUpdate , secp_ctx : Option < & Secp256k1 < secp256k1:: VerifyOnly > > ) -> Result < bool , LightningError > {
627
650
let dest_node_id;
0 commit comments