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 `Listen` trait is used to notify when blocks have been connected or disconnected from the
14 * Useful when needing to replay chain data upon startup or as new chain events occur. Clients
15 * sourcing chain data using a block-oriented API should prefer this interface over [`Confirm`].
16 * Such clients fetch the entire header chain whereas clients using [`Confirm`] only fetch headers
19 * By using [`Listen::filtered_block_connected`] this interface supports clients fetching the
20 * entire header chain and only blocks with matching transaction data using BIP 157 filters or
21 * other similar filtering.
23 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
24 public class Listen extends CommonBase {
25 final bindings.LDKListen bindings_instance;
26 Listen(Object _dummy, long ptr) { super(ptr); bindings_instance = null; }
27 private Listen(bindings.LDKListen arg) {
28 super(bindings.LDKListen_new(arg));
29 this.ptrs_to.add(arg);
30 this.bindings_instance = arg;
32 @Override @SuppressWarnings("deprecation")
33 protected void finalize() throws Throwable {
34 if (ptr != 0) { bindings.Listen_free(ptr); } super.finalize();
37 * Destroys the object, freeing associated resources. After this call, any access
38 * to this object may result in a SEGFAULT or worse.
40 * You should generally NEVER call this method. You should let the garbage collector
41 * do this for you when it finalizes objects. However, it may be useful for types
42 * which represent locks and should be closed immediately to avoid holding locks
45 public void destroy() {
46 if (ptr != 0) { bindings.Listen_free(ptr); }
49 public static interface ListenInterface {
51 * Notifies the listener that a block was added at the given height, with the transaction data
54 void filtered_block_connected(byte[] header, TwoTuple_usizeTransactionZ[] txdata, int height);
56 * Notifies the listener that a block was added at the given height.
58 void block_connected(byte[] block, int height);
60 * Notifies the listener that a block was removed at the given height.
62 void block_disconnected(byte[] header, int height);
64 private static class LDKListenHolder { Listen held; }
65 public static Listen new_impl(ListenInterface arg) {
66 final LDKListenHolder impl_holder = new LDKListenHolder();
67 impl_holder.held = new Listen(new bindings.LDKListen() {
68 @Override public void filtered_block_connected(byte[] header, long[] txdata, int height) {
69 int txdata_conv_28_len = txdata.length;
70 TwoTuple_usizeTransactionZ[] txdata_conv_28_arr = new TwoTuple_usizeTransactionZ[txdata_conv_28_len];
71 for (int c = 0; c < txdata_conv_28_len; c++) {
72 long txdata_conv_28 = txdata[c];
73 TwoTuple_usizeTransactionZ txdata_conv_28_hu_conv = new TwoTuple_usizeTransactionZ(null, txdata_conv_28);
74 if (txdata_conv_28_hu_conv != null) { txdata_conv_28_hu_conv.ptrs_to.add(this); };
75 txdata_conv_28_arr[c] = txdata_conv_28_hu_conv;
77 arg.filtered_block_connected(header, txdata_conv_28_arr, height);
78 Reference.reachabilityFence(arg);
80 @Override public void block_connected(byte[] block, int height) {
81 arg.block_connected(block, height);
82 Reference.reachabilityFence(arg);
84 @Override public void block_disconnected(byte[] header, int height) {
85 arg.block_disconnected(header, height);
86 Reference.reachabilityFence(arg);
89 return impl_holder.held;
92 * Notifies the listener that a block was added at the given height, with the transaction data
95 public void filtered_block_connected(byte[] header, TwoTuple_usizeTransactionZ[] txdata, int height) {
96 bindings.Listen_filtered_block_connected(this.ptr, InternalUtils.check_arr_len(header, 80), txdata != null ? Arrays.stream(txdata).mapToLong(txdata_conv_28 -> txdata_conv_28.ptr).toArray() : null, height);
97 Reference.reachabilityFence(this);
98 Reference.reachabilityFence(header);
99 Reference.reachabilityFence(txdata);
100 Reference.reachabilityFence(height);
104 * Notifies the listener that a block was added at the given height.
106 public void block_connected(byte[] block, int height) {
107 bindings.Listen_block_connected(this.ptr, block, height);
108 Reference.reachabilityFence(this);
109 Reference.reachabilityFence(block);
110 Reference.reachabilityFence(height);
114 * Notifies the listener that a block was removed at the given height.
116 public void block_disconnected(byte[] header, int height) {
117 bindings.Listen_block_disconnected(this.ptr, InternalUtils.check_arr_len(header, 80), height);
118 Reference.reachabilityFence(this);
119 Reference.reachabilityFence(header);
120 Reference.reachabilityFence(height);