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 [`InProgress`].
28 * [`InProgress`]: ChannelMonitorUpdateStatus::InProgress
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 * Destroys the object, freeing associated resources. After this call, any access
47 * to this object may result in a SEGFAULT or worse.
49 * You should generally NEVER call this method. You should let the garbage collector
50 * do this for you when it finalizes objects. However, it may be useful for types
51 * which represent locks and should be closed immediately to avoid holding locks
54 public void destroy() {
55 if (ptr != 0) { bindings.Filter_free(ptr); }
58 public static interface FilterInterface {
60 * Registers interest in a transaction with `txid` and having an output with `script_pubkey` as
61 * a spending condition.
63 void register_tx(byte[] txid, byte[] script_pubkey);
65 * Registers interest in spends of a transaction output.
67 * Note that this method might be called during processing of a new block. You therefore need
68 * to ensure that also dependent output spents within an already connected block are correctly
69 * handled, e.g., by re-scanning the block in question whenever new outputs have been
70 * registered mid-processing.
72 void register_output(WatchedOutput output);
74 private static class LDKFilterHolder { Filter held; }
75 public static Filter new_impl(FilterInterface arg) {
76 final LDKFilterHolder impl_holder = new LDKFilterHolder();
77 impl_holder.held = new Filter(new bindings.LDKFilter() {
78 @Override public void register_tx(byte[] txid, byte[] script_pubkey) {
79 arg.register_tx(txid, script_pubkey);
80 Reference.reachabilityFence(arg);
82 @Override public void register_output(long output) {
83 org.ldk.structs.WatchedOutput output_hu_conv = null; if (output < 0 || output > 4096) { output_hu_conv = new org.ldk.structs.WatchedOutput(null, output); }
84 if (output_hu_conv != null) { output_hu_conv.ptrs_to.add(this); };
85 arg.register_output(output_hu_conv);
86 Reference.reachabilityFence(arg);
89 return impl_holder.held;
92 * Registers interest in a transaction with `txid` and having an output with `script_pubkey` as
93 * a spending condition.
95 public void register_tx(byte[] txid, byte[] script_pubkey) {
96 bindings.Filter_register_tx(this.ptr, InternalUtils.check_arr_len(txid, 32), script_pubkey);
97 Reference.reachabilityFence(this);
98 Reference.reachabilityFence(txid);
99 Reference.reachabilityFence(script_pubkey);
103 * Registers interest in spends of a transaction output.
105 * Note that this method might be called during processing of a new block. You therefore need
106 * to ensure that also dependent output spents within an already connected block are correctly
107 * handled, e.g., by re-scanning the block in question whenever new outputs have been
108 * registered mid-processing.
110 public void register_output(org.ldk.structs.WatchedOutput output) {
111 bindings.Filter_register_output(this.ptr, output.ptr);
112 Reference.reachabilityFence(this);
113 Reference.reachabilityFence(output);
114 if (this != null) { this.ptrs_to.add(output); };