@@ -19,6 +19,7 @@ use lightning::util::config::UserConfig;
1919use bitcoin:: secp256k1:: PublicKey ;
2020use bitcoin:: Network ;
2121
22+ use std:: fmt;
2223use std:: time:: Duration ;
2324
2425// Config defaults
@@ -263,9 +264,37 @@ pub fn default_config() -> Config {
263264 Config :: default ( )
264265}
265266
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+ }
269298}
270299
271300pub ( crate ) fn default_user_config ( config : & Config ) -> UserConfig {
@@ -280,7 +309,7 @@ pub(crate) fn default_user_config(config: &Config) -> UserConfig {
280309 user_config. channel_handshake_config . negotiate_anchors_zero_fee_htlc_tx =
281310 config. anchor_channels_config . is_some ( ) ;
282311
283- if ! may_announce_channel ( config) {
312+ if may_announce_channel ( config) . is_err ( ) {
284313 user_config. accept_forwards_to_priv_channels = false ;
285314 user_config. channel_handshake_config . announce_for_forwarding = false ;
286315 user_config. channel_handshake_limits . force_announced_channel_preference = true ;
@@ -468,6 +497,7 @@ mod tests {
468497 use std:: str:: FromStr ;
469498
470499 use super :: may_announce_channel;
500+ use super :: AnnounceError ;
471501 use super :: Config ;
472502 use super :: NodeAlias ;
473503 use super :: SocketAddress ;
@@ -476,7 +506,10 @@ mod tests {
476506 fn node_announce_channel ( ) {
477507 // Default configuration with node alias and listening addresses unset
478508 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+ ) ;
480513
481514 // Set node alias with listening addresses unset
482515 let alias_frm_str = |alias : & str | {
@@ -485,24 +518,33 @@ mod tests {
485518 NodeAlias ( bytes)
486519 } ;
487520 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+ ) ;
489525
490526 // Set announcement addresses with listening addresses unset
491527 let announcement_address = SocketAddress :: from_str ( "123.45.67.89:9735" )
492528 . expect ( "Socket address conversion failed." ) ;
493529 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+ ) ;
495534
496535 // Set node alias with an empty list of listening addresses
497536 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+ ) ;
499541
500542 // Set node alias with a non-empty list of listening addresses
501543 let socket_address =
502544 SocketAddress :: from_str ( "localhost:8000" ) . expect ( "Socket address conversion failed." ) ;
503545 if let Some ( ref mut addresses) = node_config. listening_addresses {
504546 addresses. push ( socket_address) ;
505547 }
506- assert ! ( may_announce_channel( & node_config) ) ;
548+ assert ! ( may_announce_channel( & node_config) . is_ok ( ) ) ;
507549 }
508550}
0 commit comments