f35fa9dc825627989dc30e73beee9d2c7af5eda0
[ldk-java] / src / main / java / org / ldk / structs / Filter.java
1 package org.ldk.structs;
2
3 import org.ldk.impl.bindings;
4 import org.ldk.enums.*;
5 import org.ldk.util.*;
6 import java.util.Arrays;
7 import java.lang.ref.Reference;
8 import javax.annotation.Nullable;
9
10 /**
11  * The `Filter` trait defines behavior for indicating chain activity of interest pertaining to
12  * channels.
13  * 
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.
18  * 
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
21  * transactions.
22  * 
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`].
27  * 
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
31  */
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;
40         }
41         @Override @SuppressWarnings("deprecation")
42         protected void finalize() throws Throwable {
43                 if (ptr != 0) { bindings.Filter_free(ptr); } super.finalize();
44         }
45
46         public static interface FilterInterface {
47                 /**
48                  * Registers interest in a transaction with `txid` and having an output with `script_pubkey` as
49                  * a spending condition.
50                  */
51                 void register_tx(byte[] txid, byte[] script_pubkey);
52                 /**
53                  * Registers interest in spends of a transaction output.
54                  * 
55                  * Note that this method might be called during processing of a new block. You therefore need
56                  * to ensure that also dependent output spents within an already connected block are correctly
57                  * handled, e.g., by re-scanning the block in question whenever new outputs have been
58                  * registered mid-processing.
59                  */
60                 void register_output(WatchedOutput output);
61         }
62         private static class LDKFilterHolder { Filter held; }
63         public static Filter new_impl(FilterInterface arg) {
64                 final LDKFilterHolder impl_holder = new LDKFilterHolder();
65                 impl_holder.held = new Filter(new bindings.LDKFilter() {
66                         @Override public void register_tx(byte[] txid, byte[] script_pubkey) {
67                                 arg.register_tx(txid, script_pubkey);
68                                 Reference.reachabilityFence(arg);
69                         }
70                         @Override public void register_output(long output) {
71                                 org.ldk.structs.WatchedOutput output_hu_conv = null; if (output < 0 || output > 4096) { output_hu_conv = new org.ldk.structs.WatchedOutput(null, output); }
72                                 if (output_hu_conv != null) { output_hu_conv.ptrs_to.add(this); };
73                                 arg.register_output(output_hu_conv);
74                                 Reference.reachabilityFence(arg);
75                         }
76                 });
77                 return impl_holder.held;
78         }
79         /**
80          * Registers interest in a transaction with `txid` and having an output with `script_pubkey` as
81          * a spending condition.
82          */
83         public void register_tx(byte[] txid, byte[] script_pubkey) {
84                 bindings.Filter_register_tx(this.ptr, InternalUtils.check_arr_len(txid, 32), script_pubkey);
85                 Reference.reachabilityFence(this);
86                 Reference.reachabilityFence(txid);
87                 Reference.reachabilityFence(script_pubkey);
88         }
89
90         /**
91          * Registers interest in spends of a transaction output.
92          * 
93          * Note that this method might be called during processing of a new block. You therefore need
94          * to ensure that also dependent output spents within an already connected block are correctly
95          * handled, e.g., by re-scanning the block in question whenever new outputs have been
96          * registered mid-processing.
97          */
98         public void register_output(org.ldk.structs.WatchedOutput output) {
99                 bindings.Filter_register_output(this.ptr, output == null ? 0 : output.ptr);
100                 Reference.reachabilityFence(this);
101                 Reference.reachabilityFence(output);
102                 if (this != null) { this.ptrs_to.add(output); };
103         }
104
105 }