Skip to content

Commit fb9243a

Browse files
committed
Add configuration for node announcement addresses
In certain node configurations a user might want or need to announce different addresses to the network than the ones they bind their tcp listeners to.
1 parent 0dd78ee commit fb9243a

File tree

4 files changed

+62
-2
lines changed

4 files changed

+62
-2
lines changed

bindings/ldk_node.udl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ dictionary Config {
77
string storage_dir_path;
88
Network network;
99
sequence<SocketAddress>? listening_addresses;
10+
sequence<SocketAddress>? announcement_addresses;
1011
NodeAlias? node_alias;
1112
sequence<PublicKey> trusted_peers_0conf;
1213
u64 probing_liquidity_limit_multiplier;
@@ -84,6 +85,8 @@ interface Builder {
8485
[Throws=BuildError]
8586
void set_listening_addresses(sequence<SocketAddress> listening_addresses);
8687
[Throws=BuildError]
88+
void set_announcement_addresses(sequence<SocketAddress> announcement_addresses);
89+
[Throws=BuildError]
8790
void set_node_alias(string node_alias);
8891
[Throws=BuildError]
8992
Node build();
@@ -112,6 +115,7 @@ interface Node {
112115
void event_handled();
113116
PublicKey node_id();
114117
sequence<SocketAddress>? listening_addresses();
118+
sequence<SocketAddress>? announcement_addresses();
115119
NodeAlias? node_alias();
116120
Bolt11Payment bolt11_payment();
117121
Bolt12Payment bolt12_payment();
@@ -319,6 +323,7 @@ enum BuildError {
319323
"InvalidSystemTime",
320324
"InvalidChannelMonitor",
321325
"InvalidListeningAddresses",
326+
"InvalidAnnouncementAddresses",
322327
"InvalidNodeAlias",
323328
"ReadFailed",
324329
"WriteFailed",

src/builder.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ pub enum BuildError {
148148
InvalidChannelMonitor,
149149
/// The given listening addresses are invalid, e.g. too many were passed.
150150
InvalidListeningAddresses,
151+
/// The given announcement addresses are invalid, e.g. too many were passed.
152+
InvalidAnnouncementAddresses,
151153
/// The provided alias is invalid.
152154
InvalidNodeAlias,
153155
/// We failed to read data from the [`KVStore`].
@@ -184,6 +186,9 @@ impl fmt::Display for BuildError {
184186
write!(f, "Failed to watch a deserialized ChannelMonitor")
185187
},
186188
Self::InvalidListeningAddresses => write!(f, "Given listening addresses are invalid."),
189+
Self::InvalidAnnouncementAddresses => {
190+
write!(f, "Given announcement addresses are invalid.")
191+
},
187192
Self::ReadFailed => write!(f, "Failed to read from store."),
188193
Self::WriteFailed => write!(f, "Failed to write to store."),
189194
Self::StoragePathAccessFailed => write!(f, "Failed to access the given storage path."),
@@ -413,6 +418,22 @@ impl NodeBuilder {
413418
Ok(self)
414419
}
415420

421+
/// Sets the IP address and TCP port which [`Node`] will announce to the gossip network that it accepts connections on.
422+
///
423+
/// **Note**: If unset, the [`listening_addresses`] will be used as the list of addresses to announce.
424+
///
425+
/// [`listening_addresses`]: Self::set_listening_addresses
426+
pub fn set_announcement_addresses(
427+
&mut self, announcement_addresses: Vec<SocketAddress>,
428+
) -> Result<&mut Self, BuildError> {
429+
if announcement_addresses.len() > 100 {
430+
return Err(BuildError::InvalidAnnouncementAddresses);
431+
}
432+
433+
self.config.announcement_addresses = Some(announcement_addresses);
434+
Ok(self)
435+
}
436+
416437
/// Sets the node alias that will be used when broadcasting announcements to the gossip
417438
/// network.
418439
///
@@ -776,6 +797,17 @@ impl ArcedNodeBuilder {
776797
self.inner.write().unwrap().set_listening_addresses(listening_addresses).map(|_| ())
777798
}
778799

800+
/// Sets the IP address and TCP port which [`Node`] will announce to the gossip network that it accepts connections on.
801+
///
802+
/// **Note**: If unset, the [`listening_addresses`] will be used as the list of addresses to announce.
803+
///
804+
/// [`listening_addresses`]: Self::set_listening_addresses
805+
pub fn set_announcement_addresses(
806+
&self, announcement_addresses: Vec<SocketAddress>,
807+
) -> Result<(), BuildError> {
808+
self.inner.write().unwrap().set_announcement_addresses(announcement_addresses).map(|_| ())
809+
}
810+
779811
/// Sets the node alias that will be used when broadcasting announcements to the gossip
780812
/// network.
781813
///

src/config.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,12 @@ pub struct Config {
117117
/// **Note**: We will only allow opening and accepting public channels if the `node_alias` and the
118118
/// `listening_addresses` are set.
119119
pub listening_addresses: Option<Vec<SocketAddress>>,
120+
/// The addresses which the node will announce to the gossip network that it accepts connections on.
121+
///
122+
/// **Note**: If unset, the [`listening_addresses`] will be used as the list of addresses to announce.
123+
///
124+
/// [`listening_addresses`]: Config::listening_addresses
125+
pub announcement_addresses: Option<Vec<SocketAddress>>,
120126
/// The node alias that will be used when broadcasting announcements to the gossip network.
121127
///
122128
/// The provided alias must be a valid UTF-8 string and no longer than 32 bytes in total.
@@ -168,6 +174,7 @@ impl Default for Config {
168174
storage_dir_path: DEFAULT_STORAGE_DIR_PATH.to_string(),
169175
network: DEFAULT_NETWORK,
170176
listening_addresses: None,
177+
announcement_addresses: None,
171178
trusted_peers_0conf: Vec::new(),
172179
probing_liquidity_limit_multiplier: DEFAULT_PROBING_LIQUIDITY_LIMIT_MULTIPLIER,
173180
anchor_channels_config: Some(AnchorChannelsConfig::default()),
@@ -480,6 +487,12 @@ mod tests {
480487
node_config.node_alias = Some(alias_frm_str("LDK_Node"));
481488
assert!(!may_announce_channel(&node_config));
482489

490+
// Set announcement addresses with listening addresses unset
491+
let announcement_address = SocketAddress::from_str("123.45.67.89:9735")
492+
.expect("Socket address conversion failed.");
493+
node_config.announcement_addresses = Some(vec![announcement_address]);
494+
assert!(!may_announce_channel(&node_config));
495+
483496
// Set node alias with an empty list of listening addresses
484497
node_config.listening_addresses = Some(vec![]);
485498
assert!(!may_announce_channel(&node_config));

src/lib.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -457,8 +457,10 @@ impl Node {
457457
continue;
458458
}
459459

460-
let addresses = if let Some(addresses) = bcast_config.listening_addresses.clone() {
461-
addresses
460+
let addresses = if let Some(announcement_addresses) = bcast_config.announcement_addresses.clone() {
461+
announcement_addresses
462+
} else if let Some(listening_addresses) = bcast_config.listening_addresses.clone() {
463+
listening_addresses
462464
} else {
463465
debug_assert!(false, "We checked whether the node may announce, so listening addresses should always be set");
464466
continue;
@@ -802,6 +804,14 @@ impl Node {
802804
self.config.listening_addresses.clone()
803805
}
804806

807+
/// Returns the addresses that the node will announce to the network.
808+
pub fn announcement_addresses(&self) -> Option<Vec<SocketAddress>> {
809+
self.config
810+
.announcement_addresses
811+
.clone()
812+
.or_else(|| self.config.listening_addresses.clone())
813+
}
814+
805815
/// Returns our node alias.
806816
pub fn node_alias(&self) -> Option<NodeAlias> {
807817
self.config.node_alias

0 commit comments

Comments
 (0)