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