Skip to content

Commit

Permalink
Reintroduce addresses to NodeAnnouncementInfo.
Browse files Browse the repository at this point in the history
  • Loading branch information
arik-so committed May 21, 2024
1 parent 1d452d0 commit cb08eb2
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions lightning/src/routing/gossip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,7 @@ pub struct ChannelInfo {
/// The timestamp when we received the announcement, if we are running with feature = "std"
/// (which we can probably assume we are - no-std environments probably won't have a full
/// network graph in memory!).
announcement_received_time: u64,
pub announcement_received_time: u64,
}

impl ChannelInfo {
Expand Down Expand Up @@ -1143,32 +1143,37 @@ pub struct NodeAnnouncementInfo {
/// May be invalid or malicious (eg control chars),
/// should not be exposed to the user.
pub alias: NodeAlias,

/// Internet-level addresses via which one can connect to the node
pub addresses: Vec<SocketAddress>,

/// List of addresses on which this node is reachable
// pub addresses: Vec<SocketAddress>,
/// An initial announcement of the node
/// Mostly redundant with the data we store in fields explicitly.
/// Everything else is useful only for sending out for initial routing sync.
/// Not stored if contains excess data to prevent DoS.
pub announcement_message: Option<NodeAnnouncement>
pub announcement_message: Option<NodeAnnouncement>,
}

impl NodeAnnouncementInfo {
/// Internet-level addresses via which one can connect to the node
pub fn addresses(&self) -> &[SocketAddress] {
self.announcement_message.as_ref()
.map(|msg| msg.contents.addresses.as_slice())
.unwrap_or_default()
.unwrap_or(self.addresses.as_slice())
}
}

impl Writeable for NodeAnnouncementInfo {
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
let empty_addresses = Vec::<SocketAddress>::new();
write_tlv_fields!(writer, {
(0, self.features, required),
(2, self.last_update, required),
(4, self.rgb, required),
(6, self.alias, required),
(8, self.announcement_message, option),
(10, empty_addresses, required_vec), // Versions prior to 0.0.115 require this field
(10, self.addresses, required_vec), // Versions prior to 0.0.115 require this field
});
Ok(())
}
Expand All @@ -1182,11 +1187,16 @@ impl Readable for NodeAnnouncementInfo {
(4, rgb, required),
(6, alias, required),
(8, announcement_message, option),
(10, _addresses, optional_vec), // deprecated, not used anymore
(10, addresses, optional_vec),
});
let _: Option<Vec<SocketAddress>> = _addresses;
Ok(Self { features: features.0.unwrap(), last_update: last_update.0.unwrap(), rgb: rgb.0.unwrap(),
alias: alias.0.unwrap(), announcement_message })
Ok(Self {
features: features.0.unwrap(),
last_update: last_update.0.unwrap(),
rgb: rgb.0.unwrap(),
alias: alias.0.unwrap(),
addresses: addresses.unwrap_or(Vec::new()),
announcement_message,
})
}
}

Expand Down Expand Up @@ -1504,6 +1514,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
last_update: msg.timestamp,
rgb: msg.rgb,
alias: msg.alias,
addresses: msg.addresses.clone(),
announcement_message: if should_relay { full_msg.cloned() } else { None },
});

Expand Down Expand Up @@ -3453,6 +3464,7 @@ pub(crate) mod tests {
last_update: 0,
rgb: [0u8; 3],
alias: NodeAlias([0u8; 32]),
addresses: announcement_message.contents.addresses.clone(),
announcement_message: Some(announcement_message)
};

Expand Down Expand Up @@ -3487,8 +3499,8 @@ pub(crate) mod tests {
let old_ann_info_with_addresses = <Vec<u8>>::from_hex("3f0009000708a000080a51220204000000000403000000062000000000000000000000000000000000000000000000000000000000000000000a0505014104d2").unwrap();
let ann_info_with_addresses = NodeAnnouncementInfo::read(&mut old_ann_info_with_addresses.as_slice())
.expect("to be able to read an old NodeAnnouncementInfo with addresses");
// This serialized info has an address field but no announcement_message, therefore the addresses returned by our function will still be empty
assert!(ann_info_with_addresses.addresses().is_empty());
// This serialized info has no announcement_message but its address field should still be considered
assert!(!ann_info_with_addresses.addresses().is_empty());
}

#[test]
Expand Down

0 comments on commit cb08eb2

Please sign in to comment.