Ok(msg.contents.excess_data.len() <= MAX_EXCESS_BYTES_FOR_RELAY)
}
- fn get_next_channel_announcements(&self, starting_point: u64, batch_amount: u8) -> Vec<(ChannelAnnouncement, Option<ChannelUpdate>, Option<ChannelUpdate>)> {
- let mut result = Vec::with_capacity(batch_amount as usize);
+ fn get_next_channel_announcement(&self, starting_point: u64) -> Option<(ChannelAnnouncement, Option<ChannelUpdate>, Option<ChannelUpdate>)> {
let channels = self.network_graph.channels.read().unwrap();
let mut iter = channels.range(starting_point..);
- while result.len() < batch_amount as usize {
+ loop {
if let Some((_, ref chan)) = iter.next() {
if chan.announcement_message.is_some() {
let chan_announcement = chan.announcement_message.clone().unwrap();
if let Some(two_to_one) = chan.two_to_one.as_ref() {
two_to_one_announcement = two_to_one.last_update_message.clone();
}
- result.push((chan_announcement, one_to_two_announcement, two_to_one_announcement));
+ return Some((chan_announcement, one_to_two_announcement, two_to_one_announcement));
} else {
// TODO: We may end up sending un-announced channel_updates if we are sending
// initial sync data while receiving announce/updates for this channel.
}
} else {
- return result;
+ return None;
}
}
- result
}
- fn get_next_node_announcements(&self, starting_point: Option<&PublicKey>, batch_amount: u8) -> Vec<NodeAnnouncement> {
- let mut result = Vec::with_capacity(batch_amount as usize);
+ fn get_next_node_announcement(&self, starting_point: Option<&PublicKey>) -> Option<NodeAnnouncement> {
let nodes = self.network_graph.nodes.read().unwrap();
let mut iter = if let Some(pubkey) = starting_point {
let mut iter = nodes.range(NodeId::from_pubkey(pubkey)..);
} else {
nodes.range::<NodeId, _>(..)
};
- while result.len() < batch_amount as usize {
+ loop {
if let Some((_, ref node)) = iter.next() {
if let Some(node_info) = node.announcement_info.as_ref() {
- if node_info.announcement_message.is_some() {
- result.push(node_info.announcement_message.clone().unwrap());
+ if let Some(msg) = node_info.announcement_message.clone() {
+ return Some(msg);
}
}
} else {
- return result;
+ return None;
}
}
- result
}
/// Initiates a stateless sync of routing gossip information with a peer
self.channels.get(&short_channel_id)
}
+ #[cfg(c_bindings)] // Non-bindings users should use `channels`
+ /// Returns the list of channels in the graph
+ pub fn list_channels(&self) -> Vec<u64> {
+ self.channels.keys().map(|c| *c).collect()
+ }
+
/// Returns all known nodes' public keys along with announced node info.
///
/// (C-not exported) because we have no mapping for `BTreeMap`s
self.nodes.get(node_id)
}
+ #[cfg(c_bindings)] // Non-bindings users should use `nodes`
+ /// Returns the list of nodes in the graph
+ pub fn list_nodes(&self) -> Vec<NodeId> {
+ self.nodes.keys().map(|n| *n).collect()
+ }
+
/// Get network addresses by node id.
/// Returns None if the requested node is completely unknown,
/// or if node announcement for the node was never received.
let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap();
// Channels were not announced yet.
- let channels_with_announcements = gossip_sync.get_next_channel_announcements(0, 1);
- assert_eq!(channels_with_announcements.len(), 0);
+ let channels_with_announcements = gossip_sync.get_next_channel_announcement(0);
+ assert!(channels_with_announcements.is_none());
let short_channel_id;
{
}
// Contains initial channel announcement now.
- let channels_with_announcements = gossip_sync.get_next_channel_announcements(short_channel_id, 1);
- assert_eq!(channels_with_announcements.len(), 1);
- if let Some(channel_announcements) = channels_with_announcements.first() {
- let &(_, ref update_1, ref update_2) = channel_announcements;
+ let channels_with_announcements = gossip_sync.get_next_channel_announcement(short_channel_id);
+ if let Some(channel_announcements) = channels_with_announcements {
+ let (_, ref update_1, ref update_2) = channel_announcements;
assert_eq!(update_1, &None);
assert_eq!(update_2, &None);
} else {
panic!();
}
-
{
// Valid channel update
let valid_channel_update = get_signed_channel_update(|unsigned_channel_update| {
}
// Now contains an initial announcement and an update.
- let channels_with_announcements = gossip_sync.get_next_channel_announcements(short_channel_id, 1);
- assert_eq!(channels_with_announcements.len(), 1);
- if let Some(channel_announcements) = channels_with_announcements.first() {
- let &(_, ref update_1, ref update_2) = channel_announcements;
+ let channels_with_announcements = gossip_sync.get_next_channel_announcement(short_channel_id);
+ if let Some(channel_announcements) = channels_with_announcements {
+ let (_, ref update_1, ref update_2) = channel_announcements;
assert_ne!(update_1, &None);
assert_eq!(update_2, &None);
} else {
}
// Test that announcements with excess data won't be returned
- let channels_with_announcements = gossip_sync.get_next_channel_announcements(short_channel_id, 1);
- assert_eq!(channels_with_announcements.len(), 1);
- if let Some(channel_announcements) = channels_with_announcements.first() {
- let &(_, ref update_1, ref update_2) = channel_announcements;
+ let channels_with_announcements = gossip_sync.get_next_channel_announcement(short_channel_id);
+ if let Some(channel_announcements) = channels_with_announcements {
+ let (_, ref update_1, ref update_2) = channel_announcements;
assert_eq!(update_1, &None);
assert_eq!(update_2, &None);
} else {
}
// Further starting point have no channels after it
- let channels_with_announcements = gossip_sync.get_next_channel_announcements(short_channel_id + 1000, 1);
- assert_eq!(channels_with_announcements.len(), 0);
+ let channels_with_announcements = gossip_sync.get_next_channel_announcement(short_channel_id + 1000);
+ assert!(channels_with_announcements.is_none());
}
#[test]
let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_privkey);
// No nodes yet.
- let next_announcements = gossip_sync.get_next_node_announcements(None, 10);
- assert_eq!(next_announcements.len(), 0);
+ let next_announcements = gossip_sync.get_next_node_announcement(None);
+ assert!(next_announcements.is_none());
{
// Announce a channel to add 2 nodes
};
}
-
// Nodes were never announced
- let next_announcements = gossip_sync.get_next_node_announcements(None, 3);
- assert_eq!(next_announcements.len(), 0);
+ let next_announcements = gossip_sync.get_next_node_announcement(None);
+ assert!(next_announcements.is_none());
{
let valid_announcement = get_signed_node_announcement(|_| {}, node_1_privkey, &secp_ctx);
};
}
- let next_announcements = gossip_sync.get_next_node_announcements(None, 3);
- assert_eq!(next_announcements.len(), 2);
+ let next_announcements = gossip_sync.get_next_node_announcement(None);
+ assert!(next_announcements.is_some());
// Skip the first node.
- let next_announcements = gossip_sync.get_next_node_announcements(Some(&node_id_1), 2);
- assert_eq!(next_announcements.len(), 1);
+ let next_announcements = gossip_sync.get_next_node_announcement(Some(&node_id_1));
+ assert!(next_announcements.is_some());
{
// Later announcement which should not be relayed (excess data) prevent us from sharing a node
};
}
- let next_announcements = gossip_sync.get_next_node_announcements(Some(&node_id_1), 2);
- assert_eq!(next_announcements.len(), 0);
+ let next_announcements = gossip_sync.get_next_node_announcement(Some(&node_id_1));
+ assert!(next_announcements.is_none());
}
#[test]