Skip to content

Commit 9780c83

Browse files
committed
Switch listening_address to use NetAddress
1 parent bd3c6c9 commit 9780c83

File tree

3 files changed

+15
-24
lines changed

3 files changed

+15
-24
lines changed

bindings/ldk_node.udl

+2-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ dictionary Config {
55
string storage_dir_path;
66
string esplora_server_url;
77
Network network;
8-
SocketAddr? listening_address;
8+
NetAddress? listening_address;
99
u32 default_cltv_expiry_delta;
1010
};
1111

@@ -24,7 +24,7 @@ interface Node {
2424
Event next_event();
2525
void event_handled();
2626
PublicKey node_id();
27-
SocketAddr? listening_address();
27+
NetAddress? listening_address();
2828
[Throws=NodeError]
2929
Address new_funding_address();
3030
[Throws=NodeError]
@@ -159,9 +159,6 @@ dictionary PeerDetails {
159159
[Custom]
160160
typedef string Txid;
161161

162-
[Custom]
163-
typedef string SocketAddr;
164-
165162
[Custom]
166163
typedef string NetAddress;
167164

src/lib.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ use rand::Rng;
151151
use std::convert::TryInto;
152152
use std::default::Default;
153153
use std::fs;
154-
use std::net::{SocketAddr, ToSocketAddrs};
154+
use std::net::ToSocketAddrs;
155155
use std::str::FromStr;
156156
use std::sync::atomic::{AtomicBool, Ordering};
157157
use std::sync::{Arc, Mutex, RwLock};
@@ -185,7 +185,7 @@ pub struct Config {
185185
/// The used Bitcoin network.
186186
pub network: Network,
187187
/// The IP address and TCP port the node will listen on.
188-
pub listening_address: Option<SocketAddr>,
188+
pub listening_address: Option<NetAddress>,
189189
/// The default CLTV expiry delta to be used for payments.
190190
pub default_cltv_expiry_delta: u32,
191191
}
@@ -309,7 +309,7 @@ impl Builder {
309309
/// Sets the IP address and TCP port on which [`Node`] will listen for incoming network connections.
310310
///
311311
/// Default: `0.0.0.0:9735`
312-
pub fn set_listening_address(&mut self, listening_address: SocketAddr) -> &mut Self {
312+
pub fn set_listening_address(&mut self, listening_address: NetAddress) -> &mut Self {
313313
self.config.listening_address = Some(listening_address);
314314
self
315315
}
@@ -816,9 +816,15 @@ impl Node {
816816
let stop_listen = Arc::clone(&stop_running);
817817
let listening_address = listening_address.clone();
818818

819+
let bind_addr = listening_address
820+
.to_socket_addrs()
821+
.expect("Unable to resolve listing address")
822+
.next()
823+
.expect("Unable to resolve listing address");
824+
819825
runtime.spawn(async move {
820826
let listener =
821-
tokio::net::TcpListener::bind(listening_address).await.expect(
827+
tokio::net::TcpListener::bind(bind_addr).await.expect(
822828
"Failed to bind to listen address/port - is something else already listening on it?",
823829
);
824830
loop {
@@ -953,8 +959,8 @@ impl Node {
953959
}
954960

955961
/// Returns our own listening address.
956-
pub fn listening_address(&self) -> Option<SocketAddr> {
957-
self.config.listening_address
962+
pub fn listening_address(&self) -> Option<NetAddress> {
963+
self.config.listening_address.clone()
958964
}
959965

960966
/// Retrieve a new on-chain/funding address.

src/types.rs

+1-13
Original file line numberDiff line numberDiff line change
@@ -405,23 +405,11 @@ pub struct PeerDetails {
405405
pub is_connected: bool,
406406
}
407407

408-
impl UniffiCustomTypeConverter for SocketAddr {
409-
type Builtin = String;
410-
411-
fn into_custom(val: Self::Builtin) -> uniffi::Result<Self> {
412-
Ok(SocketAddr::from_str(&val).map_err(|_| Error::InvalidNetAddress)?)
413-
}
414-
415-
fn from_custom(obj: Self) -> Self::Builtin {
416-
obj.to_string()
417-
}
418-
}
419-
420408
/// The network address of a Lightning node.
421409
///
422410
/// Currently only IPv4, IPv6, and DNS hostnames are supported.
423411
#[derive(Debug, Clone, PartialEq, Eq)]
424-
pub struct NetAddress(LdkNetAddress);
412+
pub struct NetAddress(pub LdkNetAddress);
425413

426414
impl Display for NetAddress {
427415
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {

0 commit comments

Comments
 (0)