@@ -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
}
@@ -872,7 +872,7 @@ pub struct ChannelInfo {
872
872
/// The timestamp when we received the announcement, if we are running with feature = "std"
873
873
/// (which we can probably assume we are - no-std environments probably won't have a full
874
874
/// network graph in memory!).
875
- announcement_received_time : u64 ,
875
+ pub announcement_received_time : u64 ,
876
876
}
877
877
878
878
impl ChannelInfo {
@@ -1130,45 +1130,134 @@ 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
+ /// May be invalid or malicious (eg control chars), 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
+
1154
1219
/// Internet-level addresses via which one can connect to the node
1155
1220
pub fn addresses ( & self ) -> & [ SocketAddress ] {
1156
- self . announcement_message . as_ref ( )
1157
- . map ( |msg| msg. contents . addresses . as_slice ( ) )
1158
- . unwrap_or_default ( )
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
+ }
1159
1242
}
1160
1243
}
1161
1244
1162
1245
impl Writeable for NodeAnnouncementInfo {
1163
1246
fn write < W : Writer > ( & self , writer : & mut W ) -> Result < ( ) , io:: Error > {
1164
- let empty_addresses = Vec :: < SocketAddress > :: new ( ) ;
1247
+ let features = self . features ( ) ;
1248
+ let last_update = self . last_update ( ) ;
1249
+ let rgb = self . rgb ( ) ;
1250
+ let alias = self . alias ( ) ;
1251
+ let addresses = self . addresses ( ) . to_vec ( ) ;
1252
+ let announcement_message = self . announcement_message ( ) ;
1253
+
1165
1254
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
1255
+ ( 0 , features, required) ,
1256
+ ( 2 , last_update, required) ,
1257
+ ( 4 , rgb, required) ,
1258
+ ( 6 , alias, required) ,
1259
+ ( 8 , announcement_message, option) ,
1260
+ ( 10 , addresses , required_vec) , // Versions prior to 0.0.115 require this field
1172
1261
} ) ;
1173
1262
Ok ( ( ) )
1174
1263
}
@@ -1182,11 +1271,19 @@ impl Readable for NodeAnnouncementInfo {
1182
1271
( 4 , rgb, required) ,
1183
1272
( 6 , alias, required) ,
1184
1273
( 8 , announcement_message, option) ,
1185
- ( 10 , _addresses , optional_vec) , // deprecated, not used anymore
1274
+ ( 10 , addresses , optional_vec) ,
1186
1275
} ) ;
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 } )
1276
+ if let Some ( announcement) = announcement_message {
1277
+ Ok ( Self :: Relayed ( announcement) )
1278
+ } else {
1279
+ Ok ( Self :: Local ( NodeAnnouncementDetails {
1280
+ features : features. 0 . unwrap ( ) ,
1281
+ last_update : last_update. 0 . unwrap ( ) ,
1282
+ rgb : rgb. 0 . unwrap ( ) ,
1283
+ alias : alias. 0 . unwrap ( ) ,
1284
+ addresses : addresses. unwrap_or ( Vec :: new ( ) ) ,
1285
+ } ) )
1286
+ }
1190
1287
}
1191
1288
}
1192
1289
@@ -1488,9 +1585,9 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
1488
1585
// The timestamp field is somewhat of a misnomer - the BOLTs use it to order
1489
1586
// updates to ensure you always have the latest one, only vaguely suggesting
1490
1587
// that it be at least the current time.
1491
- if node_info. last_update > msg. timestamp {
1588
+ if node_info. last_update ( ) > msg. timestamp {
1492
1589
return Err ( LightningError { err : "Update older than last processed update" . to_owned ( ) , action : ErrorAction :: IgnoreDuplicateGossip } ) ;
1493
- } else if node_info. last_update == msg. timestamp {
1590
+ } else if node_info. last_update ( ) == msg. timestamp {
1494
1591
return Err ( LightningError { err : "Update had the same timestamp as last processed update" . to_owned ( ) , action : ErrorAction :: IgnoreDuplicateGossip } ) ;
1495
1592
}
1496
1593
}
@@ -1499,13 +1596,18 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
1499
1596
msg. excess_data . len ( ) <= MAX_EXCESS_BYTES_FOR_RELAY &&
1500
1597
msg. excess_address_data . len ( ) <= MAX_EXCESS_BYTES_FOR_RELAY &&
1501
1598
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
+
1600
+ node. announcement_info = if should_relay {
1601
+ Some ( NodeAnnouncementInfo :: Relayed ( full_msg. unwrap ( ) . clone ( ) ) )
1602
+ } else {
1603
+ Some ( NodeAnnouncementInfo :: Local ( NodeAnnouncementDetails {
1604
+ features : msg. features . clone ( ) ,
1605
+ last_update : msg. timestamp ,
1606
+ rgb : msg. rgb ,
1607
+ alias : msg. alias ,
1608
+ addresses : msg. addresses . clone ( ) ,
1609
+ } ) )
1610
+ } ;
1509
1611
1510
1612
Ok ( ( ) )
1511
1613
}
@@ -3448,13 +3550,7 @@ pub(crate) mod tests {
3448
3550
// 1. Check we can read a valid NodeAnnouncementInfo and fail on an invalid one
3449
3551
let announcement_message = <Vec < u8 > >:: from_hex ( "d977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a000122013413a7031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f2020201010101010101010101010101010101010101010101010101010101010101010000701fffefdfc2607" ) . unwrap ( ) ;
3450
3552
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
- } ;
3553
+ let valid_node_ann_info = NodeAnnouncementInfo :: Relayed ( announcement_message) ;
3458
3554
3459
3555
let mut encoded_valid_node_ann_info = Vec :: new ( ) ;
3460
3556
assert ! ( valid_node_ann_info. write( & mut encoded_valid_node_ann_info) . is_ok( ) ) ;
@@ -3487,8 +3583,8 @@ pub(crate) mod tests {
3487
3583
let old_ann_info_with_addresses = <Vec < u8 > >:: from_hex ( "3f0009000708a000080a51220204000000000403000000062000000000000000000000000000000000000000000000000000000000000000000a0505014104d2" ) . unwrap ( ) ;
3488
3584
let ann_info_with_addresses = NodeAnnouncementInfo :: read ( & mut old_ann_info_with_addresses. as_slice ( ) )
3489
3585
. 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( ) ) ;
3586
+ // This serialized info has no announcement_message but its address field should still be considered
3587
+ assert ! ( ! ann_info_with_addresses. addresses( ) . is_empty( ) ) ;
3492
3588
}
3493
3589
3494
3590
#[ test]
0 commit comments