[Java] Update auto-generated bindings to 0.0.117
[ldk-java] / src / main / java / org / ldk / structs / MonitorUpdatingPersister.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  * Implements [`Persist`] in a way that writes and reads both [`ChannelMonitor`]s and
13  * [`ChannelMonitorUpdate`]s.
14  * 
15  * # Overview
16  * 
17  * The main benefit this provides over the [`KVStore`]'s [`Persist`] implementation is decreased
18  * I/O bandwidth and storage churn, at the expense of more IOPS (including listing, reading, and
19  * deleting) and complexity. This is because it writes channel monitor differential updates,
20  * whereas the other (default) implementation rewrites the entire monitor on each update. For
21  * routing nodes, updates can happen many times per second to a channel, and monitors can be tens
22  * of megabytes (or more). Updates can be as small as a few hundred bytes.
23  * 
24  * Note that monitors written with `MonitorUpdatingPersister` are _not_ backward-compatible with
25  * the default [`KVStore`]'s [`Persist`] implementation. They have a prepended byte sequence,
26  * [`MONITOR_UPDATING_PERSISTER_PREPEND_SENTINEL`], applied to prevent deserialization with other
27  * persisters. This is because monitors written by this struct _may_ have unapplied updates. In
28  * order to downgrade, you must ensure that all updates are applied to the monitor, and remove the
29  * sentinel bytes.
30  * 
31  * # Storing monitors
32  * 
33  * Monitors are stored by implementing the [`Persist`] trait, which has two functions:
34  * 
35  * - [`Persist::persist_new_channel`], which persists whole [`ChannelMonitor`]s.
36  * - [`Persist::update_persisted_channel`], which persists only a [`ChannelMonitorUpdate`]
37  * 
38  * Whole [`ChannelMonitor`]s are stored in the [`CHANNEL_MONITOR_PERSISTENCE_PRIMARY_NAMESPACE`],
39  * using the familiar encoding of an [`OutPoint`] (for example, `[SOME-64-CHAR-HEX-STRING]_1`).
40  * 
41  * Each [`ChannelMonitorUpdate`] is stored in a dynamic secondary namespace, as follows:
42  * 
43  * - primary namespace: [`CHANNEL_MONITOR_UPDATE_PERSISTENCE_PRIMARY_NAMESPACE`]
44  * - secondary namespace: [the monitor's encoded outpoint name]
45  * 
46  * Under that secondary namespace, each update is stored with a number string, like `21`, which
47  * represents its `update_id` value.
48  * 
49  * For example, consider this channel, named for its transaction ID and index, or [`OutPoint`]:
50  * 
51  * - Transaction ID: `deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef`
52  * - Index: `1`
53  * 
54  * Full channel monitors would be stored at a single key:
55  * 
56  * `[CHANNEL_MONITOR_PERSISTENCE_PRIMARY_NAMESPACE]/deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef_1`
57  * 
58  * Updates would be stored as follows (with `/` delimiting primary_namespace/secondary_namespace/key):
59  * 
60  * ```text
61  * [CHANNEL_MONITOR_UPDATE_PERSISTENCE_PRIMARY_NAMESPACE]/deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef_1/1
62  * [CHANNEL_MONITOR_UPDATE_PERSISTENCE_PRIMARY_NAMESPACE]/deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef_1/2
63  * [CHANNEL_MONITOR_UPDATE_PERSISTENCE_PRIMARY_NAMESPACE]/deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef_1/3
64  * ```
65  * ... and so on.
66  * 
67  * # Reading channel state from storage
68  * 
69  * Channel state can be reconstructed by calling
70  * [`MonitorUpdatingPersister::read_all_channel_monitors_with_updates`]. Alternatively, users can
71  * list channel monitors themselves and load channels individually using
72  * [`MonitorUpdatingPersister::read_channel_monitor_with_updates`].
73  * 
74  * ## EXTREMELY IMPORTANT
75  * 
76  * It is extremely important that your [`KVStore::read`] implementation uses the
77  * [`io::ErrorKind::NotFound`] variant correctly: that is, when a file is not found, and _only_ in
78  * that circumstance (not when there is really a permissions error, for example). This is because
79  * neither channel monitor reading function lists updates. Instead, either reads the monitor, and
80  * using its stored `update_id`, synthesizes update storage keys, and tries them in sequence until
81  * one is not found. All _other_ errors will be bubbled up in the function's [`Result`].
82  * 
83  * # Pruning stale channel updates
84  * 
85  * Stale updates are pruned when a full monitor is written. The old monitor is first read, and if
86  * that succeeds, updates in the range between the old and new monitors are deleted. The `lazy`
87  * flag is used on the [`KVStore::remove`] method, so there are no guarantees that the deletions
88  * will complete. However, stale updates are not a problem for data integrity, since updates are
89  * only read that are higher than the stored [`ChannelMonitor`]'s `update_id`.
90  * 
91  * If you have many stale updates stored (such as after a crash with pending lazy deletes), and
92  * would like to get rid of them, consider using the
93  * [`MonitorUpdatingPersister::cleanup_stale_updates`] function.
94  */
95 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
96 public class MonitorUpdatingPersister extends CommonBase {
97         MonitorUpdatingPersister(Object _dummy, long ptr) { super(ptr); }
98         @Override @SuppressWarnings("deprecation")
99         protected void finalize() throws Throwable {
100                 super.finalize();
101                 if (ptr != 0) { bindings.MonitorUpdatingPersister_free(ptr); }
102         }
103
104         /**
105          * Constructs a new [`MonitorUpdatingPersister`].
106          * 
107          * The `maximum_pending_updates` parameter controls how many updates may be stored before a
108          * [`MonitorUpdatingPersister`] consolidates updates by writing a full monitor. Note that
109          * consolidation will frequently occur with fewer updates than what you set here; this number
110          * is merely the maximum that may be stored. When setting this value, consider that for higher
111          * values of `maximum_pending_updates`:
112          * 
113          * - [`MonitorUpdatingPersister`] will tend to write more [`ChannelMonitorUpdate`]s than
114          * [`ChannelMonitor`]s, approaching one [`ChannelMonitor`] write for every
115          * `maximum_pending_updates` [`ChannelMonitorUpdate`]s.
116          * - [`MonitorUpdatingPersister`] will issue deletes differently. Lazy deletes will come in
117          * \"waves\" for each [`ChannelMonitor`] write. A larger `maximum_pending_updates` means bigger,
118          * less frequent \"waves.\"
119          * - [`MonitorUpdatingPersister`] will potentially have more listing to do if you need to run
120          * [`MonitorUpdatingPersister::cleanup_stale_updates`].
121          */
122         public static MonitorUpdatingPersister of(org.ldk.structs.KVStore kv_store, org.ldk.structs.Logger logger, long maximum_pending_updates, org.ldk.structs.EntropySource entropy_source, org.ldk.structs.SignerProvider signer_provider) {
123                 long ret = bindings.MonitorUpdatingPersister_new(kv_store.ptr, logger.ptr, maximum_pending_updates, entropy_source.ptr, signer_provider.ptr);
124                 Reference.reachabilityFence(kv_store);
125                 Reference.reachabilityFence(logger);
126                 Reference.reachabilityFence(maximum_pending_updates);
127                 Reference.reachabilityFence(entropy_source);
128                 Reference.reachabilityFence(signer_provider);
129                 if (ret >= 0 && ret <= 4096) { return null; }
130                 org.ldk.structs.MonitorUpdatingPersister ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.MonitorUpdatingPersister(null, ret); }
131                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
132                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(kv_store); };
133                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(logger); };
134                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(entropy_source); };
135                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(signer_provider); };
136                 return ret_hu_conv;
137         }
138
139         /**
140          * Reads all stored channel monitors, along with any stored updates for them.
141          * 
142          * It is extremely important that your [`KVStore::read`] implementation uses the
143          * [`io::ErrorKind::NotFound`] variant correctly. For more information, please see the
144          * documentation for [`MonitorUpdatingPersister`].
145          */
146         public Result_CVec_C2Tuple_ThirtyTwoBytesChannelMonitorZZIOErrorZ read_all_channel_monitors_with_updates(org.ldk.structs.BroadcasterInterface broadcaster, org.ldk.structs.FeeEstimator fee_estimator) {
147                 long ret = bindings.MonitorUpdatingPersister_read_all_channel_monitors_with_updates(this.ptr, broadcaster.ptr, fee_estimator.ptr);
148                 Reference.reachabilityFence(this);
149                 Reference.reachabilityFence(broadcaster);
150                 Reference.reachabilityFence(fee_estimator);
151                 if (ret >= 0 && ret <= 4096) { return null; }
152                 Result_CVec_C2Tuple_ThirtyTwoBytesChannelMonitorZZIOErrorZ ret_hu_conv = Result_CVec_C2Tuple_ThirtyTwoBytesChannelMonitorZZIOErrorZ.constr_from_ptr(ret);
153                 if (this != null) { this.ptrs_to.add(broadcaster); };
154                 if (this != null) { this.ptrs_to.add(fee_estimator); };
155                 return ret_hu_conv;
156         }
157
158         /**
159          * Read a single channel monitor, along with any stored updates for it.
160          * 
161          * It is extremely important that your [`KVStore::read`] implementation uses the
162          * [`io::ErrorKind::NotFound`] variant correctly. For more information, please see the
163          * documentation for [`MonitorUpdatingPersister`].
164          * 
165          * For `monitor_key`, channel storage keys be the channel's transaction ID and index, or
166          * [`OutPoint`], with an underscore `_` between them. For example, given:
167          * 
168          * - Transaction ID: `deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef`
169          * - Index: `1`
170          * 
171          * The correct `monitor_key` would be:
172          * `deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef_1`
173          * 
174          * Loading a large number of monitors will be faster if done in parallel. You can use this
175          * function to accomplish this. Take care to limit the number of parallel readers.
176          */
177         public Result_C2Tuple_ThirtyTwoBytesChannelMonitorZIOErrorZ read_channel_monitor_with_updates(org.ldk.structs.BroadcasterInterface broadcaster, org.ldk.structs.FeeEstimator fee_estimator, java.lang.String monitor_key) {
178                 long ret = bindings.MonitorUpdatingPersister_read_channel_monitor_with_updates(this.ptr, broadcaster.ptr, fee_estimator.ptr, monitor_key);
179                 Reference.reachabilityFence(this);
180                 Reference.reachabilityFence(broadcaster);
181                 Reference.reachabilityFence(fee_estimator);
182                 Reference.reachabilityFence(monitor_key);
183                 if (ret >= 0 && ret <= 4096) { return null; }
184                 Result_C2Tuple_ThirtyTwoBytesChannelMonitorZIOErrorZ ret_hu_conv = Result_C2Tuple_ThirtyTwoBytesChannelMonitorZIOErrorZ.constr_from_ptr(ret);
185                 if (this != null) { this.ptrs_to.add(broadcaster); };
186                 if (this != null) { this.ptrs_to.add(fee_estimator); };
187                 return ret_hu_conv;
188         }
189
190         /**
191          * Cleans up stale updates for all monitors.
192          * 
193          * This function works by first listing all monitors, and then for each of them, listing all
194          * updates. The updates that have an `update_id` less than or equal to than the stored monitor
195          * are deleted. The deletion can either be lazy or non-lazy based on the `lazy` flag; this will
196          * be passed to [`KVStore::remove`].
197          */
198         public Result_NoneIOErrorZ cleanup_stale_updates(boolean lazy) {
199                 long ret = bindings.MonitorUpdatingPersister_cleanup_stale_updates(this.ptr, lazy);
200                 Reference.reachabilityFence(this);
201                 Reference.reachabilityFence(lazy);
202                 if (ret >= 0 && ret <= 4096) { return null; }
203                 Result_NoneIOErrorZ ret_hu_conv = Result_NoneIOErrorZ.constr_from_ptr(ret);
204                 return ret_hu_conv;
205         }
206
207         /**
208          * Constructs a new Persist which calls the relevant methods on this_arg.
209          * This copies the `inner` pointer in this_arg and thus the returned Persist must be freed before this_arg is
210          */
211         public Persist as_Persist() {
212                 long ret = bindings.MonitorUpdatingPersister_as_Persist(this.ptr);
213                 Reference.reachabilityFence(this);
214                 if (ret >= 0 && ret <= 4096) { return null; }
215                 Persist ret_hu_conv = new Persist(null, ret);
216                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
217                 return ret_hu_conv;
218         }
219
220 }