Skip to content

Commit

Permalink
Switch listening_address to use NetAddress
Browse files Browse the repository at this point in the history
  • Loading branch information
tnull committed May 12, 2023
1 parent 9d75bb0 commit 3f9d68e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 27 deletions.
7 changes: 2 additions & 5 deletions bindings/ldk_node.udl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ dictionary Config {
string storage_dir_path;
string esplora_server_url;
Network network;
SocketAddr? listening_address;
NetAddress? listening_address;
u32 default_cltv_expiry_delta;
};

Expand All @@ -24,7 +24,7 @@ interface Node {
Event next_event();
void event_handled();
PublicKey node_id();
SocketAddr? listening_address();
NetAddress? listening_address();
[Throws=NodeError]
Address new_funding_address();
[Throws=NodeError]
Expand Down Expand Up @@ -154,9 +154,6 @@ dictionary PeerDetails {
[Custom]
typedef string Txid;

[Custom]
typedef string SocketAddr;

[Custom]
typedef string NetAddress;

Expand Down
18 changes: 12 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ use rand::Rng;
use std::convert::TryInto;
use std::default::Default;
use std::fs;
use std::net::{SocketAddr, ToSocketAddrs};
use std::net::ToSocketAddrs;
use std::str::FromStr;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex, RwLock};
Expand Down Expand Up @@ -185,7 +185,7 @@ pub struct Config {
/// The used Bitcoin network.
pub network: Network,
/// The IP address and TCP port the node will listen on.
pub listening_address: Option<SocketAddr>,
pub listening_address: Option<NetAddress>,
/// The default CLTV expiry delta to be used for payments.
pub default_cltv_expiry_delta: u32,
}
Expand Down Expand Up @@ -285,7 +285,7 @@ impl Builder {
/// Sets the IP address and TCP port on which [`Node`] will listen for incoming network connections.
///
/// Default: `0.0.0.0:9735`
pub fn set_listening_address(&mut self, listening_address: SocketAddr) -> &mut Self {
pub fn set_listening_address(&mut self, listening_address: NetAddress) -> &mut Self {
self.config.listening_address = Some(listening_address);
self
}
Expand Down Expand Up @@ -712,9 +712,15 @@ impl Node {
let stop_listen = Arc::clone(&stop_running);
let listening_address = listening_address.clone();

let bind_addr = listening_address
.to_socket_addrs()
.expect("Unable to resolve listing address")
.next()
.expect("Unable to resolve listing address");

runtime.spawn(async move {
let listener =
tokio::net::TcpListener::bind(listening_address).await.expect(
tokio::net::TcpListener::bind(bind_addr).await.expect(
"Failed to bind to listen address/port - is something else already listening on it?",
);
loop {
Expand Down Expand Up @@ -849,8 +855,8 @@ impl Node {
}

/// Returns our own listening address.
pub fn listening_address(&self) -> Option<SocketAddr> {
self.config.listening_address
pub fn listening_address(&self) -> Option<NetAddress> {
self.config.listening_address.clone()
}

/// Retrieve a new on-chain/funding address.
Expand Down
16 changes: 0 additions & 16 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,22 +389,6 @@ pub struct PeerDetails {
pub is_connected: bool,
}

impl UniffiCustomTypeConverter for SocketAddr {
type Builtin = String;

fn into_custom(val: Self::Builtin) -> uniffi::Result<Self> {
if let Ok(addr) = SocketAddr::from_str(&val) {
return Ok(addr);
}

Err(Error::InvalidPublicKey.into())
}

fn from_custom(obj: Self) -> Self::Builtin {
obj.to_string()
}
}

/// The network address of a Lightning node.
///
/// Currently only IPv4, IPv6, and DNS hostnames are supported.
Expand Down

0 comments on commit 3f9d68e

Please sign in to comment.