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 TwoTuple_TxidCVec_C2Tuple_u32ScriptZZZ[] ret_conv_40_arr = new TwoTuple_TxidCVec_C2Tuple_u32ScriptZZZ[ret.length];
117 for (int o = 0; o < ret.length; o++) {
118 long ret_conv_40 = ret[o];
119 TwoTuple_TxidCVec_C2Tuple_u32ScriptZZZ ret_conv_40_hu_conv = new TwoTuple_TxidCVec_C2Tuple_u32ScriptZZZ(null, ret_conv_40);
120 ret_conv_40_hu_conv.ptrs_to.add(this);
121 ret_conv_40_arr[o] = ret_conv_40_hu_conv;
123 return ret_conv_40_arr;
127 * Loads the funding txo and outputs to watch into the given `chain::Filter` by repeatedly
128 * calling `chain::Filter::register_output` and `chain::Filter::register_tx` until all outputs
129 * have been registered.
131 public void load_outputs_to_watch(Filter filter) {
132 bindings.ChannelMonitor_load_outputs_to_watch(this.ptr, filter == null ? 0 : filter.ptr);
133 Reference.reachabilityFence(this);
134 Reference.reachabilityFence(filter);
135 this.ptrs_to.add(filter);
139 * Get the list of HTLCs who's status has been updated on chain. This should be called by
140 * ChannelManager via [`chain::Watch::release_pending_monitor_events`].
142 public MonitorEvent[] get_and_clear_pending_monitor_events() {
143 long[] ret = bindings.ChannelMonitor_get_and_clear_pending_monitor_events(this.ptr);
144 Reference.reachabilityFence(this);
145 MonitorEvent[] ret_conv_14_arr = new MonitorEvent[ret.length];
146 for (int o = 0; o < ret.length; o++) {
147 long ret_conv_14 = ret[o];
148 MonitorEvent ret_conv_14_hu_conv = MonitorEvent.constr_from_ptr(ret_conv_14);
149 ret_conv_14_hu_conv.ptrs_to.add(this);
150 ret_conv_14_arr[o] = ret_conv_14_hu_conv;
152 return ret_conv_14_arr;
156 * Gets the list of pending events which were generated by previous actions, clearing the list
159 * This is called by ChainMonitor::get_and_clear_pending_events() and is equivalent to
160 * EventsProvider::get_and_clear_pending_events() except that it requires &mut self as we do
161 * no internal locking in ChannelMonitors.
163 public Event[] get_and_clear_pending_events() {
164 long[] ret = bindings.ChannelMonitor_get_and_clear_pending_events(this.ptr);
165 Reference.reachabilityFence(this);
166 Event[] ret_conv_7_arr = new Event[ret.length];
167 for (int h = 0; h < ret.length; h++) {
168 long ret_conv_7 = ret[h];
169 Event ret_conv_7_hu_conv = Event.constr_from_ptr(ret_conv_7);
170 ret_conv_7_hu_conv.ptrs_to.add(this);
171 ret_conv_7_arr[h] = ret_conv_7_hu_conv;
173 return ret_conv_7_arr;
177 * Used by ChannelManager deserialization to broadcast the latest holder state if its copy of
178 * the Channel was out-of-date. You may use it to get a broadcastable holder toxic tx in case of
179 * fallen-behind, i.e when receiving a channel_reestablish with a proof that our counterparty side knows
180 * a higher revocation secret than the holder commitment number we are aware of. Broadcasting these
181 * transactions are UNSAFE, as they allow counterparty side to punish you. Nevertheless you may want to
182 * broadcast them if counterparty don't close channel with his higher commitment transaction after a
183 * substantial amount of time (a month or even a year) to get back funds. Best may be to contact
184 * out-of-band the other node operator to coordinate with him if option is available to you.
185 * In any-case, choice is up to the user.
187 public byte[][] get_latest_holder_commitment_txn(Logger logger) {
188 byte[][] ret = bindings.ChannelMonitor_get_latest_holder_commitment_txn(this.ptr, logger == null ? 0 : logger.ptr);
189 Reference.reachabilityFence(this);
190 Reference.reachabilityFence(logger);
191 this.ptrs_to.add(logger);
196 * Processes transactions in a newly connected block, which may result in any of the following:
197 * - update the monitor's state against resolved HTLCs
198 * - punish the counterparty in the case of seeing a revoked commitment transaction
199 * - force close the channel and claim/timeout incoming/outgoing HTLCs if near expiration
200 * - detect settled outputs for later spending
201 * - schedule and bump any in-flight claims
203 * Returns any new outputs to watch from `txdata`; after called, these are also included in
204 * [`get_outputs_to_watch`].
206 * [`get_outputs_to_watch`]: #method.get_outputs_to_watch
208 public TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[] block_connected(byte[] header, TwoTuple_usizeTransactionZ[] txdata, int height, BroadcasterInterface broadcaster, FeeEstimator fee_estimator, Logger logger) {
209 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);
210 Reference.reachabilityFence(this);
211 Reference.reachabilityFence(header);
212 Reference.reachabilityFence(txdata);
213 Reference.reachabilityFence(height);
214 Reference.reachabilityFence(broadcaster);
215 Reference.reachabilityFence(fee_estimator);
216 Reference.reachabilityFence(logger);
217 TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[] ret_conv_39_arr = new TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[ret.length];
218 for (int n = 0; n < ret.length; n++) {
219 long ret_conv_39 = ret[n];
220 TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ ret_conv_39_hu_conv = new TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ(null, ret_conv_39);
221 ret_conv_39_hu_conv.ptrs_to.add(this);
222 ret_conv_39_arr[n] = ret_conv_39_hu_conv;
224 this.ptrs_to.add(broadcaster);
225 this.ptrs_to.add(fee_estimator);
226 this.ptrs_to.add(logger);
227 return ret_conv_39_arr;
231 * Determines if the disconnected block contained any transactions of interest and updates
234 public void block_disconnected(byte[] header, int height, BroadcasterInterface broadcaster, FeeEstimator fee_estimator, Logger logger) {
235 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);
236 Reference.reachabilityFence(this);
237 Reference.reachabilityFence(header);
238 Reference.reachabilityFence(height);
239 Reference.reachabilityFence(broadcaster);
240 Reference.reachabilityFence(fee_estimator);
241 Reference.reachabilityFence(logger);
242 this.ptrs_to.add(broadcaster);
243 this.ptrs_to.add(fee_estimator);
244 this.ptrs_to.add(logger);
248 * Processes transactions confirmed in a block with the given header and height, returning new
249 * outputs to watch. See [`block_connected`] for details.
251 * Used instead of [`block_connected`] by clients that are notified of transactions rather than
252 * blocks. See [`chain::Confirm`] for calling expectations.
254 * [`block_connected`]: Self::block_connected
256 public TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[] transactions_confirmed(byte[] header, TwoTuple_usizeTransactionZ[] txdata, int height, BroadcasterInterface broadcaster, FeeEstimator fee_estimator, Logger logger) {
257 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);
258 Reference.reachabilityFence(this);
259 Reference.reachabilityFence(header);
260 Reference.reachabilityFence(txdata);
261 Reference.reachabilityFence(height);
262 Reference.reachabilityFence(broadcaster);
263 Reference.reachabilityFence(fee_estimator);
264 Reference.reachabilityFence(logger);
265 TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[] ret_conv_39_arr = new TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[ret.length];
266 for (int n = 0; n < ret.length; n++) {
267 long ret_conv_39 = ret[n];
268 TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ ret_conv_39_hu_conv = new TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ(null, ret_conv_39);
269 ret_conv_39_hu_conv.ptrs_to.add(this);
270 ret_conv_39_arr[n] = ret_conv_39_hu_conv;
272 this.ptrs_to.add(broadcaster);
273 this.ptrs_to.add(fee_estimator);
274 this.ptrs_to.add(logger);
275 return ret_conv_39_arr;
279 * Processes a transaction that was reorganized out of the chain.
281 * Used instead of [`block_disconnected`] by clients that are notified of transactions rather
282 * than blocks. See [`chain::Confirm`] for calling expectations.
284 * [`block_disconnected`]: Self::block_disconnected
286 public void transaction_unconfirmed(byte[] txid, BroadcasterInterface broadcaster, FeeEstimator fee_estimator, Logger logger) {
287 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);
288 Reference.reachabilityFence(this);
289 Reference.reachabilityFence(txid);
290 Reference.reachabilityFence(broadcaster);
291 Reference.reachabilityFence(fee_estimator);
292 Reference.reachabilityFence(logger);
293 this.ptrs_to.add(broadcaster);
294 this.ptrs_to.add(fee_estimator);
295 this.ptrs_to.add(logger);
299 * Updates the monitor with the current best chain tip, returning new outputs to watch. See
300 * [`block_connected`] for details.
302 * Used instead of [`block_connected`] by clients that are notified of transactions rather than
303 * blocks. See [`chain::Confirm`] for calling expectations.
305 * [`block_connected`]: Self::block_connected
307 public TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[] best_block_updated(byte[] header, int height, BroadcasterInterface broadcaster, FeeEstimator fee_estimator, Logger logger) {
308 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);
309 Reference.reachabilityFence(this);
310 Reference.reachabilityFence(header);
311 Reference.reachabilityFence(height);
312 Reference.reachabilityFence(broadcaster);
313 Reference.reachabilityFence(fee_estimator);
314 Reference.reachabilityFence(logger);
315 TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[] ret_conv_39_arr = new TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[ret.length];
316 for (int n = 0; n < ret.length; n++) {
317 long ret_conv_39 = ret[n];
318 TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ ret_conv_39_hu_conv = new TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ(null, ret_conv_39);
319 ret_conv_39_hu_conv.ptrs_to.add(this);
320 ret_conv_39_arr[n] = ret_conv_39_hu_conv;
322 this.ptrs_to.add(broadcaster);
323 this.ptrs_to.add(fee_estimator);
324 this.ptrs_to.add(logger);
325 return ret_conv_39_arr;
329 * Returns the set of txids that should be monitored for re-organization out of the chain.
331 public byte[][] get_relevant_txids() {
332 byte[][] ret = bindings.ChannelMonitor_get_relevant_txids(this.ptr);
333 Reference.reachabilityFence(this);
338 * Gets the latest best block which was connected either via the [`chain::Listen`] or
339 * [`chain::Confirm`] interfaces.
341 public BestBlock current_best_block() {
342 long ret = bindings.ChannelMonitor_current_best_block(this.ptr);
343 Reference.reachabilityFence(this);
344 if (ret >= 0 && ret <= 4096) { return null; }
345 BestBlock ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new BestBlock(null, ret); }
346 ret_hu_conv.ptrs_to.add(this);
351 * Gets the balances in this channel which are either claimable by us if we were to
352 * force-close the channel now or which are claimable on-chain (possibly awaiting
355 * Any balances in the channel which are available on-chain (excluding on-chain fees) are
356 * included here until an [`Event::SpendableOutputs`] event has been generated for the
357 * balance, or until our counterparty has claimed the balance and accrued several
358 * confirmations on the claim transaction.
360 * Note that the balances available when you or your counterparty have broadcasted revoked
361 * state(s) may not be fully captured here.
363 * See [`Balance`] for additional details on the types of claimable balances which
364 * may be returned here and their meanings.
366 public Balance[] get_claimable_balances() {
367 long[] ret = bindings.ChannelMonitor_get_claimable_balances(this.ptr);
368 Reference.reachabilityFence(this);
369 Balance[] ret_conv_9_arr = new Balance[ret.length];
370 for (int j = 0; j < ret.length; j++) {
371 long ret_conv_9 = ret[j];
372 Balance ret_conv_9_hu_conv = Balance.constr_from_ptr(ret_conv_9);
373 ret_conv_9_hu_conv.ptrs_to.add(this);
374 ret_conv_9_arr[j] = ret_conv_9_hu_conv;
376 return ret_conv_9_arr;