Merge pull request #141 from TheBlueMatt/main
[ldk-java] / c_sharp / src / org / ldk / structs / ChannelMonitor.cs
1 using org.ldk.impl;
2 using org.ldk.enums;
3 using org.ldk.util;
4 using System;
5
6 namespace org { namespace ldk { namespace structs {
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  * Note that the deserializer is only implemented for (BlockHash, ChannelMonitor), which
17  * tells you the last block hash which was block_connect()ed. You MUST rescan any blocks along
18  * the \"reorg path\" (ie disconnecting blocks until you find a common ancestor from both the
19  * returned block hash and the the current chain and then reconnecting blocks to get to the
20  * best chain) upon deserializing the object!
21  */
22 public class ChannelMonitor : CommonBase {
23         internal ChannelMonitor(object _dummy, long ptr) : base(ptr) { }
24         ~ChannelMonitor() {
25                 if (ptr != 0) { bindings.ChannelMonitor_free(ptr); }
26         }
27
28         internal long clone_ptr() {
29                 long ret = bindings.ChannelMonitor_clone_ptr(this.ptr);
30                 GC.KeepAlive(this);
31                 return ret;
32         }
33
34         /**
35          * Creates a copy of the ChannelMonitor
36          */
37         public ChannelMonitor clone() {
38                 long ret = bindings.ChannelMonitor_clone(this.ptr);
39                 GC.KeepAlive(this);
40                 if (ret >= 0 && ret <= 4096) { return null; }
41                 org.ldk.structs.ChannelMonitor ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.ChannelMonitor(null, ret); }
42                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(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                 GC.KeepAlive(this);
52                 return ret;
53         }
54
55         /**
56          * Updates a ChannelMonitor on the basis of some new information provided by the Channel
57          * itself.
58          * 
59          * panics if the given update is not the next update by update_id.
60          */
61         public Result_NoneNoneZ update_monitor(org.ldk.structs.ChannelMonitorUpdate updates, org.ldk.structs.BroadcasterInterface broadcaster, org.ldk.structs.FeeEstimator fee_estimator, org.ldk.structs.Logger logger) {
62                 long ret = bindings.ChannelMonitor_update_monitor(this.ptr, updates == null ? 0 : updates.ptr, broadcaster.ptr, fee_estimator.ptr, logger.ptr);
63                 GC.KeepAlive(this);
64                 GC.KeepAlive(updates);
65                 GC.KeepAlive(broadcaster);
66                 GC.KeepAlive(fee_estimator);
67                 GC.KeepAlive(logger);
68                 if (ret >= 0 && ret <= 4096) { return null; }
69                 Result_NoneNoneZ ret_hu_conv = Result_NoneNoneZ.constr_from_ptr(ret);
70                 if (this != null) { this.ptrs_to.AddLast(updates); };
71                 if (this != null) { this.ptrs_to.AddLast(broadcaster); };
72                 if (this != null) { this.ptrs_to.AddLast(fee_estimator); };
73                 if (this != null) { this.ptrs_to.AddLast(logger); };
74                 return ret_hu_conv;
75         }
76
77         /**
78          * Gets the update_id from the latest ChannelMonitorUpdate which was applied to this
79          * ChannelMonitor.
80          */
81         public long get_latest_update_id() {
82                 long ret = bindings.ChannelMonitor_get_latest_update_id(this.ptr);
83                 GC.KeepAlive(this);
84                 return ret;
85         }
86
87         /**
88          * Gets the funding transaction outpoint of the channel this ChannelMonitor is monitoring for.
89          */
90         public TwoTuple_OutPointScriptZ get_funding_txo() {
91                 long ret = bindings.ChannelMonitor_get_funding_txo(this.ptr);
92                 GC.KeepAlive(this);
93                 if (ret >= 0 && ret <= 4096) { return null; }
94                 TwoTuple_OutPointScriptZ ret_hu_conv = new TwoTuple_OutPointScriptZ(null, ret);
95                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };
96                 return ret_hu_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_TxidCVec_C2Tuple_u32ScriptZZZ[] get_outputs_to_watch() {
104                 long[] ret = bindings.ChannelMonitor_get_outputs_to_watch(this.ptr);
105                 GC.KeepAlive(this);
106                 int ret_conv_40_len = ret.Length;
107                 TwoTuple_TxidCVec_C2Tuple_u32ScriptZZZ[] ret_conv_40_arr = new TwoTuple_TxidCVec_C2Tuple_u32ScriptZZZ[ret_conv_40_len];
108                 for (int o = 0; o < ret_conv_40_len; o++) {
109                         long ret_conv_40 = ret[o];
110                         TwoTuple_TxidCVec_C2Tuple_u32ScriptZZZ ret_conv_40_hu_conv = new TwoTuple_TxidCVec_C2Tuple_u32ScriptZZZ(null, ret_conv_40);
111                         if (ret_conv_40_hu_conv != null) { ret_conv_40_hu_conv.ptrs_to.AddLast(this); };
112                         ret_conv_40_arr[o] = ret_conv_40_hu_conv;
113                 }
114                 return ret_conv_40_arr;
115         }
116
117         /**
118          * Loads the funding txo and outputs to watch into the given `chain::Filter` by repeatedly
119          * calling `chain::Filter::register_output` and `chain::Filter::register_tx` until all outputs
120          * have been registered.
121          */
122         public void load_outputs_to_watch(org.ldk.structs.Filter filter) {
123                 bindings.ChannelMonitor_load_outputs_to_watch(this.ptr, filter.ptr);
124                 GC.KeepAlive(this);
125                 GC.KeepAlive(filter);
126                 if (this != null) { this.ptrs_to.AddLast(filter); };
127         }
128
129         /**
130          * Get the list of HTLCs who's status has been updated on chain. This should be called by
131          * ChannelManager via [`chain::Watch::release_pending_monitor_events`].
132          */
133         public MonitorEvent[] get_and_clear_pending_monitor_events() {
134                 long[] ret = bindings.ChannelMonitor_get_and_clear_pending_monitor_events(this.ptr);
135                 GC.KeepAlive(this);
136                 int ret_conv_14_len = ret.Length;
137                 MonitorEvent[] ret_conv_14_arr = new MonitorEvent[ret_conv_14_len];
138                 for (int o = 0; o < ret_conv_14_len; o++) {
139                         long ret_conv_14 = ret[o];
140                         org.ldk.structs.MonitorEvent ret_conv_14_hu_conv = org.ldk.structs.MonitorEvent.constr_from_ptr(ret_conv_14);
141                         if (ret_conv_14_hu_conv != null) { ret_conv_14_hu_conv.ptrs_to.AddLast(this); };
142                         ret_conv_14_arr[o] = ret_conv_14_hu_conv;
143                 }
144                 return ret_conv_14_arr;
145         }
146
147         /**
148          * Processes [`SpendableOutputs`] events produced from each [`ChannelMonitor`] upon maturity.
149          * 
150          * For channels featuring anchor outputs, this method will also process [`BumpTransaction`]
151          * events produced from each [`ChannelMonitor`] while there is a balance to claim onchain
152          * within each channel. As the confirmation of a commitment transaction may be critical to the
153          * safety of funds, we recommend invoking this every 30 seconds, or lower if running in an
154          * environment with spotty connections, like on mobile.
155          * 
156          * An [`EventHandler`] may safely call back to the provider, though this shouldn't be needed in
157          * order to handle these events.
158          * 
159          * [`SpendableOutputs`]: crate::events::Event::SpendableOutputs
160          * [`BumpTransaction`]: crate::events::Event::BumpTransaction
161          */
162         public void process_pending_events(org.ldk.structs.EventHandler handler) {
163                 bindings.ChannelMonitor_process_pending_events(this.ptr, handler.ptr);
164                 GC.KeepAlive(this);
165                 GC.KeepAlive(handler);
166                 if (this != null) { this.ptrs_to.AddLast(handler); };
167         }
168
169         /**
170          * Gets the `node_id` of the counterparty for this channel.
171          * 
172          * Will be `None` for channels constructed on LDK versions prior to 0.0.110 and always `Some`
173          * otherwise.
174          * 
175          * Note that the return value (or a relevant inner pointer) may be NULL or all-0s to represent None
176          */
177         public byte[] get_counterparty_node_id() {
178                 byte[] ret = bindings.ChannelMonitor_get_counterparty_node_id(this.ptr);
179                 GC.KeepAlive(this);
180                 return ret;
181         }
182
183         /**
184          * Used by ChannelManager deserialization to broadcast the latest holder state if its copy of
185          * the Channel was out-of-date.
186          * 
187          * You may also use this to broadcast the latest local commitment transaction, either because
188          * a monitor update failed with [`ChannelMonitorUpdateStatus::PermanentFailure`] or because we've
189          * fallen behind (i.e. we've received proof that our counterparty side knows a revocation
190          * secret we gave them that they shouldn't know).
191          * 
192          * Broadcasting these transactions in the second case is UNSAFE, as they allow counterparty
193          * side to punish you. Nevertheless you may want to broadcast them if counterparty doesn't
194          * close channel with their commitment transaction after a substantial amount of time. Best
195          * may be to contact the other node operator out-of-band to coordinate other options available
196          * to you. In any-case, the choice is up to you.
197          * 
198          * [`ChannelMonitorUpdateStatus::PermanentFailure`]: super::ChannelMonitorUpdateStatus::PermanentFailure
199          */
200         public byte[][] get_latest_holder_commitment_txn(org.ldk.structs.Logger logger) {
201                 byte[][] ret = bindings.ChannelMonitor_get_latest_holder_commitment_txn(this.ptr, logger.ptr);
202                 GC.KeepAlive(this);
203                 GC.KeepAlive(logger);
204                 if (this != null) { this.ptrs_to.AddLast(logger); };
205                 return ret;
206         }
207
208         /**
209          * Processes transactions in a newly connected block, which may result in any of the following:
210          * - update the monitor's state against resolved HTLCs
211          * - punish the counterparty in the case of seeing a revoked commitment transaction
212          * - force close the channel and claim/timeout incoming/outgoing HTLCs if near expiration
213          * - detect settled outputs for later spending
214          * - schedule and bump any in-flight claims
215          * 
216          * Returns any new outputs to watch from `txdata`; after called, these are also included in
217          * [`get_outputs_to_watch`].
218          * 
219          * [`get_outputs_to_watch`]: #method.get_outputs_to_watch
220          */
221         public TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[] block_connected(byte[] header, TwoTuple_usizeTransactionZ[] txdata, int height, org.ldk.structs.BroadcasterInterface broadcaster, org.ldk.structs.FeeEstimator fee_estimator, org.ldk.structs.Logger logger) {
222                 long[] ret = bindings.ChannelMonitor_block_connected(this.ptr, InternalUtils.check_arr_len(header, 80), txdata != null ? InternalUtils.mapArray(txdata, txdata_conv_28 => txdata_conv_28 != null ? txdata_conv_28.ptr : 0) : null, height, broadcaster.ptr, fee_estimator.ptr, logger.ptr);
223                 GC.KeepAlive(this);
224                 GC.KeepAlive(header);
225                 GC.KeepAlive(txdata);
226                 GC.KeepAlive(height);
227                 GC.KeepAlive(broadcaster);
228                 GC.KeepAlive(fee_estimator);
229                 GC.KeepAlive(logger);
230                 int ret_conv_39_len = ret.Length;
231                 TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[] ret_conv_39_arr = new TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[ret_conv_39_len];
232                 for (int n = 0; n < ret_conv_39_len; n++) {
233                         long ret_conv_39 = ret[n];
234                         TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ ret_conv_39_hu_conv = new TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ(null, ret_conv_39);
235                         if (ret_conv_39_hu_conv != null) { ret_conv_39_hu_conv.ptrs_to.AddLast(this); };
236                         ret_conv_39_arr[n] = ret_conv_39_hu_conv;
237                 }
238                 if (this != null) { this.ptrs_to.AddLast(broadcaster); };
239                 if (this != null) { this.ptrs_to.AddLast(fee_estimator); };
240                 if (this != null) { this.ptrs_to.AddLast(logger); };
241                 return ret_conv_39_arr;
242         }
243
244         /**
245          * Determines if the disconnected block contained any transactions of interest and updates
246          * appropriately.
247          */
248         public void block_disconnected(byte[] header, int height, org.ldk.structs.BroadcasterInterface broadcaster, org.ldk.structs.FeeEstimator fee_estimator, org.ldk.structs.Logger logger) {
249                 bindings.ChannelMonitor_block_disconnected(this.ptr, InternalUtils.check_arr_len(header, 80), height, broadcaster.ptr, fee_estimator.ptr, logger.ptr);
250                 GC.KeepAlive(this);
251                 GC.KeepAlive(header);
252                 GC.KeepAlive(height);
253                 GC.KeepAlive(broadcaster);
254                 GC.KeepAlive(fee_estimator);
255                 GC.KeepAlive(logger);
256                 if (this != null) { this.ptrs_to.AddLast(broadcaster); };
257                 if (this != null) { this.ptrs_to.AddLast(fee_estimator); };
258                 if (this != null) { this.ptrs_to.AddLast(logger); };
259         }
260
261         /**
262          * Processes transactions confirmed in a block with the given header and height, returning new
263          * outputs to watch. See [`block_connected`] for details.
264          * 
265          * Used instead of [`block_connected`] by clients that are notified of transactions rather than
266          * blocks. See [`chain::Confirm`] for calling expectations.
267          * 
268          * [`block_connected`]: Self::block_connected
269          */
270         public TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[] transactions_confirmed(byte[] header, TwoTuple_usizeTransactionZ[] txdata, int height, org.ldk.structs.BroadcasterInterface broadcaster, org.ldk.structs.FeeEstimator fee_estimator, org.ldk.structs.Logger logger) {
271                 long[] ret = bindings.ChannelMonitor_transactions_confirmed(this.ptr, InternalUtils.check_arr_len(header, 80), txdata != null ? InternalUtils.mapArray(txdata, txdata_conv_28 => txdata_conv_28 != null ? txdata_conv_28.ptr : 0) : null, height, broadcaster.ptr, fee_estimator.ptr, logger.ptr);
272                 GC.KeepAlive(this);
273                 GC.KeepAlive(header);
274                 GC.KeepAlive(txdata);
275                 GC.KeepAlive(height);
276                 GC.KeepAlive(broadcaster);
277                 GC.KeepAlive(fee_estimator);
278                 GC.KeepAlive(logger);
279                 int ret_conv_39_len = ret.Length;
280                 TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[] ret_conv_39_arr = new TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[ret_conv_39_len];
281                 for (int n = 0; n < ret_conv_39_len; n++) {
282                         long ret_conv_39 = ret[n];
283                         TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ ret_conv_39_hu_conv = new TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ(null, ret_conv_39);
284                         if (ret_conv_39_hu_conv != null) { ret_conv_39_hu_conv.ptrs_to.AddLast(this); };
285                         ret_conv_39_arr[n] = ret_conv_39_hu_conv;
286                 }
287                 if (this != null) { this.ptrs_to.AddLast(broadcaster); };
288                 if (this != null) { this.ptrs_to.AddLast(fee_estimator); };
289                 if (this != null) { this.ptrs_to.AddLast(logger); };
290                 return ret_conv_39_arr;
291         }
292
293         /**
294          * Processes a transaction that was reorganized out of the chain.
295          * 
296          * Used instead of [`block_disconnected`] by clients that are notified of transactions rather
297          * than blocks. See [`chain::Confirm`] for calling expectations.
298          * 
299          * [`block_disconnected`]: Self::block_disconnected
300          */
301         public void transaction_unconfirmed(byte[] txid, org.ldk.structs.BroadcasterInterface broadcaster, org.ldk.structs.FeeEstimator fee_estimator, org.ldk.structs.Logger logger) {
302                 bindings.ChannelMonitor_transaction_unconfirmed(this.ptr, InternalUtils.check_arr_len(txid, 32), broadcaster.ptr, fee_estimator.ptr, logger.ptr);
303                 GC.KeepAlive(this);
304                 GC.KeepAlive(txid);
305                 GC.KeepAlive(broadcaster);
306                 GC.KeepAlive(fee_estimator);
307                 GC.KeepAlive(logger);
308                 if (this != null) { this.ptrs_to.AddLast(broadcaster); };
309                 if (this != null) { this.ptrs_to.AddLast(fee_estimator); };
310                 if (this != null) { this.ptrs_to.AddLast(logger); };
311         }
312
313         /**
314          * Updates the monitor with the current best chain tip, returning new outputs to watch. See
315          * [`block_connected`] for details.
316          * 
317          * Used instead of [`block_connected`] by clients that are notified of transactions rather than
318          * blocks. See [`chain::Confirm`] for calling expectations.
319          * 
320          * [`block_connected`]: Self::block_connected
321          */
322         public TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[] best_block_updated(byte[] header, int height, org.ldk.structs.BroadcasterInterface broadcaster, org.ldk.structs.FeeEstimator fee_estimator, org.ldk.structs.Logger logger) {
323                 long[] ret = bindings.ChannelMonitor_best_block_updated(this.ptr, InternalUtils.check_arr_len(header, 80), height, broadcaster.ptr, fee_estimator.ptr, logger.ptr);
324                 GC.KeepAlive(this);
325                 GC.KeepAlive(header);
326                 GC.KeepAlive(height);
327                 GC.KeepAlive(broadcaster);
328                 GC.KeepAlive(fee_estimator);
329                 GC.KeepAlive(logger);
330                 int ret_conv_39_len = ret.Length;
331                 TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[] ret_conv_39_arr = new TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[ret_conv_39_len];
332                 for (int n = 0; n < ret_conv_39_len; n++) {
333                         long ret_conv_39 = ret[n];
334                         TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ ret_conv_39_hu_conv = new TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ(null, ret_conv_39);
335                         if (ret_conv_39_hu_conv != null) { ret_conv_39_hu_conv.ptrs_to.AddLast(this); };
336                         ret_conv_39_arr[n] = ret_conv_39_hu_conv;
337                 }
338                 if (this != null) { this.ptrs_to.AddLast(broadcaster); };
339                 if (this != null) { this.ptrs_to.AddLast(fee_estimator); };
340                 if (this != null) { this.ptrs_to.AddLast(logger); };
341                 return ret_conv_39_arr;
342         }
343
344         /**
345          * Returns the set of txids that should be monitored for re-organization out of the chain.
346          */
347         public TwoTuple_TxidCOption_BlockHashZZ[] get_relevant_txids() {
348                 long[] ret = bindings.ChannelMonitor_get_relevant_txids(this.ptr);
349                 GC.KeepAlive(this);
350                 int ret_conv_34_len = ret.Length;
351                 TwoTuple_TxidCOption_BlockHashZZ[] ret_conv_34_arr = new TwoTuple_TxidCOption_BlockHashZZ[ret_conv_34_len];
352                 for (int i = 0; i < ret_conv_34_len; i++) {
353                         long ret_conv_34 = ret[i];
354                         TwoTuple_TxidCOption_BlockHashZZ ret_conv_34_hu_conv = new TwoTuple_TxidCOption_BlockHashZZ(null, ret_conv_34);
355                         if (ret_conv_34_hu_conv != null) { ret_conv_34_hu_conv.ptrs_to.AddLast(this); };
356                         ret_conv_34_arr[i] = ret_conv_34_hu_conv;
357                 }
358                 return ret_conv_34_arr;
359         }
360
361         /**
362          * Gets the latest best block which was connected either via the [`chain::Listen`] or
363          * [`chain::Confirm`] interfaces.
364          */
365         public BestBlock current_best_block() {
366                 long ret = bindings.ChannelMonitor_current_best_block(this.ptr);
367                 GC.KeepAlive(this);
368                 if (ret >= 0 && ret <= 4096) { return null; }
369                 org.ldk.structs.BestBlock ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.BestBlock(null, ret); }
370                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };
371                 return ret_hu_conv;
372         }
373
374         /**
375          * Triggers rebroadcasts/fee-bumps of pending claims from a force-closed channel. This is
376          * crucial in preventing certain classes of pinning attacks, detecting substantial mempool
377          * feerate changes between blocks, and ensuring reliability if broadcasting fails. We recommend
378          * invoking this every 30 seconds, or lower if running in an environment with spotty
379          * connections, like on mobile.
380          */
381         public void rebroadcast_pending_claims(org.ldk.structs.BroadcasterInterface broadcaster, org.ldk.structs.FeeEstimator fee_estimator, org.ldk.structs.Logger logger) {
382                 bindings.ChannelMonitor_rebroadcast_pending_claims(this.ptr, broadcaster.ptr, fee_estimator.ptr, logger.ptr);
383                 GC.KeepAlive(this);
384                 GC.KeepAlive(broadcaster);
385                 GC.KeepAlive(fee_estimator);
386                 GC.KeepAlive(logger);
387                 if (this != null) { this.ptrs_to.AddLast(broadcaster); };
388                 if (this != null) { this.ptrs_to.AddLast(fee_estimator); };
389                 if (this != null) { this.ptrs_to.AddLast(logger); };
390         }
391
392         /**
393          * Gets the balances in this channel which are either claimable by us if we were to
394          * force-close the channel now or which are claimable on-chain (possibly awaiting
395          * confirmation).
396          * 
397          * Any balances in the channel which are available on-chain (excluding on-chain fees) are
398          * included here until an [`Event::SpendableOutputs`] event has been generated for the
399          * balance, or until our counterparty has claimed the balance and accrued several
400          * confirmations on the claim transaction.
401          * 
402          * Note that for `ChannelMonitors` which track a channel which went on-chain with versions of
403          * LDK prior to 0.0.111, balances may not be fully captured if our counterparty broadcasted
404          * a revoked state.
405          * 
406          * See [`Balance`] for additional details on the types of claimable balances which
407          * may be returned here and their meanings.
408          */
409         public Balance[] get_claimable_balances() {
410                 long[] ret = bindings.ChannelMonitor_get_claimable_balances(this.ptr);
411                 GC.KeepAlive(this);
412                 int ret_conv_9_len = ret.Length;
413                 Balance[] ret_conv_9_arr = new Balance[ret_conv_9_len];
414                 for (int j = 0; j < ret_conv_9_len; j++) {
415                         long ret_conv_9 = ret[j];
416                         org.ldk.structs.Balance ret_conv_9_hu_conv = org.ldk.structs.Balance.constr_from_ptr(ret_conv_9);
417                         if (ret_conv_9_hu_conv != null) { ret_conv_9_hu_conv.ptrs_to.AddLast(this); };
418                         ret_conv_9_arr[j] = ret_conv_9_hu_conv;
419                 }
420                 return ret_conv_9_arr;
421         }
422
423 }
424 } } }