Skip to content

Commit bb83948

Browse files
committed
introduce AnnounceError to know why we can't announce
1 parent e0e05d5 commit bb83948

File tree

4 files changed

+100
-49
lines changed

4 files changed

+100
-49
lines changed

src/builder.rs

+24-9
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
use crate::chain::{ChainSource, DEFAULT_ESPLORA_SERVER_URL};
99
use crate::config::{
10-
default_user_config, Config, EsploraSyncConfig, DEFAULT_LOG_FILENAME, DEFAULT_LOG_LEVEL,
11-
WALLET_KEYS_SEED_LEN,
10+
default_user_config, AnnounceError, Config, EsploraSyncConfig, DEFAULT_LOG_FILENAME,
11+
DEFAULT_LOG_LEVEL, WALLET_KEYS_SEED_LEN,
1212
};
1313

1414
use crate::connection::ConnectionManager;
@@ -896,14 +896,29 @@ fn build_with_store_internal(
896896
liquidity_source_config: Option<&LiquiditySourceConfig>, seed_bytes: [u8; 64],
897897
logger: Arc<Logger>, kv_store: Arc<DynStore>,
898898
) -> Result<Node, BuildError> {
899-
if config.announcement_addresses.is_some() && !may_announce_channel(&config) {
900-
log_error!(logger, "Announcement addresses are set but the node will not announce itself because both node alias and listening addresses are not set.");
901-
return Err(BuildError::InvalidAnnouncementAddresses);
902-
}
899+
if let Err(err) = may_announce_channel(&config) {
900+
let error_msg_for = |prefix: &str| -> String {
901+
match err {
902+
AnnounceError::MissingNodeAlias =>
903+
format!("{} set but the node will not announce itself as node alias is not configured. Please set a node alias.", prefix),
904+
AnnounceError::MissingListeningAddresses =>
905+
format!("{} set but the node will not announce itself as listening addresses are not configured. Please set listening addresses.", prefix),
906+
AnnounceError::MissingBoth =>
907+
format!("{} set but the node will not announce itself as both node alias and listening addresses are not configured. Please configure both.", prefix),
908+
}
909+
};
910+
if config.announcement_addresses.is_some() {
911+
log_error!(logger, "{}", error_msg_for("Announcement addresses are"));
912+
return Err(match err {
913+
AnnounceError::MissingNodeAlias => BuildError::InvalidNodeAlias,
914+
_ => BuildError::InvalidListeningAddresses,
915+
});
916+
}
903917

904-
if config.node_alias.is_some() && !may_announce_channel(&config) {
905-
log_error!(logger, "Node alias is set but the node will not announce itself because no listening addresses were provided.");
906-
return Err(BuildError::InvalidListeningAddresses);
918+
if config.node_alias.is_some() {
919+
log_error!(logger, "{}", error_msg_for("Node alias is"));
920+
return Err(BuildError::InvalidListeningAddresses);
921+
}
907922
}
908923

909924
// Initialize the status fields.

src/config.rs

+33-9
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,23 @@ pub fn default_config() -> Config {
261261
Config::default()
262262
}
263263

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())
264+
#[derive(Debug, PartialEq)]
265+
pub(crate) enum AnnounceError {
266+
MissingNodeAlias,
267+
MissingListeningAddresses,
268+
MissingBoth,
269+
}
270+
271+
pub(crate) fn may_announce_channel(config: &Config) -> Result<(), AnnounceError> {
272+
let has_listening_addresses =
273+
config.listening_addresses.as_ref().map_or(false, |addrs| !addrs.is_empty());
274+
275+
match (config.node_alias.is_some(), has_listening_addresses) {
276+
(true, true) => Ok(()),
277+
(true, false) => Err(AnnounceError::MissingListeningAddresses),
278+
(false, true) => Err(AnnounceError::MissingNodeAlias),
279+
(false, false) => Err(AnnounceError::MissingBoth),
280+
}
267281
}
268282

269283
pub(crate) fn default_user_config(config: &Config) -> UserConfig {
@@ -278,7 +292,7 @@ pub(crate) fn default_user_config(config: &Config) -> UserConfig {
278292
user_config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx =
279293
config.anchor_channels_config.is_some();
280294

281-
if !may_announce_channel(config) {
295+
if may_announce_channel(config).is_err() {
282296
user_config.accept_forwards_to_priv_channels = false;
283297
user_config.channel_handshake_config.announce_for_forwarding = false;
284298
user_config.channel_handshake_limits.force_announced_channel_preference = true;
@@ -443,6 +457,7 @@ mod tests {
443457
use std::str::FromStr;
444458

445459
use super::may_announce_channel;
460+
use super::AnnounceError;
446461
use super::Config;
447462
use super::NodeAlias;
448463
use super::SocketAddress;
@@ -451,7 +466,7 @@ mod tests {
451466
fn node_announce_channel() {
452467
// Default configuration with node alias and listening addresses unset
453468
let mut node_config = Config::default();
454-
assert!(!may_announce_channel(&node_config));
469+
assert_eq!(may_announce_channel(&node_config), Err(AnnounceError::MissingBoth));
455470

456471
// Set node alias with listening addresses unset
457472
let alias_frm_str = |alias: &str| {
@@ -460,24 +475,33 @@ mod tests {
460475
NodeAlias(bytes)
461476
};
462477
node_config.node_alias = Some(alias_frm_str("LDK_Node"));
463-
assert!(!may_announce_channel(&node_config));
478+
assert_eq!(
479+
may_announce_channel(&node_config),
480+
Err(AnnounceError::MissingListeningAddresses)
481+
);
464482

465483
// Set announcement addresses with listening addresses unset
466484
let announcement_address = SocketAddress::from_str("123.45.67.89:9735")
467485
.expect("Socket address conversion failed.");
468486
node_config.announcement_addresses = Some(vec![announcement_address]);
469-
assert!(!may_announce_channel(&node_config));
487+
assert_eq!(
488+
may_announce_channel(&node_config),
489+
Err(AnnounceError::MissingListeningAddresses)
490+
);
470491

471492
// Set node alias with an empty list of listening addresses
472493
node_config.listening_addresses = Some(vec![]);
473-
assert!(!may_announce_channel(&node_config));
494+
assert_eq!(
495+
may_announce_channel(&node_config),
496+
Err(AnnounceError::MissingListeningAddresses)
497+
);
474498

475499
// Set node alias with a non-empty list of listening addresses
476500
let socket_address =
477501
SocketAddress::from_str("localhost:8000").expect("Socket address conversion failed.");
478502
if let Some(ref mut addresses) = node_config.listening_addresses {
479503
addresses.push(socket_address);
480504
}
481-
assert!(may_announce_channel(&node_config));
505+
assert!(may_announce_channel(&node_config).is_ok());
482506
}
483507
}

src/event.rs

+24-17
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::{
1212
UserChannelId,
1313
};
1414

15-
use crate::config::{may_announce_channel, Config};
15+
use crate::config::{may_announce_channel, AnnounceError, Config};
1616
use crate::connection::ConnectionManager;
1717
use crate::fee_estimator::ConfirmationTarget;
1818
use crate::liquidity::LiquiditySource;
@@ -1057,23 +1057,30 @@ where
10571057
is_announced,
10581058
params: _,
10591059
} => {
1060-
if is_announced && !may_announce_channel(&*self.config) {
1061-
log_error!(
1062-
self.logger,
1063-
"Rejecting inbound announced channel from peer {} as not all required details are set. Please ensure node alias and listening addresses have been configured.",
1064-
counterparty_node_id,
1065-
);
1060+
if is_announced {
1061+
if let Err(err) = may_announce_channel(&*self.config) {
1062+
let error_msg = match err {
1063+
AnnounceError::MissingNodeAlias =>
1064+
format!("Rejecting inbound announced channel from peer {} as node alias is not set. Please ensure node alias has been configured.", counterparty_node_id),
1065+
AnnounceError::MissingListeningAddresses =>
1066+
format!("Rejecting inbound announced channel from peer {} as listening addresses are not set. Please ensure listening addresses have been configured.", counterparty_node_id),
1067+
AnnounceError::MissingBoth =>
1068+
format!("Rejecting inbound announced channel from peer {} as node alias and listening addresses are not set. Please ensure both have been configured.", counterparty_node_id),
1069+
};
10661070

1067-
self.channel_manager
1068-
.force_close_without_broadcasting_txn(
1069-
&temporary_channel_id,
1070-
&counterparty_node_id,
1071-
"Channel request rejected".to_string(),
1072-
)
1073-
.unwrap_or_else(|e| {
1074-
log_error!(self.logger, "Failed to reject channel: {:?}", e)
1075-
});
1076-
return Ok(());
1071+
log_error!(self.logger, "{}", error_msg,);
1072+
1073+
self.channel_manager
1074+
.force_close_without_broadcasting_txn(
1075+
&temporary_channel_id,
1076+
&counterparty_node_id,
1077+
"Channel request rejected".to_string(),
1078+
)
1079+
.unwrap_or_else(|e| {
1080+
log_error!(self.logger, "Failed to reject channel: {:?}", e)
1081+
});
1082+
return Ok(());
1083+
}
10771084
}
10781085

10791086
let anchor_channel = channel_type.requires_anchors_zero_fee_htlc_tx();

src/lib.rs

+19-14
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ pub use builder::NodeBuilder as Builder;
123123

124124
use chain::ChainSource;
125125
use config::{
126-
default_user_config, may_announce_channel, ChannelConfig, Config, NODE_ANN_BCAST_INTERVAL,
127-
PEER_RECONNECTION_INTERVAL, RGS_SYNC_INTERVAL,
126+
default_user_config, may_announce_channel, AnnounceError, ChannelConfig, Config,
127+
NODE_ANN_BCAST_INTERVAL, PEER_RECONNECTION_INTERVAL, RGS_SYNC_INTERVAL,
128128
};
129129
use connection::ConnectionManager;
130130
use event::{EventHandler, EventQueue};
@@ -414,7 +414,7 @@ impl Node {
414414
let bcast_node_metrics = Arc::clone(&self.node_metrics);
415415
let mut stop_bcast = self.stop_sender.subscribe();
416416
let node_alias = self.config.node_alias.clone();
417-
if may_announce_channel(&self.config) {
417+
if may_announce_channel(&self.config).is_ok() {
418418
runtime.spawn(async move {
419419
// We check every 30 secs whether our last broadcast is NODE_ANN_BCAST_INTERVAL away.
420420
#[cfg(not(test))]
@@ -1211,19 +1211,24 @@ impl Node {
12111211
&self, node_id: PublicKey, address: SocketAddress, channel_amount_sats: u64,
12121212
push_to_counterparty_msat: Option<u64>, channel_config: Option<ChannelConfig>,
12131213
) -> Result<UserChannelId, Error> {
1214-
if may_announce_channel(&self.config) {
1215-
self.open_channel_inner(
1216-
node_id,
1217-
address,
1218-
channel_amount_sats,
1219-
push_to_counterparty_msat,
1220-
channel_config,
1221-
true,
1222-
)
1223-
} else {
1224-
log_error!(self.logger, "Failed to open announced channel as the node hasn't been sufficiently configured to act as a forwarding node. Please make sure to configure listening addreesses and node alias");
1214+
if let Err(err) = may_announce_channel(&self.config) {
1215+
let error_msg = match err {
1216+
AnnounceError::MissingNodeAlias => "Failed to open announced channel as the node hasn't been sufficiently configured to act as a forwarding node. Please make sure to configure a node alias",
1217+
AnnounceError::MissingListeningAddresses => "Failed to open announced channel as the node hasn't been sufficiently configured to act as a forwarding node. Please make sure to configure listening addreesses",
1218+
AnnounceError::MissingBoth => "Failed to open announced channel as the node hasn't been sufficiently configured to act as a forwarding node. Please make sure to configure listening addreesses and node alias",
1219+
};
1220+
log_error!(self.logger, "{}", error_msg);
12251221
return Err(Error::ChannelCreationFailed);
12261222
}
1223+
1224+
self.open_channel_inner(
1225+
node_id,
1226+
address,
1227+
channel_amount_sats,
1228+
push_to_counterparty_msat,
1229+
channel_config,
1230+
true,
1231+
)
12271232
}
12281233

12291234
/// Manually sync the LDK and BDK wallets with the current chain state and update the fee rate

0 commit comments

Comments
 (0)