f424851a8d37e88b3e22351d27ba72abdedd5022
[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 javax.annotation.Nullable;
8
9 /**
10  * `Persist` defines behavior for persisting channel monitors: this could mean
11  * writing once to disk, and/or uploading to one or more backup services.
12  * 
13  * Note that for every new monitor, you **must** persist the new `ChannelMonitor`
14  * to disk/backups. And, on every update, you **must** persist either the
15  * `ChannelMonitorUpdate` or the updated monitor itself. Otherwise, there is risk
16  * of situations such as revoking a transaction, then crashing before this
17  * revocation can be persisted, then unintentionally broadcasting a revoked
18  * transaction and losing money. This is a risk because previous channel states
19  * are toxic, so it's important that whatever channel state is persisted is
20  * kept up-to-date.
21  */
22 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
23 public class Persist extends CommonBase {
24         final bindings.LDKPersist bindings_instance;
25         Persist(Object _dummy, long ptr) { super(ptr); bindings_instance = null; }
26         private Persist(bindings.LDKPersist arg) {
27                 super(bindings.LDKPersist_new(arg));
28                 this.ptrs_to.add(arg);
29                 this.bindings_instance = arg;
30         }
31         @Override @SuppressWarnings("deprecation")
32         protected void finalize() throws Throwable {
33                 if (ptr != 0) { bindings.Persist_free(ptr); } super.finalize();
34         }
35
36         public static interface PersistInterface {
37                 /**
38                  * Persist a new channel's data. The data can be stored any way you want, but
39                  * the identifier provided by Rust-Lightning is the channel's outpoint (and
40                  * it is up to you to maintain a correct mapping between the outpoint and the
41                  * stored channel data). Note that you **must** persist every new monitor to
42                  * disk. See the `Persist` trait documentation for more details.
43                  * 
44                  * See [`Writeable::write`] on [`ChannelMonitor`] for writing out a `ChannelMonitor`
45                  * and [`ChannelMonitorUpdateErr`] for requirements when returning errors.
46                  * 
47                  * [`Writeable::write`]: crate::util::ser::Writeable::write
48                  */
49                 Result_NoneChannelMonitorUpdateErrZ persist_new_channel(OutPoint id, ChannelMonitor data);
50                 /**
51                  * Update one channel's data. The provided `ChannelMonitor` has already
52                  * applied the given update.
53                  * 
54                  * Note that on every update, you **must** persist either the
55                  * `ChannelMonitorUpdate` or the updated monitor itself to disk/backups. See
56                  * the `Persist` trait documentation for more details.
57                  * 
58                  * If an implementer chooses to persist the updates only, they need to make
59                  * sure that all the updates are applied to the `ChannelMonitors` *before
60                  * the set of channel monitors is given to the `ChannelManager`
61                  * deserialization routine. See [`ChannelMonitor::update_monitor`] for
62                  * applying a monitor update to a monitor. If full `ChannelMonitors` are
63                  * persisted, then there is no need to persist individual updates.
64                  * 
65                  * Note that there could be a performance tradeoff between persisting complete
66                  * channel monitors on every update vs. persisting only updates and applying
67                  * them in batches. The size of each monitor grows `O(number of state updates)`
68                  * whereas updates are small and `O(1)`.
69                  * 
70                  * See [`Writeable::write`] on [`ChannelMonitor`] for writing out a `ChannelMonitor`,
71                  * [`Writeable::write`] on [`ChannelMonitorUpdate`] for writing out an update, and
72                  * [`ChannelMonitorUpdateErr`] for requirements when returning errors.
73                  * 
74                  * [`Writeable::write`]: crate::util::ser::Writeable::write
75                  */
76                 Result_NoneChannelMonitorUpdateErrZ update_persisted_channel(OutPoint id, ChannelMonitorUpdate update, ChannelMonitor data);
77         }
78         private static class LDKPersistHolder { Persist held; }
79         public static Persist new_impl(PersistInterface arg) {
80                 final LDKPersistHolder impl_holder = new LDKPersistHolder();
81                 impl_holder.held = new Persist(new bindings.LDKPersist() {
82                         @Override public long persist_new_channel(long id, long data) {
83                                 OutPoint id_hu_conv = new OutPoint(null, id);
84                                 id_hu_conv.ptrs_to.add(this);
85                                 ChannelMonitor data_hu_conv = new ChannelMonitor(null, data);
86                                 Result_NoneChannelMonitorUpdateErrZ ret = arg.persist_new_channel(id_hu_conv, data_hu_conv);
87                                 long result = ret != null ? ret.ptr : 0;
88                                 return result;
89                         }
90                         @Override public long update_persisted_channel(long id, long update, long data) {
91                                 OutPoint id_hu_conv = new OutPoint(null, id);
92                                 id_hu_conv.ptrs_to.add(this);
93                                 ChannelMonitorUpdate update_hu_conv = new ChannelMonitorUpdate(null, update);
94                                 ChannelMonitor data_hu_conv = new ChannelMonitor(null, data);
95                                 Result_NoneChannelMonitorUpdateErrZ ret = arg.update_persisted_channel(id_hu_conv, update_hu_conv, data_hu_conv);
96                                 long result = ret != null ? ret.ptr : 0;
97                                 return result;
98                         }
99                 });
100                 return impl_holder.held;
101         }
102         /**
103          * Persist a new channel's data. The data can be stored any way you want, but
104          * the identifier provided by Rust-Lightning is the channel's outpoint (and
105          * it is up to you to maintain a correct mapping between the outpoint and the
106          * stored channel data). Note that you **must** persist every new monitor to
107          * disk. See the `Persist` trait documentation for more details.
108          * 
109          * See [`Writeable::write`] on [`ChannelMonitor`] for writing out a `ChannelMonitor`
110          * and [`ChannelMonitorUpdateErr`] for requirements when returning errors.
111          * 
112          * [`Writeable::write`]: crate::util::ser::Writeable::write
113          */
114         public Result_NoneChannelMonitorUpdateErrZ persist_new_channel(OutPoint id, ChannelMonitor data) {
115                 long ret = bindings.Persist_persist_new_channel(this.ptr, id == null ? 0 : id.ptr & ~1, data == null ? 0 : data.ptr & ~1);
116                 if (ret >= 0 && ret < 1024) { return null; }
117                 Result_NoneChannelMonitorUpdateErrZ ret_hu_conv = Result_NoneChannelMonitorUpdateErrZ.constr_from_ptr(ret);
118                 this.ptrs_to.add(data);
119                 return ret_hu_conv;
120         }
121
122         /**
123          * Update one channel's data. The provided `ChannelMonitor` has already
124          * applied the given update.
125          * 
126          * Note that on every update, you **must** persist either the
127          * `ChannelMonitorUpdate` or the updated monitor itself to disk/backups. See
128          * the `Persist` trait documentation for more details.
129          * 
130          * If an implementer chooses to persist the updates only, they need to make
131          * sure that all the updates are applied to the `ChannelMonitors` *before
132          * the set of channel monitors is given to the `ChannelManager`
133          * deserialization routine. See [`ChannelMonitor::update_monitor`] for
134          * applying a monitor update to a monitor. If full `ChannelMonitors` are
135          * persisted, then there is no need to persist individual updates.
136          * 
137          * Note that there could be a performance tradeoff between persisting complete
138          * channel monitors on every update vs. persisting only updates and applying
139          * them in batches. The size of each monitor grows `O(number of state updates)`
140          * whereas updates are small and `O(1)`.
141          * 
142          * See [`Writeable::write`] on [`ChannelMonitor`] for writing out a `ChannelMonitor`,
143          * [`Writeable::write`] on [`ChannelMonitorUpdate`] for writing out an update, and
144          * [`ChannelMonitorUpdateErr`] for requirements when returning errors.
145          * 
146          * [`Writeable::write`]: crate::util::ser::Writeable::write
147          */
148         public Result_NoneChannelMonitorUpdateErrZ update_persisted_channel(OutPoint id, ChannelMonitorUpdate update, ChannelMonitor data) {
149                 long ret = bindings.Persist_update_persisted_channel(this.ptr, id == null ? 0 : id.ptr & ~1, update == null ? 0 : update.ptr & ~1, data == null ? 0 : data.ptr & ~1);
150                 if (ret >= 0 && ret < 1024) { return null; }
151                 Result_NoneChannelMonitorUpdateErrZ ret_hu_conv = Result_NoneChannelMonitorUpdateErrZ.constr_from_ptr(ret);
152                 this.ptrs_to.add(update);
153                 this.ptrs_to.add(data);
154                 return ret_hu_conv;
155         }
156
157 }