698f9fb827644ecbf77cf79fbd43be70ffc326bb
[ldk-java] / src / main / java / org / ldk / structs / BackgroundProcessor.java
1 package org.ldk.structs;
2
3 import org.ldk.impl.bindings;
4 import org.ldk.enums.*;
5 import org.ldk.util.*;
6 import java.util.Arrays;
7 import java.lang.ref.Reference;
8 import javax.annotation.Nullable;
9
10
11 /**
12  * `BackgroundProcessor` takes care of tasks that (1) need to happen periodically to keep
13  * Rust-Lightning running properly, and (2) either can or should be run in the background. Its
14  * responsibilities are:
15  * Processing [`Event`]s with a user-provided [`EventHandler`].
16  * Monitoring whether the [`ChannelManager`] needs to be re-persisted to disk, and if so,
17  * writing it to disk/backups by invoking the callback given to it at startup.
18  * [`ChannelManager`] persistence should be done in the background.
19  * Calling [`ChannelManager::timer_tick_occurred`] and [`PeerManager::timer_tick_occurred`]
20  * at the appropriate intervals.
21  * Calling [`NetworkGraph::remove_stale_channels`] (if a [`GossipSync`] with a [`NetworkGraph`]
22  * is provided to [`BackgroundProcessor::start`]).
23  * 
24  * It will also call [`PeerManager::process_events`] periodically though this shouldn't be relied
25  * upon as doing so may result in high latency.
26  * 
27  * # Note
28  * 
29  * If [`ChannelManager`] persistence fails and the persisted manager becomes out-of-date, then
30  * there is a risk of channels force-closing on startup when the manager realizes it's outdated.
31  * However, as long as [`ChannelMonitor`] backups are sound, no funds besides those used for
32  * unilateral chain closure fees are at risk.
33  * 
34  * [`ChannelMonitor`]: lightning::chain::channelmonitor::ChannelMonitor
35  * [`Event`]: lightning::util::events::Event
36  * BackgroundProcessor will immediately stop on drop. It should be stored until shutdown.
37  */
38 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
39 public class BackgroundProcessor extends CommonBase {
40         BackgroundProcessor(Object _dummy, long ptr) { super(ptr); }
41         @Override @SuppressWarnings("deprecation")
42         protected void finalize() throws Throwable {
43                 super.finalize();
44                 if (ptr != 0) { bindings.BackgroundProcessor_free(ptr); }
45         }
46
47         /**
48          * Start a background thread that takes care of responsibilities enumerated in the [top-level
49          * documentation].
50          * 
51          * The thread runs indefinitely unless the object is dropped, [`stop`] is called, or
52          * [`Persister::persist_manager`] returns an error. In case of an error, the error is retrieved by calling
53          * either [`join`] or [`stop`].
54          * 
55          * # Data Persistence
56          * 
57          * [`Persister::persist_manager`] is responsible for writing out the [`ChannelManager`] to disk, and/or
58          * uploading to one or more backup services. See [`ChannelManager::write`] for writing out a
59          * [`ChannelManager`]. See the `lightning-persister` crate for LDK's
60          * provided implementation.
61          * 
62          * [`Persister::persist_graph`] is responsible for writing out the [`NetworkGraph`] to disk, if
63          * [`GossipSync`] is supplied. See [`NetworkGraph::write`] for writing out a [`NetworkGraph`].
64          * See the `lightning-persister` crate for LDK's provided implementation.
65          * 
66          * Typically, users should either implement [`Persister::persist_manager`] to never return an
67          * error or call [`join`] and handle any error that may arise. For the latter case,
68          * `BackgroundProcessor` must be restarted by calling `start` again after handling the error.
69          * 
70          * # Event Handling
71          * 
72          * `event_handler` is responsible for handling events that users should be notified of (e.g.,
73          * payment failed). [`BackgroundProcessor`] may decorate the given [`EventHandler`] with common
74          * functionality implemented by other handlers.
75          * [`P2PGossipSync`] if given will update the [`NetworkGraph`] based on payment failures.
76          * 
77          * # Rapid Gossip Sync
78          * 
79          * If rapid gossip sync is meant to run at startup, pass [`RapidGossipSync`] via `gossip_sync`
80          * to indicate that the [`BackgroundProcessor`] should not prune the [`NetworkGraph`] instance
81          * until the [`RapidGossipSync`] instance completes its first sync.
82          * 
83          * [top-level documentation]: BackgroundProcessor
84          * [`join`]: Self::join
85          * [`stop`]: Self::stop
86          * [`ChannelManager`]: lightning::ln::channelmanager::ChannelManager
87          * [`ChannelManager::write`]: lightning::ln::channelmanager::ChannelManager#impl-Writeable
88          * [`Persister::persist_manager`]: lightning::util::persist::Persister::persist_manager
89          * [`Persister::persist_graph`]: lightning::util::persist::Persister::persist_graph
90          * [`NetworkGraph`]: lightning::routing::gossip::NetworkGraph
91          * [`NetworkGraph::write`]: lightning::routing::gossip::NetworkGraph#impl-Writeable
92          * 
93          * Note that scorer (or a relevant inner pointer) may be NULL or all-0s to represent None
94          */
95         public static BackgroundProcessor start(Persister persister, EventHandler event_handler, ChainMonitor chain_monitor, ChannelManager channel_manager, GossipSync gossip_sync, PeerManager peer_manager, Logger logger, @Nullable MultiThreadedLockableScore scorer) {
96                 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 == null ? 0 : scorer.ptr);
97                 Reference.reachabilityFence(persister);
98                 Reference.reachabilityFence(event_handler);
99                 Reference.reachabilityFence(chain_monitor);
100                 Reference.reachabilityFence(channel_manager);
101                 Reference.reachabilityFence(gossip_sync);
102                 Reference.reachabilityFence(peer_manager);
103                 Reference.reachabilityFence(logger);
104                 Reference.reachabilityFence(scorer);
105                 if (ret >= 0 && ret <= 4096) { return null; }
106                 org.ldk.structs.BackgroundProcessor ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.BackgroundProcessor(null, ret); }
107                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
108                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(persister); };
109                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(event_handler); };
110                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(chain_monitor); };
111                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(channel_manager); };
112                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(peer_manager); };
113                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(logger); };
114                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(scorer); };
115                 return ret_hu_conv;
116         }
117
118         /**
119          * Join `BackgroundProcessor`'s thread, returning any error that occurred while persisting
120          * [`ChannelManager`].
121          * 
122          * # Panics
123          * 
124          * This function panics if the background thread has panicked such as while persisting or
125          * handling events.
126          * 
127          * [`ChannelManager`]: lightning::ln::channelmanager::ChannelManager
128          */
129         public Result_NoneErrorZ join() {
130                 long ret = bindings.BackgroundProcessor_join(this.ptr);
131                 Reference.reachabilityFence(this);
132                 if (ret >= 0 && ret <= 4096) { return null; }
133                 Result_NoneErrorZ ret_hu_conv = Result_NoneErrorZ.constr_from_ptr(ret);
134                 if (this != null) { this.ptrs_to.add(this); };
135                 // Due to rust's strict-ownership memory model, in some cases we need to "move"
136                 // an object to pass exclusive ownership to the function being called.
137                 // In most cases, we avoid this being visible in GC'd languages by cloning the object
138                 // at the FFI layer, creating a new object which Rust can claim ownership of
139                 // However, in some cases (eg here), there is no way to clone an object, and thus
140                 // we actually have to pass full ownership to Rust.
141                 // Thus, after this call, this is reset to null and is now a dummy object.
142                 this.ptr = 0;;
143                 return ret_hu_conv;
144         }
145
146         /**
147          * Stop `BackgroundProcessor`'s thread, returning any error that occurred while persisting
148          * [`ChannelManager`].
149          * 
150          * # Panics
151          * 
152          * This function panics if the background thread has panicked such as while persisting or
153          * handling events.
154          * 
155          * [`ChannelManager`]: lightning::ln::channelmanager::ChannelManager
156          */
157         public Result_NoneErrorZ stop() {
158                 long ret = bindings.BackgroundProcessor_stop(this.ptr);
159                 Reference.reachabilityFence(this);
160                 if (ret >= 0 && ret <= 4096) { return null; }
161                 Result_NoneErrorZ ret_hu_conv = Result_NoneErrorZ.constr_from_ptr(ret);
162                 if (this != null) { this.ptrs_to.add(this); };
163                 // Due to rust's strict-ownership memory model, in some cases we need to "move"
164                 // an object to pass exclusive ownership to the function being called.
165                 // In most cases, we avoid this being visible in GC'd languages by cloning the object
166                 // at the FFI layer, creating a new object which Rust can claim ownership of
167                 // However, in some cases (eg here), there is no way to clone an object, and thus
168                 // we actually have to pass full ownership to Rust.
169                 // Thus, after this call, this is reset to null and is now a dummy object.
170                 this.ptr = 0;;
171                 return ret_hu_conv;
172         }
173
174 }