@@ -29,8 +29,8 @@ use chain::chaininterface::{BroadcasterInterface,ChainListener,FeeEstimator};
29
29
use chain:: transaction:: OutPoint ;
30
30
use ln:: channel:: { Channel , ChannelError } ;
31
31
use ln:: channelmonitor:: { ChannelMonitor , ChannelMonitorUpdateErr , ManyChannelMonitor , CLTV_CLAIM_BUFFER , LATENCY_GRACE_PERIOD_BLOCKS , ANTI_REORG_DELAY } ;
32
+ use ln:: features:: { InitFeatures , NodeFeatures } ;
32
33
use ln:: router:: Route ;
33
- use ln:: features:: InitFeatures ;
34
34
use ln:: msgs;
35
35
use ln:: onion_utils;
36
36
use ln:: msgs:: { ChannelMessageHandler , DecodeError , LightningError } ;
@@ -368,6 +368,10 @@ pub struct ChannelManager<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref,
368
368
channel_state : Mutex < ChannelHolder < ChanSigner > > ,
369
369
our_network_key : SecretKey ,
370
370
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
+
371
375
/// The bulk of our storage will eventually be here (channels and message queues and the like).
372
376
/// If we are connected to a peer we always at least have an entry here, even if no channels
373
377
/// are currently open with that peer.
@@ -665,6 +669,8 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
665
669
} ) ,
666
670
our_network_key : keys_manager. get_node_secret ( ) ,
667
671
672
+ last_node_announcement_serial : AtomicUsize :: new ( 0 ) ,
673
+
668
674
per_peer_state : RwLock :: new ( HashMap :: new ( ) ) ,
669
675
670
676
pending_events : Mutex :: new ( Vec :: new ( ) ) ,
@@ -1118,7 +1124,7 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
1118
1124
let unsigned = msgs:: UnsignedChannelUpdate {
1119
1125
chain_hash : self . genesis_hash ,
1120
1126
short_channel_id : short_channel_id,
1121
- timestamp : chan. get_channel_update_count ( ) ,
1127
+ timestamp : chan. get_update_time_counter ( ) ,
1122
1128
flags : ( !were_node_one) as u16 | ( ( !chan. is_live ( ) as u16 ) << 1 ) ,
1123
1129
cltv_expiry_delta : CLTV_EXPIRY_DELTA ,
1124
1130
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
1334
1340
} )
1335
1341
}
1336
1342
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
+
1337
1394
/// Processes HTLCs which are pending waiting on random forward delay.
1338
1395
///
1339
1396
/// 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:
2719
2776
}
2720
2777
self . latest_block_height . store ( height as usize , Ordering :: Release ) ;
2721
2778
* 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
+ }
2722
2791
}
2723
2792
2724
2793
/// 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:
2970
3039
& events:: MessageSendEvent :: SendShutdown { ref node_id, .. } => node_id != their_node_id,
2971
3040
& events:: MessageSendEvent :: SendChannelReestablish { ref node_id, .. } => node_id != their_node_id,
2972
3041
& events:: MessageSendEvent :: BroadcastChannelAnnouncement { .. } => true ,
3042
+ & events:: MessageSendEvent :: BroadcastNodeAnnouncement { .. } => true ,
2973
3043
& events:: MessageSendEvent :: BroadcastChannelUpdate { .. } => true ,
2974
3044
& events:: MessageSendEvent :: HandleError { ref node_id, .. } => node_id != their_node_id,
2975
3045
& events:: MessageSendEvent :: PaymentFailureNetworkUpdate { .. } => true ,
@@ -3288,6 +3358,8 @@ impl<ChanSigner: ChannelKeys + Writeable, M: Deref, T: Deref, K: Deref, F: Deref
3288
3358
peer_state. latest_features . write ( writer) ?;
3289
3359
}
3290
3360
3361
+ ( self . last_node_announcement_serial . load ( Ordering :: Acquire ) as u32 ) . write ( writer) ?;
3362
+
3291
3363
Ok ( ( ) )
3292
3364
}
3293
3365
}
@@ -3459,6 +3531,8 @@ impl<'a, ChanSigner: ChannelKeys + Readable, M: Deref, T: Deref, K: Deref, F: De
3459
3531
per_peer_state. insert ( peer_pubkey, Mutex :: new ( peer_state) ) ;
3460
3532
}
3461
3533
3534
+ let last_node_announcement_serial: u32 = Readable :: read ( reader) ?;
3535
+
3462
3536
let channel_manager = ChannelManager {
3463
3537
genesis_hash,
3464
3538
fee_estimator : args. fee_estimator ,
@@ -3478,6 +3552,8 @@ impl<'a, ChanSigner: ChannelKeys + Readable, M: Deref, T: Deref, K: Deref, F: De
3478
3552
} ) ,
3479
3553
our_network_key : args. keys_manager . get_node_secret ( ) ,
3480
3554
3555
+ last_node_announcement_serial : AtomicUsize :: new ( last_node_announcement_serial as usize ) ,
3556
+
3481
3557
per_peer_state : RwLock :: new ( per_peer_state) ,
3482
3558
3483
3559
pending_events : Mutex :: new ( Vec :: new ( ) ) ,
0 commit comments