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;
12 * Represents the network as nodes and channels between them
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 {
20 if (ptr != 0) { bindings.NetworkGraph_free(ptr); }
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
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 ret_hu_conv.ptrs_to.add(this);
37 * Serialize the NetworkGraph object into a byte array which can be read by NetworkGraph_read
39 public byte[] write() {
40 byte[] ret = bindings.NetworkGraph_write(this.ptr);
41 Reference.reachabilityFence(this);
46 * Read a NetworkGraph from a byte array, created by NetworkGraph_write
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 ret_hu_conv.ptrs_to.add(arg);
59 * Creates a new, empty, network graph.
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 ret_hu_conv.ptrs_to.add(ret_hu_conv);
68 ret_hu_conv.ptrs_to.add(logger);
73 * Returns a read-only view of the network graph.
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 ret_hu_conv.ptrs_to.add(this);
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.
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 ret_hu_conv.ptrs_to.add(this);
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.
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);
108 * For an already known node (from channel announcements), update its stored properties from a
109 * given node announcement.
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.
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 & ~1);
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 this.ptrs_to.add(msg);
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
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 & ~1);
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 this.ptrs_to.add(msg);
142 * Store or update channel info from a channel announcement.
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.
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.
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 & ~1, 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 this.ptrs_to.add(msg);
159 this.ptrs_to.add(chain_access);
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.
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.
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 & ~1, 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 this.ptrs_to.add(msg);
179 this.ptrs_to.add(chain_access);
184 * Update channel from partial announcement data received via rapid gossip sync
186 * `timestamp: u64`: Timestamp emulating the backdated original announcement receipt (by the
187 * rapid gossip sync server)
189 * All other parameters as used in [`msgs::UnsignedChannelAnnouncement`] fields.
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 & ~1, 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);
205 * Marks a channel in the graph as failed if a corresponding HTLC fail was sent.
206 * If permanent, removes a channel from the local storage.
207 * May cause the removal of nodes too, if this was their last channel.
208 * If not permanent, makes channels unavailable for routing.
210 public void channel_failed(long short_channel_id, boolean is_permanent) {
211 bindings.NetworkGraph_channel_failed(this.ptr, short_channel_id, is_permanent);
212 Reference.reachabilityFence(this);
213 Reference.reachabilityFence(short_channel_id);
214 Reference.reachabilityFence(is_permanent);
218 * Marks a node in the graph as failed.
220 public void node_failed(byte[] _node_id, boolean is_permanent) {
221 bindings.NetworkGraph_node_failed(this.ptr, InternalUtils.check_arr_len(_node_id, 33), is_permanent);
222 Reference.reachabilityFence(this);
223 Reference.reachabilityFence(_node_id);
224 Reference.reachabilityFence(is_permanent);
228 * Removes information about channels that we haven't heard any updates about in some time.
229 * This can be used regularly to prune the network graph of channels that likely no longer
232 * While there is no formal requirement that nodes regularly re-broadcast their channel
233 * updates every two weeks, the non-normative section of BOLT 7 currently suggests that
234 * pruning occur for updates which are at least two weeks old, which we implement here.
236 * Note that for users of the `lightning-background-processor` crate this method may be
237 * automatically called regularly for you.
239 * This method is only available with the `std` feature. See
240 * [`NetworkGraph::remove_stale_channels_with_time`] for `no-std` use.
242 public void remove_stale_channels() {
243 bindings.NetworkGraph_remove_stale_channels(this.ptr);
244 Reference.reachabilityFence(this);
248 * Removes information about channels that we haven't heard any updates about in some time.
249 * This can be used regularly to prune the network graph of channels that likely no longer
252 * While there is no formal requirement that nodes regularly re-broadcast their channel
253 * updates every two weeks, the non-normative section of BOLT 7 currently suggests that
254 * pruning occur for updates which are at least two weeks old, which we implement here.
256 * This function takes the current unix time as an argument. For users with the `std` feature
257 * enabled, [`NetworkGraph::remove_stale_channels`] may be preferable.
259 public void remove_stale_channels_with_time(long current_time_unix) {
260 bindings.NetworkGraph_remove_stale_channels_with_time(this.ptr, current_time_unix);
261 Reference.reachabilityFence(this);
262 Reference.reachabilityFence(current_time_unix);
266 * For an already known (from announcement) channel, update info about one of the directions
269 * You probably don't want to call this directly, instead relying on a P2PGossipSync's
270 * RoutingMessageHandler implementation to call it indirectly. This may be useful to accept
271 * routing messages from a source using a protocol other than the lightning P2P protocol.
273 * If built with `no-std`, any updates with a timestamp more than two weeks in the past or
274 * materially in the future will be rejected.
276 public Result_NoneLightningErrorZ update_channel(ChannelUpdate msg) {
277 long ret = bindings.NetworkGraph_update_channel(this.ptr, msg == null ? 0 : msg.ptr & ~1);
278 Reference.reachabilityFence(this);
279 Reference.reachabilityFence(msg);
280 if (ret >= 0 && ret <= 4096) { return null; }
281 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
282 this.ptrs_to.add(msg);
287 * For an already known (from announcement) channel, update info about one of the directions
288 * of the channel without verifying the associated signatures. Because we aren't given the
289 * associated signatures here we cannot relay the channel update to any of our peers.
291 * If built with `no-std`, any updates with a timestamp more than two weeks in the past or
292 * materially in the future will be rejected.
294 public Result_NoneLightningErrorZ update_channel_unsigned(UnsignedChannelUpdate msg) {
295 long ret = bindings.NetworkGraph_update_channel_unsigned(this.ptr, msg == null ? 0 : msg.ptr & ~1);
296 Reference.reachabilityFence(this);
297 Reference.reachabilityFence(msg);
298 if (ret >= 0 && ret <= 4096) { return null; }
299 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
300 this.ptrs_to.add(msg);