Skip to content

Commit afd4282

Browse files
committed
Move node announcement fields into a separate struct
1 parent a7e1932 commit afd4282

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
@@ -162,8 +167,10 @@ impl RoutingMessageHandler for NetGraphMsgHandler {
162167
};
163168
while result.len() < batch_amount as usize {
164169
if let Some((_, ref node)) = iter.next() {
165-
if node.announcement_message.is_some() {
166-
result.push(node.announcement_message.clone().unwrap());
170+
if let Some(node_info) = node.announcement_info.as_ref() {
171+
if node_info.announcement_message.is_some() {
172+
result.push(node_info.announcement_message.clone().unwrap());
173+
}
167174
}
168175
} else {
169176
return result;
@@ -325,47 +332,27 @@ impl Writeable for RoutingFees {
325332
}
326333
}
327334

328-
329-
#[derive(PartialEq)]
330-
/// Details regarding a node in the network
331-
pub struct NodeInfo {
332-
/// All valid channels a node has announced
333-
pub channels: Vec<u64>,
334-
/// Lowest fees enabling routing via any of the known channels to a node
335-
pub lowest_inbound_channel_fees: Option<RoutingFees>,
335+
#[derive(PartialEq, Debug)]
336+
/// Information received in the latest node_announcement from this node.
337+
pub struct NodeAnnouncementInfo {
336338
/// Protocol features the node announced support for
337-
pub features: NodeFeatures,
339+
pub features: NodeFeatures,
338340
/// When the last known update to the node state was issued
339-
/// Unlike for channels, we may have a NodeInfo entry before having received a node_update.
340-
/// Thus, we have to be able to capture "no update has been received", which we do with an
341-
/// Option here.
342-
pub last_update: Option<u32>,
341+
pub last_update: u32,
343342
/// Color assigned to the node
344343
pub rgb: [u8; 3],
345344
/// Moniker assigned to the node
346345
pub alias: [u8; 32],
347346
/// Internet-level addresses via which one can connect to the node
348347
pub addresses: Vec<NetAddress>,
349348
/// An initial announcement of the node
350-
//this is cached here so we can send out it later if required by initial routing sync
351-
//keep an eye on this to see if the extra memory is a problem
352-
pub announcement_message: Option<msgs::NodeAnnouncement>,
349+
// this is cached here so we can send out it later if required by initial routing sync
350+
// keep an eye on this to see if the extra memory is a problem
351+
pub announcement_message: Option<msgs::NodeAnnouncement>
353352
}
354353

355-
impl std::fmt::Display for NodeInfo {
356-
fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
357-
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[..])?;
358-
Ok(())
359-
}
360-
}
361-
362-
impl Writeable for NodeInfo {
354+
impl Writeable for NodeAnnouncementInfo {
363355
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
364-
(self.channels.len() as u64).write(writer)?;
365-
for ref chan in self.channels.iter() {
366-
chan.write(writer)?;
367-
}
368-
self.lowest_inbound_channel_fees.write(writer)?;
369356
self.features.write(writer)?;
370357
self.last_update.write(writer)?;
371358
self.rgb.write(writer)?;
@@ -379,16 +366,8 @@ impl Writeable for NodeInfo {
379366
}
380367
}
381368

382-
const MAX_ALLOC_SIZE: u64 = 64*1024;
383-
384-
impl Readable for NodeInfo {
385-
fn read<R: ::std::io::Read>(reader: &mut R) -> Result<NodeInfo, DecodeError> {
386-
let channels_count: u64 = Readable::read(reader)?;
387-
let mut channels = Vec::with_capacity(cmp::min(channels_count, MAX_ALLOC_SIZE / 8) as usize);
388-
for _ in 0..channels_count {
389-
channels.push(Readable::read(reader)?);
390-
}
391-
let lowest_inbound_channel_fees = Readable::read(reader)?;
369+
impl Readable for NodeAnnouncementInfo {
370+
fn read<R: ::std::io::Read>(reader: &mut R) -> Result<NodeAnnouncementInfo, DecodeError> {
392371
let features = Readable::read(reader)?;
393372
let last_update = Readable::read(reader)?;
394373
let rgb = Readable::read(reader)?;
@@ -404,9 +383,7 @@ impl Readable for NodeInfo {
404383
}
405384
}
406385
let announcement_message = Readable::read(reader)?;
407-
Ok(NodeInfo {
408-
channels,
409-
lowest_inbound_channel_fees,
386+
Ok(NodeAnnouncementInfo {
410387
features,
411388
last_update,
412389
rgb,
@@ -417,6 +394,57 @@ impl Readable for NodeInfo {
417394
}
418395
}
419396

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

504-
node.features = msg.contents.features.clone();
505-
node.last_update = Some(msg.contents.timestamp);
506-
node.rgb = msg.contents.rgb;
507-
node.alias = msg.contents.alias;
508-
node.addresses = msg.contents.addresses.clone();
509-
510531
let should_relay = msg.contents.excess_data.is_empty() && msg.contents.excess_address_data.is_empty();
511-
node.announcement_message = if should_relay { Some(msg.clone()) } else { None };
532+
node.announcement_info = Some(NodeAnnouncementInfo {
533+
features: msg.contents.features.clone(),
534+
last_update: msg.contents.timestamp,
535+
rgb: msg.contents.rgb,
536+
alias: msg.contents.alias,
537+
addresses: msg.contents.addresses.clone(),
538+
announcement_message: if should_relay { Some(msg.clone()) } else { None },
539+
});
540+
512541
Ok(should_relay)
513542
}
514543
}
@@ -593,12 +622,7 @@ impl NetworkGraph {
593622
node_entry.insert(NodeInfo {
594623
channels: vec!(msg.contents.short_channel_id),
595624
lowest_inbound_channel_fees: None,
596-
features: NodeFeatures::empty(),
597-
last_update: None,
598-
rgb: [0; 3],
599-
alias: [0; 32],
600-
addresses: Vec::new(),
601-
announcement_message: None,
625+
announcement_info: None,
602626
});
603627
}
604628
}

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)