Merge pull request #1436 from TheBlueMatt/2022-04-event-process-try-lock
[rust-lightning] / lightning-background-processor / src / lib.rs
1 //! Utilities that take care of tasks that (1) need to happen periodically to keep Rust-Lightning
2 //! running properly, and (2) either can or should be run in the background. See docs for
3 //! [`BackgroundProcessor`] for more details on the nitty-gritty.
4
5 #![deny(broken_intra_doc_links)]
6 #![deny(missing_docs)]
7 #![deny(unsafe_code)]
8
9 #![cfg_attr(docsrs, feature(doc_auto_cfg))]
10
11 #[macro_use] extern crate lightning;
12
13 use lightning::chain;
14 use lightning::chain::chaininterface::{BroadcasterInterface, FeeEstimator};
15 use lightning::chain::chainmonitor::{ChainMonitor, Persist};
16 use lightning::chain::keysinterface::{Sign, KeysInterface};
17 use lightning::ln::channelmanager::ChannelManager;
18 use lightning::ln::msgs::{ChannelMessageHandler, RoutingMessageHandler};
19 use lightning::ln::peer_handler::{CustomMessageHandler, PeerManager, SocketDescriptor};
20 use lightning::routing::network_graph::{NetworkGraph, NetGraphMsgHandler};
21 use lightning::util::events::{Event, EventHandler, EventsProvider};
22 use lightning::util::logger::Logger;
23 use lightning::util::persist::Persister;
24 use std::sync::Arc;
25 use std::sync::atomic::{AtomicBool, Ordering};
26 use std::thread;
27 use std::thread::JoinHandle;
28 use std::time::{Duration, Instant};
29 use std::ops::Deref;
30
31 /// `BackgroundProcessor` takes care of tasks that (1) need to happen periodically to keep
32 /// Rust-Lightning running properly, and (2) either can or should be run in the background. Its
33 /// responsibilities are:
34 /// * Processing [`Event`]s with a user-provided [`EventHandler`].
35 /// * Monitoring whether the [`ChannelManager`] needs to be re-persisted to disk, and if so,
36 ///   writing it to disk/backups by invoking the callback given to it at startup.
37 ///   [`ChannelManager`] persistence should be done in the background.
38 /// * Calling [`ChannelManager::timer_tick_occurred`] and [`PeerManager::timer_tick_occurred`]
39 ///   at the appropriate intervals.
40 /// * Calling [`NetworkGraph::remove_stale_channels`] (if a [`NetGraphMsgHandler`] is provided to
41 ///   [`BackgroundProcessor::start`]).
42 ///
43 /// It will also call [`PeerManager::process_events`] periodically though this shouldn't be relied
44 /// upon as doing so may result in high latency.
45 ///
46 /// # Note
47 ///
48 /// If [`ChannelManager`] persistence fails and the persisted manager becomes out-of-date, then
49 /// there is a risk of channels force-closing on startup when the manager realizes it's outdated.
50 /// However, as long as [`ChannelMonitor`] backups are sound, no funds besides those used for
51 /// unilateral chain closure fees are at risk.
52 ///
53 /// [`ChannelMonitor`]: lightning::chain::channelmonitor::ChannelMonitor
54 /// [`Event`]: lightning::util::events::Event
55 #[must_use = "BackgroundProcessor will immediately stop on drop. It should be stored until shutdown."]
56 pub struct BackgroundProcessor {
57         stop_thread: Arc<AtomicBool>,
58         thread_handle: Option<JoinHandle<Result<(), std::io::Error>>>,
59 }
60
61 #[cfg(not(test))]
62 const FRESHNESS_TIMER: u64 = 60;
63 #[cfg(test)]
64 const FRESHNESS_TIMER: u64 = 1;
65
66 #[cfg(all(not(test), not(debug_assertions)))]
67 const PING_TIMER: u64 = 10;
68 /// Signature operations take a lot longer without compiler optimisations.
69 /// Increasing the ping timer allows for this but slower devices will be disconnected if the
70 /// timeout is reached.
71 #[cfg(all(not(test), debug_assertions))]
72 const PING_TIMER: u64 = 30;
73 #[cfg(test)]
74 const PING_TIMER: u64 = 1;
75
76 /// Prune the network graph of stale entries hourly.
77 const NETWORK_PRUNE_TIMER: u64 = 60 * 60;
78
79 #[cfg(not(test))]
80 const FIRST_NETWORK_PRUNE_TIMER: u64 = 60;
81 #[cfg(test)]
82 const FIRST_NETWORK_PRUNE_TIMER: u64 = 1;
83
84
85 /// Decorates an [`EventHandler`] with common functionality provided by standard [`EventHandler`]s.
86 struct DecoratingEventHandler<
87         E: EventHandler,
88         N: Deref<Target = NetGraphMsgHandler<G, A, L>>,
89         G: Deref<Target = NetworkGraph>,
90         A: Deref,
91         L: Deref,
92 >
93 where A::Target: chain::Access, L::Target: Logger {
94         event_handler: E,
95         net_graph_msg_handler: Option<N>,
96 }
97
98 impl<
99         E: EventHandler,
100         N: Deref<Target = NetGraphMsgHandler<G, A, L>>,
101         G: Deref<Target = NetworkGraph>,
102         A: Deref,
103         L: Deref,
104 > EventHandler for DecoratingEventHandler<E, N, G, A, L>
105 where A::Target: chain::Access, L::Target: Logger {
106         fn handle_event(&self, event: &Event) {
107                 if let Some(event_handler) = &self.net_graph_msg_handler {
108                         event_handler.handle_event(event);
109                 }
110                 self.event_handler.handle_event(event);
111         }
112 }
113
114 impl BackgroundProcessor {
115         /// Start a background thread that takes care of responsibilities enumerated in the [top-level
116         /// documentation].
117         ///
118         /// The thread runs indefinitely unless the object is dropped, [`stop`] is called, or
119         /// [`Persister::persist_manager`] returns an error. In case of an error, the error is retrieved by calling
120         /// either [`join`] or [`stop`].
121         ///
122         /// # Data Persistence
123         ///
124         /// [`Persister::persist_manager`] is responsible for writing out the [`ChannelManager`] to disk, and/or
125         /// uploading to one or more backup services. See [`ChannelManager::write`] for writing out a
126         /// [`ChannelManager`]. See the `lightning-persister` crate for LDK's
127         /// provided implementation.
128         ///
129         /// [`Persister::persist_graph`] is responsible for writing out the [`NetworkGraph`] to disk. See
130         /// [`NetworkGraph::write`] for writing out a [`NetworkGraph`]. See the `lightning-persister` crate
131         /// for LDK's provided implementation.
132         ///
133         /// Typically, users should either implement [`Persister::persist_manager`] to never return an
134         /// error or call [`join`] and handle any error that may arise. For the latter case,
135         /// `BackgroundProcessor` must be restarted by calling `start` again after handling the error.
136         ///
137         /// # Event Handling
138         ///
139         /// `event_handler` is responsible for handling events that users should be notified of (e.g.,
140         /// payment failed). [`BackgroundProcessor`] may decorate the given [`EventHandler`] with common
141         /// functionality implemented by other handlers.
142         /// * [`NetGraphMsgHandler`] if given will update the [`NetworkGraph`] based on payment failures.
143         ///
144         /// [top-level documentation]: BackgroundProcessor
145         /// [`join`]: Self::join
146         /// [`stop`]: Self::stop
147         /// [`ChannelManager`]: lightning::ln::channelmanager::ChannelManager
148         /// [`ChannelManager::write`]: lightning::ln::channelmanager::ChannelManager#impl-Writeable
149         /// [`Persister::persist_manager`]: lightning::util::persist::Persister::persist_manager
150         /// [`Persister::persist_graph`]: lightning::util::persist::Persister::persist_graph
151         /// [`NetworkGraph`]: lightning::routing::network_graph::NetworkGraph
152         /// [`NetworkGraph::write`]: lightning::routing::network_graph::NetworkGraph#impl-Writeable
153         pub fn start<
154                 Signer: 'static + Sign,
155                 CA: 'static + Deref + Send + Sync,
156                 CF: 'static + Deref + Send + Sync,
157                 CW: 'static + Deref + Send + Sync,
158                 T: 'static + Deref + Send + Sync,
159                 K: 'static + Deref + Send + Sync,
160                 F: 'static + Deref + Send + Sync,
161                 G: 'static + Deref<Target = NetworkGraph> + Send + Sync,
162                 L: 'static + Deref + Send + Sync,
163                 P: 'static + Deref + Send + Sync,
164                 Descriptor: 'static + SocketDescriptor + Send + Sync,
165                 CMH: 'static + Deref + Send + Sync,
166                 RMH: 'static + Deref + Send + Sync,
167                 EH: 'static + EventHandler + Send,
168                 PS: 'static + Deref + Send,
169                 M: 'static + Deref<Target = ChainMonitor<Signer, CF, T, F, L, P>> + Send + Sync,
170                 CM: 'static + Deref<Target = ChannelManager<Signer, CW, T, K, F, L>> + Send + Sync,
171                 NG: 'static + Deref<Target = NetGraphMsgHandler<G, CA, L>> + Send + Sync,
172                 UMH: 'static + Deref + Send + Sync,
173                 PM: 'static + Deref<Target = PeerManager<Descriptor, CMH, RMH, L, UMH>> + Send + Sync,
174         >(
175                 persister: PS, event_handler: EH, chain_monitor: M, channel_manager: CM,
176                 net_graph_msg_handler: Option<NG>, peer_manager: PM, logger: L
177         ) -> Self
178         where
179                 CA::Target: 'static + chain::Access,
180                 CF::Target: 'static + chain::Filter,
181                 CW::Target: 'static + chain::Watch<Signer>,
182                 T::Target: 'static + BroadcasterInterface,
183                 K::Target: 'static + KeysInterface<Signer = Signer>,
184                 F::Target: 'static + FeeEstimator,
185                 L::Target: 'static + Logger,
186                 P::Target: 'static + Persist<Signer>,
187                 CMH::Target: 'static + ChannelMessageHandler,
188                 RMH::Target: 'static + RoutingMessageHandler,
189                 UMH::Target: 'static + CustomMessageHandler,
190                 PS::Target: 'static + Persister<Signer, CW, T, K, F, L>
191         {
192                 let stop_thread = Arc::new(AtomicBool::new(false));
193                 let stop_thread_clone = stop_thread.clone();
194                 let handle = thread::spawn(move || -> Result<(), std::io::Error> {
195                         let event_handler = DecoratingEventHandler { event_handler, net_graph_msg_handler: net_graph_msg_handler.as_ref().map(|t| t.deref()) };
196
197                         log_trace!(logger, "Calling ChannelManager's timer_tick_occurred on startup");
198                         channel_manager.timer_tick_occurred();
199
200                         let mut last_freshness_call = Instant::now();
201                         let mut last_ping_call = Instant::now();
202                         let mut last_prune_call = Instant::now();
203                         let mut have_pruned = false;
204
205                         loop {
206                                 channel_manager.process_pending_events(&event_handler);
207                                 chain_monitor.process_pending_events(&event_handler);
208
209                                 // Note that the PeerManager::process_events may block on ChannelManager's locks,
210                                 // hence it comes last here. When the ChannelManager finishes whatever it's doing,
211                                 // we want to ensure we get into `persist_manager` as quickly as we can, especially
212                                 // without running the normal event processing above and handing events to users.
213                                 //
214                                 // Specifically, on an *extremely* slow machine, we may see ChannelManager start
215                                 // processing a message effectively at any point during this loop. In order to
216                                 // minimize the time between such processing completing and persisting the updated
217                                 // ChannelManager, we want to minimize methods blocking on a ChannelManager
218                                 // generally, and as a fallback place such blocking only immediately before
219                                 // persistence.
220                                 peer_manager.process_events();
221
222                                 // We wait up to 100ms, but track how long it takes to detect being put to sleep,
223                                 // see `await_start`'s use below.
224                                 let await_start = Instant::now();
225                                 let updates_available =
226                                         channel_manager.await_persistable_update_timeout(Duration::from_millis(100));
227                                 let await_time = await_start.elapsed();
228
229                                 if updates_available {
230                                         log_trace!(logger, "Persisting ChannelManager...");
231                                         persister.persist_manager(&*channel_manager)?;
232                                         log_trace!(logger, "Done persisting ChannelManager.");
233                                 }
234                                 // Exit the loop if the background processor was requested to stop.
235                                 if stop_thread.load(Ordering::Acquire) == true {
236                                         log_trace!(logger, "Terminating background processor.");
237                                         break;
238                                 }
239                                 if last_freshness_call.elapsed().as_secs() > FRESHNESS_TIMER {
240                                         log_trace!(logger, "Calling ChannelManager's timer_tick_occurred");
241                                         channel_manager.timer_tick_occurred();
242                                         last_freshness_call = Instant::now();
243                                 }
244                                 if await_time > Duration::from_secs(1) {
245                                         // On various platforms, we may be starved of CPU cycles for several reasons.
246                                         // E.g. on iOS, if we've been in the background, we will be entirely paused.
247                                         // Similarly, if we're on a desktop platform and the device has been asleep, we
248                                         // may not get any cycles.
249                                         // We detect this by checking if our max-100ms-sleep, above, ran longer than a
250                                         // full second, at which point we assume sockets may have been killed (they
251                                         // appear to be at least on some platforms, even if it has only been a second).
252                                         // Note that we have to take care to not get here just because user event
253                                         // processing was slow at the top of the loop. For example, the sample client
254                                         // may call Bitcoin Core RPCs during event handling, which very often takes
255                                         // more than a handful of seconds to complete, and shouldn't disconnect all our
256                                         // peers.
257                                         log_trace!(logger, "100ms sleep took more than a second, disconnecting peers.");
258                                         peer_manager.disconnect_all_peers();
259                                         last_ping_call = Instant::now();
260                                 } else if last_ping_call.elapsed().as_secs() > PING_TIMER {
261                                         log_trace!(logger, "Calling PeerManager's timer_tick_occurred");
262                                         peer_manager.timer_tick_occurred();
263                                         last_ping_call = Instant::now();
264                                 }
265
266                                 // Note that we want to run a graph prune once not long after startup before
267                                 // falling back to our usual hourly prunes. This avoids short-lived clients never
268                                 // pruning their network graph. We run once 60 seconds after startup before
269                                 // continuing our normal cadence.
270                                 if last_prune_call.elapsed().as_secs() > if have_pruned { NETWORK_PRUNE_TIMER } else { FIRST_NETWORK_PRUNE_TIMER } {
271                                         if let Some(ref handler) = net_graph_msg_handler {
272                                                 log_trace!(logger, "Pruning network graph of stale entries");
273                                                 handler.network_graph().remove_stale_channels();
274                                                 if let Err(e) = persister.persist_graph(handler.network_graph()) {
275                                                         log_error!(logger, "Error: Failed to persist network graph, check your disk and permissions {}", e)
276                                                 }
277                                                 last_prune_call = Instant::now();
278                                                 have_pruned = true;
279                                         }
280                                 }
281                         }
282
283                         // After we exit, ensure we persist the ChannelManager one final time - this avoids
284                         // some races where users quit while channel updates were in-flight, with
285                         // ChannelMonitor update(s) persisted without a corresponding ChannelManager update.
286                         persister.persist_manager(&*channel_manager)?;
287
288                         // Persist NetworkGraph on exit
289                         if let Some(ref handler) = net_graph_msg_handler {
290                                 persister.persist_graph(handler.network_graph())?;
291                         }
292                         Ok(())
293                 });
294                 Self { stop_thread: stop_thread_clone, thread_handle: Some(handle) }
295         }
296
297         /// Join `BackgroundProcessor`'s thread, returning any error that occurred while persisting
298         /// [`ChannelManager`].
299         ///
300         /// # Panics
301         ///
302         /// This function panics if the background thread has panicked such as while persisting or
303         /// handling events.
304         ///
305         /// [`ChannelManager`]: lightning::ln::channelmanager::ChannelManager
306         pub fn join(mut self) -> Result<(), std::io::Error> {
307                 assert!(self.thread_handle.is_some());
308                 self.join_thread()
309         }
310
311         /// Stop `BackgroundProcessor`'s thread, returning any error that occurred while persisting
312         /// [`ChannelManager`].
313         ///
314         /// # Panics
315         ///
316         /// This function panics if the background thread has panicked such as while persisting or
317         /// handling events.
318         ///
319         /// [`ChannelManager`]: lightning::ln::channelmanager::ChannelManager
320         pub fn stop(mut self) -> Result<(), std::io::Error> {
321                 assert!(self.thread_handle.is_some());
322                 self.stop_and_join_thread()
323         }
324
325         fn stop_and_join_thread(&mut self) -> Result<(), std::io::Error> {
326                 self.stop_thread.store(true, Ordering::Release);
327                 self.join_thread()
328         }
329
330         fn join_thread(&mut self) -> Result<(), std::io::Error> {
331                 match self.thread_handle.take() {
332                         Some(handle) => handle.join().unwrap(),
333                         None => Ok(()),
334                 }
335         }
336 }
337
338 impl Drop for BackgroundProcessor {
339         fn drop(&mut self) {
340                 self.stop_and_join_thread().unwrap();
341         }
342 }
343
344 #[cfg(test)]
345 mod tests {
346         use bitcoin::blockdata::block::BlockHeader;
347         use bitcoin::blockdata::constants::genesis_block;
348         use bitcoin::blockdata::transaction::{Transaction, TxOut};
349         use bitcoin::network::constants::Network;
350         use lightning::chain::{BestBlock, Confirm, chainmonitor};
351         use lightning::chain::channelmonitor::ANTI_REORG_DELAY;
352         use lightning::chain::keysinterface::{InMemorySigner, Recipient, KeysInterface, KeysManager};
353         use lightning::chain::transaction::OutPoint;
354         use lightning::get_event_msg;
355         use lightning::ln::channelmanager::{BREAKDOWN_TIMEOUT, ChainParameters, ChannelManager, SimpleArcChannelManager};
356         use lightning::ln::features::InitFeatures;
357         use lightning::ln::msgs::{ChannelMessageHandler, Init};
358         use lightning::ln::peer_handler::{PeerManager, MessageHandler, SocketDescriptor, IgnoringMessageHandler};
359         use lightning::routing::network_graph::{NetworkGraph, NetGraphMsgHandler};
360         use lightning::util::config::UserConfig;
361         use lightning::util::events::{Event, MessageSendEventsProvider, MessageSendEvent};
362         use lightning::util::ser::Writeable;
363         use lightning::util::test_utils;
364         use lightning::util::persist::KVStorePersister;
365         use lightning_invoice::payment::{InvoicePayer, RetryAttempts};
366         use lightning_invoice::utils::DefaultRouter;
367         use lightning_persister::FilesystemPersister;
368         use std::fs;
369         use std::path::PathBuf;
370         use std::sync::{Arc, Mutex};
371         use std::time::Duration;
372         use super::{BackgroundProcessor, FRESHNESS_TIMER};
373
374         const EVENT_DEADLINE: u64 = 5 * FRESHNESS_TIMER;
375
376         #[derive(Clone, Eq, Hash, PartialEq)]
377         struct TestDescriptor{}
378         impl SocketDescriptor for TestDescriptor {
379                 fn send_data(&mut self, _data: &[u8], _resume_read: bool) -> usize {
380                         0
381                 }
382
383                 fn disconnect_socket(&mut self) {}
384         }
385
386         type ChainMonitor = chainmonitor::ChainMonitor<InMemorySigner, Arc<test_utils::TestChainSource>, Arc<test_utils::TestBroadcaster>, Arc<test_utils::TestFeeEstimator>, Arc<test_utils::TestLogger>, Arc<FilesystemPersister>>;
387
388         struct Node {
389                 node: Arc<SimpleArcChannelManager<ChainMonitor, test_utils::TestBroadcaster, test_utils::TestFeeEstimator, test_utils::TestLogger>>,
390                 net_graph_msg_handler: Option<Arc<NetGraphMsgHandler<Arc<NetworkGraph>, Arc<test_utils::TestChainSource>, Arc<test_utils::TestLogger>>>>,
391                 peer_manager: Arc<PeerManager<TestDescriptor, Arc<test_utils::TestChannelMessageHandler>, Arc<test_utils::TestRoutingMessageHandler>, Arc<test_utils::TestLogger>, IgnoringMessageHandler>>,
392                 chain_monitor: Arc<ChainMonitor>,
393                 persister: Arc<FilesystemPersister>,
394                 tx_broadcaster: Arc<test_utils::TestBroadcaster>,
395                 network_graph: Arc<NetworkGraph>,
396                 logger: Arc<test_utils::TestLogger>,
397                 best_block: BestBlock,
398         }
399
400         impl Drop for Node {
401                 fn drop(&mut self) {
402                         let data_dir = self.persister.get_data_dir();
403                         match fs::remove_dir_all(data_dir.clone()) {
404                                 Err(e) => println!("Failed to remove test persister directory {}: {}", data_dir, e),
405                                 _ => {}
406                         }
407                 }
408         }
409
410         struct Persister {
411                 graph_error: Option<(std::io::ErrorKind, &'static str)>,
412                 manager_error: Option<(std::io::ErrorKind, &'static str)>,
413                 filesystem_persister: FilesystemPersister,
414         }
415
416         impl Persister {
417                 fn new(data_dir: String) -> Self {
418                         let filesystem_persister = FilesystemPersister::new(data_dir.clone());
419                         Self { graph_error: None, manager_error: None, filesystem_persister }
420                 }
421
422                 fn with_graph_error(self, error: std::io::ErrorKind, message: &'static str) -> Self {
423                         Self { graph_error: Some((error, message)), ..self }
424                 }
425
426                 fn with_manager_error(self, error: std::io::ErrorKind, message: &'static str) -> Self {
427                         Self { manager_error: Some((error, message)), ..self }
428                 }
429         }
430
431         impl KVStorePersister for Persister {
432                 fn persist<W: Writeable>(&self, key: &str, object: &W) -> std::io::Result<()> {
433                         if key == "manager" {
434                                 if let Some((error, message)) = self.manager_error {
435                                         return Err(std::io::Error::new(error, message))
436                                 }
437                         }
438
439                         if key == "network_graph" {
440                                 if let Some((error, message)) = self.graph_error {
441                                         return Err(std::io::Error::new(error, message))
442                                 }
443                         }
444
445                         self.filesystem_persister.persist(key, object)
446                 }
447         }
448
449         fn get_full_filepath(filepath: String, filename: String) -> String {
450                 let mut path = PathBuf::from(filepath);
451                 path.push(filename);
452                 path.to_str().unwrap().to_string()
453         }
454
455         fn create_nodes(num_nodes: usize, persist_dir: String) -> Vec<Node> {
456                 let mut nodes = Vec::new();
457                 for i in 0..num_nodes {
458                         let tx_broadcaster = Arc::new(test_utils::TestBroadcaster{txn_broadcasted: Mutex::new(Vec::new()), blocks: Arc::new(Mutex::new(Vec::new()))});
459                         let fee_estimator = Arc::new(test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) });
460                         let chain_source = Arc::new(test_utils::TestChainSource::new(Network::Testnet));
461                         let logger = Arc::new(test_utils::TestLogger::with_id(format!("node {}", i)));
462                         let persister = Arc::new(FilesystemPersister::new(format!("{}_persister_{}", persist_dir, i)));
463                         let seed = [i as u8; 32];
464                         let network = Network::Testnet;
465                         let genesis_block = genesis_block(network);
466                         let now = Duration::from_secs(genesis_block.header.time as u64);
467                         let keys_manager = Arc::new(KeysManager::new(&seed, now.as_secs(), now.subsec_nanos()));
468                         let chain_monitor = Arc::new(chainmonitor::ChainMonitor::new(Some(chain_source.clone()), tx_broadcaster.clone(), logger.clone(), fee_estimator.clone(), persister.clone()));
469                         let best_block = BestBlock::from_genesis(network);
470                         let params = ChainParameters { network, best_block };
471                         let manager = Arc::new(ChannelManager::new(fee_estimator.clone(), chain_monitor.clone(), tx_broadcaster.clone(), logger.clone(), keys_manager.clone(), UserConfig::default(), params));
472                         let network_graph = Arc::new(NetworkGraph::new(genesis_block.header.block_hash()));
473                         let net_graph_msg_handler = Some(Arc::new(NetGraphMsgHandler::new(network_graph.clone(), Some(chain_source.clone()), logger.clone())));
474                         let msg_handler = MessageHandler { chan_handler: Arc::new(test_utils::TestChannelMessageHandler::new()), route_handler: Arc::new(test_utils::TestRoutingMessageHandler::new() )};
475                         let peer_manager = Arc::new(PeerManager::new(msg_handler, keys_manager.get_node_secret(Recipient::Node).unwrap(), &seed, logger.clone(), IgnoringMessageHandler{}));
476                         let node = Node { node: manager, net_graph_msg_handler, peer_manager, chain_monitor, persister, tx_broadcaster, network_graph, logger, best_block };
477                         nodes.push(node);
478                 }
479
480                 for i in 0..num_nodes {
481                         for j in (i+1)..num_nodes {
482                                 nodes[i].node.peer_connected(&nodes[j].node.get_our_node_id(), &Init { features: InitFeatures::known(), remote_network_address: None });
483                                 nodes[j].node.peer_connected(&nodes[i].node.get_our_node_id(), &Init { features: InitFeatures::known(), remote_network_address: None });
484                         }
485                 }
486
487                 nodes
488         }
489
490         macro_rules! open_channel {
491                 ($node_a: expr, $node_b: expr, $channel_value: expr) => {{
492                         begin_open_channel!($node_a, $node_b, $channel_value);
493                         let events = $node_a.node.get_and_clear_pending_events();
494                         assert_eq!(events.len(), 1);
495                         let (temporary_channel_id, tx) = handle_funding_generation_ready!(&events[0], $channel_value);
496                         end_open_channel!($node_a, $node_b, temporary_channel_id, tx);
497                         tx
498                 }}
499         }
500
501         macro_rules! begin_open_channel {
502                 ($node_a: expr, $node_b: expr, $channel_value: expr) => {{
503                         $node_a.node.create_channel($node_b.node.get_our_node_id(), $channel_value, 100, 42, None).unwrap();
504                         $node_b.node.handle_open_channel(&$node_a.node.get_our_node_id(), InitFeatures::known(), &get_event_msg!($node_a, MessageSendEvent::SendOpenChannel, $node_b.node.get_our_node_id()));
505                         $node_a.node.handle_accept_channel(&$node_b.node.get_our_node_id(), InitFeatures::known(), &get_event_msg!($node_b, MessageSendEvent::SendAcceptChannel, $node_a.node.get_our_node_id()));
506                 }}
507         }
508
509         macro_rules! handle_funding_generation_ready {
510                 ($event: expr, $channel_value: expr) => {{
511                         match $event {
512                                 &Event::FundingGenerationReady { temporary_channel_id, channel_value_satoshis, ref output_script, user_channel_id } => {
513                                         assert_eq!(channel_value_satoshis, $channel_value);
514                                         assert_eq!(user_channel_id, 42);
515
516                                         let tx = Transaction { version: 1 as i32, lock_time: 0, input: Vec::new(), output: vec![TxOut {
517                                                 value: channel_value_satoshis, script_pubkey: output_script.clone(),
518                                         }]};
519                                         (temporary_channel_id, tx)
520                                 },
521                                 _ => panic!("Unexpected event"),
522                         }
523                 }}
524         }
525
526         macro_rules! end_open_channel {
527                 ($node_a: expr, $node_b: expr, $temporary_channel_id: expr, $tx: expr) => {{
528                         $node_a.node.funding_transaction_generated(&$temporary_channel_id, $tx.clone()).unwrap();
529                         $node_b.node.handle_funding_created(&$node_a.node.get_our_node_id(), &get_event_msg!($node_a, MessageSendEvent::SendFundingCreated, $node_b.node.get_our_node_id()));
530                         $node_a.node.handle_funding_signed(&$node_b.node.get_our_node_id(), &get_event_msg!($node_b, MessageSendEvent::SendFundingSigned, $node_a.node.get_our_node_id()));
531                 }}
532         }
533
534         fn confirm_transaction_depth(node: &mut Node, tx: &Transaction, depth: u32) {
535                 for i in 1..=depth {
536                         let prev_blockhash = node.best_block.block_hash();
537                         let height = node.best_block.height() + 1;
538                         let header = BlockHeader { version: 0x20000000, prev_blockhash, merkle_root: Default::default(), time: height, bits: 42, nonce: 42 };
539                         let txdata = vec![(0, tx)];
540                         node.best_block = BestBlock::new(header.block_hash(), height);
541                         match i {
542                                 1 => {
543                                         node.node.transactions_confirmed(&header, &txdata, height);
544                                         node.chain_monitor.transactions_confirmed(&header, &txdata, height);
545                                 },
546                                 x if x == depth => {
547                                         node.node.best_block_updated(&header, height);
548                                         node.chain_monitor.best_block_updated(&header, height);
549                                 },
550                                 _ => {},
551                         }
552                 }
553         }
554         fn confirm_transaction(node: &mut Node, tx: &Transaction) {
555                 confirm_transaction_depth(node, tx, ANTI_REORG_DELAY);
556         }
557
558         #[test]
559         fn test_background_processor() {
560                 // Test that when a new channel is created, the ChannelManager needs to be re-persisted with
561                 // updates. Also test that when new updates are available, the manager signals that it needs
562                 // re-persistence and is successfully re-persisted.
563                 let nodes = create_nodes(2, "test_background_processor".to_string());
564
565                 // Go through the channel creation process so that each node has something to persist. Since
566                 // open_channel consumes events, it must complete before starting BackgroundProcessor to
567                 // avoid a race with processing events.
568                 let tx = open_channel!(nodes[0], nodes[1], 100000);
569
570                 // Initiate the background processors to watch each node.
571                 let data_dir = nodes[0].persister.get_data_dir();
572                 let persister = Arc::new(Persister::new(data_dir));
573                 let event_handler = |_: &_| {};
574                 let bg_processor = BackgroundProcessor::start(persister, event_handler, nodes[0].chain_monitor.clone(), nodes[0].node.clone(), nodes[0].net_graph_msg_handler.clone(), nodes[0].peer_manager.clone(), nodes[0].logger.clone());
575
576                 macro_rules! check_persisted_data {
577                         ($node: expr, $filepath: expr) => {
578                                 let mut expected_bytes = Vec::new();
579                                 loop {
580                                         expected_bytes.clear();
581                                         match $node.write(&mut expected_bytes) {
582                                                 Ok(()) => {
583                                                         match std::fs::read($filepath) {
584                                                                 Ok(bytes) => {
585                                                                         if bytes == expected_bytes {
586                                                                                 break
587                                                                         } else {
588                                                                                 continue
589                                                                         }
590                                                                 },
591                                                                 Err(_) => continue
592                                                         }
593                                                 },
594                                                 Err(e) => panic!("Unexpected error: {}", e)
595                                         }
596                                 }
597                         }
598                 }
599
600                 // Check that the initial channel manager data is persisted as expected.
601                 let filepath = get_full_filepath("test_background_processor_persister_0".to_string(), "manager".to_string());
602                 check_persisted_data!(nodes[0].node, filepath.clone());
603
604                 loop {
605                         if !nodes[0].node.get_persistence_condvar_value() { break }
606                 }
607
608                 // Force-close the channel.
609                 nodes[0].node.force_close_channel(&OutPoint { txid: tx.txid(), index: 0 }.to_channel_id()).unwrap();
610
611                 // Check that the force-close updates are persisted.
612                 check_persisted_data!(nodes[0].node, filepath.clone());
613                 loop {
614                         if !nodes[0].node.get_persistence_condvar_value() { break }
615                 }
616
617                 // Check network graph is persisted
618                 let filepath = get_full_filepath("test_background_processor_persister_0".to_string(), "network_graph".to_string());
619                 if let Some(ref handler) = nodes[0].net_graph_msg_handler {
620                         let network_graph = handler.network_graph();
621                         check_persisted_data!(network_graph, filepath.clone());
622                 }
623
624                 assert!(bg_processor.stop().is_ok());
625         }
626
627         #[test]
628         fn test_timer_tick_called() {
629                 // Test that ChannelManager's and PeerManager's `timer_tick_occurred` is called every
630                 // `FRESHNESS_TIMER`.
631                 let nodes = create_nodes(1, "test_timer_tick_called".to_string());
632                 let data_dir = nodes[0].persister.get_data_dir();
633                 let persister = Arc::new(Persister::new(data_dir));
634                 let event_handler = |_: &_| {};
635                 let bg_processor = BackgroundProcessor::start(persister, event_handler, nodes[0].chain_monitor.clone(), nodes[0].node.clone(), nodes[0].net_graph_msg_handler.clone(), nodes[0].peer_manager.clone(), nodes[0].logger.clone());
636                 loop {
637                         let log_entries = nodes[0].logger.lines.lock().unwrap();
638                         let desired_log = "Calling ChannelManager's timer_tick_occurred".to_string();
639                         let second_desired_log = "Calling PeerManager's timer_tick_occurred".to_string();
640                         if log_entries.get(&("lightning_background_processor".to_string(), desired_log)).is_some() &&
641                                         log_entries.get(&("lightning_background_processor".to_string(), second_desired_log)).is_some() {
642                                 break
643                         }
644                 }
645
646                 assert!(bg_processor.stop().is_ok());
647         }
648
649         #[test]
650         fn test_channel_manager_persist_error() {
651                 // Test that if we encounter an error during manager persistence, the thread panics.
652                 let nodes = create_nodes(2, "test_persist_error".to_string());
653                 open_channel!(nodes[0], nodes[1], 100000);
654
655                 let data_dir = nodes[0].persister.get_data_dir();
656                 let persister = Arc::new(Persister::new(data_dir).with_manager_error(std::io::ErrorKind::Other, "test"));
657                 let event_handler = |_: &_| {};
658                 let bg_processor = BackgroundProcessor::start(persister, event_handler, nodes[0].chain_monitor.clone(), nodes[0].node.clone(), nodes[0].net_graph_msg_handler.clone(), nodes[0].peer_manager.clone(), nodes[0].logger.clone());
659                 match bg_processor.join() {
660                         Ok(_) => panic!("Expected error persisting manager"),
661                         Err(e) => {
662                                 assert_eq!(e.kind(), std::io::ErrorKind::Other);
663                                 assert_eq!(e.get_ref().unwrap().to_string(), "test");
664                         },
665                 }
666         }
667
668         #[test]
669         fn test_network_graph_persist_error() {
670                 // Test that if we encounter an error during network graph persistence, an error gets returned.
671                 let nodes = create_nodes(2, "test_persist_network_graph_error".to_string());
672                 let data_dir = nodes[0].persister.get_data_dir();
673                 let persister = Arc::new(Persister::new(data_dir).with_graph_error(std::io::ErrorKind::Other, "test"));
674                 let event_handler = |_: &_| {};
675                 let bg_processor = BackgroundProcessor::start(persister, event_handler, nodes[0].chain_monitor.clone(), nodes[0].node.clone(), nodes[0].net_graph_msg_handler.clone(), nodes[0].peer_manager.clone(), nodes[0].logger.clone());
676
677                 match bg_processor.stop() {
678                         Ok(_) => panic!("Expected error persisting network graph"),
679                         Err(e) => {
680                                 assert_eq!(e.kind(), std::io::ErrorKind::Other);
681                                 assert_eq!(e.get_ref().unwrap().to_string(), "test");
682                         },
683                 }
684         }
685
686         #[test]
687         fn test_background_event_handling() {
688                 let mut nodes = create_nodes(2, "test_background_event_handling".to_string());
689                 let channel_value = 100000;
690                 let data_dir = nodes[0].persister.get_data_dir();
691                 let persister = Arc::new(Persister::new(data_dir.clone()));
692
693                 // Set up a background event handler for FundingGenerationReady events.
694                 let (sender, receiver) = std::sync::mpsc::sync_channel(1);
695                 let event_handler = move |event: &Event| {
696                         sender.send(handle_funding_generation_ready!(event, channel_value)).unwrap();
697                 };
698                 let bg_processor = BackgroundProcessor::start(persister, event_handler, nodes[0].chain_monitor.clone(), nodes[0].node.clone(), nodes[0].net_graph_msg_handler.clone(), nodes[0].peer_manager.clone(), nodes[0].logger.clone());
699
700                 // Open a channel and check that the FundingGenerationReady event was handled.
701                 begin_open_channel!(nodes[0], nodes[1], channel_value);
702                 let (temporary_channel_id, funding_tx) = receiver
703                         .recv_timeout(Duration::from_secs(EVENT_DEADLINE))
704                         .expect("FundingGenerationReady not handled within deadline");
705                 end_open_channel!(nodes[0], nodes[1], temporary_channel_id, funding_tx);
706
707                 // Confirm the funding transaction.
708                 confirm_transaction(&mut nodes[0], &funding_tx);
709                 let as_funding = get_event_msg!(nodes[0], MessageSendEvent::SendFundingLocked, nodes[1].node.get_our_node_id());
710                 confirm_transaction(&mut nodes[1], &funding_tx);
711                 let bs_funding = get_event_msg!(nodes[1], MessageSendEvent::SendFundingLocked, nodes[0].node.get_our_node_id());
712                 nodes[0].node.handle_funding_locked(&nodes[1].node.get_our_node_id(), &bs_funding);
713                 let _as_channel_update = get_event_msg!(nodes[0], MessageSendEvent::SendChannelUpdate, nodes[1].node.get_our_node_id());
714                 nodes[1].node.handle_funding_locked(&nodes[0].node.get_our_node_id(), &as_funding);
715                 let _bs_channel_update = get_event_msg!(nodes[1], MessageSendEvent::SendChannelUpdate, nodes[0].node.get_our_node_id());
716
717                 assert!(bg_processor.stop().is_ok());
718
719                 // Set up a background event handler for SpendableOutputs events.
720                 let (sender, receiver) = std::sync::mpsc::sync_channel(1);
721                 let event_handler = move |event: &Event| sender.send(event.clone()).unwrap();
722                 let persister = Arc::new(Persister::new(data_dir));
723                 let bg_processor = BackgroundProcessor::start(persister, event_handler, nodes[0].chain_monitor.clone(), nodes[0].node.clone(), nodes[0].net_graph_msg_handler.clone(), nodes[0].peer_manager.clone(), nodes[0].logger.clone());
724
725                 // Force close the channel and check that the SpendableOutputs event was handled.
726                 nodes[0].node.force_close_channel(&nodes[0].node.list_channels()[0].channel_id).unwrap();
727                 let commitment_tx = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().pop().unwrap();
728                 confirm_transaction_depth(&mut nodes[0], &commitment_tx, BREAKDOWN_TIMEOUT as u32);
729                 let event = receiver
730                         .recv_timeout(Duration::from_secs(EVENT_DEADLINE))
731                         .expect("SpendableOutputs not handled within deadline");
732                 match event {
733                         Event::SpendableOutputs { .. } => {},
734                         Event::ChannelClosed { .. } => {},
735                         _ => panic!("Unexpected event: {:?}", event),
736                 }
737
738                 assert!(bg_processor.stop().is_ok());
739         }
740
741         #[test]
742         fn test_invoice_payer() {
743                 let keys_manager = test_utils::TestKeysInterface::new(&[0u8; 32], Network::Testnet);
744                 let random_seed_bytes = keys_manager.get_secure_random_bytes();
745                 let nodes = create_nodes(2, "test_invoice_payer".to_string());
746
747                 // Initiate the background processors to watch each node.
748                 let data_dir = nodes[0].persister.get_data_dir();
749                 let persister = Arc::new(Persister::new(data_dir));
750                 let scorer = Arc::new(Mutex::new(test_utils::TestScorer::with_penalty(0)));
751                 let router = DefaultRouter::new(Arc::clone(&nodes[0].network_graph), Arc::clone(&nodes[0].logger), random_seed_bytes);
752                 let invoice_payer = Arc::new(InvoicePayer::new(Arc::clone(&nodes[0].node), router, scorer, Arc::clone(&nodes[0].logger), |_: &_| {}, RetryAttempts(2)));
753                 let event_handler = Arc::clone(&invoice_payer);
754                 let bg_processor = BackgroundProcessor::start(persister, event_handler, nodes[0].chain_monitor.clone(), nodes[0].node.clone(), nodes[0].net_graph_msg_handler.clone(), nodes[0].peer_manager.clone(), nodes[0].logger.clone());
755                 assert!(bg_processor.stop().is_ok());
756         }
757 }