@@ -318,11 +318,10 @@ where C::Target: chain::Access, L::Target: Logger
318
318
Ok ( msg. contents . excess_data . len ( ) <= MAX_EXCESS_BYTES_FOR_RELAY )
319
319
}
320
320
321
- fn get_next_channel_announcements ( & self , starting_point : u64 , batch_amount : u8 ) -> Vec < ( ChannelAnnouncement , Option < ChannelUpdate > , Option < ChannelUpdate > ) > {
322
- let mut result = Vec :: with_capacity ( batch_amount as usize ) ;
321
+ fn get_next_channel_announcements ( & self , starting_point : u64 ) -> Option < ( ChannelAnnouncement , Option < ChannelUpdate > , Option < ChannelUpdate > ) > {
323
322
let channels = self . network_graph . channels . read ( ) . unwrap ( ) ;
324
323
let mut iter = channels. range ( starting_point..) ;
325
- while result . len ( ) < batch_amount as usize {
324
+ loop {
326
325
if let Some ( ( _, ref chan) ) = iter. next ( ) {
327
326
if chan. announcement_message . is_some ( ) {
328
327
let chan_announcement = chan. announcement_message . clone ( ) . unwrap ( ) ;
@@ -334,20 +333,18 @@ where C::Target: chain::Access, L::Target: Logger
334
333
if let Some ( two_to_one) = chan. two_to_one . as_ref ( ) {
335
334
two_to_one_announcement = two_to_one. last_update_message . clone ( ) ;
336
335
}
337
- result . push ( ( chan_announcement, one_to_two_announcement, two_to_one_announcement) ) ;
336
+ return Some ( ( chan_announcement, one_to_two_announcement, two_to_one_announcement) ) ;
338
337
} else {
339
338
// TODO: We may end up sending un-announced channel_updates if we are sending
340
339
// initial sync data while receiving announce/updates for this channel.
341
340
}
342
341
} else {
343
- return result ;
342
+ return None ;
344
343
}
345
344
}
346
- result
347
345
}
348
346
349
- fn get_next_node_announcements ( & self , starting_point : Option < & PublicKey > , batch_amount : u8 ) -> Vec < NodeAnnouncement > {
350
- let mut result = Vec :: with_capacity ( batch_amount as usize ) ;
347
+ fn get_next_node_announcements ( & self , starting_point : Option < & PublicKey > ) -> Option < NodeAnnouncement > {
351
348
let nodes = self . network_graph . nodes . read ( ) . unwrap ( ) ;
352
349
let mut iter = if let Some ( pubkey) = starting_point {
353
350
let mut iter = nodes. range ( NodeId :: from_pubkey ( pubkey) ..) ;
@@ -356,18 +353,17 @@ where C::Target: chain::Access, L::Target: Logger
356
353
} else {
357
354
nodes. range :: < NodeId , _ > ( ..)
358
355
} ;
359
- while result . len ( ) < batch_amount as usize {
356
+ loop {
360
357
if let Some ( ( _, ref node) ) = iter. next ( ) {
361
358
if let Some ( node_info) = node. announcement_info . as_ref ( ) {
362
- if node_info. announcement_message . is_some ( ) {
363
- result . push ( node_info . announcement_message . clone ( ) . unwrap ( ) ) ;
359
+ if let Some ( msg ) = node_info. announcement_message . clone ( ) {
360
+ return Some ( msg ) ;
364
361
}
365
362
}
366
363
} else {
367
- return result ;
364
+ return None ;
368
365
}
369
366
}
370
- result
371
367
}
372
368
373
369
/// Initiates a stateless sync of routing gossip information with a peer
@@ -2412,8 +2408,8 @@ mod tests {
2412
2408
let node_2_privkey = & SecretKey :: from_slice ( & [ 41 ; 32 ] ) . unwrap ( ) ;
2413
2409
2414
2410
// Channels were not announced yet.
2415
- let channels_with_announcements = gossip_sync. get_next_channel_announcements ( 0 , 1 ) ;
2416
- assert_eq ! ( channels_with_announcements. len ( ) , 0 ) ;
2411
+ let channels_with_announcements = gossip_sync. get_next_channel_announcements ( 0 ) ;
2412
+ assert ! ( channels_with_announcements. is_none ( ) ) ;
2417
2413
2418
2414
let short_channel_id;
2419
2415
{
@@ -2427,17 +2423,15 @@ mod tests {
2427
2423
}
2428
2424
2429
2425
// Contains initial channel announcement now.
2430
- let channels_with_announcements = gossip_sync. get_next_channel_announcements ( short_channel_id, 1 ) ;
2431
- assert_eq ! ( channels_with_announcements. len( ) , 1 ) ;
2432
- if let Some ( channel_announcements) = channels_with_announcements. first ( ) {
2433
- let & ( _, ref update_1, ref update_2) = channel_announcements;
2426
+ let channels_with_announcements = gossip_sync. get_next_channel_announcements ( short_channel_id) ;
2427
+ if let Some ( channel_announcements) = channels_with_announcements {
2428
+ let ( _, ref update_1, ref update_2) = channel_announcements;
2434
2429
assert_eq ! ( update_1, & None ) ;
2435
2430
assert_eq ! ( update_2, & None ) ;
2436
2431
} else {
2437
2432
panic ! ( ) ;
2438
2433
}
2439
2434
2440
-
2441
2435
{
2442
2436
// Valid channel update
2443
2437
let valid_channel_update = get_signed_channel_update ( |unsigned_channel_update| {
@@ -2450,10 +2444,9 @@ mod tests {
2450
2444
}
2451
2445
2452
2446
// Now contains an initial announcement and an update.
2453
- let channels_with_announcements = gossip_sync. get_next_channel_announcements ( short_channel_id, 1 ) ;
2454
- assert_eq ! ( channels_with_announcements. len( ) , 1 ) ;
2455
- if let Some ( channel_announcements) = channels_with_announcements. first ( ) {
2456
- let & ( _, ref update_1, ref update_2) = channel_announcements;
2447
+ let channels_with_announcements = gossip_sync. get_next_channel_announcements ( short_channel_id) ;
2448
+ if let Some ( channel_announcements) = channels_with_announcements {
2449
+ let ( _, ref update_1, ref update_2) = channel_announcements;
2457
2450
assert_ne ! ( update_1, & None ) ;
2458
2451
assert_eq ! ( update_2, & None ) ;
2459
2452
} else {
@@ -2473,19 +2466,18 @@ mod tests {
2473
2466
}
2474
2467
2475
2468
// Test that announcements with excess data won't be returned
2476
- let channels_with_announcements = gossip_sync. get_next_channel_announcements ( short_channel_id, 1 ) ;
2477
- assert_eq ! ( channels_with_announcements. len( ) , 1 ) ;
2478
- if let Some ( channel_announcements) = channels_with_announcements. first ( ) {
2479
- let & ( _, ref update_1, ref update_2) = channel_announcements;
2469
+ let channels_with_announcements = gossip_sync. get_next_channel_announcements ( short_channel_id) ;
2470
+ if let Some ( channel_announcements) = channels_with_announcements {
2471
+ let ( _, ref update_1, ref update_2) = channel_announcements;
2480
2472
assert_eq ! ( update_1, & None ) ;
2481
2473
assert_eq ! ( update_2, & None ) ;
2482
2474
} else {
2483
2475
panic ! ( ) ;
2484
2476
}
2485
2477
2486
2478
// Further starting point have no channels after it
2487
- let channels_with_announcements = gossip_sync. get_next_channel_announcements ( short_channel_id + 1000 , 1 ) ;
2488
- assert_eq ! ( channels_with_announcements. len ( ) , 0 ) ;
2479
+ let channels_with_announcements = gossip_sync. get_next_channel_announcements ( short_channel_id + 1000 ) ;
2480
+ assert ! ( channels_with_announcements. is_none ( ) ) ;
2489
2481
}
2490
2482
2491
2483
#[ test]
@@ -2497,8 +2489,8 @@ mod tests {
2497
2489
let node_id_1 = PublicKey :: from_secret_key ( & secp_ctx, node_1_privkey) ;
2498
2490
2499
2491
// No nodes yet.
2500
- let next_announcements = gossip_sync. get_next_node_announcements ( None , 10 ) ;
2501
- assert_eq ! ( next_announcements. len ( ) , 0 ) ;
2492
+ let next_announcements = gossip_sync. get_next_node_announcements ( None ) ;
2493
+ assert ! ( next_announcements. is_none ( ) ) ;
2502
2494
2503
2495
{
2504
2496
// Announce a channel to add 2 nodes
@@ -2509,10 +2501,9 @@ mod tests {
2509
2501
} ;
2510
2502
}
2511
2503
2512
-
2513
2504
// Nodes were never announced
2514
- let next_announcements = gossip_sync. get_next_node_announcements ( None , 3 ) ;
2515
- assert_eq ! ( next_announcements. len ( ) , 0 ) ;
2505
+ let next_announcements = gossip_sync. get_next_node_announcements ( None ) ;
2506
+ assert ! ( next_announcements. is_none ( ) ) ;
2516
2507
2517
2508
{
2518
2509
let valid_announcement = get_signed_node_announcement ( |_| { } , node_1_privkey, & secp_ctx) ;
@@ -2528,12 +2519,12 @@ mod tests {
2528
2519
} ;
2529
2520
}
2530
2521
2531
- let next_announcements = gossip_sync. get_next_node_announcements ( None , 3 ) ;
2532
- assert_eq ! ( next_announcements. len ( ) , 2 ) ;
2522
+ let next_announcements = gossip_sync. get_next_node_announcements ( None ) ;
2523
+ assert ! ( next_announcements. is_some ( ) ) ;
2533
2524
2534
2525
// Skip the first node.
2535
- let next_announcements = gossip_sync. get_next_node_announcements ( Some ( & node_id_1) , 2 ) ;
2536
- assert_eq ! ( next_announcements. len ( ) , 1 ) ;
2526
+ let next_announcements = gossip_sync. get_next_node_announcements ( Some ( & node_id_1) ) ;
2527
+ assert ! ( next_announcements. is_some ( ) ) ;
2537
2528
2538
2529
{
2539
2530
// Later announcement which should not be relayed (excess data) prevent us from sharing a node
@@ -2547,8 +2538,8 @@ mod tests {
2547
2538
} ;
2548
2539
}
2549
2540
2550
- let next_announcements = gossip_sync. get_next_node_announcements ( Some ( & node_id_1) , 2 ) ;
2551
- assert_eq ! ( next_announcements. len ( ) , 0 ) ;
2541
+ let next_announcements = gossip_sync. get_next_node_announcements ( Some ( & node_id_1) ) ;
2542
+ assert ! ( next_announcements. is_none ( ) ) ;
2552
2543
}
2553
2544
2554
2545
#[ test]
0 commit comments