Skip to content

Commit 0ee88d0

Browse files
committed
Replace manual iteration with for loops in outbound gossip sync
1 parent 7717fa2 commit 0ee88d0

File tree

1 file changed

+23
-32
lines changed

1 file changed

+23
-32
lines changed

lightning/src/routing/gossip.rs

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use core::{cmp, fmt};
4141
use sync::{RwLock, RwLockReadGuard};
4242
use core::sync::atomic::{AtomicUsize, Ordering};
4343
use sync::Mutex;
44-
use core::ops::Deref;
44+
use core::ops::{Bound, Deref};
4545
use bitcoin::hashes::hex::ToHex;
4646

4747
#[cfg(feature = "std")]
@@ -320,50 +320,41 @@ where C::Target: chain::Access, L::Target: Logger
320320

321321
fn get_next_channel_announcement(&self, starting_point: u64) -> Option<(ChannelAnnouncement, Option<ChannelUpdate>, Option<ChannelUpdate>)> {
322322
let channels = self.network_graph.channels.read().unwrap();
323-
let mut iter = channels.range(starting_point..);
324-
loop {
325-
if let Some((_, ref chan)) = iter.next() {
326-
if chan.announcement_message.is_some() {
327-
let chan_announcement = chan.announcement_message.clone().unwrap();
328-
let mut one_to_two_announcement: Option<msgs::ChannelUpdate> = None;
329-
let mut two_to_one_announcement: Option<msgs::ChannelUpdate> = None;
330-
if let Some(one_to_two) = chan.one_to_two.as_ref() {
331-
one_to_two_announcement = one_to_two.last_update_message.clone();
332-
}
333-
if let Some(two_to_one) = chan.two_to_one.as_ref() {
334-
two_to_one_announcement = two_to_one.last_update_message.clone();
335-
}
336-
return Some((chan_announcement, one_to_two_announcement, two_to_one_announcement));
337-
} else {
338-
// TODO: We may end up sending un-announced channel_updates if we are sending
339-
// initial sync data while receiving announce/updates for this channel.
323+
for (_, ref chan) in channels.range(starting_point..) {
324+
if chan.announcement_message.is_some() {
325+
let chan_announcement = chan.announcement_message.clone().unwrap();
326+
let mut one_to_two_announcement: Option<msgs::ChannelUpdate> = None;
327+
let mut two_to_one_announcement: Option<msgs::ChannelUpdate> = None;
328+
if let Some(one_to_two) = chan.one_to_two.as_ref() {
329+
one_to_two_announcement = one_to_two.last_update_message.clone();
340330
}
331+
if let Some(two_to_one) = chan.two_to_one.as_ref() {
332+
two_to_one_announcement = two_to_one.last_update_message.clone();
333+
}
334+
return Some((chan_announcement, one_to_two_announcement, two_to_one_announcement));
341335
} else {
342-
return None;
336+
// TODO: We may end up sending un-announced channel_updates if we are sending
337+
// initial sync data while receiving announce/updates for this channel.
343338
}
344339
}
340+
None
345341
}
346342

347343
fn get_next_node_announcement(&self, starting_point: Option<&PublicKey>) -> Option<NodeAnnouncement> {
348344
let nodes = self.network_graph.nodes.read().unwrap();
349-
let mut iter = if let Some(pubkey) = starting_point {
350-
let mut iter = nodes.range(NodeId::from_pubkey(pubkey)..);
351-
iter.next();
352-
iter
345+
let iter = if let Some(pubkey) = starting_point {
346+
nodes.range((Bound::Excluded(NodeId::from_pubkey(pubkey)), Bound::Unbounded))
353347
} else {
354-
nodes.range::<NodeId, _>(..)
348+
nodes.range(..)
355349
};
356-
loop {
357-
if let Some((_, ref node)) = iter.next() {
358-
if let Some(node_info) = node.announcement_info.as_ref() {
359-
if let Some(msg) = node_info.announcement_message.clone() {
360-
return Some(msg);
361-
}
350+
for (_, ref node) in iter {
351+
if let Some(node_info) = node.announcement_info.as_ref() {
352+
if let Some(msg) = node_info.announcement_message.clone() {
353+
return Some(msg);
362354
}
363-
} else {
364-
return None;
365355
}
366356
}
357+
None
367358
}
368359

369360
/// Initiates a stateless sync of routing gossip information with a peer

0 commit comments

Comments
 (0)