@@ -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
@@ -170,8 +175,10 @@ impl RoutingMessageHandler for NetGraphMsgHandler {
170
175
} ;
171
176
while result. len ( ) < batch_amount as usize {
172
177
if let Some ( ( _, ref node) ) = iter. next ( ) {
173
- if node. announcement_message . is_some ( ) {
174
- result. push ( node. announcement_message . clone ( ) . unwrap ( ) ) ;
178
+ if let Some ( node_info) = node. announcement_info . as_ref ( ) {
179
+ if node_info. announcement_message . is_some ( ) {
180
+ result. push ( node_info. announcement_message . clone ( ) . unwrap ( ) ) ;
181
+ }
175
182
}
176
183
} else {
177
184
return result;
@@ -333,47 +340,27 @@ impl Writeable for RoutingFees {
333
340
}
334
341
}
335
342
336
-
337
- #[ derive( PartialEq ) ]
338
- /// Details regarding a node in the network
339
- pub struct NodeInfo {
340
- /// All valid channels a node has announced
341
- pub channels : Vec < u64 > ,
342
- /// Lowest fees enabling routing via any of the known channels to a node
343
- pub lowest_inbound_channel_fees : Option < RoutingFees > ,
343
+ #[ derive( PartialEq , Debug ) ]
344
+ /// Information received in the latest node_announcement from this node.
345
+ pub struct NodeAnnouncementInfo {
344
346
/// Protocol features the node announced support for
345
- pub features : NodeFeatures ,
347
+ pub features : NodeFeatures ,
346
348
/// When the last known update to the node state was issued
347
- /// Unlike for channels, we may have a NodeInfo entry before having received a node_update.
348
- /// Thus, we have to be able to capture "no update has been received", which we do with an
349
- /// Option here.
350
- pub last_update : Option < u32 > ,
349
+ pub last_update : u32 ,
351
350
/// Color assigned to the node
352
351
pub rgb : [ u8 ; 3 ] ,
353
352
/// Moniker assigned to the node
354
353
pub alias : [ u8 ; 32 ] ,
355
354
/// Internet-level addresses via which one can connect to the node
356
355
pub addresses : Vec < NetAddress > ,
357
356
/// An initial announcement of the node
358
- //this is cached here so we can send out it later if required by initial routing sync
359
- //keep an eye on this to see if the extra memory is a problem
360
- pub announcement_message : Option < msgs:: NodeAnnouncement > ,
357
+ // this is cached here so we can send out it later if required by initial routing sync
358
+ // keep an eye on this to see if the extra memory is a problem
359
+ pub announcement_message : Option < msgs:: NodeAnnouncement >
361
360
}
362
361
363
- impl std:: fmt:: Display for NodeInfo {
364
- fn fmt ( & self , f : & mut std:: fmt:: Formatter ) -> Result < ( ) , std:: fmt:: Error > {
365
- 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[ ..] ) ?;
366
- Ok ( ( ) )
367
- }
368
- }
369
-
370
- impl Writeable for NodeInfo {
362
+ impl Writeable for NodeAnnouncementInfo {
371
363
fn write < W : Writer > ( & self , writer : & mut W ) -> Result < ( ) , :: std:: io:: Error > {
372
- ( self . channels . len ( ) as u64 ) . write ( writer) ?;
373
- for ref chan in self . channels . iter ( ) {
374
- chan. write ( writer) ?;
375
- }
376
- self . lowest_inbound_channel_fees . write ( writer) ?;
377
364
self . features . write ( writer) ?;
378
365
self . last_update . write ( writer) ?;
379
366
self . rgb . write ( writer) ?;
@@ -387,16 +374,8 @@ impl Writeable for NodeInfo {
387
374
}
388
375
}
389
376
390
- const MAX_ALLOC_SIZE : u64 = 64 * 1024 ;
391
-
392
- impl Readable for NodeInfo {
393
- fn read < R : :: std:: io:: Read > ( reader : & mut R ) -> Result < NodeInfo , DecodeError > {
394
- let channels_count: u64 = Readable :: read ( reader) ?;
395
- let mut channels = Vec :: with_capacity ( cmp:: min ( channels_count, MAX_ALLOC_SIZE / 8 ) as usize ) ;
396
- for _ in 0 ..channels_count {
397
- channels. push ( Readable :: read ( reader) ?) ;
398
- }
399
- let lowest_inbound_channel_fees = Readable :: read ( reader) ?;
377
+ impl Readable for NodeAnnouncementInfo {
378
+ fn read < R : :: std:: io:: Read > ( reader : & mut R ) -> Result < NodeAnnouncementInfo , DecodeError > {
400
379
let features = Readable :: read ( reader) ?;
401
380
let last_update = Readable :: read ( reader) ?;
402
381
let rgb = Readable :: read ( reader) ?;
@@ -412,9 +391,7 @@ impl Readable for NodeInfo {
412
391
}
413
392
}
414
393
let announcement_message = Readable :: read ( reader) ?;
415
- Ok ( NodeInfo {
416
- channels,
417
- lowest_inbound_channel_fees,
394
+ Ok ( NodeAnnouncementInfo {
418
395
features,
419
396
last_update,
420
397
rgb,
@@ -425,6 +402,57 @@ impl Readable for NodeInfo {
425
402
}
426
403
}
427
404
405
+ #[ derive( PartialEq ) ]
406
+ /// Details regarding a node in the network
407
+ pub struct NodeInfo {
408
+ /// All valid channels a node has announced
409
+ pub channels : Vec < u64 > ,
410
+ /// Lowest fees enabling routing via any of the known channels to a node
411
+ pub lowest_inbound_channel_fees : Option < RoutingFees > ,
412
+ /// More information about a node from node_announcement
413
+ /// Optional because we may have a NodeInfo entry before having received the announcement
414
+ pub announcement_info : Option < NodeAnnouncementInfo >
415
+ }
416
+
417
+ impl std:: fmt:: Display for NodeInfo {
418
+ fn fmt ( & self , f : & mut std:: fmt:: Formatter ) -> Result < ( ) , std:: fmt:: Error > {
419
+ write ! ( f, "lowest_inbound_channel_fees: {:?}, channels: {:?}, announcement_info: {:?}" ,
420
+ self . lowest_inbound_channel_fees, & self . channels[ ..] , self . announcement_info) ?;
421
+ Ok ( ( ) )
422
+ }
423
+ }
424
+
425
+ impl Writeable for NodeInfo {
426
+ fn write < W : Writer > ( & self , writer : & mut W ) -> Result < ( ) , :: std:: io:: Error > {
427
+ ( self . channels . len ( ) as u64 ) . write ( writer) ?;
428
+ for ref chan in self . channels . iter ( ) {
429
+ chan. write ( writer) ?;
430
+ }
431
+ self . lowest_inbound_channel_fees . write ( writer) ?;
432
+ self . announcement_info . write ( writer) ?;
433
+ Ok ( ( ) )
434
+ }
435
+ }
436
+
437
+ const MAX_ALLOC_SIZE : u64 = 64 * 1024 ;
438
+
439
+ impl Readable for NodeInfo {
440
+ fn read < R : :: std:: io:: Read > ( reader : & mut R ) -> Result < NodeInfo , DecodeError > {
441
+ let channels_count: u64 = Readable :: read ( reader) ?;
442
+ let mut channels = Vec :: with_capacity ( cmp:: min ( channels_count, MAX_ALLOC_SIZE / 8 ) as usize ) ;
443
+ for _ in 0 ..channels_count {
444
+ channels. push ( Readable :: read ( reader) ?) ;
445
+ }
446
+ let lowest_inbound_channel_fees = Readable :: read ( reader) ?;
447
+ let announcement_info = Readable :: read ( reader) ?;
448
+ Ok ( NodeInfo {
449
+ channels,
450
+ lowest_inbound_channel_fees,
451
+ announcement_info,
452
+ } )
453
+ }
454
+ }
455
+
428
456
/// Represents the network as nodes and channels between them
429
457
#[ derive( PartialEq ) ]
430
458
pub struct NetworkGraph {
@@ -497,21 +525,22 @@ impl NetworkGraph {
497
525
match self . nodes . get_mut ( & msg. contents . node_id ) {
498
526
None => Err ( LightningError { err : "No existing channels for node_announcement" , action : ErrorAction :: IgnoreError } ) ,
499
527
Some ( node) => {
500
- match node. last_update {
501
- Some ( last_update ) => if last_update >= msg. contents . timestamp {
528
+ if let Some ( node_info ) = node. announcement_info . as_ref ( ) {
529
+ if node_info . last_update >= msg. contents . timestamp {
502
530
return Err ( LightningError { err : "Update older than last processed update" , action : ErrorAction :: IgnoreError } ) ;
503
- } ,
504
- None => { } ,
531
+ }
505
532
}
506
533
507
- node. features = msg. contents . features . clone ( ) ;
508
- node. last_update = Some ( msg. contents . timestamp ) ;
509
- node. rgb = msg. contents . rgb ;
510
- node. alias = msg. contents . alias ;
511
- node. addresses = msg. contents . addresses . clone ( ) ;
512
-
513
534
let should_relay = msg. contents . excess_data . is_empty ( ) && msg. contents . excess_address_data . is_empty ( ) ;
514
- node. announcement_message = if should_relay { Some ( msg. clone ( ) ) } else { None } ;
535
+ node. announcement_info = Some ( NodeAnnouncementInfo {
536
+ features : msg. contents . features . clone ( ) ,
537
+ last_update : msg. contents . timestamp ,
538
+ rgb : msg. contents . rgb ,
539
+ alias : msg. contents . alias ,
540
+ addresses : msg. contents . addresses . clone ( ) ,
541
+ announcement_message : if should_relay { Some ( msg. clone ( ) ) } else { None } ,
542
+ } ) ;
543
+
515
544
Ok ( should_relay)
516
545
}
517
546
}
@@ -588,12 +617,7 @@ impl NetworkGraph {
588
617
node_entry. insert( NodeInfo {
589
618
channels: vec!( msg. contents. short_channel_id) ,
590
619
lowest_inbound_channel_fees: None ,
591
- features: NodeFeatures :: empty( ) ,
592
- last_update: None ,
593
- rgb: [ 0 ; 3 ] ,
594
- alias: [ 0 ; 32 ] ,
595
- addresses: Vec :: new( ) ,
596
- announcement_message: None ,
620
+ announcement_info: None ,
597
621
} ) ;
598
622
}
599
623
}
0 commit comments