1 package org.ldk.structs;
3 import org.ldk.impl.bindings;
4 import org.ldk.enums.*;
6 import java.util.Arrays;
7 import javax.annotation.Nullable;
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.
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
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;
31 @Override @SuppressWarnings("deprecation")
32 protected void finalize() throws Throwable {
33 if (ptr != 0) { bindings.Persist_free(ptr); } super.finalize();
36 public static interface PersistInterface {
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.
44 * See [`Writeable::write`] on [`ChannelMonitor`] for writing out a `ChannelMonitor`
45 * and [`ChannelMonitorUpdateErr`] for requirements when returning errors.
47 * [`Writeable::write`]: crate::util::ser::Writeable::write
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 [`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.
74 * [`Writeable::write`]: crate::util::ser::Writeable::write
76 Result_NoneChannelMonitorUpdateErrZ update_persisted_channel(OutPoint id, ChannelMonitorUpdate update, ChannelMonitor data);
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;
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;
100 return impl_holder.held;
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.
109 * See [`Writeable::write`] on [`ChannelMonitor`] for writing out a `ChannelMonitor`
110 * and [`ChannelMonitorUpdateErr`] for requirements when returning errors.
112 * [`Writeable::write`]: crate::util::ser::Writeable::write
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);
123 * Update one channel's data. The provided `ChannelMonitor` has already
124 * applied the given update.
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.
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.
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)`.
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.
146 * [`Writeable::write`]: crate::util::ser::Writeable::write
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);