Merge pull request #33 from TheBlueMatt/main
[ldk-java] / src / main / java / org / ldk / structs / NetGraphMsgHandler.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 javax.annotation.Nullable;
8
9
10 /**
11  * Receives and validates network updates from peers,
12  * stores authentic and relevant data as a network graph.
13  * This network graph is then used for routing payments.
14  * Provides interface to help with initial routing sync by
15  * serving historical announcements.
16  */
17 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
18 public class NetGraphMsgHandler extends CommonBase {
19         NetGraphMsgHandler(Object _dummy, long ptr) { super(ptr); }
20         @Override @SuppressWarnings("deprecation")
21         protected void finalize() throws Throwable {
22                 super.finalize();
23                 if (ptr != 0) { bindings.NetGraphMsgHandler_free(ptr); }
24         }
25
26         /**
27          * Creates a new tracker of the actual state of the network of channels and nodes,
28          * assuming a fresh network graph.
29          * Chain monitor is used to make sure announced channels exist on-chain,
30          * channel data is correct, and that the announcement is signed with
31          * channel owners' keys.
32          * 
33          * Note that chain_access (or a relevant inner pointer) may be NULL or all-0s to represent None
34          */
35         public static NetGraphMsgHandler of(byte[] genesis_hash, @Nullable Access chain_access, Logger logger) {
36                 long ret = bindings.NetGraphMsgHandler_new(genesis_hash, chain_access == null ? 0 : chain_access.ptr, logger == null ? 0 : logger.ptr);
37                 if (ret < 1024) { return null; }
38                 NetGraphMsgHandler ret_hu_conv = new NetGraphMsgHandler(null, ret);
39                 ret_hu_conv.ptrs_to.add(ret_hu_conv);
40                 ret_hu_conv.ptrs_to.add(chain_access);
41                 ret_hu_conv.ptrs_to.add(logger);
42                 return ret_hu_conv;
43         }
44
45         /**
46          * Creates a new tracker of the actual state of the network of channels and nodes,
47          * assuming an existing Network Graph.
48          * 
49          * Note that chain_access (or a relevant inner pointer) may be NULL or all-0s to represent None
50          */
51         public static NetGraphMsgHandler from_net_graph(@Nullable Access chain_access, Logger logger, NetworkGraph network_graph) {
52                 long ret = bindings.NetGraphMsgHandler_from_net_graph(chain_access == null ? 0 : chain_access.ptr, logger == null ? 0 : logger.ptr, network_graph == null ? 0 : network_graph.ptr & ~1);
53                 if (ret < 1024) { return null; }
54                 NetGraphMsgHandler ret_hu_conv = new NetGraphMsgHandler(null, ret);
55                 ret_hu_conv.ptrs_to.add(ret_hu_conv);
56                 ret_hu_conv.ptrs_to.add(chain_access);
57                 ret_hu_conv.ptrs_to.add(logger);
58                 ret_hu_conv.ptrs_to.add(network_graph);
59                 return ret_hu_conv;
60         }
61
62         /**
63          * Adds a provider used to check new announcements. Does not affect
64          * existing announcements unless they are updated.
65          * Add, update or remove the provider would replace the current one.
66          * 
67          * Note that chain_access (or a relevant inner pointer) may be NULL or all-0s to represent None
68          */
69         public void add_chain_access(@Nullable Access chain_access) {
70                 bindings.NetGraphMsgHandler_add_chain_access(this.ptr, chain_access == null ? 0 : chain_access.ptr);
71                 this.ptrs_to.add(chain_access);
72         }
73
74         /**
75          * Take a read lock on the network_graph and return it in the C-bindings
76          * newtype helper. This is likely only useful when called via the C
77          * bindings as you can call `self.network_graph.read().unwrap()` in Rust
78          * yourself.
79          */
80         public LockedNetworkGraph read_locked_graph() {
81                 long ret = bindings.NetGraphMsgHandler_read_locked_graph(this.ptr);
82                 if (ret < 1024) { return null; }
83                 LockedNetworkGraph ret_hu_conv = new LockedNetworkGraph(null, ret);
84                 ret_hu_conv.ptrs_to.add(this);
85                 return ret_hu_conv;
86         }
87
88         /**
89          * Constructs a new RoutingMessageHandler which calls the relevant methods on this_arg.
90          * This copies the `inner` pointer in this_arg and thus the returned RoutingMessageHandler must be freed before this_arg is
91          */
92         public RoutingMessageHandler as_RoutingMessageHandler() {
93                 long ret = bindings.NetGraphMsgHandler_as_RoutingMessageHandler(this.ptr);
94                 if (ret < 1024) { return null; }
95                 RoutingMessageHandler ret_hu_conv = new RoutingMessageHandler(null, ret);
96                 ret_hu_conv.ptrs_to.add(this);
97                 return ret_hu_conv;
98         }
99
100         /**
101          * Constructs a new MessageSendEventsProvider which calls the relevant methods on this_arg.
102          * This copies the `inner` pointer in this_arg and thus the returned MessageSendEventsProvider must be freed before this_arg is
103          */
104         public MessageSendEventsProvider as_MessageSendEventsProvider() {
105                 long ret = bindings.NetGraphMsgHandler_as_MessageSendEventsProvider(this.ptr);
106                 if (ret < 1024) { return null; }
107                 MessageSendEventsProvider ret_hu_conv = new MessageSendEventsProvider(null, ret);
108                 ret_hu_conv.ptrs_to.add(this);
109                 return ret_hu_conv;
110         }
111
112 }