[Java] Print error stack trace when tests fail
[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`], [`ChainMonitor::rebroadcast_pending_claims`]
20  * and [`PeerManager::timer_tick_occurred`] at the appropriate intervals.
21  * Calling [`NetworkGraph::remove_stale_channels_and_tracking`] (if a [`GossipSync`] with a
22  * [`NetworkGraph`] 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  * [`ChannelManager`]: lightning::ln::channelmanager::ChannelManager
35  * [`ChannelManager::timer_tick_occurred`]: lightning::ln::channelmanager::ChannelManager::timer_tick_occurred
36  * [`ChannelMonitor`]: lightning::chain::channelmonitor::ChannelMonitor
37  * [`Event`]: lightning::events::Event
38  * [`PeerManager::timer_tick_occurred`]: lightning::ln::peer_handler::PeerManager::timer_tick_occurred
39  * [`PeerManager::process_events`]: lightning::ln::peer_handler::PeerManager::process_events
40  * BackgroundProcessor will immediately stop on drop. It should be stored until shutdown.
41  */
42 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
43 public class BackgroundProcessor extends CommonBase {
44         BackgroundProcessor(Object _dummy, long ptr) { super(ptr); }
45         @Override @SuppressWarnings("deprecation")
46         protected void finalize() throws Throwable {
47                 super.finalize();
48                 if (ptr != 0) { bindings.BackgroundProcessor_free(ptr); }
49         }
50
51         /**
52          * Start a background thread that takes care of responsibilities enumerated in the [top-level
53          * documentation].
54          * 
55          * The thread runs indefinitely unless the object is dropped, [`stop`] is called, or
56          * [`Persister::persist_manager`] returns an error. In case of an error, the error is retrieved by calling
57          * either [`join`] or [`stop`].
58          * 
59          * # Data Persistence
60          * 
61          * [`Persister::persist_manager`] is responsible for writing out the [`ChannelManager`] to disk, and/or
62          * uploading to one or more backup services. See [`ChannelManager::write`] for writing out a
63          * [`ChannelManager`]. See the `lightning-persister` crate for LDK's
64          * provided implementation.
65          * 
66          * [`Persister::persist_graph`] is responsible for writing out the [`NetworkGraph`] to disk, if
67          * [`GossipSync`] is supplied. See [`NetworkGraph::write`] for writing out a [`NetworkGraph`].
68          * See the `lightning-persister` crate for LDK's provided implementation.
69          * 
70          * Typically, users should either implement [`Persister::persist_manager`] to never return an
71          * error or call [`join`] and handle any error that may arise. For the latter case,
72          * `BackgroundProcessor` must be restarted by calling `start` again after handling the error.
73          * 
74          * # Event Handling
75          * 
76          * `event_handler` is responsible for handling events that users should be notified of (e.g.,
77          * payment failed). [`BackgroundProcessor`] may decorate the given [`EventHandler`] with common
78          * functionality implemented by other handlers.
79          * [`P2PGossipSync`] if given will update the [`NetworkGraph`] based on payment failures.
80          * 
81          * # Rapid Gossip Sync
82          * 
83          * If rapid gossip sync is meant to run at startup, pass [`RapidGossipSync`] via `gossip_sync`
84          * to indicate that the [`BackgroundProcessor`] should not prune the [`NetworkGraph`] instance
85          * until the [`RapidGossipSync`] instance completes its first sync.
86          * 
87          * [top-level documentation]: BackgroundProcessor
88          * [`join`]: Self::join
89          * [`stop`]: Self::stop
90          * [`ChannelManager`]: lightning::ln::channelmanager::ChannelManager
91          * [`ChannelManager::write`]: lightning::ln::channelmanager::ChannelManager#impl-Writeable
92          * [`Persister::persist_manager`]: lightning::util::persist::Persister::persist_manager
93          * [`Persister::persist_graph`]: lightning::util::persist::Persister::persist_graph
94          * [`NetworkGraph`]: lightning::routing::gossip::NetworkGraph
95          * [`NetworkGraph::write`]: lightning::routing::gossip::NetworkGraph#impl-Writeable
96          */
97         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) {
98                 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);
99                 Reference.reachabilityFence(persister);
100                 Reference.reachabilityFence(event_handler);
101                 Reference.reachabilityFence(chain_monitor);
102                 Reference.reachabilityFence(channel_manager);
103                 Reference.reachabilityFence(gossip_sync);
104                 Reference.reachabilityFence(peer_manager);
105                 Reference.reachabilityFence(logger);
106                 Reference.reachabilityFence(scorer);
107                 if (ret >= 0 && ret <= 4096) { return null; }
108                 org.ldk.structs.BackgroundProcessor ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.BackgroundProcessor(null, ret); }
109                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
110                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(persister); };
111                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(event_handler); };
112                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(chain_monitor); };
113                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(channel_manager); };
114                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(gossip_sync); };
115                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(peer_manager); };
116                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(logger); };
117                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(scorer); };
118                 return ret_hu_conv;
119         }
120
121         /**
122          * Join `BackgroundProcessor`'s thread, returning any error that occurred while persisting
123          * [`ChannelManager`].
124          * 
125          * # Panics
126          * 
127          * This function panics if the background thread has panicked such as while persisting or
128          * handling events.
129          * 
130          * [`ChannelManager`]: lightning::ln::channelmanager::ChannelManager
131          */
132         public Result_NoneIOErrorZ join() {
133                 long ret = bindings.BackgroundProcessor_join(this.ptr);
134                 Reference.reachabilityFence(this);
135                 if (ret >= 0 && ret <= 4096) { return null; }
136                 Result_NoneIOErrorZ ret_hu_conv = Result_NoneIOErrorZ.constr_from_ptr(ret);
137                 if (this != null) { 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.
145                 this.ptr = 0;;
146                 return ret_hu_conv;
147         }
148
149         /**
150          * Stop `BackgroundProcessor`'s thread, returning any error that occurred while persisting
151          * [`ChannelManager`].
152          * 
153          * # Panics
154          * 
155          * This function panics if the background thread has panicked such as while persisting or
156          * handling events.
157          * 
158          * [`ChannelManager`]: lightning::ln::channelmanager::ChannelManager
159          */
160         public Result_NoneIOErrorZ stop() {
161                 long ret = bindings.BackgroundProcessor_stop(this.ptr);
162                 Reference.reachabilityFence(this);
163                 if (ret >= 0 && ret <= 4096) { return null; }
164                 Result_NoneIOErrorZ ret_hu_conv = Result_NoneIOErrorZ.constr_from_ptr(ret);
165                 if (this != null) { this.ptrs_to.add(this); };
166                 // Due to rust's strict-ownership memory model, in some cases we need to "move"
167                 // an object to pass exclusive ownership to the function being called.
168                 // In most cases, we avoid this being visible in GC'd languages by cloning the object
169                 // at the FFI layer, creating a new object which Rust can claim ownership of
170                 // However, in some cases (eg here), there is no way to clone an object, and thus
171                 // we actually have to pass full ownership to Rust.
172                 // Thus, after this call, this is reset to null and is now a dummy object.
173                 this.ptr = 0;;
174                 return ret_hu_conv;
175         }
176
177 }