@@ -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,6 +39,9 @@ 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.
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.
38
45
pub fn new ( chain_monitor : Arc < ChainWatchInterface > , logger : Arc < Logger > ) -> Self {
39
46
NetGraphMsgHandler {
40
47
secp_ctx : Secp256k1 :: verification_only ( ) ,
@@ -48,7 +55,9 @@ impl NetGraphMsgHandler {
48
55
}
49
56
}
50
57
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.
52
61
pub fn get_addresses ( & self , pubkey : & PublicKey ) -> Option < Vec < NetAddress > > {
53
62
let network = self . network_graph . read ( ) . unwrap ( ) ;
54
63
if let Some ( node) = network. get_nodes ( ) . get ( pubkey) {
@@ -199,13 +208,15 @@ impl RoutingMessageHandler for NetGraphMsgHandler {
199
208
}
200
209
201
210
#[ 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.
203
213
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.
205
216
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).
207
218
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.
209
220
pub cltv_expiry_delta : u16 ,
210
221
/// The minimum value, which must be relayed to the next hop via the channel
211
222
pub htlc_minimum_msat : u64 ,
@@ -232,7 +243,8 @@ impl_writeable!(DirectionalChannelInfo, 0, {
232
243
} ) ;
233
244
234
245
#[ derive( PartialEq ) ]
235
- /// Details regarding a channel (both directions)
246
+ /// Details about a channel (both directions).
247
+ /// Received within a channel announcement.
236
248
pub struct ChannelInfo {
237
249
/// Protocol features of a channel communicated during its announcement
238
250
pub features : ChannelFeatures ,
@@ -245,8 +257,9 @@ pub struct ChannelInfo {
245
257
/// Details about the second direction of a channel
246
258
pub two_to_one : Option < DirectionalChannelInfo > ,
247
259
/// 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.
250
263
pub announcement_message : Option < msgs:: ChannelAnnouncement > ,
251
264
}
252
265
@@ -271,9 +284,10 @@ impl_writeable!(ChannelInfo, 0, {
271
284
/// Fees for routing via a given channel or a node
272
285
#[ derive( Eq , PartialEq , Copy , Clone , Debug ) ]
273
286
pub struct RoutingFees {
274
- /// Flat routing fee
287
+ /// Flat routing fee in satoshis
275
288
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%.
277
291
pub proportional_millionths : u32 ,
278
292
}
279
293
@@ -301,17 +315,21 @@ impl Writeable for RoutingFees {
301
315
pub struct NodeAnnouncementInfo {
302
316
/// Protocol features the node announced support for
303
317
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.
305
320
pub last_update : u32 ,
306
321
/// Color assigned to the node
307
322
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.
309
326
pub alias : [ u8 ; 32 ] ,
310
327
/// Internet-level addresses via which one can connect to the node
311
328
pub addresses : Vec < NetAddress > ,
312
329
/// 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.
315
333
pub announcement_message : Option < msgs:: NodeAnnouncement >
316
334
}
317
335
@@ -359,14 +377,17 @@ impl Readable for NodeAnnouncementInfo {
359
377
}
360
378
361
379
#[ derive( PartialEq ) ]
362
- /// Details regarding a node in the network
380
+ /// Details about a node in the network, known from the network announcement.
363
381
pub struct NodeInfo {
364
382
/// All valid channels a node has announced
365
383
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.
367
387
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.
370
391
pub announcement_info : Option < NodeAnnouncementInfo >
371
392
}
372
393
@@ -505,10 +526,12 @@ impl NetworkGraph {
505
526
}
506
527
}
507
528
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.
510
530
/// Also store nodes (if not stored yet) the channel is between, and make node aware of this channel.
511
531
/// 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.
512
535
fn update_channel_from_announcement ( & mut self , msg : & msgs:: ChannelAnnouncement , checked_utxo : bool , secp_ctx : & Secp256k1 < secp256k1:: VerifyOnly > ) -> Result < bool , LightningError > {
513
536
let msg_hash = hash_to_message ! ( & Sha256dHash :: hash( & msg. contents. encode( ) [ ..] ) [ ..] ) ;
514
537
secp_verify_sig ! ( secp_ctx, & msg_hash, & msg. node_signature_1, & msg. contents. node_id_1) ;
@@ -604,7 +627,7 @@ impl NetworkGraph {
604
627
}
605
628
}
606
629
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.
608
631
/// Announcement signatures are checked here.
609
632
fn update_channel ( & mut self , msg : & msgs:: ChannelUpdate , secp_ctx : & Secp256k1 < secp256k1:: VerifyOnly > ) -> Result < bool , LightningError > {
610
633
let dest_node_id;
0 commit comments