Update java bindings with new generator and new upstream code
[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
8 /**
9  * The `Watch` trait defines behavior for watching on-chain activity pertaining to channels as
10  * blocks are connected and disconnected.
11  * 
12  * Each channel is associated with a [`ChannelMonitor`]. Implementations of this trait are
13  * responsible for maintaining a set of monitors such that they can be updated accordingly as
14  * channel state changes and HTLCs are resolved. See method documentation for specific
15  * requirements.
16  * 
17  * Implementations **must** ensure that updates are successfully applied and persisted upon method
18  * completion. If an update fails with a [`PermanentFailure`], then it must immediately shut down
19  * without taking any further action such as persisting the current state.
20  * 
21  * If an implementation maintains multiple instances of a channel's monitor (e.g., by storing
22  * backup copies), then it must ensure that updates are applied across all instances. Otherwise, it
23  * could result in a revoked transaction being broadcast, allowing the counterparty to claim all
24  * funds in the channel. See [`ChannelMonitorUpdateErr`] for more details about how to handle
25  * multiple instances.
26  * 
27  * [`ChannelMonitor`]: channelmonitor/struct.ChannelMonitor.html
28  * [`ChannelMonitorUpdateErr`]: channelmonitor/enum.ChannelMonitorUpdateErr.html
29  * [`PermanentFailure`]: channelmonitor/enum.ChannelMonitorUpdateErr.html#variant.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                  * [`get_outputs_to_watch`]: channelmonitor/struct.ChannelMonitor.html#method.get_outputs_to_watch
54                  * [`block_connected`]: channelmonitor/struct.ChannelMonitor.html#method.block_connected
55                  * [`block_disconnected`]: channelmonitor/struct.ChannelMonitor.html#method.block_disconnected
56                  */
57                 Result_NoneChannelMonitorUpdateErrZ watch_channel(OutPoint funding_txo, ChannelMonitor monitor);
58                 /**
59                  * Updates a channel identified by `funding_txo` by applying `update` to its monitor.
60                  * 
61                  * Implementations must call [`update_monitor`] with the given update. See
62                  * [`ChannelMonitorUpdateErr`] for invariants around returning an error.
63                  * 
64                  * [`update_monitor`]: channelmonitor/struct.ChannelMonitor.html#method.update_monitor
65                  * [`ChannelMonitorUpdateErr`]: channelmonitor/enum.ChannelMonitorUpdateErr.html
66                  */
67                 Result_NoneChannelMonitorUpdateErrZ update_channel(OutPoint funding_txo, ChannelMonitorUpdate update);
68                 /**
69                  * Returns any monitor events since the last call. Subsequent calls must only return new
70                  * events.
71                  */
72                 MonitorEvent[] release_pending_monitor_events();
73         }
74         private static class LDKWatchHolder { Watch held; }
75         public static Watch new_impl(WatchInterface arg) {
76                 final LDKWatchHolder impl_holder = new LDKWatchHolder();
77                 impl_holder.held = new Watch(new bindings.LDKWatch() {
78                         @Override public long watch_channel(long funding_txo, long monitor) {
79                                 OutPoint funding_txo_hu_conv = new OutPoint(null, funding_txo);
80                                 funding_txo_hu_conv.ptrs_to.add(this);
81                                 ChannelMonitor monitor_hu_conv = new ChannelMonitor(null, monitor);
82                                 monitor_hu_conv.ptrs_to.add(this);
83                                 Result_NoneChannelMonitorUpdateErrZ ret = arg.watch_channel(funding_txo_hu_conv, monitor_hu_conv);
84                                 long result = ret != null ? ret.ptr : 0;
85                                 return result;
86                         }
87                         @Override public long update_channel(long funding_txo, long update) {
88                                 OutPoint funding_txo_hu_conv = new OutPoint(null, funding_txo);
89                                 funding_txo_hu_conv.ptrs_to.add(this);
90                                 ChannelMonitorUpdate update_hu_conv = new ChannelMonitorUpdate(null, update);
91                                 update_hu_conv.ptrs_to.add(this);
92                                 Result_NoneChannelMonitorUpdateErrZ ret = arg.update_channel(funding_txo_hu_conv, update_hu_conv);
93                                 long result = ret != null ? ret.ptr : 0;
94                                 return result;
95                         }
96                         @Override public long[] release_pending_monitor_events() {
97                                 MonitorEvent[] ret = arg.release_pending_monitor_events();
98                                 long[] result = Arrays.stream(ret).mapToLong(ret_conv_14 -> ret_conv_14.ptr).toArray();
99                                 /* TODO 2 MonitorEvent  */;
100                                 return result;
101                         }
102                 });
103                 return impl_holder.held;
104         }
105         /**
106          * Watches a channel identified by `funding_txo` using `monitor`.
107          * 
108          * Implementations are responsible for watching the chain for the funding transaction along
109          * with any spends of outputs returned by [`get_outputs_to_watch`]. In practice, this means
110          * calling [`block_connected`] and [`block_disconnected`] on the monitor.
111          * 
112          * [`get_outputs_to_watch`]: channelmonitor/struct.ChannelMonitor.html#method.get_outputs_to_watch
113          * [`block_connected`]: channelmonitor/struct.ChannelMonitor.html#method.block_connected
114          * [`block_disconnected`]: channelmonitor/struct.ChannelMonitor.html#method.block_disconnected
115          */
116         public Result_NoneChannelMonitorUpdateErrZ watch_channel(OutPoint funding_txo, ChannelMonitor monitor) {
117                 long ret = bindings.Watch_watch_channel(this.ptr, funding_txo == null ? 0 : funding_txo.ptr & ~1, monitor == null ? 0 : monitor.ptr & ~1);
118                 Result_NoneChannelMonitorUpdateErrZ ret_hu_conv = Result_NoneChannelMonitorUpdateErrZ.constr_from_ptr(ret);
119                 this.ptrs_to.add(funding_txo);
120                 this.ptrs_to.add(monitor);
121                 return ret_hu_conv;
122         }
123
124         /**
125          * Updates a channel identified by `funding_txo` by applying `update` to its monitor.
126          * 
127          * Implementations must call [`update_monitor`] with the given update. See
128          * [`ChannelMonitorUpdateErr`] for invariants around returning an error.
129          * 
130          * [`update_monitor`]: channelmonitor/struct.ChannelMonitor.html#method.update_monitor
131          * [`ChannelMonitorUpdateErr`]: channelmonitor/enum.ChannelMonitorUpdateErr.html
132          */
133         public Result_NoneChannelMonitorUpdateErrZ update_channel(OutPoint funding_txo, ChannelMonitorUpdate update) {
134                 long ret = bindings.Watch_update_channel(this.ptr, funding_txo == null ? 0 : funding_txo.ptr & ~1, update == null ? 0 : update.ptr & ~1);
135                 Result_NoneChannelMonitorUpdateErrZ ret_hu_conv = Result_NoneChannelMonitorUpdateErrZ.constr_from_ptr(ret);
136                 this.ptrs_to.add(funding_txo);
137                 this.ptrs_to.add(update);
138                 return ret_hu_conv;
139         }
140
141         /**
142          * Returns any monitor events since the last call. Subsequent calls must only return new
143          * events.
144          */
145         public MonitorEvent[] release_pending_monitor_events() {
146                 long[] ret = bindings.Watch_release_pending_monitor_events(this.ptr);
147                 MonitorEvent[] ret_conv_14_arr = new MonitorEvent[ret.length];
148                 for (int o = 0; o < ret.length; o++) {
149                         long ret_conv_14 = ret[o];
150                         MonitorEvent ret_conv_14_hu_conv = MonitorEvent.constr_from_ptr(ret_conv_14);
151                         ret_conv_14_hu_conv.ptrs_to.add(this);
152                         ret_conv_14_arr[o] = ret_conv_14_hu_conv;
153                 }
154                 return ret_conv_14_arr;
155         }
156
157 }