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;
11 * The `Confirm` trait is used to notify LDK when relevant transactions have been confirmed on
12 * chain or unconfirmed during a chain reorganization.
14 * Clients sourcing chain data using a transaction-oriented API should prefer this interface over
15 * [`Listen`]. For instance, an Electrum-based transaction sync implementation may implement
16 * [`Filter`] to subscribe to relevant transactions and unspent outputs it should monitor for
17 * on-chain activity. Then, it needs to notify LDK via this interface upon observing any changes
18 * with reference to the confirmation status of the monitored objects.
21 * The intended use is as follows:
22 * - Call [`transactions_confirmed`] to notify LDK whenever any of the registered transactions or
23 * outputs are, respectively, confirmed or spent on chain.
24 * - Call [`transaction_unconfirmed`] to notify LDK whenever any transaction returned by
25 * [`get_relevant_txids`] is no longer confirmed in the block with the given block hash.
26 * - Call [`best_block_updated`] to notify LDK whenever a new chain tip becomes available.
30 * Clients must call these methods in chain order. Specifically:
31 * - Transactions which are confirmed in a particular block must be given before transactions
32 * confirmed in a later block.
33 * - Dependent transactions within the same block must be given in topological order, possibly in
35 * - All unconfirmed transactions must be given after the original confirmations and before *any*
36 * reconfirmations, i.e., [`transactions_confirmed`] and [`transaction_unconfirmed`] calls should
37 * never be interleaved, but always conduced *en bloc*.
38 * - Any reconfirmed transactions need to be explicitly unconfirmed before they are reconfirmed
39 * in regard to the new block.
41 * See individual method documentation for further details.
43 * [`transactions_confirmed`]: Self::transactions_confirmed
44 * [`transaction_unconfirmed`]: Self::transaction_unconfirmed
45 * [`best_block_updated`]: Self::best_block_updated
46 * [`get_relevant_txids`]: Self::get_relevant_txids
48 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
49 public class Confirm extends CommonBase {
50 final bindings.LDKConfirm bindings_instance;
51 Confirm(Object _dummy, long ptr) { super(ptr); bindings_instance = null; }
52 private Confirm(bindings.LDKConfirm arg) {
53 super(bindings.LDKConfirm_new(arg));
54 this.ptrs_to.add(arg);
55 this.bindings_instance = arg;
57 @Override @SuppressWarnings("deprecation")
58 protected void finalize() throws Throwable {
59 if (ptr != 0) { bindings.Confirm_free(ptr); } super.finalize();
62 * Destroys the object, freeing associated resources. After this call, any access
63 * to this object may result in a SEGFAULT or worse.
65 * You should generally NEVER call this method. You should let the garbage collector
66 * do this for you when it finalizes objects. However, it may be useful for types
67 * which represent locks and should be closed immediately to avoid holding locks
70 public void destroy() {
71 if (ptr != 0) { bindings.Confirm_free(ptr); }
74 public static interface ConfirmInterface {
76 * Notifies LDK of transactions confirmed in a block with a given header and height.
78 * Must be called for any transactions registered by [`Filter::register_tx`] or any
79 * transactions spending an output registered by [`Filter::register_output`]. Such transactions
80 * appearing in the same block do not need to be included in the same call; instead, multiple
81 * calls with additional transactions may be made so long as they are made in [chain order].
83 * May be called before or after [`best_block_updated`] for the corresponding block. However,
84 * in the event of a chain reorganization, it must not be called with a `header` that is no
85 * longer in the chain as of the last call to [`best_block_updated`].
87 * [chain order]: Confirm#order
88 * [`best_block_updated`]: Self::best_block_updated
90 void transactions_confirmed(byte[] header, TwoTuple_usizeTransactionZ[] txdata, int height);
92 * Notifies LDK of a transaction that is no longer confirmed as result of a chain reorganization.
94 * Must be called for any transaction returned by [`get_relevant_txids`] if it has been
95 * reorganized out of the best chain or if it is no longer confirmed in the block with the
96 * given block hash. Once called, the given transaction will not be returned
97 * by [`get_relevant_txids`], unless it has been reconfirmed via [`transactions_confirmed`].
99 * [`get_relevant_txids`]: Self::get_relevant_txids
100 * [`transactions_confirmed`]: Self::transactions_confirmed
102 void transaction_unconfirmed(byte[] txid);
104 * Notifies LDK of an update to the best header connected at the given height.
106 * Must be called whenever a new chain tip becomes available. May be skipped for intermediary
109 void best_block_updated(byte[] header, int height);
111 * Returns transactions that must be monitored for reorganization out of the chain along
112 * with the hash of the block as part of which it had been previously confirmed.
114 * Note that the returned `Option<BlockHash>` might be `None` for channels created with LDK
115 * 0.0.112 and prior, in which case you need to manually track previous confirmations.
117 * Will include any transactions passed to [`transactions_confirmed`] that have insufficient
118 * confirmations to be safe from a chain reorganization. Will not include any transactions
119 * passed to [`transaction_unconfirmed`], unless later reconfirmed.
121 * Must be called to determine the subset of transactions that must be monitored for
122 * reorganization. Will be idempotent between calls but may change as a result of calls to the
123 * other interface methods. Thus, this is useful to determine which transactions must be
124 * given to [`transaction_unconfirmed`].
126 * If any of the returned transactions are confirmed in a block other than the one with the
127 * given hash, they need to be unconfirmed and reconfirmed via [`transaction_unconfirmed`] and
128 * [`transactions_confirmed`], respectively.
130 * [`transactions_confirmed`]: Self::transactions_confirmed
131 * [`transaction_unconfirmed`]: Self::transaction_unconfirmed
133 TwoTuple_ThirtyTwoBytesCOption_ThirtyTwoBytesZZ[] get_relevant_txids();
135 private static class LDKConfirmHolder { Confirm held; }
136 public static Confirm new_impl(ConfirmInterface arg) {
137 final LDKConfirmHolder impl_holder = new LDKConfirmHolder();
138 impl_holder.held = new Confirm(new bindings.LDKConfirm() {
139 @Override public void transactions_confirmed(byte[] header, long[] txdata, int height) {
140 int txdata_conv_28_len = txdata.length;
141 TwoTuple_usizeTransactionZ[] txdata_conv_28_arr = new TwoTuple_usizeTransactionZ[txdata_conv_28_len];
142 for (int c = 0; c < txdata_conv_28_len; c++) {
143 long txdata_conv_28 = txdata[c];
144 TwoTuple_usizeTransactionZ txdata_conv_28_hu_conv = new TwoTuple_usizeTransactionZ(null, txdata_conv_28);
145 if (txdata_conv_28_hu_conv != null) { txdata_conv_28_hu_conv.ptrs_to.add(this); };
146 txdata_conv_28_arr[c] = txdata_conv_28_hu_conv;
148 arg.transactions_confirmed(header, txdata_conv_28_arr, height);
149 Reference.reachabilityFence(arg);
151 @Override public void transaction_unconfirmed(byte[] txid) {
152 arg.transaction_unconfirmed(txid);
153 Reference.reachabilityFence(arg);
155 @Override public void best_block_updated(byte[] header, int height) {
156 arg.best_block_updated(header, height);
157 Reference.reachabilityFence(arg);
159 @Override public long[] get_relevant_txids() {
160 TwoTuple_ThirtyTwoBytesCOption_ThirtyTwoBytesZZ[] ret = arg.get_relevant_txids();
161 Reference.reachabilityFence(arg);
162 long[] result = ret != null ? Arrays.stream(ret).mapToLong(ret_conv_49 -> ret_conv_49 == null ? 0 : ret_conv_49.clone_ptr()).toArray() : null;
166 return impl_holder.held;
169 * Notifies LDK of transactions confirmed in a block with a given header and height.
171 * Must be called for any transactions registered by [`Filter::register_tx`] or any
172 * transactions spending an output registered by [`Filter::register_output`]. Such transactions
173 * appearing in the same block do not need to be included in the same call; instead, multiple
174 * calls with additional transactions may be made so long as they are made in [chain order].
176 * May be called before or after [`best_block_updated`] for the corresponding block. However,
177 * in the event of a chain reorganization, it must not be called with a `header` that is no
178 * longer in the chain as of the last call to [`best_block_updated`].
180 * [chain order]: Confirm#order
181 * [`best_block_updated`]: Self::best_block_updated
183 public void transactions_confirmed(byte[] header, TwoTuple_usizeTransactionZ[] txdata, int height) {
184 bindings.Confirm_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);
185 Reference.reachabilityFence(this);
186 Reference.reachabilityFence(header);
187 Reference.reachabilityFence(txdata);
188 Reference.reachabilityFence(height);
192 * Notifies LDK of a transaction that is no longer confirmed as result of a chain reorganization.
194 * Must be called for any transaction returned by [`get_relevant_txids`] if it has been
195 * reorganized out of the best chain or if it is no longer confirmed in the block with the
196 * given block hash. Once called, the given transaction will not be returned
197 * by [`get_relevant_txids`], unless it has been reconfirmed via [`transactions_confirmed`].
199 * [`get_relevant_txids`]: Self::get_relevant_txids
200 * [`transactions_confirmed`]: Self::transactions_confirmed
202 public void transaction_unconfirmed(byte[] txid) {
203 bindings.Confirm_transaction_unconfirmed(this.ptr, InternalUtils.check_arr_len(txid, 32));
204 Reference.reachabilityFence(this);
205 Reference.reachabilityFence(txid);
209 * Notifies LDK of an update to the best header connected at the given height.
211 * Must be called whenever a new chain tip becomes available. May be skipped for intermediary
214 public void best_block_updated(byte[] header, int height) {
215 bindings.Confirm_best_block_updated(this.ptr, InternalUtils.check_arr_len(header, 80), height);
216 Reference.reachabilityFence(this);
217 Reference.reachabilityFence(header);
218 Reference.reachabilityFence(height);
222 * Returns transactions that must be monitored for reorganization out of the chain along
223 * with the hash of the block as part of which it had been previously confirmed.
225 * Note that the returned `Option<BlockHash>` might be `None` for channels created with LDK
226 * 0.0.112 and prior, in which case you need to manually track previous confirmations.
228 * Will include any transactions passed to [`transactions_confirmed`] that have insufficient
229 * confirmations to be safe from a chain reorganization. Will not include any transactions
230 * passed to [`transaction_unconfirmed`], unless later reconfirmed.
232 * Must be called to determine the subset of transactions that must be monitored for
233 * reorganization. Will be idempotent between calls but may change as a result of calls to the
234 * other interface methods. Thus, this is useful to determine which transactions must be
235 * given to [`transaction_unconfirmed`].
237 * If any of the returned transactions are confirmed in a block other than the one with the
238 * given hash, they need to be unconfirmed and reconfirmed via [`transaction_unconfirmed`] and
239 * [`transactions_confirmed`], respectively.
241 * [`transactions_confirmed`]: Self::transactions_confirmed
242 * [`transaction_unconfirmed`]: Self::transaction_unconfirmed
244 public TwoTuple_ThirtyTwoBytesCOption_ThirtyTwoBytesZZ[] get_relevant_txids() {
245 long[] ret = bindings.Confirm_get_relevant_txids(this.ptr);
246 Reference.reachabilityFence(this);
247 int ret_conv_49_len = ret.length;
248 TwoTuple_ThirtyTwoBytesCOption_ThirtyTwoBytesZZ[] ret_conv_49_arr = new TwoTuple_ThirtyTwoBytesCOption_ThirtyTwoBytesZZ[ret_conv_49_len];
249 for (int x = 0; x < ret_conv_49_len; x++) {
250 long ret_conv_49 = ret[x];
251 TwoTuple_ThirtyTwoBytesCOption_ThirtyTwoBytesZZ ret_conv_49_hu_conv = new TwoTuple_ThirtyTwoBytesCOption_ThirtyTwoBytesZZ(null, ret_conv_49);
252 if (ret_conv_49_hu_conv != null) { ret_conv_49_hu_conv.ptrs_to.add(this); };
253 ret_conv_49_arr[x] = ret_conv_49_hu_conv;
255 return ret_conv_49_arr;