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 [`ChannelMonitorUpdateErr`] for more details about how to handle
29 * [`PermanentFailure`]: ChannelMonitorUpdateErr::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 public static interface WatchInterface {
47 * Watches a channel identified by `funding_txo` using `monitor`.
49 * Implementations are responsible for watching the chain for the funding transaction along
50 * with any spends of outputs returned by [`get_outputs_to_watch`]. In practice, this means
51 * calling [`block_connected`] and [`block_disconnected`] on the monitor.
53 * Note: this interface MUST error with `ChannelMonitorUpdateErr::PermanentFailure` if
54 * the given `funding_txo` has previously been registered via `watch_channel`.
56 * [`get_outputs_to_watch`]: channelmonitor::ChannelMonitor::get_outputs_to_watch
57 * [`block_connected`]: channelmonitor::ChannelMonitor::block_connected
58 * [`block_disconnected`]: channelmonitor::ChannelMonitor::block_disconnected
60 Result_NoneChannelMonitorUpdateErrZ watch_channel(OutPoint funding_txo, ChannelMonitor monitor);
62 * Updates a channel identified by `funding_txo` by applying `update` to its monitor.
64 * Implementations must call [`update_monitor`] with the given update. See
65 * [`ChannelMonitorUpdateErr`] for invariants around returning an error.
67 * [`update_monitor`]: channelmonitor::ChannelMonitor::update_monitor
69 Result_NoneChannelMonitorUpdateErrZ update_channel(OutPoint funding_txo, ChannelMonitorUpdate update);
71 * Returns any monitor events since the last call. Subsequent calls must only return new
74 * Note that after any block- or transaction-connection calls to a [`ChannelMonitor`], no
75 * further events may be returned here until the [`ChannelMonitor`] has been fully persisted
78 * For details on asynchronous [`ChannelMonitor`] updating and returning
79 * [`MonitorEvent::UpdateCompleted`] here, see [`ChannelMonitorUpdateErr::TemporaryFailure`].
81 MonitorEvent[] release_pending_monitor_events();
83 private static class LDKWatchHolder { Watch held; }
84 public static Watch new_impl(WatchInterface arg) {
85 final LDKWatchHolder impl_holder = new LDKWatchHolder();
86 impl_holder.held = new Watch(new bindings.LDKWatch() {
87 @Override public long watch_channel(long funding_txo, long monitor) {
88 OutPoint funding_txo_hu_conv = null; if (funding_txo < 0 || funding_txo > 4096) { funding_txo_hu_conv = new OutPoint(null, funding_txo); }
89 funding_txo_hu_conv.ptrs_to.add(this);
90 ChannelMonitor monitor_hu_conv = null; if (monitor < 0 || monitor > 4096) { monitor_hu_conv = new ChannelMonitor(null, monitor); }
91 monitor_hu_conv.ptrs_to.add(this);
92 Result_NoneChannelMonitorUpdateErrZ ret = arg.watch_channel(funding_txo_hu_conv, monitor_hu_conv);
93 Reference.reachabilityFence(arg);
94 long result = ret == null ? 0 : ret.clone_ptr();
97 @Override public long update_channel(long funding_txo, long update) {
98 OutPoint funding_txo_hu_conv = null; if (funding_txo < 0 || funding_txo > 4096) { funding_txo_hu_conv = new OutPoint(null, funding_txo); }
99 funding_txo_hu_conv.ptrs_to.add(this);
100 ChannelMonitorUpdate update_hu_conv = null; if (update < 0 || update > 4096) { update_hu_conv = new ChannelMonitorUpdate(null, update); }
101 update_hu_conv.ptrs_to.add(this);
102 Result_NoneChannelMonitorUpdateErrZ ret = arg.update_channel(funding_txo_hu_conv, update_hu_conv);
103 Reference.reachabilityFence(arg);
104 long result = ret == null ? 0 : ret.clone_ptr();
107 @Override public long[] release_pending_monitor_events() {
108 MonitorEvent[] ret = arg.release_pending_monitor_events();
109 Reference.reachabilityFence(arg);
110 long[] result = ret != null ? Arrays.stream(ret).mapToLong(ret_conv_14 -> ret_conv_14 == null ? 0 : ret_conv_14.clone_ptr()).toArray() : null;
114 return impl_holder.held;
117 * Watches a channel identified by `funding_txo` using `monitor`.
119 * Implementations are responsible for watching the chain for the funding transaction along
120 * with any spends of outputs returned by [`get_outputs_to_watch`]. In practice, this means
121 * calling [`block_connected`] and [`block_disconnected`] on the monitor.
123 * Note: this interface MUST error with `ChannelMonitorUpdateErr::PermanentFailure` if
124 * the given `funding_txo` has previously been registered via `watch_channel`.
126 * [`get_outputs_to_watch`]: channelmonitor::ChannelMonitor::get_outputs_to_watch
127 * [`block_connected`]: channelmonitor::ChannelMonitor::block_connected
128 * [`block_disconnected`]: channelmonitor::ChannelMonitor::block_disconnected
130 public Result_NoneChannelMonitorUpdateErrZ watch_channel(OutPoint funding_txo, ChannelMonitor monitor) {
131 long ret = bindings.Watch_watch_channel(this.ptr, funding_txo == null ? 0 : funding_txo.ptr & ~1, monitor == null ? 0 : monitor.ptr & ~1);
132 Reference.reachabilityFence(this);
133 Reference.reachabilityFence(funding_txo);
134 Reference.reachabilityFence(monitor);
135 if (ret >= 0 && ret <= 4096) { return null; }
136 Result_NoneChannelMonitorUpdateErrZ ret_hu_conv = Result_NoneChannelMonitorUpdateErrZ.constr_from_ptr(ret);
141 * Updates a channel identified by `funding_txo` by applying `update` to its monitor.
143 * Implementations must call [`update_monitor`] with the given update. See
144 * [`ChannelMonitorUpdateErr`] for invariants around returning an error.
146 * [`update_monitor`]: channelmonitor::ChannelMonitor::update_monitor
148 public Result_NoneChannelMonitorUpdateErrZ update_channel(OutPoint funding_txo, ChannelMonitorUpdate update) {
149 long ret = bindings.Watch_update_channel(this.ptr, funding_txo == null ? 0 : funding_txo.ptr & ~1, update == null ? 0 : update.ptr & ~1);
150 Reference.reachabilityFence(this);
151 Reference.reachabilityFence(funding_txo);
152 Reference.reachabilityFence(update);
153 if (ret >= 0 && ret <= 4096) { return null; }
154 Result_NoneChannelMonitorUpdateErrZ ret_hu_conv = Result_NoneChannelMonitorUpdateErrZ.constr_from_ptr(ret);
159 * Returns any monitor events since the last call. Subsequent calls must only return new
162 * Note that after any block- or transaction-connection calls to a [`ChannelMonitor`], no
163 * further events may be returned here until the [`ChannelMonitor`] has been fully persisted
166 * For details on asynchronous [`ChannelMonitor`] updating and returning
167 * [`MonitorEvent::UpdateCompleted`] here, see [`ChannelMonitorUpdateErr::TemporaryFailure`].
169 public MonitorEvent[] release_pending_monitor_events() {
170 long[] ret = bindings.Watch_release_pending_monitor_events(this.ptr);
171 Reference.reachabilityFence(this);
172 int ret_conv_14_len = ret.length;
173 MonitorEvent[] ret_conv_14_arr = new MonitorEvent[ret_conv_14_len];
174 for (int o = 0; o < ret_conv_14_len; o++) {
175 long ret_conv_14 = ret[o];
176 org.ldk.structs.MonitorEvent ret_conv_14_hu_conv = org.ldk.structs.MonitorEvent.constr_from_ptr(ret_conv_14);
177 ret_conv_14_hu_conv.ptrs_to.add(this);
178 ret_conv_14_arr[o] = ret_conv_14_hu_conv;
180 return ret_conv_14_arr;