1 package org.ldk.structs;
3 import org.ldk.impl.bindings;
4 import org.ldk.enums.*;
6 import java.util.Arrays;
9 * `Persist` defines behavior for persisting channel monitors: this could mean
10 * writing once to disk, and/or uploading to one or more backup services.
12 * Note that for every new monitor, you **must** persist the new `ChannelMonitor`
13 * to disk/backups. And, on every update, you **must** persist either the
14 * `ChannelMonitorUpdate` or the updated monitor itself. Otherwise, there is risk
15 * of situations such as revoking a transaction, then crashing before this
16 * revocation can be persisted, then unintentionally broadcasting a revoked
17 * transaction and losing money. This is a risk because previous channel states
18 * are toxic, so it's important that whatever channel state is persisted is
21 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
22 public class Persist extends CommonBase {
23 final bindings.LDKPersist bindings_instance;
24 Persist(Object _dummy, long ptr) { super(ptr); bindings_instance = null; }
25 private Persist(bindings.LDKPersist arg) {
26 super(bindings.LDKPersist_new(arg));
27 this.ptrs_to.add(arg);
28 this.bindings_instance = arg;
30 @Override @SuppressWarnings("deprecation")
31 protected void finalize() throws Throwable {
32 if (ptr != 0) { bindings.Persist_free(ptr); } super.finalize();
35 public static interface PersistInterface {
37 * Persist a new channel's data. The data can be stored any way you want, but
38 * the identifier provided by Rust-Lightning is the channel's outpoint (and
39 * it is up to you to maintain a correct mapping between the outpoint and the
40 * stored channel data). Note that you **must** persist every new monitor to
41 * disk. See the `Persist` trait documentation for more details.
43 * See [`ChannelMonitor::serialize_for_disk`] for writing out a `ChannelMonitor`,
44 * and [`ChannelMonitorUpdateErr`] for requirements when returning errors.
46 * [`ChannelMonitor::serialize_for_disk`]: struct.ChannelMonitor.html#method.serialize_for_disk
47 * [`ChannelMonitorUpdateErr`]: enum.ChannelMonitorUpdateErr.html
49 Result_NoneChannelMonitorUpdateErrZ persist_new_channel(OutPoint id, ChannelMonitor data);
51 * Update one channel's data. The provided `ChannelMonitor` has already
52 * applied the given update.
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.
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.
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)`.
70 * See [`ChannelMonitor::serialize_for_disk`] for writing out a `ChannelMonitor`,
71 * [`ChannelMonitorUpdate::write`] for writing out an update, and
72 * [`ChannelMonitorUpdateErr`] for requirements when returning errors.
74 * [`ChannelMonitor::update_monitor`]: struct.ChannelMonitor.html#impl-1
75 * [`ChannelMonitor::serialize_for_disk`]: struct.ChannelMonitor.html#method.serialize_for_disk
76 * [`ChannelMonitorUpdate::write`]: struct.ChannelMonitorUpdate.html#method.write
77 * [`ChannelMonitorUpdateErr`]: enum.ChannelMonitorUpdateErr.html
79 Result_NoneChannelMonitorUpdateErrZ update_persisted_channel(OutPoint id, ChannelMonitorUpdate update, ChannelMonitor data);
81 private static class LDKPersistHolder { Persist held; }
82 public static Persist new_impl(PersistInterface arg) {
83 final LDKPersistHolder impl_holder = new LDKPersistHolder();
84 impl_holder.held = new Persist(new bindings.LDKPersist() {
85 @Override public long persist_new_channel(long id, long data) {
86 OutPoint id_hu_conv = new OutPoint(null, id);
87 id_hu_conv.ptrs_to.add(this);
88 ChannelMonitor data_hu_conv = new ChannelMonitor(null, data);
89 Result_NoneChannelMonitorUpdateErrZ ret = arg.persist_new_channel(id_hu_conv, data_hu_conv);
90 long result = ret != null ? ret.ptr : 0;
93 @Override public long update_persisted_channel(long id, long update, long data) {
94 OutPoint id_hu_conv = new OutPoint(null, id);
95 id_hu_conv.ptrs_to.add(this);
96 ChannelMonitorUpdate update_hu_conv = new ChannelMonitorUpdate(null, update);
97 ChannelMonitor data_hu_conv = new ChannelMonitor(null, data);
98 Result_NoneChannelMonitorUpdateErrZ ret = arg.update_persisted_channel(id_hu_conv, update_hu_conv, data_hu_conv);
99 long result = ret != null ? ret.ptr : 0;
103 return impl_holder.held;
106 * Persist a new channel's data. The data can be stored any way you want, but
107 * the identifier provided by Rust-Lightning is the channel's outpoint (and
108 * it is up to you to maintain a correct mapping between the outpoint and the
109 * stored channel data). Note that you **must** persist every new monitor to
110 * disk. See the `Persist` trait documentation for more details.
112 * See [`ChannelMonitor::serialize_for_disk`] for writing out a `ChannelMonitor`,
113 * and [`ChannelMonitorUpdateErr`] for requirements when returning errors.
115 * [`ChannelMonitor::serialize_for_disk`]: struct.ChannelMonitor.html#method.serialize_for_disk
116 * [`ChannelMonitorUpdateErr`]: enum.ChannelMonitorUpdateErr.html
118 public Result_NoneChannelMonitorUpdateErrZ persist_new_channel(OutPoint id, ChannelMonitor data) {
119 long ret = bindings.Persist_persist_new_channel(this.ptr, id == null ? 0 : id.ptr & ~1, data == null ? 0 : data.ptr & ~1);
120 Result_NoneChannelMonitorUpdateErrZ ret_hu_conv = Result_NoneChannelMonitorUpdateErrZ.constr_from_ptr(ret);
121 this.ptrs_to.add(id);
122 this.ptrs_to.add(data);
127 * Update one channel's data. The provided `ChannelMonitor` has already
128 * applied the given update.
130 * Note that on every update, you **must** persist either the
131 * `ChannelMonitorUpdate` or the updated monitor itself to disk/backups. See
132 * the `Persist` trait documentation for more details.
134 * If an implementer chooses to persist the updates only, they need to make
135 * sure that all the updates are applied to the `ChannelMonitors` *before
136 * the set of channel monitors is given to the `ChannelManager`
137 * deserialization routine. See [`ChannelMonitor::update_monitor`] for
138 * applying a monitor update to a monitor. If full `ChannelMonitors` are
139 * persisted, then there is no need to persist individual updates.
141 * Note that there could be a performance tradeoff between persisting complete
142 * channel monitors on every update vs. persisting only updates and applying
143 * them in batches. The size of each monitor grows `O(number of state updates)`
144 * whereas updates are small and `O(1)`.
146 * See [`ChannelMonitor::serialize_for_disk`] for writing out a `ChannelMonitor`,
147 * [`ChannelMonitorUpdate::write`] for writing out an update, and
148 * [`ChannelMonitorUpdateErr`] for requirements when returning errors.
150 * [`ChannelMonitor::update_monitor`]: struct.ChannelMonitor.html#impl-1
151 * [`ChannelMonitor::serialize_for_disk`]: struct.ChannelMonitor.html#method.serialize_for_disk
152 * [`ChannelMonitorUpdate::write`]: struct.ChannelMonitorUpdate.html#method.write
153 * [`ChannelMonitorUpdateErr`]: enum.ChannelMonitorUpdateErr.html
155 public Result_NoneChannelMonitorUpdateErrZ update_persisted_channel(OutPoint id, ChannelMonitorUpdate update, ChannelMonitor data) {
156 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);
157 Result_NoneChannelMonitorUpdateErrZ ret_hu_conv = Result_NoneChannelMonitorUpdateErrZ.constr_from_ptr(ret);
158 this.ptrs_to.add(id);
159 this.ptrs_to.add(update);
160 this.ptrs_to.add(data);