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 `Filter` trait defines behavior for indicating chain activity of interest pertaining to
14 * This is useful in order to have a [`Watch`] implementation convey to a chain source which
15 * transactions to be notified of. Notification may take the form of pre-filtering blocks or, in
16 * the case of [BIP 157]/[BIP 158], only fetching a block if the compact filter matches. If
17 * receiving full blocks from a chain source, any further filtering is unnecessary.
19 * After an output has been registered, subsequent block retrievals from the chain source must not
20 * exclude any transactions matching the new criteria nor any in-block descendants of such
23 * Note that use as part of a [`Watch`] implementation involves reentrancy. Therefore, the `Filter`
24 * should not block on I/O. Implementations should instead queue the newly monitored data to be
25 * processed later. Then, in order to block until the data has been processed, any [`Watch`]
26 * invocation that has called the `Filter` must return [`TemporaryFailure`].
28 * [`TemporaryFailure`]: ChannelMonitorUpdateErr::TemporaryFailure
29 * [BIP 157]: https://github.com/bitcoin/bips/blob/master/bip-0157.mediawiki
30 * [BIP 158]: https://github.com/bitcoin/bips/blob/master/bip-0158.mediawiki
32 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
33 public class Filter extends CommonBase {
34 final bindings.LDKFilter bindings_instance;
35 Filter(Object _dummy, long ptr) { super(ptr); bindings_instance = null; }
36 private Filter(bindings.LDKFilter arg) {
37 super(bindings.LDKFilter_new(arg));
38 this.ptrs_to.add(arg);
39 this.bindings_instance = arg;
41 @Override @SuppressWarnings("deprecation")
42 protected void finalize() throws Throwable {
43 if (ptr != 0) { bindings.Filter_free(ptr); } super.finalize();
46 public static interface FilterInterface {
48 * Registers interest in a transaction with `txid` and having an output with `script_pubkey` as
49 * a spending condition.
51 void register_tx(byte[] txid, byte[] script_pubkey);
53 * Registers interest in spends of a transaction output.
55 * Optionally, when `output.block_hash` is set, should return any transaction spending the
56 * output that is found in the corresponding block along with its index.
58 * This return value is useful for Electrum clients in order to supply in-block descendant
59 * transactions which otherwise were not included. This is not necessary for other clients if
60 * such descendant transactions were already included (e.g., when a BIP 157 client provides the
63 Option_C2Tuple_usizeTransactionZZ register_output(WatchedOutput output);
65 private static class LDKFilterHolder { Filter held; }
66 public static Filter new_impl(FilterInterface arg) {
67 final LDKFilterHolder impl_holder = new LDKFilterHolder();
68 impl_holder.held = new Filter(new bindings.LDKFilter() {
69 @Override public void register_tx(byte[] txid, byte[] script_pubkey) {
70 arg.register_tx(txid, script_pubkey);
72 @Override public long register_output(long output) {
73 WatchedOutput output_hu_conv = null; if (output < 0 || output > 4096) { output_hu_conv = new WatchedOutput(null, output); }
74 output_hu_conv.ptrs_to.add(this);
75 Option_C2Tuple_usizeTransactionZZ ret = arg.register_output(output_hu_conv);
76 long result = ret == null ? 0 : ret.clone_ptr();
80 return impl_holder.held;
83 * Registers interest in a transaction with `txid` and having an output with `script_pubkey` as
84 * a spending condition.
86 public void register_tx(byte[] txid, byte[] script_pubkey) {
87 bindings.Filter_register_tx(this.ptr, InternalUtils.check_arr_len(txid, 32), script_pubkey);
88 Reference.reachabilityFence(this);
89 Reference.reachabilityFence(txid);
90 Reference.reachabilityFence(script_pubkey);
94 * Registers interest in spends of a transaction output.
96 * Optionally, when `output.block_hash` is set, should return any transaction spending the
97 * output that is found in the corresponding block along with its index.
99 * This return value is useful for Electrum clients in order to supply in-block descendant
100 * transactions which otherwise were not included. This is not necessary for other clients if
101 * such descendant transactions were already included (e.g., when a BIP 157 client provides the
104 public Option_C2Tuple_usizeTransactionZZ register_output(WatchedOutput output) {
105 long ret = bindings.Filter_register_output(this.ptr, output == null ? 0 : output.ptr & ~1);
106 Reference.reachabilityFence(this);
107 Reference.reachabilityFence(output);
108 if (ret >= 0 && ret <= 4096) { return null; }
109 Option_C2Tuple_usizeTransactionZZ ret_hu_conv = Option_C2Tuple_usizeTransactionZZ.constr_from_ptr(ret);
110 ret_hu_conv.ptrs_to.add(this);