Skip to content

Commit 3ee0a00

Browse files
committed
Reintroduce addresses to NodeAnnouncementInfo.
1 parent 1d452d0 commit 3ee0a00

File tree

3 files changed

+147
-46
lines changed

3 files changed

+147
-46
lines changed

lightning/src/onion_message/messenger.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,8 +386,11 @@ where
386386
let node_announcement = network_graph
387387
.node(&NodeId::from_pubkey(&first_node))
388388
.and_then(|node_info| node_info.announcement_info.as_ref())
389-
.and_then(|announcement_info| announcement_info.announcement_message.as_ref())
390-
.map(|node_announcement| &node_announcement.contents);
389+
.and_then(|announcement_info| {
390+
announcement_info
391+
.announcement_message()
392+
.map(|node_announcement| &node_announcement.contents)
393+
});
391394

392395
match node_announcement {
393396
Some(node_announcement) if node_announcement.features.supports_onion_messages() => {

lightning/src/routing/gossip.rs

Lines changed: 139 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -504,8 +504,8 @@ where U::Target: UtxoLookup, L::Target: Logger
504504
};
505505
for (_, ref node) in iter {
506506
if let Some(node_info) = node.announcement_info.as_ref() {
507-
if let Some(msg) = node_info.announcement_message.clone() {
508-
return Some(msg);
507+
if let NodeAnnouncementInfo::Relayed(announcement) = node_info {
508+
return Some(announcement.clone());
509509
}
510510
}
511511
}
@@ -1130,45 +1130,136 @@ impl_writeable_tlv_based!(RoutingFees, {
11301130
});
11311131

11321132
#[derive(Clone, Debug, PartialEq, Eq)]
1133-
/// Information received in the latest node_announcement from this node.
1134-
pub struct NodeAnnouncementInfo {
1133+
/// Non-relayable information received in the latest node_announcement from this node.
1134+
pub struct NodeAnnouncementDetails {
11351135
/// Protocol features the node announced support for
11361136
pub features: NodeFeatures,
1137+
11371138
/// When the last known update to the node state was issued.
11381139
/// Value is opaque, as set in the announcement.
11391140
pub last_update: u32,
1141+
11401142
/// Color assigned to the node
11411143
pub rgb: [u8; 3],
1144+
11421145
/// Moniker assigned to the node.
11431146
/// May be invalid or malicious (eg control chars),
11441147
/// should not be exposed to the user.
11451148
pub alias: NodeAlias,
1149+
1150+
/// Internet-level addresses via which one can connect to the node
1151+
pub addresses: Vec<SocketAddress>,
1152+
}
1153+
1154+
#[derive(Clone, Debug, PartialEq, Eq)]
1155+
/// Information received in the latest node_announcement from this node.
1156+
pub enum NodeAnnouncementInfo {
11461157
/// An initial announcement of the node
1147-
/// Mostly redundant with the data we store in fields explicitly.
11481158
/// Everything else is useful only for sending out for initial routing sync.
11491159
/// Not stored if contains excess data to prevent DoS.
1150-
pub announcement_message: Option<NodeAnnouncement>
1160+
Relayed(NodeAnnouncement),
1161+
1162+
/// Non-relayable information received in the latest node_announcement from this node.
1163+
Local(NodeAnnouncementDetails),
11511164
}
11521165

11531166
impl NodeAnnouncementInfo {
1167+
1168+
/// Protocol features the node announced support for
1169+
pub fn features(&self) -> &NodeFeatures {
1170+
match self {
1171+
NodeAnnouncementInfo::Relayed(relayed) => {
1172+
&relayed.contents.features
1173+
}
1174+
NodeAnnouncementInfo::Local(local) => {
1175+
&local.features
1176+
}
1177+
}
1178+
}
1179+
1180+
/// When the last known update to the node state was issued.
1181+
///
1182+
/// Value may or may not be a timestamp, depending on the policy of the origin node.
1183+
pub fn last_update(&self) -> u32 {
1184+
match self {
1185+
NodeAnnouncementInfo::Relayed(relayed) => {
1186+
relayed.contents.timestamp
1187+
}
1188+
NodeAnnouncementInfo::Local(local) => {
1189+
local.last_update
1190+
}
1191+
}
1192+
}
1193+
1194+
/// Color assigned to the node
1195+
pub fn rgb(&self) -> [u8; 3] {
1196+
match self {
1197+
NodeAnnouncementInfo::Relayed(relayed) => {
1198+
relayed.contents.rgb
1199+
}
1200+
NodeAnnouncementInfo::Local(local) => {
1201+
local.rgb
1202+
}
1203+
}
1204+
}
1205+
1206+
/// Moniker assigned to the node.
1207+
///
1208+
/// May be invalid or malicious (eg control chars), should not be exposed to the user.
1209+
pub fn alias(&self) -> &NodeAlias {
1210+
match self {
1211+
NodeAnnouncementInfo::Relayed(relayed) => {
1212+
&relayed.contents.alias
1213+
}
1214+
NodeAnnouncementInfo::Local(local) => {
1215+
&local.alias
1216+
}
1217+
}
1218+
}
1219+
11541220
/// Internet-level addresses via which one can connect to the node
1155-
pub fn addresses(&self) -> &[SocketAddress] {
1156-
self.announcement_message.as_ref()
1157-
.map(|msg| msg.contents.addresses.as_slice())
1158-
.unwrap_or_default()
1221+
pub fn addresses(&self) -> &Vec<SocketAddress> {
1222+
match self {
1223+
NodeAnnouncementInfo::Relayed(relayed) => {
1224+
&relayed.contents.addresses
1225+
}
1226+
NodeAnnouncementInfo::Local(local) => {
1227+
&local.addresses
1228+
}
1229+
}
1230+
}
1231+
1232+
/// An initial announcement of the node
1233+
///
1234+
/// Not stored if contains excess data to prevent DoS.
1235+
pub fn announcement_message(&self) -> Option<&NodeAnnouncement> {
1236+
match self {
1237+
NodeAnnouncementInfo::Relayed(announcement) => {
1238+
Some(announcement)
1239+
}
1240+
NodeAnnouncementInfo::Local(_) => {
1241+
None
1242+
}
1243+
}
11591244
}
11601245
}
11611246

11621247
impl Writeable for NodeAnnouncementInfo {
11631248
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
1164-
let empty_addresses = Vec::<SocketAddress>::new();
1249+
let features = self.features();
1250+
let last_update = self.last_update();
1251+
let rgb = self.rgb();
1252+
let alias = self.alias();
1253+
let addresses = self.addresses();
1254+
let announcement_message = self.announcement_message();
1255+
11651256
write_tlv_fields!(writer, {
1166-
(0, self.features, required),
1167-
(2, self.last_update, required),
1168-
(4, self.rgb, required),
1169-
(6, self.alias, required),
1170-
(8, self.announcement_message, option),
1171-
(10, empty_addresses, required_vec), // Versions prior to 0.0.115 require this field
1257+
(0, features, required),
1258+
(2, last_update, required),
1259+
(4, rgb, required),
1260+
(6, alias, required),
1261+
(8, announcement_message, option),
1262+
(10, *addresses, required_vec), // Versions prior to 0.0.115 require this field
11721263
});
11731264
Ok(())
11741265
}
@@ -1182,11 +1273,19 @@ impl Readable for NodeAnnouncementInfo {
11821273
(4, rgb, required),
11831274
(6, alias, required),
11841275
(8, announcement_message, option),
1185-
(10, _addresses, optional_vec), // deprecated, not used anymore
1276+
(10, addresses, required_vec),
11861277
});
1187-
let _: Option<Vec<SocketAddress>> = _addresses;
1188-
Ok(Self { features: features.0.unwrap(), last_update: last_update.0.unwrap(), rgb: rgb.0.unwrap(),
1189-
alias: alias.0.unwrap(), announcement_message })
1278+
if let Some(announcement) = announcement_message {
1279+
Ok(Self::Relayed(announcement))
1280+
} else {
1281+
Ok(Self::Local(NodeAnnouncementDetails {
1282+
features: features.0.unwrap(),
1283+
last_update: last_update.0.unwrap(),
1284+
rgb: rgb.0.unwrap(),
1285+
alias: alias.0.unwrap(),
1286+
addresses,
1287+
}))
1288+
}
11901289
}
11911290
}
11921291

@@ -1488,24 +1587,29 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
14881587
// The timestamp field is somewhat of a misnomer - the BOLTs use it to order
14891588
// updates to ensure you always have the latest one, only vaguely suggesting
14901589
// that it be at least the current time.
1491-
if node_info.last_update > msg.timestamp {
1590+
if node_info.last_update() > msg.timestamp {
14921591
return Err(LightningError{err: "Update older than last processed update".to_owned(), action: ErrorAction::IgnoreDuplicateGossip});
1493-
} else if node_info.last_update == msg.timestamp {
1592+
} else if node_info.last_update() == msg.timestamp {
14941593
return Err(LightningError{err: "Update had the same timestamp as last processed update".to_owned(), action: ErrorAction::IgnoreDuplicateGossip});
14951594
}
14961595
}
14971596

14981597
let should_relay =
14991598
msg.excess_data.len() <= MAX_EXCESS_BYTES_FOR_RELAY &&
1500-
msg.excess_address_data.len() <= MAX_EXCESS_BYTES_FOR_RELAY &&
1501-
msg.excess_data.len() + msg.excess_address_data.len() <= MAX_EXCESS_BYTES_FOR_RELAY;
1502-
node.announcement_info = Some(NodeAnnouncementInfo {
1503-
features: msg.features.clone(),
1504-
last_update: msg.timestamp,
1505-
rgb: msg.rgb,
1506-
alias: msg.alias,
1507-
announcement_message: if should_relay { full_msg.cloned() } else { None },
1508-
});
1599+
msg.excess_address_data.len() <= MAX_EXCESS_BYTES_FOR_RELAY &&
1600+
msg.excess_data.len() + msg.excess_address_data.len() <= MAX_EXCESS_BYTES_FOR_RELAY;
1601+
1602+
node.announcement_info = if let (Some(signed_announcement), true) = (full_msg, should_relay) {
1603+
Some(NodeAnnouncementInfo::Relayed(signed_announcement.clone()))
1604+
} else {
1605+
Some(NodeAnnouncementInfo::Local(NodeAnnouncementDetails {
1606+
features: msg.features.clone(),
1607+
last_update: msg.timestamp,
1608+
rgb: msg.rgb,
1609+
alias: msg.alias,
1610+
addresses: msg.addresses.clone(),
1611+
}))
1612+
};
15091613

15101614
Ok(())
15111615
}
@@ -3448,13 +3552,7 @@ pub(crate) mod tests {
34483552
// 1. Check we can read a valid NodeAnnouncementInfo and fail on an invalid one
34493553
let announcement_message = <Vec<u8>>::from_hex("d977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a000122013413a7031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f2020201010101010101010101010101010101010101010101010101010101010101010000701fffefdfc2607").unwrap();
34503554
let announcement_message = NodeAnnouncement::read(&mut announcement_message.as_slice()).unwrap();
3451-
let valid_node_ann_info = NodeAnnouncementInfo {
3452-
features: channelmanager::provided_node_features(&UserConfig::default()),
3453-
last_update: 0,
3454-
rgb: [0u8; 3],
3455-
alias: NodeAlias([0u8; 32]),
3456-
announcement_message: Some(announcement_message)
3457-
};
3555+
let valid_node_ann_info = NodeAnnouncementInfo::Relayed(announcement_message);
34583556

34593557
let mut encoded_valid_node_ann_info = Vec::new();
34603558
assert!(valid_node_ann_info.write(&mut encoded_valid_node_ann_info).is_ok());
@@ -3487,8 +3585,8 @@ pub(crate) mod tests {
34873585
let old_ann_info_with_addresses = <Vec<u8>>::from_hex("3f0009000708a000080a51220204000000000403000000062000000000000000000000000000000000000000000000000000000000000000000a0505014104d2").unwrap();
34883586
let ann_info_with_addresses = NodeAnnouncementInfo::read(&mut old_ann_info_with_addresses.as_slice())
34893587
.expect("to be able to read an old NodeAnnouncementInfo with addresses");
3490-
// This serialized info has an address field but no announcement_message, therefore the addresses returned by our function will still be empty
3491-
assert!(ann_info_with_addresses.addresses().is_empty());
3588+
// This serialized info has no announcement_message but its address field should still be considered
3589+
assert!(!ann_info_with_addresses.addresses().is_empty());
34923590
}
34933591

34943592
#[test]

lightning/src/routing/router.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1984,7 +1984,7 @@ where L::Target: Logger {
19841984
true
19851985
} else if let Some(payee) = payee_node_id_opt {
19861986
network_nodes.get(&payee).map_or(false, |node| node.announcement_info.as_ref().map_or(false,
1987-
|info| info.features.supports_basic_mpp()))
1987+
|info| info.features().supports_basic_mpp()))
19881988
} else { false };
19891989

19901990
let max_total_routing_fee_msat = route_params.max_total_routing_fee_msat.unwrap_or(u64::max_value());
@@ -2469,7 +2469,7 @@ where L::Target: Logger {
24692469
}
24702470

24712471
let features = if let Some(node_info) = $node.announcement_info.as_ref() {
2472-
&node_info.features
2472+
&node_info.features()
24732473
} else {
24742474
&default_node_features
24752475
};
@@ -2799,7 +2799,7 @@ where L::Target: Logger {
27992799
if !features_set {
28002800
if let Some(node) = network_nodes.get(&target) {
28012801
if let Some(node_info) = node.announcement_info.as_ref() {
2802-
ordered_hops.last_mut().unwrap().1 = node_info.features.clone();
2802+
ordered_hops.last_mut().unwrap().1 = node_info.features().clone();
28032803
} else {
28042804
ordered_hops.last_mut().unwrap().1 = default_node_features.clone();
28052805
}

0 commit comments

Comments
 (0)