@@ -30,7 +30,7 @@ use chain::transaction::OutPoint;
3030use ln:: channel:: { Channel , ChannelError } ;
3131use ln:: channelmonitor:: { ChannelMonitor , ChannelMonitorUpdateErr , ManyChannelMonitor , CLTV_CLAIM_BUFFER , LATENCY_GRACE_PERIOD_BLOCKS , ANTI_REORG_DELAY } ;
3232use ln:: router:: Route ;
33- use ln:: features:: InitFeatures ;
33+ use ln:: features:: { InitFeatures , NodeFeatures } ;
3434use ln:: msgs;
3535use ln:: onion_utils;
3636use ln:: msgs:: { ChannelMessageHandler , DecodeError , LightningError } ;
@@ -350,6 +350,8 @@ pub struct ChannelManager<ChanSigner: ChannelKeys> {
350350 channel_state : Mutex < ChannelHolder < ChanSigner > > ,
351351 our_network_key : SecretKey ,
352352
353+ last_node_announcement_serial : AtomicUsize ,
354+
353355 /// Per-peer state storage.
354356 /// Because adding or removing an entry is rare, we usually take an outer read lock and then
355357 /// operate on the inner value freely. Sadly, this prevents parallel operation when opening a
@@ -645,6 +647,8 @@ impl<ChanSigner: ChannelKeys> ChannelManager<ChanSigner> {
645647 } ) ,
646648 our_network_key : keys_manager. get_node_secret ( ) ,
647649
650+ last_node_announcement_serial : AtomicUsize :: new ( 0 ) ,
651+
648652 per_peer_state : RwLock :: new ( HashMap :: new ( ) ) ,
649653
650654 pending_events : Mutex :: new ( Vec :: new ( ) ) ,
@@ -1331,6 +1335,37 @@ impl<ChanSigner: ChannelKeys> ChannelManager<ChanSigner> {
13311335 } )
13321336 }
13331337
1338+ /// Generates a signed node_announcement from the given arguments and creates a
1339+ /// BroadcastNodeAnnouncement event.
1340+ ///
1341+ /// RGB is a node "color" and alias a printable human-readable string to describe this node to
1342+ /// humans. They carry no in-protocol meaning.
1343+ ///
1344+ /// addresses represent the set (possibly empty) of socket addresses on which this node accepts
1345+ /// incoming connections.
1346+ pub fn broadcast_node_announcement ( & self , rgb : [ u8 ; 3 ] , alias : [ u8 ; 32 ] , addresses : msgs:: NetAddressSet ) {
1347+ let _ = self . total_consistency_lock . read ( ) . unwrap ( ) ;
1348+
1349+ let announcement = msgs:: UnsignedNodeAnnouncement {
1350+ features : NodeFeatures :: our_features ( ) ,
1351+ timestamp : self . last_node_announcement_serial . fetch_add ( 1 , Ordering :: AcqRel ) as u32 ,
1352+ node_id : self . get_our_node_id ( ) ,
1353+ rgb, alias,
1354+ addresses : addresses. to_vec ( ) ,
1355+ excess_address_data : Vec :: new ( ) ,
1356+ excess_data : Vec :: new ( ) ,
1357+ } ;
1358+ let msghash = hash_to_message ! ( & Sha256dHash :: hash( & announcement. encode( ) [ ..] ) [ ..] ) ;
1359+
1360+ let mut channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
1361+ channel_state. pending_msg_events . push ( events:: MessageSendEvent :: BroadcastNodeAnnouncement {
1362+ msg : msgs:: NodeAnnouncement {
1363+ signature : self . secp_ctx . sign ( & msghash, & self . our_network_key ) ,
1364+ contents : announcement
1365+ } ,
1366+ } ) ;
1367+ }
1368+
13341369 /// Processes HTLCs which are pending waiting on random forward delay.
13351370 ///
13361371 /// Should only really ever be called in response to a PendingHTLCsForwardable event.
@@ -2925,6 +2960,7 @@ impl<ChanSigner: ChannelKeys> ChannelMessageHandler for ChannelManager<ChanSigne
29252960 & events:: MessageSendEvent :: SendShutdown { ref node_id, .. } => node_id != their_node_id,
29262961 & events:: MessageSendEvent :: SendChannelReestablish { ref node_id, .. } => node_id != their_node_id,
29272962 & events:: MessageSendEvent :: BroadcastChannelAnnouncement { .. } => true ,
2963+ & events:: MessageSendEvent :: BroadcastNodeAnnouncement { .. } => true ,
29282964 & events:: MessageSendEvent :: BroadcastChannelUpdate { .. } => true ,
29292965 & events:: MessageSendEvent :: HandleError { ref node_id, .. } => node_id != their_node_id,
29302966 & events:: MessageSendEvent :: PaymentFailureNetworkUpdate { .. } => true ,
@@ -3238,6 +3274,8 @@ impl<ChanSigner: ChannelKeys + Writeable> Writeable for ChannelManager<ChanSigne
32383274 peer_state. latest_features . write ( writer) ?;
32393275 }
32403276
3277+ ( self . last_node_announcement_serial . load ( Ordering :: Acquire ) as u32 ) . write ( writer) ?;
3278+
32413279 Ok ( ( ) )
32423280 }
32433281}
@@ -3381,6 +3419,8 @@ impl<'a, R : ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>> ReadableArg
33813419 per_peer_state. insert ( peer_pubkey, Mutex :: new ( peer_state) ) ;
33823420 }
33833421
3422+ let last_node_announcement_serial: u32 = Readable :: read ( reader) ?;
3423+
33843424 let channel_manager = ChannelManager {
33853425 genesis_hash,
33863426 fee_estimator : args. fee_estimator ,
@@ -3400,6 +3440,8 @@ impl<'a, R : ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>> ReadableArg
34003440 } ) ,
34013441 our_network_key : args. keys_manager . get_node_secret ( ) ,
34023442
3443+ last_node_announcement_serial : AtomicUsize :: new ( last_node_announcement_serial as usize ) ,
3444+
34033445 per_peer_state : RwLock :: new ( per_peer_state) ,
34043446
34053447 pending_events : Mutex :: new ( Vec :: new ( ) ) ,
0 commit comments