@@ -51,7 +51,12 @@ impl NetGraphMsgHandler {
51
51
/// Get network addresses by node id
52
52
pub fn get_addresses ( & self , pubkey : & PublicKey ) -> Option < Vec < NetAddress > > {
53
53
let network = self . network_graph . read ( ) . unwrap ( ) ;
54
- network. get_nodes ( ) . get ( pubkey) . map ( |n| n. addresses . clone ( ) )
54
+ if let Some ( node) = network. get_nodes ( ) . get ( pubkey) {
55
+ if let Some ( node_info) = node. announcement_info . as_ref ( ) {
56
+ return Some ( node_info. addresses . clone ( ) )
57
+ }
58
+ }
59
+ None
55
60
}
56
61
57
62
/// Dumps the entire network view of this NetGraphMsgHandler to the logger provided in the constructor at
@@ -162,8 +167,10 @@ impl RoutingMessageHandler for NetGraphMsgHandler {
162
167
} ;
163
168
while result. len ( ) < batch_amount as usize {
164
169
if let Some ( ( _, ref node) ) = iter. next ( ) {
165
- if node. announcement_message . is_some ( ) {
166
- result. push ( node. announcement_message . clone ( ) . unwrap ( ) ) ;
170
+ if let Some ( node_info) = node. announcement_info . as_ref ( ) {
171
+ if node_info. announcement_message . is_some ( ) {
172
+ result. push ( node_info. announcement_message . clone ( ) . unwrap ( ) ) ;
173
+ }
167
174
}
168
175
} else {
169
176
return result;
@@ -325,47 +332,27 @@ impl Writeable for RoutingFees {
325
332
}
326
333
}
327
334
328
-
329
- #[ derive( PartialEq ) ]
330
- /// Details regarding a node in the network
331
- pub struct NodeInfo {
332
- /// All valid channels a node has announced
333
- pub channels : Vec < u64 > ,
334
- /// Lowest fees enabling routing via any of the known channels to a node
335
- pub lowest_inbound_channel_fees : Option < RoutingFees > ,
335
+ #[ derive( PartialEq , Debug ) ]
336
+ /// Information received in the latest node_announcement from this node.
337
+ pub struct NodeAnnouncementInfo {
336
338
/// Protocol features the node announced support for
337
- pub features : NodeFeatures ,
339
+ pub features : NodeFeatures ,
338
340
/// When the last known update to the node state was issued
339
- /// Unlike for channels, we may have a NodeInfo entry before having received a node_update.
340
- /// Thus, we have to be able to capture "no update has been received", which we do with an
341
- /// Option here.
342
- pub last_update : Option < u32 > ,
341
+ pub last_update : u32 ,
343
342
/// Color assigned to the node
344
343
pub rgb : [ u8 ; 3 ] ,
345
344
/// Moniker assigned to the node
346
345
pub alias : [ u8 ; 32 ] ,
347
346
/// Internet-level addresses via which one can connect to the node
348
347
pub addresses : Vec < NetAddress > ,
349
348
/// An initial announcement of the node
350
- //this is cached here so we can send out it later if required by initial routing sync
351
- //keep an eye on this to see if the extra memory is a problem
352
- pub announcement_message : Option < msgs:: NodeAnnouncement > ,
349
+ // this is cached here so we can send out it later if required by initial routing sync
350
+ // keep an eye on this to see if the extra memory is a problem
351
+ pub announcement_message : Option < msgs:: NodeAnnouncement >
353
352
}
354
353
355
- impl std:: fmt:: Display for NodeInfo {
356
- fn fmt ( & self , f : & mut std:: fmt:: Formatter ) -> Result < ( ) , std:: fmt:: Error > {
357
- write ! ( f, "features: {}, last_update: {:?}, lowest_inbound_channel_fees: {:?}, channels: {:?}" , log_bytes!( self . features. encode( ) ) , self . last_update, self . lowest_inbound_channel_fees, & self . channels[ ..] ) ?;
358
- Ok ( ( ) )
359
- }
360
- }
361
-
362
- impl Writeable for NodeInfo {
354
+ impl Writeable for NodeAnnouncementInfo {
363
355
fn write < W : Writer > ( & self , writer : & mut W ) -> Result < ( ) , :: std:: io:: Error > {
364
- ( self . channels . len ( ) as u64 ) . write ( writer) ?;
365
- for ref chan in self . channels . iter ( ) {
366
- chan. write ( writer) ?;
367
- }
368
- self . lowest_inbound_channel_fees . write ( writer) ?;
369
356
self . features . write ( writer) ?;
370
357
self . last_update . write ( writer) ?;
371
358
self . rgb . write ( writer) ?;
@@ -379,16 +366,8 @@ impl Writeable for NodeInfo {
379
366
}
380
367
}
381
368
382
- const MAX_ALLOC_SIZE : u64 = 64 * 1024 ;
383
-
384
- impl Readable for NodeInfo {
385
- fn read < R : :: std:: io:: Read > ( reader : & mut R ) -> Result < NodeInfo , DecodeError > {
386
- let channels_count: u64 = Readable :: read ( reader) ?;
387
- let mut channels = Vec :: with_capacity ( cmp:: min ( channels_count, MAX_ALLOC_SIZE / 8 ) as usize ) ;
388
- for _ in 0 ..channels_count {
389
- channels. push ( Readable :: read ( reader) ?) ;
390
- }
391
- let lowest_inbound_channel_fees = Readable :: read ( reader) ?;
369
+ impl Readable for NodeAnnouncementInfo {
370
+ fn read < R : :: std:: io:: Read > ( reader : & mut R ) -> Result < NodeAnnouncementInfo , DecodeError > {
392
371
let features = Readable :: read ( reader) ?;
393
372
let last_update = Readable :: read ( reader) ?;
394
373
let rgb = Readable :: read ( reader) ?;
@@ -404,9 +383,7 @@ impl Readable for NodeInfo {
404
383
}
405
384
}
406
385
let announcement_message = Readable :: read ( reader) ?;
407
- Ok ( NodeInfo {
408
- channels,
409
- lowest_inbound_channel_fees,
386
+ Ok ( NodeAnnouncementInfo {
410
387
features,
411
388
last_update,
412
389
rgb,
@@ -417,6 +394,57 @@ impl Readable for NodeInfo {
417
394
}
418
395
}
419
396
397
+ #[ derive( PartialEq ) ]
398
+ /// Details regarding a node in the network
399
+ pub struct NodeInfo {
400
+ /// All valid channels a node has announced
401
+ pub channels : Vec < u64 > ,
402
+ /// Lowest fees enabling routing via any of the known channels to a node
403
+ pub lowest_inbound_channel_fees : Option < RoutingFees > ,
404
+ /// More information about a node from node_announcement
405
+ /// Optional because we may have a NodeInfo entry before having received the announcement
406
+ pub announcement_info : Option < NodeAnnouncementInfo >
407
+ }
408
+
409
+ impl std:: fmt:: Display for NodeInfo {
410
+ fn fmt ( & self , f : & mut std:: fmt:: Formatter ) -> Result < ( ) , std:: fmt:: Error > {
411
+ write ! ( f, "lowest_inbound_channel_fees: {:?}, channels: {:?}, announcement_info: {:?}" ,
412
+ self . lowest_inbound_channel_fees, & self . channels[ ..] , self . announcement_info) ?;
413
+ Ok ( ( ) )
414
+ }
415
+ }
416
+
417
+ impl Writeable for NodeInfo {
418
+ fn write < W : Writer > ( & self , writer : & mut W ) -> Result < ( ) , :: std:: io:: Error > {
419
+ ( self . channels . len ( ) as u64 ) . write ( writer) ?;
420
+ for ref chan in self . channels . iter ( ) {
421
+ chan. write ( writer) ?;
422
+ }
423
+ self . lowest_inbound_channel_fees . write ( writer) ?;
424
+ self . announcement_info . write ( writer) ?;
425
+ Ok ( ( ) )
426
+ }
427
+ }
428
+
429
+ const MAX_ALLOC_SIZE : u64 = 64 * 1024 ;
430
+
431
+ impl Readable for NodeInfo {
432
+ fn read < R : :: std:: io:: Read > ( reader : & mut R ) -> Result < NodeInfo , DecodeError > {
433
+ let channels_count: u64 = Readable :: read ( reader) ?;
434
+ let mut channels = Vec :: with_capacity ( cmp:: min ( channels_count, MAX_ALLOC_SIZE / 8 ) as usize ) ;
435
+ for _ in 0 ..channels_count {
436
+ channels. push ( Readable :: read ( reader) ?) ;
437
+ }
438
+ let lowest_inbound_channel_fees = Readable :: read ( reader) ?;
439
+ let announcement_info = Readable :: read ( reader) ?;
440
+ Ok ( NodeInfo {
441
+ channels,
442
+ lowest_inbound_channel_fees,
443
+ announcement_info,
444
+ } )
445
+ }
446
+ }
447
+
420
448
/// Represents the network as nodes and channels between them
421
449
#[ derive( PartialEq ) ]
422
450
pub struct NetworkGraph {
@@ -494,21 +522,22 @@ impl NetworkGraph {
494
522
match self . nodes . get_mut ( & msg. contents . node_id ) {
495
523
None => Err ( LightningError { err : "No existing channels for node_announcement" , action : ErrorAction :: IgnoreError } ) ,
496
524
Some ( node) => {
497
- match node. last_update {
498
- Some ( last_update ) => if last_update >= msg. contents . timestamp {
525
+ if let Some ( node_info ) = node. announcement_info . as_ref ( ) {
526
+ if node_info . last_update >= msg. contents . timestamp {
499
527
return Err ( LightningError { err : "Update older than last processed update" , action : ErrorAction :: IgnoreError } ) ;
500
- } ,
501
- None => { } ,
528
+ }
502
529
}
503
530
504
- node. features = msg. contents . features . clone ( ) ;
505
- node. last_update = Some ( msg. contents . timestamp ) ;
506
- node. rgb = msg. contents . rgb ;
507
- node. alias = msg. contents . alias ;
508
- node. addresses = msg. contents . addresses . clone ( ) ;
509
-
510
531
let should_relay = msg. contents . excess_data . is_empty ( ) && msg. contents . excess_address_data . is_empty ( ) ;
511
- node. announcement_message = if should_relay { Some ( msg. clone ( ) ) } else { None } ;
532
+ node. announcement_info = Some ( NodeAnnouncementInfo {
533
+ features : msg. contents . features . clone ( ) ,
534
+ last_update : msg. contents . timestamp ,
535
+ rgb : msg. contents . rgb ,
536
+ alias : msg. contents . alias ,
537
+ addresses : msg. contents . addresses . clone ( ) ,
538
+ announcement_message : if should_relay { Some ( msg. clone ( ) ) } else { None } ,
539
+ } ) ;
540
+
512
541
Ok ( should_relay)
513
542
}
514
543
}
@@ -593,12 +622,7 @@ impl NetworkGraph {
593
622
node_entry. insert( NodeInfo {
594
623
channels: vec!( msg. contents. short_channel_id) ,
595
624
lowest_inbound_channel_fees: None ,
596
- features: NodeFeatures :: empty( ) ,
597
- last_update: None ,
598
- rgb: [ 0 ; 3 ] ,
599
- alias: [ 0 ; 32 ] ,
600
- addresses: Vec :: new( ) ,
601
- announcement_message: None ,
625
+ announcement_info: None ,
602
626
} ) ;
603
627
}
604
628
}
0 commit comments