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 ChannelMonitor ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new ChannelMonitor(null, ret); }
52 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(ChannelMonitorUpdate updates, BroadcasterInterface broadcaster, FeeEstimator fee_estimator, Logger logger) {
72 long ret = bindings.ChannelMonitor_update_monitor(this.ptr, updates == null ? 0 : updates.ptr & ~1, 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);
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 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 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(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);
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 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 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.
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;
176 return ret_conv_7_arr;
180 * Used by ChannelManager deserialization to broadcast the latest holder state if its copy of
181 * the Channel was out-of-date. You may use it to get a broadcastable holder toxic tx in case of
182 * fallen-behind, i.e when receiving a channel_reestablish with a proof that our counterparty side knows
183 * a higher revocation secret than the holder commitment number we are aware of. Broadcasting these
184 * transactions are UNSAFE, as they allow counterparty side to punish you. Nevertheless you may want to
185 * broadcast them if counterparty don't close channel with his higher commitment transaction after a
186 * substantial amount of time (a month or even a year) to get back funds. Best may be to contact
187 * out-of-band the other node operator to coordinate with him if option is available to you.
188 * In any-case, choice is up to the user.
190 public byte[][] get_latest_holder_commitment_txn(Logger logger) {
191 byte[][] ret = bindings.ChannelMonitor_get_latest_holder_commitment_txn(this.ptr, logger == null ? 0 : logger.ptr);
192 Reference.reachabilityFence(this);
193 Reference.reachabilityFence(logger);
194 this.ptrs_to.add(logger);
199 * Processes transactions in a newly connected block, which may result in any of the following:
200 * - update the monitor's state against resolved HTLCs
201 * - punish the counterparty in the case of seeing a revoked commitment transaction
202 * - force close the channel and claim/timeout incoming/outgoing HTLCs if near expiration
203 * - detect settled outputs for later spending
204 * - schedule and bump any in-flight claims
206 * Returns any new outputs to watch from `txdata`; after called, these are also included in
207 * [`get_outputs_to_watch`].
209 * [`get_outputs_to_watch`]: #method.get_outputs_to_watch
211 public TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[] block_connected(byte[] header, TwoTuple_usizeTransactionZ[] txdata, int height, BroadcasterInterface broadcaster, FeeEstimator fee_estimator, Logger logger) {
212 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);
213 Reference.reachabilityFence(this);
214 Reference.reachabilityFence(header);
215 Reference.reachabilityFence(txdata);
216 Reference.reachabilityFence(height);
217 Reference.reachabilityFence(broadcaster);
218 Reference.reachabilityFence(fee_estimator);
219 Reference.reachabilityFence(logger);
220 int ret_conv_39_len = ret.length;
221 TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[] ret_conv_39_arr = new TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[ret_conv_39_len];
222 for (int n = 0; n < ret_conv_39_len; n++) {
223 long ret_conv_39 = ret[n];
224 TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ ret_conv_39_hu_conv = new TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ(null, ret_conv_39);
225 ret_conv_39_hu_conv.ptrs_to.add(this);
226 ret_conv_39_arr[n] = ret_conv_39_hu_conv;
228 this.ptrs_to.add(broadcaster);
229 this.ptrs_to.add(fee_estimator);
230 this.ptrs_to.add(logger);
231 return ret_conv_39_arr;
235 * Determines if the disconnected block contained any transactions of interest and updates
238 public void block_disconnected(byte[] header, int height, BroadcasterInterface broadcaster, FeeEstimator fee_estimator, Logger logger) {
239 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);
240 Reference.reachabilityFence(this);
241 Reference.reachabilityFence(header);
242 Reference.reachabilityFence(height);
243 Reference.reachabilityFence(broadcaster);
244 Reference.reachabilityFence(fee_estimator);
245 Reference.reachabilityFence(logger);
246 this.ptrs_to.add(broadcaster);
247 this.ptrs_to.add(fee_estimator);
248 this.ptrs_to.add(logger);
252 * Processes transactions confirmed in a block with the given header and height, returning new
253 * outputs to watch. See [`block_connected`] for details.
255 * Used instead of [`block_connected`] by clients that are notified of transactions rather than
256 * blocks. See [`chain::Confirm`] for calling expectations.
258 * [`block_connected`]: Self::block_connected
260 public TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[] transactions_confirmed(byte[] header, TwoTuple_usizeTransactionZ[] txdata, int height, BroadcasterInterface broadcaster, FeeEstimator fee_estimator, Logger logger) {
261 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);
262 Reference.reachabilityFence(this);
263 Reference.reachabilityFence(header);
264 Reference.reachabilityFence(txdata);
265 Reference.reachabilityFence(height);
266 Reference.reachabilityFence(broadcaster);
267 Reference.reachabilityFence(fee_estimator);
268 Reference.reachabilityFence(logger);
269 int ret_conv_39_len = ret.length;
270 TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[] ret_conv_39_arr = new TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[ret_conv_39_len];
271 for (int n = 0; n < ret_conv_39_len; n++) {
272 long ret_conv_39 = ret[n];
273 TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ ret_conv_39_hu_conv = new TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ(null, ret_conv_39);
274 ret_conv_39_hu_conv.ptrs_to.add(this);
275 ret_conv_39_arr[n] = ret_conv_39_hu_conv;
277 this.ptrs_to.add(broadcaster);
278 this.ptrs_to.add(fee_estimator);
279 this.ptrs_to.add(logger);
280 return ret_conv_39_arr;
284 * Processes a transaction that was reorganized out of the chain.
286 * Used instead of [`block_disconnected`] by clients that are notified of transactions rather
287 * than blocks. See [`chain::Confirm`] for calling expectations.
289 * [`block_disconnected`]: Self::block_disconnected
291 public void transaction_unconfirmed(byte[] txid, BroadcasterInterface broadcaster, FeeEstimator fee_estimator, Logger logger) {
292 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);
293 Reference.reachabilityFence(this);
294 Reference.reachabilityFence(txid);
295 Reference.reachabilityFence(broadcaster);
296 Reference.reachabilityFence(fee_estimator);
297 Reference.reachabilityFence(logger);
298 this.ptrs_to.add(broadcaster);
299 this.ptrs_to.add(fee_estimator);
300 this.ptrs_to.add(logger);
304 * Updates the monitor with the current best chain tip, returning new outputs to watch. See
305 * [`block_connected`] for details.
307 * Used instead of [`block_connected`] by clients that are notified of transactions rather than
308 * blocks. See [`chain::Confirm`] for calling expectations.
310 * [`block_connected`]: Self::block_connected
312 public TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[] best_block_updated(byte[] header, int height, BroadcasterInterface broadcaster, FeeEstimator fee_estimator, Logger logger) {
313 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);
314 Reference.reachabilityFence(this);
315 Reference.reachabilityFence(header);
316 Reference.reachabilityFence(height);
317 Reference.reachabilityFence(broadcaster);
318 Reference.reachabilityFence(fee_estimator);
319 Reference.reachabilityFence(logger);
320 int ret_conv_39_len = ret.length;
321 TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[] ret_conv_39_arr = new TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[ret_conv_39_len];
322 for (int n = 0; n < ret_conv_39_len; n++) {
323 long ret_conv_39 = ret[n];
324 TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ ret_conv_39_hu_conv = new TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ(null, ret_conv_39);
325 ret_conv_39_hu_conv.ptrs_to.add(this);
326 ret_conv_39_arr[n] = ret_conv_39_hu_conv;
328 this.ptrs_to.add(broadcaster);
329 this.ptrs_to.add(fee_estimator);
330 this.ptrs_to.add(logger);
331 return ret_conv_39_arr;
335 * Returns the set of txids that should be monitored for re-organization out of the chain.
337 public byte[][] get_relevant_txids() {
338 byte[][] ret = bindings.ChannelMonitor_get_relevant_txids(this.ptr);
339 Reference.reachabilityFence(this);
344 * Gets the latest best block which was connected either via the [`chain::Listen`] or
345 * [`chain::Confirm`] interfaces.
347 public BestBlock current_best_block() {
348 long ret = bindings.ChannelMonitor_current_best_block(this.ptr);
349 Reference.reachabilityFence(this);
350 if (ret >= 0 && ret <= 4096) { return null; }
351 BestBlock ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new BestBlock(null, ret); }
352 ret_hu_conv.ptrs_to.add(this);
357 * Gets the balances in this channel which are either claimable by us if we were to
358 * force-close the channel now or which are claimable on-chain (possibly awaiting
361 * Any balances in the channel which are available on-chain (excluding on-chain fees) are
362 * included here until an [`Event::SpendableOutputs`] event has been generated for the
363 * balance, or until our counterparty has claimed the balance and accrued several
364 * confirmations on the claim transaction.
366 * Note that the balances available when you or your counterparty have broadcasted revoked
367 * state(s) may not be fully captured here.
369 * See [`Balance`] for additional details on the types of claimable balances which
370 * may be returned here and their meanings.
372 public Balance[] get_claimable_balances() {
373 long[] ret = bindings.ChannelMonitor_get_claimable_balances(this.ptr);
374 Reference.reachabilityFence(this);
375 int ret_conv_9_len = ret.length;
376 Balance[] ret_conv_9_arr = new Balance[ret_conv_9_len];
377 for (int j = 0; j < ret_conv_9_len; j++) {
378 long ret_conv_9 = ret[j];
379 org.ldk.structs.Balance ret_conv_9_hu_conv = org.ldk.structs.Balance.constr_from_ptr(ret_conv_9);
380 ret_conv_9_hu_conv.ptrs_to.add(this);
381 ret_conv_9_arr[j] = ret_conv_9_hu_conv;
383 return ret_conv_9_arr;