@@ -29,8 +29,8 @@ use chain::chaininterface::{BroadcasterInterface,ChainListener,FeeEstimator};
2929use chain:: transaction:: OutPoint ;
3030use ln:: channel:: { Channel , ChannelError } ;
3131use ln:: channelmonitor:: { ChannelMonitor , ChannelMonitorUpdateErr , ManyChannelMonitor , CLTV_CLAIM_BUFFER , LATENCY_GRACE_PERIOD_BLOCKS , ANTI_REORG_DELAY } ;
32+ use ln:: features:: { InitFeatures , NodeFeatures } ;
3233use ln:: router:: Route ;
33- use ln:: features:: InitFeatures ;
3434use ln:: msgs;
3535use ln:: onion_utils;
3636use ln:: msgs:: { ChannelMessageHandler , DecodeError , LightningError } ;
@@ -362,6 +362,10 @@ pub struct ChannelManager<ChanSigner: ChannelKeys, M: Deref, T: Deref>
362362 channel_state : Mutex < ChannelHolder < ChanSigner > > ,
363363 our_network_key : SecretKey ,
364364
365+ /// Used to track the last value sent in a node_announcement "timestamp" field. We just set
366+ /// them to be monotonically increasing since we don't assume access to a time source.
367+ last_node_announcement_serial : AtomicUsize ,
368+
365369 /// The bulk of our storage will eventually be here (channels and message queues and the like).
366370 /// If we are connected to a peer we always at least have an entry here, even if no channels
367371 /// are currently open with that peer.
@@ -657,6 +661,8 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref> ChannelManager<ChanSigner, M,
657661 } ) ,
658662 our_network_key : keys_manager. get_node_secret ( ) ,
659663
664+ last_node_announcement_serial : AtomicUsize :: new ( 0 ) ,
665+
660666 per_peer_state : RwLock :: new ( HashMap :: new ( ) ) ,
661667
662668 pending_events : Mutex :: new ( Vec :: new ( ) ) ,
@@ -1325,6 +1331,39 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref> ChannelManager<ChanSigner, M,
13251331 } )
13261332 }
13271333
1334+ /// Generates a signed node_announcement from the given arguments and creates a
1335+ /// BroadcastNodeAnnouncement event.
1336+ ///
1337+ /// RGB is a node "color" and alias ia a printable human-readable string to describe this node
1338+ /// to humans. They carry no in-protocol meaning.
1339+ ///
1340+ /// addresses represent the set (possibly empty) of socket addresses on which this node accepts
1341+ /// incoming connections. These will be broadcast to the network, publicly tying these
1342+ /// addresses together. If you wish to preserve user privacy, addresses should likely contain
1343+ /// only Tor Onion addresses.
1344+ pub fn broadcast_node_announcement ( & self , rgb : [ u8 ; 3 ] , alias : [ u8 ; 32 ] , addresses : msgs:: NetAddressSet ) {
1345+ let _ = self . total_consistency_lock . read ( ) . unwrap ( ) ;
1346+
1347+ let announcement = msgs:: UnsignedNodeAnnouncement {
1348+ features : NodeFeatures :: supported ( ) ,
1349+ timestamp : self . last_node_announcement_serial . fetch_add ( 1 , Ordering :: AcqRel ) as u32 ,
1350+ node_id : self . get_our_node_id ( ) ,
1351+ rgb, alias,
1352+ addresses : addresses. into_vec ( ) ,
1353+ excess_address_data : Vec :: new ( ) ,
1354+ excess_data : Vec :: new ( ) ,
1355+ } ;
1356+ let msghash = hash_to_message ! ( & Sha256dHash :: hash( & announcement. encode( ) [ ..] ) [ ..] ) ;
1357+
1358+ let mut channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
1359+ channel_state. pending_msg_events . push ( events:: MessageSendEvent :: BroadcastNodeAnnouncement {
1360+ msg : msgs:: NodeAnnouncement {
1361+ signature : self . secp_ctx . sign ( & msghash, & self . our_network_key ) ,
1362+ contents : announcement
1363+ } ,
1364+ } ) ;
1365+ }
1366+
13281367 /// Processes HTLCs which are pending waiting on random forward delay.
13291368 ///
13301369 /// Should only really ever be called in response to a PendingHTLCsForwardable event.
@@ -2951,6 +2990,7 @@ impl<ChanSigner: ChannelKeys, M: Deref + Sync + Send, T: Deref + Sync + Send> Ch
29512990 & events:: MessageSendEvent :: SendShutdown { ref node_id, .. } => node_id != their_node_id,
29522991 & events:: MessageSendEvent :: SendChannelReestablish { ref node_id, .. } => node_id != their_node_id,
29532992 & events:: MessageSendEvent :: BroadcastChannelAnnouncement { .. } => true ,
2993+ & events:: MessageSendEvent :: BroadcastNodeAnnouncement { .. } => true ,
29542994 & events:: MessageSendEvent :: BroadcastChannelUpdate { .. } => true ,
29552995 & events:: MessageSendEvent :: HandleError { ref node_id, .. } => node_id != their_node_id,
29562996 & events:: MessageSendEvent :: PaymentFailureNetworkUpdate { .. } => true ,
@@ -3267,6 +3307,8 @@ impl<ChanSigner: ChannelKeys + Writeable, M: Deref, T: Deref> Writeable for Chan
32673307 peer_state. latest_features . write ( writer) ?;
32683308 }
32693309
3310+ ( self . last_node_announcement_serial . load ( Ordering :: Acquire ) as u32 ) . write ( writer) ?;
3311+
32703312 Ok ( ( ) )
32713313 }
32723314}
@@ -3418,6 +3460,8 @@ impl<'a, R : ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>, M: Deref, T
34183460 per_peer_state. insert ( peer_pubkey, Mutex :: new ( peer_state) ) ;
34193461 }
34203462
3463+ let last_node_announcement_serial: u32 = Readable :: read ( reader) ?;
3464+
34213465 let channel_manager = ChannelManager {
34223466 genesis_hash,
34233467 fee_estimator : args. fee_estimator ,
@@ -3437,6 +3481,8 @@ impl<'a, R : ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>, M: Deref, T
34373481 } ) ,
34383482 our_network_key : args. keys_manager . get_node_secret ( ) ,
34393483
3484+ last_node_announcement_serial : AtomicUsize :: new ( last_node_announcement_serial as usize ) ,
3485+
34403486 per_peer_state : RwLock :: new ( per_peer_state) ,
34413487
34423488 pending_events : Mutex :: new ( Vec :: new ( ) ) ,
0 commit comments