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 * Creates a copy of the ChannelMonitor
40 public ChannelMonitor clone() {
41 long ret = bindings.ChannelMonitor_clone(this.ptr);
42 if (ret < 1024) { return null; }
43 ChannelMonitor ret_hu_conv = new ChannelMonitor(null, ret);
44 ret_hu_conv.ptrs_to.add(this);
49 * Serialize the ChannelMonitor object into a byte array which can be read by ChannelMonitor_read
51 public byte[] write() {
52 byte[] ret = bindings.ChannelMonitor_write(this.ptr);
57 * Updates a ChannelMonitor on the basis of some new information provided by the Channel
60 * panics if the given update is not the next update by update_id.
62 public Result_NoneMonitorUpdateErrorZ update_monitor(ChannelMonitorUpdate updates, BroadcasterInterface broadcaster, FeeEstimator fee_estimator, Logger logger) {
63 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);
64 if (ret < 1024) { return null; }
65 Result_NoneMonitorUpdateErrorZ ret_hu_conv = Result_NoneMonitorUpdateErrorZ.constr_from_ptr(ret);
66 this.ptrs_to.add(updates);
67 this.ptrs_to.add(broadcaster);
68 this.ptrs_to.add(fee_estimator);
69 this.ptrs_to.add(logger);
74 * Gets the update_id from the latest ChannelMonitorUpdate which was applied to this
77 public long get_latest_update_id() {
78 long ret = bindings.ChannelMonitor_get_latest_update_id(this.ptr);
83 * Gets the funding transaction outpoint of the channel this ChannelMonitor is monitoring for.
85 public TwoTuple_OutPointScriptZ get_funding_txo() {
86 long ret = bindings.ChannelMonitor_get_funding_txo(this.ptr);
87 if (ret < 1024) { return null; }
88 TwoTuple_OutPointScriptZ ret_hu_conv = new TwoTuple_OutPointScriptZ(null, ret);
89 ret_hu_conv.ptrs_to.add(this);
94 * Gets a list of txids, with their output scripts (in the order they appear in the
95 * transaction), which we must learn about spends of via block_connected().
97 public TwoTuple_TxidCVec_C2Tuple_u32ScriptZZZ[] get_outputs_to_watch() {
98 long[] ret = bindings.ChannelMonitor_get_outputs_to_watch(this.ptr);
99 TwoTuple_TxidCVec_C2Tuple_u32ScriptZZZ[] ret_conv_40_arr = new TwoTuple_TxidCVec_C2Tuple_u32ScriptZZZ[ret.length];
100 for (int o = 0; o < ret.length; o++) {
101 long ret_conv_40 = ret[o];
102 TwoTuple_TxidCVec_C2Tuple_u32ScriptZZZ ret_conv_40_hu_conv = new TwoTuple_TxidCVec_C2Tuple_u32ScriptZZZ(null, ret_conv_40);
103 ret_conv_40_hu_conv.ptrs_to.add(this);
104 ret_conv_40_arr[o] = ret_conv_40_hu_conv;
106 return ret_conv_40_arr;
110 * Loads the funding txo and outputs to watch into the given `chain::Filter` by repeatedly
111 * calling `chain::Filter::register_output` and `chain::Filter::register_tx` until all outputs
112 * have been registered.
114 public void load_outputs_to_watch(Filter filter) {
115 bindings.ChannelMonitor_load_outputs_to_watch(this.ptr, filter == null ? 0 : filter.ptr);
116 this.ptrs_to.add(filter);
120 * Get the list of HTLCs who's status has been updated on chain. This should be called by
121 * ChannelManager via [`chain::Watch::release_pending_monitor_events`].
123 public MonitorEvent[] get_and_clear_pending_monitor_events() {
124 long[] ret = bindings.ChannelMonitor_get_and_clear_pending_monitor_events(this.ptr);
125 MonitorEvent[] ret_conv_14_arr = new MonitorEvent[ret.length];
126 for (int o = 0; o < ret.length; o++) {
127 long ret_conv_14 = ret[o];
128 MonitorEvent ret_conv_14_hu_conv = MonitorEvent.constr_from_ptr(ret_conv_14);
129 ret_conv_14_hu_conv.ptrs_to.add(this);
130 ret_conv_14_arr[o] = ret_conv_14_hu_conv;
132 return ret_conv_14_arr;
136 * Gets the list of pending events which were generated by previous actions, clearing the list
139 * This is called by ChainMonitor::get_and_clear_pending_events() and is equivalent to
140 * EventsProvider::get_and_clear_pending_events() except that it requires &mut self as we do
141 * no internal locking in ChannelMonitors.
143 public Event[] get_and_clear_pending_events() {
144 long[] ret = bindings.ChannelMonitor_get_and_clear_pending_events(this.ptr);
145 Event[] ret_conv_7_arr = new Event[ret.length];
146 for (int h = 0; h < ret.length; h++) {
147 long ret_conv_7 = ret[h];
148 Event ret_conv_7_hu_conv = Event.constr_from_ptr(ret_conv_7);
149 ret_conv_7_hu_conv.ptrs_to.add(this);
150 ret_conv_7_arr[h] = ret_conv_7_hu_conv;
152 return ret_conv_7_arr;
156 * Used by ChannelManager deserialization to broadcast the latest holder state if its copy of
157 * the Channel was out-of-date. You may use it to get a broadcastable holder toxic tx in case of
158 * fallen-behind, i.e when receiving a channel_reestablish with a proof that our counterparty side knows
159 * a higher revocation secret than the holder commitment number we are aware of. Broadcasting these
160 * transactions are UNSAFE, as they allow counterparty side to punish you. Nevertheless you may want to
161 * broadcast them if counterparty don't close channel with his higher commitment transaction after a
162 * substantial amount of time (a month or even a year) to get back funds. Best may be to contact
163 * out-of-band the other node operator to coordinate with him if option is available to you.
164 * In any-case, choice is up to the user.
166 public byte[][] get_latest_holder_commitment_txn(Logger logger) {
167 byte[][] ret = bindings.ChannelMonitor_get_latest_holder_commitment_txn(this.ptr, logger == null ? 0 : logger.ptr);
168 this.ptrs_to.add(logger);
173 * Processes transactions in a newly connected block, which may result in any of the following:
174 * - update the monitor's state against resolved HTLCs
175 * - punish the counterparty in the case of seeing a revoked commitment transaction
176 * - force close the channel and claim/timeout incoming/outgoing HTLCs if near expiration
177 * - detect settled outputs for later spending
178 * - schedule and bump any in-flight claims
180 * Returns any new outputs to watch from `txdata`; after called, these are also included in
181 * [`get_outputs_to_watch`].
183 * [`get_outputs_to_watch`]: #method.get_outputs_to_watch
185 public TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[] block_connected(byte[] header, TwoTuple_usizeTransactionZ[] txdata, int height, BroadcasterInterface broadcaster, FeeEstimator fee_estimator, Logger logger) {
186 long[] ret = bindings.ChannelMonitor_block_connected(this.ptr, header, 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);
187 TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[] ret_conv_39_arr = new TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[ret.length];
188 for (int n = 0; n < ret.length; n++) {
189 long ret_conv_39 = ret[n];
190 TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ ret_conv_39_hu_conv = new TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ(null, ret_conv_39);
191 ret_conv_39_hu_conv.ptrs_to.add(this);
192 ret_conv_39_arr[n] = ret_conv_39_hu_conv;
194 this.ptrs_to.add(broadcaster);
195 this.ptrs_to.add(fee_estimator);
196 this.ptrs_to.add(logger);
197 return ret_conv_39_arr;
201 * Determines if the disconnected block contained any transactions of interest and updates
204 public void block_disconnected(byte[] header, int height, BroadcasterInterface broadcaster, FeeEstimator fee_estimator, Logger logger) {
205 bindings.ChannelMonitor_block_disconnected(this.ptr, header, height, broadcaster == null ? 0 : broadcaster.ptr, fee_estimator == null ? 0 : fee_estimator.ptr, logger == null ? 0 : logger.ptr);
206 this.ptrs_to.add(broadcaster);
207 this.ptrs_to.add(fee_estimator);
208 this.ptrs_to.add(logger);
212 * Processes transactions confirmed in a block with the given header and height, returning new
213 * outputs to watch. See [`block_connected`] for details.
215 * Used instead of [`block_connected`] by clients that are notified of transactions rather than
216 * blocks. See [`chain::Confirm`] for calling expectations.
218 * [`block_connected`]: Self::block_connected
220 public TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[] transactions_confirmed(byte[] header, TwoTuple_usizeTransactionZ[] txdata, int height, BroadcasterInterface broadcaster, FeeEstimator fee_estimator, Logger logger) {
221 long[] ret = bindings.ChannelMonitor_transactions_confirmed(this.ptr, header, 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);
222 TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[] ret_conv_39_arr = new TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[ret.length];
223 for (int n = 0; n < ret.length; n++) {
224 long ret_conv_39 = ret[n];
225 TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ ret_conv_39_hu_conv = new TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ(null, ret_conv_39);
226 ret_conv_39_hu_conv.ptrs_to.add(this);
227 ret_conv_39_arr[n] = ret_conv_39_hu_conv;
229 this.ptrs_to.add(broadcaster);
230 this.ptrs_to.add(fee_estimator);
231 this.ptrs_to.add(logger);
232 return ret_conv_39_arr;
236 * Processes a transaction that was reorganized out of the chain.
238 * Used instead of [`block_disconnected`] by clients that are notified of transactions rather
239 * than blocks. See [`chain::Confirm`] for calling expectations.
241 * [`block_disconnected`]: Self::block_disconnected
243 public void transaction_unconfirmed(byte[] txid, BroadcasterInterface broadcaster, FeeEstimator fee_estimator, Logger logger) {
244 bindings.ChannelMonitor_transaction_unconfirmed(this.ptr, txid, broadcaster == null ? 0 : broadcaster.ptr, fee_estimator == null ? 0 : fee_estimator.ptr, logger == null ? 0 : logger.ptr);
245 this.ptrs_to.add(broadcaster);
246 this.ptrs_to.add(fee_estimator);
247 this.ptrs_to.add(logger);
251 * Updates the monitor with the current best chain tip, returning new outputs to watch. See
252 * [`block_connected`] for details.
254 * Used instead of [`block_connected`] by clients that are notified of transactions rather than
255 * blocks. See [`chain::Confirm`] for calling expectations.
257 * [`block_connected`]: Self::block_connected
259 public TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[] best_block_updated(byte[] header, int height, BroadcasterInterface broadcaster, FeeEstimator fee_estimator, Logger logger) {
260 long[] ret = bindings.ChannelMonitor_best_block_updated(this.ptr, header, height, broadcaster == null ? 0 : broadcaster.ptr, fee_estimator == null ? 0 : fee_estimator.ptr, logger == null ? 0 : logger.ptr);
261 TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[] ret_conv_39_arr = new TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ[ret.length];
262 for (int n = 0; n < ret.length; n++) {
263 long ret_conv_39 = ret[n];
264 TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ ret_conv_39_hu_conv = new TwoTuple_TxidCVec_C2Tuple_u32TxOutZZZ(null, ret_conv_39);
265 ret_conv_39_hu_conv.ptrs_to.add(this);
266 ret_conv_39_arr[n] = ret_conv_39_hu_conv;
268 this.ptrs_to.add(broadcaster);
269 this.ptrs_to.add(fee_estimator);
270 this.ptrs_to.add(logger);
271 return ret_conv_39_arr;
275 * Returns the set of txids that should be monitored for re-organization out of the chain.
277 public byte[][] get_relevant_txids() {
278 byte[][] ret = bindings.ChannelMonitor_get_relevant_txids(this.ptr);
283 * Gets the latest best block which was connected either via the [`chain::Listen`] or
284 * [`chain::Confirm`] interfaces.
286 public BestBlock current_best_block() {
287 long ret = bindings.ChannelMonitor_current_best_block(this.ptr);
288 if (ret < 1024) { return null; }
289 BestBlock ret_hu_conv = new BestBlock(null, ret);
290 ret_hu_conv.ptrs_to.add(this);
295 * Gets the balances in this channel which are either claimable by us if we were to
296 * force-close the channel now or which are claimable on-chain (possibly awaiting
299 * Any balances in the channel which are available on-chain (excluding on-chain fees) are
300 * included here until an [`Event::SpendableOutputs`] event has been generated for the
301 * balance, or until our counterparty has claimed the balance and accrued several
302 * confirmations on the claim transaction.
304 * Note that the balances available when you or your counterparty have broadcasted revoked
305 * state(s) may not be fully captured here.
307 * See [`Balance`] for additional details on the types of claimable balances which
308 * may be returned here and their meanings.
310 public Balance[] get_claimable_balances() {
311 long[] ret = bindings.ChannelMonitor_get_claimable_balances(this.ptr);
312 Balance[] ret_conv_9_arr = new Balance[ret.length];
313 for (int j = 0; j < ret.length; j++) {
314 long ret_conv_9 = ret[j];
315 Balance ret_conv_9_hu_conv = Balance.constr_from_ptr(ret_conv_9);
316 ret_conv_9_hu_conv.ptrs_to.add(this);
317 ret_conv_9_arr[j] = ret_conv_9_hu_conv;
319 return ret_conv_9_arr;