93c1fc228dc5d4e934bb306486397873123959f2
[ldk-java] / src / main / java / org / ldk / structs / Listen.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 `Listen` trait is used to notify when blocks have been connected or disconnected from the
12  * chain.
13  * 
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
17  * when needed.
18  */
19 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
20 public class Listen extends CommonBase {
21         final bindings.LDKListen bindings_instance;
22         Listen(Object _dummy, long ptr) { super(ptr); bindings_instance = null; }
23         private Listen(bindings.LDKListen arg) {
24                 super(bindings.LDKListen_new(arg));
25                 this.ptrs_to.add(arg);
26                 this.bindings_instance = arg;
27         }
28         @Override @SuppressWarnings("deprecation")
29         protected void finalize() throws Throwable {
30                 if (ptr != 0) { bindings.Listen_free(ptr); } super.finalize();
31         }
32
33         public static interface ListenInterface {
34                 /**
35                  * Notifies the listener that a block was added at the given height.
36                  */
37                 void block_connected(byte[] block, int height);
38                 /**
39                  * Notifies the listener that a block was removed at the given height.
40                  */
41                 void block_disconnected(byte[] header, int height);
42         }
43         private static class LDKListenHolder { Listen held; }
44         public static Listen new_impl(ListenInterface arg) {
45                 final LDKListenHolder impl_holder = new LDKListenHolder();
46                 impl_holder.held = new Listen(new bindings.LDKListen() {
47                         @Override public void block_connected(byte[] block, int height) {
48                                 arg.block_connected(block, height);
49                         }
50                         @Override public void block_disconnected(byte[] header, int height) {
51                                 arg.block_disconnected(header, height);
52                         }
53                 });
54                 return impl_holder.held;
55         }
56         /**
57          * Notifies the listener that a block was added at the given height.
58          */
59         public void block_connected(byte[] block, int height) {
60                 bindings.Listen_block_connected(this.ptr, block, height);
61                 Reference.reachabilityFence(this);
62                 Reference.reachabilityFence(block);
63                 Reference.reachabilityFence(height);
64         }
65
66         /**
67          * Notifies the listener that a block was removed at the given height.
68          */
69         public void block_disconnected(byte[] header, int height) {
70                 bindings.Listen_block_disconnected(this.ptr, InternalUtils.check_arr_len(header, 80), height);
71                 Reference.reachabilityFence(this);
72                 Reference.reachabilityFence(header);
73                 Reference.reachabilityFence(height);
74         }
75
76 }