8da382138843a9b4262b8dbcb20197f383a24dbc
[ldk-java] / src / main / java / org / ldk / structs / Watch.java
1 package org.ldk.structs;
2
3 import org.ldk.impl.bindings;
4 import org.ldk.enums.*;
5 import org.ldk.util.*;
6 import java.util.Arrays;
7 import java.lang.ref.Reference;
8 import javax.annotation.Nullable;
9
10 /**
11  * The `Watch` trait defines behavior for watching on-chain activity pertaining to channels as
12  * blocks are connected and disconnected.
13  * 
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
17  * requirements.
18  * 
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.
22  * 
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
27  * multiple instances.
28  * 
29  * [`PermanentFailure`]: ChannelMonitorUpdateStatus::PermanentFailure
30  */
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;
39         }
40         @Override @SuppressWarnings("deprecation")
41         protected void finalize() throws Throwable {
42                 if (ptr != 0) { bindings.Watch_free(ptr); } super.finalize();
43         }
44
45         public static interface WatchInterface {
46                 /**
47                  * Watches a channel identified by `funding_txo` using `monitor`.
48                  * 
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.
52                  * 
53                  * Note: this interface MUST error with [`ChannelMonitorUpdateStatus::PermanentFailure`] if
54                  * the given `funding_txo` has previously been registered via `watch_channel`.
55                  * 
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
59                  */
60                 ChannelMonitorUpdateStatus watch_channel(OutPoint funding_txo, ChannelMonitor monitor);
61                 /**
62                  * Updates a channel identified by `funding_txo` by applying `update` to its monitor.
63                  * 
64                  * Implementations must call [`update_monitor`] with the given update. See
65                  * [`ChannelMonitorUpdateStatus`] for invariants around returning an error.
66                  * 
67                  * [`update_monitor`]: channelmonitor::ChannelMonitor::update_monitor
68                  */
69                 ChannelMonitorUpdateStatus update_channel(OutPoint funding_txo, ChannelMonitorUpdate update);
70                 /**
71                  * Returns any monitor events since the last call. Subsequent calls must only return new
72                  * events.
73                  * 
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
76                  * to disk.
77                  * 
78                  * For details on asynchronous [`ChannelMonitor`] updating and returning
79                  * [`MonitorEvent::Completed`] here, see [`ChannelMonitorUpdateStatus::InProgress`].
80                  */
81                 ThreeTuple_OutPointCVec_MonitorEventZPublicKeyZ[] release_pending_monitor_events();
82         }
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 ChannelMonitorUpdateStatus watch_channel(long funding_txo, long monitor) {
88                                 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); }
89                                 if (funding_txo_hu_conv != null) { funding_txo_hu_conv.ptrs_to.add(this); };
90                                 org.ldk.structs.ChannelMonitor monitor_hu_conv = null; if (monitor < 0 || monitor > 4096) { monitor_hu_conv = new org.ldk.structs.ChannelMonitor(null, monitor); }
91                                 if (monitor_hu_conv != null) { monitor_hu_conv.ptrs_to.add(this); };
92                                 ChannelMonitorUpdateStatus ret = arg.watch_channel(funding_txo_hu_conv, monitor_hu_conv);
93                                 Reference.reachabilityFence(arg);
94                                 return ret;
95                         }
96                         @Override public ChannelMonitorUpdateStatus update_channel(long funding_txo, long update) {
97                                 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); }
98                                 if (funding_txo_hu_conv != null) { funding_txo_hu_conv.ptrs_to.add(this); };
99                                 org.ldk.structs.ChannelMonitorUpdate update_hu_conv = null; if (update < 0 || update > 4096) { update_hu_conv = new org.ldk.structs.ChannelMonitorUpdate(null, update); }
100                                 if (update_hu_conv != null) { update_hu_conv.ptrs_to.add(this); };
101                                 ChannelMonitorUpdateStatus ret = arg.update_channel(funding_txo_hu_conv, update_hu_conv);
102                                 Reference.reachabilityFence(arg);
103                                 return ret;
104                         }
105                         @Override public long[] release_pending_monitor_events() {
106                                 ThreeTuple_OutPointCVec_MonitorEventZPublicKeyZ[] ret = arg.release_pending_monitor_events();
107                                 Reference.reachabilityFence(arg);
108                                 long[] result = ret != null ? Arrays.stream(ret).mapToLong(ret_conv_49 -> ret_conv_49 == null ? 0 : ret_conv_49.clone_ptr()).toArray() : null;
109                                 return result;
110                         }
111                 });
112                 return impl_holder.held;
113         }
114         /**
115          * Watches a channel identified by `funding_txo` using `monitor`.
116          * 
117          * Implementations are responsible for watching the chain for the funding transaction along
118          * with any spends of outputs returned by [`get_outputs_to_watch`]. In practice, this means
119          * calling [`block_connected`] and [`block_disconnected`] on the monitor.
120          * 
121          * Note: this interface MUST error with [`ChannelMonitorUpdateStatus::PermanentFailure`] if
122          * the given `funding_txo` has previously been registered via `watch_channel`.
123          * 
124          * [`get_outputs_to_watch`]: channelmonitor::ChannelMonitor::get_outputs_to_watch
125          * [`block_connected`]: channelmonitor::ChannelMonitor::block_connected
126          * [`block_disconnected`]: channelmonitor::ChannelMonitor::block_disconnected
127          */
128         public ChannelMonitorUpdateStatus watch_channel(org.ldk.structs.OutPoint funding_txo, org.ldk.structs.ChannelMonitor monitor) {
129                 ChannelMonitorUpdateStatus ret = bindings.Watch_watch_channel(this.ptr, funding_txo == null ? 0 : funding_txo.ptr, monitor == null ? 0 : monitor.ptr);
130                 Reference.reachabilityFence(this);
131                 Reference.reachabilityFence(funding_txo);
132                 Reference.reachabilityFence(monitor);
133                 if (this != null) { this.ptrs_to.add(funding_txo); };
134                 if (this != null) { this.ptrs_to.add(monitor); };
135                 return ret;
136         }
137
138         /**
139          * Updates a channel identified by `funding_txo` by applying `update` to its monitor.
140          * 
141          * Implementations must call [`update_monitor`] with the given update. See
142          * [`ChannelMonitorUpdateStatus`] for invariants around returning an error.
143          * 
144          * [`update_monitor`]: channelmonitor::ChannelMonitor::update_monitor
145          */
146         public ChannelMonitorUpdateStatus update_channel(org.ldk.structs.OutPoint funding_txo, org.ldk.structs.ChannelMonitorUpdate update) {
147                 ChannelMonitorUpdateStatus ret = bindings.Watch_update_channel(this.ptr, funding_txo == null ? 0 : funding_txo.ptr, update == null ? 0 : update.ptr);
148                 Reference.reachabilityFence(this);
149                 Reference.reachabilityFence(funding_txo);
150                 Reference.reachabilityFence(update);
151                 if (this != null) { this.ptrs_to.add(funding_txo); };
152                 if (this != null) { this.ptrs_to.add(update); };
153                 return ret;
154         }
155
156         /**
157          * Returns any monitor events since the last call. Subsequent calls must only return new
158          * events.
159          * 
160          * Note that after any block- or transaction-connection calls to a [`ChannelMonitor`], no
161          * further events may be returned here until the [`ChannelMonitor`] has been fully persisted
162          * to disk.
163          * 
164          * For details on asynchronous [`ChannelMonitor`] updating and returning
165          * [`MonitorEvent::Completed`] here, see [`ChannelMonitorUpdateStatus::InProgress`].
166          */
167         public ThreeTuple_OutPointCVec_MonitorEventZPublicKeyZ[] release_pending_monitor_events() {
168                 long[] ret = bindings.Watch_release_pending_monitor_events(this.ptr);
169                 Reference.reachabilityFence(this);
170                 int ret_conv_49_len = ret.length;
171                 ThreeTuple_OutPointCVec_MonitorEventZPublicKeyZ[] ret_conv_49_arr = new ThreeTuple_OutPointCVec_MonitorEventZPublicKeyZ[ret_conv_49_len];
172                 for (int x = 0; x < ret_conv_49_len; x++) {
173                         long ret_conv_49 = ret[x];
174                         ThreeTuple_OutPointCVec_MonitorEventZPublicKeyZ ret_conv_49_hu_conv = new ThreeTuple_OutPointCVec_MonitorEventZPublicKeyZ(null, ret_conv_49);
175                         if (ret_conv_49_hu_conv != null) { ret_conv_49_hu_conv.ptrs_to.add(this); };
176                         ret_conv_49_arr[x] = ret_conv_49_hu_conv;
177                 }
178                 return ret_conv_49_arr;
179         }
180
181 }