6 namespace org { namespace ldk { namespace structs {
10 * `BackgroundProcessor` takes care of tasks that (1) need to happen periodically to keep
11 * Rust-Lightning running properly, and (2) either can or should be run in the background. Its
12 * responsibilities are:
13 * Processing [`Event`]s with a user-provided [`EventHandler`].
14 * Monitoring whether the [`ChannelManager`] needs to be re-persisted to disk, and if so,
15 * writing it to disk/backups by invoking the callback given to it at startup.
16 * [`ChannelManager`] persistence should be done in the background.
17 * Calling [`ChannelManager::timer_tick_occurred`] and [`PeerManager::timer_tick_occurred`]
18 * at the appropriate intervals.
19 * Calling [`NetworkGraph::remove_stale_channels_and_tracking`] (if a [`GossipSync`] with a
20 * [`NetworkGraph`] is provided to [`BackgroundProcessor::start`]).
22 * It will also call [`PeerManager::process_events`] periodically though this shouldn't be relied
23 * upon as doing so may result in high latency.
27 * If [`ChannelManager`] persistence fails and the persisted manager becomes out-of-date, then
28 * there is a risk of channels force-closing on startup when the manager realizes it's outdated.
29 * However, as long as [`ChannelMonitor`] backups are sound, no funds besides those used for
30 * unilateral chain closure fees are at risk.
32 * [`ChannelMonitor`]: lightning::chain::channelmonitor::ChannelMonitor
33 * [`Event`]: lightning::util::events::Event
34 * BackgroundProcessor will immediately stop on drop. It should be stored until shutdown.
36 public class BackgroundProcessor : CommonBase {
37 internal BackgroundProcessor(object _dummy, long ptr) : base(ptr) { }
38 ~BackgroundProcessor() {
39 if (ptr != 0) { bindings.BackgroundProcessor_free(ptr); }
43 * Start a background thread that takes care of responsibilities enumerated in the [top-level
46 * The thread runs indefinitely unless the object is dropped, [`stop`] is called, or
47 * [`Persister::persist_manager`] returns an error. In case of an error, the error is retrieved by calling
48 * either [`join`] or [`stop`].
52 * [`Persister::persist_manager`] is responsible for writing out the [`ChannelManager`] to disk, and/or
53 * uploading to one or more backup services. See [`ChannelManager::write`] for writing out a
54 * [`ChannelManager`]. See the `lightning-persister` crate for LDK's
55 * provided implementation.
57 * [`Persister::persist_graph`] is responsible for writing out the [`NetworkGraph`] to disk, if
58 * [`GossipSync`] is supplied. See [`NetworkGraph::write`] for writing out a [`NetworkGraph`].
59 * See the `lightning-persister` crate for LDK's provided implementation.
61 * Typically, users should either implement [`Persister::persist_manager`] to never return an
62 * error or call [`join`] and handle any error that may arise. For the latter case,
63 * `BackgroundProcessor` must be restarted by calling `start` again after handling the error.
67 * `event_handler` is responsible for handling events that users should be notified of (e.g.,
68 * payment failed). [`BackgroundProcessor`] may decorate the given [`EventHandler`] with common
69 * functionality implemented by other handlers.
70 * [`P2PGossipSync`] if given will update the [`NetworkGraph`] based on payment failures.
74 * If rapid gossip sync is meant to run at startup, pass [`RapidGossipSync`] via `gossip_sync`
75 * to indicate that the [`BackgroundProcessor`] should not prune the [`NetworkGraph`] instance
76 * until the [`RapidGossipSync`] instance completes its first sync.
78 * [top-level documentation]: BackgroundProcessor
79 * [`join`]: Self::join
80 * [`stop`]: Self::stop
81 * [`ChannelManager`]: lightning::ln::channelmanager::ChannelManager
82 * [`ChannelManager::write`]: lightning::ln::channelmanager::ChannelManager#impl-Writeable
83 * [`Persister::persist_manager`]: lightning::util::persist::Persister::persist_manager
84 * [`Persister::persist_graph`]: lightning::util::persist::Persister::persist_graph
85 * [`NetworkGraph`]: lightning::routing::gossip::NetworkGraph
86 * [`NetworkGraph::write`]: lightning::routing::gossip::NetworkGraph#impl-Writeable
88 public static BackgroundProcessor start(org.ldk.structs.Persister persister, org.ldk.structs.EventHandler event_handler, org.ldk.structs.ChainMonitor chain_monitor, org.ldk.structs.ChannelManager channel_manager, org.ldk.structs.GossipSync gossip_sync, org.ldk.structs.PeerManager peer_manager, org.ldk.structs.Logger logger, org.ldk.structs.Option_WriteableScoreZ scorer) {
89 long ret = bindings.BackgroundProcessor_start(persister == null ? 0 : persister.ptr, event_handler == null ? 0 : event_handler.ptr, chain_monitor == null ? 0 : chain_monitor.ptr, channel_manager == null ? 0 : channel_manager.ptr, gossip_sync.ptr, peer_manager == null ? 0 : peer_manager.ptr, logger == null ? 0 : logger.ptr, scorer.ptr);
90 GC.KeepAlive(persister);
91 GC.KeepAlive(event_handler);
92 GC.KeepAlive(chain_monitor);
93 GC.KeepAlive(channel_manager);
94 GC.KeepAlive(gossip_sync);
95 GC.KeepAlive(peer_manager);
98 if (ret >= 0 && ret <= 4096) { return null; }
99 org.ldk.structs.BackgroundProcessor ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.BackgroundProcessor(null, ret); }
100 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(ret_hu_conv); };
101 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(persister); };
102 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(event_handler); };
103 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(chain_monitor); };
104 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(channel_manager); };
105 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(peer_manager); };
106 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(logger); };
107 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(scorer); };
112 * Join `BackgroundProcessor`'s thread, returning any error that occurred while persisting
113 * [`ChannelManager`].
117 * This function panics if the background thread has panicked such as while persisting or
120 * [`ChannelManager`]: lightning::ln::channelmanager::ChannelManager
122 public Result_NoneErrorZ join() {
123 long ret = bindings.BackgroundProcessor_join(this.ptr);
125 if (ret >= 0 && ret <= 4096) { return null; }
126 Result_NoneErrorZ ret_hu_conv = Result_NoneErrorZ.constr_from_ptr(ret);
127 if (this != null) { this.ptrs_to.AddLast(this); };
128 // Due to rust's strict-ownership memory model, in some cases we need to "move"
129 // an object to pass exclusive ownership to the function being called.
130 // In most cases, we avoid this being visible in GC'd languages by cloning the object
131 // at the FFI layer, creating a new object which Rust can claim ownership of
132 // However, in some cases (eg here), there is no way to clone an object, and thus
133 // we actually have to pass full ownership to Rust.
134 // Thus, after this call, this is reset to null and is now a dummy object.
140 * Stop `BackgroundProcessor`'s thread, returning any error that occurred while persisting
141 * [`ChannelManager`].
145 * This function panics if the background thread has panicked such as while persisting or
148 * [`ChannelManager`]: lightning::ln::channelmanager::ChannelManager
150 public Result_NoneErrorZ stop() {
151 long ret = bindings.BackgroundProcessor_stop(this.ptr);
153 if (ret >= 0 && ret <= 4096) { return null; }
154 Result_NoneErrorZ ret_hu_conv = Result_NoneErrorZ.constr_from_ptr(ret);
155 if (this != null) { this.ptrs_to.AddLast(this); };
156 // Due to rust's strict-ownership memory model, in some cases we need to "move"
157 // an object to pass exclusive ownership to the function being called.
158 // In most cases, we avoid this being visible in GC'd languages by cloning the object
159 // at the FFI layer, creating a new object which Rust can claim ownership of
160 // However, in some cases (eg here), there is no way to clone an object, and thus
161 // we actually have to pass full ownership to Rust.
162 // Thus, after this call, this is reset to null and is now a dummy object.