@@ -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 ensure this
372+ /// value increases strictly 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 ( ) ) ,
@@ -1334,6 +1340,40 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
13341340 } )
13351341 }
13361342
1343+ /// Generates a signed node_announcement from the given arguments and creates a
1344+ /// BroadcastNodeAnnouncement event. Note that such messages will be ignored unless peers have
1345+ /// seen a channel_announcement from us (ie unless we have public channels open).
1346+ ///
1347+ /// RGB is a node "color" and alias is a printable human-readable string to describe this node
1348+ /// to humans. They carry no in-protocol meaning.
1349+ ///
1350+ /// addresses represent the set (possibly empty) of socket addresses on which this node accepts
1351+ /// incoming connections. These will be broadcast to the network, publicly tying these
1352+ /// addresses together. If you wish to preserve user privacy, addresses should likely contain
1353+ /// only Tor Onion addresses.
1354+ pub fn broadcast_node_announcement ( & self , rgb : [ u8 ; 3 ] , alias : [ u8 ; 32 ] , addresses : msgs:: NetAddressSet ) {
1355+ let _ = self . total_consistency_lock . read ( ) . unwrap ( ) ;
1356+
1357+ let announcement = msgs:: UnsignedNodeAnnouncement {
1358+ features : NodeFeatures :: supported ( ) ,
1359+ timestamp : self . last_node_announcement_serial . fetch_add ( 1 , Ordering :: AcqRel ) as u32 ,
1360+ node_id : self . get_our_node_id ( ) ,
1361+ rgb, alias,
1362+ addresses : addresses. into_vec ( ) ,
1363+ excess_address_data : Vec :: new ( ) ,
1364+ excess_data : Vec :: new ( ) ,
1365+ } ;
1366+ let msghash = hash_to_message ! ( & Sha256dHash :: hash( & announcement. encode( ) [ ..] ) [ ..] ) ;
1367+
1368+ let mut channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
1369+ channel_state. pending_msg_events . push ( events:: MessageSendEvent :: BroadcastNodeAnnouncement {
1370+ msg : msgs:: NodeAnnouncement {
1371+ signature : self . secp_ctx . sign ( & msghash, & self . our_network_key ) ,
1372+ contents : announcement
1373+ } ,
1374+ } ) ;
1375+ }
1376+
13371377 /// Processes HTLCs which are pending waiting on random forward delay.
13381378 ///
13391379 /// Should only really ever be called in response to a PendingHTLCsForwardable event.
@@ -2970,6 +3010,7 @@ impl<ChanSigner: ChannelKeys, M: Deref + Sync + Send, T: Deref + Sync + Send, K:
29703010 & events:: MessageSendEvent :: SendShutdown { ref node_id, .. } => node_id != their_node_id,
29713011 & events:: MessageSendEvent :: SendChannelReestablish { ref node_id, .. } => node_id != their_node_id,
29723012 & events:: MessageSendEvent :: BroadcastChannelAnnouncement { .. } => true ,
3013+ & events:: MessageSendEvent :: BroadcastNodeAnnouncement { .. } => true ,
29733014 & events:: MessageSendEvent :: BroadcastChannelUpdate { .. } => true ,
29743015 & events:: MessageSendEvent :: HandleError { ref node_id, .. } => node_id != their_node_id,
29753016 & events:: MessageSendEvent :: PaymentFailureNetworkUpdate { .. } => true ,
@@ -3288,6 +3329,8 @@ impl<ChanSigner: ChannelKeys + Writeable, M: Deref, T: Deref, K: Deref, F: Deref
32883329 peer_state. latest_features . write ( writer) ?;
32893330 }
32903331
3332+ ( self . last_node_announcement_serial . load ( Ordering :: Acquire ) as u32 ) . write ( writer) ?;
3333+
32913334 Ok ( ( ) )
32923335 }
32933336}
@@ -3459,6 +3502,8 @@ impl<'a, ChanSigner: ChannelKeys + Readable, M: Deref, T: Deref, K: Deref, F: De
34593502 per_peer_state. insert ( peer_pubkey, Mutex :: new ( peer_state) ) ;
34603503 }
34613504
3505+ let last_node_announcement_serial: u32 = Readable :: read ( reader) ?;
3506+
34623507 let channel_manager = ChannelManager {
34633508 genesis_hash,
34643509 fee_estimator : args. fee_estimator ,
@@ -3478,6 +3523,8 @@ impl<'a, ChanSigner: ChannelKeys + Readable, M: Deref, T: Deref, K: Deref, F: De
34783523 } ) ,
34793524 our_network_key : args. keys_manager . get_node_secret ( ) ,
34803525
3526+ last_node_announcement_serial : AtomicUsize :: new ( last_node_announcement_serial as usize ) ,
3527+
34813528 per_peer_state : RwLock :: new ( per_peer_state) ,
34823529
34833530 pending_events : Mutex :: new ( Vec :: new ( ) ) ,
0 commit comments