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 * 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.
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!
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 {
35 if (ptr != 0) { bindings.ChannelMonitor_free(ptr); }
39 long ret = bindings.ChannelMonitor_clone_ptr(this.ptr);
40 Reference.reachabilityFence(this);
45 * Creates a copy of the ChannelMonitor
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 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
57 * Serialize the ChannelMonitor object into a byte array which can be read by ChannelMonitor_read
59 public byte[] write() {
60 byte[] ret = bindings.ChannelMonitor_write(this.ptr);
61 Reference.reachabilityFence(this);
66 * Updates a ChannelMonitor on the basis of some new information provided by the Channel
69 * panics if the given update is not the next update by update_id.
71 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) {
72 long ret = bindings.ChannelMonitor_update_monitor(this.ptr, updates == null ? 0 : updates.ptr, broadcaster.ptr, fee_estimator.ptr, 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 if (this != null) { this.ptrs_to.add(updates); };
81 if (this != null) { this.ptrs_to.add(broadcaster); };
82 if (this != null) { this.ptrs_to.add(fee_estimator); };
83 if (this != null) { this.ptrs_to.add(logger); };
88 * Gets the update_id from the latest ChannelMonitorUpdate which was applied to this
91 public long get_latest_update_id() {
92 long ret = bindings.ChannelMonitor_get_latest_update_id(this.ptr);
93 Reference.reachabilityFence(this);
98 * Gets the funding transaction outpoint of the channel this ChannelMonitor is monitoring for.
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 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
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().
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 if (ret_conv_40_hu_conv != null) { ret_conv_40_hu_conv.ptrs_to.add(this); };
122 ret_conv_40_arr[o] = ret_conv_40_hu_conv;
124 return ret_conv_40_arr;
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.
132 public void load_outputs_to_watch(org.ldk.structs.Filter filter) {
133 bindings.ChannelMonitor_load_outputs_to_watch(this.ptr, filter.ptr);
134 Reference.reachabilityFence(this);
135 Reference.reachabilityFence(filter);
136 if (this != null) { this.ptrs_to.add(filter); };
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`].
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 if (ret_conv_14_hu_conv != null) { ret_conv_14_hu_conv.ptrs_to.add(this); };
152 ret_conv_14_arr[o] = ret_conv_14_hu_conv;
154 return ret_conv_14_arr;
158 * Gets the list of pending events which were generated by previous actions, clearing the list
161 * This is called by the [`EventsProvider::process_pending_events`] implementation for
164 * [`EventsProvider::process_pending_events`]: crate::events::EventsProvider::process_pending_events
165 * [`ChainMonitor`]: crate::chain::chainmonitor::ChainMonitor
167 public Event[] get_and_clear_pending_events() {
168 long[] ret = bindings.ChannelMonitor_get_and_clear_pending_events(this.ptr);
169 Reference.reachabilityFence(this);
170 int ret_conv_7_len = ret.length;
171 Event[] ret_conv_7_arr = new Event[ret_conv_7_len];
172 for (int h = 0; h < ret_conv_7_len; h++) {
173 long ret_conv_7 = ret[h];
174 org.ldk.structs.Event ret_conv_7_hu_conv = org.ldk.structs.Event.constr_from_ptr(ret_conv_7);
175 if (ret_conv_7_hu_conv != null) { ret_conv_7_hu_conv.ptrs_to.add(this); };
176 ret_conv_7_arr[h] = ret_conv_7_hu_conv;
178 return ret_conv_7_arr;
182 * Gets the `node_id` of the counterparty for this channel.
184 * Will be `None` for channels constructed on LDK versions prior to 0.0.110 and always `Some`
187 * Note that the return value (or a relevant inner pointer) may be NULL or all-0s to represent None
190 public byte[] get_counterparty_node_id() {
191 byte[] ret = bindings.ChannelMonitor_get_counterparty_node_id(this.ptr);
192 Reference.reachabilityFence(this);
197 * Used by ChannelManager deserialization to broadcast the latest holder state if its copy of
198 * the Channel was out-of-date.
200 * You may also use this to broadcast the latest local commitment transaction, either because
201 * a monitor update failed with [`ChannelMonitorUpdateStatus::PermanentFailure`] or because we've
202 * fallen behind (i.e. we've received proof that our counterparty side knows a revocation
203 * secret we gave them that they shouldn't know).
205 * Broadcasting these transactions in the second case is UNSAFE, as they allow counterparty
206 * side to punish you. Nevertheless you may want to broadcast them if counterparty doesn't
207 * close channel with their commitment transaction after a substantial amount of time. Best
208 * may be to contact the other node operator out-of-band to coordinate other options available
209 * to you. In any-case, the choice is up to you.
211 * [`ChannelMonitorUpdateStatus::PermanentFailure`]: super::ChannelMonitorUpdateStatus::PermanentFailure
213 public byte[][] get_latest_holder_commitment_txn(org.ldk.structs.Logger logger) {
214 byte[][] ret = bindings.ChannelMonitor_get_latest_holder_commitment_txn(this.ptr, logger.ptr);
215 Reference.reachabilityFence(this);
216 Reference.reachabilityFence(logger);
217 if (this != null) { this.ptrs_to.add(logger); };
222 * Processes transactions in a newly connected block, which may result in any of the following:
223 * - update the monitor's state against resolved HTLCs
224 * - punish the counterparty in the case of seeing a revoked commitment transaction
225 * - force close the channel and claim/timeout incoming/outgoing HTLCs if near expiration
226 * - detect settled outputs for later spending
227 * - schedule and bump any in-flight claims
229 * Returns any new outputs to watch from `txdata`; after called, these are also included in
230 * [`get_outputs_to_watch`].
232 * [`get_outputs_to_watch`]: #method.get_outputs_to_watch
234 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) {
235 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);
236 Reference.reachabilityFence(this);
237 Reference.reachabilityFence(header);
238 Reference.reachabilityFence(txdata);
239 Reference.reachabilityFence(height);
240 Reference.reachabilityFence(broadcaster);
241 Reference.reachabilityFence(fee_estimator);
242 Reference.reachabilityFence(logger);
243 int ret_conv_39_len = ret.length;
244 TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[] ret_conv_39_arr = new TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[ret_conv_39_len];
245 for (int n = 0; n < ret_conv_39_len; n++) {
246 long ret_conv_39 = ret[n];
247 TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ ret_conv_39_hu_conv = new TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ(null, ret_conv_39);
248 if (ret_conv_39_hu_conv != null) { ret_conv_39_hu_conv.ptrs_to.add(this); };
249 ret_conv_39_arr[n] = ret_conv_39_hu_conv;
251 if (this != null) { this.ptrs_to.add(broadcaster); };
252 if (this != null) { this.ptrs_to.add(fee_estimator); };
253 if (this != null) { this.ptrs_to.add(logger); };
254 return ret_conv_39_arr;
258 * Determines if the disconnected block contained any transactions of interest and updates
261 public void block_disconnected(byte[] header, int height, org.ldk.structs.BroadcasterInterface broadcaster, org.ldk.structs.FeeEstimator fee_estimator, org.ldk.structs.Logger logger) {
262 bindings.ChannelMonitor_block_disconnected(this.ptr, InternalUtils.check_arr_len(header, 80), height, broadcaster.ptr, fee_estimator.ptr, logger.ptr);
263 Reference.reachabilityFence(this);
264 Reference.reachabilityFence(header);
265 Reference.reachabilityFence(height);
266 Reference.reachabilityFence(broadcaster);
267 Reference.reachabilityFence(fee_estimator);
268 Reference.reachabilityFence(logger);
269 if (this != null) { this.ptrs_to.add(broadcaster); };
270 if (this != null) { this.ptrs_to.add(fee_estimator); };
271 if (this != null) { this.ptrs_to.add(logger); };
275 * Processes transactions confirmed in a block with the given header and height, returning new
276 * outputs to watch. See [`block_connected`] for details.
278 * Used instead of [`block_connected`] by clients that are notified of transactions rather than
279 * blocks. See [`chain::Confirm`] for calling expectations.
281 * [`block_connected`]: Self::block_connected
283 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) {
284 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);
285 Reference.reachabilityFence(this);
286 Reference.reachabilityFence(header);
287 Reference.reachabilityFence(txdata);
288 Reference.reachabilityFence(height);
289 Reference.reachabilityFence(broadcaster);
290 Reference.reachabilityFence(fee_estimator);
291 Reference.reachabilityFence(logger);
292 int ret_conv_39_len = ret.length;
293 TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[] ret_conv_39_arr = new TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[ret_conv_39_len];
294 for (int n = 0; n < ret_conv_39_len; n++) {
295 long ret_conv_39 = ret[n];
296 TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ ret_conv_39_hu_conv = new TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ(null, ret_conv_39);
297 if (ret_conv_39_hu_conv != null) { ret_conv_39_hu_conv.ptrs_to.add(this); };
298 ret_conv_39_arr[n] = ret_conv_39_hu_conv;
300 if (this != null) { this.ptrs_to.add(broadcaster); };
301 if (this != null) { this.ptrs_to.add(fee_estimator); };
302 if (this != null) { this.ptrs_to.add(logger); };
303 return ret_conv_39_arr;
307 * Processes a transaction that was reorganized out of the chain.
309 * Used instead of [`block_disconnected`] by clients that are notified of transactions rather
310 * than blocks. See [`chain::Confirm`] for calling expectations.
312 * [`block_disconnected`]: Self::block_disconnected
314 public void transaction_unconfirmed(byte[] txid, org.ldk.structs.BroadcasterInterface broadcaster, org.ldk.structs.FeeEstimator fee_estimator, org.ldk.structs.Logger logger) {
315 bindings.ChannelMonitor_transaction_unconfirmed(this.ptr, InternalUtils.check_arr_len(txid, 32), broadcaster.ptr, fee_estimator.ptr, logger.ptr);
316 Reference.reachabilityFence(this);
317 Reference.reachabilityFence(txid);
318 Reference.reachabilityFence(broadcaster);
319 Reference.reachabilityFence(fee_estimator);
320 Reference.reachabilityFence(logger);
321 if (this != null) { this.ptrs_to.add(broadcaster); };
322 if (this != null) { this.ptrs_to.add(fee_estimator); };
323 if (this != null) { this.ptrs_to.add(logger); };
327 * Updates the monitor with the current best chain tip, returning new outputs to watch. See
328 * [`block_connected`] for details.
330 * Used instead of [`block_connected`] by clients that are notified of transactions rather than
331 * blocks. See [`chain::Confirm`] for calling expectations.
333 * [`block_connected`]: Self::block_connected
335 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) {
336 long[] ret = bindings.ChannelMonitor_best_block_updated(this.ptr, InternalUtils.check_arr_len(header, 80), height, broadcaster.ptr, fee_estimator.ptr, logger.ptr);
337 Reference.reachabilityFence(this);
338 Reference.reachabilityFence(header);
339 Reference.reachabilityFence(height);
340 Reference.reachabilityFence(broadcaster);
341 Reference.reachabilityFence(fee_estimator);
342 Reference.reachabilityFence(logger);
343 int ret_conv_39_len = ret.length;
344 TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[] ret_conv_39_arr = new TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[ret_conv_39_len];
345 for (int n = 0; n < ret_conv_39_len; n++) {
346 long ret_conv_39 = ret[n];
347 TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ ret_conv_39_hu_conv = new TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ(null, ret_conv_39);
348 if (ret_conv_39_hu_conv != null) { ret_conv_39_hu_conv.ptrs_to.add(this); };
349 ret_conv_39_arr[n] = ret_conv_39_hu_conv;
351 if (this != null) { this.ptrs_to.add(broadcaster); };
352 if (this != null) { this.ptrs_to.add(fee_estimator); };
353 if (this != null) { this.ptrs_to.add(logger); };
354 return ret_conv_39_arr;
358 * Returns the set of txids that should be monitored for re-organization out of the chain.
360 public TwoTuple_TxidBlockHashZ[] get_relevant_txids() {
361 long[] ret = bindings.ChannelMonitor_get_relevant_txids(this.ptr);
362 Reference.reachabilityFence(this);
363 int ret_conv_25_len = ret.length;
364 TwoTuple_TxidBlockHashZ[] ret_conv_25_arr = new TwoTuple_TxidBlockHashZ[ret_conv_25_len];
365 for (int z = 0; z < ret_conv_25_len; z++) {
366 long ret_conv_25 = ret[z];
367 TwoTuple_TxidBlockHashZ ret_conv_25_hu_conv = new TwoTuple_TxidBlockHashZ(null, ret_conv_25);
368 if (ret_conv_25_hu_conv != null) { ret_conv_25_hu_conv.ptrs_to.add(this); };
369 ret_conv_25_arr[z] = ret_conv_25_hu_conv;
371 return ret_conv_25_arr;
375 * Gets the latest best block which was connected either via the [`chain::Listen`] or
376 * [`chain::Confirm`] interfaces.
378 public BestBlock current_best_block() {
379 long ret = bindings.ChannelMonitor_current_best_block(this.ptr);
380 Reference.reachabilityFence(this);
381 if (ret >= 0 && ret <= 4096) { return null; }
382 org.ldk.structs.BestBlock ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.BestBlock(null, ret); }
383 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
388 * Triggers rebroadcasts/fee-bumps of pending claims from a force-closed channel. This is
389 * crucial in preventing certain classes of pinning attacks, detecting substantial mempool
390 * feerate changes between blocks, and ensuring reliability if broadcasting fails. We recommend
391 * invoking this every 30 seconds, or lower if running in an environment with spotty
392 * connections, like on mobile.
394 public void rebroadcast_pending_claims(org.ldk.structs.BroadcasterInterface broadcaster, org.ldk.structs.FeeEstimator fee_estimator, org.ldk.structs.Logger logger) {
395 bindings.ChannelMonitor_rebroadcast_pending_claims(this.ptr, broadcaster.ptr, fee_estimator.ptr, logger.ptr);
396 Reference.reachabilityFence(this);
397 Reference.reachabilityFence(broadcaster);
398 Reference.reachabilityFence(fee_estimator);
399 Reference.reachabilityFence(logger);
400 if (this != null) { this.ptrs_to.add(broadcaster); };
401 if (this != null) { this.ptrs_to.add(fee_estimator); };
402 if (this != null) { this.ptrs_to.add(logger); };
406 * Gets the balances in this channel which are either claimable by us if we were to
407 * force-close the channel now or which are claimable on-chain (possibly awaiting
410 * Any balances in the channel which are available on-chain (excluding on-chain fees) are
411 * included here until an [`Event::SpendableOutputs`] event has been generated for the
412 * balance, or until our counterparty has claimed the balance and accrued several
413 * confirmations on the claim transaction.
415 * Note that for `ChannelMonitors` which track a channel which went on-chain with versions of
416 * LDK prior to 0.0.111, balances may not be fully captured if our counterparty broadcasted
419 * See [`Balance`] for additional details on the types of claimable balances which
420 * may be returned here and their meanings.
422 public Balance[] get_claimable_balances() {
423 long[] ret = bindings.ChannelMonitor_get_claimable_balances(this.ptr);
424 Reference.reachabilityFence(this);
425 int ret_conv_9_len = ret.length;
426 Balance[] ret_conv_9_arr = new Balance[ret_conv_9_len];
427 for (int j = 0; j < ret_conv_9_len; j++) {
428 long ret_conv_9 = ret[j];
429 org.ldk.structs.Balance ret_conv_9_hu_conv = org.ldk.structs.Balance.constr_from_ptr(ret_conv_9);
430 if (ret_conv_9_hu_conv != null) { ret_conv_9_hu_conv.ptrs_to.add(this); };
431 ret_conv_9_arr[j] = ret_conv_9_hu_conv;
433 return ret_conv_9_arr;