0237bcb2c8703e97e698f0ba2c4344d651ac0377
[ldk-java] / src / main / java / org / ldk / structs / Persist.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  * `Persist` defines behavior for persisting channel monitors: this could mean
12  * writing once to disk, and/or uploading to one or more backup services.
13  * 
14  * Each method can return three possible values:
15  * If persistence (including any relevant `fsync()` calls) happens immediately, the
16  * implementation should return `Ok(())`, indicating normal channel operation should continue.
17  * If persistence happens asynchronously, implementations should first ensure the
18  * [`ChannelMonitor`] or [`ChannelMonitorUpdate`] are written durably to disk, and then return
19  * `Err(ChannelMonitorUpdateErr::TemporaryFailure)` while the update continues in the
20  * background. Once the update completes, [`ChainMonitor::channel_monitor_updated`] should be
21  * called with the corresponding [`MonitorUpdateId`].
22  * 
23  * Note that unlike the direct [`chain::Watch`] interface,
24  * [`ChainMonitor::channel_monitor_updated`] must be called once for *each* update which occurs.
25  * 
26  * If persistence fails for some reason, implementations should return
27  * `Err(ChannelMonitorUpdateErr::PermanentFailure)`, in which case the channel will likely be
28  * closed without broadcasting the latest state. See
29  * [`ChannelMonitorUpdateErr::PermanentFailure`] for more details.
30  */
31 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
32 public class Persist extends CommonBase {
33         final bindings.LDKPersist bindings_instance;
34         Persist(Object _dummy, long ptr) { super(ptr); bindings_instance = null; }
35         private Persist(bindings.LDKPersist arg) {
36                 super(bindings.LDKPersist_new(arg));
37                 this.ptrs_to.add(arg);
38                 this.bindings_instance = arg;
39         }
40         @Override @SuppressWarnings("deprecation")
41         protected void finalize() throws Throwable {
42                 if (ptr != 0) { bindings.Persist_free(ptr); } super.finalize();
43         }
44
45         public static interface PersistInterface {
46                 /**
47                  * Persist a new channel's data in response to a [`chain::Watch::watch_channel`] call. This is
48                  * called by [`ChannelManager`] for new channels, or may be called directly, e.g. on startup.
49                  * 
50                  * The data can be stored any way you want, but the identifier provided by LDK is the
51                  * channel's outpoint (and it is up to you to maintain a correct mapping between the outpoint
52                  * and the stored channel data). Note that you **must** persist every new monitor to disk.
53                  * 
54                  * The `update_id` is used to identify this call to [`ChainMonitor::channel_monitor_updated`],
55                  * if you return [`ChannelMonitorUpdateErr::TemporaryFailure`].
56                  * 
57                  * See [`Writeable::write`] on [`ChannelMonitor`] for writing out a `ChannelMonitor`
58                  * and [`ChannelMonitorUpdateErr`] for requirements when returning errors.
59                  * 
60                  * [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
61                  * [`Writeable::write`]: crate::util::ser::Writeable::write
62                  */
63                 Result_NoneChannelMonitorUpdateErrZ persist_new_channel(OutPoint channel_id, ChannelMonitor data, MonitorUpdateId update_id);
64                 /**
65                  * Update one channel's data. The provided [`ChannelMonitor`] has already applied the given
66                  * update.
67                  * 
68                  * Note that on every update, you **must** persist either the [`ChannelMonitorUpdate`] or the
69                  * updated monitor itself to disk/backups. See the [`Persist`] trait documentation for more
70                  * details.
71                  * 
72                  * During blockchain synchronization operations, this may be called with no
73                  * [`ChannelMonitorUpdate`], in which case the full [`ChannelMonitor`] needs to be persisted.
74                  * Note that after the full [`ChannelMonitor`] is persisted any previous
75                  * [`ChannelMonitorUpdate`]s which were persisted should be discarded - they can no longer be
76                  * applied to the persisted [`ChannelMonitor`] as they were already applied.
77                  * 
78                  * If an implementer chooses to persist the updates only, they need to make
79                  * sure that all the updates are applied to the `ChannelMonitors` *before
80                  * the set of channel monitors is given to the `ChannelManager`
81                  * deserialization routine. See [`ChannelMonitor::update_monitor`] for
82                  * applying a monitor update to a monitor. If full `ChannelMonitors` are
83                  * persisted, then there is no need to persist individual updates.
84                  * 
85                  * Note that there could be a performance tradeoff between persisting complete
86                  * channel monitors on every update vs. persisting only updates and applying
87                  * them in batches. The size of each monitor grows `O(number of state updates)`
88                  * whereas updates are small and `O(1)`.
89                  * 
90                  * The `update_id` is used to identify this call to [`ChainMonitor::channel_monitor_updated`],
91                  * if you return [`ChannelMonitorUpdateErr::TemporaryFailure`].
92                  * 
93                  * See [`Writeable::write`] on [`ChannelMonitor`] for writing out a `ChannelMonitor`,
94                  * [`Writeable::write`] on [`ChannelMonitorUpdate`] for writing out an update, and
95                  * [`ChannelMonitorUpdateErr`] for requirements when returning errors.
96                  * 
97                  * [`Writeable::write`]: crate::util::ser::Writeable::write
98                  * 
99                  * Note that update (or a relevant inner pointer) may be NULL or all-0s to represent None
100                  */
101                 Result_NoneChannelMonitorUpdateErrZ update_persisted_channel(OutPoint channel_id, ChannelMonitorUpdate update, ChannelMonitor data, MonitorUpdateId update_id);
102         }
103         private static class LDKPersistHolder { Persist held; }
104         public static Persist new_impl(PersistInterface arg) {
105                 final LDKPersistHolder impl_holder = new LDKPersistHolder();
106                 impl_holder.held = new Persist(new bindings.LDKPersist() {
107                         @Override public long persist_new_channel(long channel_id, long data, long update_id) {
108                                 OutPoint channel_id_hu_conv = null; if (channel_id < 0 || channel_id > 4096) { channel_id_hu_conv = new OutPoint(null, channel_id); }
109                                 channel_id_hu_conv.ptrs_to.add(this);
110                                 ChannelMonitor data_hu_conv = null; if (data < 0 || data > 4096) { data_hu_conv = new ChannelMonitor(null, data); }
111                                 MonitorUpdateId update_id_hu_conv = null; if (update_id < 0 || update_id > 4096) { update_id_hu_conv = new MonitorUpdateId(null, update_id); }
112                                 update_id_hu_conv.ptrs_to.add(this);
113                                 Result_NoneChannelMonitorUpdateErrZ ret = arg.persist_new_channel(channel_id_hu_conv, data_hu_conv, update_id_hu_conv);
114                                 long result = ret == null ? 0 : ret.clone_ptr();
115                                 return result;
116                         }
117                         @Override public long update_persisted_channel(long channel_id, long update, long data, long update_id) {
118                                 OutPoint channel_id_hu_conv = null; if (channel_id < 0 || channel_id > 4096) { channel_id_hu_conv = new OutPoint(null, channel_id); }
119                                 channel_id_hu_conv.ptrs_to.add(this);
120                                 ChannelMonitorUpdate update_hu_conv = null; if (update < 0 || update > 4096) { update_hu_conv = new ChannelMonitorUpdate(null, update); }
121                                 ChannelMonitor data_hu_conv = null; if (data < 0 || data > 4096) { data_hu_conv = new ChannelMonitor(null, data); }
122                                 MonitorUpdateId update_id_hu_conv = null; if (update_id < 0 || update_id > 4096) { update_id_hu_conv = new MonitorUpdateId(null, update_id); }
123                                 update_id_hu_conv.ptrs_to.add(this);
124                                 Result_NoneChannelMonitorUpdateErrZ ret = arg.update_persisted_channel(channel_id_hu_conv, update_hu_conv, data_hu_conv, update_id_hu_conv);
125                                 long result = ret == null ? 0 : ret.clone_ptr();
126                                 return result;
127                         }
128                 });
129                 return impl_holder.held;
130         }
131         /**
132          * Persist a new channel's data in response to a [`chain::Watch::watch_channel`] call. This is
133          * called by [`ChannelManager`] for new channels, or may be called directly, e.g. on startup.
134          * 
135          * The data can be stored any way you want, but the identifier provided by LDK is the
136          * channel's outpoint (and it is up to you to maintain a correct mapping between the outpoint
137          * and the stored channel data). Note that you **must** persist every new monitor to disk.
138          * 
139          * The `update_id` is used to identify this call to [`ChainMonitor::channel_monitor_updated`],
140          * if you return [`ChannelMonitorUpdateErr::TemporaryFailure`].
141          * 
142          * See [`Writeable::write`] on [`ChannelMonitor`] for writing out a `ChannelMonitor`
143          * and [`ChannelMonitorUpdateErr`] for requirements when returning errors.
144          * 
145          * [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
146          * [`Writeable::write`]: crate::util::ser::Writeable::write
147          */
148         public Result_NoneChannelMonitorUpdateErrZ persist_new_channel(OutPoint channel_id, ChannelMonitor data, MonitorUpdateId update_id) {
149                 long ret = bindings.Persist_persist_new_channel(this.ptr, channel_id == null ? 0 : channel_id.ptr & ~1, data == null ? 0 : data.ptr & ~1, update_id == null ? 0 : update_id.ptr & ~1);
150                 Reference.reachabilityFence(this);
151                 Reference.reachabilityFence(channel_id);
152                 Reference.reachabilityFence(data);
153                 Reference.reachabilityFence(update_id);
154                 if (ret >= 0 && ret <= 4096) { return null; }
155                 Result_NoneChannelMonitorUpdateErrZ ret_hu_conv = Result_NoneChannelMonitorUpdateErrZ.constr_from_ptr(ret);
156                 this.ptrs_to.add(data);
157                 return ret_hu_conv;
158         }
159
160         /**
161          * Update one channel's data. The provided [`ChannelMonitor`] has already applied the given
162          * update.
163          * 
164          * Note that on every update, you **must** persist either the [`ChannelMonitorUpdate`] or the
165          * updated monitor itself to disk/backups. See the [`Persist`] trait documentation for more
166          * details.
167          * 
168          * During blockchain synchronization operations, this may be called with no
169          * [`ChannelMonitorUpdate`], in which case the full [`ChannelMonitor`] needs to be persisted.
170          * Note that after the full [`ChannelMonitor`] is persisted any previous
171          * [`ChannelMonitorUpdate`]s which were persisted should be discarded - they can no longer be
172          * applied to the persisted [`ChannelMonitor`] as they were already applied.
173          * 
174          * If an implementer chooses to persist the updates only, they need to make
175          * sure that all the updates are applied to the `ChannelMonitors` *before
176          * the set of channel monitors is given to the `ChannelManager`
177          * deserialization routine. See [`ChannelMonitor::update_monitor`] for
178          * applying a monitor update to a monitor. If full `ChannelMonitors` are
179          * persisted, then there is no need to persist individual updates.
180          * 
181          * Note that there could be a performance tradeoff between persisting complete
182          * channel monitors on every update vs. persisting only updates and applying
183          * them in batches. The size of each monitor grows `O(number of state updates)`
184          * whereas updates are small and `O(1)`.
185          * 
186          * The `update_id` is used to identify this call to [`ChainMonitor::channel_monitor_updated`],
187          * if you return [`ChannelMonitorUpdateErr::TemporaryFailure`].
188          * 
189          * See [`Writeable::write`] on [`ChannelMonitor`] for writing out a `ChannelMonitor`,
190          * [`Writeable::write`] on [`ChannelMonitorUpdate`] for writing out an update, and
191          * [`ChannelMonitorUpdateErr`] for requirements when returning errors.
192          * 
193          * [`Writeable::write`]: crate::util::ser::Writeable::write
194          * 
195          * Note that update (or a relevant inner pointer) may be NULL or all-0s to represent None
196          */
197         public Result_NoneChannelMonitorUpdateErrZ update_persisted_channel(OutPoint channel_id, @Nullable ChannelMonitorUpdate update, ChannelMonitor data, MonitorUpdateId update_id) {
198                 long ret = bindings.Persist_update_persisted_channel(this.ptr, channel_id == null ? 0 : channel_id.ptr & ~1, update == null ? 0 : update.ptr & ~1, data == null ? 0 : data.ptr & ~1, update_id == null ? 0 : update_id.ptr & ~1);
199                 Reference.reachabilityFence(this);
200                 Reference.reachabilityFence(channel_id);
201                 Reference.reachabilityFence(update);
202                 Reference.reachabilityFence(data);
203                 Reference.reachabilityFence(update_id);
204                 if (ret >= 0 && ret <= 4096) { return null; }
205                 Result_NoneChannelMonitorUpdateErrZ ret_hu_conv = Result_NoneChannelMonitorUpdateErrZ.constr_from_ptr(ret);
206                 this.ptrs_to.add(update);
207                 this.ptrs_to.add(data);
208                 return ret_hu_conv;
209         }
210
211 }