Merge pull request #33 from TheBlueMatt/main
[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 import javax.annotation.Nullable;
8
9
10 /**
11  * A ChannelMonitor handles chain events (blocks connected and disconnected) and generates
12  * on-chain transactions to ensure no loss of funds occurs.
13  * 
14  * You MUST ensure that no ChannelMonitors for a given channel anywhere contain out-of-date
15  * information and are actively monitoring the chain.
16  * 
17  * Pending Events or updated HTLCs which have not yet been read out by
18  * get_and_clear_pending_monitor_events or get_and_clear_pending_events are serialized to disk and
19  * reloaded at deserialize-time. Thus, you must ensure that, when handling events, all events
20  * gotten are fully handled before re-serializing the new state.
21  * 
22  * Note that the deserializer is only implemented for (BlockHash, ChannelMonitor), which
23  * tells you the last block hash which was block_connect()ed. You MUST rescan any blocks along
24  * the \"reorg path\" (ie disconnecting blocks until you find a common ancestor from both the
25  * returned block hash and the the current chain and then reconnecting blocks to get to the
26  * best chain) upon deserializing the object!
27  */
28 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
29 public class ChannelMonitor extends CommonBase {
30         ChannelMonitor(Object _dummy, long ptr) { super(ptr); }
31         @Override @SuppressWarnings("deprecation")
32         protected void finalize() throws Throwable {
33                 super.finalize();
34                 if (ptr != 0) { bindings.ChannelMonitor_free(ptr); }
35         }
36
37         /**
38          * Creates a copy of the ChannelMonitor
39          */
40         public ChannelMonitor clone() {
41                 long ret = bindings.ChannelMonitor_clone(this.ptr);
42                 if (ret < 1024) { return null; }
43                 ChannelMonitor ret_hu_conv = new ChannelMonitor(null, ret);
44                 ret_hu_conv.ptrs_to.add(this);
45                 return ret_hu_conv;
46         }
47
48         /**
49          * Serialize the ChannelMonitor object into a byte array which can be read by ChannelMonitor_read
50          */
51         public byte[] write() {
52                 byte[] ret = bindings.ChannelMonitor_write(this.ptr);
53                 return ret;
54         }
55
56         /**
57          * Updates a ChannelMonitor on the basis of some new information provided by the Channel
58          * itself.
59          * 
60          * panics if the given update is not the next update by update_id.
61          */
62         public Result_NoneMonitorUpdateErrorZ update_monitor(ChannelMonitorUpdate updates, BroadcasterInterface broadcaster, FeeEstimator fee_estimator, Logger logger) {
63                 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);
64                 if (ret < 1024) { return null; }
65                 Result_NoneMonitorUpdateErrorZ ret_hu_conv = Result_NoneMonitorUpdateErrorZ.constr_from_ptr(ret);
66                 this.ptrs_to.add(updates);
67                 this.ptrs_to.add(broadcaster);
68                 this.ptrs_to.add(fee_estimator);
69                 this.ptrs_to.add(logger);
70                 return ret_hu_conv;
71         }
72
73         /**
74          * Gets the update_id from the latest ChannelMonitorUpdate which was applied to this
75          * ChannelMonitor.
76          */
77         public long get_latest_update_id() {
78                 long ret = bindings.ChannelMonitor_get_latest_update_id(this.ptr);
79                 return ret;
80         }
81
82         /**
83          * Gets the funding transaction outpoint of the channel this ChannelMonitor is monitoring for.
84          */
85         public TwoTuple<OutPoint, byte[]> get_funding_txo() {
86                 long ret = bindings.ChannelMonitor_get_funding_txo(this.ptr);
87                 if (ret < 1024) { return null; }
88                 long ret_a = bindings.LDKC2Tuple_OutPointScriptZ_get_a(ret);
89                 OutPoint ret_a_hu_conv = new OutPoint(null, ret_a);
90                 ret_a_hu_conv.ptrs_to.add(this);;
91                 byte[] ret_b = bindings.LDKC2Tuple_OutPointScriptZ_get_b(ret);
92                 TwoTuple<OutPoint, byte[]> ret_conv = new TwoTuple<OutPoint, byte[]>(ret_a_hu_conv, ret_b, () -> {
93                         bindings.C2Tuple_OutPointScriptZ_free(ret);
94                 });
95                 ret_a_hu_conv.ptrs_to.add(ret_conv);
96                 return ret_conv;
97         }
98
99         /**
100          * Gets a list of txids, with their output scripts (in the order they appear in the
101          * transaction), which we must learn about spends of via block_connected().
102          */
103         public TwoTuple<byte[], TwoTuple<Integer, byte[]>[]>[] get_outputs_to_watch() {
104                 long[] ret = bindings.ChannelMonitor_get_outputs_to_watch(this.ptr);
105                 TwoTuple<byte[], TwoTuple<Integer, byte[]>[]>[] ret_conv_47_arr = new TwoTuple[ret.length];
106                 for (int v = 0; v < ret.length; v++) {
107                         long ret_conv_47 = ret[v];
108                         byte[] ret_conv_47_a = bindings.LDKC2Tuple_TxidCVec_C2Tuple_u32ScriptZZZ_get_a(ret_conv_47);
109                         long[] ret_conv_47_b = bindings.LDKC2Tuple_TxidCVec_C2Tuple_u32ScriptZZZ_get_b(ret_conv_47);
110                         TwoTuple<Integer, byte[]>[] ret_conv_47_b_conv_27_arr = new TwoTuple[ret_conv_47_b.length];
111                         for (int b = 0; b < ret_conv_47_b.length; b++) {
112                                 long ret_conv_47_b_conv_27 = ret_conv_47_b[b];
113                                 int ret_conv_47_b_conv_27_a = bindings.LDKC2Tuple_u32ScriptZ_get_a(ret_conv_47_b_conv_27);
114                                 byte[] ret_conv_47_b_conv_27_b = bindings.LDKC2Tuple_u32ScriptZ_get_b(ret_conv_47_b_conv_27);
115                                 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, () -> {
116                                         bindings.C2Tuple_u32ScriptZ_free(ret_conv_47_b_conv_27);
117                                 });
118                                 ret_conv_47_b_conv_27_arr[b] = ret_conv_47_b_conv_27_conv;
119                         };
120                         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);
121                         // Warning: We may not free the C tuple object!
122                         ret_conv_47_arr[v] = ret_conv_47_conv;
123                 }
124                 return ret_conv_47_arr;
125         }
126
127         /**
128          * Loads the funding txo and outputs to watch into the given `chain::Filter` by repeatedly
129          * calling `chain::Filter::register_output` and `chain::Filter::register_tx` until all outputs
130          * have been registered.
131          */
132         public void load_outputs_to_watch(Filter filter) {
133                 bindings.ChannelMonitor_load_outputs_to_watch(this.ptr, filter == null ? 0 : filter.ptr);
134                 this.ptrs_to.add(filter);
135         }
136
137         /**
138          * Get the list of HTLCs who's status has been updated on chain. This should be called by
139          * ChannelManager via [`chain::Watch::release_pending_monitor_events`].
140          */
141         public MonitorEvent[] get_and_clear_pending_monitor_events() {
142                 long[] ret = bindings.ChannelMonitor_get_and_clear_pending_monitor_events(this.ptr);
143                 MonitorEvent[] ret_conv_14_arr = new MonitorEvent[ret.length];
144                 for (int o = 0; o < ret.length; o++) {
145                         long ret_conv_14 = ret[o];
146                         MonitorEvent ret_conv_14_hu_conv = MonitorEvent.constr_from_ptr(ret_conv_14);
147                         ret_conv_14_hu_conv.ptrs_to.add(this);
148                         ret_conv_14_arr[o] = ret_conv_14_hu_conv;
149                 }
150                 return ret_conv_14_arr;
151         }
152
153         /**
154          * Gets the list of pending events which were generated by previous actions, clearing the list
155          * in the process.
156          * 
157          * This is called by ChainMonitor::get_and_clear_pending_events() and is equivalent to
158          * EventsProvider::get_and_clear_pending_events() except that it requires &mut self as we do
159          * no internal locking in ChannelMonitors.
160          */
161         public Event[] get_and_clear_pending_events() {
162                 long[] ret = bindings.ChannelMonitor_get_and_clear_pending_events(this.ptr);
163                 Event[] ret_conv_7_arr = new Event[ret.length];
164                 for (int h = 0; h < ret.length; h++) {
165                         long ret_conv_7 = ret[h];
166                         Event ret_conv_7_hu_conv = Event.constr_from_ptr(ret_conv_7);
167                         ret_conv_7_hu_conv.ptrs_to.add(this);
168                         ret_conv_7_arr[h] = ret_conv_7_hu_conv;
169                 }
170                 return ret_conv_7_arr;
171         }
172
173         /**
174          * Used by ChannelManager deserialization to broadcast the latest holder state if its copy of
175          * the Channel was out-of-date. You may use it to get a broadcastable holder toxic tx in case of
176          * fallen-behind, i.e when receiving a channel_reestablish with a proof that our counterparty side knows
177          * a higher revocation secret than the holder commitment number we are aware of. Broadcasting these
178          * transactions are UNSAFE, as they allow counterparty side to punish you. Nevertheless you may want to
179          * broadcast them if counterparty don't close channel with his higher commitment transaction after a
180          * substantial amount of time (a month or even a year) to get back funds. Best may be to contact
181          * out-of-band the other node operator to coordinate with him if option is available to you.
182          * In any-case, choice is up to the user.
183          */
184         public byte[][] get_latest_holder_commitment_txn(Logger logger) {
185                 byte[][] ret = bindings.ChannelMonitor_get_latest_holder_commitment_txn(this.ptr, logger == null ? 0 : logger.ptr);
186                 this.ptrs_to.add(logger);
187                 return ret;
188         }
189
190         /**
191          * Processes transactions in a newly connected block, which may result in any of the following:
192          * - update the monitor's state against resolved HTLCs
193          * - punish the counterparty in the case of seeing a revoked commitment transaction
194          * - force close the channel and claim/timeout incoming/outgoing HTLCs if near expiration
195          * - detect settled outputs for later spending
196          * - schedule and bump any in-flight claims
197          * 
198          * Returns any new outputs to watch from `txdata`; after called, these are also included in
199          * [`get_outputs_to_watch`].
200          * 
201          * [`get_outputs_to_watch`]: #method.get_outputs_to_watch
202          */
203         public TwoTuple<byte[], TwoTuple<Integer, TxOut>[]>[] block_connected(byte[] header, TwoTuple<Long, byte[]>[] txdata, int height, BroadcasterInterface broadcaster, FeeEstimator fee_estimator, Logger logger) {
204                 long[] ret = bindings.ChannelMonitor_block_connected(this.ptr, header, txdata != null ? Arrays.stream(txdata).mapToLong(txdata_conv_24 -> bindings.C2Tuple_usizeTransactionZ_new(txdata_conv_24.a, txdata_conv_24.b)).toArray() : null, height, broadcaster == null ? 0 : broadcaster.ptr, fee_estimator == null ? 0 : fee_estimator.ptr, logger == null ? 0 : logger.ptr);
205                 TwoTuple<byte[], TwoTuple<Integer, TxOut>[]>[] ret_conv_46_arr = new TwoTuple[ret.length];
206                 for (int u = 0; u < ret.length; u++) {
207                         long ret_conv_46 = ret[u];
208                         byte[] ret_conv_46_a = bindings.LDKC2Tuple_TxidCVec_C2Tuple_u32TxOutZZZ_get_a(ret_conv_46);
209                         long[] ret_conv_46_b = bindings.LDKC2Tuple_TxidCVec_C2Tuple_u32TxOutZZZ_get_b(ret_conv_46);
210                         TwoTuple<Integer, TxOut>[] ret_conv_46_b_conv_26_arr = new TwoTuple[ret_conv_46_b.length];
211                         for (int a = 0; a < ret_conv_46_b.length; a++) {
212                                 long ret_conv_46_b_conv_26 = ret_conv_46_b[a];
213                                 int ret_conv_46_b_conv_26_a = bindings.LDKC2Tuple_u32TxOutZ_get_a(ret_conv_46_b_conv_26);
214                                 long ret_conv_46_b_conv_26_b = bindings.TxOut_clone(bindings.LDKC2Tuple_u32TxOutZ_get_b(ret_conv_46_b_conv_26));
215                                 TxOut ret_conv_46_b_conv_26_b_conv = new TxOut(null, ret_conv_46_b_conv_26_b);;
216                                 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, () -> {
217                                         bindings.C2Tuple_u32TxOutZ_free(ret_conv_46_b_conv_26);
218                                 });
219                                 ret_conv_46_b_conv_26_b_conv.ptrs_to.add(ret_conv_46_b_conv_26_conv);
220                                 ret_conv_46_b_conv_26_arr[a] = ret_conv_46_b_conv_26_conv;
221                         };
222                         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);
223                         // Warning: We may not free the C tuple object!
224                         ret_conv_46_arr[u] = ret_conv_46_conv;
225                 }
226                 /* TODO 2 TwoTuple<Long, byte[]>  */;
227                 this.ptrs_to.add(broadcaster);
228                 this.ptrs_to.add(fee_estimator);
229                 this.ptrs_to.add(logger);
230                 return ret_conv_46_arr;
231         }
232
233         /**
234          * Determines if the disconnected block contained any transactions of interest and updates
235          * appropriately.
236          */
237         public void block_disconnected(byte[] header, int height, BroadcasterInterface broadcaster, FeeEstimator fee_estimator, Logger logger) {
238                 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);
239                 this.ptrs_to.add(broadcaster);
240                 this.ptrs_to.add(fee_estimator);
241                 this.ptrs_to.add(logger);
242         }
243
244         /**
245          * Processes transactions confirmed in a block with the given header and height, returning new
246          * outputs to watch. See [`block_connected`] for details.
247          * 
248          * Used instead of [`block_connected`] by clients that are notified of transactions rather than
249          * blocks. See [`chain::Confirm`] for calling expectations.
250          * 
251          * [`block_connected`]: Self::block_connected
252          */
253         public TwoTuple<byte[], TwoTuple<Integer, TxOut>[]>[] transactions_confirmed(byte[] header, TwoTuple<Long, byte[]>[] txdata, int height, BroadcasterInterface broadcaster, FeeEstimator fee_estimator, Logger logger) {
254                 long[] ret = bindings.ChannelMonitor_transactions_confirmed(this.ptr, header, txdata != null ? Arrays.stream(txdata).mapToLong(txdata_conv_24 -> bindings.C2Tuple_usizeTransactionZ_new(txdata_conv_24.a, txdata_conv_24.b)).toArray() : null, height, broadcaster == null ? 0 : broadcaster.ptr, fee_estimator == null ? 0 : fee_estimator.ptr, logger == null ? 0 : logger.ptr);
255                 TwoTuple<byte[], TwoTuple<Integer, TxOut>[]>[] ret_conv_46_arr = new TwoTuple[ret.length];
256                 for (int u = 0; u < ret.length; u++) {
257                         long ret_conv_46 = ret[u];
258                         byte[] ret_conv_46_a = bindings.LDKC2Tuple_TxidCVec_C2Tuple_u32TxOutZZZ_get_a(ret_conv_46);
259                         long[] ret_conv_46_b = bindings.LDKC2Tuple_TxidCVec_C2Tuple_u32TxOutZZZ_get_b(ret_conv_46);
260                         TwoTuple<Integer, TxOut>[] ret_conv_46_b_conv_26_arr = new TwoTuple[ret_conv_46_b.length];
261                         for (int a = 0; a < ret_conv_46_b.length; a++) {
262                                 long ret_conv_46_b_conv_26 = ret_conv_46_b[a];
263                                 int ret_conv_46_b_conv_26_a = bindings.LDKC2Tuple_u32TxOutZ_get_a(ret_conv_46_b_conv_26);
264                                 long ret_conv_46_b_conv_26_b = bindings.TxOut_clone(bindings.LDKC2Tuple_u32TxOutZ_get_b(ret_conv_46_b_conv_26));
265                                 TxOut ret_conv_46_b_conv_26_b_conv = new TxOut(null, ret_conv_46_b_conv_26_b);;
266                                 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, () -> {
267                                         bindings.C2Tuple_u32TxOutZ_free(ret_conv_46_b_conv_26);
268                                 });
269                                 ret_conv_46_b_conv_26_b_conv.ptrs_to.add(ret_conv_46_b_conv_26_conv);
270                                 ret_conv_46_b_conv_26_arr[a] = ret_conv_46_b_conv_26_conv;
271                         };
272                         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);
273                         // Warning: We may not free the C tuple object!
274                         ret_conv_46_arr[u] = ret_conv_46_conv;
275                 }
276                 /* TODO 2 TwoTuple<Long, byte[]>  */;
277                 this.ptrs_to.add(broadcaster);
278                 this.ptrs_to.add(fee_estimator);
279                 this.ptrs_to.add(logger);
280                 return ret_conv_46_arr;
281         }
282
283         /**
284          * Processes a transaction that was reorganized out of the chain.
285          * 
286          * Used instead of [`block_disconnected`] by clients that are notified of transactions rather
287          * than blocks. See [`chain::Confirm`] for calling expectations.
288          * 
289          * [`block_disconnected`]: Self::block_disconnected
290          */
291         public void transaction_unconfirmed(byte[] txid, BroadcasterInterface broadcaster, FeeEstimator fee_estimator, Logger logger) {
292                 bindings.ChannelMonitor_transaction_unconfirmed(this.ptr, txid, broadcaster == null ? 0 : broadcaster.ptr, fee_estimator == null ? 0 : fee_estimator.ptr, logger == null ? 0 : logger.ptr);
293                 this.ptrs_to.add(broadcaster);
294                 this.ptrs_to.add(fee_estimator);
295                 this.ptrs_to.add(logger);
296         }
297
298         /**
299          * Updates the monitor with the current best chain tip, returning new outputs to watch. See
300          * [`block_connected`] for details.
301          * 
302          * Used instead of [`block_connected`] by clients that are notified of transactions rather than
303          * blocks. See [`chain::Confirm`] for calling expectations.
304          * 
305          * [`block_connected`]: Self::block_connected
306          */
307         public TwoTuple<byte[], TwoTuple<Integer, TxOut>[]>[] best_block_updated(byte[] header, int height, BroadcasterInterface broadcaster, FeeEstimator fee_estimator, Logger logger) {
308                 long[] ret = bindings.ChannelMonitor_best_block_updated(this.ptr, header, height, broadcaster == null ? 0 : broadcaster.ptr, fee_estimator == null ? 0 : fee_estimator.ptr, logger == null ? 0 : logger.ptr);
309                 TwoTuple<byte[], TwoTuple<Integer, TxOut>[]>[] ret_conv_46_arr = new TwoTuple[ret.length];
310                 for (int u = 0; u < ret.length; u++) {
311                         long ret_conv_46 = ret[u];
312                         byte[] ret_conv_46_a = bindings.LDKC2Tuple_TxidCVec_C2Tuple_u32TxOutZZZ_get_a(ret_conv_46);
313                         long[] ret_conv_46_b = bindings.LDKC2Tuple_TxidCVec_C2Tuple_u32TxOutZZZ_get_b(ret_conv_46);
314                         TwoTuple<Integer, TxOut>[] ret_conv_46_b_conv_26_arr = new TwoTuple[ret_conv_46_b.length];
315                         for (int a = 0; a < ret_conv_46_b.length; a++) {
316                                 long ret_conv_46_b_conv_26 = ret_conv_46_b[a];
317                                 int ret_conv_46_b_conv_26_a = bindings.LDKC2Tuple_u32TxOutZ_get_a(ret_conv_46_b_conv_26);
318                                 long ret_conv_46_b_conv_26_b = bindings.TxOut_clone(bindings.LDKC2Tuple_u32TxOutZ_get_b(ret_conv_46_b_conv_26));
319                                 TxOut ret_conv_46_b_conv_26_b_conv = new TxOut(null, ret_conv_46_b_conv_26_b);;
320                                 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, () -> {
321                                         bindings.C2Tuple_u32TxOutZ_free(ret_conv_46_b_conv_26);
322                                 });
323                                 ret_conv_46_b_conv_26_b_conv.ptrs_to.add(ret_conv_46_b_conv_26_conv);
324                                 ret_conv_46_b_conv_26_arr[a] = ret_conv_46_b_conv_26_conv;
325                         };
326                         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);
327                         // Warning: We may not free the C tuple object!
328                         ret_conv_46_arr[u] = ret_conv_46_conv;
329                 }
330                 this.ptrs_to.add(broadcaster);
331                 this.ptrs_to.add(fee_estimator);
332                 this.ptrs_to.add(logger);
333                 return ret_conv_46_arr;
334         }
335
336         /**
337          * Returns the set of txids that should be monitored for re-organization out of the chain.
338          */
339         public byte[][] get_relevant_txids() {
340                 byte[][] ret = bindings.ChannelMonitor_get_relevant_txids(this.ptr);
341                 return ret;
342         }
343
344         /**
345          * Gets the latest best block which was connected either via the [`chain::Listen`] or
346          * [`chain::Confirm`] interfaces.
347          */
348         public BestBlock current_best_block() {
349                 long ret = bindings.ChannelMonitor_current_best_block(this.ptr);
350                 if (ret < 1024) { return null; }
351                 BestBlock ret_hu_conv = new BestBlock(null, ret);
352                 ret_hu_conv.ptrs_to.add(this);
353                 return ret_hu_conv;
354         }
355
356 }