e2f17ce1fcfbf7c912055ee2c64ca9f9c495ec15
[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         long clone_ptr() {
24                 long ret = bindings.NetworkGraph_clone_ptr(this.ptr);
25                 Reference.reachabilityFence(this);
26                 return ret;
27         }
28
29         /**
30          * Creates a copy of the NetworkGraph
31          */
32         public NetworkGraph clone() {
33                 long ret = bindings.NetworkGraph_clone(this.ptr);
34                 Reference.reachabilityFence(this);
35                 if (ret >= 0 && ret <= 4096) { return null; }
36                 NetworkGraph ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new NetworkGraph(null, ret); }
37                 ret_hu_conv.ptrs_to.add(this);
38                 return ret_hu_conv;
39         }
40
41         /**
42          * Serialize the NetworkGraph object into a byte array which can be read by NetworkGraph_read
43          */
44         public byte[] write() {
45                 byte[] ret = bindings.NetworkGraph_write(this.ptr);
46                 Reference.reachabilityFence(this);
47                 return ret;
48         }
49
50         /**
51          * Read a NetworkGraph from a byte array, created by NetworkGraph_write
52          */
53         public static Result_NetworkGraphDecodeErrorZ read(byte[] ser) {
54                 long ret = bindings.NetworkGraph_read(ser);
55                 Reference.reachabilityFence(ser);
56                 if (ret >= 0 && ret <= 4096) { return null; }
57                 Result_NetworkGraphDecodeErrorZ ret_hu_conv = Result_NetworkGraphDecodeErrorZ.constr_from_ptr(ret);
58                 return ret_hu_conv;
59         }
60
61         /**
62          * Creates a new, empty, network graph.
63          */
64         public static NetworkGraph of(byte[] genesis_hash) {
65                 long ret = bindings.NetworkGraph_new(InternalUtils.check_arr_len(genesis_hash, 32));
66                 Reference.reachabilityFence(genesis_hash);
67                 if (ret >= 0 && ret <= 4096) { return null; }
68                 NetworkGraph ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new NetworkGraph(null, ret); }
69                 ret_hu_conv.ptrs_to.add(ret_hu_conv);
70                 return ret_hu_conv;
71         }
72
73         /**
74          * Returns a read-only view of the network graph.
75          */
76         public ReadOnlyNetworkGraph read_only() {
77                 long ret = bindings.NetworkGraph_read_only(this.ptr);
78                 Reference.reachabilityFence(this);
79                 if (ret >= 0 && ret <= 4096) { return null; }
80                 ReadOnlyNetworkGraph ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new ReadOnlyNetworkGraph(null, ret); }
81                 ret_hu_conv.ptrs_to.add(this);
82                 return ret_hu_conv;
83         }
84
85         /**
86          * For an already known node (from channel announcements), update its stored properties from a
87          * given node announcement.
88          * 
89          * You probably don't want to call this directly, instead relying on a NetGraphMsgHandler's
90          * RoutingMessageHandler implementation to call it indirectly. This may be useful to accept
91          * routing messages from a source using a protocol other than the lightning P2P protocol.
92          */
93         public Result_NoneLightningErrorZ update_node_from_announcement(NodeAnnouncement msg) {
94                 long ret = bindings.NetworkGraph_update_node_from_announcement(this.ptr, msg == null ? 0 : msg.ptr & ~1);
95                 Reference.reachabilityFence(this);
96                 Reference.reachabilityFence(msg);
97                 if (ret >= 0 && ret <= 4096) { return null; }
98                 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
99                 this.ptrs_to.add(msg);
100                 return ret_hu_conv;
101         }
102
103         /**
104          * For an already known node (from channel announcements), update its stored properties from a
105          * given node announcement without verifying the associated signatures. Because we aren't
106          * given the associated signatures here we cannot relay the node announcement to any of our
107          * peers.
108          */
109         public Result_NoneLightningErrorZ update_node_from_unsigned_announcement(UnsignedNodeAnnouncement msg) {
110                 long ret = bindings.NetworkGraph_update_node_from_unsigned_announcement(this.ptr, msg == null ? 0 : msg.ptr & ~1);
111                 Reference.reachabilityFence(this);
112                 Reference.reachabilityFence(msg);
113                 if (ret >= 0 && ret <= 4096) { return null; }
114                 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
115                 this.ptrs_to.add(msg);
116                 return ret_hu_conv;
117         }
118
119         /**
120          * Store or update channel info from a channel announcement.
121          * 
122          * You probably don't want to call this directly, instead relying on a NetGraphMsgHandler's
123          * RoutingMessageHandler implementation to call it indirectly. This may be useful to accept
124          * routing messages from a source using a protocol other than the lightning P2P protocol.
125          * 
126          * If a `chain::Access` object is provided via `chain_access`, it will be called to verify
127          * the corresponding UTXO exists on chain and is correctly-formatted.
128          */
129         public Result_NoneLightningErrorZ update_channel_from_announcement(ChannelAnnouncement msg, Option_AccessZ chain_access) {
130                 long ret = bindings.NetworkGraph_update_channel_from_announcement(this.ptr, msg == null ? 0 : msg.ptr & ~1, chain_access.ptr);
131                 Reference.reachabilityFence(this);
132                 Reference.reachabilityFence(msg);
133                 Reference.reachabilityFence(chain_access);
134                 if (ret >= 0 && ret <= 4096) { return null; }
135                 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
136                 this.ptrs_to.add(msg);
137                 this.ptrs_to.add(chain_access);
138                 return ret_hu_conv;
139         }
140
141         /**
142          * Store or update channel info from a channel announcement without verifying the associated
143          * signatures. Because we aren't given the associated signatures here we cannot relay the
144          * channel announcement to any of our peers.
145          * 
146          * If a `chain::Access` object is provided via `chain_access`, it will be called to verify
147          * the corresponding UTXO exists on chain and is correctly-formatted.
148          */
149         public Result_NoneLightningErrorZ update_channel_from_unsigned_announcement(UnsignedChannelAnnouncement msg, Option_AccessZ chain_access) {
150                 long ret = bindings.NetworkGraph_update_channel_from_unsigned_announcement(this.ptr, msg == null ? 0 : msg.ptr & ~1, chain_access.ptr);
151                 Reference.reachabilityFence(this);
152                 Reference.reachabilityFence(msg);
153                 Reference.reachabilityFence(chain_access);
154                 if (ret >= 0 && ret <= 4096) { return null; }
155                 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
156                 this.ptrs_to.add(msg);
157                 this.ptrs_to.add(chain_access);
158                 return ret_hu_conv;
159         }
160
161         /**
162          * Close a channel if a corresponding HTLC fail was sent.
163          * If permanent, removes a channel from the local storage.
164          * May cause the removal of nodes too, if this was their last channel.
165          * If not permanent, makes channels unavailable for routing.
166          */
167         public void close_channel_from_update(long short_channel_id, boolean is_permanent) {
168                 bindings.NetworkGraph_close_channel_from_update(this.ptr, short_channel_id, is_permanent);
169                 Reference.reachabilityFence(this);
170                 Reference.reachabilityFence(short_channel_id);
171                 Reference.reachabilityFence(is_permanent);
172         }
173
174         /**
175          * Marks a node in the graph as failed.
176          */
177         public void fail_node(byte[] _node_id, boolean is_permanent) {
178                 bindings.NetworkGraph_fail_node(this.ptr, InternalUtils.check_arr_len(_node_id, 33), is_permanent);
179                 Reference.reachabilityFence(this);
180                 Reference.reachabilityFence(_node_id);
181                 Reference.reachabilityFence(is_permanent);
182         }
183
184         /**
185          * Removes information about channels that we haven't heard any updates about in some time.
186          * This can be used regularly to prune the network graph of channels that likely no longer
187          * exist.
188          * 
189          * While there is no formal requirement that nodes regularly re-broadcast their channel
190          * updates every two weeks, the non-normative section of BOLT 7 currently suggests that
191          * pruning occur for updates which are at least two weeks old, which we implement here.
192          * 
193          * This function takes the current unix time as an argument. For users with the `std` feature
194          * enabled, [`NetworkGraph::remove_stale_channels`] may be preferable.
195          */
196         public void remove_stale_channels_with_time(long current_time_unix) {
197                 bindings.NetworkGraph_remove_stale_channels_with_time(this.ptr, current_time_unix);
198                 Reference.reachabilityFence(this);
199                 Reference.reachabilityFence(current_time_unix);
200         }
201
202         /**
203          * For an already known (from announcement) channel, update info about one of the directions
204          * of the channel.
205          * 
206          * You probably don't want to call this directly, instead relying on a NetGraphMsgHandler's
207          * RoutingMessageHandler implementation to call it indirectly. This may be useful to accept
208          * routing messages from a source using a protocol other than the lightning P2P protocol.
209          * 
210          * If built with `no-std`, any updates with a timestamp more than two weeks in the past or
211          * materially in the future will be rejected.
212          */
213         public Result_NoneLightningErrorZ update_channel(ChannelUpdate msg) {
214                 long ret = bindings.NetworkGraph_update_channel(this.ptr, msg == null ? 0 : msg.ptr & ~1);
215                 Reference.reachabilityFence(this);
216                 Reference.reachabilityFence(msg);
217                 if (ret >= 0 && ret <= 4096) { return null; }
218                 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
219                 this.ptrs_to.add(msg);
220                 return ret_hu_conv;
221         }
222
223         /**
224          * For an already known (from announcement) channel, update info about one of the directions
225          * of the channel without verifying the associated signatures. Because we aren't given the
226          * associated signatures here we cannot relay the channel update to any of our peers.
227          * 
228          * If built with `no-std`, any updates with a timestamp more than two weeks in the past or
229          * materially in the future will be rejected.
230          */
231         public Result_NoneLightningErrorZ update_channel_unsigned(UnsignedChannelUpdate msg) {
232                 long ret = bindings.NetworkGraph_update_channel_unsigned(this.ptr, msg == null ? 0 : msg.ptr & ~1);
233                 Reference.reachabilityFence(this);
234                 Reference.reachabilityFence(msg);
235                 if (ret >= 0 && ret <= 4096) { return null; }
236                 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
237                 this.ptrs_to.add(msg);
238                 return ret_hu_conv;
239         }
240
241 }