Skip to content

Commit 58f40b9

Browse files
committed
Replace ChannelAnnouncementStatus boilerplate with a simple bool flag
We drop the previously-introduced `ChannelAnnouncementStatus`/`ChannelAnnouncementBlocker` types. While informative, they were a bit too much boilerplate. Instead we opt to simply return a `bool` from `may_announce_channel`, and don't spawn the node announcment task to begin with if we're not configured properly.
1 parent d630365 commit 58f40b9

File tree

2 files changed

+57
-122
lines changed

2 files changed

+57
-122
lines changed

src/config.rs

Lines changed: 13 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -278,47 +278,9 @@ pub fn default_config() -> Config {
278278
Config::default()
279279
}
280280

281-
/// Specifies reasons why a channel cannot be announced.
282-
#[derive(Debug, PartialEq)]
283-
pub(crate) enum ChannelAnnouncementBlocker {
284-
/// The node alias is not set.
285-
MissingNodeAlias,
286-
/// The listening addresses are not set.
287-
MissingListeningAddresses,
288-
// This listening addresses is set but the vector is empty.
289-
EmptyListeningAddresses,
290-
}
291-
292-
/// Enumeration defining the announcement status of a channel.
293-
#[derive(Debug, PartialEq)]
294-
pub(crate) enum ChannelAnnouncementStatus {
295-
/// The channel is announceable.
296-
Announceable,
297-
/// The channel is not announceable.
298-
Unannounceable(ChannelAnnouncementBlocker),
299-
}
300-
301-
/// Checks if a node is can announce a channel based on the configured values of both the node's
302-
/// alias and its listening addresses.
303-
///
304-
/// If either of them is unset, the node cannot announce the channel. This ability to announce/
305-
/// unannounce a channel is codified with `ChannelAnnouncementStatus`
306-
pub(crate) fn can_announce_channel(config: &Config) -> ChannelAnnouncementStatus {
307-
if config.node_alias.is_none() {
308-
return ChannelAnnouncementStatus::Unannounceable(
309-
ChannelAnnouncementBlocker::MissingNodeAlias,
310-
);
311-
}
312-
313-
match &config.listening_addresses {
314-
None => ChannelAnnouncementStatus::Unannounceable(
315-
ChannelAnnouncementBlocker::MissingListeningAddresses,
316-
),
317-
Some(addresses) if addresses.is_empty() => ChannelAnnouncementStatus::Unannounceable(
318-
ChannelAnnouncementBlocker::EmptyListeningAddresses,
319-
),
320-
Some(_) => ChannelAnnouncementStatus::Announceable,
321-
}
281+
pub(crate) fn may_announce_channel(config: &Config) -> bool {
282+
config.node_alias.is_some()
283+
&& config.listening_addresses.as_ref().map_or(false, |addrs| !addrs.is_empty())
322284
}
323285

324286
pub(crate) fn default_user_config(config: &Config) -> UserConfig {
@@ -333,13 +295,10 @@ pub(crate) fn default_user_config(config: &Config) -> UserConfig {
333295
user_config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx =
334296
config.anchor_channels_config.is_some();
335297

336-
match can_announce_channel(config) {
337-
ChannelAnnouncementStatus::Announceable => (),
338-
ChannelAnnouncementStatus::Unannounceable(_) => {
339-
user_config.accept_forwards_to_priv_channels = false;
340-
user_config.channel_handshake_config.announced_channel = false;
341-
user_config.channel_handshake_limits.force_announced_channel_preference = true;
342-
},
298+
if !may_announce_channel(config) {
299+
user_config.accept_forwards_to_priv_channels = false;
300+
user_config.channel_handshake_config.announced_channel = false;
301+
user_config.channel_handshake_limits.force_announced_channel_preference = true;
343302
}
344303

345304
user_config
@@ -349,23 +308,16 @@ pub(crate) fn default_user_config(config: &Config) -> UserConfig {
349308
mod tests {
350309
use std::str::FromStr;
351310

352-
use crate::config::ChannelAnnouncementStatus;
353-
354-
use super::can_announce_channel;
311+
use super::may_announce_channel;
355312
use super::Config;
356313
use super::NodeAlias;
357314
use super::SocketAddress;
358315

359316
#[test]
360-
fn node_can_announce_channel() {
317+
fn node_announce_channel() {
361318
// Default configuration with node alias and listening addresses unset
362319
let mut node_config = Config::default();
363-
assert_eq!(
364-
can_announce_channel(&node_config),
365-
ChannelAnnouncementStatus::Unannounceable(
366-
crate::config::ChannelAnnouncementBlocker::MissingNodeAlias
367-
)
368-
);
320+
assert!(!may_announce_channel(&node_config));
369321

370322
// Set node alias with listening addresses unset
371323
let alias_frm_str = |alias: &str| {
@@ -374,28 +326,18 @@ mod tests {
374326
NodeAlias(bytes)
375327
};
376328
node_config.node_alias = Some(alias_frm_str("LDK_Node"));
377-
assert_eq!(
378-
can_announce_channel(&node_config),
379-
ChannelAnnouncementStatus::Unannounceable(
380-
crate::config::ChannelAnnouncementBlocker::MissingListeningAddresses
381-
)
382-
);
329+
assert!(!may_announce_channel(&node_config));
383330

384331
// Set node alias with an empty list of listening addresses
385332
node_config.listening_addresses = Some(vec![]);
386-
assert_eq!(
387-
can_announce_channel(&node_config),
388-
ChannelAnnouncementStatus::Unannounceable(
389-
crate::config::ChannelAnnouncementBlocker::EmptyListeningAddresses
390-
)
391-
);
333+
assert!(!may_announce_channel(&node_config));
392334

393335
// Set node alias with a non-empty list of listening addresses
394336
let socket_address =
395337
SocketAddress::from_str("localhost:8000").expect("Socket address conversion failed.");
396338
if let Some(ref mut addresses) = node_config.listening_addresses {
397339
addresses.push(socket_address);
398340
}
399-
assert_eq!(can_announce_channel(&node_config), ChannelAnnouncementStatus::Announceable);
341+
assert!(may_announce_channel(&node_config));
400342
}
401343
}

src/lib.rs

Lines changed: 44 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ pub use builder::BuildError;
122122
pub use builder::NodeBuilder as Builder;
123123

124124
use config::{
125-
can_announce_channel, default_user_config, ChannelAnnouncementStatus,
126-
LDK_WALLET_SYNC_TIMEOUT_SECS, NODE_ANN_BCAST_INTERVAL, PEER_RECONNECTION_INTERVAL,
125+
default_user_config, may_announce_channel, LDK_WALLET_SYNC_TIMEOUT_SECS,
126+
NODE_ANN_BCAST_INTERVAL, PEER_RECONNECTION_INTERVAL,
127127
RESOLVED_CHANNEL_MONITOR_ARCHIVAL_INTERVAL, RGS_SYNC_INTERVAL,
128128
WALLET_SYNC_INTERVAL_MINIMUM_SECS,
129129
};
@@ -605,20 +605,20 @@ impl Node {
605605
let bcast_ann_timestamp = Arc::clone(&self.latest_node_announcement_broadcast_timestamp);
606606
let mut stop_bcast = self.stop_sender.subscribe();
607607
let node_alias = self.config.node_alias.clone();
608-
let can_announce_channel = can_announce_channel(&self.config);
609-
runtime.spawn(async move {
610-
// We check every 30 secs whether our last broadcast is NODE_ANN_BCAST_INTERVAL away.
611-
#[cfg(not(test))]
612-
let mut interval = tokio::time::interval(Duration::from_secs(30));
613-
#[cfg(test)]
614-
let mut interval = tokio::time::interval(Duration::from_secs(5));
615-
loop {
616-
tokio::select! {
608+
if may_announce_channel(&self.config) {
609+
runtime.spawn(async move {
610+
// We check every 30 secs whether our last broadcast is NODE_ANN_BCAST_INTERVAL away.
611+
#[cfg(not(test))]
612+
let mut interval = tokio::time::interval(Duration::from_secs(30));
613+
#[cfg(test)]
614+
let mut interval = tokio::time::interval(Duration::from_secs(5));
615+
loop {
616+
tokio::select! {
617617
_ = stop_bcast.changed() => {
618618
log_trace!(
619619
bcast_logger,
620620
"Stopping broadcasting node announcements.",
621-
);
621+
);
622622
return;
623623
}
624624
_ = interval.tick() => {
@@ -648,36 +648,37 @@ impl Node {
648648
continue;
649649
}
650650

651-
match can_announce_channel {
652-
ChannelAnnouncementStatus::Unannounceable(_) => {
653-
// Skip if we are not listening on any addresses or if the node alias is not set.
654-
continue;
655-
}
656-
ChannelAnnouncementStatus::Announceable => {
657-
let addresses = bcast_config.listening_addresses.clone().unwrap_or(Vec::new());
658-
if let Some(node_alias) = node_alias.as_ref() {
659-
bcast_pm.broadcast_node_announcement([0; 3], node_alias.0, addresses);
660-
} else {
661-
continue
662-
}
663-
}
664-
}
651+
let addresses = if let Some(addresses) = bcast_config.listening_addresses.clone() {
652+
addresses
653+
} else {
654+
debug_assert!(false, "We checked whether the node may announce, so listening addresses should always be set");
655+
continue;
656+
};
657+
658+
if let Some(node_alias) = node_alias.as_ref() {
659+
bcast_pm.broadcast_node_announcement([0; 3], node_alias.0, addresses);
665660

666-
let unix_time_secs_opt =
667-
SystemTime::now().duration_since(UNIX_EPOCH).ok().map(|d| d.as_secs());
668-
*bcast_ann_timestamp.write().unwrap() = unix_time_secs_opt;
661+
let unix_time_secs_opt =
662+
SystemTime::now().duration_since(UNIX_EPOCH).ok().map(|d| d.as_secs());
663+
*bcast_ann_timestamp.write().unwrap() = unix_time_secs_opt;
669664

670-
if let Some(unix_time_secs) = unix_time_secs_opt {
671-
io::utils::write_latest_node_ann_bcast_timestamp(unix_time_secs, Arc::clone(&bcast_store), Arc::clone(&bcast_logger))
672-
.unwrap_or_else(|e| {
673-
log_error!(bcast_logger, "Persistence failed: {}", e);
674-
panic!("Persistence failed");
675-
});
665+
if let Some(unix_time_secs) = unix_time_secs_opt {
666+
io::utils::write_latest_node_ann_bcast_timestamp(unix_time_secs, Arc::clone(&bcast_store), Arc::clone(&bcast_logger))
667+
.unwrap_or_else(|e| {
668+
log_error!(bcast_logger, "Persistence failed: {}", e);
669+
panic!("Persistence failed");
670+
});
671+
}
672+
} else {
673+
debug_assert!(false, "We checked whether the node may announce, so node alias should always be set");
674+
continue
676675
}
676+
677677
}
678+
}
678679
}
679-
}
680-
});
680+
});
681+
}
681682

682683
let mut stop_tx_bcast = self.stop_sender.subscribe();
683684
let tx_bcaster = Arc::clone(&self.tx_broadcaster);
@@ -1341,26 +1342,18 @@ impl Node {
13411342
&self, node_id: PublicKey, address: SocketAddress, channel_amount_sats: u64,
13421343
push_to_counterparty_msat: Option<u64>, channel_config: Option<ChannelConfig>,
13431344
) -> Result<UserChannelId, Error> {
1344-
match can_announce_channel(&self.config) {
1345-
config::ChannelAnnouncementStatus::Announceable => self.open_channel_inner(
1345+
if may_announce_channel(&self.config) {
1346+
self.open_channel_inner(
13461347
node_id,
13471348
address,
13481349
channel_amount_sats,
13491350
push_to_counterparty_msat,
13501351
channel_config,
13511352
true,
1352-
),
1353-
config::ChannelAnnouncementStatus::Unannounceable(reason) => match reason {
1354-
config::ChannelAnnouncementBlocker::MissingNodeAlias => {
1355-
return Err(Error::InvalidNodeAlias)
1356-
},
1357-
config::ChannelAnnouncementBlocker::MissingListeningAddresses => {
1358-
return Err(Error::InvalidSocketAddress)
1359-
},
1360-
config::ChannelAnnouncementBlocker::EmptyListeningAddresses => {
1361-
return Err(Error::InvalidSocketAddress)
1362-
},
1363-
},
1353+
)
1354+
} else {
1355+
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");
1356+
return Err(Error::ChannelCreationFailed);
13641357
}
13651358
}
13661359

0 commit comments

Comments
 (0)