7ed5112e2e273be80fcc8266a27f48d7ff377912
[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  * 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 public class ChannelMonitor : CommonBase {
28         internal ChannelMonitor(object _dummy, long ptr) : base(ptr) { }
29         ~ChannelMonitor() {
30                 if (ptr != 0) { bindings.ChannelMonitor_free(ptr); }
31         }
32
33         internal long clone_ptr() {
34                 long ret = bindings.ChannelMonitor_clone_ptr(this.ptr);
35                 GC.KeepAlive(this);
36                 return ret;
37         }
38
39         /**
40          * Creates a copy of the ChannelMonitor
41          */
42         public ChannelMonitor clone() {
43                 long ret = bindings.ChannelMonitor_clone(this.ptr);
44                 GC.KeepAlive(this);
45                 if (ret >= 0 && ret <= 4096) { return null; }
46                 org.ldk.structs.ChannelMonitor ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.ChannelMonitor(null, ret); }
47                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };
48                 return ret_hu_conv;
49         }
50
51         /**
52          * Serialize the ChannelMonitor object into a byte array which can be read by ChannelMonitor_read
53          */
54         public byte[] write() {
55                 byte[] ret = bindings.ChannelMonitor_write(this.ptr);
56                 GC.KeepAlive(this);
57                 return ret;
58         }
59
60         /**
61          * Updates a ChannelMonitor on the basis of some new information provided by the Channel
62          * itself.
63          * 
64          * panics if the given update is not the next update by update_id.
65          */
66         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) {
67                 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);
68                 GC.KeepAlive(this);
69                 GC.KeepAlive(updates);
70                 GC.KeepAlive(broadcaster);
71                 GC.KeepAlive(fee_estimator);
72                 GC.KeepAlive(logger);
73                 if (ret >= 0 && ret <= 4096) { return null; }
74                 Result_NoneNoneZ ret_hu_conv = Result_NoneNoneZ.constr_from_ptr(ret);
75                 if (this != null) { this.ptrs_to.AddLast(updates); };
76                 if (this != null) { this.ptrs_to.AddLast(broadcaster); };
77                 if (this != null) { this.ptrs_to.AddLast(fee_estimator); };
78                 if (this != null) { this.ptrs_to.AddLast(logger); };
79                 return ret_hu_conv;
80         }
81
82         /**
83          * Gets the update_id from the latest ChannelMonitorUpdate which was applied to this
84          * ChannelMonitor.
85          */
86         public long get_latest_update_id() {
87                 long ret = bindings.ChannelMonitor_get_latest_update_id(this.ptr);
88                 GC.KeepAlive(this);
89                 return ret;
90         }
91
92         /**
93          * Gets the funding transaction outpoint of the channel this ChannelMonitor is monitoring for.
94          */
95         public TwoTuple_OutPointScriptZ get_funding_txo() {
96                 long ret = bindings.ChannelMonitor_get_funding_txo(this.ptr);
97                 GC.KeepAlive(this);
98                 if (ret >= 0 && ret <= 4096) { return null; }
99                 TwoTuple_OutPointScriptZ ret_hu_conv = new TwoTuple_OutPointScriptZ(null, ret);
100                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };
101                 return ret_hu_conv;
102         }
103
104         /**
105          * Gets a list of txids, with their output scripts (in the order they appear in the
106          * transaction), which we must learn about spends of via block_connected().
107          */
108         public TwoTuple_TxidCVec_C2Tuple_u32ScriptZZZ[] get_outputs_to_watch() {
109                 long[] ret = bindings.ChannelMonitor_get_outputs_to_watch(this.ptr);
110                 GC.KeepAlive(this);
111                 int ret_conv_40_len = ret.Length;
112                 TwoTuple_TxidCVec_C2Tuple_u32ScriptZZZ[] ret_conv_40_arr = new TwoTuple_TxidCVec_C2Tuple_u32ScriptZZZ[ret_conv_40_len];
113                 for (int o = 0; o < ret_conv_40_len; o++) {
114                         long ret_conv_40 = ret[o];
115                         TwoTuple_TxidCVec_C2Tuple_u32ScriptZZZ ret_conv_40_hu_conv = new TwoTuple_TxidCVec_C2Tuple_u32ScriptZZZ(null, ret_conv_40);
116                         if (ret_conv_40_hu_conv != null) { ret_conv_40_hu_conv.ptrs_to.AddLast(this); };
117                         ret_conv_40_arr[o] = ret_conv_40_hu_conv;
118                 }
119                 return ret_conv_40_arr;
120         }
121
122         /**
123          * Loads the funding txo and outputs to watch into the given `chain::Filter` by repeatedly
124          * calling `chain::Filter::register_output` and `chain::Filter::register_tx` until all outputs
125          * have been registered.
126          */
127         public void load_outputs_to_watch(org.ldk.structs.Filter filter) {
128                 bindings.ChannelMonitor_load_outputs_to_watch(this.ptr, filter == null ? 0 : filter.ptr);
129                 GC.KeepAlive(this);
130                 GC.KeepAlive(filter);
131                 if (this != null) { this.ptrs_to.AddLast(filter); };
132         }
133
134         /**
135          * Get the list of HTLCs who's status has been updated on chain. This should be called by
136          * ChannelManager via [`chain::Watch::release_pending_monitor_events`].
137          */
138         public MonitorEvent[] get_and_clear_pending_monitor_events() {
139                 long[] ret = bindings.ChannelMonitor_get_and_clear_pending_monitor_events(this.ptr);
140                 GC.KeepAlive(this);
141                 int ret_conv_14_len = ret.Length;
142                 MonitorEvent[] ret_conv_14_arr = new MonitorEvent[ret_conv_14_len];
143                 for (int o = 0; o < ret_conv_14_len; o++) {
144                         long ret_conv_14 = ret[o];
145                         org.ldk.structs.MonitorEvent ret_conv_14_hu_conv = org.ldk.structs.MonitorEvent.constr_from_ptr(ret_conv_14);
146                         if (ret_conv_14_hu_conv != null) { ret_conv_14_hu_conv.ptrs_to.AddLast(this); };
147                         ret_conv_14_arr[o] = ret_conv_14_hu_conv;
148                 }
149                 return ret_conv_14_arr;
150         }
151
152         /**
153          * Gets the list of pending events which were generated by previous actions, clearing the list
154          * in the process.
155          * 
156          * This is called by the [`EventsProvider::process_pending_events`] implementation for
157          * [`ChainMonitor`].
158          * 
159          * [`EventsProvider::process_pending_events`]: crate::util::events::EventsProvider::process_pending_events
160          * [`ChainMonitor`]: crate::chain::chainmonitor::ChainMonitor
161          */
162         public Event[] get_and_clear_pending_events() {
163                 long[] ret = bindings.ChannelMonitor_get_and_clear_pending_events(this.ptr);
164                 GC.KeepAlive(this);
165                 int ret_conv_7_len = ret.Length;
166                 Event[] ret_conv_7_arr = new Event[ret_conv_7_len];
167                 for (int h = 0; h < ret_conv_7_len; h++) {
168                         long ret_conv_7 = ret[h];
169                         org.ldk.structs.Event ret_conv_7_hu_conv = org.ldk.structs.Event.constr_from_ptr(ret_conv_7);
170                         if (ret_conv_7_hu_conv != null) { ret_conv_7_hu_conv.ptrs_to.AddLast(this); };
171                         ret_conv_7_arr[h] = ret_conv_7_hu_conv;
172                 }
173                 return ret_conv_7_arr;
174         }
175
176         /**
177          * Gets the `node_id` of the counterparty for this channel.
178          * 
179          * Will be `None` for channels constructed on LDK versions prior to 0.0.110 and always `Some`
180          * otherwise.
181          * 
182          * Note that the return value (or a relevant inner pointer) may be NULL or all-0s to represent None
183          */
184         public byte[] get_counterparty_node_id() {
185                 byte[] ret = bindings.ChannelMonitor_get_counterparty_node_id(this.ptr);
186                 GC.KeepAlive(this);
187                 return ret;
188         }
189
190         /**
191          * Used by ChannelManager deserialization to broadcast the latest holder state if its copy of
192          * the Channel was out-of-date.
193          * 
194          * You may also use this to broadcast the latest local commitment transaction, either because
195          * a monitor update failed with [`ChannelMonitorUpdateStatus::PermanentFailure`] or because we've
196          * fallen behind (i.e. we've received proof that our counterparty side knows a revocation
197          * secret we gave them that they shouldn't know).
198          * 
199          * Broadcasting these transactions in the second case is UNSAFE, as they allow counterparty
200          * side to punish you. Nevertheless you may want to broadcast them if counterparty doesn't
201          * close channel with their commitment transaction after a substantial amount of time. Best
202          * may be to contact the other node operator out-of-band to coordinate other options available
203          * to you. In any-case, the choice is up to you.
204          * 
205          * [`ChannelMonitorUpdateStatus::PermanentFailure`]: super::ChannelMonitorUpdateStatus::PermanentFailure
206          */
207         public byte[][] get_latest_holder_commitment_txn(org.ldk.structs.Logger logger) {
208                 byte[][] ret = bindings.ChannelMonitor_get_latest_holder_commitment_txn(this.ptr, logger == null ? 0 : logger.ptr);
209                 GC.KeepAlive(this);
210                 GC.KeepAlive(logger);
211                 if (this != null) { this.ptrs_to.AddLast(logger); };
212                 return ret;
213         }
214
215         /**
216          * Processes transactions in a newly connected block, which may result in any of the following:
217          * - update the monitor's state against resolved HTLCs
218          * - punish the counterparty in the case of seeing a revoked commitment transaction
219          * - force close the channel and claim/timeout incoming/outgoing HTLCs if near expiration
220          * - detect settled outputs for later spending
221          * - schedule and bump any in-flight claims
222          * 
223          * Returns any new outputs to watch from `txdata`; after called, these are also included in
224          * [`get_outputs_to_watch`].
225          * 
226          * [`get_outputs_to_watch`]: #method.get_outputs_to_watch
227          */
228         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) {
229                 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 == null ? 0 : broadcaster.ptr, fee_estimator == null ? 0 : fee_estimator.ptr, logger == null ? 0 : logger.ptr);
230                 GC.KeepAlive(this);
231                 GC.KeepAlive(header);
232                 GC.KeepAlive(txdata);
233                 GC.KeepAlive(height);
234                 GC.KeepAlive(broadcaster);
235                 GC.KeepAlive(fee_estimator);
236                 GC.KeepAlive(logger);
237                 int ret_conv_39_len = ret.Length;
238                 TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[] ret_conv_39_arr = new TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[ret_conv_39_len];
239                 for (int n = 0; n < ret_conv_39_len; n++) {
240                         long ret_conv_39 = ret[n];
241                         TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ ret_conv_39_hu_conv = new TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ(null, ret_conv_39);
242                         if (ret_conv_39_hu_conv != null) { ret_conv_39_hu_conv.ptrs_to.AddLast(this); };
243                         ret_conv_39_arr[n] = ret_conv_39_hu_conv;
244                 }
245                 if (this != null) { this.ptrs_to.AddLast(broadcaster); };
246                 if (this != null) { this.ptrs_to.AddLast(fee_estimator); };
247                 if (this != null) { this.ptrs_to.AddLast(logger); };
248                 return ret_conv_39_arr;
249         }
250
251         /**
252          * Determines if the disconnected block contained any transactions of interest and updates
253          * appropriately.
254          */
255         public void block_disconnected(byte[] header, int height, org.ldk.structs.BroadcasterInterface broadcaster, org.ldk.structs.FeeEstimator fee_estimator, org.ldk.structs.Logger logger) {
256                 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);
257                 GC.KeepAlive(this);
258                 GC.KeepAlive(header);
259                 GC.KeepAlive(height);
260                 GC.KeepAlive(broadcaster);
261                 GC.KeepAlive(fee_estimator);
262                 GC.KeepAlive(logger);
263                 if (this != null) { this.ptrs_to.AddLast(broadcaster); };
264                 if (this != null) { this.ptrs_to.AddLast(fee_estimator); };
265                 if (this != null) { this.ptrs_to.AddLast(logger); };
266         }
267
268         /**
269          * Processes transactions confirmed in a block with the given header and height, returning new
270          * outputs to watch. See [`block_connected`] for details.
271          * 
272          * Used instead of [`block_connected`] by clients that are notified of transactions rather than
273          * blocks. See [`chain::Confirm`] for calling expectations.
274          * 
275          * [`block_connected`]: Self::block_connected
276          */
277         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) {
278                 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 == null ? 0 : broadcaster.ptr, fee_estimator == null ? 0 : fee_estimator.ptr, logger == null ? 0 : logger.ptr);
279                 GC.KeepAlive(this);
280                 GC.KeepAlive(header);
281                 GC.KeepAlive(txdata);
282                 GC.KeepAlive(height);
283                 GC.KeepAlive(broadcaster);
284                 GC.KeepAlive(fee_estimator);
285                 GC.KeepAlive(logger);
286                 int ret_conv_39_len = ret.Length;
287                 TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[] ret_conv_39_arr = new TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[ret_conv_39_len];
288                 for (int n = 0; n < ret_conv_39_len; n++) {
289                         long ret_conv_39 = ret[n];
290                         TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ ret_conv_39_hu_conv = new TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ(null, ret_conv_39);
291                         if (ret_conv_39_hu_conv != null) { ret_conv_39_hu_conv.ptrs_to.AddLast(this); };
292                         ret_conv_39_arr[n] = ret_conv_39_hu_conv;
293                 }
294                 if (this != null) { this.ptrs_to.AddLast(broadcaster); };
295                 if (this != null) { this.ptrs_to.AddLast(fee_estimator); };
296                 if (this != null) { this.ptrs_to.AddLast(logger); };
297                 return ret_conv_39_arr;
298         }
299
300         /**
301          * Processes a transaction that was reorganized out of the chain.
302          * 
303          * Used instead of [`block_disconnected`] by clients that are notified of transactions rather
304          * than blocks. See [`chain::Confirm`] for calling expectations.
305          * 
306          * [`block_disconnected`]: Self::block_disconnected
307          */
308         public void transaction_unconfirmed(byte[] txid, org.ldk.structs.BroadcasterInterface broadcaster, org.ldk.structs.FeeEstimator fee_estimator, org.ldk.structs.Logger logger) {
309                 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);
310                 GC.KeepAlive(this);
311                 GC.KeepAlive(txid);
312                 GC.KeepAlive(broadcaster);
313                 GC.KeepAlive(fee_estimator);
314                 GC.KeepAlive(logger);
315                 if (this != null) { this.ptrs_to.AddLast(broadcaster); };
316                 if (this != null) { this.ptrs_to.AddLast(fee_estimator); };
317                 if (this != null) { this.ptrs_to.AddLast(logger); };
318         }
319
320         /**
321          * Updates the monitor with the current best chain tip, returning new outputs to watch. See
322          * [`block_connected`] for details.
323          * 
324          * Used instead of [`block_connected`] by clients that are notified of transactions rather than
325          * blocks. See [`chain::Confirm`] for calling expectations.
326          * 
327          * [`block_connected`]: Self::block_connected
328          */
329         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) {
330                 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);
331                 GC.KeepAlive(this);
332                 GC.KeepAlive(header);
333                 GC.KeepAlive(height);
334                 GC.KeepAlive(broadcaster);
335                 GC.KeepAlive(fee_estimator);
336                 GC.KeepAlive(logger);
337                 int ret_conv_39_len = ret.Length;
338                 TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[] ret_conv_39_arr = new TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[ret_conv_39_len];
339                 for (int n = 0; n < ret_conv_39_len; n++) {
340                         long ret_conv_39 = ret[n];
341                         TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ ret_conv_39_hu_conv = new TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ(null, ret_conv_39);
342                         if (ret_conv_39_hu_conv != null) { ret_conv_39_hu_conv.ptrs_to.AddLast(this); };
343                         ret_conv_39_arr[n] = ret_conv_39_hu_conv;
344                 }
345                 if (this != null) { this.ptrs_to.AddLast(broadcaster); };
346                 if (this != null) { this.ptrs_to.AddLast(fee_estimator); };
347                 if (this != null) { this.ptrs_to.AddLast(logger); };
348                 return ret_conv_39_arr;
349         }
350
351         /**
352          * Returns the set of txids that should be monitored for re-organization out of the chain.
353          */
354         public TwoTuple_TxidBlockHashZ[] get_relevant_txids() {
355                 long[] ret = bindings.ChannelMonitor_get_relevant_txids(this.ptr);
356                 GC.KeepAlive(this);
357                 int ret_conv_25_len = ret.Length;
358                 TwoTuple_TxidBlockHashZ[] ret_conv_25_arr = new TwoTuple_TxidBlockHashZ[ret_conv_25_len];
359                 for (int z = 0; z < ret_conv_25_len; z++) {
360                         long ret_conv_25 = ret[z];
361                         TwoTuple_TxidBlockHashZ ret_conv_25_hu_conv = new TwoTuple_TxidBlockHashZ(null, ret_conv_25);
362                         if (ret_conv_25_hu_conv != null) { ret_conv_25_hu_conv.ptrs_to.AddLast(this); };
363                         ret_conv_25_arr[z] = ret_conv_25_hu_conv;
364                 }
365                 return ret_conv_25_arr;
366         }
367
368         /**
369          * Gets the latest best block which was connected either via the [`chain::Listen`] or
370          * [`chain::Confirm`] interfaces.
371          */
372         public BestBlock current_best_block() {
373                 long ret = bindings.ChannelMonitor_current_best_block(this.ptr);
374                 GC.KeepAlive(this);
375                 if (ret >= 0 && ret <= 4096) { return null; }
376                 org.ldk.structs.BestBlock ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.BestBlock(null, ret); }
377                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };
378                 return ret_hu_conv;
379         }
380
381         /**
382          * Gets the balances in this channel which are either claimable by us if we were to
383          * force-close the channel now or which are claimable on-chain (possibly awaiting
384          * confirmation).
385          * 
386          * Any balances in the channel which are available on-chain (excluding on-chain fees) are
387          * included here until an [`Event::SpendableOutputs`] event has been generated for the
388          * balance, or until our counterparty has claimed the balance and accrued several
389          * confirmations on the claim transaction.
390          * 
391          * Note that for `ChannelMonitors` which track a channel which went on-chain with versions of
392          * LDK prior to 0.0.111, balances may not be fully captured if our counterparty broadcasted
393          * a revoked state.
394          * 
395          * See [`Balance`] for additional details on the types of claimable balances which
396          * may be returned here and their meanings.
397          */
398         public Balance[] get_claimable_balances() {
399                 long[] ret = bindings.ChannelMonitor_get_claimable_balances(this.ptr);
400                 GC.KeepAlive(this);
401                 int ret_conv_9_len = ret.Length;
402                 Balance[] ret_conv_9_arr = new Balance[ret_conv_9_len];
403                 for (int j = 0; j < ret_conv_9_len; j++) {
404                         long ret_conv_9 = ret[j];
405                         org.ldk.structs.Balance ret_conv_9_hu_conv = org.ldk.structs.Balance.constr_from_ptr(ret_conv_9);
406                         if (ret_conv_9_hu_conv != null) { ret_conv_9_hu_conv.ptrs_to.AddLast(this); };
407                         ret_conv_9_arr[j] = ret_conv_9_hu_conv;
408                 }
409                 return ret_conv_9_arr;
410         }
411
412 }
413 } } }