1 package org.ldk.structs;
3 import org.ldk.impl.bindings;
4 import org.ldk.enums.*;
6 import java.util.Arrays;
10 * A ChannelMonitor handles chain events (blocks connected and disconnected) and generates
11 * on-chain transactions to ensure no loss of funds occurs.
13 * You MUST ensure that no ChannelMonitors for a given channel anywhere contain out-of-date
14 * information and are actively monitoring the chain.
16 * Pending Events or updated HTLCs which have not yet been read out by
17 * get_and_clear_pending_monitor_events or get_and_clear_pending_events are serialized to disk and
18 * reloaded at deserialize-time. Thus, you must ensure that, when handling events, all events
19 * gotten are fully handled before re-serializing the new state.
21 * Note that the deserializer is only implemented for (BlockHash, ChannelMonitor), which
22 * tells you the last block hash which was block_connect()ed. You MUST rescan any blocks along
23 * the \"reorg path\" (ie disconnecting blocks until you find a common ancestor from both the
24 * returned block hash and the the current chain and then reconnecting blocks to get to the
25 * best chain) upon deserializing the object!
27 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
28 public class ChannelMonitor extends CommonBase {
29 ChannelMonitor(Object _dummy, long ptr) { super(ptr); }
30 @Override @SuppressWarnings("deprecation")
31 protected void finalize() throws Throwable {
33 if (ptr != 0) { bindings.ChannelMonitor_free(ptr); }
37 * Creates a copy of the ChannelMonitor
39 public ChannelMonitor clone() {
40 long ret = bindings.ChannelMonitor_clone(this.ptr);
41 ChannelMonitor ret_hu_conv = new ChannelMonitor(null, ret);
42 ret_hu_conv.ptrs_to.add(this);
47 * Serialize the ChannelMonitor object into a byte array which can be read by ChannelMonitor_read
49 public byte[] write() {
50 byte[] ret = bindings.ChannelMonitor_write(this.ptr);
55 * Updates a ChannelMonitor on the basis of some new information provided by the Channel
58 * panics if the given update is not the next update by update_id.
60 public Result_NoneMonitorUpdateErrorZ update_monitor(ChannelMonitorUpdate updates, BroadcasterInterface broadcaster, FeeEstimator fee_estimator, Logger logger) {
61 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);
62 Result_NoneMonitorUpdateErrorZ ret_hu_conv = Result_NoneMonitorUpdateErrorZ.constr_from_ptr(ret);
63 this.ptrs_to.add(updates);
64 this.ptrs_to.add(broadcaster);
65 this.ptrs_to.add(fee_estimator);
66 this.ptrs_to.add(logger);
71 * Gets the update_id from the latest ChannelMonitorUpdate which was applied to this
74 public long get_latest_update_id() {
75 long ret = bindings.ChannelMonitor_get_latest_update_id(this.ptr);
80 * Gets the funding transaction outpoint of the channel this ChannelMonitor is monitoring for.
82 public TwoTuple<OutPoint, byte[]> get_funding_txo() {
83 long ret = bindings.ChannelMonitor_get_funding_txo(this.ptr);
84 long ret_a = bindings.LDKC2Tuple_OutPointScriptZ_get_a(ret);
85 OutPoint ret_a_hu_conv = new OutPoint(null, ret_a);
86 ret_a_hu_conv.ptrs_to.add(this);;
87 byte[] ret_b = bindings.LDKC2Tuple_OutPointScriptZ_get_b(ret);
88 TwoTuple<OutPoint, byte[]> ret_conv = new TwoTuple<OutPoint, byte[]>(ret_a_hu_conv, ret_b, () -> {
89 bindings.C2Tuple_OutPointScriptZ_free(ret);
91 ret_a_hu_conv.ptrs_to.add(ret_conv);
96 * Gets a list of txids, with their output scripts (in the order they appear in the
97 * transaction), which we must learn about spends of via block_connected().
99 public TwoTuple<byte[], TwoTuple<Integer, byte[]>[]>[] get_outputs_to_watch() {
100 long[] ret = bindings.ChannelMonitor_get_outputs_to_watch(this.ptr);
101 TwoTuple<byte[], TwoTuple<Integer, byte[]>[]>[] ret_conv_47_arr = new TwoTuple[ret.length];
102 for (int v = 0; v < ret.length; v++) {
103 long ret_conv_47 = ret[v];
104 byte[] ret_conv_47_a = bindings.LDKC2Tuple_TxidCVec_C2Tuple_u32ScriptZZZ_get_a(ret_conv_47);
105 long[] ret_conv_47_b = bindings.LDKC2Tuple_TxidCVec_C2Tuple_u32ScriptZZZ_get_b(ret_conv_47);
106 TwoTuple<Integer, byte[]>[] ret_conv_47_b_conv_27_arr = new TwoTuple[ret_conv_47_b.length];
107 for (int b = 0; b < ret_conv_47_b.length; b++) {
108 long ret_conv_47_b_conv_27 = ret_conv_47_b[b];
109 int ret_conv_47_b_conv_27_a = bindings.LDKC2Tuple_u32ScriptZ_get_a(ret_conv_47_b_conv_27);
110 byte[] ret_conv_47_b_conv_27_b = bindings.LDKC2Tuple_u32ScriptZ_get_b(ret_conv_47_b_conv_27);
111 TwoTuple<Integer, byte[]> ret_conv_47_b_conv_27_conv = new TwoTuple<Integer, byte[]>(ret_conv_47_b_conv_27_a, ret_conv_47_b_conv_27_b, () -> {
112 bindings.C2Tuple_u32ScriptZ_free(ret_conv_47_b_conv_27);
114 ret_conv_47_b_conv_27_arr[b] = ret_conv_47_b_conv_27_conv;
116 TwoTuple<byte[], TwoTuple<Integer, byte[]>[]> ret_conv_47_conv = new TwoTuple<byte[], TwoTuple<Integer, byte[]>[]>(ret_conv_47_a, ret_conv_47_b_conv_27_arr);
117 // Warning: We may not free the C tuple object!
118 ret_conv_47_arr[v] = ret_conv_47_conv;
120 return ret_conv_47_arr;
124 * Loads the funding txo and outputs to watch into the given `chain::Filter` by repeatedly
125 * calling `chain::Filter::register_output` and `chain::Filter::register_tx` until all outputs
126 * have been registered.
128 public void load_outputs_to_watch(Filter filter) {
129 bindings.ChannelMonitor_load_outputs_to_watch(this.ptr, filter == null ? 0 : filter.ptr);
130 this.ptrs_to.add(filter);
134 * Get the list of HTLCs who's status has been updated on chain. This should be called by
135 * ChannelManager via [`chain::Watch::release_pending_monitor_events`].
137 public MonitorEvent[] get_and_clear_pending_monitor_events() {
138 long[] ret = bindings.ChannelMonitor_get_and_clear_pending_monitor_events(this.ptr);
139 MonitorEvent[] ret_conv_14_arr = new MonitorEvent[ret.length];
140 for (int o = 0; o < ret.length; o++) {
141 long ret_conv_14 = ret[o];
142 MonitorEvent ret_conv_14_hu_conv = MonitorEvent.constr_from_ptr(ret_conv_14);
143 ret_conv_14_hu_conv.ptrs_to.add(this);
144 ret_conv_14_arr[o] = ret_conv_14_hu_conv;
146 return ret_conv_14_arr;
150 * Gets the list of pending events which were generated by previous actions, clearing the list
153 * This is called by ChainMonitor::get_and_clear_pending_events() and is equivalent to
154 * EventsProvider::get_and_clear_pending_events() except that it requires &mut self as we do
155 * no internal locking in ChannelMonitors.
157 public Event[] get_and_clear_pending_events() {
158 long[] ret = bindings.ChannelMonitor_get_and_clear_pending_events(this.ptr);
159 Event[] ret_conv_7_arr = new Event[ret.length];
160 for (int h = 0; h < ret.length; h++) {
161 long ret_conv_7 = ret[h];
162 Event ret_conv_7_hu_conv = Event.constr_from_ptr(ret_conv_7);
163 ret_conv_7_hu_conv.ptrs_to.add(this);
164 ret_conv_7_arr[h] = ret_conv_7_hu_conv;
166 return ret_conv_7_arr;
170 * Used by ChannelManager deserialization to broadcast the latest holder state if its copy of
171 * the Channel was out-of-date. You may use it to get a broadcastable holder toxic tx in case of
172 * fallen-behind, i.e when receiving a channel_reestablish with a proof that our counterparty side knows
173 * a higher revocation secret than the holder commitment number we are aware of. Broadcasting these
174 * transactions are UNSAFE, as they allow counterparty side to punish you. Nevertheless you may want to
175 * broadcast them if counterparty don't close channel with his higher commitment transaction after a
176 * substantial amount of time (a month or even a year) to get back funds. Best may be to contact
177 * out-of-band the other node operator to coordinate with him if option is available to you.
178 * In any-case, choice is up to the user.
180 public byte[][] get_latest_holder_commitment_txn(Logger logger) {
181 byte[][] ret = bindings.ChannelMonitor_get_latest_holder_commitment_txn(this.ptr, logger == null ? 0 : logger.ptr);
182 this.ptrs_to.add(logger);
187 * Processes transactions in a newly connected block, which may result in any of the following:
188 * - update the monitor's state against resolved HTLCs
189 * - punish the counterparty in the case of seeing a revoked commitment transaction
190 * - force close the channel and claim/timeout incoming/outgoing HTLCs if near expiration
191 * - detect settled outputs for later spending
192 * - schedule and bump any in-flight claims
194 * Returns any new outputs to watch from `txdata`; after called, these are also included in
195 * [`get_outputs_to_watch`].
197 * [`get_outputs_to_watch`]: #method.get_outputs_to_watch
199 public TwoTuple<byte[], TwoTuple<Integer, TxOut>[]>[] block_connected(byte[] header, TwoTuple<Long, byte[]>[] txdata, int height, BroadcasterInterface broadcaster, FeeEstimator fee_estimator, Logger logger) {
200 long[] ret = bindings.ChannelMonitor_block_connected(this.ptr, header, Arrays.stream(txdata).mapToLong(txdata_conv_24 -> bindings.C2Tuple_usizeTransactionZ_new(txdata_conv_24.a, txdata_conv_24.b)).toArray(), height, broadcaster == null ? 0 : broadcaster.ptr, fee_estimator == null ? 0 : fee_estimator.ptr, logger == null ? 0 : logger.ptr);
201 TwoTuple<byte[], TwoTuple<Integer, TxOut>[]>[] ret_conv_46_arr = new TwoTuple[ret.length];
202 for (int u = 0; u < ret.length; u++) {
203 long ret_conv_46 = ret[u];
204 byte[] ret_conv_46_a = bindings.LDKC2Tuple_TxidCVec_C2Tuple_u32TxOutZZZ_get_a(ret_conv_46);
205 long[] ret_conv_46_b = bindings.LDKC2Tuple_TxidCVec_C2Tuple_u32TxOutZZZ_get_b(ret_conv_46);
206 TwoTuple<Integer, TxOut>[] ret_conv_46_b_conv_26_arr = new TwoTuple[ret_conv_46_b.length];
207 for (int a = 0; a < ret_conv_46_b.length; a++) {
208 long ret_conv_46_b_conv_26 = ret_conv_46_b[a];
209 int ret_conv_46_b_conv_26_a = bindings.LDKC2Tuple_u32TxOutZ_get_a(ret_conv_46_b_conv_26);
210 long ret_conv_46_b_conv_26_b = bindings.TxOut_clone(bindings.LDKC2Tuple_u32TxOutZ_get_b(ret_conv_46_b_conv_26));
211 TxOut ret_conv_46_b_conv_26_b_conv = new TxOut(null, ret_conv_46_b_conv_26_b);;
212 TwoTuple<Integer, TxOut> ret_conv_46_b_conv_26_conv = new TwoTuple<Integer, TxOut>(ret_conv_46_b_conv_26_a, ret_conv_46_b_conv_26_b_conv, () -> {
213 bindings.C2Tuple_u32TxOutZ_free(ret_conv_46_b_conv_26);
215 ret_conv_46_b_conv_26_b_conv.ptrs_to.add(ret_conv_46_b_conv_26_conv);
216 ret_conv_46_b_conv_26_arr[a] = ret_conv_46_b_conv_26_conv;
218 TwoTuple<byte[], TwoTuple<Integer, TxOut>[]> ret_conv_46_conv = new TwoTuple<byte[], TwoTuple<Integer, TxOut>[]>(ret_conv_46_a, ret_conv_46_b_conv_26_arr);
219 // Warning: We may not free the C tuple object!
220 ret_conv_46_arr[u] = ret_conv_46_conv;
222 /* TODO 2 TwoTuple<Long, byte[]> */;
223 this.ptrs_to.add(broadcaster);
224 this.ptrs_to.add(fee_estimator);
225 this.ptrs_to.add(logger);
226 return ret_conv_46_arr;
230 * Determines if the disconnected block contained any transactions of interest and updates
233 public void block_disconnected(byte[] header, int height, BroadcasterInterface broadcaster, FeeEstimator fee_estimator, Logger logger) {
234 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);
235 this.ptrs_to.add(broadcaster);
236 this.ptrs_to.add(fee_estimator);
237 this.ptrs_to.add(logger);
241 * Processes transactions confirmed in a block with the given header and height, returning new
242 * outputs to watch. See [`block_connected`] for details.
244 * Used instead of [`block_connected`] by clients that are notified of transactions rather than
245 * blocks. May be called before or after [`update_best_block`] for transactions in the
246 * corresponding block. See [`update_best_block`] for further calling expectations.
248 * [`block_connected`]: Self::block_connected
249 * [`update_best_block`]: Self::update_best_block
251 public TwoTuple<byte[], TwoTuple<Integer, TxOut>[]>[] transactions_confirmed(byte[] header, TwoTuple<Long, byte[]>[] txdata, int height, BroadcasterInterface broadcaster, FeeEstimator fee_estimator, Logger logger) {
252 long[] ret = bindings.ChannelMonitor_transactions_confirmed(this.ptr, header, Arrays.stream(txdata).mapToLong(txdata_conv_24 -> bindings.C2Tuple_usizeTransactionZ_new(txdata_conv_24.a, txdata_conv_24.b)).toArray(), height, broadcaster == null ? 0 : broadcaster.ptr, fee_estimator == null ? 0 : fee_estimator.ptr, logger == null ? 0 : logger.ptr);
253 TwoTuple<byte[], TwoTuple<Integer, TxOut>[]>[] ret_conv_46_arr = new TwoTuple[ret.length];
254 for (int u = 0; u < ret.length; u++) {
255 long ret_conv_46 = ret[u];
256 byte[] ret_conv_46_a = bindings.LDKC2Tuple_TxidCVec_C2Tuple_u32TxOutZZZ_get_a(ret_conv_46);
257 long[] ret_conv_46_b = bindings.LDKC2Tuple_TxidCVec_C2Tuple_u32TxOutZZZ_get_b(ret_conv_46);
258 TwoTuple<Integer, TxOut>[] ret_conv_46_b_conv_26_arr = new TwoTuple[ret_conv_46_b.length];
259 for (int a = 0; a < ret_conv_46_b.length; a++) {
260 long ret_conv_46_b_conv_26 = ret_conv_46_b[a];
261 int ret_conv_46_b_conv_26_a = bindings.LDKC2Tuple_u32TxOutZ_get_a(ret_conv_46_b_conv_26);
262 long ret_conv_46_b_conv_26_b = bindings.TxOut_clone(bindings.LDKC2Tuple_u32TxOutZ_get_b(ret_conv_46_b_conv_26));
263 TxOut ret_conv_46_b_conv_26_b_conv = new TxOut(null, ret_conv_46_b_conv_26_b);;
264 TwoTuple<Integer, TxOut> ret_conv_46_b_conv_26_conv = new TwoTuple<Integer, TxOut>(ret_conv_46_b_conv_26_a, ret_conv_46_b_conv_26_b_conv, () -> {
265 bindings.C2Tuple_u32TxOutZ_free(ret_conv_46_b_conv_26);
267 ret_conv_46_b_conv_26_b_conv.ptrs_to.add(ret_conv_46_b_conv_26_conv);
268 ret_conv_46_b_conv_26_arr[a] = ret_conv_46_b_conv_26_conv;
270 TwoTuple<byte[], TwoTuple<Integer, TxOut>[]> ret_conv_46_conv = new TwoTuple<byte[], TwoTuple<Integer, TxOut>[]>(ret_conv_46_a, ret_conv_46_b_conv_26_arr);
271 // Warning: We may not free the C tuple object!
272 ret_conv_46_arr[u] = ret_conv_46_conv;
274 /* TODO 2 TwoTuple<Long, byte[]> */;
275 this.ptrs_to.add(broadcaster);
276 this.ptrs_to.add(fee_estimator);
277 this.ptrs_to.add(logger);
278 return ret_conv_46_arr;
282 * Processes a transaction that was reorganized out of the chain.
284 * Used instead of [`block_disconnected`] by clients that are notified of transactions rather
285 * than blocks. May be called before or after [`update_best_block`] for transactions in the
286 * corresponding block. See [`update_best_block`] for further calling expectations.
288 * [`block_disconnected`]: Self::block_disconnected
289 * [`update_best_block`]: Self::update_best_block
291 public void transaction_unconfirmed(byte[] txid, BroadcasterInterface broadcaster, FeeEstimator fee_estimator, Logger logger) {
292 bindings.ChannelMonitor_transaction_unconfirmed(this.ptr, txid, broadcaster == null ? 0 : broadcaster.ptr, fee_estimator == null ? 0 : fee_estimator.ptr, logger == null ? 0 : logger.ptr);
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. May be called before or after [`transactions_confirmed`] for the corresponding
306 * Must be called after new blocks become available for the most recent block. Intermediary
307 * blocks, however, may be safely skipped. In the event of a chain re-organization, this only
308 * needs to be called for the most recent block assuming `transaction_unconfirmed` is called
309 * for any affected transactions.
311 * [`block_connected`]: Self::block_connected
312 * [`transactions_confirmed`]: Self::transactions_confirmed
313 * [`transaction_unconfirmed`]: Self::transaction_unconfirmed
315 public TwoTuple<byte[], TwoTuple<Integer, TxOut>[]>[] update_best_block(byte[] header, int height, BroadcasterInterface broadcaster, FeeEstimator fee_estimator, Logger logger) {
316 long[] ret = bindings.ChannelMonitor_update_best_block(this.ptr, header, height, broadcaster == null ? 0 : broadcaster.ptr, fee_estimator == null ? 0 : fee_estimator.ptr, logger == null ? 0 : logger.ptr);
317 TwoTuple<byte[], TwoTuple<Integer, TxOut>[]>[] ret_conv_46_arr = new TwoTuple[ret.length];
318 for (int u = 0; u < ret.length; u++) {
319 long ret_conv_46 = ret[u];
320 byte[] ret_conv_46_a = bindings.LDKC2Tuple_TxidCVec_C2Tuple_u32TxOutZZZ_get_a(ret_conv_46);
321 long[] ret_conv_46_b = bindings.LDKC2Tuple_TxidCVec_C2Tuple_u32TxOutZZZ_get_b(ret_conv_46);
322 TwoTuple<Integer, TxOut>[] ret_conv_46_b_conv_26_arr = new TwoTuple[ret_conv_46_b.length];
323 for (int a = 0; a < ret_conv_46_b.length; a++) {
324 long ret_conv_46_b_conv_26 = ret_conv_46_b[a];
325 int ret_conv_46_b_conv_26_a = bindings.LDKC2Tuple_u32TxOutZ_get_a(ret_conv_46_b_conv_26);
326 long ret_conv_46_b_conv_26_b = bindings.TxOut_clone(bindings.LDKC2Tuple_u32TxOutZ_get_b(ret_conv_46_b_conv_26));
327 TxOut ret_conv_46_b_conv_26_b_conv = new TxOut(null, ret_conv_46_b_conv_26_b);;
328 TwoTuple<Integer, TxOut> ret_conv_46_b_conv_26_conv = new TwoTuple<Integer, TxOut>(ret_conv_46_b_conv_26_a, ret_conv_46_b_conv_26_b_conv, () -> {
329 bindings.C2Tuple_u32TxOutZ_free(ret_conv_46_b_conv_26);
331 ret_conv_46_b_conv_26_b_conv.ptrs_to.add(ret_conv_46_b_conv_26_conv);
332 ret_conv_46_b_conv_26_arr[a] = ret_conv_46_b_conv_26_conv;
334 TwoTuple<byte[], TwoTuple<Integer, TxOut>[]> ret_conv_46_conv = new TwoTuple<byte[], TwoTuple<Integer, TxOut>[]>(ret_conv_46_a, ret_conv_46_b_conv_26_arr);
335 // Warning: We may not free the C tuple object!
336 ret_conv_46_arr[u] = ret_conv_46_conv;
338 this.ptrs_to.add(broadcaster);
339 this.ptrs_to.add(fee_estimator);
340 this.ptrs_to.add(logger);
341 return ret_conv_46_arr;
345 * Returns the set of txids that should be monitored for re-organization out of the chain.
347 public byte[][] get_relevant_txids() {
348 byte[][] ret = bindings.ChannelMonitor_get_relevant_txids(this.ptr);