Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions lightning-net-tokio/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,16 +374,24 @@ where
/// making an outbound connection which is expected to be accepted by a peer with the given
/// public key. The relevant processing is set to run free (via tokio::spawn).
///
/// The `remote_addr` parameter should be set to the remote network address the stream is
/// connected to, and will set the [`PeerDetails::socket_address`] field for that peer in
/// [`PeerManager::list_peers`]. If this address is set, and it is not a private IPV4 or IPV6
/// address, it will also be reported to the peer in our init message.
///
/// The returned future will complete when the peer is disconnected and associated handling
/// futures are freed, though, because all processing futures are spawned with tokio::spawn, you do
/// not need to poll the provided future in order to make progress.
///
/// [`PeerDetails::socket_address`]: lightning::ln::peer_handler::PeerDetails::socket_address
/// [`PeerManager::list_peers`]: lightning::ln::peer_handler::PeerManager::list_peers
pub fn setup_outbound<PM: Deref + 'static + Send + Sync + Clone>(
peer_manager: PM, their_node_id: PublicKey, stream: StdTcpStream,
remote_addr: Option<SocketAddress>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, rather than adding this on the generic setup_outbound, would it make sense to refactor this to be an setup_outbound_internal and offer two setup_outbound and setup_outbound_tor variants that do the right (tm) thing for the user? Or, put differently, for non-Tor users this remote_addr argument might be confusing?

) -> impl std::future::Future<Output = ()>
where
PM::Target: APeerManager<Descriptor = SocketDescriptor>,
{
let remote_addr = get_addr_from_stream(&stream);
let (reader, mut write_receiver, read_receiver, us) = Connection::new(stream);
#[cfg(test)]
let last_us = Arc::clone(&us);
Expand Down Expand Up @@ -469,7 +477,8 @@ where
if let Ok(Ok(stream)) =
time::timeout(Duration::from_secs(CONNECT_OUTBOUND_TIMEOUT), connect_fut).await
{
Some(setup_outbound(peer_manager, their_node_id, stream))
let remote_addr = get_addr_from_stream(&stream);
Some(setup_outbound(peer_manager, their_node_id, stream, remote_addr))
} else {
None
}
Expand All @@ -488,12 +497,14 @@ where
PM::Target: APeerManager<Descriptor = SocketDescriptor>,
{
let connect_fut = async {
tor_connect(addr, tor_proxy_addr, entropy_source).await.map(|s| s.into_std().unwrap())
tor_connect(addr.clone(), tor_proxy_addr, entropy_source)
.await
.map(|s| s.into_std().unwrap())
};
if let Ok(Ok(stream)) =
time::timeout(Duration::from_secs(TOR_CONNECT_OUTBOUND_TIMEOUT), connect_fut).await
{
Some(setup_outbound(peer_manager, their_node_id, stream))
Some(setup_outbound(peer_manager, their_node_id, stream, Some(addr)))
} else {
None
}
Expand Down Expand Up @@ -1015,7 +1026,8 @@ mod tests {
// 127.0.0.1.
let (conn_a, conn_b) = make_tcp_connection();

let fut_a = super::setup_outbound(Arc::clone(&a_manager), b_pub, conn_a);
let remote_addr = super::get_addr_from_stream(&conn_a);
let fut_a = super::setup_outbound(Arc::clone(&a_manager), b_pub, conn_a, remote_addr);
let fut_b = super::setup_inbound(b_manager, conn_b);

tokio::time::timeout(Duration::from_secs(10), a_connected.recv()).await.unwrap();
Expand Down Expand Up @@ -1085,7 +1097,10 @@ mod tests {
// Call connection setup inside new tokio tasks.
let manager_reference = Arc::clone(&a_manager);
tokio::spawn(async move { super::setup_inbound(manager_reference, conn_a).await });
tokio::spawn(async move { super::setup_outbound(a_manager, b_pub, conn_b).await });
let remote_addr = super::get_addr_from_stream(&conn_b);
tokio::spawn(
async move { super::setup_outbound(a_manager, b_pub, conn_b, remote_addr).await },
);
}

#[tokio::test(flavor = "multi_thread")]
Expand Down
Loading