lightning_net_tokio/
lib.rs

1// This file is Copyright its original authors, visible in version control
2// history.
3//
4// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7// You may not use this file except in accordance with one or both of these
8// licenses.
9
10//! A socket handling library for those running in Tokio environments who wish to use
11//! rust-lightning with native [`TcpStream`]s.
12//!
13//! Designed to be as simple as possible, the high-level usage is almost as simple as "hand over a
14//! [`TcpStream`] and a reference to a [`PeerManager`] and the rest is handled".
15//!
16//! The [`PeerManager`], due to the fire-and-forget nature of this logic, must be a reference,
17//! (e.g. an [`Arc`]) and must use the [`SocketDescriptor`] provided here as the [`PeerManager`]'s
18//! `SocketDescriptor` implementation.
19//!
20//! Three methods are exposed to register a new connection for handling in [`tokio::spawn`] calls;
21//! see their individual docs for details.
22//!
23//! [`PeerManager`]: lightning::ln::peer_handler::PeerManager
24
25#![deny(rustdoc::broken_intra_doc_links)]
26#![deny(rustdoc::private_intra_doc_links)]
27#![deny(missing_docs)]
28#![cfg_attr(docsrs, feature(doc_cfg))]
29
30use bitcoin::secp256k1::PublicKey;
31
32use tokio::net::TcpStream;
33use tokio::sync::mpsc;
34use tokio::time;
35
36use lightning::ln::msgs::SocketAddress;
37use lightning::ln::peer_handler;
38use lightning::ln::peer_handler::APeerManager;
39use lightning::ln::peer_handler::SocketDescriptor as LnSocketTrait;
40
41use std::future::Future;
42use std::hash::Hash;
43use std::net::SocketAddr;
44use std::net::TcpStream as StdTcpStream;
45use std::ops::Deref;
46use std::pin::Pin;
47use std::sync::atomic::{AtomicU64, Ordering};
48use std::sync::{Arc, Mutex};
49use std::task::{self, Poll};
50use std::time::Duration;
51
52static ID_COUNTER: AtomicU64 = AtomicU64::new(0);
53
54// We only need to select over multiple futures in one place, and taking on the full `tokio/macros`
55// dependency tree in order to do so (which has broken our MSRV before) is excessive. Instead, we
56// define a trivial two- and three- select macro with the specific types we need and just use that.
57
58pub(crate) enum SelectorOutput {
59	A(Option<()>),
60	B(Option<()>),
61	C(tokio::io::Result<()>),
62}
63
64pub(crate) struct TwoSelector<
65	A: Future<Output = Option<()>> + Unpin,
66	B: Future<Output = Option<()>> + Unpin,
67> {
68	pub a: A,
69	pub b: B,
70}
71
72impl<A: Future<Output = Option<()>> + Unpin, B: Future<Output = Option<()>> + Unpin> Future
73	for TwoSelector<A, B>
74{
75	type Output = SelectorOutput;
76	fn poll(mut self: Pin<&mut Self>, ctx: &mut task::Context<'_>) -> Poll<SelectorOutput> {
77		match Pin::new(&mut self.a).poll(ctx) {
78			Poll::Ready(res) => {
79				return Poll::Ready(SelectorOutput::A(res));
80			},
81			Poll::Pending => {},
82		}
83		match Pin::new(&mut self.b).poll(ctx) {
84			Poll::Ready(res) => {
85				return Poll::Ready(SelectorOutput::B(res));
86			},
87			Poll::Pending => {},
88		}
89		Poll::Pending
90	}
91}
92
93pub(crate) struct ThreeSelector<
94	A: Future<Output = Option<()>> + Unpin,
95	B: Future<Output = Option<()>> + Unpin,
96	C: Future<Output = tokio::io::Result<()>> + Unpin,
97> {
98	pub a: A,
99	pub b: B,
100	pub c: C,
101}
102
103impl<
104		A: Future<Output = Option<()>> + Unpin,
105		B: Future<Output = Option<()>> + Unpin,
106		C: Future<Output = tokio::io::Result<()>> + Unpin,
107	> Future for ThreeSelector<A, B, C>
108{
109	type Output = SelectorOutput;
110	fn poll(mut self: Pin<&mut Self>, ctx: &mut task::Context<'_>) -> Poll<SelectorOutput> {
111		match Pin::new(&mut self.a).poll(ctx) {
112			Poll::Ready(res) => {
113				return Poll::Ready(SelectorOutput::A(res));
114			},
115			Poll::Pending => {},
116		}
117		match Pin::new(&mut self.b).poll(ctx) {
118			Poll::Ready(res) => {
119				return Poll::Ready(SelectorOutput::B(res));
120			},
121			Poll::Pending => {},
122		}
123		match Pin::new(&mut self.c).poll(ctx) {
124			Poll::Ready(res) => {
125				return Poll::Ready(SelectorOutput::C(res));
126			},
127			Poll::Pending => {},
128		}
129		Poll::Pending
130	}
131}
132
133/// Connection contains all our internal state for a connection - we hold a reference to the
134/// Connection object (in an Arc<Mutex<>>) in each SocketDescriptor we create as well as in the
135/// read future (which is returned by schedule_read).
136struct Connection {
137	writer: Option<Arc<TcpStream>>,
138	// Because our PeerManager is templated by user-provided types, and we can't (as far as I can
139	// tell) have a const RawWakerVTable built out of templated functions, we need some indirection
140	// between being woken up with write-ready and calling PeerManager::write_buffer_space_avail.
141	// This provides that indirection, with a Sender which gets handed to the PeerManager Arc on
142	// the schedule_read stack.
143	//
144	// An alternative (likely more effecient) approach would involve creating a RawWakerVTable at
145	// runtime with functions templated by the Arc<PeerManager> type, calling
146	// write_buffer_space_avail directly from tokio's write wake, however doing so would require
147	// more unsafe voodo than I really feel like writing.
148	write_avail: mpsc::Sender<()>,
149	// When we are told by rust-lightning to pause read (because we have writes backing up), we do
150	// so by setting read_paused. At that point, the read task will stop reading bytes from the
151	// socket. To wake it up (without otherwise changing its state, we can push a value into this
152	// Sender.
153	read_waker: mpsc::Sender<()>,
154	read_paused: bool,
155	rl_requested_disconnect: bool,
156	id: u64,
157}
158impl Connection {
159	async fn poll_event_process<PM: Deref + 'static + Send + Sync>(
160		peer_manager: PM, mut event_receiver: mpsc::Receiver<()>,
161	) where
162		PM::Target: APeerManager<Descriptor = SocketDescriptor>,
163	{
164		loop {
165			if event_receiver.recv().await.is_none() {
166				return;
167			}
168			peer_manager.as_ref().process_events();
169		}
170	}
171
172	async fn schedule_read<PM: Deref + 'static + Send + Sync + Clone>(
173		peer_manager: PM, us: Arc<Mutex<Self>>, reader: Arc<TcpStream>,
174		mut read_wake_receiver: mpsc::Receiver<()>, mut write_avail_receiver: mpsc::Receiver<()>,
175	) where
176		PM::Target: APeerManager<Descriptor = SocketDescriptor>,
177	{
178		// Create a waker to wake up poll_event_process, above
179		let (event_waker, event_receiver) = mpsc::channel(1);
180		tokio::spawn(Self::poll_event_process(peer_manager.clone(), event_receiver));
181
182		// 4KiB is nice and big without handling too many messages all at once, giving other peers
183		// a chance to do some work.
184		let mut buf = [0; 4096];
185
186		let mut our_descriptor = SocketDescriptor::new(Arc::clone(&us));
187		// An enum describing why we did/are disconnecting:
188		enum Disconnect {
189			// Rust-Lightning told us to disconnect, either by returning an Err or by calling
190			// SocketDescriptor::disconnect_socket.
191			// In this case, we do not call peer_manager.socket_disconnected() as Rust-Lightning
192			// already knows we're disconnected.
193			CloseConnection,
194			// The connection was disconnected for some other reason, ie because the socket was
195			// closed.
196			// In this case, we do need to call peer_manager.socket_disconnected() to inform
197			// Rust-Lightning that the socket is gone.
198			PeerDisconnected,
199		}
200		let disconnect_type = loop {
201			let read_paused = {
202				let us_lock = us.lock().unwrap();
203				if us_lock.rl_requested_disconnect {
204					break Disconnect::CloseConnection;
205				}
206				us_lock.read_paused
207			};
208			// TODO: Drop the Box'ing of the futures once Rust has pin-on-stack support.
209			let select_result = if read_paused {
210				TwoSelector {
211					a: Box::pin(write_avail_receiver.recv()),
212					b: Box::pin(read_wake_receiver.recv()),
213				}
214				.await
215			} else {
216				ThreeSelector {
217					a: Box::pin(write_avail_receiver.recv()),
218					b: Box::pin(read_wake_receiver.recv()),
219					c: Box::pin(reader.readable()),
220				}
221				.await
222			};
223			match select_result {
224				SelectorOutput::A(v) => {
225					assert!(v.is_some()); // We can't have dropped the sending end, its in the us Arc!
226					if peer_manager.as_ref().write_buffer_space_avail(&mut our_descriptor).is_err()
227					{
228						break Disconnect::CloseConnection;
229					}
230				},
231				SelectorOutput::B(some) => {
232					// The mpsc Receiver should only return `None` if the write side has been
233					// dropped, but that shouldn't be possible since its referenced by the Self in
234					// `us`.
235					debug_assert!(some.is_some());
236				},
237				SelectorOutput::C(res) => {
238					if res.is_err() {
239						break Disconnect::PeerDisconnected;
240					}
241					match reader.try_read(&mut buf) {
242						Ok(0) => break Disconnect::PeerDisconnected,
243						Ok(len) => {
244							let read_res =
245								peer_manager.as_ref().read_event(&mut our_descriptor, &buf[0..len]);
246							let mut us_lock = us.lock().unwrap();
247							match read_res {
248								Ok(pause_read) => {
249									if pause_read {
250										us_lock.read_paused = true;
251									}
252								},
253								Err(_) => break Disconnect::CloseConnection,
254							}
255						},
256						Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => {
257							// readable() is allowed to spuriously wake, so we have to handle
258							// WouldBlock here.
259						},
260						Err(_) => break Disconnect::PeerDisconnected,
261					}
262				},
263			}
264			let _ = event_waker.try_send(());
265
266			// At this point we've processed a message or two, and reset the ping timer for this
267			// peer, at least in the "are we still receiving messages" context, if we don't give up
268			// our timeslice to another task we may just spin on this peer, starving other peers
269			// and eventually disconnecting them for ping timeouts. Instead, we explicitly yield
270			// here.
271			let _ = tokio::task::yield_now().await;
272		};
273		us.lock().unwrap().writer.take();
274		if let Disconnect::PeerDisconnected = disconnect_type {
275			peer_manager.as_ref().socket_disconnected(&our_descriptor);
276			peer_manager.as_ref().process_events();
277		}
278	}
279
280	fn new(
281		stream: StdTcpStream,
282	) -> (Arc<TcpStream>, mpsc::Receiver<()>, mpsc::Receiver<()>, Arc<Mutex<Self>>) {
283		// We only ever need a channel of depth 1 here: if we returned a non-full write to the
284		// PeerManager, we will eventually get notified that there is room in the socket to write
285		// new bytes, which will generate an event. That event will be popped off the queue before
286		// we call write_buffer_space_avail, ensuring that we have room to push a new () if, during
287		// the write_buffer_space_avail() call, send_data() returns a non-full write.
288		let (write_avail, write_receiver) = mpsc::channel(1);
289		// Similarly here - our only goal is to make sure the reader wakes up at some point after
290		// we shove a value into the channel which comes after we've reset the read_paused bool to
291		// false.
292		let (read_waker, read_receiver) = mpsc::channel(1);
293		stream.set_nonblocking(true).unwrap();
294		let tokio_stream = Arc::new(TcpStream::from_std(stream).unwrap());
295
296		let id = ID_COUNTER.fetch_add(1, Ordering::AcqRel);
297		let writer = Some(Arc::clone(&tokio_stream));
298		let conn = Arc::new(Mutex::new(Self {
299			writer,
300			write_avail,
301			read_waker,
302			read_paused: false,
303			rl_requested_disconnect: false,
304			id,
305		}));
306		(tokio_stream, write_receiver, read_receiver, conn)
307	}
308}
309
310fn get_addr_from_stream(stream: &StdTcpStream) -> Option<SocketAddress> {
311	match stream.peer_addr() {
312		Ok(SocketAddr::V4(sockaddr)) => {
313			Some(SocketAddress::TcpIpV4 { addr: sockaddr.ip().octets(), port: sockaddr.port() })
314		},
315		Ok(SocketAddr::V6(sockaddr)) => {
316			Some(SocketAddress::TcpIpV6 { addr: sockaddr.ip().octets(), port: sockaddr.port() })
317		},
318		Err(_) => None,
319	}
320}
321
322/// Process incoming messages and feed outgoing messages on the provided socket generated by
323/// accepting an incoming connection.
324///
325/// The returned future will complete when the peer is disconnected and associated handling
326/// futures are freed, though, because all processing futures are spawned with tokio::spawn, you do
327/// not need to poll the provided future in order to make progress.
328pub fn setup_inbound<PM: Deref + 'static + Send + Sync + Clone>(
329	peer_manager: PM, stream: StdTcpStream,
330) -> impl std::future::Future<Output = ()>
331where
332	PM::Target: APeerManager<Descriptor = SocketDescriptor>,
333{
334	let remote_addr = get_addr_from_stream(&stream);
335	let (reader, write_receiver, read_receiver, us) = Connection::new(stream);
336	#[cfg(test)]
337	let last_us = Arc::clone(&us);
338
339	let handle_opt = if peer_manager
340		.as_ref()
341		.new_inbound_connection(SocketDescriptor::new(Arc::clone(&us)), remote_addr)
342		.is_ok()
343	{
344		let handle = tokio::spawn(Connection::schedule_read(
345			peer_manager,
346			us,
347			reader,
348			read_receiver,
349			write_receiver,
350		));
351		Some(handle)
352	} else {
353		// Note that we will skip socket_disconnected here, in accordance with the PeerManager
354		// requirements.
355		None
356	};
357
358	async move {
359		if let Some(handle) = handle_opt {
360			if let Err(e) = handle.await {
361				assert!(e.is_cancelled());
362			} else {
363				// This is certainly not guaranteed to always be true - the read loop may exit
364				// while there are still pending write wakers that need to be woken up after the
365				// socket shutdown(). Still, as a check during testing, to make sure tokio doesn't
366				// keep too many wakers around, this makes sense. The race should be rare (we do
367				// some work after shutdown()) and an error would be a major memory leak.
368				#[cfg(test)]
369				debug_assert!(Arc::try_unwrap(last_us).is_ok());
370			}
371		}
372	}
373}
374
375/// Process incoming messages and feed outgoing messages on the provided socket generated by
376/// making an outbound connection which is expected to be accepted by a peer with the given
377/// public key. The relevant processing is set to run free (via tokio::spawn).
378///
379/// The returned future will complete when the peer is disconnected and associated handling
380/// futures are freed, though, because all processing futures are spawned with tokio::spawn, you do
381/// not need to poll the provided future in order to make progress.
382pub fn setup_outbound<PM: Deref + 'static + Send + Sync + Clone>(
383	peer_manager: PM, their_node_id: PublicKey, stream: StdTcpStream,
384) -> impl std::future::Future<Output = ()>
385where
386	PM::Target: APeerManager<Descriptor = SocketDescriptor>,
387{
388	let remote_addr = get_addr_from_stream(&stream);
389	let (reader, mut write_receiver, read_receiver, us) = Connection::new(stream);
390	#[cfg(test)]
391	let last_us = Arc::clone(&us);
392	let handle_opt = if let Ok(initial_send) = peer_manager.as_ref().new_outbound_connection(
393		their_node_id,
394		SocketDescriptor::new(Arc::clone(&us)),
395		remote_addr,
396	) {
397		let handle = tokio::spawn(async move {
398			// We should essentially always have enough room in a TCP socket buffer to send the
399			// initial 10s of bytes. However, tokio running in single-threaded mode will always
400			// fail writes and wake us back up later to write. Thus, we handle a single
401			// std::task::Poll::Pending but still expect to write the full set of bytes at once
402			// and use a relatively tight timeout.
403			let send_fut = async {
404				loop {
405					match SocketDescriptor::new(Arc::clone(&us)).send_data(&initial_send, true) {
406						v if v == initial_send.len() => break Ok(()),
407						0 => {
408							write_receiver.recv().await;
409							// In theory we could check for if we've been instructed to disconnect
410							// the peer here, but its OK to just skip it - we'll check for it in
411							// schedule_read prior to any relevant calls into RL.
412						},
413						_ => {
414							eprintln!("Failed to write first full message to socket!");
415							peer_manager
416								.as_ref()
417								.socket_disconnected(&SocketDescriptor::new(Arc::clone(&us)));
418							break Err(());
419						},
420					}
421				}
422			};
423			let timeout_send_fut = tokio::time::timeout(Duration::from_millis(100), send_fut);
424			if let Ok(Ok(())) = timeout_send_fut.await {
425				Connection::schedule_read(peer_manager, us, reader, read_receiver, write_receiver)
426					.await;
427			}
428		});
429		Some(handle)
430	} else {
431		// Note that we will skip socket_disconnected here, in accordance with the PeerManager
432		// requirements.
433		None
434	};
435
436	async move {
437		if let Some(handle) = handle_opt {
438			if let Err(e) = handle.await {
439				assert!(e.is_cancelled());
440			} else {
441				// This is certainly not guaranteed to always be true - the read loop may exit
442				// while there are still pending write wakers that need to be woken up after the
443				// socket shutdown(). Still, as a check during testing, to make sure tokio doesn't
444				// keep too many wakers around, this makes sense. The race should be rare (we do
445				// some work after shutdown()) and an error would be a major memory leak.
446				#[cfg(test)]
447				debug_assert!(Arc::try_unwrap(last_us).is_ok());
448			}
449		}
450	}
451}
452
453/// Process incoming messages and feed outgoing messages on a new connection made to the given
454/// socket address which is expected to be accepted by a peer with the given public key (by
455/// scheduling futures with tokio::spawn).
456///
457/// Shorthand for TcpStream::connect(addr) with a timeout followed by setup_outbound().
458///
459/// Returns a future (as the fn is async) which needs to be polled to complete the connection and
460/// connection setup. That future then returns a future which will complete when the peer is
461/// disconnected and associated handling futures are freed, though, because all processing in said
462/// futures are spawned with tokio::spawn, you do not need to poll the second future in order to
463/// make progress.
464pub async fn connect_outbound<PM: Deref + 'static + Send + Sync + Clone>(
465	peer_manager: PM, their_node_id: PublicKey, addr: SocketAddr,
466) -> Option<impl std::future::Future<Output = ()>>
467where
468	PM::Target: APeerManager<Descriptor = SocketDescriptor>,
469{
470	let connect_fut = async { TcpStream::connect(&addr).await.map(|s| s.into_std().unwrap()) };
471	if let Ok(Ok(stream)) = time::timeout(Duration::from_secs(10), connect_fut).await {
472		Some(setup_outbound(peer_manager, their_node_id, stream))
473	} else {
474		None
475	}
476}
477
478const SOCK_WAKER_VTABLE: task::RawWakerVTable = task::RawWakerVTable::new(
479	clone_socket_waker,
480	wake_socket_waker,
481	wake_socket_waker_by_ref,
482	drop_socket_waker,
483);
484
485fn clone_socket_waker(orig_ptr: *const ()) -> task::RawWaker {
486	let new_waker = unsafe { Arc::from_raw(orig_ptr as *const mpsc::Sender<()>) };
487	let res = write_avail_to_waker(&new_waker);
488	// Don't decrement the refcount when dropping new_waker by turning it back `into_raw`.
489	let _ = Arc::into_raw(new_waker);
490	res
491}
492// When waking, an error should be fine. Most likely we got two send_datas in a row, both of which
493// failed to fully write, but we only need to call write_buffer_space_avail() once. Otherwise, the
494// sending thread may have already gone away due to a socket close, in which case there's nothing
495// to wake up anyway.
496fn wake_socket_waker(orig_ptr: *const ()) {
497	let sender = unsafe { &mut *(orig_ptr as *mut mpsc::Sender<()>) };
498	let _ = sender.try_send(());
499	drop_socket_waker(orig_ptr);
500}
501fn wake_socket_waker_by_ref(orig_ptr: *const ()) {
502	let sender_ptr = orig_ptr as *const mpsc::Sender<()>;
503	let sender = unsafe { &*sender_ptr };
504	let _ = sender.try_send(());
505}
506fn drop_socket_waker(orig_ptr: *const ()) {
507	let _orig_arc = unsafe { Arc::from_raw(orig_ptr as *mut mpsc::Sender<()>) };
508	// _orig_arc is now dropped
509}
510fn write_avail_to_waker(sender: &Arc<mpsc::Sender<()>>) -> task::RawWaker {
511	let new_ptr = Arc::into_raw(Arc::clone(&sender));
512	task::RawWaker::new(new_ptr as *const (), &SOCK_WAKER_VTABLE)
513}
514
515/// The SocketDescriptor used to refer to sockets by a PeerHandler. This is pub only as it is a
516/// type in the template of PeerHandler.
517pub struct SocketDescriptor {
518	conn: Arc<Mutex<Connection>>,
519	// We store a copy of the mpsc::Sender to wake the read task in an Arc here. While we can
520	// simply clone the sender and store a copy in each waker, that would require allocating for
521	// each waker. Instead, we can simply `Arc::clone`, creating a new reference and store the
522	// pointer in the waker.
523	write_avail_sender: Arc<mpsc::Sender<()>>,
524	id: u64,
525}
526impl SocketDescriptor {
527	fn new(conn: Arc<Mutex<Connection>>) -> Self {
528		let (id, write_avail_sender) = {
529			let us = conn.lock().unwrap();
530			(us.id, Arc::new(us.write_avail.clone()))
531		};
532		Self { conn, id, write_avail_sender }
533	}
534}
535impl peer_handler::SocketDescriptor for SocketDescriptor {
536	fn send_data(&mut self, data: &[u8], resume_read: bool) -> usize {
537		// To send data, we take a lock on our Connection to access the TcpStream, writing to it if
538		// there's room in the kernel buffer, or otherwise create a new Waker with a
539		// SocketDescriptor in it which can wake up the write_avail Sender, waking up the
540		// processing future which will call write_buffer_space_avail and we'll end up back here.
541		let mut us = self.conn.lock().unwrap();
542		if us.writer.is_none() {
543			// The writer gets take()n when it is time to shut down, so just fast-return 0 here.
544			return 0;
545		}
546
547		if resume_read && us.read_paused {
548			// The schedule_read future may go to lock up but end up getting woken up by there
549			// being more room in the write buffer, dropping the other end of this Sender
550			// before we get here, so we ignore any failures to wake it up.
551			us.read_paused = false;
552			let _ = us.read_waker.try_send(());
553		}
554		if data.is_empty() {
555			return 0;
556		}
557		let waker =
558			unsafe { task::Waker::from_raw(write_avail_to_waker(&self.write_avail_sender)) };
559		let mut ctx = task::Context::from_waker(&waker);
560		let mut written_len = 0;
561		loop {
562			match us.writer.as_ref().unwrap().poll_write_ready(&mut ctx) {
563				task::Poll::Ready(Ok(())) => {
564					match us.writer.as_ref().unwrap().try_write(&data[written_len..]) {
565						Ok(res) => {
566							debug_assert_ne!(res, 0);
567							written_len += res;
568							if written_len == data.len() {
569								return written_len;
570							}
571						},
572						Err(ref e) if e.kind() == std::io::ErrorKind::WouldBlock => {
573							continue;
574						},
575						Err(_) => return written_len,
576					}
577				},
578				task::Poll::Ready(Err(_)) => return written_len,
579				task::Poll::Pending => {
580					// We're queued up for a write event now, but we need to make sure we also
581					// pause read given we're now waiting on the remote end to ACK (and in
582					// accordance with the send_data() docs).
583					us.read_paused = true;
584					// Further, to avoid any current pending read causing a `read_event` call, wake
585					// up the read_waker and restart its loop.
586					let _ = us.read_waker.try_send(());
587					return written_len;
588				},
589			}
590		}
591	}
592
593	fn disconnect_socket(&mut self) {
594		let mut us = self.conn.lock().unwrap();
595		us.rl_requested_disconnect = true;
596		// Wake up the sending thread, assuming it is still alive
597		let _ = us.write_avail.try_send(());
598	}
599}
600impl Clone for SocketDescriptor {
601	fn clone(&self) -> Self {
602		Self {
603			conn: Arc::clone(&self.conn),
604			id: self.id,
605			write_avail_sender: Arc::clone(&self.write_avail_sender),
606		}
607	}
608}
609impl Eq for SocketDescriptor {}
610impl PartialEq for SocketDescriptor {
611	fn eq(&self, o: &Self) -> bool {
612		self.id == o.id
613	}
614}
615impl Hash for SocketDescriptor {
616	fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
617		self.id.hash(state);
618	}
619}
620
621#[cfg(test)]
622mod tests {
623	use bitcoin::constants::ChainHash;
624	use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
625	use bitcoin::Network;
626	use lightning::ln::msgs::*;
627	use lightning::ln::peer_handler::{IgnoringMessageHandler, MessageHandler, PeerManager};
628	use lightning::ln::types::ChannelId;
629	use lightning::routing::gossip::NodeId;
630	use lightning::types::features::*;
631	use lightning::util::test_utils::TestNodeSigner;
632
633	use tokio::sync::mpsc;
634
635	use std::mem;
636	use std::sync::atomic::{AtomicBool, Ordering};
637	use std::sync::{Arc, Mutex};
638	use std::time::Duration;
639
640	pub struct TestLogger();
641	impl lightning::util::logger::Logger for TestLogger {
642		fn log(&self, record: lightning::util::logger::Record) {
643			println!(
644				"{:<5} [{} : {}, {}] {}",
645				record.level.to_string(),
646				record.module_path,
647				record.file,
648				record.line,
649				record.args
650			);
651		}
652	}
653
654	struct MsgHandler {
655		expected_pubkey: PublicKey,
656		pubkey_connected: mpsc::Sender<()>,
657		pubkey_disconnected: mpsc::Sender<()>,
658		disconnected_flag: AtomicBool,
659		msg_events: Mutex<Vec<MessageSendEvent>>,
660	}
661	impl RoutingMessageHandler for MsgHandler {
662		fn handle_node_announcement(
663			&self, _their_node_id: Option<PublicKey>, _msg: &NodeAnnouncement,
664		) -> Result<bool, LightningError> {
665			Ok(false)
666		}
667		fn handle_channel_announcement(
668			&self, _their_node_id: Option<PublicKey>, _msg: &ChannelAnnouncement,
669		) -> Result<bool, LightningError> {
670			Ok(false)
671		}
672		fn handle_channel_update(
673			&self, _their_node_id: Option<PublicKey>, _msg: &ChannelUpdate,
674		) -> Result<bool, LightningError> {
675			Ok(false)
676		}
677		fn get_next_channel_announcement(
678			&self, _starting_point: u64,
679		) -> Option<(ChannelAnnouncement, Option<ChannelUpdate>, Option<ChannelUpdate>)> {
680			None
681		}
682		fn get_next_node_announcement(
683			&self, _starting_point: Option<&NodeId>,
684		) -> Option<NodeAnnouncement> {
685			None
686		}
687		fn handle_reply_channel_range(
688			&self, _their_node_id: PublicKey, _msg: ReplyChannelRange,
689		) -> Result<(), LightningError> {
690			Ok(())
691		}
692		fn handle_reply_short_channel_ids_end(
693			&self, _their_node_id: PublicKey, _msg: ReplyShortChannelIdsEnd,
694		) -> Result<(), LightningError> {
695			Ok(())
696		}
697		fn handle_query_channel_range(
698			&self, _their_node_id: PublicKey, _msg: QueryChannelRange,
699		) -> Result<(), LightningError> {
700			Ok(())
701		}
702		fn handle_query_short_channel_ids(
703			&self, _their_node_id: PublicKey, _msg: QueryShortChannelIds,
704		) -> Result<(), LightningError> {
705			Ok(())
706		}
707		fn processing_queue_high(&self) -> bool {
708			false
709		}
710	}
711	impl ChannelMessageHandler for MsgHandler {
712		fn handle_open_channel(&self, _their_node_id: PublicKey, _msg: &OpenChannel) {}
713		fn handle_accept_channel(&self, _their_node_id: PublicKey, _msg: &AcceptChannel) {}
714		fn handle_funding_created(&self, _their_node_id: PublicKey, _msg: &FundingCreated) {}
715		fn handle_funding_signed(&self, _their_node_id: PublicKey, _msg: &FundingSigned) {}
716		fn handle_channel_ready(&self, _their_node_id: PublicKey, _msg: &ChannelReady) {}
717		fn handle_shutdown(&self, _their_node_id: PublicKey, _msg: &Shutdown) {}
718		fn handle_closing_signed(&self, _their_node_id: PublicKey, _msg: &ClosingSigned) {}
719		#[cfg(simple_close)]
720		fn handle_closing_complete(&self, _their_node_id: PublicKey, _msg: ClosingComplete) {}
721		#[cfg(simple_close)]
722		fn handle_closing_sig(&self, _their_node_id: PublicKey, _msg: ClosingSig) {}
723		fn handle_update_add_htlc(&self, _their_node_id: PublicKey, _msg: &UpdateAddHTLC) {}
724		fn handle_update_fulfill_htlc(&self, _their_node_id: PublicKey, _msg: UpdateFulfillHTLC) {}
725		fn handle_update_fail_htlc(&self, _their_node_id: PublicKey, _msg: &UpdateFailHTLC) {}
726		fn handle_update_fail_malformed_htlc(
727			&self, _their_node_id: PublicKey, _msg: &UpdateFailMalformedHTLC,
728		) {
729		}
730		fn handle_commitment_signed(&self, _their_node_id: PublicKey, _msg: &CommitmentSigned) {}
731		fn handle_commitment_signed_batch(
732			&self, _their_node_id: PublicKey, _channel_id: ChannelId, _batch: Vec<CommitmentSigned>,
733		) {
734		}
735		fn handle_revoke_and_ack(&self, _their_node_id: PublicKey, _msg: &RevokeAndACK) {}
736		fn handle_update_fee(&self, _their_node_id: PublicKey, _msg: &UpdateFee) {}
737		fn handle_announcement_signatures(
738			&self, _their_node_id: PublicKey, _msg: &AnnouncementSignatures,
739		) {
740		}
741		fn handle_channel_update(&self, _their_node_id: PublicKey, _msg: &ChannelUpdate) {}
742		fn handle_open_channel_v2(&self, _their_node_id: PublicKey, _msg: &OpenChannelV2) {}
743		fn handle_accept_channel_v2(&self, _their_node_id: PublicKey, _msg: &AcceptChannelV2) {}
744		fn handle_stfu(&self, _their_node_id: PublicKey, _msg: &Stfu) {}
745		fn handle_splice_init(&self, _their_node_id: PublicKey, _msg: &SpliceInit) {}
746		fn handle_splice_ack(&self, _their_node_id: PublicKey, _msg: &SpliceAck) {}
747		fn handle_splice_locked(&self, _their_node_id: PublicKey, _msg: &SpliceLocked) {}
748		fn handle_tx_add_input(&self, _their_node_id: PublicKey, _msg: &TxAddInput) {}
749		fn handle_tx_add_output(&self, _their_node_id: PublicKey, _msg: &TxAddOutput) {}
750		fn handle_tx_remove_input(&self, _their_node_id: PublicKey, _msg: &TxRemoveInput) {}
751		fn handle_tx_remove_output(&self, _their_node_id: PublicKey, _msg: &TxRemoveOutput) {}
752		fn handle_tx_complete(&self, _their_node_id: PublicKey, _msg: &TxComplete) {}
753		fn handle_tx_signatures(&self, _their_node_id: PublicKey, _msg: &TxSignatures) {}
754		fn handle_tx_init_rbf(&self, _their_node_id: PublicKey, _msg: &TxInitRbf) {}
755		fn handle_tx_ack_rbf(&self, _their_node_id: PublicKey, _msg: &TxAckRbf) {}
756		fn handle_tx_abort(&self, _their_node_id: PublicKey, _msg: &TxAbort) {}
757		fn handle_peer_storage(&self, _their_node_id: PublicKey, _msg: PeerStorage) {}
758		fn handle_peer_storage_retrieval(
759			&self, _their_node_id: PublicKey, _msg: PeerStorageRetrieval,
760		) {
761		}
762		fn handle_channel_reestablish(&self, _their_node_id: PublicKey, _msg: &ChannelReestablish) {
763		}
764		fn handle_error(&self, _their_node_id: PublicKey, _msg: &ErrorMessage) {}
765		fn get_chain_hashes(&self) -> Option<Vec<ChainHash>> {
766			Some(vec![ChainHash::using_genesis_block(Network::Testnet)])
767		}
768		fn message_received(&self) {}
769	}
770	impl BaseMessageHandler for MsgHandler {
771		fn peer_disconnected(&self, their_node_id: PublicKey) {
772			if their_node_id == self.expected_pubkey {
773				self.disconnected_flag.store(true, Ordering::SeqCst);
774				// This method is called twice as we're two message handlers. `try_send` will fail
775				// the second time.
776				let _ = self.pubkey_disconnected.clone().try_send(());
777			}
778		}
779		fn peer_connected(
780			&self, their_node_id: PublicKey, _init_msg: &Init, _inbound: bool,
781		) -> Result<(), ()> {
782			if their_node_id == self.expected_pubkey {
783				// This method is called twice as we're two message handlers. `try_send` will fail
784				// the second time.
785				let _ = self.pubkey_connected.clone().try_send(());
786			}
787			Ok(())
788		}
789		fn provided_node_features(&self) -> NodeFeatures {
790			NodeFeatures::empty()
791		}
792		fn provided_init_features(&self, _their_node_id: PublicKey) -> InitFeatures {
793			InitFeatures::empty()
794		}
795		fn get_and_clear_pending_msg_events(&self) -> Vec<MessageSendEvent> {
796			let mut ret = Vec::new();
797			mem::swap(&mut *self.msg_events.lock().unwrap(), &mut ret);
798			ret
799		}
800	}
801
802	fn make_tcp_connection() -> (std::net::TcpStream, std::net::TcpStream) {
803		if let Ok(listener) = std::net::TcpListener::bind("127.0.0.1:9735") {
804			(std::net::TcpStream::connect("127.0.0.1:9735").unwrap(), listener.accept().unwrap().0)
805		} else if let Ok(listener) = std::net::TcpListener::bind("127.0.0.1:19735") {
806			(std::net::TcpStream::connect("127.0.0.1:19735").unwrap(), listener.accept().unwrap().0)
807		} else if let Ok(listener) = std::net::TcpListener::bind("127.0.0.1:9997") {
808			(std::net::TcpStream::connect("127.0.0.1:9997").unwrap(), listener.accept().unwrap().0)
809		} else if let Ok(listener) = std::net::TcpListener::bind("127.0.0.1:9998") {
810			(std::net::TcpStream::connect("127.0.0.1:9998").unwrap(), listener.accept().unwrap().0)
811		} else if let Ok(listener) = std::net::TcpListener::bind("127.0.0.1:9999") {
812			(std::net::TcpStream::connect("127.0.0.1:9999").unwrap(), listener.accept().unwrap().0)
813		} else if let Ok(listener) = std::net::TcpListener::bind("127.0.0.1:46926") {
814			(std::net::TcpStream::connect("127.0.0.1:46926").unwrap(), listener.accept().unwrap().0)
815		} else {
816			panic!("Failed to bind to v4 localhost on common ports");
817		}
818	}
819
820	async fn do_basic_connection_test() {
821		let secp_ctx = Secp256k1::new();
822		let a_key = SecretKey::from_slice(&[1; 32]).unwrap();
823		let b_key = SecretKey::from_slice(&[1; 32]).unwrap();
824		let a_pub = PublicKey::from_secret_key(&secp_ctx, &a_key);
825		let b_pub = PublicKey::from_secret_key(&secp_ctx, &b_key);
826
827		let (a_connected_sender, mut a_connected) = mpsc::channel(1);
828		let (a_disconnected_sender, mut a_disconnected) = mpsc::channel(1);
829		let a_handler = Arc::new(MsgHandler {
830			expected_pubkey: b_pub,
831			pubkey_connected: a_connected_sender,
832			pubkey_disconnected: a_disconnected_sender,
833			disconnected_flag: AtomicBool::new(false),
834			msg_events: Mutex::new(Vec::new()),
835		});
836		let a_msg_handler = MessageHandler {
837			chan_handler: Arc::clone(&a_handler),
838			route_handler: Arc::clone(&a_handler),
839			onion_message_handler: Arc::new(IgnoringMessageHandler {}),
840			custom_message_handler: Arc::new(IgnoringMessageHandler {}),
841			send_only_message_handler: Arc::new(IgnoringMessageHandler {}),
842		};
843		let a_manager = Arc::new(PeerManager::new(
844			a_msg_handler,
845			0,
846			&[1; 32],
847			Arc::new(TestLogger()),
848			Arc::new(TestNodeSigner::new(a_key)),
849		));
850
851		let (b_connected_sender, mut b_connected) = mpsc::channel(1);
852		let (b_disconnected_sender, mut b_disconnected) = mpsc::channel(1);
853		let b_handler = Arc::new(MsgHandler {
854			expected_pubkey: a_pub,
855			pubkey_connected: b_connected_sender,
856			pubkey_disconnected: b_disconnected_sender,
857			disconnected_flag: AtomicBool::new(false),
858			msg_events: Mutex::new(Vec::new()),
859		});
860		let b_msg_handler = MessageHandler {
861			chan_handler: Arc::clone(&b_handler),
862			route_handler: Arc::clone(&b_handler),
863			onion_message_handler: Arc::new(IgnoringMessageHandler {}),
864			custom_message_handler: Arc::new(IgnoringMessageHandler {}),
865			send_only_message_handler: Arc::new(IgnoringMessageHandler {}),
866		};
867		let b_manager = Arc::new(PeerManager::new(
868			b_msg_handler,
869			0,
870			&[2; 32],
871			Arc::new(TestLogger()),
872			Arc::new(TestNodeSigner::new(b_key)),
873		));
874
875		// We bind on localhost, hoping the environment is properly configured with a local
876		// address. This may not always be the case in containers and the like, so if this test is
877		// failing for you check that you have a loopback interface and it is configured with
878		// 127.0.0.1.
879		let (conn_a, conn_b) = make_tcp_connection();
880
881		let fut_a = super::setup_outbound(Arc::clone(&a_manager), b_pub, conn_a);
882		let fut_b = super::setup_inbound(b_manager, conn_b);
883
884		tokio::time::timeout(Duration::from_secs(10), a_connected.recv()).await.unwrap();
885		tokio::time::timeout(Duration::from_secs(1), b_connected.recv()).await.unwrap();
886
887		a_handler.msg_events.lock().unwrap().push(MessageSendEvent::HandleError {
888			node_id: b_pub,
889			action: ErrorAction::DisconnectPeer { msg: None },
890		});
891		assert!(!a_handler.disconnected_flag.load(Ordering::SeqCst));
892		assert!(!b_handler.disconnected_flag.load(Ordering::SeqCst));
893
894		a_manager.process_events();
895		tokio::time::timeout(Duration::from_secs(10), a_disconnected.recv()).await.unwrap();
896		tokio::time::timeout(Duration::from_secs(1), b_disconnected.recv()).await.unwrap();
897		assert!(a_handler.disconnected_flag.load(Ordering::SeqCst));
898		assert!(b_handler.disconnected_flag.load(Ordering::SeqCst));
899
900		fut_a.await;
901		fut_b.await;
902	}
903
904	#[tokio::test(flavor = "multi_thread")]
905	async fn basic_threaded_connection_test() {
906		do_basic_connection_test().await;
907	}
908
909	#[tokio::test]
910	async fn basic_unthreaded_connection_test() {
911		do_basic_connection_test().await;
912	}
913
914	async fn race_disconnect_accept() {
915		// Previously, if we handed an already-disconnected socket to `setup_inbound` we'd panic.
916		// This attempts to find other similar races by opening connections and shutting them down
917		// while connecting. Sadly in testing this did *not* reproduce the previous issue.
918		let secp_ctx = Secp256k1::new();
919		let a_key = SecretKey::from_slice(&[1; 32]).unwrap();
920		let b_key = SecretKey::from_slice(&[2; 32]).unwrap();
921		let b_pub = PublicKey::from_secret_key(&secp_ctx, &b_key);
922
923		let a_msg_handler = MessageHandler {
924			chan_handler: Arc::new(lightning::ln::peer_handler::ErroringMessageHandler::new()),
925			onion_message_handler: Arc::new(IgnoringMessageHandler {}),
926			route_handler: Arc::new(lightning::ln::peer_handler::IgnoringMessageHandler {}),
927			custom_message_handler: Arc::new(IgnoringMessageHandler {}),
928			send_only_message_handler: Arc::new(IgnoringMessageHandler {}),
929		};
930		let a_manager = Arc::new(PeerManager::new(
931			a_msg_handler,
932			0,
933			&[1; 32],
934			Arc::new(TestLogger()),
935			Arc::new(TestNodeSigner::new(a_key)),
936		));
937
938		// Make two connections, one for an inbound and one for an outbound connection
939		let conn_a = {
940			let (conn_a, _) = make_tcp_connection();
941			conn_a
942		};
943		let conn_b = {
944			let (_, conn_b) = make_tcp_connection();
945			conn_b
946		};
947
948		// Call connection setup inside new tokio tasks.
949		let manager_reference = Arc::clone(&a_manager);
950		tokio::spawn(async move { super::setup_inbound(manager_reference, conn_a).await });
951		tokio::spawn(async move { super::setup_outbound(a_manager, b_pub, conn_b).await });
952	}
953
954	#[tokio::test(flavor = "multi_thread")]
955	async fn threaded_race_disconnect_accept() {
956		race_disconnect_accept().await;
957	}
958
959	#[tokio::test]
960	async fn unthreaded_race_disconnect_accept() {
961		race_disconnect_accept().await;
962	}
963}