1 package org.ldk.structs;
3 import org.ldk.impl.bindings;
4 import org.ldk.enums.*;
6 import java.util.Arrays;
7 import javax.annotation.Nullable;
11 * A ChannelMonitor handles chain events (blocks connected and disconnected) and generates
12 * on-chain transactions to ensure no loss of funds occurs.
14 * You MUST ensure that no ChannelMonitors for a given channel anywhere contain out-of-date
15 * information and are actively monitoring the chain.
17 * Pending Events or updated HTLCs which have not yet been read out by
18 * get_and_clear_pending_monitor_events or get_and_clear_pending_events are serialized to disk and
19 * reloaded at deserialize-time. Thus, you must ensure that, when handling events, all events
20 * gotten are fully handled before re-serializing the new state.
22 * Note that the deserializer is only implemented for (BlockHash, ChannelMonitor), which
23 * tells you the last block hash which was block_connect()ed. You MUST rescan any blocks along
24 * the \"reorg path\" (ie disconnecting blocks until you find a common ancestor from both the
25 * returned block hash and the the current chain and then reconnecting blocks to get to the
26 * best chain) upon deserializing the object!
28 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
29 public class ChannelMonitor extends CommonBase {
30 ChannelMonitor(Object _dummy, long ptr) { super(ptr); }
31 @Override @SuppressWarnings("deprecation")
32 protected void finalize() throws Throwable {
34 if (ptr != 0) { bindings.ChannelMonitor_free(ptr); }
38 long ret = bindings.ChannelMonitor_clone_ptr(this.ptr);
43 * Creates a copy of the ChannelMonitor
45 public ChannelMonitor clone() {
46 long ret = bindings.ChannelMonitor_clone(this.ptr);
47 if (ret >= 0 && ret <= 4096) { return null; }
48 ChannelMonitor ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new ChannelMonitor(null, ret); }
49 ret_hu_conv.ptrs_to.add(this);
54 * Serialize the ChannelMonitor object into a byte array which can be read by ChannelMonitor_read
56 public byte[] write() {
57 byte[] ret = bindings.ChannelMonitor_write(this.ptr);
62 * Updates a ChannelMonitor on the basis of some new information provided by the Channel
65 * panics if the given update is not the next update by update_id.
67 public Result_NoneMonitorUpdateErrorZ update_monitor(ChannelMonitorUpdate updates, BroadcasterInterface broadcaster, FeeEstimator fee_estimator, Logger logger) {
68 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);
69 if (ret >= 0 && ret <= 4096) { return null; }
70 Result_NoneMonitorUpdateErrorZ ret_hu_conv = Result_NoneMonitorUpdateErrorZ.constr_from_ptr(ret);
71 this.ptrs_to.add(updates);
72 this.ptrs_to.add(broadcaster);
73 this.ptrs_to.add(fee_estimator);
74 this.ptrs_to.add(logger);
79 * Gets the update_id from the latest ChannelMonitorUpdate which was applied to this
82 public long get_latest_update_id() {
83 long ret = bindings.ChannelMonitor_get_latest_update_id(this.ptr);
88 * Gets the funding transaction outpoint of the channel this ChannelMonitor is monitoring for.
90 public TwoTuple_OutPointScriptZ get_funding_txo() {
91 long ret = bindings.ChannelMonitor_get_funding_txo(this.ptr);
92 if (ret >= 0 && ret <= 4096) { return null; }
93 TwoTuple_OutPointScriptZ ret_hu_conv = new TwoTuple_OutPointScriptZ(null, ret);
94 ret_hu_conv.ptrs_to.add(this);
99 * Gets a list of txids, with their output scripts (in the order they appear in the
100 * transaction), which we must learn about spends of via block_connected().
102 public TwoTuple_TxidCVec_C2Tuple_u32ScriptZZZ[] get_outputs_to_watch() {
103 long[] ret = bindings.ChannelMonitor_get_outputs_to_watch(this.ptr);
104 TwoTuple_TxidCVec_C2Tuple_u32ScriptZZZ[] ret_conv_40_arr = new TwoTuple_TxidCVec_C2Tuple_u32ScriptZZZ[ret.length];
105 for (int o = 0; o < ret.length; o++) {
106 long ret_conv_40 = ret[o];
107 TwoTuple_TxidCVec_C2Tuple_u32ScriptZZZ ret_conv_40_hu_conv = new TwoTuple_TxidCVec_C2Tuple_u32ScriptZZZ(null, ret_conv_40);
108 ret_conv_40_hu_conv.ptrs_to.add(this);
109 ret_conv_40_arr[o] = ret_conv_40_hu_conv;
111 return ret_conv_40_arr;
115 * Loads the funding txo and outputs to watch into the given `chain::Filter` by repeatedly
116 * calling `chain::Filter::register_output` and `chain::Filter::register_tx` until all outputs
117 * have been registered.
119 public void load_outputs_to_watch(Filter filter) {
120 bindings.ChannelMonitor_load_outputs_to_watch(this.ptr, filter == null ? 0 : filter.ptr);
121 this.ptrs_to.add(filter);
125 * Get the list of HTLCs who's status has been updated on chain. This should be called by
126 * ChannelManager via [`chain::Watch::release_pending_monitor_events`].
128 public MonitorEvent[] get_and_clear_pending_monitor_events() {
129 long[] ret = bindings.ChannelMonitor_get_and_clear_pending_monitor_events(this.ptr);
130 MonitorEvent[] ret_conv_14_arr = new MonitorEvent[ret.length];
131 for (int o = 0; o < ret.length; o++) {
132 long ret_conv_14 = ret[o];
133 MonitorEvent ret_conv_14_hu_conv = MonitorEvent.constr_from_ptr(ret_conv_14);
134 ret_conv_14_hu_conv.ptrs_to.add(this);
135 ret_conv_14_arr[o] = ret_conv_14_hu_conv;
137 return ret_conv_14_arr;
141 * Gets the list of pending events which were generated by previous actions, clearing the list
144 * This is called by ChainMonitor::get_and_clear_pending_events() and is equivalent to
145 * EventsProvider::get_and_clear_pending_events() except that it requires &mut self as we do
146 * no internal locking in ChannelMonitors.
148 public Event[] get_and_clear_pending_events() {
149 long[] ret = bindings.ChannelMonitor_get_and_clear_pending_events(this.ptr);
150 Event[] ret_conv_7_arr = new Event[ret.length];
151 for (int h = 0; h < ret.length; h++) {
152 long ret_conv_7 = ret[h];
153 Event ret_conv_7_hu_conv = Event.constr_from_ptr(ret_conv_7);
154 ret_conv_7_hu_conv.ptrs_to.add(this);
155 ret_conv_7_arr[h] = ret_conv_7_hu_conv;
157 return ret_conv_7_arr;
161 * Used by ChannelManager deserialization to broadcast the latest holder state if its copy of
162 * the Channel was out-of-date. You may use it to get a broadcastable holder toxic tx in case of
163 * fallen-behind, i.e when receiving a channel_reestablish with a proof that our counterparty side knows
164 * a higher revocation secret than the holder commitment number we are aware of. Broadcasting these
165 * transactions are UNSAFE, as they allow counterparty side to punish you. Nevertheless you may want to
166 * broadcast them if counterparty don't close channel with his higher commitment transaction after a
167 * substantial amount of time (a month or even a year) to get back funds. Best may be to contact
168 * out-of-band the other node operator to coordinate with him if option is available to you.
169 * In any-case, choice is up to the user.
171 public byte[][] get_latest_holder_commitment_txn(Logger logger) {
172 byte[][] ret = bindings.ChannelMonitor_get_latest_holder_commitment_txn(this.ptr, logger == null ? 0 : logger.ptr);
173 this.ptrs_to.add(logger);
178 * Processes transactions in a newly connected block, which may result in any of the following:
179 * - update the monitor's state against resolved HTLCs
180 * - punish the counterparty in the case of seeing a revoked commitment transaction
181 * - force close the channel and claim/timeout incoming/outgoing HTLCs if near expiration
182 * - detect settled outputs for later spending
183 * - schedule and bump any in-flight claims
185 * Returns any new outputs to watch from `txdata`; after called, these are also included in
186 * [`get_outputs_to_watch`].
188 * [`get_outputs_to_watch`]: #method.get_outputs_to_watch
190 public TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[] block_connected(byte[] header, TwoTuple_usizeTransactionZ[] txdata, int height, BroadcasterInterface broadcaster, FeeEstimator fee_estimator, Logger logger) {
191 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);
192 TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[] ret_conv_39_arr = new TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[ret.length];
193 for (int n = 0; n < ret.length; n++) {
194 long ret_conv_39 = ret[n];
195 TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ ret_conv_39_hu_conv = new TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ(null, ret_conv_39);
196 ret_conv_39_hu_conv.ptrs_to.add(this);
197 ret_conv_39_arr[n] = ret_conv_39_hu_conv;
199 this.ptrs_to.add(broadcaster);
200 this.ptrs_to.add(fee_estimator);
201 this.ptrs_to.add(logger);
202 return ret_conv_39_arr;
206 * Determines if the disconnected block contained any transactions of interest and updates
209 public void block_disconnected(byte[] header, int height, BroadcasterInterface broadcaster, FeeEstimator fee_estimator, Logger logger) {
210 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);
211 this.ptrs_to.add(broadcaster);
212 this.ptrs_to.add(fee_estimator);
213 this.ptrs_to.add(logger);
217 * Processes transactions confirmed in a block with the given header and height, returning new
218 * outputs to watch. See [`block_connected`] for details.
220 * Used instead of [`block_connected`] by clients that are notified of transactions rather than
221 * blocks. See [`chain::Confirm`] for calling expectations.
223 * [`block_connected`]: Self::block_connected
225 public TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[] transactions_confirmed(byte[] header, TwoTuple_usizeTransactionZ[] txdata, int height, BroadcasterInterface broadcaster, FeeEstimator fee_estimator, Logger logger) {
226 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);
227 TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[] ret_conv_39_arr = new TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[ret.length];
228 for (int n = 0; n < ret.length; n++) {
229 long ret_conv_39 = ret[n];
230 TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ ret_conv_39_hu_conv = new TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ(null, ret_conv_39);
231 ret_conv_39_hu_conv.ptrs_to.add(this);
232 ret_conv_39_arr[n] = ret_conv_39_hu_conv;
234 this.ptrs_to.add(broadcaster);
235 this.ptrs_to.add(fee_estimator);
236 this.ptrs_to.add(logger);
237 return ret_conv_39_arr;
241 * Processes a transaction that was reorganized out of the chain.
243 * Used instead of [`block_disconnected`] by clients that are notified of transactions rather
244 * than blocks. See [`chain::Confirm`] for calling expectations.
246 * [`block_disconnected`]: Self::block_disconnected
248 public void transaction_unconfirmed(byte[] txid, BroadcasterInterface broadcaster, FeeEstimator fee_estimator, Logger logger) {
249 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);
250 this.ptrs_to.add(broadcaster);
251 this.ptrs_to.add(fee_estimator);
252 this.ptrs_to.add(logger);
256 * Updates the monitor with the current best chain tip, returning new outputs to watch. See
257 * [`block_connected`] for details.
259 * Used instead of [`block_connected`] by clients that are notified of transactions rather than
260 * blocks. See [`chain::Confirm`] for calling expectations.
262 * [`block_connected`]: Self::block_connected
264 public TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[] best_block_updated(byte[] header, int height, BroadcasterInterface broadcaster, FeeEstimator fee_estimator, Logger logger) {
265 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);
266 TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[] ret_conv_39_arr = new TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[ret.length];
267 for (int n = 0; n < ret.length; n++) {
268 long ret_conv_39 = ret[n];
269 TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ ret_conv_39_hu_conv = new TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ(null, ret_conv_39);
270 ret_conv_39_hu_conv.ptrs_to.add(this);
271 ret_conv_39_arr[n] = ret_conv_39_hu_conv;
273 this.ptrs_to.add(broadcaster);
274 this.ptrs_to.add(fee_estimator);
275 this.ptrs_to.add(logger);
276 return ret_conv_39_arr;
280 * Returns the set of txids that should be monitored for re-organization out of the chain.
282 public byte[][] get_relevant_txids() {
283 byte[][] ret = bindings.ChannelMonitor_get_relevant_txids(this.ptr);
288 * Gets the latest best block which was connected either via the [`chain::Listen`] or
289 * [`chain::Confirm`] interfaces.
291 public BestBlock current_best_block() {
292 long ret = bindings.ChannelMonitor_current_best_block(this.ptr);
293 if (ret >= 0 && ret <= 4096) { return null; }
294 BestBlock ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new BestBlock(null, ret); }
295 ret_hu_conv.ptrs_to.add(this);
300 * Gets the balances in this channel which are either claimable by us if we were to
301 * force-close the channel now or which are claimable on-chain (possibly awaiting
304 * Any balances in the channel which are available on-chain (excluding on-chain fees) are
305 * included here until an [`Event::SpendableOutputs`] event has been generated for the
306 * balance, or until our counterparty has claimed the balance and accrued several
307 * confirmations on the claim transaction.
309 * Note that the balances available when you or your counterparty have broadcasted revoked
310 * state(s) may not be fully captured here.
312 * See [`Balance`] for additional details on the types of claimable balances which
313 * may be returned here and their meanings.
315 public Balance[] get_claimable_balances() {
316 long[] ret = bindings.ChannelMonitor_get_claimable_balances(this.ptr);
317 Balance[] ret_conv_9_arr = new Balance[ret.length];
318 for (int j = 0; j < ret.length; j++) {
319 long ret_conv_9 = ret[j];
320 Balance ret_conv_9_hu_conv = Balance.constr_from_ptr(ret_conv_9);
321 ret_conv_9_hu_conv.ptrs_to.add(this);
322 ret_conv_9_arr[j] = ret_conv_9_hu_conv;
324 return ret_conv_9_arr;