Skip to content

Commit 77a2aee

Browse files
committed
Move node announcement fields into a separate struct
1 parent 4c79f1e commit 77a2aee

File tree

3 files changed

+100
-65
lines changed

3 files changed

+100
-65
lines changed

lightning/src/ln/msgs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ pub struct UnsignedNodeAnnouncement {
391391
pub(crate) excess_address_data: Vec<u8>,
392392
pub(crate) excess_data: Vec<u8>,
393393
}
394-
#[derive(PartialEq, Clone)]
394+
#[derive(PartialEq, Clone, Debug)]
395395
/// A node_announcement message to be sent or received from a peer
396396
pub struct NodeAnnouncement {
397397
pub(crate) signature: Signature,

lightning/src/routing/network_graph.rs

Lines changed: 86 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,12 @@ impl NetGraphMsgHandler {
5151
/// Get network addresses by node id
5252
pub fn get_addresses(&self, pubkey: &PublicKey) -> Option<Vec<NetAddress>> {
5353
let network = self.network_graph.read().unwrap();
54-
network.get_nodes().get(pubkey).map(|n| n.addresses.clone())
54+
if let Some(node) = network.get_nodes().get(pubkey) {
55+
if let Some(node_info) = node.announcement_info.as_ref() {
56+
return Some(node_info.addresses.clone())
57+
}
58+
}
59+
None
5560
}
5661

5762
/// Dumps the entire network view of this NetGraphMsgHandler to the logger provided in the constructor at
@@ -170,8 +175,10 @@ impl RoutingMessageHandler for NetGraphMsgHandler {
170175
};
171176
while result.len() < batch_amount as usize {
172177
if let Some((_, ref node)) = iter.next() {
173-
if node.announcement_message.is_some() {
174-
result.push(node.announcement_message.clone().unwrap());
178+
if let Some(node_info) = node.announcement_info.as_ref() {
179+
if node_info.announcement_message.is_some() {
180+
result.push(node_info.announcement_message.clone().unwrap());
181+
}
175182
}
176183
} else {
177184
return result;
@@ -333,47 +340,27 @@ impl Writeable for RoutingFees {
333340
}
334341
}
335342

336-
337-
#[derive(PartialEq)]
338-
/// Details regarding a node in the network
339-
pub struct NodeInfo {
340-
/// All valid channels a node has announced
341-
pub channels: Vec<u64>,
342-
/// Lowest fees enabling routing via any of the known channels to a node
343-
pub lowest_inbound_channel_fees: Option<RoutingFees>,
343+
#[derive(PartialEq, Debug)]
344+
/// Information received in the latest node_announcement from this node.
345+
pub struct NodeAnnouncementInfo {
344346
/// Protocol features the node announced support for
345-
pub features: NodeFeatures,
347+
pub features: NodeFeatures,
346348
/// When the last known update to the node state was issued
347-
/// Unlike for channels, we may have a NodeInfo entry before having received a node_update.
348-
/// Thus, we have to be able to capture "no update has been received", which we do with an
349-
/// Option here.
350-
pub last_update: Option<u32>,
349+
pub last_update: u32,
351350
/// Color assigned to the node
352351
pub rgb: [u8; 3],
353352
/// Moniker assigned to the node
354353
pub alias: [u8; 32],
355354
/// Internet-level addresses via which one can connect to the node
356355
pub addresses: Vec<NetAddress>,
357356
/// An initial announcement of the node
358-
//this is cached here so we can send out it later if required by initial routing sync
359-
//keep an eye on this to see if the extra memory is a problem
360-
pub announcement_message: Option<msgs::NodeAnnouncement>,
357+
// this is cached here so we can send out it later if required by initial routing sync
358+
// keep an eye on this to see if the extra memory is a problem
359+
pub announcement_message: Option<msgs::NodeAnnouncement>
361360
}
362361

363-
impl std::fmt::Display for NodeInfo {
364-
fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
365-
write!(f, "features: {}, last_update: {:?}, lowest_inbound_channel_fees: {:?}, channels: {:?}", log_bytes!(self.features.encode()), self.last_update, self.lowest_inbound_channel_fees, &self.channels[..])?;
366-
Ok(())
367-
}
368-
}
369-
370-
impl Writeable for NodeInfo {
362+
impl Writeable for NodeAnnouncementInfo {
371363
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
372-
(self.channels.len() as u64).write(writer)?;
373-
for ref chan in self.channels.iter() {
374-
chan.write(writer)?;
375-
}
376-
self.lowest_inbound_channel_fees.write(writer)?;
377364
self.features.write(writer)?;
378365
self.last_update.write(writer)?;
379366
self.rgb.write(writer)?;
@@ -387,16 +374,8 @@ impl Writeable for NodeInfo {
387374
}
388375
}
389376

390-
const MAX_ALLOC_SIZE: u64 = 64*1024;
391-
392-
impl Readable for NodeInfo {
393-
fn read<R: ::std::io::Read>(reader: &mut R) -> Result<NodeInfo, DecodeError> {
394-
let channels_count: u64 = Readable::read(reader)?;
395-
let mut channels = Vec::with_capacity(cmp::min(channels_count, MAX_ALLOC_SIZE / 8) as usize);
396-
for _ in 0..channels_count {
397-
channels.push(Readable::read(reader)?);
398-
}
399-
let lowest_inbound_channel_fees = Readable::read(reader)?;
377+
impl Readable for NodeAnnouncementInfo {
378+
fn read<R: ::std::io::Read>(reader: &mut R) -> Result<NodeAnnouncementInfo, DecodeError> {
400379
let features = Readable::read(reader)?;
401380
let last_update = Readable::read(reader)?;
402381
let rgb = Readable::read(reader)?;
@@ -412,9 +391,7 @@ impl Readable for NodeInfo {
412391
}
413392
}
414393
let announcement_message = Readable::read(reader)?;
415-
Ok(NodeInfo {
416-
channels,
417-
lowest_inbound_channel_fees,
394+
Ok(NodeAnnouncementInfo {
418395
features,
419396
last_update,
420397
rgb,
@@ -425,6 +402,57 @@ impl Readable for NodeInfo {
425402
}
426403
}
427404

405+
#[derive(PartialEq)]
406+
/// Details regarding a node in the network
407+
pub struct NodeInfo {
408+
/// All valid channels a node has announced
409+
pub channels: Vec<u64>,
410+
/// Lowest fees enabling routing via any of the known channels to a node
411+
pub lowest_inbound_channel_fees: Option<RoutingFees>,
412+
/// More information about a node from node_announcement
413+
/// Optional because we may have a NodeInfo entry before having received the announcement
414+
pub announcement_info: Option<NodeAnnouncementInfo>
415+
}
416+
417+
impl std::fmt::Display for NodeInfo {
418+
fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
419+
write!(f, "lowest_inbound_channel_fees: {:?}, channels: {:?}, announcement_info: {:?}",
420+
self.lowest_inbound_channel_fees, &self.channels[..], self.announcement_info)?;
421+
Ok(())
422+
}
423+
}
424+
425+
impl Writeable for NodeInfo {
426+
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
427+
(self.channels.len() as u64).write(writer)?;
428+
for ref chan in self.channels.iter() {
429+
chan.write(writer)?;
430+
}
431+
self.lowest_inbound_channel_fees.write(writer)?;
432+
self.announcement_info.write(writer)?;
433+
Ok(())
434+
}
435+
}
436+
437+
const MAX_ALLOC_SIZE: u64 = 64*1024;
438+
439+
impl Readable for NodeInfo {
440+
fn read<R: ::std::io::Read>(reader: &mut R) -> Result<NodeInfo, DecodeError> {
441+
let channels_count: u64 = Readable::read(reader)?;
442+
let mut channels = Vec::with_capacity(cmp::min(channels_count, MAX_ALLOC_SIZE / 8) as usize);
443+
for _ in 0..channels_count {
444+
channels.push(Readable::read(reader)?);
445+
}
446+
let lowest_inbound_channel_fees = Readable::read(reader)?;
447+
let announcement_info = Readable::read(reader)?;
448+
Ok(NodeInfo {
449+
channels,
450+
lowest_inbound_channel_fees,
451+
announcement_info,
452+
})
453+
}
454+
}
455+
428456
/// Represents the network as nodes and channels between them
429457
#[derive(PartialEq)]
430458
pub struct NetworkGraph {
@@ -497,21 +525,22 @@ impl NetworkGraph {
497525
match self.nodes.get_mut(&msg.contents.node_id) {
498526
None => Err(LightningError{err: "No existing channels for node_announcement", action: ErrorAction::IgnoreError}),
499527
Some(node) => {
500-
match node.last_update {
501-
Some(last_update) => if last_update >= msg.contents.timestamp {
528+
if let Some(node_info) = node.announcement_info.as_ref() {
529+
if node_info.last_update >= msg.contents.timestamp {
502530
return Err(LightningError{err: "Update older than last processed update", action: ErrorAction::IgnoreError});
503-
},
504-
None => {},
531+
}
505532
}
506533

507-
node.features = msg.contents.features.clone();
508-
node.last_update = Some(msg.contents.timestamp);
509-
node.rgb = msg.contents.rgb;
510-
node.alias = msg.contents.alias;
511-
node.addresses = msg.contents.addresses.clone();
512-
513534
let should_relay = msg.contents.excess_data.is_empty() && msg.contents.excess_address_data.is_empty();
514-
node.announcement_message = if should_relay { Some(msg.clone()) } else { None };
535+
node.announcement_info = Some(NodeAnnouncementInfo {
536+
features: msg.contents.features.clone(),
537+
last_update: msg.contents.timestamp,
538+
rgb: msg.contents.rgb,
539+
alias: msg.contents.alias,
540+
addresses: msg.contents.addresses.clone(),
541+
announcement_message: if should_relay { Some(msg.clone()) } else { None },
542+
});
543+
515544
Ok(should_relay)
516545
}
517546
}
@@ -588,12 +617,7 @@ impl NetworkGraph {
588617
node_entry.insert(NodeInfo {
589618
channels: vec!(msg.contents.short_channel_id),
590619
lowest_inbound_channel_fees: None,
591-
features: NodeFeatures::empty(),
592-
last_update: None,
593-
rgb: [0; 3],
594-
alias: [0; 32],
595-
addresses: Vec::new(),
596-
announcement_message: None,
620+
announcement_info: None,
597621
});
598622
}
599623
}

lightning/src/routing/router.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,14 @@ pub fn get_route(our_node_id: &PublicKey, net_graph_msg_handler: &NetGraphMsgHan
290290
}
291291
}
292292

293-
if !$node.features.requires_unknown_bits() {
293+
let features;
294+
if let Some(node_info) = $node.announcement_info.as_ref() {
295+
features = node_info.features.clone();
296+
} else {
297+
features = NodeFeatures::empty();
298+
}
299+
300+
if !features.requires_unknown_bits() {
294301
for chan_id in $node.channels.iter() {
295302
let chan = network.get_channels().get(chan_id).unwrap();
296303
if !chan.features.requires_unknown_bits() {
@@ -347,7 +354,11 @@ pub fn get_route(our_node_id: &PublicKey, net_graph_msg_handler: &NetGraphMsgHan
347354
if let Some(&(_, ref features)) = first_hop_targets.get(&res.last().unwrap().pubkey) {
348355
res.last_mut().unwrap().node_features = features.to_context();
349356
} else if let Some(node) = network.get_nodes().get(&res.last().unwrap().pubkey) {
350-
res.last_mut().unwrap().node_features = node.features.clone();
357+
if let Some(node_info) = node.announcement_info.as_ref() {
358+
res.last_mut().unwrap().node_features = node_info.features.clone();
359+
} else {
360+
res.last_mut().unwrap().node_features = NodeFeatures::empty();
361+
}
351362
} else {
352363
// We should be able to fill in features for everything except the last
353364
// hop, if the last hop was provided via a BOLT 11 invoice (though we

0 commit comments

Comments
 (0)