@@ -87,6 +87,7 @@ pub(crate) const WALLET_KEYS_SEED_LEN: usize = 64;
87
87
/// | `log_dir_path` | None |
88
88
/// | `network` | Bitcoin |
89
89
/// | `listening_addresses` | None |
90
+ /// | `node_alias` | None |
90
91
/// | `default_cltv_expiry_delta` | 144 |
91
92
/// | `onchain_wallet_sync_interval_secs` | 80 |
92
93
/// | `wallet_sync_interval_secs` | 30 |
@@ -112,9 +113,14 @@ pub struct Config {
112
113
pub network : Network ,
113
114
/// The addresses on which the node will listen for incoming connections.
114
115
///
115
- /// **Note**: Node announcements will only be broadcast if the node_alias and the
116
- /// listening_addresses are set.
116
+ /// **Note**: Node announcements will only be broadcast if the ` node_alias` and the
117
+ /// ` listening_addresses` are set.
117
118
pub listening_addresses : Option < Vec < SocketAddress > > ,
119
+ /// The node alias to be used in announcements.
120
+ ///
121
+ /// **Note**: Node announcements will only be broadcast if the `node_alias` and the
122
+ /// `listening_addresses` are set.
123
+ pub node_alias : Option < NodeAlias > ,
118
124
/// The time in-between background sync attempts of the onchain wallet, in seconds.
119
125
///
120
126
/// **Note:** A minimum of 10 seconds is always enforced.
@@ -167,11 +173,6 @@ pub struct Config {
167
173
/// **Note:** If unset, default parameters will be used, and you will be able to override the
168
174
/// parameters on a per-payment basis in the corresponding method calls.
169
175
pub sending_parameters : Option < SendingParameters > ,
170
- /// The node alias to be used in announcements.
171
- ///
172
- /// **Note**: Node announcements will only be broadcast if the node_alias and the
173
- /// listening_addresses are set.
174
- pub node_alias : Option < NodeAlias > ,
175
176
}
176
177
177
178
impl Default for Config {
@@ -275,33 +276,68 @@ pub fn default_config() -> Config {
275
276
Config :: default ( )
276
277
}
277
278
279
+ /// Specifies reasons why a channel cannot be announced.
280
+ #[ derive( Debug , PartialEq ) ]
281
+ pub ( crate ) enum ChannelAnnouncementBlocker {
282
+ /// The node alias is not set.
283
+ MissingNodeAlias ,
284
+ /// The listening addresses are not set.
285
+ MissingListeningAddresses ,
286
+ // This listening addresses is set but the vector is empty.
287
+ EmptyListeningAddresses ,
288
+ }
289
+
290
+ /// Enumeration defining the announcement status of a channel.
291
+ #[ derive( Debug , PartialEq ) ]
292
+ pub ( crate ) enum ChannelAnnouncementStatus {
293
+ /// The channel is announceable.
294
+ Announceable ,
295
+ /// The channel is not announceable.
296
+ Unannounceable ( ChannelAnnouncementBlocker ) ,
297
+ }
298
+
278
299
/// Checks if a node is can announce a channel based on the configured values of both the node's
279
- /// alias and its listening addresses. If either of them is unset, the node cannot announce the
280
- /// channel.
281
- pub fn can_announce_channel ( config : & Config ) -> bool {
282
- let are_addresses_set =
283
- config. listening_addresses . clone ( ) . map_or ( false , |addr_vec| !addr_vec. is_empty ( ) ) ;
284
- let is_alias_set = config. node_alias . is_some ( ) ;
285
-
286
- is_alias_set && are_addresses_set
300
+ /// alias and its listening addresses.
301
+ ///
302
+ /// If either of them is unset, the node cannot announce the channel. This ability to announce/
303
+ /// unannounce a channel is codified with `ChannelAnnouncementStatus`
304
+ pub ( crate ) fn can_announce_channel ( config : & Config ) -> ChannelAnnouncementStatus {
305
+ if config. node_alias . is_none ( ) {
306
+ return ChannelAnnouncementStatus :: Unannounceable (
307
+ ChannelAnnouncementBlocker :: MissingNodeAlias ,
308
+ ) ;
309
+ }
310
+
311
+ match & config. listening_addresses {
312
+ None => ChannelAnnouncementStatus :: Unannounceable (
313
+ ChannelAnnouncementBlocker :: MissingListeningAddresses ,
314
+ ) ,
315
+ Some ( addresses) if addresses. is_empty ( ) => ChannelAnnouncementStatus :: Unannounceable (
316
+ ChannelAnnouncementBlocker :: EmptyListeningAddresses ,
317
+ ) ,
318
+ Some ( _) => ChannelAnnouncementStatus :: Announceable ,
319
+ }
287
320
}
288
321
289
322
pub ( crate ) fn default_user_config ( config : & Config ) -> UserConfig {
290
323
// Initialize the default config values.
291
324
//
292
- // Note that methods such as Node::connect_open_channel might override some of the values set
293
- // here, e.g. the ChannelHandshakeConfig, meaning these default values will mostly be relevant
294
- // for inbound channels.
325
+ // Note that methods such as Node::open_channel and Node::open_announced_channel might override
326
+ // some of the values set here, e.g. the ChannelHandshakeConfig, meaning these default values
327
+ // will mostly be relevant for inbound channels.
295
328
let mut user_config = UserConfig :: default ( ) ;
296
329
user_config. channel_handshake_limits . force_announced_channel_preference = false ;
297
330
user_config. manually_accept_inbound_channels = true ;
298
331
user_config. channel_handshake_config . negotiate_anchors_zero_fee_htlc_tx =
299
332
config. anchor_channels_config . is_some ( ) ;
300
333
301
- if !can_announce_channel ( config) {
302
- user_config. accept_forwards_to_priv_channels = false ;
303
- user_config. channel_handshake_config . announced_channel = false ;
304
- user_config. channel_handshake_limits . force_announced_channel_preference = true ;
334
+ match can_announce_channel ( config) {
335
+ ChannelAnnouncementStatus :: Announceable => ( ) ,
336
+ ChannelAnnouncementStatus :: Unannounceable ( _) => {
337
+ user_config. accept_forwards_to_priv_channels = false ;
338
+ user_config. channel_handshake_config . announced_channel = false ;
339
+ user_config. channel_handshake_limits . force_announced_channel_preference = true ;
340
+ } ,
305
341
}
306
342
307
343
user_config
@@ -311,17 +347,23 @@ pub(crate) fn default_user_config(config: &Config) -> UserConfig {
311
347
mod tests {
312
348
use std:: str:: FromStr ;
313
349
314
- use lightning:: { ln:: msgs:: SocketAddress , routing:: gossip:: NodeAlias } ;
315
-
316
- use crate :: config:: can_announce_channel;
350
+ use crate :: config:: ChannelAnnouncementStatus ;
317
351
352
+ use super :: can_announce_channel;
318
353
use super :: Config ;
354
+ use super :: NodeAlias ;
355
+ use super :: SocketAddress ;
319
356
320
357
#[ test]
321
358
fn node_can_announce_channel ( ) {
322
359
// Default configuration with node alias and listening addresses unset
323
360
let mut node_config = Config :: default ( ) ;
324
- assert_eq ! ( can_announce_channel( & node_config) , false ) ;
361
+ assert_eq ! (
362
+ can_announce_channel( & node_config) ,
363
+ ChannelAnnouncementStatus :: Unannounceable (
364
+ crate :: config:: ChannelAnnouncementBlocker :: MissingNodeAlias
365
+ )
366
+ ) ;
325
367
326
368
// Set node alias with listening addresses unset
327
369
let alias_frm_str = |alias : & str | {
@@ -330,18 +372,28 @@ mod tests {
330
372
NodeAlias ( bytes)
331
373
} ;
332
374
node_config. node_alias = Some ( alias_frm_str ( "LDK_Node" ) ) ;
333
- assert_eq ! ( can_announce_channel( & node_config) , false ) ;
375
+ assert_eq ! (
376
+ can_announce_channel( & node_config) ,
377
+ ChannelAnnouncementStatus :: Unannounceable (
378
+ crate :: config:: ChannelAnnouncementBlocker :: MissingListeningAddresses
379
+ )
380
+ ) ;
334
381
335
382
// Set node alias with an empty list of listening addresses
336
383
node_config. listening_addresses = Some ( vec ! [ ] ) ;
337
- assert_eq ! ( can_announce_channel( & node_config) , false ) ;
384
+ assert_eq ! (
385
+ can_announce_channel( & node_config) ,
386
+ ChannelAnnouncementStatus :: Unannounceable (
387
+ crate :: config:: ChannelAnnouncementBlocker :: EmptyListeningAddresses
388
+ )
389
+ ) ;
338
390
339
391
// Set node alias with a non-empty list of listening addresses
340
392
let socket_address =
341
393
SocketAddress :: from_str ( "localhost:8000" ) . expect ( "Socket address conversion failed." ) ;
342
394
if let Some ( ref mut addresses) = node_config. listening_addresses {
343
395
addresses. push ( socket_address) ;
344
396
}
345
- assert_eq ! ( can_announce_channel( & node_config) , true ) ;
397
+ assert_eq ! ( can_announce_channel( & node_config) , ChannelAnnouncementStatus :: Announceable ) ;
346
398
}
347
399
}
0 commit comments