1 package org.ldk.structs;
3 import org.ldk.impl.bindings;
4 import org.ldk.enums.*;
6 import java.util.Arrays;
7 import java.lang.ref.Reference;
8 import javax.annotation.Nullable;
12 * A ChannelMonitor handles chain events (blocks connected and disconnected) and generates
13 * on-chain transactions to ensure no loss of funds occurs.
15 * You MUST ensure that no ChannelMonitors for a given channel anywhere contain out-of-date
16 * information and are actively monitoring the chain.
18 * Note that the deserializer is only implemented for (BlockHash, ChannelMonitor), which
19 * tells you the last block hash which was block_connect()ed. You MUST rescan any blocks along
20 * the \"reorg path\" (ie disconnecting blocks until you find a common ancestor from both the
21 * returned block hash and the the current chain and then reconnecting blocks to get to the
22 * best chain) upon deserializing the object!
24 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
25 public class ChannelMonitor extends CommonBase {
26 ChannelMonitor(Object _dummy, long ptr) { super(ptr); }
27 @Override @SuppressWarnings("deprecation")
28 protected void finalize() throws Throwable {
30 if (ptr != 0) { bindings.ChannelMonitor_free(ptr); }
34 long ret = bindings.ChannelMonitor_clone_ptr(this.ptr);
35 Reference.reachabilityFence(this);
40 * Creates a copy of the ChannelMonitor
42 public ChannelMonitor clone() {
43 long ret = bindings.ChannelMonitor_clone(this.ptr);
44 Reference.reachabilityFence(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.add(this); };
52 * Serialize the ChannelMonitor object into a byte array which can be read by ChannelMonitor_read
54 public byte[] write() {
55 byte[] ret = bindings.ChannelMonitor_write(this.ptr);
56 Reference.reachabilityFence(this);
61 * Updates a ChannelMonitor on the basis of some new information provided by the Channel
64 * panics if the given update is not the next update by update_id.
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.ptr, fee_estimator.ptr, logger.ptr);
68 Reference.reachabilityFence(this);
69 Reference.reachabilityFence(updates);
70 Reference.reachabilityFence(broadcaster);
71 Reference.reachabilityFence(fee_estimator);
72 Reference.reachabilityFence(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.add(updates); };
76 if (this != null) { this.ptrs_to.add(broadcaster); };
77 if (this != null) { this.ptrs_to.add(fee_estimator); };
78 if (this != null) { this.ptrs_to.add(logger); };
83 * Gets the update_id from the latest ChannelMonitorUpdate which was applied to this
86 public long get_latest_update_id() {
87 long ret = bindings.ChannelMonitor_get_latest_update_id(this.ptr);
88 Reference.reachabilityFence(this);
93 * Gets the funding transaction outpoint of the channel this ChannelMonitor is monitoring for.
95 public TwoTuple_OutPointScriptZ get_funding_txo() {
96 long ret = bindings.ChannelMonitor_get_funding_txo(this.ptr);
97 Reference.reachabilityFence(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.add(this); };
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().
108 public TwoTuple_TxidCVec_C2Tuple_u32ScriptZZZ[] get_outputs_to_watch() {
109 long[] ret = bindings.ChannelMonitor_get_outputs_to_watch(this.ptr);
110 Reference.reachabilityFence(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.add(this); };
117 ret_conv_40_arr[o] = ret_conv_40_hu_conv;
119 return ret_conv_40_arr;
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.
127 public void load_outputs_to_watch(org.ldk.structs.Filter filter) {
128 bindings.ChannelMonitor_load_outputs_to_watch(this.ptr, filter.ptr);
129 Reference.reachabilityFence(this);
130 Reference.reachabilityFence(filter);
131 if (this != null) { this.ptrs_to.add(filter); };
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`].
138 public MonitorEvent[] get_and_clear_pending_monitor_events() {
139 long[] ret = bindings.ChannelMonitor_get_and_clear_pending_monitor_events(this.ptr);
140 Reference.reachabilityFence(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.add(this); };
147 ret_conv_14_arr[o] = ret_conv_14_hu_conv;
149 return ret_conv_14_arr;
153 * Processes [`SpendableOutputs`] events produced from each [`ChannelMonitor`] upon maturity.
155 * For channels featuring anchor outputs, this method will also process [`BumpTransaction`]
156 * events produced from each [`ChannelMonitor`] while there is a balance to claim onchain
157 * within each channel. As the confirmation of a commitment transaction may be critical to the
158 * safety of funds, we recommend invoking this every 30 seconds, or lower if running in an
159 * environment with spotty connections, like on mobile.
161 * An [`EventHandler`] may safely call back to the provider, though this shouldn't be needed in
162 * order to handle these events.
164 * [`SpendableOutputs`]: crate::events::Event::SpendableOutputs
165 * [`BumpTransaction`]: crate::events::Event::BumpTransaction
167 public void process_pending_events(org.ldk.structs.EventHandler handler) {
168 bindings.ChannelMonitor_process_pending_events(this.ptr, handler.ptr);
169 Reference.reachabilityFence(this);
170 Reference.reachabilityFence(handler);
171 if (this != null) { this.ptrs_to.add(handler); };
175 * Gets the `node_id` of the counterparty for this channel.
177 * Will be `None` for channels constructed on LDK versions prior to 0.0.110 and always `Some`
180 * Note that the return value (or a relevant inner pointer) may be NULL or all-0s to represent None
183 public byte[] get_counterparty_node_id() {
184 byte[] ret = bindings.ChannelMonitor_get_counterparty_node_id(this.ptr);
185 Reference.reachabilityFence(this);
190 * Used by ChannelManager deserialization to broadcast the latest holder state if its copy of
191 * the Channel was out-of-date.
193 * You may also use this to broadcast the latest local commitment transaction, either because
194 * a monitor update failed with [`ChannelMonitorUpdateStatus::PermanentFailure`] or because we've
195 * fallen behind (i.e. we've received proof that our counterparty side knows a revocation
196 * secret we gave them that they shouldn't know).
198 * Broadcasting these transactions in the second case is UNSAFE, as they allow counterparty
199 * side to punish you. Nevertheless you may want to broadcast them if counterparty doesn't
200 * close channel with their commitment transaction after a substantial amount of time. Best
201 * may be to contact the other node operator out-of-band to coordinate other options available
202 * to you. In any-case, the choice is up to you.
204 * [`ChannelMonitorUpdateStatus::PermanentFailure`]: super::ChannelMonitorUpdateStatus::PermanentFailure
206 public byte[][] get_latest_holder_commitment_txn(org.ldk.structs.Logger logger) {
207 byte[][] ret = bindings.ChannelMonitor_get_latest_holder_commitment_txn(this.ptr, logger.ptr);
208 Reference.reachabilityFence(this);
209 Reference.reachabilityFence(logger);
210 if (this != null) { this.ptrs_to.add(logger); };
215 * Processes transactions in a newly connected block, which may result in any of the following:
216 * - update the monitor's state against resolved HTLCs
217 * - punish the counterparty in the case of seeing a revoked commitment transaction
218 * - force close the channel and claim/timeout incoming/outgoing HTLCs if near expiration
219 * - detect settled outputs for later spending
220 * - schedule and bump any in-flight claims
222 * Returns any new outputs to watch from `txdata`; after called, these are also included in
223 * [`get_outputs_to_watch`].
225 * [`get_outputs_to_watch`]: #method.get_outputs_to_watch
227 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) {
228 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.ptr, fee_estimator.ptr, logger.ptr);
229 Reference.reachabilityFence(this);
230 Reference.reachabilityFence(header);
231 Reference.reachabilityFence(txdata);
232 Reference.reachabilityFence(height);
233 Reference.reachabilityFence(broadcaster);
234 Reference.reachabilityFence(fee_estimator);
235 Reference.reachabilityFence(logger);
236 int ret_conv_39_len = ret.length;
237 TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[] ret_conv_39_arr = new TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[ret_conv_39_len];
238 for (int n = 0; n < ret_conv_39_len; n++) {
239 long ret_conv_39 = ret[n];
240 TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ ret_conv_39_hu_conv = new TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ(null, ret_conv_39);
241 if (ret_conv_39_hu_conv != null) { ret_conv_39_hu_conv.ptrs_to.add(this); };
242 ret_conv_39_arr[n] = ret_conv_39_hu_conv;
244 if (this != null) { this.ptrs_to.add(broadcaster); };
245 if (this != null) { this.ptrs_to.add(fee_estimator); };
246 if (this != null) { this.ptrs_to.add(logger); };
247 return ret_conv_39_arr;
251 * Determines if the disconnected block contained any transactions of interest and updates
254 public void block_disconnected(byte[] header, int height, org.ldk.structs.BroadcasterInterface broadcaster, org.ldk.structs.FeeEstimator fee_estimator, org.ldk.structs.Logger logger) {
255 bindings.ChannelMonitor_block_disconnected(this.ptr, InternalUtils.check_arr_len(header, 80), height, broadcaster.ptr, fee_estimator.ptr, logger.ptr);
256 Reference.reachabilityFence(this);
257 Reference.reachabilityFence(header);
258 Reference.reachabilityFence(height);
259 Reference.reachabilityFence(broadcaster);
260 Reference.reachabilityFence(fee_estimator);
261 Reference.reachabilityFence(logger);
262 if (this != null) { this.ptrs_to.add(broadcaster); };
263 if (this != null) { this.ptrs_to.add(fee_estimator); };
264 if (this != null) { this.ptrs_to.add(logger); };
268 * Processes transactions confirmed in a block with the given header and height, returning new
269 * outputs to watch. See [`block_connected`] for details.
271 * Used instead of [`block_connected`] by clients that are notified of transactions rather than
272 * blocks. See [`chain::Confirm`] for calling expectations.
274 * [`block_connected`]: Self::block_connected
276 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) {
277 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.ptr, fee_estimator.ptr, logger.ptr);
278 Reference.reachabilityFence(this);
279 Reference.reachabilityFence(header);
280 Reference.reachabilityFence(txdata);
281 Reference.reachabilityFence(height);
282 Reference.reachabilityFence(broadcaster);
283 Reference.reachabilityFence(fee_estimator);
284 Reference.reachabilityFence(logger);
285 int ret_conv_39_len = ret.length;
286 TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[] ret_conv_39_arr = new TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[ret_conv_39_len];
287 for (int n = 0; n < ret_conv_39_len; n++) {
288 long ret_conv_39 = ret[n];
289 TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ ret_conv_39_hu_conv = new TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ(null, ret_conv_39);
290 if (ret_conv_39_hu_conv != null) { ret_conv_39_hu_conv.ptrs_to.add(this); };
291 ret_conv_39_arr[n] = ret_conv_39_hu_conv;
293 if (this != null) { this.ptrs_to.add(broadcaster); };
294 if (this != null) { this.ptrs_to.add(fee_estimator); };
295 if (this != null) { this.ptrs_to.add(logger); };
296 return ret_conv_39_arr;
300 * Processes a transaction that was reorganized out of the chain.
302 * Used instead of [`block_disconnected`] by clients that are notified of transactions rather
303 * than blocks. See [`chain::Confirm`] for calling expectations.
305 * [`block_disconnected`]: Self::block_disconnected
307 public void transaction_unconfirmed(byte[] txid, org.ldk.structs.BroadcasterInterface broadcaster, org.ldk.structs.FeeEstimator fee_estimator, org.ldk.structs.Logger logger) {
308 bindings.ChannelMonitor_transaction_unconfirmed(this.ptr, InternalUtils.check_arr_len(txid, 32), broadcaster.ptr, fee_estimator.ptr, logger.ptr);
309 Reference.reachabilityFence(this);
310 Reference.reachabilityFence(txid);
311 Reference.reachabilityFence(broadcaster);
312 Reference.reachabilityFence(fee_estimator);
313 Reference.reachabilityFence(logger);
314 if (this != null) { this.ptrs_to.add(broadcaster); };
315 if (this != null) { this.ptrs_to.add(fee_estimator); };
316 if (this != null) { this.ptrs_to.add(logger); };
320 * Updates the monitor with the current best chain tip, returning new outputs to watch. See
321 * [`block_connected`] for details.
323 * Used instead of [`block_connected`] by clients that are notified of transactions rather than
324 * blocks. See [`chain::Confirm`] for calling expectations.
326 * [`block_connected`]: Self::block_connected
328 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) {
329 long[] ret = bindings.ChannelMonitor_best_block_updated(this.ptr, InternalUtils.check_arr_len(header, 80), height, broadcaster.ptr, fee_estimator.ptr, logger.ptr);
330 Reference.reachabilityFence(this);
331 Reference.reachabilityFence(header);
332 Reference.reachabilityFence(height);
333 Reference.reachabilityFence(broadcaster);
334 Reference.reachabilityFence(fee_estimator);
335 Reference.reachabilityFence(logger);
336 int ret_conv_39_len = ret.length;
337 TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[] ret_conv_39_arr = new TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[ret_conv_39_len];
338 for (int n = 0; n < ret_conv_39_len; n++) {
339 long ret_conv_39 = ret[n];
340 TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ ret_conv_39_hu_conv = new TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ(null, ret_conv_39);
341 if (ret_conv_39_hu_conv != null) { ret_conv_39_hu_conv.ptrs_to.add(this); };
342 ret_conv_39_arr[n] = ret_conv_39_hu_conv;
344 if (this != null) { this.ptrs_to.add(broadcaster); };
345 if (this != null) { this.ptrs_to.add(fee_estimator); };
346 if (this != null) { this.ptrs_to.add(logger); };
347 return ret_conv_39_arr;
351 * Returns the set of txids that should be monitored for re-organization out of the chain.
353 public TwoTuple_TxidCOption_BlockHashZZ[] get_relevant_txids() {
354 long[] ret = bindings.ChannelMonitor_get_relevant_txids(this.ptr);
355 Reference.reachabilityFence(this);
356 int ret_conv_34_len = ret.length;
357 TwoTuple_TxidCOption_BlockHashZZ[] ret_conv_34_arr = new TwoTuple_TxidCOption_BlockHashZZ[ret_conv_34_len];
358 for (int i = 0; i < ret_conv_34_len; i++) {
359 long ret_conv_34 = ret[i];
360 TwoTuple_TxidCOption_BlockHashZZ ret_conv_34_hu_conv = new TwoTuple_TxidCOption_BlockHashZZ(null, ret_conv_34);
361 if (ret_conv_34_hu_conv != null) { ret_conv_34_hu_conv.ptrs_to.add(this); };
362 ret_conv_34_arr[i] = ret_conv_34_hu_conv;
364 return ret_conv_34_arr;
368 * Gets the latest best block which was connected either via the [`chain::Listen`] or
369 * [`chain::Confirm`] interfaces.
371 public BestBlock current_best_block() {
372 long ret = bindings.ChannelMonitor_current_best_block(this.ptr);
373 Reference.reachabilityFence(this);
374 if (ret >= 0 && ret <= 4096) { return null; }
375 org.ldk.structs.BestBlock ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.BestBlock(null, ret); }
376 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
381 * Triggers rebroadcasts/fee-bumps of pending claims from a force-closed channel. This is
382 * crucial in preventing certain classes of pinning attacks, detecting substantial mempool
383 * feerate changes between blocks, and ensuring reliability if broadcasting fails. We recommend
384 * invoking this every 30 seconds, or lower if running in an environment with spotty
385 * connections, like on mobile.
387 public void rebroadcast_pending_claims(org.ldk.structs.BroadcasterInterface broadcaster, org.ldk.structs.FeeEstimator fee_estimator, org.ldk.structs.Logger logger) {
388 bindings.ChannelMonitor_rebroadcast_pending_claims(this.ptr, broadcaster.ptr, fee_estimator.ptr, logger.ptr);
389 Reference.reachabilityFence(this);
390 Reference.reachabilityFence(broadcaster);
391 Reference.reachabilityFence(fee_estimator);
392 Reference.reachabilityFence(logger);
393 if (this != null) { this.ptrs_to.add(broadcaster); };
394 if (this != null) { this.ptrs_to.add(fee_estimator); };
395 if (this != null) { this.ptrs_to.add(logger); };
399 * Gets the balances in this channel which are either claimable by us if we were to
400 * force-close the channel now or which are claimable on-chain (possibly awaiting
403 * Any balances in the channel which are available on-chain (excluding on-chain fees) are
404 * included here until an [`Event::SpendableOutputs`] event has been generated for the
405 * balance, or until our counterparty has claimed the balance and accrued several
406 * confirmations on the claim transaction.
408 * Note that for `ChannelMonitors` which track a channel which went on-chain with versions of
409 * LDK prior to 0.0.111, balances may not be fully captured if our counterparty broadcasted
412 * See [`Balance`] for additional details on the types of claimable balances which
413 * may be returned here and their meanings.
415 public Balance[] get_claimable_balances() {
416 long[] ret = bindings.ChannelMonitor_get_claimable_balances(this.ptr);
417 Reference.reachabilityFence(this);
418 int ret_conv_9_len = ret.length;
419 Balance[] ret_conv_9_arr = new Balance[ret_conv_9_len];
420 for (int j = 0; j < ret_conv_9_len; j++) {
421 long ret_conv_9 = ret[j];
422 org.ldk.structs.Balance ret_conv_9_hu_conv = org.ldk.structs.Balance.constr_from_ptr(ret_conv_9);
423 if (ret_conv_9_hu_conv != null) { ret_conv_9_hu_conv.ptrs_to.add(this); };
424 ret_conv_9_arr[j] = ret_conv_9_hu_conv;
426 return ret_conv_9_arr;