@@ -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,71 +1130,162 @@ 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
- pub features : NodeFeatures ,
1136
+ 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
- pub last_update : u32 ,
1140
+ last_update : u32 ,
1141
+
1140
1142
/// Color assigned to the node
1141
- pub rgb : [ u8 ; 3 ] ,
1143
+ 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
- pub alias : NodeAlias ,
1148
+ alias : NodeAlias ,
1146
1149
1147
1150
/// Internet-level addresses via which one can connect to the node
1148
- pub addresses : Vec < SocketAddress > ,
1151
+ addresses : Vec < SocketAddress > ,
1152
+ }
1149
1153
1154
+ #[ derive( Clone , Debug , PartialEq , Eq ) ]
1155
+ /// Information received in the latest node_announcement from this node.
1156
+ pub enum NodeAnnouncementInfo {
1150
1157
/// An initial announcement of the node
1151
- /// Mostly redundant with the data we store in fields explicitly.
1152
1158
/// Everything else is useful only for sending out for initial routing sync.
1153
1159
/// 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 ) ,
1155
1164
}
1156
1165
1157
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
+ /// 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
+
1158
1219
/// Internet-level addresses via which one can connect to the node
1159
1220
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
+ }
1163
1242
}
1164
1243
}
1165
1244
1166
1245
impl Writeable for NodeAnnouncementInfo {
1167
1246
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
+
1176
1264
Ok ( ( ) )
1177
1265
}
1178
1266
}
1179
1267
1180
1268
impl Readable for NodeAnnouncementInfo {
1181
1269
fn read < R : io:: Read > ( reader : & mut R ) -> Result < Self , DecodeError > {
1182
1270
_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 ) ,
1187
1275
( 8 , announcement_message, option) ,
1188
1276
( 10 , addresses, optional_vec) ,
1189
1277
} ) ;
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
+ }
1198
1289
}
1199
1290
}
1200
1291
@@ -1496,9 +1587,9 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
1496
1587
// The timestamp field is somewhat of a misnomer - the BOLTs use it to order
1497
1588
// updates to ensure you always have the latest one, only vaguely suggesting
1498
1589
// that it be at least the current time.
1499
- if node_info. last_update > msg. timestamp {
1590
+ if node_info. last_update ( ) > msg. timestamp {
1500
1591
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 {
1502
1593
return Err ( LightningError { err : "Update had the same timestamp as last processed update" . to_owned ( ) , action : ErrorAction :: IgnoreDuplicateGossip } ) ;
1503
1594
}
1504
1595
}
@@ -1507,14 +1598,18 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
1507
1598
msg. excess_data . len ( ) <= MAX_EXCESS_BYTES_FOR_RELAY &&
1508
1599
msg. excess_address_data . len ( ) <= MAX_EXCESS_BYTES_FOR_RELAY &&
1509
1600
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
+ } ;
1518
1613
1519
1614
Ok ( ( ) )
1520
1615
}
0 commit comments