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 * The `Watch` trait defines behavior for watching on-chain activity pertaining to channels as
11 * blocks are connected and disconnected.
13 * Each channel is associated with a [`ChannelMonitor`]. Implementations of this trait are
14 * responsible for maintaining a set of monitors such that they can be updated accordingly as
15 * channel state changes and HTLCs are resolved. See method documentation for specific
18 * Implementations **must** ensure that updates are successfully applied and persisted upon method
19 * completion. If an update fails with a [`PermanentFailure`], then it must immediately shut down
20 * without taking any further action such as persisting the current state.
22 * If an implementation maintains multiple instances of a channel's monitor (e.g., by storing
23 * backup copies), then it must ensure that updates are applied across all instances. Otherwise, it
24 * could result in a revoked transaction being broadcast, allowing the counterparty to claim all
25 * funds in the channel. See [`ChannelMonitorUpdateErr`] for more details about how to handle
28 * [`ChannelMonitor`]: channelmonitor::ChannelMonitor
29 * [`ChannelMonitorUpdateErr`]: channelmonitor::ChannelMonitorUpdateErr
30 * [`PermanentFailure`]: channelmonitor::ChannelMonitorUpdateErr::PermanentFailure
32 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
33 public class Watch extends CommonBase {
34 final bindings.LDKWatch bindings_instance;
35 Watch(Object _dummy, long ptr) { super(ptr); bindings_instance = null; }
36 private Watch(bindings.LDKWatch arg) {
37 super(bindings.LDKWatch_new(arg));
38 this.ptrs_to.add(arg);
39 this.bindings_instance = arg;
41 @Override @SuppressWarnings("deprecation")
42 protected void finalize() throws Throwable {
43 if (ptr != 0) { bindings.Watch_free(ptr); } super.finalize();
46 public static interface WatchInterface {
48 * Watches a channel identified by `funding_txo` using `monitor`.
50 * Implementations are responsible for watching the chain for the funding transaction along
51 * with any spends of outputs returned by [`get_outputs_to_watch`]. In practice, this means
52 * calling [`block_connected`] and [`block_disconnected`] on the monitor.
54 * [`get_outputs_to_watch`]: channelmonitor::ChannelMonitor::get_outputs_to_watch
55 * [`block_connected`]: channelmonitor::ChannelMonitor::block_connected
56 * [`block_disconnected`]: channelmonitor::ChannelMonitor::block_disconnected
58 Result_NoneChannelMonitorUpdateErrZ watch_channel(OutPoint funding_txo, ChannelMonitor monitor);
60 * Updates a channel identified by `funding_txo` by applying `update` to its monitor.
62 * Implementations must call [`update_monitor`] with the given update. See
63 * [`ChannelMonitorUpdateErr`] for invariants around returning an error.
65 * [`update_monitor`]: channelmonitor::ChannelMonitor::update_monitor
66 * [`ChannelMonitorUpdateErr`]: channelmonitor::ChannelMonitorUpdateErr
68 Result_NoneChannelMonitorUpdateErrZ update_channel(OutPoint funding_txo, ChannelMonitorUpdate update);
70 * Returns any monitor events since the last call. Subsequent calls must only return new
73 MonitorEvent[] release_pending_monitor_events();
75 private static class LDKWatchHolder { Watch held; }
76 public static Watch new_impl(WatchInterface arg) {
77 final LDKWatchHolder impl_holder = new LDKWatchHolder();
78 impl_holder.held = new Watch(new bindings.LDKWatch() {
79 @Override public long watch_channel(long funding_txo, long monitor) {
80 OutPoint funding_txo_hu_conv = new OutPoint(null, funding_txo);
81 funding_txo_hu_conv.ptrs_to.add(this);
82 ChannelMonitor monitor_hu_conv = new ChannelMonitor(null, monitor);
83 monitor_hu_conv.ptrs_to.add(this);
84 Result_NoneChannelMonitorUpdateErrZ ret = arg.watch_channel(funding_txo_hu_conv, monitor_hu_conv);
85 long result = ret != null ? ret.ptr : 0;
88 @Override public long update_channel(long funding_txo, long update) {
89 OutPoint funding_txo_hu_conv = new OutPoint(null, funding_txo);
90 funding_txo_hu_conv.ptrs_to.add(this);
91 ChannelMonitorUpdate update_hu_conv = new ChannelMonitorUpdate(null, update);
92 update_hu_conv.ptrs_to.add(this);
93 Result_NoneChannelMonitorUpdateErrZ ret = arg.update_channel(funding_txo_hu_conv, update_hu_conv);
94 long result = ret != null ? ret.ptr : 0;
97 @Override public long[] release_pending_monitor_events() {
98 MonitorEvent[] ret = arg.release_pending_monitor_events();
99 long[] result = ret != null ? Arrays.stream(ret).mapToLong(ret_conv_14 -> ret_conv_14.ptr).toArray() : null;
100 /* TODO 2 MonitorEvent */;
104 return impl_holder.held;
107 * Watches a channel identified by `funding_txo` using `monitor`.
109 * Implementations are responsible for watching the chain for the funding transaction along
110 * with any spends of outputs returned by [`get_outputs_to_watch`]. In practice, this means
111 * calling [`block_connected`] and [`block_disconnected`] on the monitor.
113 * [`get_outputs_to_watch`]: channelmonitor::ChannelMonitor::get_outputs_to_watch
114 * [`block_connected`]: channelmonitor::ChannelMonitor::block_connected
115 * [`block_disconnected`]: channelmonitor::ChannelMonitor::block_disconnected
117 public Result_NoneChannelMonitorUpdateErrZ watch_channel(OutPoint funding_txo, ChannelMonitor monitor) {
118 long ret = bindings.Watch_watch_channel(this.ptr, funding_txo == null ? 0 : funding_txo.ptr & ~1, monitor == null ? 0 : monitor.ptr & ~1);
119 if (ret < 1024) { return null; }
120 Result_NoneChannelMonitorUpdateErrZ ret_hu_conv = Result_NoneChannelMonitorUpdateErrZ.constr_from_ptr(ret);
121 this.ptrs_to.add(funding_txo);
122 this.ptrs_to.add(monitor);
127 * Updates a channel identified by `funding_txo` by applying `update` to its monitor.
129 * Implementations must call [`update_monitor`] with the given update. See
130 * [`ChannelMonitorUpdateErr`] for invariants around returning an error.
132 * [`update_monitor`]: channelmonitor::ChannelMonitor::update_monitor
133 * [`ChannelMonitorUpdateErr`]: channelmonitor::ChannelMonitorUpdateErr
135 public Result_NoneChannelMonitorUpdateErrZ update_channel(OutPoint funding_txo, ChannelMonitorUpdate update) {
136 long ret = bindings.Watch_update_channel(this.ptr, funding_txo == null ? 0 : funding_txo.ptr & ~1, update == null ? 0 : update.ptr & ~1);
137 if (ret < 1024) { return null; }
138 Result_NoneChannelMonitorUpdateErrZ ret_hu_conv = Result_NoneChannelMonitorUpdateErrZ.constr_from_ptr(ret);
139 this.ptrs_to.add(funding_txo);
140 this.ptrs_to.add(update);
145 * Returns any monitor events since the last call. Subsequent calls must only return new
148 public MonitorEvent[] release_pending_monitor_events() {
149 long[] ret = bindings.Watch_release_pending_monitor_events(this.ptr);
150 MonitorEvent[] ret_conv_14_arr = new MonitorEvent[ret.length];
151 for (int o = 0; o < ret.length; o++) {
152 long ret_conv_14 = ret[o];
153 MonitorEvent ret_conv_14_hu_conv = MonitorEvent.constr_from_ptr(ret_conv_14);
154 ret_conv_14_hu_conv.ptrs_to.add(this);
155 ret_conv_14_arr[o] = ret_conv_14_hu_conv;
157 return ret_conv_14_arr;