e3414f4821f84e58704d876793e15e918fc910c3
[ldk-java] / src / main / java / org / ldk / structs / NetworkGraph.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 /**
12  * Represents the network as nodes and channels between them
13  */
14 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
15 public class NetworkGraph extends CommonBase {
16         NetworkGraph(Object _dummy, long ptr) { super(ptr); }
17         @Override @SuppressWarnings("deprecation")
18         protected void finalize() throws Throwable {
19                 super.finalize();
20                 if (ptr != 0) { bindings.NetworkGraph_free(ptr); }
21         }
22
23         /**
24          * Constructs a new EventHandler which calls the relevant methods on this_arg.
25          * This copies the `inner` pointer in this_arg and thus the returned EventHandler must be freed before this_arg is
26          */
27         public EventHandler as_EventHandler() {
28                 long ret = bindings.NetworkGraph_as_EventHandler(this.ptr);
29                 Reference.reachabilityFence(this);
30                 if (ret >= 0 && ret <= 4096) { return null; }
31                 EventHandler ret_hu_conv = new EventHandler(null, ret);
32                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
33                 return ret_hu_conv;
34         }
35
36         /**
37          * Serialize the NetworkGraph object into a byte array which can be read by NetworkGraph_read
38          */
39         public byte[] write() {
40                 byte[] ret = bindings.NetworkGraph_write(this.ptr);
41                 Reference.reachabilityFence(this);
42                 return ret;
43         }
44
45         /**
46          * Read a NetworkGraph from a byte array, created by NetworkGraph_write
47          */
48         public static Result_NetworkGraphDecodeErrorZ read(byte[] ser, Logger arg) {
49                 long ret = bindings.NetworkGraph_read(ser, arg == null ? 0 : arg.ptr);
50                 Reference.reachabilityFence(ser);
51                 Reference.reachabilityFence(arg);
52                 if (ret >= 0 && ret <= 4096) { return null; }
53                 Result_NetworkGraphDecodeErrorZ ret_hu_conv = Result_NetworkGraphDecodeErrorZ.constr_from_ptr(ret);
54                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(arg); };
55                 return ret_hu_conv;
56         }
57
58         /**
59          * Creates a new, empty, network graph.
60          */
61         public static NetworkGraph of(byte[] genesis_hash, Logger logger) {
62                 long ret = bindings.NetworkGraph_new(InternalUtils.check_arr_len(genesis_hash, 32), logger == null ? 0 : logger.ptr);
63                 Reference.reachabilityFence(genesis_hash);
64                 Reference.reachabilityFence(logger);
65                 if (ret >= 0 && ret <= 4096) { return null; }
66                 org.ldk.structs.NetworkGraph ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.NetworkGraph(null, ret); }
67                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
68                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(logger); };
69                 return ret_hu_conv;
70         }
71
72         /**
73          * Returns a read-only view of the network graph.
74          */
75         public ReadOnlyNetworkGraph read_only() {
76                 long ret = bindings.NetworkGraph_read_only(this.ptr);
77                 Reference.reachabilityFence(this);
78                 if (ret >= 0 && ret <= 4096) { return null; }
79                 org.ldk.structs.ReadOnlyNetworkGraph ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.ReadOnlyNetworkGraph(null, ret); }
80                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
81                 return ret_hu_conv;
82         }
83
84         /**
85          * The unix timestamp provided by the most recent rapid gossip sync.
86          * It will be set by the rapid sync process after every sync completion.
87          */
88         public Option_u32Z get_last_rapid_gossip_sync_timestamp() {
89                 long ret = bindings.NetworkGraph_get_last_rapid_gossip_sync_timestamp(this.ptr);
90                 Reference.reachabilityFence(this);
91                 if (ret >= 0 && ret <= 4096) { return null; }
92                 org.ldk.structs.Option_u32Z ret_hu_conv = org.ldk.structs.Option_u32Z.constr_from_ptr(ret);
93                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
94                 return ret_hu_conv;
95         }
96
97         /**
98          * Update the unix timestamp provided by the most recent rapid gossip sync.
99          * This should be done automatically by the rapid sync process after every sync completion.
100          */
101         public void set_last_rapid_gossip_sync_timestamp(int last_rapid_gossip_sync_timestamp) {
102                 bindings.NetworkGraph_set_last_rapid_gossip_sync_timestamp(this.ptr, last_rapid_gossip_sync_timestamp);
103                 Reference.reachabilityFence(this);
104                 Reference.reachabilityFence(last_rapid_gossip_sync_timestamp);
105         }
106
107         /**
108          * For an already known node (from channel announcements), update its stored properties from a
109          * given node announcement.
110          * 
111          * You probably don't want to call this directly, instead relying on a P2PGossipSync's
112          * RoutingMessageHandler implementation to call it indirectly. This may be useful to accept
113          * routing messages from a source using a protocol other than the lightning P2P protocol.
114          */
115         public Result_NoneLightningErrorZ update_node_from_announcement(NodeAnnouncement msg) {
116                 long ret = bindings.NetworkGraph_update_node_from_announcement(this.ptr, msg == null ? 0 : msg.ptr);
117                 Reference.reachabilityFence(this);
118                 Reference.reachabilityFence(msg);
119                 if (ret >= 0 && ret <= 4096) { return null; }
120                 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
121                 if (this != null) { this.ptrs_to.add(msg); };
122                 return ret_hu_conv;
123         }
124
125         /**
126          * For an already known node (from channel announcements), update its stored properties from a
127          * given node announcement without verifying the associated signatures. Because we aren't
128          * given the associated signatures here we cannot relay the node announcement to any of our
129          * peers.
130          */
131         public Result_NoneLightningErrorZ update_node_from_unsigned_announcement(UnsignedNodeAnnouncement msg) {
132                 long ret = bindings.NetworkGraph_update_node_from_unsigned_announcement(this.ptr, msg == null ? 0 : msg.ptr);
133                 Reference.reachabilityFence(this);
134                 Reference.reachabilityFence(msg);
135                 if (ret >= 0 && ret <= 4096) { return null; }
136                 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
137                 if (this != null) { this.ptrs_to.add(msg); };
138                 return ret_hu_conv;
139         }
140
141         /**
142          * Store or update channel info from a channel announcement.
143          * 
144          * You probably don't want to call this directly, instead relying on a P2PGossipSync's
145          * RoutingMessageHandler implementation to call it indirectly. This may be useful to accept
146          * routing messages from a source using a protocol other than the lightning P2P protocol.
147          * 
148          * If a `chain::Access` object is provided via `chain_access`, it will be called to verify
149          * the corresponding UTXO exists on chain and is correctly-formatted.
150          */
151         public Result_NoneLightningErrorZ update_channel_from_announcement(ChannelAnnouncement msg, Option_AccessZ chain_access) {
152                 long ret = bindings.NetworkGraph_update_channel_from_announcement(this.ptr, msg == null ? 0 : msg.ptr, chain_access.ptr);
153                 Reference.reachabilityFence(this);
154                 Reference.reachabilityFence(msg);
155                 Reference.reachabilityFence(chain_access);
156                 if (ret >= 0 && ret <= 4096) { return null; }
157                 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
158                 if (this != null) { this.ptrs_to.add(msg); };
159                 if (this != null) { this.ptrs_to.add(chain_access); };
160                 return ret_hu_conv;
161         }
162
163         /**
164          * Store or update channel info from a channel announcement without verifying the associated
165          * signatures. Because we aren't given the associated signatures here we cannot relay the
166          * channel announcement to any of our peers.
167          * 
168          * If a `chain::Access` object is provided via `chain_access`, it will be called to verify
169          * the corresponding UTXO exists on chain and is correctly-formatted.
170          */
171         public Result_NoneLightningErrorZ update_channel_from_unsigned_announcement(UnsignedChannelAnnouncement msg, Option_AccessZ chain_access) {
172                 long ret = bindings.NetworkGraph_update_channel_from_unsigned_announcement(this.ptr, msg == null ? 0 : msg.ptr, chain_access.ptr);
173                 Reference.reachabilityFence(this);
174                 Reference.reachabilityFence(msg);
175                 Reference.reachabilityFence(chain_access);
176                 if (ret >= 0 && ret <= 4096) { return null; }
177                 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
178                 if (this != null) { this.ptrs_to.add(msg); };
179                 if (this != null) { this.ptrs_to.add(chain_access); };
180                 return ret_hu_conv;
181         }
182
183         /**
184          * Update channel from partial announcement data received via rapid gossip sync
185          * 
186          * `timestamp: u64`: Timestamp emulating the backdated original announcement receipt (by the
187          * rapid gossip sync server)
188          * 
189          * All other parameters as used in [`msgs::UnsignedChannelAnnouncement`] fields.
190          */
191         public Result_NoneLightningErrorZ add_channel_from_partial_announcement(long short_channel_id, long timestamp, ChannelFeatures features, byte[] node_id_1, byte[] node_id_2) {
192                 long ret = bindings.NetworkGraph_add_channel_from_partial_announcement(this.ptr, short_channel_id, timestamp, features == null ? 0 : features.ptr, InternalUtils.check_arr_len(node_id_1, 33), InternalUtils.check_arr_len(node_id_2, 33));
193                 Reference.reachabilityFence(this);
194                 Reference.reachabilityFence(short_channel_id);
195                 Reference.reachabilityFence(timestamp);
196                 Reference.reachabilityFence(features);
197                 Reference.reachabilityFence(node_id_1);
198                 Reference.reachabilityFence(node_id_2);
199                 if (ret >= 0 && ret <= 4096) { return null; }
200                 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
201                 if (this != null) { this.ptrs_to.add(features); };
202                 return ret_hu_conv;
203         }
204
205         /**
206          * Marks a channel in the graph as failed if a corresponding HTLC fail was sent.
207          * If permanent, removes a channel from the local storage.
208          * May cause the removal of nodes too, if this was their last channel.
209          * If not permanent, makes channels unavailable for routing.
210          */
211         public void channel_failed(long short_channel_id, boolean is_permanent) {
212                 bindings.NetworkGraph_channel_failed(this.ptr, short_channel_id, is_permanent);
213                 Reference.reachabilityFence(this);
214                 Reference.reachabilityFence(short_channel_id);
215                 Reference.reachabilityFence(is_permanent);
216         }
217
218         /**
219          * Marks a node in the graph as permanently failed, effectively removing it and its channels
220          * from local storage.
221          */
222         public void node_failed_permanent(byte[] node_id) {
223                 bindings.NetworkGraph_node_failed_permanent(this.ptr, InternalUtils.check_arr_len(node_id, 33));
224                 Reference.reachabilityFence(this);
225                 Reference.reachabilityFence(node_id);
226         }
227
228         /**
229          * Removes information about channels that we haven't heard any updates about in some time.
230          * This can be used regularly to prune the network graph of channels that likely no longer
231          * exist.
232          * 
233          * While there is no formal requirement that nodes regularly re-broadcast their channel
234          * updates every two weeks, the non-normative section of BOLT 7 currently suggests that
235          * pruning occur for updates which are at least two weeks old, which we implement here.
236          * 
237          * Note that for users of the `lightning-background-processor` crate this method may be
238          * automatically called regularly for you.
239          * 
240          * This method will also cause us to stop tracking removed nodes and channels if they have been
241          * in the map for a while so that these can be resynced from gossip in the future.
242          * 
243          * This method is only available with the `std` feature. See
244          * [`NetworkGraph::remove_stale_channels_and_tracking_with_time`] for `no-std` use.
245          */
246         public void remove_stale_channels_and_tracking() {
247                 bindings.NetworkGraph_remove_stale_channels_and_tracking(this.ptr);
248                 Reference.reachabilityFence(this);
249         }
250
251         /**
252          * Removes information about channels that we haven't heard any updates about in some time.
253          * This can be used regularly to prune the network graph of channels that likely no longer
254          * exist.
255          * 
256          * While there is no formal requirement that nodes regularly re-broadcast their channel
257          * updates every two weeks, the non-normative section of BOLT 7 currently suggests that
258          * pruning occur for updates which are at least two weeks old, which we implement here.
259          * 
260          * This method will also cause us to stop tracking removed nodes and channels if they have been
261          * in the map for a while so that these can be resynced from gossip in the future.
262          * 
263          * This function takes the current unix time as an argument. For users with the `std` feature
264          * enabled, [`NetworkGraph::remove_stale_channels_and_tracking`] may be preferable.
265          */
266         public void remove_stale_channels_and_tracking_with_time(long current_time_unix) {
267                 bindings.NetworkGraph_remove_stale_channels_and_tracking_with_time(this.ptr, current_time_unix);
268                 Reference.reachabilityFence(this);
269                 Reference.reachabilityFence(current_time_unix);
270         }
271
272         /**
273          * For an already known (from announcement) channel, update info about one of the directions
274          * of the channel.
275          * 
276          * You probably don't want to call this directly, instead relying on a P2PGossipSync's
277          * RoutingMessageHandler implementation to call it indirectly. This may be useful to accept
278          * routing messages from a source using a protocol other than the lightning P2P protocol.
279          * 
280          * If built with `no-std`, any updates with a timestamp more than two weeks in the past or
281          * materially in the future will be rejected.
282          */
283         public Result_NoneLightningErrorZ update_channel(ChannelUpdate msg) {
284                 long ret = bindings.NetworkGraph_update_channel(this.ptr, msg == null ? 0 : msg.ptr);
285                 Reference.reachabilityFence(this);
286                 Reference.reachabilityFence(msg);
287                 if (ret >= 0 && ret <= 4096) { return null; }
288                 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
289                 if (this != null) { this.ptrs_to.add(msg); };
290                 return ret_hu_conv;
291         }
292
293         /**
294          * For an already known (from announcement) channel, update info about one of the directions
295          * of the channel without verifying the associated signatures. Because we aren't given the
296          * associated signatures here we cannot relay the channel update to any of our peers.
297          * 
298          * If built with `no-std`, any updates with a timestamp more than two weeks in the past or
299          * materially in the future will be rejected.
300          */
301         public Result_NoneLightningErrorZ update_channel_unsigned(UnsignedChannelUpdate msg) {
302                 long ret = bindings.NetworkGraph_update_channel_unsigned(this.ptr, msg == null ? 0 : msg.ptr);
303                 Reference.reachabilityFence(this);
304                 Reference.reachabilityFence(msg);
305                 if (ret >= 0 && ret <= 4096) { return null; }
306                 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
307                 if (this != null) { this.ptrs_to.add(msg); };
308                 return ret_hu_conv;
309         }
310
311 }