[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 a full monitor is written. The old monitor is first read, and if
84  * that succeeds, updates in the range between the old and new monitors are deleted. The `lazy`
85  * flag is used on the [`KVStore::remove`] method, so there are no guarantees that the deletions
86  * will complete. However, stale updates are not a problem for data integrity, since updates are
87  * only read that are higher than the stored [`ChannelMonitor`]'s `update_id`.
88  * 
89  * If you have many stale updates stored (such as after a crash with pending lazy deletes), and
90  * would like to get rid of them, consider using the
91  * [`MonitorUpdatingPersister::cleanup_stale_updates`] function.
92  */
93 public class MonitorUpdatingPersister : CommonBase {
94         internal MonitorUpdatingPersister(object _dummy, long ptr) : base(ptr) { }
95         ~MonitorUpdatingPersister() {
96                 if (ptr != 0) { bindings.MonitorUpdatingPersister_free(ptr); }
97         }
98
99         /**
100          * Constructs a new [`MonitorUpdatingPersister`].
101          * 
102          * The `maximum_pending_updates` parameter controls how many updates may be stored before a
103          * [`MonitorUpdatingPersister`] consolidates updates by writing a full monitor. Note that
104          * consolidation will frequently occur with fewer updates than what you set here; this number
105          * is merely the maximum that may be stored. When setting this value, consider that for higher
106          * values of `maximum_pending_updates`:
107          * 
108          * - [`MonitorUpdatingPersister`] will tend to write more [`ChannelMonitorUpdate`]s than
109          * [`ChannelMonitor`]s, approaching one [`ChannelMonitor`] write for every
110          * `maximum_pending_updates` [`ChannelMonitorUpdate`]s.
111          * - [`MonitorUpdatingPersister`] will issue deletes differently. Lazy deletes will come in
112          * \"waves\" for each [`ChannelMonitor`] write. A larger `maximum_pending_updates` means bigger,
113          * less frequent \"waves.\"
114          * - [`MonitorUpdatingPersister`] will potentially have more listing to do if you need to run
115          * [`MonitorUpdatingPersister::cleanup_stale_updates`].
116          */
117         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) {
118                 long ret = bindings.MonitorUpdatingPersister_new(kv_store.ptr, logger.ptr, maximum_pending_updates, entropy_source.ptr, signer_provider.ptr);
119                 GC.KeepAlive(kv_store);
120                 GC.KeepAlive(logger);
121                 GC.KeepAlive(maximum_pending_updates);
122                 GC.KeepAlive(entropy_source);
123                 GC.KeepAlive(signer_provider);
124                 if (ret >= 0 && ret <= 4096) { return null; }
125                 org.ldk.structs.MonitorUpdatingPersister ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.MonitorUpdatingPersister(null, ret); }
126                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(ret_hu_conv); };
127                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(kv_store); };
128                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(logger); };
129                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(entropy_source); };
130                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(signer_provider); };
131                 return ret_hu_conv;
132         }
133
134         /**
135          * Reads all stored channel monitors, along with any stored updates for them.
136          * 
137          * It is extremely important that your [`KVStore::read`] implementation uses the
138          * [`io::ErrorKind::NotFound`] variant correctly. For more information, please see the
139          * documentation for [`MonitorUpdatingPersister`].
140          */
141         public Result_CVec_C2Tuple_ThirtyTwoBytesChannelMonitorZZIOErrorZ read_all_channel_monitors_with_updates(org.ldk.structs.BroadcasterInterface broadcaster, org.ldk.structs.FeeEstimator fee_estimator) {
142                 long ret = bindings.MonitorUpdatingPersister_read_all_channel_monitors_with_updates(this.ptr, broadcaster.ptr, fee_estimator.ptr);
143                 GC.KeepAlive(this);
144                 GC.KeepAlive(broadcaster);
145                 GC.KeepAlive(fee_estimator);
146                 if (ret >= 0 && ret <= 4096) { return null; }
147                 Result_CVec_C2Tuple_ThirtyTwoBytesChannelMonitorZZIOErrorZ ret_hu_conv = Result_CVec_C2Tuple_ThirtyTwoBytesChannelMonitorZZIOErrorZ.constr_from_ptr(ret);
148                 if (this != null) { this.ptrs_to.AddLast(broadcaster); };
149                 if (this != null) { this.ptrs_to.AddLast(fee_estimator); };
150                 return ret_hu_conv;
151         }
152
153         /**
154          * Read a single channel monitor, along with any stored updates for it.
155          * 
156          * It is extremely important that your [`KVStore::read`] implementation uses the
157          * [`io::ErrorKind::NotFound`] variant correctly. For more information, please see the
158          * documentation for [`MonitorUpdatingPersister`].
159          * 
160          * For `monitor_key`, channel storage keys be the channel's transaction ID and index, or
161          * [`OutPoint`], with an underscore `_` between them. For example, given:
162          * 
163          * - Transaction ID: `deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef`
164          * - Index: `1`
165          * 
166          * The correct `monitor_key` would be:
167          * `deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef_1`
168          * 
169          * Loading a large number of monitors will be faster if done in parallel. You can use this
170          * function to accomplish this. Take care to limit the number of parallel readers.
171          */
172         public Result_C2Tuple_ThirtyTwoBytesChannelMonitorZIOErrorZ read_channel_monitor_with_updates(org.ldk.structs.BroadcasterInterface broadcaster, org.ldk.structs.FeeEstimator fee_estimator, string monitor_key) {
173                 long ret = bindings.MonitorUpdatingPersister_read_channel_monitor_with_updates(this.ptr, broadcaster.ptr, fee_estimator.ptr, InternalUtils.encodeString(monitor_key));
174                 GC.KeepAlive(this);
175                 GC.KeepAlive(broadcaster);
176                 GC.KeepAlive(fee_estimator);
177                 GC.KeepAlive(monitor_key);
178                 if (ret >= 0 && ret <= 4096) { return null; }
179                 Result_C2Tuple_ThirtyTwoBytesChannelMonitorZIOErrorZ ret_hu_conv = Result_C2Tuple_ThirtyTwoBytesChannelMonitorZIOErrorZ.constr_from_ptr(ret);
180                 if (this != null) { this.ptrs_to.AddLast(broadcaster); };
181                 if (this != null) { this.ptrs_to.AddLast(fee_estimator); };
182                 return ret_hu_conv;
183         }
184
185         /**
186          * Cleans up stale updates for all monitors.
187          * 
188          * This function works by first listing all monitors, and then for each of them, listing all
189          * updates. The updates that have an `update_id` less than or equal to than the stored monitor
190          * are deleted. The deletion can either be lazy or non-lazy based on the `lazy` flag; this will
191          * be passed to [`KVStore::remove`].
192          */
193         public Result_NoneIOErrorZ cleanup_stale_updates(bool lazy) {
194                 long ret = bindings.MonitorUpdatingPersister_cleanup_stale_updates(this.ptr, lazy);
195                 GC.KeepAlive(this);
196                 GC.KeepAlive(lazy);
197                 if (ret >= 0 && ret <= 4096) { return null; }
198                 Result_NoneIOErrorZ ret_hu_conv = Result_NoneIOErrorZ.constr_from_ptr(ret);
199                 return ret_hu_conv;
200         }
201
202         /**
203          * Constructs a new Persist which calls the relevant methods on this_arg.
204          * This copies the `inner` pointer in this_arg and thus the returned Persist must be freed before this_arg is
205          */
206         public Persist as_Persist() {
207                 long ret = bindings.MonitorUpdatingPersister_as_Persist(this.ptr);
208                 GC.KeepAlive(this);
209                 if (ret >= 0 && ret <= 4096) { return null; }
210                 Persist ret_hu_conv = new Persist(null, ret);
211                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };
212                 return ret_hu_conv;
213         }
214
215 }
216 } } }