Skip to content

Commit a8b2aa9

Browse files
committed
f nodeannouncementinfo refactor
1 parent 4bf6a8c commit a8b2aa9

File tree

3 files changed

+145
-57
lines changed

3 files changed

+145
-57
lines changed

lightning/src/onion_message/messenger.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,8 +386,8 @@ 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| announcement_info.announcement_message().cloned())
390+
.map(|node_announcement| node_announcement.contents);
391391

392392
match node_announcement {
393393
Some(node_announcement) if node_announcement.features.supports_onion_messages() => {

lightning/src/routing/gossip.rs

Lines changed: 140 additions & 52 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,71 +1130,162 @@ 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
1136-
pub features: NodeFeatures,
1136+
features: NodeFeatures,
1137+
11371138
/// When the last known update to the node state was issued.
11381139
/// Value is opaque, as set in the announcement.
1139-
pub last_update: u32,
1140+
last_update: u32,
1141+
11401142
/// Color assigned to the node
1141-
pub rgb: [u8; 3],
1143+
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.
1145-
pub alias: NodeAlias,
1148+
alias: NodeAlias,
11461149

11471150
/// Internet-level addresses via which one can connect to the node
1148-
pub addresses: Vec<SocketAddress>,
1151+
addresses: Vec<SocketAddress>,
1152+
}
11491153

1154+
#[derive(Clone, Debug, PartialEq, Eq)]
1155+
/// Information received in the latest node_announcement from this node.
1156+
pub enum NodeAnnouncementInfo {
11501157
/// An initial announcement of the node
1151-
/// Mostly redundant with the data we store in fields explicitly.
11521158
/// Everything else is useful only for sending out for initial routing sync.
11531159
/// Not stored if contains excess data to prevent DoS.
1154-
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),
11551164
}
11561165

11571166
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+
/// Value is opaque, as set in the announcement.
1182+
pub fn last_update(&self) -> u32 {
1183+
match self {
1184+
NodeAnnouncementInfo::Relayed(relayed) => {
1185+
relayed.contents.timestamp
1186+
}
1187+
NodeAnnouncementInfo::Local(local) => {
1188+
local.last_update
1189+
}
1190+
}
1191+
}
1192+
1193+
/// Color assigned to the node
1194+
pub fn rgb(&self) -> [u8; 3] {
1195+
match self {
1196+
NodeAnnouncementInfo::Relayed(relayed) => {
1197+
relayed.contents.rgb
1198+
}
1199+
NodeAnnouncementInfo::Local(local) => {
1200+
local.rgb
1201+
}
1202+
}
1203+
}
1204+
1205+
/// Moniker assigned to the node.
1206+
/// May be invalid or malicious (eg control chars),
1207+
/// should not be exposed to the user.
1208+
pub fn alias(&self) -> &NodeAlias {
1209+
match self {
1210+
NodeAnnouncementInfo::Relayed(relayed) => {
1211+
&relayed.contents.alias
1212+
}
1213+
NodeAnnouncementInfo::Local(local) => {
1214+
&local.alias
1215+
}
1216+
}
1217+
}
1218+
11581219
/// Internet-level addresses via which one can connect to the node
11591220
pub fn addresses(&self) -> &[SocketAddress] {
1160-
self.announcement_message.as_ref()
1161-
.map(|msg| msg.contents.addresses.as_slice())
1162-
.unwrap_or(self.addresses.as_slice())
1221+
match self {
1222+
NodeAnnouncementInfo::Relayed(relayed) => {
1223+
relayed.contents.addresses.as_slice()
1224+
}
1225+
NodeAnnouncementInfo::Local(local) => {
1226+
local.addresses.as_slice()
1227+
}
1228+
}
1229+
}
1230+
1231+
/// An initial announcement of the node
1232+
/// Not stored if contains excess data to prevent DoS.
1233+
pub fn announcement_message(&self) -> Option<&NodeAnnouncement> {
1234+
match self {
1235+
NodeAnnouncementInfo::Relayed(announcement) => {
1236+
Some(announcement)
1237+
}
1238+
NodeAnnouncementInfo::Local(_) => {
1239+
None
1240+
}
1241+
}
11631242
}
11641243
}
11651244

11661245
impl Writeable for NodeAnnouncementInfo {
11671246
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
1168-
write_tlv_fields!(writer, {
1169-
(0, self.features, required),
1170-
(2, self.last_update, required),
1171-
(4, self.rgb, required),
1172-
(6, self.alias, required),
1173-
(8, self.announcement_message, option),
1174-
(10, self.addresses, required_vec), // Versions prior to 0.0.115 require this field
1175-
});
1247+
match self {
1248+
NodeAnnouncementInfo::Relayed(announcement) => {
1249+
write_tlv_fields!(writer, {
1250+
(8, announcement, required),
1251+
});
1252+
}
1253+
NodeAnnouncementInfo::Local(local) => {
1254+
write_tlv_fields!(writer, {
1255+
(0, local.features, required),
1256+
(2, local.last_update, required),
1257+
(4, local.rgb, required),
1258+
(6, local.alias, required),
1259+
(10, local.addresses, required_vec), // Versions prior to 0.0.115 require this field
1260+
});
1261+
}
1262+
}
1263+
11761264
Ok(())
11771265
}
11781266
}
11791267

11801268
impl Readable for NodeAnnouncementInfo {
11811269
fn read<R: io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
11821270
_init_and_read_len_prefixed_tlv_fields!(reader, {
1183-
(0, features, required),
1184-
(2, last_update, required),
1185-
(4, rgb, required),
1186-
(6, alias, required),
1271+
(0, features, option),
1272+
(2, last_update, option),
1273+
(4, rgb, option),
1274+
(6, alias, option),
11871275
(8, announcement_message, option),
11881276
(10, addresses, optional_vec),
11891277
});
1190-
Ok(Self {
1191-
features: features.0.unwrap(),
1192-
last_update: last_update.0.unwrap(),
1193-
rgb: rgb.0.unwrap(),
1194-
alias: alias.0.unwrap(),
1195-
addresses: addresses.unwrap_or(Vec::new()),
1196-
announcement_message,
1197-
})
1278+
if let Some(announcement) = announcement_message {
1279+
Ok(Self::Relayed(announcement))
1280+
} else {
1281+
Ok(Self::Local(NodeAnnouncementDetails {
1282+
features: features.unwrap(),
1283+
last_update: last_update.unwrap(),
1284+
rgb: rgb.unwrap(),
1285+
alias: alias.unwrap(),
1286+
addresses: addresses.unwrap_or(Vec::new()),
1287+
}))
1288+
}
11981289
}
11991290
}
12001291

@@ -1496,9 +1587,9 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
14961587
// The timestamp field is somewhat of a misnomer - the BOLTs use it to order
14971588
// updates to ensure you always have the latest one, only vaguely suggesting
14981589
// that it be at least the current time.
1499-
if node_info.last_update > msg.timestamp {
1590+
if node_info.last_update() > msg.timestamp {
15001591
return Err(LightningError{err: "Update older than last processed update".to_owned(), action: ErrorAction::IgnoreDuplicateGossip});
1501-
} else if node_info.last_update == msg.timestamp {
1592+
} else if node_info.last_update() == msg.timestamp {
15021593
return Err(LightningError{err: "Update had the same timestamp as last processed update".to_owned(), action: ErrorAction::IgnoreDuplicateGossip});
15031594
}
15041595
}
@@ -1507,14 +1598,18 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
15071598
msg.excess_data.len() <= MAX_EXCESS_BYTES_FOR_RELAY &&
15081599
msg.excess_address_data.len() <= MAX_EXCESS_BYTES_FOR_RELAY &&
15091600
msg.excess_data.len() + msg.excess_address_data.len() <= MAX_EXCESS_BYTES_FOR_RELAY;
1510-
node.announcement_info = Some(NodeAnnouncementInfo {
1511-
features: msg.features.clone(),
1512-
last_update: msg.timestamp,
1513-
rgb: msg.rgb,
1514-
alias: msg.alias,
1515-
addresses: msg.addresses.clone(),
1516-
announcement_message: if should_relay { full_msg.cloned() } else { None },
1517-
});
1601+
1602+
node.announcement_info = if should_relay {
1603+
Some(NodeAnnouncementInfo::Relayed(full_msg.unwrap().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+
};
15181613

15191614
Ok(())
15201615
}
@@ -3457,14 +3552,7 @@ pub(crate) mod tests {
34573552
// 1. Check we can read a valid NodeAnnouncementInfo and fail on an invalid one
34583553
let announcement_message = <Vec<u8>>::from_hex("d977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a000122013413a7031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f2020201010101010101010101010101010101010101010101010101010101010101010000701fffefdfc2607").unwrap();
34593554
let announcement_message = NodeAnnouncement::read(&mut announcement_message.as_slice()).unwrap();
3460-
let valid_node_ann_info = NodeAnnouncementInfo {
3461-
features: channelmanager::provided_node_features(&UserConfig::default()),
3462-
last_update: 0,
3463-
rgb: [0u8; 3],
3464-
alias: NodeAlias([0u8; 32]),
3465-
addresses: announcement_message.contents.addresses.clone(),
3466-
announcement_message: Some(announcement_message)
3467-
};
3555+
let valid_node_ann_info = NodeAnnouncementInfo::Relayed(announcement_message);
34683556

34693557
let mut encoded_valid_node_ann_info = Vec::new();
34703558
assert!(valid_node_ann_info.write(&mut encoded_valid_node_ann_info).is_ok());

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)