@@ -504,8 +504,8 @@ where U::Target: UtxoLookup, L::Target: Logger
504504 } ;
505505 for ( _, ref node) in iter {
506506 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 ( ) ) ;
509509 }
510510 }
511511 }
@@ -872,7 +872,7 @@ pub struct ChannelInfo {
872872 /// The timestamp when we received the announcement, if we are running with feature = "std"
873873 /// (which we can probably assume we are - no-std environments probably won't have a full
874874 /// network graph in memory!).
875- announcement_received_time : u64 ,
875+ pub announcement_received_time : u64 ,
876876}
877877
878878impl ChannelInfo {
@@ -1130,45 +1130,134 @@ impl_writeable_tlv_based!(RoutingFees, {
11301130} ) ;
11311131
11321132#[ 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 {
11351135 /// Protocol features the node announced support for
11361136 pub features : NodeFeatures ,
1137+
11371138 /// When the last known update to the node state was issued.
11381139 /// Value is opaque, as set in the announcement.
11391140 pub last_update : u32 ,
1141+
11401142 /// Color assigned to the node
11411143 pub rgb : [ u8 ; 3 ] ,
1144+
11421145 /// Moniker assigned to the node.
11431146 /// May be invalid or malicious (eg control chars),
11441147 /// should not be exposed to the user.
11451148 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 {
11461157 /// An initial announcement of the node
1147- /// Mostly redundant with the data we store in fields explicitly.
11481158 /// Everything else is useful only for sending out for initial routing sync.
11491159 /// 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 ) ,
11511164}
11521165
11531166impl 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+
11541219 /// Internet-level addresses via which one can connect to the node
11551220 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+ }
11591242 }
11601243}
11611244
11621245impl Writeable for NodeAnnouncementInfo {
11631246 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+
11651254 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
11721261 } ) ;
11731262 Ok ( ( ) )
11741263 }
@@ -1182,11 +1271,19 @@ impl Readable for NodeAnnouncementInfo {
11821271 ( 4 , rgb, required) ,
11831272 ( 6 , alias, required) ,
11841273 ( 8 , announcement_message, option) ,
1185- ( 10 , _addresses , optional_vec) , // deprecated, not used anymore
1274+ ( 10 , addresses , optional_vec) ,
11861275 } ) ;
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+ }
11901287 }
11911288}
11921289
@@ -1488,9 +1585,9 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
14881585 // The timestamp field is somewhat of a misnomer - the BOLTs use it to order
14891586 // updates to ensure you always have the latest one, only vaguely suggesting
14901587 // that it be at least the current time.
1491- if node_info. last_update > msg. timestamp {
1588+ if node_info. last_update ( ) > msg. timestamp {
14921589 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 {
14941591 return Err ( LightningError { err : "Update had the same timestamp as last processed update" . to_owned ( ) , action : ErrorAction :: IgnoreDuplicateGossip } ) ;
14951592 }
14961593 }
@@ -1499,13 +1596,18 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
14991596 msg. excess_data . len ( ) <= MAX_EXCESS_BYTES_FOR_RELAY &&
15001597 msg. excess_address_data . len ( ) <= MAX_EXCESS_BYTES_FOR_RELAY &&
15011598 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+ } ;
15091611
15101612 Ok ( ( ) )
15111613 }
@@ -3448,13 +3550,7 @@ pub(crate) mod tests {
34483550 // 1. Check we can read a valid NodeAnnouncementInfo and fail on an invalid one
34493551 let announcement_message = <Vec < u8 > >:: from_hex ( "d977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a000122013413a7031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f2020201010101010101010101010101010101010101010101010101010101010101010000701fffefdfc2607" ) . unwrap ( ) ;
34503552 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) ;
34583554
34593555 let mut encoded_valid_node_ann_info = Vec :: new ( ) ;
34603556 assert ! ( valid_node_ann_info. write( & mut encoded_valid_node_ann_info) . is_ok( ) ) ;
@@ -3487,8 +3583,8 @@ pub(crate) mod tests {
34873583 let old_ann_info_with_addresses = <Vec < u8 > >:: from_hex ( "3f0009000708a000080a51220204000000000403000000062000000000000000000000000000000000000000000000000000000000000000000a0505014104d2" ) . unwrap ( ) ;
34883584 let ann_info_with_addresses = NodeAnnouncementInfo :: read ( & mut old_ann_info_with_addresses. as_slice ( ) )
34893585 . 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( ) ) ;
34923588 }
34933589
34943590 #[ test]
0 commit comments