1 package org.ldk.structs;
3 import org.ldk.impl.bindings;
4 import org.ldk.enums.*;
6 import java.util.Arrays;
7 import java.lang.ref.Reference;
8 import javax.annotation.Nullable;
11 * The `Watch` trait defines behavior for watching on-chain activity pertaining to channels as
12 * blocks are connected and disconnected.
14 * Each channel is associated with a [`ChannelMonitor`]. Implementations of this trait are
15 * responsible for maintaining a set of monitors such that they can be updated accordingly as
16 * channel state changes and HTLCs are resolved. See method documentation for specific
19 * Implementations **must** ensure that updates are successfully applied and persisted upon method
20 * completion. If an update fails with a [`PermanentFailure`], then it must immediately shut down
21 * without taking any further action such as persisting the current state.
23 * If an implementation maintains multiple instances of a channel's monitor (e.g., by storing
24 * backup copies), then it must ensure that updates are applied across all instances. Otherwise, it
25 * could result in a revoked transaction being broadcast, allowing the counterparty to claim all
26 * funds in the channel. See [`ChannelMonitorUpdateStatus`] for more details about how to handle
29 * [`PermanentFailure`]: ChannelMonitorUpdateStatus::PermanentFailure
31 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
32 public class Watch extends CommonBase {
33 final bindings.LDKWatch bindings_instance;
34 Watch(Object _dummy, long ptr) { super(ptr); bindings_instance = null; }
35 private Watch(bindings.LDKWatch arg) {
36 super(bindings.LDKWatch_new(arg));
37 this.ptrs_to.add(arg);
38 this.bindings_instance = arg;
40 @Override @SuppressWarnings("deprecation")
41 protected void finalize() throws Throwable {
42 if (ptr != 0) { bindings.Watch_free(ptr); } super.finalize();
45 * Destroys the object, freeing associated resources. After this call, any access
46 * to this object may result in a SEGFAULT or worse.
48 * You should generally NEVER call this method. You should let the garbage collector
49 * do this for you when it finalizes objects. However, it may be useful for types
50 * which represent locks and should be closed immediately to avoid holding locks
53 public void destroy() {
54 if (ptr != 0) { bindings.Watch_free(ptr); }
57 public static interface WatchInterface {
59 * Watches a channel identified by `funding_txo` using `monitor`.
61 * Implementations are responsible for watching the chain for the funding transaction along
62 * with any spends of outputs returned by [`get_outputs_to_watch`]. In practice, this means
63 * calling [`block_connected`] and [`block_disconnected`] on the monitor.
65 * Note: this interface MUST error with [`ChannelMonitorUpdateStatus::PermanentFailure`] if
66 * the given `funding_txo` has previously been registered via `watch_channel`.
68 * [`get_outputs_to_watch`]: channelmonitor::ChannelMonitor::get_outputs_to_watch
69 * [`block_connected`]: channelmonitor::ChannelMonitor::block_connected
70 * [`block_disconnected`]: channelmonitor::ChannelMonitor::block_disconnected
72 ChannelMonitorUpdateStatus watch_channel(OutPoint funding_txo, ChannelMonitor monitor);
74 * Updates a channel identified by `funding_txo` by applying `update` to its monitor.
76 * Implementations must call [`update_monitor`] with the given update. See
77 * [`ChannelMonitorUpdateStatus`] for invariants around returning an error.
79 * [`update_monitor`]: channelmonitor::ChannelMonitor::update_monitor
81 ChannelMonitorUpdateStatus update_channel(OutPoint funding_txo, ChannelMonitorUpdate update);
83 * Returns any monitor events since the last call. Subsequent calls must only return new
86 * Note that after any block- or transaction-connection calls to a [`ChannelMonitor`], no
87 * further events may be returned here until the [`ChannelMonitor`] has been fully persisted
90 * For details on asynchronous [`ChannelMonitor`] updating and returning
91 * [`MonitorEvent::Completed`] here, see [`ChannelMonitorUpdateStatus::InProgress`].
93 ThreeTuple_OutPointCVec_MonitorEventZPublicKeyZ[] release_pending_monitor_events();
95 private static class LDKWatchHolder { Watch held; }
96 public static Watch new_impl(WatchInterface arg) {
97 final LDKWatchHolder impl_holder = new LDKWatchHolder();
98 impl_holder.held = new Watch(new bindings.LDKWatch() {
99 @Override public ChannelMonitorUpdateStatus watch_channel(long funding_txo, long monitor) {
100 org.ldk.structs.OutPoint funding_txo_hu_conv = null; if (funding_txo < 0 || funding_txo > 4096) { funding_txo_hu_conv = new org.ldk.structs.OutPoint(null, funding_txo); }
101 if (funding_txo_hu_conv != null) { funding_txo_hu_conv.ptrs_to.add(this); };
102 org.ldk.structs.ChannelMonitor monitor_hu_conv = null; if (monitor < 0 || monitor > 4096) { monitor_hu_conv = new org.ldk.structs.ChannelMonitor(null, monitor); }
103 if (monitor_hu_conv != null) { monitor_hu_conv.ptrs_to.add(this); };
104 ChannelMonitorUpdateStatus ret = arg.watch_channel(funding_txo_hu_conv, monitor_hu_conv);
105 Reference.reachabilityFence(arg);
108 @Override public ChannelMonitorUpdateStatus update_channel(long funding_txo, long update) {
109 org.ldk.structs.OutPoint funding_txo_hu_conv = null; if (funding_txo < 0 || funding_txo > 4096) { funding_txo_hu_conv = new org.ldk.structs.OutPoint(null, funding_txo); }
110 if (funding_txo_hu_conv != null) { funding_txo_hu_conv.ptrs_to.add(this); };
111 org.ldk.structs.ChannelMonitorUpdate update_hu_conv = null; if (update < 0 || update > 4096) { update_hu_conv = new org.ldk.structs.ChannelMonitorUpdate(null, update); }
112 ChannelMonitorUpdateStatus ret = arg.update_channel(funding_txo_hu_conv, update_hu_conv);
113 Reference.reachabilityFence(arg);
116 @Override public long[] release_pending_monitor_events() {
117 ThreeTuple_OutPointCVec_MonitorEventZPublicKeyZ[] ret = arg.release_pending_monitor_events();
118 Reference.reachabilityFence(arg);
119 long[] result = ret != null ? Arrays.stream(ret).mapToLong(ret_conv_49 -> ret_conv_49 == null ? 0 : ret_conv_49.clone_ptr()).toArray() : null;
123 return impl_holder.held;
126 * Watches a channel identified by `funding_txo` using `monitor`.
128 * Implementations are responsible for watching the chain for the funding transaction along
129 * with any spends of outputs returned by [`get_outputs_to_watch`]. In practice, this means
130 * calling [`block_connected`] and [`block_disconnected`] on the monitor.
132 * Note: this interface MUST error with [`ChannelMonitorUpdateStatus::PermanentFailure`] if
133 * the given `funding_txo` has previously been registered via `watch_channel`.
135 * [`get_outputs_to_watch`]: channelmonitor::ChannelMonitor::get_outputs_to_watch
136 * [`block_connected`]: channelmonitor::ChannelMonitor::block_connected
137 * [`block_disconnected`]: channelmonitor::ChannelMonitor::block_disconnected
139 public ChannelMonitorUpdateStatus watch_channel(org.ldk.structs.OutPoint funding_txo, org.ldk.structs.ChannelMonitor monitor) {
140 ChannelMonitorUpdateStatus ret = bindings.Watch_watch_channel(this.ptr, funding_txo == null ? 0 : funding_txo.ptr, monitor == null ? 0 : monitor.ptr);
141 Reference.reachabilityFence(this);
142 Reference.reachabilityFence(funding_txo);
143 Reference.reachabilityFence(monitor);
144 if (this != null) { this.ptrs_to.add(funding_txo); };
145 if (this != null) { this.ptrs_to.add(monitor); };
150 * Updates a channel identified by `funding_txo` by applying `update` to its monitor.
152 * Implementations must call [`update_monitor`] with the given update. See
153 * [`ChannelMonitorUpdateStatus`] for invariants around returning an error.
155 * [`update_monitor`]: channelmonitor::ChannelMonitor::update_monitor
157 public ChannelMonitorUpdateStatus update_channel(org.ldk.structs.OutPoint funding_txo, org.ldk.structs.ChannelMonitorUpdate update) {
158 ChannelMonitorUpdateStatus ret = bindings.Watch_update_channel(this.ptr, funding_txo == null ? 0 : funding_txo.ptr, update == null ? 0 : update.ptr);
159 Reference.reachabilityFence(this);
160 Reference.reachabilityFence(funding_txo);
161 Reference.reachabilityFence(update);
162 if (this != null) { this.ptrs_to.add(funding_txo); };
163 if (this != null) { this.ptrs_to.add(update); };
168 * Returns any monitor events since the last call. Subsequent calls must only return new
171 * Note that after any block- or transaction-connection calls to a [`ChannelMonitor`], no
172 * further events may be returned here until the [`ChannelMonitor`] has been fully persisted
175 * For details on asynchronous [`ChannelMonitor`] updating and returning
176 * [`MonitorEvent::Completed`] here, see [`ChannelMonitorUpdateStatus::InProgress`].
178 public ThreeTuple_OutPointCVec_MonitorEventZPublicKeyZ[] release_pending_monitor_events() {
179 long[] ret = bindings.Watch_release_pending_monitor_events(this.ptr);
180 Reference.reachabilityFence(this);
181 int ret_conv_49_len = ret.length;
182 ThreeTuple_OutPointCVec_MonitorEventZPublicKeyZ[] ret_conv_49_arr = new ThreeTuple_OutPointCVec_MonitorEventZPublicKeyZ[ret_conv_49_len];
183 for (int x = 0; x < ret_conv_49_len; x++) {
184 long ret_conv_49 = ret[x];
185 ThreeTuple_OutPointCVec_MonitorEventZPublicKeyZ ret_conv_49_hu_conv = new ThreeTuple_OutPointCVec_MonitorEventZPublicKeyZ(null, ret_conv_49);
186 if (ret_conv_49_hu_conv != null) { ret_conv_49_hu_conv.ptrs_to.add(this); };
187 ret_conv_49_arr[x] = ret_conv_49_hu_conv;
189 return ret_conv_49_arr;