X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fldk%2Fstructs%2FChannelMonitor.java;h=32d870a6a27dbcc9bc2f5836ca1cd4986da6ad93;hb=6f84978154512148a47502caad48038e3f264392;hp=b484c92fbadca6ec6c61badaea29c315b2e10823;hpb=0ddbeb5d1cddaa1d89cad2a63548434b2e1e227a;p=ldk-java diff --git a/src/main/java/org/ldk/structs/ChannelMonitor.java b/src/main/java/org/ldk/structs/ChannelMonitor.java index b484c92f..32d870a6 100644 --- a/src/main/java/org/ldk/structs/ChannelMonitor.java +++ b/src/main/java/org/ldk/structs/ChannelMonitor.java @@ -5,6 +5,25 @@ import org.ldk.enums.*; import org.ldk.util.*; import java.util.Arrays; + +/** + * A ChannelMonitor handles chain events (blocks connected and disconnected) and generates + * on-chain transactions to ensure no loss of funds occurs. + * + * You MUST ensure that no ChannelMonitors for a given channel anywhere contain out-of-date + * information and are actively monitoring the chain. + * + * Pending Events or updated HTLCs which have not yet been read out by + * get_and_clear_pending_monitor_events or get_and_clear_pending_events are serialized to disk and + * reloaded at deserialize-time. Thus, you must ensure that, when handling events, all events + * gotten are fully handled before re-serializing the new state. + * + * Note that the deserializer is only implemented for (BlockHash, ChannelMonitor), which + * tells you the last block hash which was block_connect()ed. You MUST rescan any blocks along + * the \"reorg path\" (ie disconnecting blocks until you find a common ancestor from both the + * returned block hash and the the current chain and then reconnecting blocks to get to the + * best chain) upon deserializing the object! + */ @SuppressWarnings("unchecked") // We correctly assign various generic arrays public class ChannelMonitor extends CommonBase { ChannelMonitor(Object _dummy, long ptr) { super(ptr); } @@ -14,80 +33,203 @@ public class ChannelMonitor extends CommonBase { if (ptr != 0) { bindings.ChannelMonitor_free(ptr); } } - public Result_NoneMonitorUpdateErrorZ update_monitor(ChannelMonitorUpdate updates, BroadcasterInterface broadcaster, Logger logger) { - long ret = bindings.ChannelMonitor_update_monitor(this.ptr, updates == null ? 0 : updates.ptr & ~1, broadcaster == null ? 0 : broadcaster.ptr, logger == null ? 0 : logger.ptr); - Result_NoneMonitorUpdateErrorZ ret_hu_conv = Result_NoneMonitorUpdateErrorZ.constr_from_ptr(ret); + /** + * Creates a copy of the ChannelMonitor + */ + public ChannelMonitor clone() { + long ret = bindings.ChannelMonitor_clone(this.ptr); + ChannelMonitor ret_hu_conv = new ChannelMonitor(null, ret); ret_hu_conv.ptrs_to.add(this); + return ret_hu_conv; + } + + /** + * Serialize the ChannelMonitor object into a byte array which can be read by ChannelMonitor_read + */ + public byte[] write() { + byte[] ret = bindings.ChannelMonitor_write(this.ptr); + return ret; + } + + /** + * Updates a ChannelMonitor on the basis of some new information provided by the Channel + * itself. + * + * panics if the given update is not the next update by update_id. + */ + public Result_NoneMonitorUpdateErrorZ update_monitor(ChannelMonitorUpdate updates, BroadcasterInterface broadcaster, FeeEstimator fee_estimator, Logger logger) { + 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); + Result_NoneMonitorUpdateErrorZ ret_hu_conv = Result_NoneMonitorUpdateErrorZ.constr_from_ptr(ret); this.ptrs_to.add(updates); this.ptrs_to.add(broadcaster); + this.ptrs_to.add(fee_estimator); this.ptrs_to.add(logger); return ret_hu_conv; } + /** + * Gets the update_id from the latest ChannelMonitorUpdate which was applied to this + * ChannelMonitor. + */ public long get_latest_update_id() { long ret = bindings.ChannelMonitor_get_latest_update_id(this.ptr); return ret; } - // Skipped ChannelMonitor_get_funding_txo + /** + * Gets the funding transaction outpoint of the channel this ChannelMonitor is monitoring for. + */ + public TwoTuple get_funding_txo() { + long ret = bindings.ChannelMonitor_get_funding_txo(this.ptr); + long ret_a = bindings.LDKC2Tuple_OutPointScriptZ_get_a(ret); + OutPoint ret_a_hu_conv = new OutPoint(null, ret_a); + ret_a_hu_conv.ptrs_to.add(this);; + byte[] ret_b = bindings.LDKC2Tuple_OutPointScriptZ_get_b(ret); + TwoTuple ret_conv = new TwoTuple(ret_a_hu_conv, ret_b, () -> { + bindings.C2Tuple_OutPointScriptZ_free(ret); + }); + ret_a_hu_conv.ptrs_to.add(ret_conv); + return ret_conv; + } + + /** + * Gets a list of txids, with their output scripts (in the order they appear in the + * transaction), which we must learn about spends of via block_connected(). + */ + public TwoTuple[]>[] get_outputs_to_watch() { + long[] ret = bindings.ChannelMonitor_get_outputs_to_watch(this.ptr); + TwoTuple[]>[] ret_conv_47_arr = new TwoTuple[ret.length]; + for (int v = 0; v < ret.length; v++) { + long ret_conv_47 = ret[v]; + byte[] ret_conv_47_a = bindings.LDKC2Tuple_TxidCVec_C2Tuple_u32ScriptZZZ_get_a(ret_conv_47); + long[] ret_conv_47_b = bindings.LDKC2Tuple_TxidCVec_C2Tuple_u32ScriptZZZ_get_b(ret_conv_47); + TwoTuple[] ret_conv_47_b_conv_27_arr = new TwoTuple[ret_conv_47_b.length]; + for (int b = 0; b < ret_conv_47_b.length; b++) { + long ret_conv_47_b_conv_27 = ret_conv_47_b[b]; + int ret_conv_47_b_conv_27_a = bindings.LDKC2Tuple_u32ScriptZ_get_a(ret_conv_47_b_conv_27); + byte[] ret_conv_47_b_conv_27_b = bindings.LDKC2Tuple_u32ScriptZ_get_b(ret_conv_47_b_conv_27); + TwoTuple ret_conv_47_b_conv_27_conv = new TwoTuple(ret_conv_47_b_conv_27_a, ret_conv_47_b_conv_27_b, () -> { + bindings.C2Tuple_u32ScriptZ_free(ret_conv_47_b_conv_27); + }); + ret_conv_47_b_conv_27_arr[b] = ret_conv_47_b_conv_27_conv; + }; + TwoTuple[]> ret_conv_47_conv = new TwoTuple[]>(ret_conv_47_a, ret_conv_47_b_conv_27_arr); + // Warning: We may not free the C tuple object! + ret_conv_47_arr[v] = ret_conv_47_conv; + } + return ret_conv_47_arr; + } + + /** + * Loads the funding txo and outputs to watch into the given `chain::Filter` by repeatedly + * calling `chain::Filter::register_output` and `chain::Filter::register_tx` until all outputs + * have been registered. + */ + public void load_outputs_to_watch(Filter filter) { + bindings.ChannelMonitor_load_outputs_to_watch(this.ptr, filter == null ? 0 : filter.ptr); + this.ptrs_to.add(filter); + } + + /** + * Get the list of HTLCs who's status has been updated on chain. This should be called by + * ChannelManager via [`chain::Watch::release_pending_monitor_events`]. + */ public MonitorEvent[] get_and_clear_pending_monitor_events() { long[] ret = bindings.ChannelMonitor_get_and_clear_pending_monitor_events(this.ptr); - MonitorEvent[] arr_conv_14_arr = new MonitorEvent[ret.length]; + MonitorEvent[] ret_conv_14_arr = new MonitorEvent[ret.length]; for (int o = 0; o < ret.length; o++) { - long arr_conv_14 = ret[o]; - MonitorEvent arr_conv_14_hu_conv = new MonitorEvent(null, arr_conv_14); - arr_conv_14_arr[o] = arr_conv_14_hu_conv; + long ret_conv_14 = ret[o]; + MonitorEvent ret_conv_14_hu_conv = MonitorEvent.constr_from_ptr(ret_conv_14); + ret_conv_14_hu_conv.ptrs_to.add(this); + ret_conv_14_arr[o] = ret_conv_14_hu_conv; } - return arr_conv_14_arr; + return ret_conv_14_arr; } + /** + * Gets the list of pending events which were generated by previous actions, clearing the list + * in the process. + * + * This is called by ChainMonitor::get_and_clear_pending_events() and is equivalent to + * EventsProvider::get_and_clear_pending_events() except that it requires &mut self as we do + * no internal locking in ChannelMonitors. + */ public Event[] get_and_clear_pending_events() { long[] ret = bindings.ChannelMonitor_get_and_clear_pending_events(this.ptr); - Event[] arr_conv_7_arr = new Event[ret.length]; + Event[] ret_conv_7_arr = new Event[ret.length]; for (int h = 0; h < ret.length; h++) { - long arr_conv_7 = ret[h]; - Event arr_conv_7_hu_conv = Event.constr_from_ptr(arr_conv_7); - arr_conv_7_hu_conv.ptrs_to.add(this); - arr_conv_7_arr[h] = arr_conv_7_hu_conv; + long ret_conv_7 = ret[h]; + Event ret_conv_7_hu_conv = Event.constr_from_ptr(ret_conv_7); + ret_conv_7_hu_conv.ptrs_to.add(this); + ret_conv_7_arr[h] = ret_conv_7_hu_conv; } - return arr_conv_7_arr; + return ret_conv_7_arr; } - public Transaction[] get_latest_holder_commitment_txn(Logger logger) { - long[] ret = bindings.ChannelMonitor_get_latest_holder_commitment_txn(this.ptr, logger == null ? 0 : logger.ptr); - Transaction[] arr_conv_13_arr = new Transaction[ret.length]; - for (int n = 0; n < ret.length; n++) { - long arr_conv_13 = ret[n]; - Transaction arr_conv_13_conv = new Transaction(null, arr_conv_13); - arr_conv_13_arr[n] = arr_conv_13_conv; - } + /** + * Used by ChannelManager deserialization to broadcast the latest holder state if its copy of + * the Channel was out-of-date. You may use it to get a broadcastable holder toxic tx in case of + * fallen-behind, i.e when receiving a channel_reestablish with a proof that our counterparty side knows + * a higher revocation secret than the holder commitment number we are aware of. Broadcasting these + * transactions are UNSAFE, as they allow counterparty side to punish you. Nevertheless you may want to + * broadcast them if counterparty don't close channel with his higher commitment transaction after a + * substantial amount of time (a month or even a year) to get back funds. Best may be to contact + * out-of-band the other node operator to coordinate with him if option is available to you. + * In any-case, choice is up to the user. + */ + public byte[][] get_latest_holder_commitment_txn(Logger logger) { + byte[][] ret = bindings.ChannelMonitor_get_latest_holder_commitment_txn(this.ptr, logger == null ? 0 : logger.ptr); this.ptrs_to.add(logger); - return arr_conv_13_arr; - } - - public TwoTuple[] block_connected(byte[] header, TwoTuple[] txdata, int height, BroadcasterInterface broadcaster, FeeEstimator fee_estimator, Logger logger) { - long[] ret = bindings.ChannelMonitor_block_connected(this.ptr, header, Arrays.stream(txdata).mapToLong(arr_conv_29 -> bindings.C2Tuple_usizeTransactionZ_new(arr_conv_29.a, arr_conv_29.b.ptr)).toArray(), height, broadcaster == null ? 0 : broadcaster.ptr, fee_estimator == null ? 0 : fee_estimator.ptr, logger == null ? 0 : logger.ptr); - TwoTuple[] arr_conv_27_arr = new TwoTuple[ret.length]; - for (int b = 0; b < ret.length; b++) { - long arr_conv_27 = ret[b]; - byte[] arr_conv_27_a = bindings.LDKC2Tuple_TxidCVec_TxOutZZ_get_a(arr_conv_27); - long[] arr_conv_27_b = bindings.LDKC2Tuple_TxidCVec_TxOutZZ_get_b(arr_conv_27); - TxOut[] arr_conv_7_arr = new TxOut[arr_conv_27_b.length]; - for (int h = 0; h < arr_conv_27_b.length; h++) { - long arr_conv_7 = arr_conv_27_b[h]; - TxOut arr_conv_7_conv = new TxOut(null, arr_conv_7); - arr_conv_7_arr[h] = arr_conv_7_conv; + return ret; + } + + /** + * Processes transactions in a newly connected block, which may result in any of the following: + * - update the monitor's state against resolved HTLCs + * - punish the counterparty in the case of seeing a revoked commitment transaction + * - force close the channel and claim/timeout incoming/outgoing HTLCs if near expiration + * - detect settled outputs for later spending + * - schedule and bump any in-flight claims + * + * Returns any new outputs to watch from `txdata`; after called, these are also included in + * [`get_outputs_to_watch`]. + * + * [`get_outputs_to_watch`]: #method.get_outputs_to_watch + */ + public TwoTuple[]>[] block_connected(byte[] header, TwoTuple[] txdata, int height, BroadcasterInterface broadcaster, FeeEstimator fee_estimator, Logger logger) { + 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); + TwoTuple[]>[] ret_conv_46_arr = new TwoTuple[ret.length]; + for (int u = 0; u < ret.length; u++) { + long ret_conv_46 = ret[u]; + byte[] ret_conv_46_a = bindings.LDKC2Tuple_TxidCVec_C2Tuple_u32TxOutZZZ_get_a(ret_conv_46); + long[] ret_conv_46_b = bindings.LDKC2Tuple_TxidCVec_C2Tuple_u32TxOutZZZ_get_b(ret_conv_46); + TwoTuple[] ret_conv_46_b_conv_26_arr = new TwoTuple[ret_conv_46_b.length]; + for (int a = 0; a < ret_conv_46_b.length; a++) { + long ret_conv_46_b_conv_26 = ret_conv_46_b[a]; + int ret_conv_46_b_conv_26_a = bindings.LDKC2Tuple_u32TxOutZ_get_a(ret_conv_46_b_conv_26); + long ret_conv_46_b_conv_26_b = bindings.TxOut_clone(bindings.LDKC2Tuple_u32TxOutZ_get_b(ret_conv_46_b_conv_26)); + TxOut ret_conv_46_b_conv_26_b_conv = new TxOut(null, ret_conv_46_b_conv_26_b);; + TwoTuple ret_conv_46_b_conv_26_conv = new TwoTuple(ret_conv_46_b_conv_26_a, ret_conv_46_b_conv_26_b_conv, () -> { + bindings.C2Tuple_u32TxOutZ_free(ret_conv_46_b_conv_26); + }); + ret_conv_46_b_conv_26_b_conv.ptrs_to.add(ret_conv_46_b_conv_26_conv); + ret_conv_46_b_conv_26_arr[a] = ret_conv_46_b_conv_26_conv; }; - TwoTuple arr_conv_27_conv = new TwoTuple(arr_conv_27_a, arr_conv_7_arr); - arr_conv_27_arr[b] = arr_conv_27_conv; + TwoTuple[]> ret_conv_46_conv = new TwoTuple[]>(ret_conv_46_a, ret_conv_46_b_conv_26_arr); + // Warning: We may not free the C tuple object! + ret_conv_46_arr[u] = ret_conv_46_conv; } - /* TODO 2 TwoTuple */; + /* TODO 2 TwoTuple */; this.ptrs_to.add(broadcaster); this.ptrs_to.add(fee_estimator); this.ptrs_to.add(logger); - return arr_conv_27_arr; + return ret_conv_46_arr; } + /** + * Determines if the disconnected block contained any transactions of interest and updates + * appropriately. + */ public void block_disconnected(byte[] header, int height, BroadcasterInterface broadcaster, FeeEstimator fee_estimator, Logger logger) { 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); this.ptrs_to.add(broadcaster); @@ -95,4 +237,116 @@ public class ChannelMonitor extends CommonBase { this.ptrs_to.add(logger); } + /** + * Processes transactions confirmed in a block with the given header and height, returning new + * outputs to watch. See [`block_connected`] for details. + * + * Used instead of [`block_connected`] by clients that are notified of transactions rather than + * blocks. May be called before or after [`update_best_block`] for transactions in the + * corresponding block. See [`update_best_block`] for further calling expectations. + * + * [`block_connected`]: Self::block_connected + * [`update_best_block`]: Self::update_best_block + */ + public TwoTuple[]>[] transactions_confirmed(byte[] header, TwoTuple[] txdata, int height, BroadcasterInterface broadcaster, FeeEstimator fee_estimator, Logger logger) { + 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); + TwoTuple[]>[] ret_conv_46_arr = new TwoTuple[ret.length]; + for (int u = 0; u < ret.length; u++) { + long ret_conv_46 = ret[u]; + byte[] ret_conv_46_a = bindings.LDKC2Tuple_TxidCVec_C2Tuple_u32TxOutZZZ_get_a(ret_conv_46); + long[] ret_conv_46_b = bindings.LDKC2Tuple_TxidCVec_C2Tuple_u32TxOutZZZ_get_b(ret_conv_46); + TwoTuple[] ret_conv_46_b_conv_26_arr = new TwoTuple[ret_conv_46_b.length]; + for (int a = 0; a < ret_conv_46_b.length; a++) { + long ret_conv_46_b_conv_26 = ret_conv_46_b[a]; + int ret_conv_46_b_conv_26_a = bindings.LDKC2Tuple_u32TxOutZ_get_a(ret_conv_46_b_conv_26); + long ret_conv_46_b_conv_26_b = bindings.TxOut_clone(bindings.LDKC2Tuple_u32TxOutZ_get_b(ret_conv_46_b_conv_26)); + TxOut ret_conv_46_b_conv_26_b_conv = new TxOut(null, ret_conv_46_b_conv_26_b);; + TwoTuple ret_conv_46_b_conv_26_conv = new TwoTuple(ret_conv_46_b_conv_26_a, ret_conv_46_b_conv_26_b_conv, () -> { + bindings.C2Tuple_u32TxOutZ_free(ret_conv_46_b_conv_26); + }); + ret_conv_46_b_conv_26_b_conv.ptrs_to.add(ret_conv_46_b_conv_26_conv); + ret_conv_46_b_conv_26_arr[a] = ret_conv_46_b_conv_26_conv; + }; + TwoTuple[]> ret_conv_46_conv = new TwoTuple[]>(ret_conv_46_a, ret_conv_46_b_conv_26_arr); + // Warning: We may not free the C tuple object! + ret_conv_46_arr[u] = ret_conv_46_conv; + } + /* TODO 2 TwoTuple */; + this.ptrs_to.add(broadcaster); + this.ptrs_to.add(fee_estimator); + this.ptrs_to.add(logger); + return ret_conv_46_arr; + } + + /** + * Processes a transaction that was reorganized out of the chain. + * + * Used instead of [`block_disconnected`] by clients that are notified of transactions rather + * than blocks. May be called before or after [`update_best_block`] for transactions in the + * corresponding block. See [`update_best_block`] for further calling expectations. + * + * [`block_disconnected`]: Self::block_disconnected + * [`update_best_block`]: Self::update_best_block + */ + public void transaction_unconfirmed(byte[] txid, BroadcasterInterface broadcaster, FeeEstimator fee_estimator, Logger logger) { + bindings.ChannelMonitor_transaction_unconfirmed(this.ptr, txid, broadcaster == null ? 0 : broadcaster.ptr, fee_estimator == null ? 0 : fee_estimator.ptr, logger == null ? 0 : logger.ptr); + this.ptrs_to.add(broadcaster); + this.ptrs_to.add(fee_estimator); + this.ptrs_to.add(logger); + } + + /** + * Updates the monitor with the current best chain tip, returning new outputs to watch. See + * [`block_connected`] for details. + * + * Used instead of [`block_connected`] by clients that are notified of transactions rather than + * blocks. May be called before or after [`transactions_confirmed`] for the corresponding + * block. + * + * Must be called after new blocks become available for the most recent block. Intermediary + * blocks, however, may be safely skipped. In the event of a chain re-organization, this only + * needs to be called for the most recent block assuming `transaction_unconfirmed` is called + * for any affected transactions. + * + * [`block_connected`]: Self::block_connected + * [`transactions_confirmed`]: Self::transactions_confirmed + * [`transaction_unconfirmed`]: Self::transaction_unconfirmed + */ + public TwoTuple[]>[] update_best_block(byte[] header, int height, BroadcasterInterface broadcaster, FeeEstimator fee_estimator, Logger logger) { + 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); + TwoTuple[]>[] ret_conv_46_arr = new TwoTuple[ret.length]; + for (int u = 0; u < ret.length; u++) { + long ret_conv_46 = ret[u]; + byte[] ret_conv_46_a = bindings.LDKC2Tuple_TxidCVec_C2Tuple_u32TxOutZZZ_get_a(ret_conv_46); + long[] ret_conv_46_b = bindings.LDKC2Tuple_TxidCVec_C2Tuple_u32TxOutZZZ_get_b(ret_conv_46); + TwoTuple[] ret_conv_46_b_conv_26_arr = new TwoTuple[ret_conv_46_b.length]; + for (int a = 0; a < ret_conv_46_b.length; a++) { + long ret_conv_46_b_conv_26 = ret_conv_46_b[a]; + int ret_conv_46_b_conv_26_a = bindings.LDKC2Tuple_u32TxOutZ_get_a(ret_conv_46_b_conv_26); + long ret_conv_46_b_conv_26_b = bindings.TxOut_clone(bindings.LDKC2Tuple_u32TxOutZ_get_b(ret_conv_46_b_conv_26)); + TxOut ret_conv_46_b_conv_26_b_conv = new TxOut(null, ret_conv_46_b_conv_26_b);; + TwoTuple ret_conv_46_b_conv_26_conv = new TwoTuple(ret_conv_46_b_conv_26_a, ret_conv_46_b_conv_26_b_conv, () -> { + bindings.C2Tuple_u32TxOutZ_free(ret_conv_46_b_conv_26); + }); + ret_conv_46_b_conv_26_b_conv.ptrs_to.add(ret_conv_46_b_conv_26_conv); + ret_conv_46_b_conv_26_arr[a] = ret_conv_46_b_conv_26_conv; + }; + TwoTuple[]> ret_conv_46_conv = new TwoTuple[]>(ret_conv_46_a, ret_conv_46_b_conv_26_arr); + // Warning: We may not free the C tuple object! + ret_conv_46_arr[u] = ret_conv_46_conv; + } + this.ptrs_to.add(broadcaster); + this.ptrs_to.add(fee_estimator); + this.ptrs_to.add(logger); + return ret_conv_46_arr; + } + + /** + * Returns the set of txids that should be monitored for re-organization out of the chain. + */ + public byte[][] get_relevant_txids() { + byte[][] ret = bindings.ChannelMonitor_get_relevant_txids(this.ptr); + return ret; + } + }