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`], [`ChainMonitor::rebroadcast_pending_claims`]
18 * and [`PeerManager::timer_tick_occurred`] 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::events::Event
34 * [`PeerManager::timer_tick_occurred`]: lightning::ln::peer_handler::PeerManager::timer_tick_occurred
35 * [`PeerManager::process_events`]: lightning::ln::peer_handler::PeerManager::process_events
36 * BackgroundProcessor will immediately stop on drop. It should be stored until shutdown.
38 public class BackgroundProcessor : CommonBase {
39 internal BackgroundProcessor(object _dummy, long ptr) : base(ptr) { }
40 ~BackgroundProcessor() {
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 * [`Persister::persist_manager`] returns an error. In case of an error, the error is retrieved by calling
50 * either [`join`] or [`stop`].
54 * [`Persister::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 the `lightning-persister` crate for LDK's
57 * provided implementation.
59 * [`Persister::persist_graph`] is responsible for writing out the [`NetworkGraph`] to disk, if
60 * [`GossipSync`] is supplied. See [`NetworkGraph::write`] for writing out a [`NetworkGraph`].
61 * See the `lightning-persister` crate for LDK's provided implementation.
63 * Typically, users should either implement [`Persister::persist_manager`] to never return an
64 * error or call [`join`] and handle any error that may arise. For the latter case,
65 * `BackgroundProcessor` must be restarted by calling `start` again after handling the error.
69 * `event_handler` is responsible for handling events that users should be notified of (e.g.,
70 * payment failed). [`BackgroundProcessor`] may decorate the given [`EventHandler`] with common
71 * functionality implemented by other handlers.
72 * [`P2PGossipSync`] if given will update the [`NetworkGraph`] based on payment failures.
76 * If rapid gossip sync is meant to run at startup, pass [`RapidGossipSync`] via `gossip_sync`
77 * to indicate that the [`BackgroundProcessor`] should not prune the [`NetworkGraph`] instance
78 * until the [`RapidGossipSync`] instance completes its first sync.
80 * [top-level documentation]: BackgroundProcessor
81 * [`join`]: Self::join
82 * [`stop`]: Self::stop
83 * [`ChannelManager`]: lightning::ln::channelmanager::ChannelManager
84 * [`ChannelManager::write`]: lightning::ln::channelmanager::ChannelManager#impl-Writeable
85 * [`Persister::persist_manager`]: lightning::util::persist::Persister::persist_manager
86 * [`Persister::persist_graph`]: lightning::util::persist::Persister::persist_graph
87 * [`NetworkGraph`]: lightning::routing::gossip::NetworkGraph
88 * [`NetworkGraph::write`]: lightning::routing::gossip::NetworkGraph#impl-Writeable
90 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) {
91 long ret = bindings.BackgroundProcessor_start(persister.ptr, 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.ptr, scorer.ptr);
92 GC.KeepAlive(persister);
93 GC.KeepAlive(event_handler);
94 GC.KeepAlive(chain_monitor);
95 GC.KeepAlive(channel_manager);
96 GC.KeepAlive(gossip_sync);
97 GC.KeepAlive(peer_manager);
100 if (ret >= 0 && ret <= 4096) { return null; }
101 org.ldk.structs.BackgroundProcessor ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.BackgroundProcessor(null, ret); }
102 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(ret_hu_conv); };
103 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(persister); };
104 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(event_handler); };
105 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(chain_monitor); };
106 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(channel_manager); };
107 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(gossip_sync); };
108 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(peer_manager); };
109 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(logger); };
110 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(scorer); };
115 * Join `BackgroundProcessor`'s thread, returning any error that occurred while persisting
116 * [`ChannelManager`].
120 * This function panics if the background thread has panicked such as while persisting or
123 * [`ChannelManager`]: lightning::ln::channelmanager::ChannelManager
125 public Result_NoneErrorZ join() {
126 long ret = bindings.BackgroundProcessor_join(this.ptr);
128 if (ret >= 0 && ret <= 4096) { return null; }
129 Result_NoneErrorZ ret_hu_conv = Result_NoneErrorZ.constr_from_ptr(ret);
130 if (this != null) { this.ptrs_to.AddLast(this); };
131 // Due to rust's strict-ownership memory model, in some cases we need to "move"
132 // an object to pass exclusive ownership to the function being called.
133 // In most cases, we avoid this being visible in GC'd languages by cloning the object
134 // at the FFI layer, creating a new object which Rust can claim ownership of
135 // However, in some cases (eg here), there is no way to clone an object, and thus
136 // we actually have to pass full ownership to Rust.
137 // Thus, after this call, this is reset to null and is now a dummy object.
143 * Stop `BackgroundProcessor`'s thread, returning any error that occurred while persisting
144 * [`ChannelManager`].
148 * This function panics if the background thread has panicked such as while persisting or
151 * [`ChannelManager`]: lightning::ln::channelmanager::ChannelManager
153 public Result_NoneErrorZ stop() {
154 long ret = bindings.BackgroundProcessor_stop(this.ptr);
156 if (ret >= 0 && ret <= 4096) { return null; }
157 Result_NoneErrorZ ret_hu_conv = Result_NoneErrorZ.constr_from_ptr(ret);
158 if (this != null) { this.ptrs_to.AddLast(this); };
159 // Due to rust's strict-ownership memory model, in some cases we need to "move"
160 // an object to pass exclusive ownership to the function being called.
161 // In most cases, we avoid this being visible in GC'd languages by cloning the object
162 // at the FFI layer, creating a new object which Rust can claim ownership of
163 // However, in some cases (eg here), there is no way to clone an object, and thus
164 // we actually have to pass full ownership to Rust.
165 // Thus, after this call, this is reset to null and is now a dummy object.