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 * Update to the [`NetworkGraph`] based on payment failure information conveyed via the Onion
13 * return packet by a node along the route. See [BOLT #4] for details.
15 * [BOLT #4]: https://github.com/lightning/bolts/blob/master/04-onion-routing.md
17 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
18 public class NetworkUpdate extends CommonBase {
19 private NetworkUpdate(Object _dummy, long ptr) { super(ptr); }
20 @Override @SuppressWarnings("deprecation")
21 protected void finalize() throws Throwable {
23 if (ptr != 0) { bindings.NetworkUpdate_free(ptr); }
25 static NetworkUpdate constr_from_ptr(long ptr) {
26 bindings.LDKNetworkUpdate raw_val = bindings.LDKNetworkUpdate_ref_from_ptr(ptr);
27 if (raw_val.getClass() == bindings.LDKNetworkUpdate.ChannelUpdateMessage.class) {
28 return new ChannelUpdateMessage(ptr, (bindings.LDKNetworkUpdate.ChannelUpdateMessage)raw_val);
30 if (raw_val.getClass() == bindings.LDKNetworkUpdate.ChannelFailure.class) {
31 return new ChannelFailure(ptr, (bindings.LDKNetworkUpdate.ChannelFailure)raw_val);
33 if (raw_val.getClass() == bindings.LDKNetworkUpdate.NodeFailure.class) {
34 return new NodeFailure(ptr, (bindings.LDKNetworkUpdate.NodeFailure)raw_val);
36 assert false; return null; // Unreachable without extending the (internal) bindings interface
40 * An error indicating a `channel_update` messages should be applied via
41 * [`NetworkGraph::update_channel`].
43 public final static class ChannelUpdateMessage extends NetworkUpdate {
45 * The update to apply via [`NetworkGraph::update_channel`].
47 public final org.ldk.structs.ChannelUpdate msg;
48 private ChannelUpdateMessage(long ptr, bindings.LDKNetworkUpdate.ChannelUpdateMessage obj) {
51 org.ldk.structs.ChannelUpdate msg_hu_conv = null; if (msg < 0 || msg > 4096) { msg_hu_conv = new org.ldk.structs.ChannelUpdate(null, msg); }
52 msg_hu_conv.ptrs_to.add(this);
53 this.msg = msg_hu_conv;
57 * An error indicating that a channel failed to route a payment, which should be applied via
58 * [`NetworkGraph::channel_failed`].
60 public final static class ChannelFailure extends NetworkUpdate {
62 * The short channel id of the closed channel.
64 public final long short_channel_id;
66 * Whether the channel should be permanently removed or temporarily disabled until a new
67 * `channel_update` message is received.
69 public final boolean is_permanent;
70 private ChannelFailure(long ptr, bindings.LDKNetworkUpdate.ChannelFailure obj) {
72 this.short_channel_id = obj.short_channel_id;
73 this.is_permanent = obj.is_permanent;
77 * An error indicating that a node failed to route a payment, which should be applied via
78 * [`NetworkGraph::node_failed`].
80 public final static class NodeFailure extends NetworkUpdate {
82 * The node id of the failed node.
84 public final byte[] node_id;
86 * Whether the node should be permanently removed from consideration or can be restored
87 * when a new `channel_update` message is received.
89 public final boolean is_permanent;
90 private NodeFailure(long ptr, bindings.LDKNetworkUpdate.NodeFailure obj) {
92 this.node_id = obj.node_id;
93 this.is_permanent = obj.is_permanent;
97 long ret = bindings.NetworkUpdate_clone_ptr(this.ptr);
98 Reference.reachabilityFence(this);
103 * Creates a copy of the NetworkUpdate
105 public NetworkUpdate clone() {
106 long ret = bindings.NetworkUpdate_clone(this.ptr);
107 Reference.reachabilityFence(this);
108 if (ret >= 0 && ret <= 4096) { return null; }
109 org.ldk.structs.NetworkUpdate ret_hu_conv = org.ldk.structs.NetworkUpdate.constr_from_ptr(ret);
110 ret_hu_conv.ptrs_to.add(this);
115 * Utility method to constructs a new ChannelUpdateMessage-variant NetworkUpdate
117 public static NetworkUpdate channel_update_message(ChannelUpdate msg) {
118 long ret = bindings.NetworkUpdate_channel_update_message(msg == null ? 0 : msg.ptr & ~1);
119 Reference.reachabilityFence(msg);
120 if (ret >= 0 && ret <= 4096) { return null; }
121 org.ldk.structs.NetworkUpdate ret_hu_conv = org.ldk.structs.NetworkUpdate.constr_from_ptr(ret);
122 ret_hu_conv.ptrs_to.add(ret_hu_conv);
127 * Utility method to constructs a new ChannelFailure-variant NetworkUpdate
129 public static NetworkUpdate channel_failure(long short_channel_id, boolean is_permanent) {
130 long ret = bindings.NetworkUpdate_channel_failure(short_channel_id, is_permanent);
131 Reference.reachabilityFence(short_channel_id);
132 Reference.reachabilityFence(is_permanent);
133 if (ret >= 0 && ret <= 4096) { return null; }
134 org.ldk.structs.NetworkUpdate ret_hu_conv = org.ldk.structs.NetworkUpdate.constr_from_ptr(ret);
135 ret_hu_conv.ptrs_to.add(ret_hu_conv);
140 * Utility method to constructs a new NodeFailure-variant NetworkUpdate
142 public static NetworkUpdate node_failure(byte[] node_id, boolean is_permanent) {
143 long ret = bindings.NetworkUpdate_node_failure(InternalUtils.check_arr_len(node_id, 33), is_permanent);
144 Reference.reachabilityFence(node_id);
145 Reference.reachabilityFence(is_permanent);
146 if (ret >= 0 && ret <= 4096) { return null; }
147 org.ldk.structs.NetworkUpdate ret_hu_conv = org.ldk.structs.NetworkUpdate.constr_from_ptr(ret);
148 ret_hu_conv.ptrs_to.add(ret_hu_conv);
153 * Serialize the NetworkUpdate object into a byte array which can be read by NetworkUpdate_read
155 public byte[] write() {
156 byte[] ret = bindings.NetworkUpdate_write(this.ptr);
157 Reference.reachabilityFence(this);