@@ -505,8 +505,8 @@ where U::Target: UtxoLookup, L::Target: Logger
505
505
} ;
506
506
for ( _, ref node) in iter {
507
507
if let Some ( node_info) = node. announcement_info . as_ref ( ) {
508
- if let Some ( msg ) = node_info. announcement_message . clone ( ) {
509
- return Some ( msg ) ;
508
+ if let NodeAnnouncementInfo :: Relayed ( announcement ) = node_info {
509
+ return Some ( announcement . clone ( ) ) ;
510
510
}
511
511
}
512
512
}
@@ -1135,45 +1135,136 @@ impl_writeable_tlv_based!(RoutingFees, {
1135
1135
} ) ;
1136
1136
1137
1137
#[ derive( Clone , Debug , PartialEq , Eq ) ]
1138
- /// Information received in the latest node_announcement from this node.
1139
- pub struct NodeAnnouncementInfo {
1138
+ /// Non-relayable information received in the latest node_announcement from this node.
1139
+ pub struct NodeAnnouncementDetails {
1140
1140
/// Protocol features the node announced support for
1141
1141
pub features : NodeFeatures ,
1142
+
1142
1143
/// When the last known update to the node state was issued.
1143
1144
/// Value is opaque, as set in the announcement.
1144
1145
pub last_update : u32 ,
1146
+
1145
1147
/// Color assigned to the node
1146
1148
pub rgb : [ u8 ; 3 ] ,
1149
+
1147
1150
/// Moniker assigned to the node.
1148
1151
/// May be invalid or malicious (eg control chars),
1149
1152
/// should not be exposed to the user.
1150
1153
pub alias : NodeAlias ,
1154
+
1155
+ /// Internet-level addresses via which one can connect to the node
1156
+ pub addresses : Vec < SocketAddress > ,
1157
+ }
1158
+
1159
+ #[ derive( Clone , Debug , PartialEq , Eq ) ]
1160
+ /// Information received in the latest node_announcement from this node.
1161
+ pub enum NodeAnnouncementInfo {
1151
1162
/// An initial announcement of the node
1152
- /// Mostly redundant with the data we store in fields explicitly.
1153
1163
/// Everything else is useful only for sending out for initial routing sync.
1154
1164
/// Not stored if contains excess data to prevent DoS.
1155
- pub announcement_message : Option < NodeAnnouncement >
1165
+ Relayed ( NodeAnnouncement ) ,
1166
+
1167
+ /// Non-relayable information received in the latest node_announcement from this node.
1168
+ Local ( NodeAnnouncementDetails ) ,
1156
1169
}
1157
1170
1158
1171
impl NodeAnnouncementInfo {
1172
+
1173
+ /// Protocol features the node announced support for
1174
+ pub fn features ( & self ) -> & NodeFeatures {
1175
+ match self {
1176
+ NodeAnnouncementInfo :: Relayed ( relayed) => {
1177
+ & relayed. contents . features
1178
+ }
1179
+ NodeAnnouncementInfo :: Local ( local) => {
1180
+ & local. features
1181
+ }
1182
+ }
1183
+ }
1184
+
1185
+ /// When the last known update to the node state was issued.
1186
+ ///
1187
+ /// Value may or may not be a timestamp, depending on the policy of the origin node.
1188
+ pub fn last_update ( & self ) -> u32 {
1189
+ match self {
1190
+ NodeAnnouncementInfo :: Relayed ( relayed) => {
1191
+ relayed. contents . timestamp
1192
+ }
1193
+ NodeAnnouncementInfo :: Local ( local) => {
1194
+ local. last_update
1195
+ }
1196
+ }
1197
+ }
1198
+
1199
+ /// Color assigned to the node
1200
+ pub fn rgb ( & self ) -> [ u8 ; 3 ] {
1201
+ match self {
1202
+ NodeAnnouncementInfo :: Relayed ( relayed) => {
1203
+ relayed. contents . rgb
1204
+ }
1205
+ NodeAnnouncementInfo :: Local ( local) => {
1206
+ local. rgb
1207
+ }
1208
+ }
1209
+ }
1210
+
1211
+ /// Moniker assigned to the node.
1212
+ ///
1213
+ /// May be invalid or malicious (eg control chars), should not be exposed to the user.
1214
+ pub fn alias ( & self ) -> & NodeAlias {
1215
+ match self {
1216
+ NodeAnnouncementInfo :: Relayed ( relayed) => {
1217
+ & relayed. contents . alias
1218
+ }
1219
+ NodeAnnouncementInfo :: Local ( local) => {
1220
+ & local. alias
1221
+ }
1222
+ }
1223
+ }
1224
+
1159
1225
/// Internet-level addresses via which one can connect to the node
1160
- pub fn addresses ( & self ) -> & [ SocketAddress ] {
1161
- self . announcement_message . as_ref ( )
1162
- . map ( |msg| msg. contents . addresses . as_slice ( ) )
1163
- . unwrap_or_default ( )
1226
+ pub fn addresses ( & self ) -> & Vec < SocketAddress > {
1227
+ match self {
1228
+ NodeAnnouncementInfo :: Relayed ( relayed) => {
1229
+ & relayed. contents . addresses
1230
+ }
1231
+ NodeAnnouncementInfo :: Local ( local) => {
1232
+ & local. addresses
1233
+ }
1234
+ }
1235
+ }
1236
+
1237
+ /// An initial announcement of the node
1238
+ ///
1239
+ /// Not stored if contains excess data to prevent DoS.
1240
+ pub fn announcement_message ( & self ) -> Option < & NodeAnnouncement > {
1241
+ match self {
1242
+ NodeAnnouncementInfo :: Relayed ( announcement) => {
1243
+ Some ( announcement)
1244
+ }
1245
+ NodeAnnouncementInfo :: Local ( _) => {
1246
+ None
1247
+ }
1248
+ }
1164
1249
}
1165
1250
}
1166
1251
1167
1252
impl Writeable for NodeAnnouncementInfo {
1168
1253
fn write < W : Writer > ( & self , writer : & mut W ) -> Result < ( ) , io:: Error > {
1169
- let empty_addresses = Vec :: < SocketAddress > :: new ( ) ;
1254
+ let features = self . features ( ) ;
1255
+ let last_update = self . last_update ( ) ;
1256
+ let rgb = self . rgb ( ) ;
1257
+ let alias = self . alias ( ) ;
1258
+ let addresses = self . addresses ( ) ;
1259
+ let announcement_message = self . announcement_message ( ) ;
1260
+
1170
1261
write_tlv_fields ! ( writer, {
1171
- ( 0 , self . features, required) ,
1172
- ( 2 , self . last_update, required) ,
1173
- ( 4 , self . rgb, required) ,
1174
- ( 6 , self . alias, required) ,
1175
- ( 8 , self . announcement_message, option) ,
1176
- ( 10 , empty_addresses , required_vec) , // Versions prior to 0.0.115 require this field
1262
+ ( 0 , features, required) ,
1263
+ ( 2 , last_update, required) ,
1264
+ ( 4 , rgb, required) ,
1265
+ ( 6 , alias, required) ,
1266
+ ( 8 , announcement_message, option) ,
1267
+ ( 10 , * addresses , required_vec) , // Versions 0.0.115 through 0.0.123 only serialized an empty vec
1177
1268
} ) ;
1178
1269
Ok ( ( ) )
1179
1270
}
@@ -1187,11 +1278,19 @@ impl Readable for NodeAnnouncementInfo {
1187
1278
( 4 , rgb, required) ,
1188
1279
( 6 , alias, required) ,
1189
1280
( 8 , announcement_message, option) ,
1190
- ( 10 , _addresses , optional_vec ) , // deprecated, not used anymore
1281
+ ( 10 , addresses , required_vec ) ,
1191
1282
} ) ;
1192
- let _: Option < Vec < SocketAddress > > = _addresses;
1193
- Ok ( Self { features : features. 0 . unwrap ( ) , last_update : last_update. 0 . unwrap ( ) , rgb : rgb. 0 . unwrap ( ) ,
1194
- alias : alias. 0 . unwrap ( ) , announcement_message } )
1283
+ if let Some ( announcement) = announcement_message {
1284
+ Ok ( Self :: Relayed ( announcement) )
1285
+ } else {
1286
+ Ok ( Self :: Local ( NodeAnnouncementDetails {
1287
+ features : features. 0 . unwrap ( ) ,
1288
+ last_update : last_update. 0 . unwrap ( ) ,
1289
+ rgb : rgb. 0 . unwrap ( ) ,
1290
+ alias : alias. 0 . unwrap ( ) ,
1291
+ addresses,
1292
+ } ) )
1293
+ }
1195
1294
}
1196
1295
}
1197
1296
@@ -1493,24 +1592,29 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
1493
1592
// The timestamp field is somewhat of a misnomer - the BOLTs use it to order
1494
1593
// updates to ensure you always have the latest one, only vaguely suggesting
1495
1594
// that it be at least the current time.
1496
- if node_info. last_update > msg. timestamp {
1595
+ if node_info. last_update ( ) > msg. timestamp {
1497
1596
return Err ( LightningError { err : "Update older than last processed update" . to_owned ( ) , action : ErrorAction :: IgnoreDuplicateGossip } ) ;
1498
- } else if node_info. last_update == msg. timestamp {
1597
+ } else if node_info. last_update ( ) == msg. timestamp {
1499
1598
return Err ( LightningError { err : "Update had the same timestamp as last processed update" . to_owned ( ) , action : ErrorAction :: IgnoreDuplicateGossip } ) ;
1500
1599
}
1501
1600
}
1502
1601
1503
1602
let should_relay =
1504
1603
msg. excess_data . len ( ) <= MAX_EXCESS_BYTES_FOR_RELAY &&
1505
- msg. excess_address_data . len ( ) <= MAX_EXCESS_BYTES_FOR_RELAY &&
1506
- msg. excess_data . len ( ) + msg. excess_address_data . len ( ) <= MAX_EXCESS_BYTES_FOR_RELAY ;
1507
- node. announcement_info = Some ( NodeAnnouncementInfo {
1508
- features : msg. features . clone ( ) ,
1509
- last_update : msg. timestamp ,
1510
- rgb : msg. rgb ,
1511
- alias : msg. alias ,
1512
- announcement_message : if should_relay { full_msg. cloned ( ) } else { None } ,
1513
- } ) ;
1604
+ msg. excess_address_data . len ( ) <= MAX_EXCESS_BYTES_FOR_RELAY &&
1605
+ msg. excess_data . len ( ) + msg. excess_address_data . len ( ) <= MAX_EXCESS_BYTES_FOR_RELAY ;
1606
+
1607
+ node. announcement_info = if let ( Some ( signed_announcement) , true ) = ( full_msg, should_relay) {
1608
+ Some ( NodeAnnouncementInfo :: Relayed ( signed_announcement. clone ( ) ) )
1609
+ } else {
1610
+ Some ( NodeAnnouncementInfo :: Local ( NodeAnnouncementDetails {
1611
+ features : msg. features . clone ( ) ,
1612
+ last_update : msg. timestamp ,
1613
+ rgb : msg. rgb ,
1614
+ alias : msg. alias ,
1615
+ addresses : msg. addresses . clone ( ) ,
1616
+ } ) )
1617
+ } ;
1514
1618
1515
1619
Ok ( ( ) )
1516
1620
}
@@ -3454,13 +3558,7 @@ pub(crate) mod tests {
3454
3558
// 1. Check we can read a valid NodeAnnouncementInfo and fail on an invalid one
3455
3559
let announcement_message = <Vec < u8 > >:: from_hex ( "d977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a000122013413a7031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f2020201010101010101010101010101010101010101010101010101010101010101010000701fffefdfc2607" ) . unwrap ( ) ;
3456
3560
let announcement_message = NodeAnnouncement :: read ( & mut announcement_message. as_slice ( ) ) . unwrap ( ) ;
3457
- let valid_node_ann_info = NodeAnnouncementInfo {
3458
- features : channelmanager:: provided_node_features ( & UserConfig :: default ( ) ) ,
3459
- last_update : 0 ,
3460
- rgb : [ 0u8 ; 3 ] ,
3461
- alias : NodeAlias ( [ 0u8 ; 32 ] ) ,
3462
- announcement_message : Some ( announcement_message)
3463
- } ;
3561
+ let valid_node_ann_info = NodeAnnouncementInfo :: Relayed ( announcement_message) ;
3464
3562
3465
3563
let mut encoded_valid_node_ann_info = Vec :: new ( ) ;
3466
3564
assert ! ( valid_node_ann_info. write( & mut encoded_valid_node_ann_info) . is_ok( ) ) ;
@@ -3493,8 +3591,8 @@ pub(crate) mod tests {
3493
3591
let old_ann_info_with_addresses = <Vec < u8 > >:: from_hex ( "3f0009000708a000080a51220204000000000403000000062000000000000000000000000000000000000000000000000000000000000000000a0505014104d2" ) . unwrap ( ) ;
3494
3592
let ann_info_with_addresses = NodeAnnouncementInfo :: read ( & mut old_ann_info_with_addresses. as_slice ( ) )
3495
3593
. expect ( "to be able to read an old NodeAnnouncementInfo with addresses" ) ;
3496
- // This serialized info has an address field but no announcement_message, therefore the addresses returned by our function will still be empty
3497
- assert ! ( ann_info_with_addresses. addresses( ) . is_empty( ) ) ;
3594
+ // This serialized info has no announcement_message but its address field should still be considered
3595
+ assert ! ( ! ann_info_with_addresses. addresses( ) . is_empty( ) ) ;
3498
3596
}
3499
3597
3500
3598
#[ test]
0 commit comments