1 package org.ldk.structs;
3 import org.ldk.impl.bindings;
4 import org.ldk.enums.*;
6 import java.util.Arrays;
7 import javax.annotation.Nullable;
11 * Update to the [`NetworkGraph`] based on payment failure information conveyed via the Onion
12 * return packet by a node along the route. See [BOLT #4] for details.
14 * [BOLT #4]: https://github.com/lightningnetwork/lightning-rfc/blob/master/04-onion-routing.md
16 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
17 public class NetworkUpdate extends CommonBase {
18 private NetworkUpdate(Object _dummy, long ptr) { super(ptr); }
19 @Override @SuppressWarnings("deprecation")
20 protected void finalize() throws Throwable {
22 if (ptr != 0) { bindings.NetworkUpdate_free(ptr); }
24 static NetworkUpdate constr_from_ptr(long ptr) {
25 bindings.LDKNetworkUpdate raw_val = bindings.LDKNetworkUpdate_ref_from_ptr(ptr);
26 if (raw_val.getClass() == bindings.LDKNetworkUpdate.ChannelUpdateMessage.class) {
27 return new ChannelUpdateMessage(ptr, (bindings.LDKNetworkUpdate.ChannelUpdateMessage)raw_val);
29 if (raw_val.getClass() == bindings.LDKNetworkUpdate.ChannelClosed.class) {
30 return new ChannelClosed(ptr, (bindings.LDKNetworkUpdate.ChannelClosed)raw_val);
32 if (raw_val.getClass() == bindings.LDKNetworkUpdate.NodeFailure.class) {
33 return new NodeFailure(ptr, (bindings.LDKNetworkUpdate.NodeFailure)raw_val);
35 assert false; return null; // Unreachable without extending the (internal) bindings interface
38 public final static class ChannelUpdateMessage extends NetworkUpdate {
40 * The update to apply via [`NetworkGraph::update_channel`].
42 public final ChannelUpdate msg;
43 private ChannelUpdateMessage(long ptr, bindings.LDKNetworkUpdate.ChannelUpdateMessage obj) {
46 ChannelUpdate msg_hu_conv = null; if (msg < 0 || msg > 4096) { msg_hu_conv = new ChannelUpdate(null, msg); }
47 msg_hu_conv.ptrs_to.add(this);
48 this.msg = msg_hu_conv;
51 public final static class ChannelClosed extends NetworkUpdate {
53 * The short channel id of the closed channel.
55 public final long short_channel_id;
57 * Whether the channel should be permanently removed or temporarily disabled until a new
58 * `channel_update` message is received.
60 public final boolean is_permanent;
61 private ChannelClosed(long ptr, bindings.LDKNetworkUpdate.ChannelClosed obj) {
63 this.short_channel_id = obj.short_channel_id;
64 this.is_permanent = obj.is_permanent;
67 public final static class NodeFailure extends NetworkUpdate {
69 * The node id of the failed node.
71 public final byte[] node_id;
73 * Whether the node should be permanently removed from consideration or can be restored
74 * when a new `channel_update` message is received.
76 public final boolean is_permanent;
77 private NodeFailure(long ptr, bindings.LDKNetworkUpdate.NodeFailure obj) {
79 this.node_id = obj.node_id;
80 this.is_permanent = obj.is_permanent;
84 * Creates a copy of the NetworkUpdate
86 public NetworkUpdate clone() {
87 long ret = bindings.NetworkUpdate_clone(this.ptr);
88 if (ret >= 0 && ret <= 4096) { return null; }
89 NetworkUpdate ret_hu_conv = NetworkUpdate.constr_from_ptr(ret);
90 ret_hu_conv.ptrs_to.add(this);
95 * Utility method to constructs a new ChannelUpdateMessage-variant NetworkUpdate
97 public static NetworkUpdate channel_update_message(ChannelUpdate msg) {
98 long ret = bindings.NetworkUpdate_channel_update_message(msg == null ? 0 : msg.ptr & ~1);
99 if (ret >= 0 && ret <= 4096) { return null; }
100 NetworkUpdate ret_hu_conv = NetworkUpdate.constr_from_ptr(ret);
101 ret_hu_conv.ptrs_to.add(ret_hu_conv);
106 * Utility method to constructs a new ChannelClosed-variant NetworkUpdate
108 public static NetworkUpdate channel_closed(long short_channel_id, boolean is_permanent) {
109 long ret = bindings.NetworkUpdate_channel_closed(short_channel_id, is_permanent);
110 if (ret >= 0 && ret <= 4096) { return null; }
111 NetworkUpdate ret_hu_conv = NetworkUpdate.constr_from_ptr(ret);
112 ret_hu_conv.ptrs_to.add(ret_hu_conv);
117 * Utility method to constructs a new NodeFailure-variant NetworkUpdate
119 public static NetworkUpdate node_failure(byte[] node_id, boolean is_permanent) {
120 long ret = bindings.NetworkUpdate_node_failure(node_id, is_permanent);
121 if (ret >= 0 && ret <= 4096) { return null; }
122 NetworkUpdate ret_hu_conv = NetworkUpdate.constr_from_ptr(ret);
123 ret_hu_conv.ptrs_to.add(ret_hu_conv);
128 * Serialize the NetworkUpdate object into a byte array which can be read by NetworkUpdate_read
130 public byte[] write() {
131 byte[] ret = bindings.NetworkUpdate_write(this.ptr);