@@ -318,11 +318,10 @@ where C::Target: chain::Access, L::Target: Logger
318318 Ok ( msg. contents . excess_data . len ( ) <= MAX_EXCESS_BYTES_FOR_RELAY )
319319 }
320320
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 > ) > {
323322 let channels = self . network_graph . channels . read ( ) . unwrap ( ) ;
324323 let mut iter = channels. range ( starting_point..) ;
325- while result . len ( ) < batch_amount as usize {
324+ loop {
326325 if let Some ( ( _, ref chan) ) = iter. next ( ) {
327326 if chan. announcement_message . is_some ( ) {
328327 let chan_announcement = chan. announcement_message . clone ( ) . unwrap ( ) ;
@@ -334,20 +333,18 @@ where C::Target: chain::Access, L::Target: Logger
334333 if let Some ( two_to_one) = chan. two_to_one . as_ref ( ) {
335334 two_to_one_announcement = two_to_one. last_update_message . clone ( ) ;
336335 }
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) ) ;
338337 } else {
339338 // TODO: We may end up sending un-announced channel_updates if we are sending
340339 // initial sync data while receiving announce/updates for this channel.
341340 }
342341 } else {
343- return result ;
342+ return None ;
344343 }
345344 }
346- result
347345 }
348346
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 > {
351348 let nodes = self . network_graph . nodes . read ( ) . unwrap ( ) ;
352349 let mut iter = if let Some ( pubkey) = starting_point {
353350 let mut iter = nodes. range ( NodeId :: from_pubkey ( pubkey) ..) ;
@@ -356,18 +353,17 @@ where C::Target: chain::Access, L::Target: Logger
356353 } else {
357354 nodes. range :: < NodeId , _ > ( ..)
358355 } ;
359- while result . len ( ) < batch_amount as usize {
356+ loop {
360357 if let Some ( ( _, ref node) ) = iter. next ( ) {
361358 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 ) ;
364361 }
365362 }
366363 } else {
367- return result ;
364+ return None ;
368365 }
369366 }
370- result
371367 }
372368
373369 /// Initiates a stateless sync of routing gossip information with a peer
@@ -2412,8 +2408,8 @@ mod tests {
24122408 let node_2_privkey = & SecretKey :: from_slice ( & [ 41 ; 32 ] ) . unwrap ( ) ;
24132409
24142410 // 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 ( ) ) ;
24172413
24182414 let short_channel_id;
24192415 {
@@ -2427,17 +2423,15 @@ mod tests {
24272423 }
24282424
24292425 // 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;
24342429 assert_eq ! ( update_1, & None ) ;
24352430 assert_eq ! ( update_2, & None ) ;
24362431 } else {
24372432 panic ! ( ) ;
24382433 }
24392434
2440-
24412435 {
24422436 // Valid channel update
24432437 let valid_channel_update = get_signed_channel_update ( |unsigned_channel_update| {
@@ -2450,10 +2444,9 @@ mod tests {
24502444 }
24512445
24522446 // 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;
24572450 assert_ne ! ( update_1, & None ) ;
24582451 assert_eq ! ( update_2, & None ) ;
24592452 } else {
@@ -2473,19 +2466,18 @@ mod tests {
24732466 }
24742467
24752468 // 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;
24802472 assert_eq ! ( update_1, & None ) ;
24812473 assert_eq ! ( update_2, & None ) ;
24822474 } else {
24832475 panic ! ( ) ;
24842476 }
24852477
24862478 // 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 ( ) ) ;
24892481 }
24902482
24912483 #[ test]
@@ -2497,8 +2489,8 @@ mod tests {
24972489 let node_id_1 = PublicKey :: from_secret_key ( & secp_ctx, node_1_privkey) ;
24982490
24992491 // 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 ( ) ) ;
25022494
25032495 {
25042496 // Announce a channel to add 2 nodes
@@ -2509,10 +2501,9 @@ mod tests {
25092501 } ;
25102502 }
25112503
2512-
25132504 // 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 ( ) ) ;
25162507
25172508 {
25182509 let valid_announcement = get_signed_node_announcement ( |_| { } , node_1_privkey, & secp_ctx) ;
@@ -2528,12 +2519,12 @@ mod tests {
25282519 } ;
25292520 }
25302521
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 ( ) ) ;
25332524
25342525 // 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 ( ) ) ;
25372528
25382529 {
25392530 // Later announcement which should not be relayed (excess data) prevent us from sharing a node
@@ -2547,8 +2538,8 @@ mod tests {
25472538 } ;
25482539 }
25492540
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 ( ) ) ;
25522543 }
25532544
25542545 #[ test]
0 commit comments