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 * [`ChannelManager`]: lightning::ln::channelmanager::ChannelManager
33 * [`ChannelManager::timer_tick_occurred`]: lightning::ln::channelmanager::ChannelManager::timer_tick_occurred
34 * [`ChannelMonitor`]: lightning::chain::channelmonitor::ChannelMonitor
35 * [`Event`]: lightning::events::Event
36 * [`PeerManager::timer_tick_occurred`]: lightning::ln::peer_handler::PeerManager::timer_tick_occurred
37 * [`PeerManager::process_events`]: lightning::ln::peer_handler::PeerManager::process_events
38 * BackgroundProcessor will immediately stop on drop. It should be stored until shutdown.
40 public class BackgroundProcessor : CommonBase {
41 internal BackgroundProcessor(object _dummy, long ptr) : base(ptr) { }
42 ~BackgroundProcessor() {
43 if (ptr != 0) { bindings.BackgroundProcessor_free(ptr); }
47 * Start a background thread that takes care of responsibilities enumerated in the [top-level
50 * The thread runs indefinitely unless the object is dropped, [`stop`] is called, or
51 * [`Persister::persist_manager`] returns an error. In case of an error, the error is retrieved by calling
52 * either [`join`] or [`stop`].
56 * [`Persister::persist_manager`] is responsible for writing out the [`ChannelManager`] to disk, and/or
57 * uploading to one or more backup services. See [`ChannelManager::write`] for writing out a
58 * [`ChannelManager`]. See the `lightning-persister` crate for LDK's
59 * provided implementation.
61 * [`Persister::persist_graph`] is responsible for writing out the [`NetworkGraph`] to disk, if
62 * [`GossipSync`] is supplied. See [`NetworkGraph::write`] for writing out a [`NetworkGraph`].
63 * See the `lightning-persister` crate for LDK's provided implementation.
65 * Typically, users should either implement [`Persister::persist_manager`] to never return an
66 * error or call [`join`] and handle any error that may arise. For the latter case,
67 * `BackgroundProcessor` must be restarted by calling `start` again after handling the error.
71 * `event_handler` is responsible for handling events that users should be notified of (e.g.,
72 * payment failed). [`BackgroundProcessor`] may decorate the given [`EventHandler`] with common
73 * functionality implemented by other handlers.
74 * [`P2PGossipSync`] if given will update the [`NetworkGraph`] based on payment failures.
78 * If rapid gossip sync is meant to run at startup, pass [`RapidGossipSync`] via `gossip_sync`
79 * to indicate that the [`BackgroundProcessor`] should not prune the [`NetworkGraph`] instance
80 * until the [`RapidGossipSync`] instance completes its first sync.
82 * [top-level documentation]: BackgroundProcessor
83 * [`join`]: Self::join
84 * [`stop`]: Self::stop
85 * [`ChannelManager`]: lightning::ln::channelmanager::ChannelManager
86 * [`ChannelManager::write`]: lightning::ln::channelmanager::ChannelManager#impl-Writeable
87 * [`Persister::persist_manager`]: lightning::util::persist::Persister::persist_manager
88 * [`Persister::persist_graph`]: lightning::util::persist::Persister::persist_graph
89 * [`NetworkGraph`]: lightning::routing::gossip::NetworkGraph
90 * [`NetworkGraph::write`]: lightning::routing::gossip::NetworkGraph#impl-Writeable
92 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) {
93 long ret = bindings.BackgroundProcessor_start(persister.ptr, event_handler.ptr, chain_monitor.ptr, channel_manager.ptr, gossip_sync.ptr, peer_manager.ptr, logger.ptr, scorer.ptr);
94 GC.KeepAlive(persister);
95 GC.KeepAlive(event_handler);
96 GC.KeepAlive(chain_monitor);
97 GC.KeepAlive(channel_manager);
98 GC.KeepAlive(gossip_sync);
99 GC.KeepAlive(peer_manager);
100 GC.KeepAlive(logger);
101 GC.KeepAlive(scorer);
102 if (ret >= 0 && ret <= 4096) { return null; }
103 org.ldk.structs.BackgroundProcessor ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.BackgroundProcessor(null, ret); }
104 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(ret_hu_conv); };
105 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(persister); };
106 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(event_handler); };
107 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(chain_monitor); };
108 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(channel_manager); };
109 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(gossip_sync); };
110 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(peer_manager); };
111 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(logger); };
112 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(scorer); };
117 * Join `BackgroundProcessor`'s thread, returning any error that occurred while persisting
118 * [`ChannelManager`].
122 * This function panics if the background thread has panicked such as while persisting or
125 * [`ChannelManager`]: lightning::ln::channelmanager::ChannelManager
127 public Result_NoneIOErrorZ join() {
128 long ret = bindings.BackgroundProcessor_join(this.ptr);
130 if (ret >= 0 && ret <= 4096) { return null; }
131 Result_NoneIOErrorZ ret_hu_conv = Result_NoneIOErrorZ.constr_from_ptr(ret);
132 if (this != null) { this.ptrs_to.AddLast(this); };
133 // Due to rust's strict-ownership memory model, in some cases we need to "move"
134 // an object to pass exclusive ownership to the function being called.
135 // In most cases, we avoid this being visible in GC'd languages by cloning the object
136 // at the FFI layer, creating a new object which Rust can claim ownership of
137 // However, in some cases (eg here), there is no way to clone an object, and thus
138 // we actually have to pass full ownership to Rust.
139 // Thus, after this call, this is reset to null and is now a dummy object.
145 * Stop `BackgroundProcessor`'s thread, returning any error that occurred while persisting
146 * [`ChannelManager`].
150 * This function panics if the background thread has panicked such as while persisting or
153 * [`ChannelManager`]: lightning::ln::channelmanager::ChannelManager
155 public Result_NoneIOErrorZ stop() {
156 long ret = bindings.BackgroundProcessor_stop(this.ptr);
158 if (ret >= 0 && ret <= 4096) { return null; }
159 Result_NoneIOErrorZ ret_hu_conv = Result_NoneIOErrorZ.constr_from_ptr(ret);
160 if (this != null) { this.ptrs_to.AddLast(this); };
161 // Due to rust's strict-ownership memory model, in some cases we need to "move"
162 // an object to pass exclusive ownership to the function being called.
163 // In most cases, we avoid this being visible in GC'd languages by cloning the object
164 // at the FFI layer, creating a new object which Rust can claim ownership of
165 // However, in some cases (eg here), there is no way to clone an object, and thus
166 // we actually have to pass full ownership to Rust.
167 // Thus, after this call, this is reset to null and is now a dummy object.