@@ -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
@@ -263,9 +264,37 @@ pub fn default_config() -> Config {
263
264
Config :: default ( )
264
265
}
265
266
266
- pub ( crate ) fn may_announce_channel ( config : & Config ) -> bool {
267
- config. node_alias . is_some ( )
268
- && config. listening_addresses . as_ref ( ) . map_or ( false , |addrs| !addrs. is_empty ( ) )
267
+ #[ derive( Debug , PartialEq ) ]
268
+ pub ( crate ) enum AnnounceError {
269
+ MissingNodeAlias ,
270
+ MissingListeningAddresses ,
271
+ MissingAliasAndAddresses ,
272
+ }
273
+
274
+ impl fmt:: Display for AnnounceError {
275
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
276
+ match self {
277
+ AnnounceError :: MissingNodeAlias => write ! ( f, "Node alias is not configured" ) ,
278
+ AnnounceError :: MissingListeningAddresses => {
279
+ write ! ( f, "Listening addresses are not configured" )
280
+ } ,
281
+ AnnounceError :: MissingAliasAndAddresses => {
282
+ write ! ( f, "Node alias and listening addresses are not configured" )
283
+ } ,
284
+ }
285
+ }
286
+ }
287
+
288
+ pub ( crate ) fn may_announce_channel ( config : & Config ) -> Result < ( ) , AnnounceError > {
289
+ let has_listening_addresses =
290
+ config. listening_addresses . as_ref ( ) . map_or ( false , |addrs| !addrs. is_empty ( ) ) ;
291
+
292
+ match ( config. node_alias . is_some ( ) , has_listening_addresses) {
293
+ ( true , true ) => Ok ( ( ) ) ,
294
+ ( true , false ) => Err ( AnnounceError :: MissingListeningAddresses ) ,
295
+ ( false , true ) => Err ( AnnounceError :: MissingNodeAlias ) ,
296
+ ( false , false ) => Err ( AnnounceError :: MissingAliasAndAddresses ) ,
297
+ }
269
298
}
270
299
271
300
pub ( crate ) fn default_user_config ( config : & Config ) -> UserConfig {
@@ -280,7 +309,7 @@ pub(crate) fn default_user_config(config: &Config) -> UserConfig {
280
309
user_config. channel_handshake_config . negotiate_anchors_zero_fee_htlc_tx =
281
310
config. anchor_channels_config . is_some ( ) ;
282
311
283
- if ! may_announce_channel ( config) {
312
+ if may_announce_channel ( config) . is_err ( ) {
284
313
user_config. accept_forwards_to_priv_channels = false ;
285
314
user_config. channel_handshake_config . announce_for_forwarding = false ;
286
315
user_config. channel_handshake_limits . force_announced_channel_preference = true ;
@@ -468,6 +497,7 @@ mod tests {
468
497
use std:: str:: FromStr ;
469
498
470
499
use super :: may_announce_channel;
500
+ use super :: AnnounceError ;
471
501
use super :: Config ;
472
502
use super :: NodeAlias ;
473
503
use super :: SocketAddress ;
@@ -476,7 +506,10 @@ mod tests {
476
506
fn node_announce_channel ( ) {
477
507
// Default configuration with node alias and listening addresses unset
478
508
let mut node_config = Config :: default ( ) ;
479
- assert ! ( !may_announce_channel( & node_config) ) ;
509
+ assert_eq ! (
510
+ may_announce_channel( & node_config) ,
511
+ Err ( AnnounceError :: MissingAliasAndAddresses )
512
+ ) ;
480
513
481
514
// Set node alias with listening addresses unset
482
515
let alias_frm_str = |alias : & str | {
@@ -485,24 +518,33 @@ mod tests {
485
518
NodeAlias ( bytes)
486
519
} ;
487
520
node_config. node_alias = Some ( alias_frm_str ( "LDK_Node" ) ) ;
488
- assert ! ( !may_announce_channel( & node_config) ) ;
521
+ assert_eq ! (
522
+ may_announce_channel( & node_config) ,
523
+ Err ( AnnounceError :: MissingListeningAddresses )
524
+ ) ;
489
525
490
526
// Set announcement addresses with listening addresses unset
491
527
let announcement_address = SocketAddress :: from_str ( "123.45.67.89:9735" )
492
528
. expect ( "Socket address conversion failed." ) ;
493
529
node_config. announcement_addresses = Some ( vec ! [ announcement_address] ) ;
494
- assert ! ( !may_announce_channel( & node_config) ) ;
530
+ assert_eq ! (
531
+ may_announce_channel( & node_config) ,
532
+ Err ( AnnounceError :: MissingListeningAddresses )
533
+ ) ;
495
534
496
535
// Set node alias with an empty list of listening addresses
497
536
node_config. listening_addresses = Some ( vec ! [ ] ) ;
498
- assert ! ( !may_announce_channel( & node_config) ) ;
537
+ assert_eq ! (
538
+ may_announce_channel( & node_config) ,
539
+ Err ( AnnounceError :: MissingListeningAddresses )
540
+ ) ;
499
541
500
542
// Set node alias with a non-empty list of listening addresses
501
543
let socket_address =
502
544
SocketAddress :: from_str ( "localhost:8000" ) . expect ( "Socket address conversion failed." ) ;
503
545
if let Some ( ref mut addresses) = node_config. listening_addresses {
504
546
addresses. push ( socket_address) ;
505
547
}
506
- assert ! ( may_announce_channel( & node_config) ) ;
548
+ assert ! ( may_announce_channel( & node_config) . is_ok ( ) ) ;
507
549
}
508
550
}
0 commit comments