@@ -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
@@ -117,6 +118,12 @@ pub struct Config {
117118 /// **Note**: We will only allow opening and accepting public channels if the `node_alias` and the
118119 /// `listening_addresses` are set.
119120 pub listening_addresses : Option < Vec < SocketAddress > > ,
121+ /// The addresses which the node will announce to the gossip network that it accepts connections on.
122+ ///
123+ /// **Note**: If unset, the [`listening_addresses`] will be used as the list of addresses to announce.
124+ ///
125+ /// [`listening_addresses`]: Config::listening_addresses
126+ pub announcement_addresses : Option < Vec < SocketAddress > > ,
120127 /// The node alias that will be used when broadcasting announcements to the gossip network.
121128 ///
122129 /// The provided alias must be a valid UTF-8 string and no longer than 32 bytes in total.
@@ -168,6 +175,7 @@ impl Default for Config {
168175 storage_dir_path : DEFAULT_STORAGE_DIR_PATH . to_string ( ) ,
169176 network : DEFAULT_NETWORK ,
170177 listening_addresses : None ,
178+ announcement_addresses : None ,
171179 trusted_peers_0conf : Vec :: new ( ) ,
172180 probing_liquidity_limit_multiplier : DEFAULT_PROBING_LIQUIDITY_LIMIT_MULTIPLIER ,
173181 anchor_channels_config : Some ( AnchorChannelsConfig :: default ( ) ) ,
@@ -256,9 +264,37 @@ pub fn default_config() -> Config {
256264 Config :: default ( )
257265}
258266
259- pub ( crate ) fn may_announce_channel ( config : & Config ) -> bool {
260- config. node_alias . is_some ( )
261- && 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+ }
262298}
263299
264300pub ( crate ) fn default_user_config ( config : & Config ) -> UserConfig {
@@ -273,7 +309,7 @@ pub(crate) fn default_user_config(config: &Config) -> UserConfig {
273309 user_config. channel_handshake_config . negotiate_anchors_zero_fee_htlc_tx =
274310 config. anchor_channels_config . is_some ( ) ;
275311
276- if ! may_announce_channel ( config) {
312+ if may_announce_channel ( config) . is_err ( ) {
277313 user_config. accept_forwards_to_priv_channels = false ;
278314 user_config. channel_handshake_config . announce_for_forwarding = false ;
279315 user_config. channel_handshake_limits . force_announced_channel_preference = true ;
@@ -461,6 +497,7 @@ mod tests {
461497 use std:: str:: FromStr ;
462498
463499 use super :: may_announce_channel;
500+ use super :: AnnounceError ;
464501 use super :: Config ;
465502 use super :: NodeAlias ;
466503 use super :: SocketAddress ;
@@ -469,7 +506,10 @@ mod tests {
469506 fn node_announce_channel ( ) {
470507 // Default configuration with node alias and listening addresses unset
471508 let mut node_config = Config :: default ( ) ;
472- assert ! ( !may_announce_channel( & node_config) ) ;
509+ assert_eq ! (
510+ may_announce_channel( & node_config) ,
511+ Err ( AnnounceError :: MissingAliasAndAddresses )
512+ ) ;
473513
474514 // Set node alias with listening addresses unset
475515 let alias_frm_str = |alias : & str | {
@@ -478,18 +518,33 @@ mod tests {
478518 NodeAlias ( bytes)
479519 } ;
480520 node_config. node_alias = Some ( alias_frm_str ( "LDK_Node" ) ) ;
481- assert ! ( !may_announce_channel( & node_config) ) ;
521+ assert_eq ! (
522+ may_announce_channel( & node_config) ,
523+ Err ( AnnounceError :: MissingListeningAddresses )
524+ ) ;
525+
526+ // Set announcement addresses with listening addresses unset
527+ let announcement_address = SocketAddress :: from_str ( "123.45.67.89:9735" )
528+ . expect ( "Socket address conversion failed." ) ;
529+ node_config. announcement_addresses = Some ( vec ! [ announcement_address] ) ;
530+ assert_eq ! (
531+ may_announce_channel( & node_config) ,
532+ Err ( AnnounceError :: MissingListeningAddresses )
533+ ) ;
482534
483535 // Set node alias with an empty list of listening addresses
484536 node_config. listening_addresses = Some ( vec ! [ ] ) ;
485- assert ! ( !may_announce_channel( & node_config) ) ;
537+ assert_eq ! (
538+ may_announce_channel( & node_config) ,
539+ Err ( AnnounceError :: MissingListeningAddresses )
540+ ) ;
486541
487542 // Set node alias with a non-empty list of listening addresses
488543 let socket_address =
489544 SocketAddress :: from_str ( "localhost:8000" ) . expect ( "Socket address conversion failed." ) ;
490545 if let Some ( ref mut addresses) = node_config. listening_addresses {
491546 addresses. push ( socket_address) ;
492547 }
493- assert ! ( may_announce_channel( & node_config) ) ;
548+ assert ! ( may_announce_channel( & node_config) . is_ok ( ) ) ;
494549 }
495550}
0 commit comments