1 package org.ldk.structs;
3 import org.ldk.impl.bindings;
4 import org.ldk.enums.*;
6 import java.util.Arrays;
7 import javax.annotation.Nullable;
11 * `BackgroundProcessor` takes care of tasks that (1) need to happen periodically to keep
12 * Rust-Lightning running properly, and (2) either can or should be run in the background. Its
13 * responsibilities are:
14 * Processing [`Event`]s with a user-provided [`EventHandler`].
15 * Monitoring whether the [`ChannelManager`] needs to be re-persisted to disk, and if so,
16 * writing it to disk/backups by invoking the callback given to it at startup.
17 * [`ChannelManager`] persistence should be done in the background.
18 * Calling [`ChannelManager::timer_tick_occurred`] and [`PeerManager::timer_tick_occurred`]
19 * at the appropriate intervals.
21 * It will also call [`PeerManager::process_events`] periodically though this shouldn't be relied
22 * upon as doing so may result in high latency.
26 * If [`ChannelManager`] persistence fails and the persisted manager becomes out-of-date, then
27 * there is a risk of channels force-closing on startup when the manager realizes it's outdated.
28 * However, as long as [`ChannelMonitor`] backups are sound, no funds besides those used for
29 * unilateral chain closure fees are at risk.
31 * [`ChannelMonitor`]: lightning::chain::channelmonitor::ChannelMonitor
32 * [`Event`]: lightning::util::events::Event
33 * BackgroundProcessor will immediately stop on drop. It should be stored until shutdown.
35 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
36 public class BackgroundProcessor extends CommonBase {
37 BackgroundProcessor(Object _dummy, long ptr) { super(ptr); }
38 @Override @SuppressWarnings("deprecation")
39 protected void finalize() throws Throwable {
41 if (ptr != 0) { bindings.BackgroundProcessor_free(ptr); }
45 * Start a background thread that takes care of responsibilities enumerated in the [top-level
48 * The thread runs indefinitely unless the object is dropped, [`stop`] is called, or
49 * `persist_manager` returns an error. In case of an error, the error is retrieved by calling
50 * either [`join`] or [`stop`].
54 * `persist_manager` is responsible for writing out the [`ChannelManager`] to disk, and/or
55 * uploading to one or more backup services. See [`ChannelManager::write`] for writing out a
56 * [`ChannelManager`]. See [`FilesystemPersister::persist_manager`] for Rust-Lightning's
57 * provided implementation.
59 * Typically, users should either implement [`ChannelManagerPersister`] to never return an
60 * error or call [`join`] and handle any error that may arise. For the latter case,
61 * `BackgroundProcessor` must be restarted by calling `start` again after handling the error.
65 * `event_handler` is responsible for handling events that users should be notified of (e.g.,
66 * payment failed). [`BackgroundProcessor`] may decorate the given [`EventHandler`] with common
67 * functionality implemented by other handlers.
68 * [`NetGraphMsgHandler`] if given will update the [`NetworkGraph`] based on payment failures.
70 * [top-level documentation]: Self
71 * [`join`]: Self::join
72 * [`stop`]: Self::stop
73 * [`ChannelManager`]: lightning::ln::channelmanager::ChannelManager
74 * [`ChannelManager::write`]: lightning::ln::channelmanager::ChannelManager#impl-Writeable
75 * [`FilesystemPersister::persist_manager`]: lightning_persister::FilesystemPersister::persist_manager
76 * [`NetworkGraph`]: lightning::routing::network_graph::NetworkGraph
78 * Note that net_graph_msg_handler (or a relevant inner pointer) may be NULL or all-0s to represent None
80 public static BackgroundProcessor start(ChannelManagerPersister persister, EventHandler event_handler, ChainMonitor chain_monitor, ChannelManager channel_manager, @Nullable NetGraphMsgHandler net_graph_msg_handler, PeerManager peer_manager, Logger logger) {
81 long ret = bindings.BackgroundProcessor_start(persister == null ? 0 : persister.ptr, event_handler == null ? 0 : event_handler.ptr, chain_monitor == null ? 0 : chain_monitor.ptr & ~1, channel_manager == null ? 0 : channel_manager.ptr & ~1, net_graph_msg_handler == null ? 0 : net_graph_msg_handler.ptr & ~1, peer_manager == null ? 0 : peer_manager.ptr & ~1, logger == null ? 0 : logger.ptr);
82 if (ret >= 0 && ret <= 4096) { return null; }
83 BackgroundProcessor ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new BackgroundProcessor(null, ret); }
84 ret_hu_conv.ptrs_to.add(ret_hu_conv);
85 ret_hu_conv.ptrs_to.add(persister);
86 ret_hu_conv.ptrs_to.add(event_handler);
87 ret_hu_conv.ptrs_to.add(chain_monitor);
88 ret_hu_conv.ptrs_to.add(channel_manager);
89 ret_hu_conv.ptrs_to.add(net_graph_msg_handler);
90 ret_hu_conv.ptrs_to.add(peer_manager);
91 ret_hu_conv.ptrs_to.add(logger);
96 * Join `BackgroundProcessor`'s thread, returning any error that occurred while persisting
101 * This function panics if the background thread has panicked such as while persisting or
104 * [`ChannelManager`]: lightning::ln::channelmanager::ChannelManager
106 public Result_NoneErrorZ join() {
107 long ret = bindings.BackgroundProcessor_join(this.ptr);
108 if (ret >= 0 && ret <= 4096) { return null; }
109 Result_NoneErrorZ ret_hu_conv = Result_NoneErrorZ.constr_from_ptr(ret);
110 this.ptrs_to.add(this);
111 // Due to rust's strict-ownership memory model, in some cases we need to "move"
112 // an object to pass exclusive ownership to the function being called.
113 // In most cases, we avoid this being visible in GC'd languages by cloning the object
114 // at the FFI layer, creating a new object which Rust can claim ownership of
115 // However, in some cases (eg here), there is no way to clone an object, and thus
116 // we actually have to pass full ownership to Rust.
117 // Thus, after this call, this is reset to null and is now a dummy object.
123 * Stop `BackgroundProcessor`'s thread, returning any error that occurred while persisting
124 * [`ChannelManager`].
128 * This function panics if the background thread has panicked such as while persisting or
131 * [`ChannelManager`]: lightning::ln::channelmanager::ChannelManager
133 public Result_NoneErrorZ stop() {
134 long ret = bindings.BackgroundProcessor_stop(this.ptr);
135 if (ret >= 0 && ret <= 4096) { return null; }
136 Result_NoneErrorZ ret_hu_conv = Result_NoneErrorZ.constr_from_ptr(ret);
137 this.ptrs_to.add(this);
138 // Due to rust's strict-ownership memory model, in some cases we need to "move"
139 // an object to pass exclusive ownership to the function being called.
140 // In most cases, we avoid this being visible in GC'd languages by cloning the object
141 // at the FFI layer, creating a new object which Rust can claim ownership of
142 // However, in some cases (eg here), there is no way to clone an object, and thus
143 // we actually have to pass full ownership to Rust.
144 // Thus, after this call, this is reset to null and is now a dummy object.