@@ -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 ( ) ) ,
@@ -1118,7 +1124,7 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
11181124 let unsigned = msgs:: UnsignedChannelUpdate {
11191125 chain_hash : self . genesis_hash ,
11201126 short_channel_id : short_channel_id,
1121- timestamp : chan. get_channel_update_count ( ) ,
1127+ timestamp : chan. get_update_time_counter ( ) ,
11221128 flags : ( !were_node_one) as u16 | ( ( !chan. is_live ( ) as u16 ) << 1 ) ,
11231129 cltv_expiry_delta : CLTV_EXPIRY_DELTA ,
11241130 htlc_minimum_msat : chan. get_our_htlc_minimum_msat ( ) ,
@@ -1334,6 +1340,57 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
13341340 } )
13351341 }
13361342
1343+ #[ allow( dead_code) ]
1344+ // Messages of up to 64KB should never end up more than half full with addresses, as that would
1345+ // be absurd. We ensure this by checking that at least 500 (our stated public contract on when
1346+ // broadcast_node_announcement panics) of the maximum-length addresses would fit in a 64KB
1347+ // message...
1348+ const HALF_MESSAGE_IS_ADDRS : u32 = :: std:: u16:: MAX as u32 / ( msgs:: NetAddress :: MAX_LEN as u32 + 1 ) / 2 ;
1349+ #[ deny( const_err) ]
1350+ #[ allow( dead_code) ]
1351+ // ...by failing to compile if the number of addresses that would be half of a message is
1352+ // smaller than 500:
1353+ const STATIC_ASSERT : u32 = Self :: HALF_MESSAGE_IS_ADDRS - 500 ;
1354+
1355+ /// Generates a signed node_announcement from the given arguments and creates a
1356+ /// BroadcastNodeAnnouncement event. Note that such messages will be ignored unless peers have
1357+ /// seen a channel_announcement from us (ie unless we have public channels open).
1358+ ///
1359+ /// RGB is a node "color" and alias is a printable human-readable string to describe this node
1360+ /// to humans. They carry no in-protocol meaning.
1361+ ///
1362+ /// addresses represent the set (possibly empty) of socket addresses on which this node accepts
1363+ /// incoming connections. These will be broadcast to the network, publicly tying these
1364+ /// addresses together. If you wish to preserve user privacy, addresses should likely contain
1365+ /// only Tor Onion addresses.
1366+ ///
1367+ /// Panics if addresses is absurdly large (more than 500).
1368+ pub fn broadcast_node_announcement ( & self , rgb : [ u8 ; 3 ] , alias : [ u8 ; 32 ] , addresses : Vec < msgs:: NetAddress > ) {
1369+ let _ = self . total_consistency_lock . read ( ) . unwrap ( ) ;
1370+
1371+ if addresses. len ( ) > 500 {
1372+ panic ! ( "More than half the message size was taken up by public addresses!" ) ;
1373+ }
1374+
1375+ let announcement = msgs:: UnsignedNodeAnnouncement {
1376+ features : NodeFeatures :: supported ( ) ,
1377+ timestamp : self . last_node_announcement_serial . fetch_add ( 1 , Ordering :: AcqRel ) as u32 ,
1378+ node_id : self . get_our_node_id ( ) ,
1379+ rgb, alias, addresses,
1380+ excess_address_data : Vec :: new ( ) ,
1381+ excess_data : Vec :: new ( ) ,
1382+ } ;
1383+ let msghash = hash_to_message ! ( & Sha256dHash :: hash( & announcement. encode( ) [ ..] ) [ ..] ) ;
1384+
1385+ let mut channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
1386+ channel_state. pending_msg_events . push ( events:: MessageSendEvent :: BroadcastNodeAnnouncement {
1387+ msg : msgs:: NodeAnnouncement {
1388+ signature : self . secp_ctx . sign ( & msghash, & self . our_network_key ) ,
1389+ contents : announcement
1390+ } ,
1391+ } ) ;
1392+ }
1393+
13371394 /// Processes HTLCs which are pending waiting on random forward delay.
13381395 ///
13391396 /// Should only really ever be called in response to a PendingHTLCsForwardable event.
@@ -2719,6 +2776,18 @@ impl<ChanSigner: ChannelKeys, M: Deref + Sync + Send, T: Deref + Sync + Send, K:
27192776 }
27202777 self . latest_block_height . store ( height as usize , Ordering :: Release ) ;
27212778 * self . last_block_hash . try_lock ( ) . expect ( "block_(dis)connected must not be called in parallel" ) = header_hash;
2779+ loop {
2780+ // Update last_node_announcement_serial to be the max of its current value and the
2781+ // block timestamp. This should keep us close to the current time without relying on
2782+ // having an explicit local time source.
2783+ // Just in case we end up in a race, we loop until we either successfully update
2784+ // last_node_announcement_serial or decide we don't need to.
2785+ let old_serial = self . last_node_announcement_serial . load ( Ordering :: Acquire ) ;
2786+ if old_serial >= header. time as usize { break ; }
2787+ if self . last_node_announcement_serial . compare_exchange ( old_serial, header. time as usize , Ordering :: AcqRel , Ordering :: Relaxed ) . is_ok ( ) {
2788+ break ;
2789+ }
2790+ }
27222791 }
27232792
27242793 /// We force-close the channel without letting our counterparty participate in the shutdown
@@ -2970,6 +3039,7 @@ impl<ChanSigner: ChannelKeys, M: Deref + Sync + Send, T: Deref + Sync + Send, K:
29703039 & events:: MessageSendEvent :: SendShutdown { ref node_id, .. } => node_id != their_node_id,
29713040 & events:: MessageSendEvent :: SendChannelReestablish { ref node_id, .. } => node_id != their_node_id,
29723041 & events:: MessageSendEvent :: BroadcastChannelAnnouncement { .. } => true ,
3042+ & events:: MessageSendEvent :: BroadcastNodeAnnouncement { .. } => true ,
29733043 & events:: MessageSendEvent :: BroadcastChannelUpdate { .. } => true ,
29743044 & events:: MessageSendEvent :: HandleError { ref node_id, .. } => node_id != their_node_id,
29753045 & events:: MessageSendEvent :: PaymentFailureNetworkUpdate { .. } => true ,
@@ -3288,6 +3358,8 @@ impl<ChanSigner: ChannelKeys + Writeable, M: Deref, T: Deref, K: Deref, F: Deref
32883358 peer_state. latest_features . write ( writer) ?;
32893359 }
32903360
3361+ ( self . last_node_announcement_serial . load ( Ordering :: Acquire ) as u32 ) . write ( writer) ?;
3362+
32913363 Ok ( ( ) )
32923364 }
32933365}
@@ -3459,6 +3531,8 @@ impl<'a, ChanSigner: ChannelKeys + Readable, M: Deref, T: Deref, K: Deref, F: De
34593531 per_peer_state. insert ( peer_pubkey, Mutex :: new ( peer_state) ) ;
34603532 }
34613533
3534+ let last_node_announcement_serial: u32 = Readable :: read ( reader) ?;
3535+
34623536 let channel_manager = ChannelManager {
34633537 genesis_hash,
34643538 fee_estimator : args. fee_estimator ,
@@ -3478,6 +3552,8 @@ impl<'a, ChanSigner: ChannelKeys + Readable, M: Deref, T: Deref, K: Deref, F: De
34783552 } ) ,
34793553 our_network_key : args. keys_manager . get_node_secret ( ) ,
34803554
3555+ last_node_announcement_serial : AtomicUsize :: new ( last_node_announcement_serial as usize ) ,
3556+
34813557 per_peer_state : RwLock :: new ( per_peer_state) ,
34823558
34833559 pending_events : Mutex :: new ( Vec :: new ( ) ) ,
0 commit comments