@@ -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 } ;
@@ -368,6 +368,10 @@ pub struct ChannelManager<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref,
368368 channel_state : Mutex < ChannelHolder < ChanSigner > > ,
369369 our_network_key : SecretKey ,
370370
371+ /// Used to track the last value sent in a node_announcement "timestamp" field. We just set
372+ /// them to be monotonically increasing since we don't assume access to a time source.
373+ last_node_announcement_serial : AtomicUsize ,
374+
371375 /// The bulk of our storage will eventually be here (channels and message queues and the like).
372376 /// If we are connected to a peer we always at least have an entry here, even if no channels
373377 /// are currently open with that peer.
@@ -665,6 +669,8 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
665669 } ) ,
666670 our_network_key : keys_manager. get_node_secret ( ) ,
667671
672+ last_node_announcement_serial : AtomicUsize :: new ( 0 ) ,
673+
668674 per_peer_state : RwLock :: new ( HashMap :: new ( ) ) ,
669675
670676 pending_events : Mutex :: new ( Vec :: new ( ) ) ,
@@ -1333,6 +1339,39 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
13331339 } )
13341340 }
13351341
1342+ /// Generates a signed node_announcement from the given arguments and creates a
1343+ /// BroadcastNodeAnnouncement event.
1344+ ///
1345+ /// RGB is a node "color" and alias ia a printable human-readable string to describe this node
1346+ /// to humans. They carry no in-protocol meaning.
1347+ ///
1348+ /// addresses represent the set (possibly empty) of socket addresses on which this node accepts
1349+ /// incoming connections. These will be broadcast to the network, publicly tying these
1350+ /// addresses together. If you wish to preserve user privacy, addresses should likely contain
1351+ /// only Tor Onion addresses.
1352+ pub fn broadcast_node_announcement ( & self , rgb : [ u8 ; 3 ] , alias : [ u8 ; 32 ] , addresses : msgs:: NetAddressSet ) {
1353+ let _ = self . total_consistency_lock . read ( ) . unwrap ( ) ;
1354+
1355+ let announcement = msgs:: UnsignedNodeAnnouncement {
1356+ features : NodeFeatures :: supported ( ) ,
1357+ timestamp : self . last_node_announcement_serial . fetch_add ( 1 , Ordering :: AcqRel ) as u32 ,
1358+ node_id : self . get_our_node_id ( ) ,
1359+ rgb, alias,
1360+ addresses : addresses. into_vec ( ) ,
1361+ excess_address_data : Vec :: new ( ) ,
1362+ excess_data : Vec :: new ( ) ,
1363+ } ;
1364+ let msghash = hash_to_message ! ( & Sha256dHash :: hash( & announcement. encode( ) [ ..] ) [ ..] ) ;
1365+
1366+ let mut channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
1367+ channel_state. pending_msg_events . push ( events:: MessageSendEvent :: BroadcastNodeAnnouncement {
1368+ msg : msgs:: NodeAnnouncement {
1369+ signature : self . secp_ctx . sign ( & msghash, & self . our_network_key ) ,
1370+ contents : announcement
1371+ } ,
1372+ } ) ;
1373+ }
1374+
13361375 /// Processes HTLCs which are pending waiting on random forward delay.
13371376 ///
13381377 /// Should only really ever be called in response to a PendingHTLCsForwardable event.
@@ -2969,6 +3008,7 @@ impl<ChanSigner: ChannelKeys, M: Deref + Sync + Send, T: Deref + Sync + Send, K:
29693008 & events:: MessageSendEvent :: SendShutdown { ref node_id, .. } => node_id != their_node_id,
29703009 & events:: MessageSendEvent :: SendChannelReestablish { ref node_id, .. } => node_id != their_node_id,
29713010 & events:: MessageSendEvent :: BroadcastChannelAnnouncement { .. } => true ,
3011+ & events:: MessageSendEvent :: BroadcastNodeAnnouncement { .. } => true ,
29723012 & events:: MessageSendEvent :: BroadcastChannelUpdate { .. } => true ,
29733013 & events:: MessageSendEvent :: HandleError { ref node_id, .. } => node_id != their_node_id,
29743014 & events:: MessageSendEvent :: PaymentFailureNetworkUpdate { .. } => true ,
@@ -3287,6 +3327,8 @@ impl<ChanSigner: ChannelKeys + Writeable, M: Deref, T: Deref, K: Deref, F: Deref
32873327 peer_state. latest_features . write ( writer) ?;
32883328 }
32893329
3330+ ( self . last_node_announcement_serial . load ( Ordering :: Acquire ) as u32 ) . write ( writer) ?;
3331+
32903332 Ok ( ( ) )
32913333 }
32923334}
@@ -3443,6 +3485,8 @@ impl<'a, R : ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>, M: Deref, T
34433485 per_peer_state. insert ( peer_pubkey, Mutex :: new ( peer_state) ) ;
34443486 }
34453487
3488+ let last_node_announcement_serial: u32 = Readable :: read ( reader) ?;
3489+
34463490 let channel_manager = ChannelManager {
34473491 genesis_hash,
34483492 fee_estimator : args. fee_estimator ,
@@ -3462,6 +3506,8 @@ impl<'a, R : ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>, M: Deref, T
34623506 } ) ,
34633507 our_network_key : args. keys_manager . get_node_secret ( ) ,
34643508
3509+ last_node_announcement_serial : AtomicUsize :: new ( last_node_announcement_serial as usize ) ,
3510+
34653511 per_peer_state : RwLock :: new ( per_peer_state) ,
34663512
34673513 pending_events : Mutex :: new ( Vec :: new ( ) ) ,
0 commit comments