Update java bindings with new generator and new upstream code
[ldk-java] / src / main / java / org / ldk / structs / ChannelMonitor.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 /**
10  * A ChannelMonitor handles chain events (blocks connected and disconnected) and generates
11  * on-chain transactions to ensure no loss of funds occurs.
12  * 
13  * You MUST ensure that no ChannelMonitors for a given channel anywhere contain out-of-date
14  * information and are actively monitoring the chain.
15  * 
16  * Pending Events or updated HTLCs which have not yet been read out by
17  * get_and_clear_pending_monitor_events or get_and_clear_pending_events are serialized to disk and
18  * reloaded at deserialize-time. Thus, you must ensure that, when handling events, all events
19  * gotten are fully handled before re-serializing the new state.
20  * 
21  * Note that the deserializer is only implemented for (BlockHash, ChannelMonitor), which
22  * tells you the last block hash which was block_connect()ed. You MUST rescan any blocks along
23  * the \"reorg path\" (ie disconnecting blocks until you find a common ancestor from both the
24  * returned block hash and the the current chain and then reconnecting blocks to get to the
25  * best chain) upon deserializing the object!
26  */
27 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
28 public class ChannelMonitor extends CommonBase {
29         ChannelMonitor(Object _dummy, long ptr) { super(ptr); }
30         @Override @SuppressWarnings("deprecation")
31         protected void finalize() throws Throwable {
32                 super.finalize();
33                 if (ptr != 0) { bindings.ChannelMonitor_free(ptr); }
34         }
35
36         /**
37          * Creates a copy of the ChannelMonitor
38          */
39         public ChannelMonitor clone() {
40                 long ret = bindings.ChannelMonitor_clone(this.ptr);
41                 ChannelMonitor ret_hu_conv = new ChannelMonitor(null, ret);
42                 ret_hu_conv.ptrs_to.add(this);
43                 return ret_hu_conv;
44         }
45
46         /**
47          * Serialize the ChannelMonitor object into a byte array which can be read by ChannelMonitor_read
48          */
49         public byte[] write() {
50                 byte[] ret = bindings.ChannelMonitor_write(this.ptr);
51                 return ret;
52         }
53
54         /**
55          * Updates a ChannelMonitor on the basis of some new information provided by the Channel
56          * itself.
57          * 
58          * panics if the given update is not the next update by update_id.
59          */
60         public Result_NoneMonitorUpdateErrorZ update_monitor(ChannelMonitorUpdate updates, BroadcasterInterface broadcaster, FeeEstimator fee_estimator, Logger logger) {
61                 long ret = bindings.ChannelMonitor_update_monitor(this.ptr, updates == null ? 0 : updates.ptr & ~1, broadcaster == null ? 0 : broadcaster.ptr, fee_estimator == null ? 0 : fee_estimator.ptr, logger == null ? 0 : logger.ptr);
62                 Result_NoneMonitorUpdateErrorZ ret_hu_conv = Result_NoneMonitorUpdateErrorZ.constr_from_ptr(ret);
63                 this.ptrs_to.add(updates);
64                 this.ptrs_to.add(broadcaster);
65                 this.ptrs_to.add(fee_estimator);
66                 this.ptrs_to.add(logger);
67                 return ret_hu_conv;
68         }
69
70         /**
71          * Gets the update_id from the latest ChannelMonitorUpdate which was applied to this
72          * ChannelMonitor.
73          */
74         public long get_latest_update_id() {
75                 long ret = bindings.ChannelMonitor_get_latest_update_id(this.ptr);
76                 return ret;
77         }
78
79         /**
80          * Gets the funding transaction outpoint of the channel this ChannelMonitor is monitoring for.
81          */
82         public TwoTuple<OutPoint, byte[]> get_funding_txo() {
83                 long ret = bindings.ChannelMonitor_get_funding_txo(this.ptr);
84                 long ret_a = bindings.LDKC2Tuple_OutPointScriptZ_get_a(ret);
85                 OutPoint ret_a_hu_conv = new OutPoint(null, ret_a);
86                 ret_a_hu_conv.ptrs_to.add(this);;
87                 byte[] ret_b = bindings.LDKC2Tuple_OutPointScriptZ_get_b(ret);
88                 TwoTuple<OutPoint, byte[]> ret_conv = new TwoTuple<OutPoint, byte[]>(ret_a_hu_conv, ret_b, () -> {
89                         bindings.C2Tuple_OutPointScriptZ_free(ret);
90                 });
91                 ret_a_hu_conv.ptrs_to.add(ret_conv);
92                 return ret_conv;
93         }
94
95         /**
96          * Gets a list of txids, with their output scripts (in the order they appear in the
97          * transaction), which we must learn about spends of via block_connected().
98          */
99         public TwoTuple<byte[], TwoTuple<Integer, byte[]>[]>[] get_outputs_to_watch() {
100                 long[] ret = bindings.ChannelMonitor_get_outputs_to_watch(this.ptr);
101                 TwoTuple<byte[], TwoTuple<Integer, byte[]>[]>[] ret_conv_47_arr = new TwoTuple[ret.length];
102                 for (int v = 0; v < ret.length; v++) {
103                         long ret_conv_47 = ret[v];
104                         byte[] ret_conv_47_a = bindings.LDKC2Tuple_TxidCVec_C2Tuple_u32ScriptZZZ_get_a(ret_conv_47);
105                         long[] ret_conv_47_b = bindings.LDKC2Tuple_TxidCVec_C2Tuple_u32ScriptZZZ_get_b(ret_conv_47);
106                         TwoTuple<Integer, byte[]>[] ret_conv_47_b_conv_27_arr = new TwoTuple[ret_conv_47_b.length];
107                         for (int b = 0; b < ret_conv_47_b.length; b++) {
108                                 long ret_conv_47_b_conv_27 = ret_conv_47_b[b];
109                                 int ret_conv_47_b_conv_27_a = bindings.LDKC2Tuple_u32ScriptZ_get_a(ret_conv_47_b_conv_27);
110                                 byte[] ret_conv_47_b_conv_27_b = bindings.LDKC2Tuple_u32ScriptZ_get_b(ret_conv_47_b_conv_27);
111                                 TwoTuple<Integer, byte[]> ret_conv_47_b_conv_27_conv = new TwoTuple<Integer, byte[]>(ret_conv_47_b_conv_27_a, ret_conv_47_b_conv_27_b, () -> {
112                                         bindings.C2Tuple_u32ScriptZ_free(ret_conv_47_b_conv_27);
113                                 });
114                                 ret_conv_47_b_conv_27_arr[b] = ret_conv_47_b_conv_27_conv;
115                         };
116                         TwoTuple<byte[], TwoTuple<Integer, byte[]>[]> ret_conv_47_conv = new TwoTuple<byte[], TwoTuple<Integer, byte[]>[]>(ret_conv_47_a, ret_conv_47_b_conv_27_arr);
117                         // Warning: We may not free the C tuple object!
118                         ret_conv_47_arr[v] = ret_conv_47_conv;
119                 }
120                 return ret_conv_47_arr;
121         }
122
123         /**
124          * Loads the funding txo and outputs to watch into the given `chain::Filter` by repeatedly
125          * calling `chain::Filter::register_output` and `chain::Filter::register_tx` until all outputs
126          * have been registered.
127          */
128         public void load_outputs_to_watch(Filter filter) {
129                 bindings.ChannelMonitor_load_outputs_to_watch(this.ptr, filter == null ? 0 : filter.ptr);
130                 this.ptrs_to.add(filter);
131         }
132
133         /**
134          * Get the list of HTLCs who's status has been updated on chain. This should be called by
135          * ChannelManager via [`chain::Watch::release_pending_monitor_events`].
136          * 
137          * [`chain::Watch::release_pending_monitor_events`]: ../trait.Watch.html#tymethod.release_pending_monitor_events
138          */
139         public MonitorEvent[] get_and_clear_pending_monitor_events() {
140                 long[] ret = bindings.ChannelMonitor_get_and_clear_pending_monitor_events(this.ptr);
141                 MonitorEvent[] ret_conv_14_arr = new MonitorEvent[ret.length];
142                 for (int o = 0; o < ret.length; o++) {
143                         long ret_conv_14 = ret[o];
144                         MonitorEvent ret_conv_14_hu_conv = MonitorEvent.constr_from_ptr(ret_conv_14);
145                         ret_conv_14_hu_conv.ptrs_to.add(this);
146                         ret_conv_14_arr[o] = ret_conv_14_hu_conv;
147                 }
148                 return ret_conv_14_arr;
149         }
150
151         /**
152          * Gets the list of pending events which were generated by previous actions, clearing the list
153          * in the process.
154          * 
155          * This is called by ChainMonitor::get_and_clear_pending_events() and is equivalent to
156          * EventsProvider::get_and_clear_pending_events() except that it requires &mut self as we do
157          * no internal locking in ChannelMonitors.
158          */
159         public Event[] get_and_clear_pending_events() {
160                 long[] ret = bindings.ChannelMonitor_get_and_clear_pending_events(this.ptr);
161                 Event[] ret_conv_7_arr = new Event[ret.length];
162                 for (int h = 0; h < ret.length; h++) {
163                         long ret_conv_7 = ret[h];
164                         Event ret_conv_7_hu_conv = Event.constr_from_ptr(ret_conv_7);
165                         ret_conv_7_hu_conv.ptrs_to.add(this);
166                         ret_conv_7_arr[h] = ret_conv_7_hu_conv;
167                 }
168                 return ret_conv_7_arr;
169         }
170
171         /**
172          * Used by ChannelManager deserialization to broadcast the latest holder state if its copy of
173          * the Channel was out-of-date. You may use it to get a broadcastable holder toxic tx in case of
174          * fallen-behind, i.e when receiving a channel_reestablish with a proof that our counterparty side knows
175          * a higher revocation secret than the holder commitment number we are aware of. Broadcasting these
176          * transactions are UNSAFE, as they allow counterparty side to punish you. Nevertheless you may want to
177          * broadcast them if counterparty don't close channel with his higher commitment transaction after a
178          * substantial amount of time (a month or even a year) to get back funds. Best may be to contact
179          * out-of-band the other node operator to coordinate with him if option is available to you.
180          * In any-case, choice is up to the user.
181          */
182         public byte[][] get_latest_holder_commitment_txn(Logger logger) {
183                 byte[][] ret = bindings.ChannelMonitor_get_latest_holder_commitment_txn(this.ptr, logger == null ? 0 : logger.ptr);
184                 this.ptrs_to.add(logger);
185                 return ret;
186         }
187
188         /**
189          * Processes transactions in a newly connected block, which may result in any of the following:
190          * - update the monitor's state against resolved HTLCs
191          * - punish the counterparty in the case of seeing a revoked commitment transaction
192          * - force close the channel and claim/timeout incoming/outgoing HTLCs if near expiration
193          * - detect settled outputs for later spending
194          * - schedule and bump any in-flight claims
195          * 
196          * Returns any new outputs to watch from `txdata`; after called, these are also included in
197          * [`get_outputs_to_watch`].
198          * 
199          * [`get_outputs_to_watch`]: #method.get_outputs_to_watch
200          */
201         public TwoTuple<byte[], TwoTuple<Integer, TxOut>[]>[] block_connected(byte[] header, TwoTuple<Long, byte[]>[] txdata, int height, BroadcasterInterface broadcaster, FeeEstimator fee_estimator, Logger logger) {
202                 long[] ret = bindings.ChannelMonitor_block_connected(this.ptr, header, Arrays.stream(txdata).mapToLong(txdata_conv_24 -> bindings.C2Tuple_usizeTransactionZ_new(txdata_conv_24.a, txdata_conv_24.b)).toArray(), height, broadcaster == null ? 0 : broadcaster.ptr, fee_estimator == null ? 0 : fee_estimator.ptr, logger == null ? 0 : logger.ptr);
203                 TwoTuple<byte[], TwoTuple<Integer, TxOut>[]>[] ret_conv_46_arr = new TwoTuple[ret.length];
204                 for (int u = 0; u < ret.length; u++) {
205                         long ret_conv_46 = ret[u];
206                         byte[] ret_conv_46_a = bindings.LDKC2Tuple_TxidCVec_C2Tuple_u32TxOutZZZ_get_a(ret_conv_46);
207                         long[] ret_conv_46_b = bindings.LDKC2Tuple_TxidCVec_C2Tuple_u32TxOutZZZ_get_b(ret_conv_46);
208                         TwoTuple<Integer, TxOut>[] ret_conv_46_b_conv_26_arr = new TwoTuple[ret_conv_46_b.length];
209                         for (int a = 0; a < ret_conv_46_b.length; a++) {
210                                 long ret_conv_46_b_conv_26 = ret_conv_46_b[a];
211                                 int ret_conv_46_b_conv_26_a = bindings.LDKC2Tuple_u32TxOutZ_get_a(ret_conv_46_b_conv_26);
212                                 long ret_conv_46_b_conv_26_b = bindings.TxOut_clone(bindings.LDKC2Tuple_u32TxOutZ_get_b(ret_conv_46_b_conv_26));
213                                 TxOut ret_conv_46_b_conv_26_b_conv = new TxOut(null, ret_conv_46_b_conv_26_b);;
214                                 TwoTuple<Integer, TxOut> ret_conv_46_b_conv_26_conv = new TwoTuple<Integer, TxOut>(ret_conv_46_b_conv_26_a, ret_conv_46_b_conv_26_b_conv, () -> {
215                                         bindings.C2Tuple_u32TxOutZ_free(ret_conv_46_b_conv_26);
216                                 });
217                                 ret_conv_46_b_conv_26_b_conv.ptrs_to.add(ret_conv_46_b_conv_26_conv);
218                                 ret_conv_46_b_conv_26_arr[a] = ret_conv_46_b_conv_26_conv;
219                         };
220                         TwoTuple<byte[], TwoTuple<Integer, TxOut>[]> ret_conv_46_conv = new TwoTuple<byte[], TwoTuple<Integer, TxOut>[]>(ret_conv_46_a, ret_conv_46_b_conv_26_arr);
221                         // Warning: We may not free the C tuple object!
222                         ret_conv_46_arr[u] = ret_conv_46_conv;
223                 }
224                 /* TODO 2 TwoTuple<Long, byte[]>  */;
225                 this.ptrs_to.add(broadcaster);
226                 this.ptrs_to.add(fee_estimator);
227                 this.ptrs_to.add(logger);
228                 return ret_conv_46_arr;
229         }
230
231         /**
232          * Determines if the disconnected block contained any transactions of interest and updates
233          * appropriately.
234          */
235         public void block_disconnected(byte[] header, int height, BroadcasterInterface broadcaster, FeeEstimator fee_estimator, Logger logger) {
236                 bindings.ChannelMonitor_block_disconnected(this.ptr, header, height, broadcaster == null ? 0 : broadcaster.ptr, fee_estimator == null ? 0 : fee_estimator.ptr, logger == null ? 0 : logger.ptr);
237                 this.ptrs_to.add(broadcaster);
238                 this.ptrs_to.add(fee_estimator);
239                 this.ptrs_to.add(logger);
240         }
241
242 }