@@ -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 } ;
@@ -355,6 +355,10 @@ pub struct ChannelManager<ChanSigner: ChannelKeys, M: Deref> where M::Target: Ma
355355 channel_state : Mutex < ChannelHolder < ChanSigner > > ,
356356 our_network_key : SecretKey ,
357357
358+ /// Used to track the last value sent in a node_announcement "timestamp" field. We just set
359+ /// them to be monotonically increasing since we don't assume access to a time source.
360+ last_node_announcement_serial : AtomicUsize ,
361+
358362 /// The bulk of our storage will eventually be here (channels and message queues and the like).
359363 /// If we are connected to a peer we always at least have an entry here, even if no channels
360364 /// are currently open with that peer.
@@ -649,6 +653,8 @@ impl<ChanSigner: ChannelKeys, M: Deref> ChannelManager<ChanSigner, M> where M::T
649653 } ) ,
650654 our_network_key : keys_manager. get_node_secret ( ) ,
651655
656+ last_node_announcement_serial : AtomicUsize :: new ( 0 ) ,
657+
652658 per_peer_state : RwLock :: new ( HashMap :: new ( ) ) ,
653659
654660 pending_events : Mutex :: new ( Vec :: new ( ) ) ,
@@ -1317,6 +1323,39 @@ impl<ChanSigner: ChannelKeys, M: Deref> ChannelManager<ChanSigner, M> where M::T
13171323 } )
13181324 }
13191325
1326+ /// Generates a signed node_announcement from the given arguments and creates a
1327+ /// BroadcastNodeAnnouncement event.
1328+ ///
1329+ /// RGB is a node "color" and alias a printable human-readable string to describe this node to
1330+ /// humans. They carry no in-protocol meaning.
1331+ ///
1332+ /// addresses represent the set (possibly empty) of socket addresses on which this node accepts
1333+ /// incoming connections. These will be broadcast to the network, publicly tying these
1334+ /// addresses together. If you wish to preserve user privacy, addresses should likely contain
1335+ /// only Tor Onion addresses.
1336+ pub fn broadcast_node_announcement ( & self , rgb : [ u8 ; 3 ] , alias : [ u8 ; 32 ] , addresses : msgs:: NetAddressSet ) {
1337+ let _ = self . total_consistency_lock . read ( ) . unwrap ( ) ;
1338+
1339+ let announcement = msgs:: UnsignedNodeAnnouncement {
1340+ features : NodeFeatures :: supported ( ) ,
1341+ timestamp : self . last_node_announcement_serial . fetch_add ( 1 , Ordering :: AcqRel ) as u32 ,
1342+ node_id : self . get_our_node_id ( ) ,
1343+ rgb, alias,
1344+ addresses : addresses. to_vec ( ) ,
1345+ excess_address_data : Vec :: new ( ) ,
1346+ excess_data : Vec :: new ( ) ,
1347+ } ;
1348+ let msghash = hash_to_message ! ( & Sha256dHash :: hash( & announcement. encode( ) [ ..] ) [ ..] ) ;
1349+
1350+ let mut channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
1351+ channel_state. pending_msg_events . push ( events:: MessageSendEvent :: BroadcastNodeAnnouncement {
1352+ msg : msgs:: NodeAnnouncement {
1353+ signature : self . secp_ctx . sign ( & msghash, & self . our_network_key ) ,
1354+ contents : announcement
1355+ } ,
1356+ } ) ;
1357+ }
1358+
13201359 /// Processes HTLCs which are pending waiting on random forward delay.
13211360 ///
13221361 /// Should only really ever be called in response to a PendingHTLCsForwardable event.
@@ -2918,6 +2957,7 @@ impl<ChanSigner: ChannelKeys, M: Deref + Sync + Send> ChannelMessageHandler for
29182957 & events:: MessageSendEvent :: SendShutdown { ref node_id, .. } => node_id != their_node_id,
29192958 & events:: MessageSendEvent :: SendChannelReestablish { ref node_id, .. } => node_id != their_node_id,
29202959 & events:: MessageSendEvent :: BroadcastChannelAnnouncement { .. } => true ,
2960+ & events:: MessageSendEvent :: BroadcastNodeAnnouncement { .. } => true ,
29212961 & events:: MessageSendEvent :: BroadcastChannelUpdate { .. } => true ,
29222962 & events:: MessageSendEvent :: HandleError { ref node_id, .. } => node_id != their_node_id,
29232963 & events:: MessageSendEvent :: PaymentFailureNetworkUpdate { .. } => true ,
@@ -3231,6 +3271,8 @@ impl<ChanSigner: ChannelKeys + Writeable, M: Deref> Writeable for ChannelManager
32313271 peer_state. latest_features . write ( writer) ?;
32323272 }
32333273
3274+ ( self . last_node_announcement_serial . load ( Ordering :: Acquire ) as u32 ) . write ( writer) ?;
3275+
32343276 Ok ( ( ) )
32353277 }
32363278}
@@ -3374,6 +3416,8 @@ impl<'a, R : ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>, M: Deref> R
33743416 per_peer_state. insert ( peer_pubkey, Mutex :: new ( peer_state) ) ;
33753417 }
33763418
3419+ let last_node_announcement_serial: u32 = Readable :: read ( reader) ?;
3420+
33773421 let channel_manager = ChannelManager {
33783422 genesis_hash,
33793423 fee_estimator : args. fee_estimator ,
@@ -3393,6 +3437,8 @@ impl<'a, R : ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>, M: Deref> R
33933437 } ) ,
33943438 our_network_key : args. keys_manager . get_node_secret ( ) ,
33953439
3440+ last_node_announcement_serial : AtomicUsize :: new ( last_node_announcement_serial as usize ) ,
3441+
33963442 per_peer_state : RwLock :: new ( per_peer_state) ,
33973443
33983444 pending_events : Mutex :: new ( Vec :: new ( ) ) ,
0 commit comments