Skip to content

Commit a17a159

Browse files
committed
Store the source and destination node_counters in ChannelInfo
In the next commit, we'll use the new `node_counter`s to remove a `HashMap` from the router, using a `Vec` to store all our per-node information. In order to make finding entries in that `Vec` cheap, here we store the source and destintaion `node_counter`s in `ChannelInfo`, givind us the counters for both ends of a channel without doing a second `HashMap` lookup.
1 parent 6d714b8 commit a17a159

File tree

1 file changed

+67
-17
lines changed

1 file changed

+67
-17
lines changed

lightning/src/routing/gossip.rs

Lines changed: 67 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ const MAX_EXCESS_BYTES_FOR_RELAY: usize = 1024;
6464
const MAX_SCIDS_PER_REPLY: usize = 8000;
6565

6666
/// Represents the compressed public key of a node
67-
#[derive(Clone, Copy)]
67+
#[derive(Clone, Copy, PartialEq, Eq)]
6868
pub struct NodeId([u8; PUBLIC_KEY_SIZE]);
6969

7070
impl NodeId {
@@ -116,14 +116,6 @@ impl core::hash::Hash for NodeId {
116116
}
117117
}
118118

119-
impl Eq for NodeId {}
120-
121-
impl PartialEq for NodeId {
122-
fn eq(&self, other: &Self) -> bool {
123-
self.0[..] == other.0[..]
124-
}
125-
}
126-
127119
impl cmp::PartialOrd for NodeId {
128120
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
129121
Some(self.cmp(other))
@@ -885,7 +877,7 @@ impl Readable for ChannelUpdateInfo {
885877
}
886878
}
887879

888-
#[derive(Clone, Debug, PartialEq, Eq)]
880+
#[derive(Clone, Debug, Eq)]
889881
/// Details about a channel (both directions).
890882
/// Received within a channel announcement.
891883
pub struct ChannelInfo {
@@ -910,6 +902,24 @@ pub struct ChannelInfo {
910902
/// (which we can probably assume we are - no-std environments probably won't have a full
911903
/// network graph in memory!).
912904
announcement_received_time: u64,
905+
906+
/// The [`NodeInfo::node_counter`] of the node pointed to by [`Self::node_one`].
907+
pub(crate) node_one_counter: u32,
908+
/// The [`NodeInfo::node_counter`] of the node pointed to by [`Self::node_two`].
909+
pub(crate) node_two_counter: u32,
910+
}
911+
912+
impl PartialEq for ChannelInfo {
913+
fn eq(&self, o: &ChannelInfo) -> bool {
914+
self.features == o.features &&
915+
self.node_one == o.node_one &&
916+
self.one_to_two == o.one_to_two &&
917+
self.node_two == o.node_two &&
918+
self.two_to_one == o.two_to_one &&
919+
self.capacity_sats == o.capacity_sats &&
920+
self.announcement_message == o.announcement_message &&
921+
self.announcement_received_time == o.announcement_received_time
922+
}
913923
}
914924

915925
impl ChannelInfo {
@@ -1030,6 +1040,8 @@ impl Readable for ChannelInfo {
10301040
capacity_sats: _init_tlv_based_struct_field!(capacity_sats, required),
10311041
announcement_message: _init_tlv_based_struct_field!(announcement_message, required),
10321042
announcement_received_time: _init_tlv_based_struct_field!(announcement_received_time, (default_value, 0)),
1043+
node_one_counter: u32::max_value(),
1044+
node_two_counter: u32::max_value(),
10331045
})
10341046
}
10351047
}
@@ -1505,7 +1517,7 @@ impl<L: Deref> ReadableArgs<L> for NetworkGraph<L> where L::Target: Logger {
15051517
let mut channels = IndexedMap::with_capacity(cmp::min(channels_count as usize, 22500));
15061518
for _ in 0..channels_count {
15071519
let chan_id: u64 = Readable::read(reader)?;
1508-
let chan_info = Readable::read(reader)?;
1520+
let chan_info: ChannelInfo = Readable::read(reader)?;
15091521
channels.insert(chan_id, chan_info);
15101522
}
15111523
let nodes_count: u64 = Readable::read(reader)?;
@@ -1521,6 +1533,13 @@ impl<L: Deref> ReadableArgs<L> for NetworkGraph<L> where L::Target: Logger {
15211533
nodes.insert(node_id, node_info);
15221534
}
15231535

1536+
for (_, chan) in channels.unordered_iter_mut() {
1537+
chan.node_one_counter =
1538+
nodes.get(&chan.node_one).ok_or(DecodeError::InvalidValue)?.node_counter;
1539+
chan.node_two_counter =
1540+
nodes.get(&chan.node_two).ok_or(DecodeError::InvalidValue)?.node_counter;
1541+
}
1542+
15241543
let mut last_rapid_gossip_sync_timestamp: Option<u32> = None;
15251544
read_tlv_fields!(reader, {
15261545
(1, last_rapid_gossip_sync_timestamp, option),
@@ -1590,6 +1609,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
15901609

15911610
fn test_node_counter_consistency(&self) {
15921611
#[cfg(debug_assertions)] {
1612+
let channels = self.channels.read().unwrap();
15931613
let nodes = self.nodes.read().unwrap();
15941614
let removed_node_counters = self.removed_node_counters.lock().unwrap();
15951615
let next_counter = self.next_node_counter.load(Ordering::Acquire);
@@ -1609,6 +1629,19 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
16091629
assert_eq!(used_node_counters[pos] & bit, 0);
16101630
used_node_counters[pos] |= bit;
16111631
}
1632+
1633+
for (idx, used_bitset) in used_node_counters.iter().enumerate() {
1634+
if idx != next_counter / 8 {
1635+
assert_eq!(*used_bitset, 0xff);
1636+
} else {
1637+
assert_eq!(*used_bitset, (1u8 << (next_counter % 8)) - 1);
1638+
}
1639+
}
1640+
1641+
for (_, chan) in channels.unordered_iter() {
1642+
assert_eq!(chan.node_one_counter, nodes.get(&chan.node_one).unwrap().node_counter);
1643+
assert_eq!(chan.node_two_counter, nodes.get(&chan.node_two).unwrap().node_counter);
1644+
}
16121645
}
16131646
}
16141647

@@ -1773,6 +1806,8 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
17731806
capacity_sats: None,
17741807
announcement_message: None,
17751808
announcement_received_time: timestamp,
1809+
node_one_counter: u32::max_value(),
1810+
node_two_counter: u32::max_value(),
17761811
};
17771812

17781813
self.add_channel_between_nodes(short_channel_id, channel_info, None)
@@ -1787,7 +1822,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
17871822

17881823
log_gossip!(self.logger, "Adding channel {} between nodes {} and {}", short_channel_id, node_id_a, node_id_b);
17891824

1790-
match channels.entry(short_channel_id) {
1825+
let channel_info = match channels.entry(short_channel_id) {
17911826
IndexedMapEntry::Occupied(mut entry) => {
17921827
//TODO: because asking the blockchain if short_channel_id is valid is only optional
17931828
//in the blockchain API, we need to handle it smartly here, though it's unclear
@@ -1803,28 +1838,35 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
18031838
// c) it's unclear how to do so without exposing ourselves to massive DoS risk.
18041839
self.remove_channel_in_nodes(&mut nodes, &entry.get(), short_channel_id);
18051840
*entry.get_mut() = channel_info;
1841+
entry.into_mut()
18061842
} else {
18071843
return Err(LightningError{err: "Already have knowledge of channel".to_owned(), action: ErrorAction::IgnoreDuplicateGossip});
18081844
}
18091845
},
18101846
IndexedMapEntry::Vacant(entry) => {
1811-
entry.insert(channel_info);
1847+
entry.insert(channel_info)
18121848
}
18131849
};
18141850

1815-
for current_node_id in [node_id_a, node_id_b].iter() {
1851+
let mut node_counter_id = [
1852+
(&mut channel_info.node_one_counter, node_id_a),
1853+
(&mut channel_info.node_two_counter, node_id_b)
1854+
];
1855+
for (node_counter, current_node_id) in node_counter_id.iter_mut() {
18161856
match nodes.entry(current_node_id.clone()) {
18171857
IndexedMapEntry::Occupied(node_entry) => {
1818-
node_entry.into_mut().channels.push(short_channel_id);
1858+
let node = node_entry.into_mut();
1859+
node.channels.push(short_channel_id);
1860+
**node_counter = node.node_counter;
18191861
},
18201862
IndexedMapEntry::Vacant(node_entry) => {
18211863
let mut removed_node_counters = self.removed_node_counters.lock().unwrap();
1822-
let node_counter = removed_node_counters.pop()
1864+
**node_counter = removed_node_counters.pop()
18231865
.unwrap_or(self.next_node_counter.fetch_add(1, Ordering::Relaxed) as u32);
18241866
node_entry.insert(NodeInfo {
18251867
channels: vec!(short_channel_id),
18261868
announcement_info: None,
1827-
node_counter,
1869+
node_counter: **node_counter,
18281870
});
18291871
}
18301872
};
@@ -1915,6 +1957,8 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
19151957
announcement_message: if msg.excess_data.len() <= MAX_EXCESS_BYTES_FOR_RELAY
19161958
{ full_msg.cloned() } else { None },
19171959
announcement_received_time,
1960+
node_one_counter: u32::max_value(),
1961+
node_two_counter: u32::max_value(),
19181962
};
19191963

19201964
self.add_channel_between_nodes(msg.short_channel_id, chan_info, utxo_value)?;
@@ -1976,6 +2020,8 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
19762020
}
19772021
}
19782022
removed_channels.insert(*scid, current_time_unix);
2023+
} else {
2024+
debug_assert!(false, "Channels in nodes must always have channel info");
19792025
}
19802026
}
19812027
removed_node_counters.push(node.node_counter);
@@ -3595,6 +3641,8 @@ pub(crate) mod tests {
35953641
capacity_sats: None,
35963642
announcement_message: None,
35973643
announcement_received_time: 87654,
3644+
node_one_counter: 0,
3645+
node_two_counter: 1,
35983646
};
35993647

36003648
let mut encoded_chan_info: Vec<u8> = Vec::new();
@@ -3613,6 +3661,8 @@ pub(crate) mod tests {
36133661
capacity_sats: None,
36143662
announcement_message: None,
36153663
announcement_received_time: 87654,
3664+
node_one_counter: 0,
3665+
node_two_counter: 1,
36163666
};
36173667

36183668
let mut encoded_chan_info: Vec<u8> = Vec::new();

0 commit comments

Comments
 (0)