da280c215be87591dd0709bb482e29dd040c05f9
[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 java.lang.ref.Reference;
8 import javax.annotation.Nullable;
9
10
11 /**
12  * A ChannelMonitor handles chain events (blocks connected and disconnected) and generates
13  * on-chain transactions to ensure no loss of funds occurs.
14  * 
15  * You MUST ensure that no ChannelMonitors for a given channel anywhere contain out-of-date
16  * information and are actively monitoring the chain.
17  * 
18  * Pending Events or updated HTLCs which have not yet been read out by
19  * get_and_clear_pending_monitor_events or get_and_clear_pending_events are serialized to disk and
20  * reloaded at deserialize-time. Thus, you must ensure that, when handling events, all events
21  * gotten are fully handled before re-serializing the new state.
22  * 
23  * Note that the deserializer is only implemented for (BlockHash, ChannelMonitor), which
24  * tells you the last block hash which was block_connect()ed. You MUST rescan any blocks along
25  * the \"reorg path\" (ie disconnecting blocks until you find a common ancestor from both the
26  * returned block hash and the the current chain and then reconnecting blocks to get to the
27  * best chain) upon deserializing the object!
28  */
29 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
30 public class ChannelMonitor extends CommonBase {
31         ChannelMonitor(Object _dummy, long ptr) { super(ptr); }
32         @Override @SuppressWarnings("deprecation")
33         protected void finalize() throws Throwable {
34                 super.finalize();
35                 if (ptr != 0) { bindings.ChannelMonitor_free(ptr); }
36         }
37
38         long clone_ptr() {
39                 long ret = bindings.ChannelMonitor_clone_ptr(this.ptr);
40                 Reference.reachabilityFence(this);
41                 return ret;
42         }
43
44         /**
45          * Creates a copy of the ChannelMonitor
46          */
47         public ChannelMonitor clone() {
48                 long ret = bindings.ChannelMonitor_clone(this.ptr);
49                 Reference.reachabilityFence(this);
50                 if (ret >= 0 && ret <= 4096) { return null; }
51                 org.ldk.structs.ChannelMonitor ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.ChannelMonitor(null, ret); }
52                 ret_hu_conv.ptrs_to.add(this);
53                 return ret_hu_conv;
54         }
55
56         /**
57          * Serialize the ChannelMonitor object into a byte array which can be read by ChannelMonitor_read
58          */
59         public byte[] write() {
60                 byte[] ret = bindings.ChannelMonitor_write(this.ptr);
61                 Reference.reachabilityFence(this);
62                 return ret;
63         }
64
65         /**
66          * Updates a ChannelMonitor on the basis of some new information provided by the Channel
67          * itself.
68          * 
69          * panics if the given update is not the next update by update_id.
70          */
71         public Result_NoneNoneZ update_monitor(ChannelMonitorUpdate updates, BroadcasterInterface broadcaster, FeeEstimator fee_estimator, Logger logger) {
72                 long ret = bindings.ChannelMonitor_update_monitor(this.ptr, updates == null ? 0 : updates.ptr, broadcaster == null ? 0 : broadcaster.ptr, fee_estimator == null ? 0 : fee_estimator.ptr, logger == null ? 0 : logger.ptr);
73                 Reference.reachabilityFence(this);
74                 Reference.reachabilityFence(updates);
75                 Reference.reachabilityFence(broadcaster);
76                 Reference.reachabilityFence(fee_estimator);
77                 Reference.reachabilityFence(logger);
78                 if (ret >= 0 && ret <= 4096) { return null; }
79                 Result_NoneNoneZ ret_hu_conv = Result_NoneNoneZ.constr_from_ptr(ret);
80                 this.ptrs_to.add(updates);
81                 this.ptrs_to.add(broadcaster);
82                 this.ptrs_to.add(fee_estimator);
83                 this.ptrs_to.add(logger);
84                 return ret_hu_conv;
85         }
86
87         /**
88          * Gets the update_id from the latest ChannelMonitorUpdate which was applied to this
89          * ChannelMonitor.
90          */
91         public long get_latest_update_id() {
92                 long ret = bindings.ChannelMonitor_get_latest_update_id(this.ptr);
93                 Reference.reachabilityFence(this);
94                 return ret;
95         }
96
97         /**
98          * Gets the funding transaction outpoint of the channel this ChannelMonitor is monitoring for.
99          */
100         public TwoTuple_OutPointScriptZ get_funding_txo() {
101                 long ret = bindings.ChannelMonitor_get_funding_txo(this.ptr);
102                 Reference.reachabilityFence(this);
103                 if (ret >= 0 && ret <= 4096) { return null; }
104                 TwoTuple_OutPointScriptZ ret_hu_conv = new TwoTuple_OutPointScriptZ(null, ret);
105                 ret_hu_conv.ptrs_to.add(this);
106                 return ret_hu_conv;
107         }
108
109         /**
110          * Gets a list of txids, with their output scripts (in the order they appear in the
111          * transaction), which we must learn about spends of via block_connected().
112          */
113         public TwoTuple_TxidCVec_C2Tuple_u32ScriptZZZ[] get_outputs_to_watch() {
114                 long[] ret = bindings.ChannelMonitor_get_outputs_to_watch(this.ptr);
115                 Reference.reachabilityFence(this);
116                 int ret_conv_40_len = ret.length;
117                 TwoTuple_TxidCVec_C2Tuple_u32ScriptZZZ[] ret_conv_40_arr = new TwoTuple_TxidCVec_C2Tuple_u32ScriptZZZ[ret_conv_40_len];
118                 for (int o = 0; o < ret_conv_40_len; o++) {
119                         long ret_conv_40 = ret[o];
120                         TwoTuple_TxidCVec_C2Tuple_u32ScriptZZZ ret_conv_40_hu_conv = new TwoTuple_TxidCVec_C2Tuple_u32ScriptZZZ(null, ret_conv_40);
121                         ret_conv_40_hu_conv.ptrs_to.add(this);
122                         ret_conv_40_arr[o] = ret_conv_40_hu_conv;
123                 }
124                 return ret_conv_40_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                 Reference.reachabilityFence(this);
135                 Reference.reachabilityFence(filter);
136                 this.ptrs_to.add(filter);
137         }
138
139         /**
140          * Get the list of HTLCs who's status has been updated on chain. This should be called by
141          * ChannelManager via [`chain::Watch::release_pending_monitor_events`].
142          */
143         public MonitorEvent[] get_and_clear_pending_monitor_events() {
144                 long[] ret = bindings.ChannelMonitor_get_and_clear_pending_monitor_events(this.ptr);
145                 Reference.reachabilityFence(this);
146                 int ret_conv_14_len = ret.length;
147                 MonitorEvent[] ret_conv_14_arr = new MonitorEvent[ret_conv_14_len];
148                 for (int o = 0; o < ret_conv_14_len; o++) {
149                         long ret_conv_14 = ret[o];
150                         org.ldk.structs.MonitorEvent ret_conv_14_hu_conv = org.ldk.structs.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         /**
158          * Gets the list of pending events which were generated by previous actions, clearing the list
159          * in the process.
160          * 
161          * This is called by ChainMonitor::get_and_clear_pending_events() and is equivalent to
162          * EventsProvider::get_and_clear_pending_events() except that it requires &mut self as we do
163          * no internal locking in ChannelMonitors.
164          */
165         public Event[] get_and_clear_pending_events() {
166                 long[] ret = bindings.ChannelMonitor_get_and_clear_pending_events(this.ptr);
167                 Reference.reachabilityFence(this);
168                 int ret_conv_7_len = ret.length;
169                 Event[] ret_conv_7_arr = new Event[ret_conv_7_len];
170                 for (int h = 0; h < ret_conv_7_len; h++) {
171                         long ret_conv_7 = ret[h];
172                         org.ldk.structs.Event ret_conv_7_hu_conv = org.ldk.structs.Event.constr_from_ptr(ret_conv_7);
173                         ret_conv_7_hu_conv.ptrs_to.add(this);
174                         ret_conv_7_arr[h] = ret_conv_7_hu_conv;
175                 }
176                 return ret_conv_7_arr;
177         }
178
179         /**
180          * Gets the `node_id` of the counterparty for this channel.
181          * 
182          * Will be `None` for channels constructed on LDK versions prior to 0.0.110 and always `Some`
183          * otherwise.
184          * 
185          * Note that the return value (or a relevant inner pointer) may be NULL or all-0s to represent None
186          */
187         @Nullable
188         public byte[] get_counterparty_node_id() {
189                 byte[] ret = bindings.ChannelMonitor_get_counterparty_node_id(this.ptr);
190                 Reference.reachabilityFence(this);
191                 return ret;
192         }
193
194         /**
195          * Used by ChannelManager deserialization to broadcast the latest holder state if its copy of
196          * the Channel was out-of-date. You may use it to get a broadcastable holder toxic tx in case of
197          * fallen-behind, i.e when receiving a channel_reestablish with a proof that our counterparty side knows
198          * a higher revocation secret than the holder commitment number we are aware of. Broadcasting these
199          * transactions are UNSAFE, as they allow counterparty side to punish you. Nevertheless you may want to
200          * broadcast them if counterparty don't close channel with his higher commitment transaction after a
201          * substantial amount of time (a month or even a year) to get back funds. Best may be to contact
202          * out-of-band the other node operator to coordinate with him if option is available to you.
203          * In any-case, choice is up to the user.
204          */
205         public byte[][] get_latest_holder_commitment_txn(Logger logger) {
206                 byte[][] ret = bindings.ChannelMonitor_get_latest_holder_commitment_txn(this.ptr, logger == null ? 0 : logger.ptr);
207                 Reference.reachabilityFence(this);
208                 Reference.reachabilityFence(logger);
209                 this.ptrs_to.add(logger);
210                 return ret;
211         }
212
213         /**
214          * Processes transactions in a newly connected block, which may result in any of the following:
215          * - update the monitor's state against resolved HTLCs
216          * - punish the counterparty in the case of seeing a revoked commitment transaction
217          * - force close the channel and claim/timeout incoming/outgoing HTLCs if near expiration
218          * - detect settled outputs for later spending
219          * - schedule and bump any in-flight claims
220          * 
221          * Returns any new outputs to watch from `txdata`; after called, these are also included in
222          * [`get_outputs_to_watch`].
223          * 
224          * [`get_outputs_to_watch`]: #method.get_outputs_to_watch
225          */
226         public TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[] block_connected(byte[] header, TwoTuple_usizeTransactionZ[] txdata, int height, BroadcasterInterface broadcaster, FeeEstimator fee_estimator, Logger logger) {
227                 long[] ret = bindings.ChannelMonitor_block_connected(this.ptr, InternalUtils.check_arr_len(header, 80), txdata != null ? Arrays.stream(txdata).mapToLong(txdata_conv_28 -> txdata_conv_28 != null ? txdata_conv_28.ptr : 0).toArray() : null, height, broadcaster == null ? 0 : broadcaster.ptr, fee_estimator == null ? 0 : fee_estimator.ptr, logger == null ? 0 : logger.ptr);
228                 Reference.reachabilityFence(this);
229                 Reference.reachabilityFence(header);
230                 Reference.reachabilityFence(txdata);
231                 Reference.reachabilityFence(height);
232                 Reference.reachabilityFence(broadcaster);
233                 Reference.reachabilityFence(fee_estimator);
234                 Reference.reachabilityFence(logger);
235                 int ret_conv_39_len = ret.length;
236                 TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[] ret_conv_39_arr = new TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[ret_conv_39_len];
237                 for (int n = 0; n < ret_conv_39_len; n++) {
238                         long ret_conv_39 = ret[n];
239                         TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ ret_conv_39_hu_conv = new TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ(null, ret_conv_39);
240                         ret_conv_39_hu_conv.ptrs_to.add(this);
241                         ret_conv_39_arr[n] = ret_conv_39_hu_conv;
242                 }
243                 this.ptrs_to.add(broadcaster);
244                 this.ptrs_to.add(fee_estimator);
245                 this.ptrs_to.add(logger);
246                 return ret_conv_39_arr;
247         }
248
249         /**
250          * Determines if the disconnected block contained any transactions of interest and updates
251          * appropriately.
252          */
253         public void block_disconnected(byte[] header, int height, BroadcasterInterface broadcaster, FeeEstimator fee_estimator, Logger logger) {
254                 bindings.ChannelMonitor_block_disconnected(this.ptr, InternalUtils.check_arr_len(header, 80), height, broadcaster == null ? 0 : broadcaster.ptr, fee_estimator == null ? 0 : fee_estimator.ptr, logger == null ? 0 : logger.ptr);
255                 Reference.reachabilityFence(this);
256                 Reference.reachabilityFence(header);
257                 Reference.reachabilityFence(height);
258                 Reference.reachabilityFence(broadcaster);
259                 Reference.reachabilityFence(fee_estimator);
260                 Reference.reachabilityFence(logger);
261                 this.ptrs_to.add(broadcaster);
262                 this.ptrs_to.add(fee_estimator);
263                 this.ptrs_to.add(logger);
264         }
265
266         /**
267          * Processes transactions confirmed in a block with the given header and height, returning new
268          * outputs to watch. See [`block_connected`] for details.
269          * 
270          * Used instead of [`block_connected`] by clients that are notified of transactions rather than
271          * blocks. See [`chain::Confirm`] for calling expectations.
272          * 
273          * [`block_connected`]: Self::block_connected
274          */
275         public TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[] transactions_confirmed(byte[] header, TwoTuple_usizeTransactionZ[] txdata, int height, BroadcasterInterface broadcaster, FeeEstimator fee_estimator, Logger logger) {
276                 long[] ret = bindings.ChannelMonitor_transactions_confirmed(this.ptr, InternalUtils.check_arr_len(header, 80), txdata != null ? Arrays.stream(txdata).mapToLong(txdata_conv_28 -> txdata_conv_28 != null ? txdata_conv_28.ptr : 0).toArray() : null, height, broadcaster == null ? 0 : broadcaster.ptr, fee_estimator == null ? 0 : fee_estimator.ptr, logger == null ? 0 : logger.ptr);
277                 Reference.reachabilityFence(this);
278                 Reference.reachabilityFence(header);
279                 Reference.reachabilityFence(txdata);
280                 Reference.reachabilityFence(height);
281                 Reference.reachabilityFence(broadcaster);
282                 Reference.reachabilityFence(fee_estimator);
283                 Reference.reachabilityFence(logger);
284                 int ret_conv_39_len = ret.length;
285                 TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[] ret_conv_39_arr = new TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[ret_conv_39_len];
286                 for (int n = 0; n < ret_conv_39_len; n++) {
287                         long ret_conv_39 = ret[n];
288                         TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ ret_conv_39_hu_conv = new TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ(null, ret_conv_39);
289                         ret_conv_39_hu_conv.ptrs_to.add(this);
290                         ret_conv_39_arr[n] = ret_conv_39_hu_conv;
291                 }
292                 this.ptrs_to.add(broadcaster);
293                 this.ptrs_to.add(fee_estimator);
294                 this.ptrs_to.add(logger);
295                 return ret_conv_39_arr;
296         }
297
298         /**
299          * Processes a transaction that was reorganized out of the chain.
300          * 
301          * Used instead of [`block_disconnected`] by clients that are notified of transactions rather
302          * than blocks. See [`chain::Confirm`] for calling expectations.
303          * 
304          * [`block_disconnected`]: Self::block_disconnected
305          */
306         public void transaction_unconfirmed(byte[] txid, BroadcasterInterface broadcaster, FeeEstimator fee_estimator, Logger logger) {
307                 bindings.ChannelMonitor_transaction_unconfirmed(this.ptr, InternalUtils.check_arr_len(txid, 32), broadcaster == null ? 0 : broadcaster.ptr, fee_estimator == null ? 0 : fee_estimator.ptr, logger == null ? 0 : logger.ptr);
308                 Reference.reachabilityFence(this);
309                 Reference.reachabilityFence(txid);
310                 Reference.reachabilityFence(broadcaster);
311                 Reference.reachabilityFence(fee_estimator);
312                 Reference.reachabilityFence(logger);
313                 this.ptrs_to.add(broadcaster);
314                 this.ptrs_to.add(fee_estimator);
315                 this.ptrs_to.add(logger);
316         }
317
318         /**
319          * Updates the monitor with the current best chain tip, returning new outputs to watch. See
320          * [`block_connected`] for details.
321          * 
322          * Used instead of [`block_connected`] by clients that are notified of transactions rather than
323          * blocks. See [`chain::Confirm`] for calling expectations.
324          * 
325          * [`block_connected`]: Self::block_connected
326          */
327         public TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[] best_block_updated(byte[] header, int height, BroadcasterInterface broadcaster, FeeEstimator fee_estimator, Logger logger) {
328                 long[] ret = bindings.ChannelMonitor_best_block_updated(this.ptr, InternalUtils.check_arr_len(header, 80), height, broadcaster == null ? 0 : broadcaster.ptr, fee_estimator == null ? 0 : fee_estimator.ptr, logger == null ? 0 : logger.ptr);
329                 Reference.reachabilityFence(this);
330                 Reference.reachabilityFence(header);
331                 Reference.reachabilityFence(height);
332                 Reference.reachabilityFence(broadcaster);
333                 Reference.reachabilityFence(fee_estimator);
334                 Reference.reachabilityFence(logger);
335                 int ret_conv_39_len = ret.length;
336                 TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[] ret_conv_39_arr = new TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[ret_conv_39_len];
337                 for (int n = 0; n < ret_conv_39_len; n++) {
338                         long ret_conv_39 = ret[n];
339                         TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ ret_conv_39_hu_conv = new TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ(null, ret_conv_39);
340                         ret_conv_39_hu_conv.ptrs_to.add(this);
341                         ret_conv_39_arr[n] = ret_conv_39_hu_conv;
342                 }
343                 this.ptrs_to.add(broadcaster);
344                 this.ptrs_to.add(fee_estimator);
345                 this.ptrs_to.add(logger);
346                 return ret_conv_39_arr;
347         }
348
349         /**
350          * Returns the set of txids that should be monitored for re-organization out of the chain.
351          */
352         public byte[][] get_relevant_txids() {
353                 byte[][] ret = bindings.ChannelMonitor_get_relevant_txids(this.ptr);
354                 Reference.reachabilityFence(this);
355                 return ret;
356         }
357
358         /**
359          * Gets the latest best block which was connected either via the [`chain::Listen`] or
360          * [`chain::Confirm`] interfaces.
361          */
362         public BestBlock current_best_block() {
363                 long ret = bindings.ChannelMonitor_current_best_block(this.ptr);
364                 Reference.reachabilityFence(this);
365                 if (ret >= 0 && ret <= 4096) { return null; }
366                 org.ldk.structs.BestBlock ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.BestBlock(null, ret); }
367                 ret_hu_conv.ptrs_to.add(this);
368                 return ret_hu_conv;
369         }
370
371         /**
372          * Gets the balances in this channel which are either claimable by us if we were to
373          * force-close the channel now or which are claimable on-chain (possibly awaiting
374          * confirmation).
375          * 
376          * Any balances in the channel which are available on-chain (excluding on-chain fees) are
377          * included here until an [`Event::SpendableOutputs`] event has been generated for the
378          * balance, or until our counterparty has claimed the balance and accrued several
379          * confirmations on the claim transaction.
380          * 
381          * Note that the balances available when you or your counterparty have broadcasted revoked
382          * state(s) may not be fully captured here.
383          * 
384          * See [`Balance`] for additional details on the types of claimable balances which
385          * may be returned here and their meanings.
386          */
387         public Balance[] get_claimable_balances() {
388                 long[] ret = bindings.ChannelMonitor_get_claimable_balances(this.ptr);
389                 Reference.reachabilityFence(this);
390                 int ret_conv_9_len = ret.length;
391                 Balance[] ret_conv_9_arr = new Balance[ret_conv_9_len];
392                 for (int j = 0; j < ret_conv_9_len; j++) {
393                         long ret_conv_9 = ret[j];
394                         org.ldk.structs.Balance ret_conv_9_hu_conv = org.ldk.structs.Balance.constr_from_ptr(ret_conv_9);
395                         ret_conv_9_hu_conv.ptrs_to.add(this);
396                         ret_conv_9_arr[j] = ret_conv_9_hu_conv;
397                 }
398                 return ret_conv_9_arr;
399         }
400
401 }