@@ -504,8 +504,8 @@ where U::Target: UtxoLookup, L::Target: Logger
504
504
} ;
505
505
for ( _, ref node) in iter {
506
506
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 ( ) ) ;
509
509
}
510
510
}
511
511
}
@@ -1130,45 +1130,136 @@ impl_writeable_tlv_based!(RoutingFees, {
1130
1130
} ) ;
1131
1131
1132
1132
#[ 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 {
1135
1135
/// Protocol features the node announced support for
1136
1136
pub features : NodeFeatures ,
1137
+
1137
1138
/// When the last known update to the node state was issued.
1138
1139
/// Value is opaque, as set in the announcement.
1139
1140
pub last_update : u32 ,
1141
+
1140
1142
/// Color assigned to the node
1141
1143
pub rgb : [ u8 ; 3 ] ,
1144
+
1142
1145
/// Moniker assigned to the node.
1143
1146
/// May be invalid or malicious (eg control chars),
1144
1147
/// should not be exposed to the user.
1145
1148
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 {
1146
1157
/// An initial announcement of the node
1147
- /// Mostly redundant with the data we store in fields explicitly.
1148
1158
/// Everything else is useful only for sending out for initial routing sync.
1149
1159
/// 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 ) ,
1151
1164
}
1152
1165
1153
1166
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
+
1154
1220
/// 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
+ }
1159
1244
}
1160
1245
}
1161
1246
1162
1247
impl Writeable for NodeAnnouncementInfo {
1163
1248
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
+
1165
1256
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 0.0.115 through 0.0.123 only serialized an empty vec
1172
1263
} ) ;
1173
1264
Ok ( ( ) )
1174
1265
}
@@ -1182,11 +1273,19 @@ impl Readable for NodeAnnouncementInfo {
1182
1273
( 4 , rgb, required) ,
1183
1274
( 6 , alias, required) ,
1184
1275
( 8 , announcement_message, option) ,
1185
- ( 10 , _addresses , optional_vec ) , // deprecated, not used anymore
1276
+ ( 10 , addresses , required_vec ) ,
1186
1277
} ) ;
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
+ }
1190
1289
}
1191
1290
}
1192
1291
@@ -1488,24 +1587,29 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
1488
1587
// The timestamp field is somewhat of a misnomer - the BOLTs use it to order
1489
1588
// updates to ensure you always have the latest one, only vaguely suggesting
1490
1589
// that it be at least the current time.
1491
- if node_info. last_update > msg. timestamp {
1590
+ if node_info. last_update ( ) > msg. timestamp {
1492
1591
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 {
1494
1593
return Err ( LightningError { err : "Update had the same timestamp as last processed update" . to_owned ( ) , action : ErrorAction :: IgnoreDuplicateGossip } ) ;
1495
1594
}
1496
1595
}
1497
1596
1498
1597
let should_relay =
1499
1598
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
+ } ;
1509
1613
1510
1614
Ok ( ( ) )
1511
1615
}
@@ -3448,13 +3552,7 @@ pub(crate) mod tests {
3448
3552
// 1. Check we can read a valid NodeAnnouncementInfo and fail on an invalid one
3449
3553
let announcement_message = <Vec < u8 > >:: from_hex ( "d977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a000122013413a7031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f2020201010101010101010101010101010101010101010101010101010101010101010000701fffefdfc2607" ) . unwrap ( ) ;
3450
3554
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) ;
3458
3556
3459
3557
let mut encoded_valid_node_ann_info = Vec :: new ( ) ;
3460
3558
assert ! ( valid_node_ann_info. write( & mut encoded_valid_node_ann_info) . is_ok( ) ) ;
@@ -3487,8 +3585,8 @@ pub(crate) mod tests {
3487
3585
let old_ann_info_with_addresses = <Vec < u8 > >:: from_hex ( "3f0009000708a000080a51220204000000000403000000062000000000000000000000000000000000000000000000000000000000000000000a0505014104d2" ) . unwrap ( ) ;
3488
3586
let ann_info_with_addresses = NodeAnnouncementInfo :: read ( & mut old_ann_info_with_addresses. as_slice ( ) )
3489
3587
. 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( ) ) ;
3492
3590
}
3493
3591
3494
3592
#[ test]
0 commit comments