@@ -505,8 +505,8 @@ where U::Target: UtxoLookup, L::Target: Logger
505505 } ;
506506 for ( _, ref node) in iter {
507507 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 ( ) ) ;
510510 }
511511 }
512512 }
@@ -1135,45 +1135,136 @@ impl_writeable_tlv_based!(RoutingFees, {
11351135} ) ;
11361136
11371137#[ 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 {
11401140 /// Protocol features the node announced support for
11411141 pub features : NodeFeatures ,
1142+
11421143 /// When the last known update to the node state was issued.
11431144 /// Value is opaque, as set in the announcement.
11441145 pub last_update : u32 ,
1146+
11451147 /// Color assigned to the node
11461148 pub rgb : [ u8 ; 3 ] ,
1149+
11471150 /// Moniker assigned to the node.
11481151 /// May be invalid or malicious (eg control chars),
11491152 /// should not be exposed to the user.
11501153 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 {
11511162 /// An initial announcement of the node
1152- /// Mostly redundant with the data we store in fields explicitly.
11531163 /// Everything else is useful only for sending out for initial routing sync.
11541164 /// 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 ) ,
11561169}
11571170
11581171impl 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+
11591225 /// 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+ }
11641249 }
11651250}
11661251
11671252impl Writeable for NodeAnnouncementInfo {
11681253 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+
11701261 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
11771268 } ) ;
11781269 Ok ( ( ) )
11791270 }
@@ -1187,11 +1278,19 @@ impl Readable for NodeAnnouncementInfo {
11871278 ( 4 , rgb, required) ,
11881279 ( 6 , alias, required) ,
11891280 ( 8 , announcement_message, option) ,
1190- ( 10 , _addresses , optional_vec ) , // deprecated, not used anymore
1281+ ( 10 , addresses , required_vec ) ,
11911282 } ) ;
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+ }
11951294 }
11961295}
11971296
@@ -1493,24 +1592,29 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
14931592 // The timestamp field is somewhat of a misnomer - the BOLTs use it to order
14941593 // updates to ensure you always have the latest one, only vaguely suggesting
14951594 // that it be at least the current time.
1496- if node_info. last_update > msg. timestamp {
1595+ if node_info. last_update ( ) > msg. timestamp {
14971596 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 {
14991598 return Err ( LightningError { err : "Update had the same timestamp as last processed update" . to_owned ( ) , action : ErrorAction :: IgnoreDuplicateGossip } ) ;
15001599 }
15011600 }
15021601
15031602 let should_relay =
15041603 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+ } ;
15141618
15151619 Ok ( ( ) )
15161620 }
@@ -3454,13 +3558,7 @@ pub(crate) mod tests {
34543558 // 1. Check we can read a valid NodeAnnouncementInfo and fail on an invalid one
34553559 let announcement_message = <Vec < u8 > >:: from_hex ( "d977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a000122013413a7031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f2020201010101010101010101010101010101010101010101010101010101010101010000701fffefdfc2607" ) . unwrap ( ) ;
34563560 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) ;
34643562
34653563 let mut encoded_valid_node_ann_info = Vec :: new ( ) ;
34663564 assert ! ( valid_node_ann_info. write( & mut encoded_valid_node_ann_info) . is_ok( ) ) ;
@@ -3493,8 +3591,8 @@ pub(crate) mod tests {
34933591 let old_ann_info_with_addresses = <Vec < u8 > >:: from_hex ( "3f0009000708a000080a51220204000000000403000000062000000000000000000000000000000000000000000000000000000000000000000a0505014104d2" ) . unwrap ( ) ;
34943592 let ann_info_with_addresses = NodeAnnouncementInfo :: read ( & mut old_ann_info_with_addresses. as_slice ( ) )
34953593 . 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( ) ) ;
34983596 }
34993597
35003598 #[ test]
0 commit comments