@@ -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 ( ) ) ,
@@ -1334,6 +1340,50 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
1334
1340
} )
1335
1341
}
1336
1342
1343
+ #[ allow( dead_code) ]
1344
+ const HALF_MESSAGE_IS_ADDRS : u32 = 64 * 1024 / ( msgs:: NetAddress :: MAX_LEN as u32 + 1 ) / 2 ;
1345
+ #[ deny( const_err) ]
1346
+ #[ allow( dead_code) ]
1347
+ const STATIC_ASSERT : u32 = Self :: HALF_MESSAGE_IS_ADDRS - 500 ; // This will fail to compile if we use half of the message with 500 addresses
1348
+ /// Generates a signed node_announcement from the given arguments and creates a
1349
+ /// BroadcastNodeAnnouncement event. Note that such messages will be ignored unless peers have
1350
+ /// seen a channel_announcement from us (ie unless we have public channels open).
1351
+ ///
1352
+ /// RGB is a node "color" and alias is a printable human-readable string to describe this node
1353
+ /// to humans. They carry no in-protocol meaning.
1354
+ ///
1355
+ /// addresses represent the set (possibly empty) of socket addresses on which this node accepts
1356
+ /// incoming connections. These will be broadcast to the network, publicly tying these
1357
+ /// addresses together. If you wish to preserve user privacy, addresses should likely contain
1358
+ /// only Tor Onion addresses.
1359
+ ///
1360
+ /// Panics if addresses is absurdly large (more than 500).
1361
+ pub fn broadcast_node_announcement ( & self , rgb : [ u8 ; 3 ] , alias : [ u8 ; 32 ] , addresses : Vec < msgs:: NetAddress > ) {
1362
+ let _ = self . total_consistency_lock . read ( ) . unwrap ( ) ;
1363
+
1364
+ if addresses. len ( ) > 500 {
1365
+ panic ! ( "More than half the message size was taken up by public addresses!" ) ;
1366
+ }
1367
+
1368
+ let announcement = msgs:: UnsignedNodeAnnouncement {
1369
+ features : NodeFeatures :: supported ( ) ,
1370
+ timestamp : self . last_node_announcement_serial . fetch_add ( 1 , Ordering :: AcqRel ) as u32 ,
1371
+ node_id : self . get_our_node_id ( ) ,
1372
+ rgb, alias, addresses,
1373
+ excess_address_data : Vec :: new ( ) ,
1374
+ excess_data : Vec :: new ( ) ,
1375
+ } ;
1376
+ let msghash = hash_to_message ! ( & Sha256dHash :: hash( & announcement. encode( ) [ ..] ) [ ..] ) ;
1377
+
1378
+ let mut channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
1379
+ channel_state. pending_msg_events . push ( events:: MessageSendEvent :: BroadcastNodeAnnouncement {
1380
+ msg : msgs:: NodeAnnouncement {
1381
+ signature : self . secp_ctx . sign ( & msghash, & self . our_network_key ) ,
1382
+ contents : announcement
1383
+ } ,
1384
+ } ) ;
1385
+ }
1386
+
1337
1387
/// Processes HTLCs which are pending waiting on random forward delay.
1338
1388
///
1339
1389
/// Should only really ever be called in response to a PendingHTLCsForwardable event.
@@ -2970,6 +3020,7 @@ impl<ChanSigner: ChannelKeys, M: Deref + Sync + Send, T: Deref + Sync + Send, K:
2970
3020
& events:: MessageSendEvent :: SendShutdown { ref node_id, .. } => node_id != their_node_id,
2971
3021
& events:: MessageSendEvent :: SendChannelReestablish { ref node_id, .. } => node_id != their_node_id,
2972
3022
& events:: MessageSendEvent :: BroadcastChannelAnnouncement { .. } => true ,
3023
+ & events:: MessageSendEvent :: BroadcastNodeAnnouncement { .. } => true ,
2973
3024
& events:: MessageSendEvent :: BroadcastChannelUpdate { .. } => true ,
2974
3025
& events:: MessageSendEvent :: HandleError { ref node_id, .. } => node_id != their_node_id,
2975
3026
& events:: MessageSendEvent :: PaymentFailureNetworkUpdate { .. } => true ,
@@ -3288,6 +3339,8 @@ impl<ChanSigner: ChannelKeys + Writeable, M: Deref, T: Deref, K: Deref, F: Deref
3288
3339
peer_state. latest_features . write ( writer) ?;
3289
3340
}
3290
3341
3342
+ ( self . last_node_announcement_serial . load ( Ordering :: Acquire ) as u32 ) . write ( writer) ?;
3343
+
3291
3344
Ok ( ( ) )
3292
3345
}
3293
3346
}
@@ -3459,6 +3512,8 @@ impl<'a, ChanSigner: ChannelKeys + Readable, M: Deref, T: Deref, K: Deref, F: De
3459
3512
per_peer_state. insert ( peer_pubkey, Mutex :: new ( peer_state) ) ;
3460
3513
}
3461
3514
3515
+ let last_node_announcement_serial: u32 = Readable :: read ( reader) ?;
3516
+
3462
3517
let channel_manager = ChannelManager {
3463
3518
genesis_hash,
3464
3519
fee_estimator : args. fee_estimator ,
@@ -3478,6 +3533,8 @@ impl<'a, ChanSigner: ChannelKeys + Readable, M: Deref, T: Deref, K: Deref, F: De
3478
3533
} ) ,
3479
3534
our_network_key : args. keys_manager . get_node_secret ( ) ,
3480
3535
3536
+ last_node_announcement_serial : AtomicUsize :: new ( last_node_announcement_serial as usize ) ,
3537
+
3481
3538
per_peer_state : RwLock :: new ( per_peer_state) ,
3482
3539
3483
3540
pending_events : Mutex :: new ( Vec :: new ( ) ) ,
0 commit comments