@@ -19,6 +19,7 @@ use lightning::util::config::UserConfig;
19
19
use bitcoin:: secp256k1:: PublicKey ;
20
20
use bitcoin:: Network ;
21
21
22
+ use std:: fmt;
22
23
use std:: time:: Duration ;
23
24
24
25
// Config defaults
@@ -261,9 +262,37 @@ pub fn default_config() -> Config {
261
262
Config :: default ( )
262
263
}
263
264
264
- pub ( crate ) fn may_announce_channel ( config : & Config ) -> bool {
265
- config. node_alias . is_some ( )
266
- && config. listening_addresses . as_ref ( ) . map_or ( false , |addrs| !addrs. is_empty ( ) )
265
+ #[ derive( Debug , PartialEq ) ]
266
+ pub ( crate ) enum AnnounceError {
267
+ MissingNodeAlias ,
268
+ MissingListeningAddresses ,
269
+ MissingBoth ,
270
+ }
271
+
272
+ impl fmt:: Display for AnnounceError {
273
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
274
+ match self {
275
+ AnnounceError :: MissingNodeAlias => write ! ( f, "Node alias is not configured" ) ,
276
+ AnnounceError :: MissingListeningAddresses => {
277
+ write ! ( f, "Listening addresses are not configured" )
278
+ } ,
279
+ AnnounceError :: MissingBoth => {
280
+ write ! ( f, "Node alias and listening addresses are not configured" )
281
+ } ,
282
+ }
283
+ }
284
+ }
285
+
286
+ pub ( crate ) fn may_announce_channel ( config : & Config ) -> Result < ( ) , AnnounceError > {
287
+ let has_listening_addresses =
288
+ config. listening_addresses . as_ref ( ) . map_or ( false , |addrs| !addrs. is_empty ( ) ) ;
289
+
290
+ match ( config. node_alias . is_some ( ) , has_listening_addresses) {
291
+ ( true , true ) => Ok ( ( ) ) ,
292
+ ( true , false ) => Err ( AnnounceError :: MissingListeningAddresses ) ,
293
+ ( false , true ) => Err ( AnnounceError :: MissingNodeAlias ) ,
294
+ ( false , false ) => Err ( AnnounceError :: MissingBoth ) ,
295
+ }
267
296
}
268
297
269
298
pub ( crate ) fn default_user_config ( config : & Config ) -> UserConfig {
@@ -278,7 +307,7 @@ pub(crate) fn default_user_config(config: &Config) -> UserConfig {
278
307
user_config. channel_handshake_config . negotiate_anchors_zero_fee_htlc_tx =
279
308
config. anchor_channels_config . is_some ( ) ;
280
309
281
- if ! may_announce_channel ( config) {
310
+ if may_announce_channel ( config) . is_err ( ) {
282
311
user_config. accept_forwards_to_priv_channels = false ;
283
312
user_config. channel_handshake_config . announce_for_forwarding = false ;
284
313
user_config. channel_handshake_limits . force_announced_channel_preference = true ;
@@ -443,6 +472,7 @@ mod tests {
443
472
use std:: str:: FromStr ;
444
473
445
474
use super :: may_announce_channel;
475
+ use super :: AnnounceError ;
446
476
use super :: Config ;
447
477
use super :: NodeAlias ;
448
478
use super :: SocketAddress ;
@@ -451,7 +481,7 @@ mod tests {
451
481
fn node_announce_channel ( ) {
452
482
// Default configuration with node alias and listening addresses unset
453
483
let mut node_config = Config :: default ( ) ;
454
- assert ! ( ! may_announce_channel( & node_config) ) ;
484
+ assert_eq ! ( may_announce_channel( & node_config) , Err ( AnnounceError :: MissingBoth ) ) ;
455
485
456
486
// Set node alias with listening addresses unset
457
487
let alias_frm_str = |alias : & str | {
@@ -460,24 +490,33 @@ mod tests {
460
490
NodeAlias ( bytes)
461
491
} ;
462
492
node_config. node_alias = Some ( alias_frm_str ( "LDK_Node" ) ) ;
463
- assert ! ( !may_announce_channel( & node_config) ) ;
493
+ assert_eq ! (
494
+ may_announce_channel( & node_config) ,
495
+ Err ( AnnounceError :: MissingListeningAddresses )
496
+ ) ;
464
497
465
498
// Set announcement addresses with listening addresses unset
466
499
let announcement_address = SocketAddress :: from_str ( "123.45.67.89:9735" )
467
500
. expect ( "Socket address conversion failed." ) ;
468
501
node_config. announcement_addresses = Some ( vec ! [ announcement_address] ) ;
469
- assert ! ( !may_announce_channel( & node_config) ) ;
502
+ assert_eq ! (
503
+ may_announce_channel( & node_config) ,
504
+ Err ( AnnounceError :: MissingListeningAddresses )
505
+ ) ;
470
506
471
507
// Set node alias with an empty list of listening addresses
472
508
node_config. listening_addresses = Some ( vec ! [ ] ) ;
473
- assert ! ( !may_announce_channel( & node_config) ) ;
509
+ assert_eq ! (
510
+ may_announce_channel( & node_config) ,
511
+ Err ( AnnounceError :: MissingListeningAddresses )
512
+ ) ;
474
513
475
514
// Set node alias with a non-empty list of listening addresses
476
515
let socket_address =
477
516
SocketAddress :: from_str ( "localhost:8000" ) . expect ( "Socket address conversion failed." ) ;
478
517
if let Some ( ref mut addresses) = node_config. listening_addresses {
479
518
addresses. push ( socket_address) ;
480
519
}
481
- assert ! ( may_announce_channel( & node_config) ) ;
520
+ assert ! ( may_announce_channel( & node_config) . is_ok ( ) ) ;
482
521
}
483
522
}
0 commit comments