Skip to content

Commit 895db9e

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 c9dc38f commit 895db9e

File tree

1 file changed

+58
-17
lines changed

1 file changed

+58
-17
lines changed

lightning/src/routing/gossip.rs

Lines changed: 58 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))
@@ -850,7 +842,7 @@ impl Readable for ChannelUpdateInfo {
850842
}
851843
}
852844

853-
#[derive(Clone, Debug, PartialEq, Eq)]
845+
#[derive(Clone, Debug, Eq)]
854846
/// Details about a channel (both directions).
855847
/// Received within a channel announcement.
856848
pub struct ChannelInfo {
@@ -875,6 +867,24 @@ pub struct ChannelInfo {
875867
/// (which we can probably assume we are - no-std environments probably won't have a full
876868
/// network graph in memory!).
877869
announcement_received_time: u64,
870+
871+
/// The [`NodeInfo::node_counter`] of the node pointed to by [`Self::node_one`].
872+
pub(crate) node_one_counter: u32,
873+
/// The [`NodeInfo::node_counter`] of the node pointed to by [`Self::node_two`].
874+
pub(crate) node_two_counter: u32,
875+
}
876+
877+
impl PartialEq for ChannelInfo {
878+
fn eq(&self, o: &ChannelInfo) -> bool {
879+
self.features == o.features &&
880+
self.node_one == o.node_one &&
881+
self.one_to_two == o.one_to_two &&
882+
self.node_two == o.node_two &&
883+
self.two_to_one == o.two_to_one &&
884+
self.capacity_sats == o.capacity_sats &&
885+
self.announcement_message == o.announcement_message &&
886+
self.announcement_received_time == o.announcement_received_time
887+
}
878888
}
879889

880890
impl ChannelInfo {
@@ -995,6 +1005,8 @@ impl Readable for ChannelInfo {
9951005
capacity_sats: _init_tlv_based_struct_field!(capacity_sats, required),
9961006
announcement_message: _init_tlv_based_struct_field!(announcement_message, required),
9971007
announcement_received_time: _init_tlv_based_struct_field!(announcement_received_time, (default_value, 0)),
1008+
node_one_counter: u32::max_value(),
1009+
node_two_counter: u32::max_value(),
9981010
})
9991011
}
10001012
}
@@ -1470,7 +1482,7 @@ impl<L: Deref> ReadableArgs<L> for NetworkGraph<L> where L::Target: Logger {
14701482
let mut channels = IndexedMap::with_capacity(cmp::min(channels_count as usize, 22500));
14711483
for _ in 0..channels_count {
14721484
let chan_id: u64 = Readable::read(reader)?;
1473-
let chan_info = Readable::read(reader)?;
1485+
let chan_info: ChannelInfo = Readable::read(reader)?;
14741486
channels.insert(chan_id, chan_info);
14751487
}
14761488
let nodes_count: u64 = Readable::read(reader)?;
@@ -1486,6 +1498,13 @@ impl<L: Deref> ReadableArgs<L> for NetworkGraph<L> where L::Target: Logger {
14861498
nodes.insert(node_id, node_info);
14871499
}
14881500

1501+
for (_, chan) in channels.unordered_iter_mut() {
1502+
chan.node_one_counter =
1503+
nodes.get(&chan.node_one).ok_or(DecodeError::InvalidValue)?.node_counter;
1504+
chan.node_two_counter =
1505+
nodes.get(&chan.node_two).ok_or(DecodeError::InvalidValue)?.node_counter;
1506+
}
1507+
14891508
let mut last_rapid_gossip_sync_timestamp: Option<u32> = None;
14901509
read_tlv_fields!(reader, {
14911510
(1, last_rapid_gossip_sync_timestamp, option),
@@ -1555,6 +1574,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
15551574

15561575
fn test_node_counter_consistency(&self) {
15571576
#[cfg(debug_assertions)] {
1577+
let channels = self.channels.read().unwrap();
15581578
let nodes = self.nodes.read().unwrap();
15591579
let removed_node_counters = self.removed_node_counters.lock().unwrap();
15601580
let next_counter = self.next_node_counter.load(Ordering::Acquire);
@@ -1574,6 +1594,11 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
15741594
assert_eq!(used_node_counters[pos] & bit, 0);
15751595
used_node_counters[pos] |= bit;
15761596
}
1597+
1598+
for (_, chan) in channels.unordered_iter() {
1599+
assert_eq!(chan.node_one_counter, nodes.get(&chan.node_one).unwrap().node_counter);
1600+
assert_eq!(chan.node_two_counter, nodes.get(&chan.node_two).unwrap().node_counter);
1601+
}
15771602
}
15781603
}
15791604

@@ -1738,6 +1763,8 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
17381763
capacity_sats: None,
17391764
announcement_message: None,
17401765
announcement_received_time: timestamp,
1766+
node_one_counter: u32::max_value(),
1767+
node_two_counter: u32::max_value(),
17411768
};
17421769

17431770
self.add_channel_between_nodes(short_channel_id, channel_info, None)
@@ -1752,7 +1779,8 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
17521779

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

1755-
match channels.entry(short_channel_id) {
1782+
let channel_entry = channels.entry(short_channel_id);
1783+
let channel_info = match channel_entry {
17561784
IndexedMapEntry::Occupied(mut entry) => {
17571785
//TODO: because asking the blockchain if short_channel_id is valid is only optional
17581786
//in the blockchain API, we need to handle it smartly here, though it's unclear
@@ -1768,28 +1796,35 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
17681796
// c) it's unclear how to do so without exposing ourselves to massive DoS risk.
17691797
self.remove_channel_in_nodes(&mut nodes, &entry.get(), short_channel_id);
17701798
*entry.get_mut() = channel_info;
1799+
entry.into_mut()
17711800
} else {
17721801
return Err(LightningError{err: "Already have knowledge of channel".to_owned(), action: ErrorAction::IgnoreDuplicateGossip});
17731802
}
17741803
},
17751804
IndexedMapEntry::Vacant(entry) => {
1776-
entry.insert(channel_info);
1805+
entry.insert(channel_info)
17771806
}
17781807
};
17791808

1780-
for current_node_id in [node_id_a, node_id_b].iter() {
1809+
let mut node_counter_id = [
1810+
(&mut channel_info.node_one_counter, node_id_a),
1811+
(&mut channel_info.node_two_counter, node_id_b)
1812+
];
1813+
for (node_counter, current_node_id) in node_counter_id.iter_mut() {
17811814
match nodes.entry(current_node_id.clone()) {
17821815
IndexedMapEntry::Occupied(node_entry) => {
1783-
node_entry.into_mut().channels.push(short_channel_id);
1816+
let node = node_entry.into_mut();
1817+
node.channels.push(short_channel_id);
1818+
**node_counter = node.node_counter;
17841819
},
17851820
IndexedMapEntry::Vacant(node_entry) => {
17861821
let mut removed_node_counters = self.removed_node_counters.lock().unwrap();
1787-
let node_counter = removed_node_counters.pop()
1822+
**node_counter = removed_node_counters.pop()
17881823
.unwrap_or(self.next_node_counter.fetch_add(1, Ordering::Relaxed) as u32);
17891824
node_entry.insert(NodeInfo {
17901825
channels: vec!(short_channel_id),
17911826
announcement_info: None,
1792-
node_counter,
1827+
node_counter: **node_counter,
17931828
});
17941829
}
17951830
};
@@ -1880,6 +1915,8 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
18801915
announcement_message: if msg.excess_data.len() <= MAX_EXCESS_BYTES_FOR_RELAY
18811916
{ full_msg.cloned() } else { None },
18821917
announcement_received_time,
1918+
node_one_counter: u32::max_value(),
1919+
node_two_counter: u32::max_value(),
18831920
};
18841921

18851922
self.add_channel_between_nodes(msg.short_channel_id, chan_info, utxo_value)?;
@@ -3565,6 +3602,8 @@ pub(crate) mod tests {
35653602
capacity_sats: None,
35663603
announcement_message: None,
35673604
announcement_received_time: 87654,
3605+
node_one_counter: 0,
3606+
node_two_counter: 1,
35683607
};
35693608

35703609
let mut encoded_chan_info: Vec<u8> = Vec::new();
@@ -3583,6 +3622,8 @@ pub(crate) mod tests {
35833622
capacity_sats: None,
35843623
announcement_message: None,
35853624
announcement_received_time: 87654,
3625+
node_one_counter: 0,
3626+
node_two_counter: 1,
35863627
};
35873628

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

0 commit comments

Comments
 (0)