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
39 * An error indicating a `channel_update` messages should be applied via
40 * [`NetworkGraph::update_channel`].
42 public final static class ChannelUpdateMessage extends NetworkUpdate {
44 * The update to apply via [`NetworkGraph::update_channel`].
46 public final ChannelUpdate msg;
47 private ChannelUpdateMessage(long ptr, bindings.LDKNetworkUpdate.ChannelUpdateMessage obj) {
50 ChannelUpdate msg_hu_conv = null; if (msg < 0 || msg > 4096) { msg_hu_conv = new ChannelUpdate(null, msg); }
51 msg_hu_conv.ptrs_to.add(this);
52 this.msg = msg_hu_conv;
56 * An error indicating only that a channel has been closed, which should be applied via
57 * [`NetworkGraph::close_channel_from_update`].
59 public final static class ChannelClosed extends NetworkUpdate {
61 * The short channel id of the closed channel.
63 public final long short_channel_id;
65 * Whether the channel should be permanently removed or temporarily disabled until a new
66 * `channel_update` message is received.
68 public final boolean is_permanent;
69 private ChannelClosed(long ptr, bindings.LDKNetworkUpdate.ChannelClosed obj) {
71 this.short_channel_id = obj.short_channel_id;
72 this.is_permanent = obj.is_permanent;
76 * An error indicating only that a node has failed, which should be applied via
77 * [`NetworkGraph::fail_node`].
79 public final static class NodeFailure extends NetworkUpdate {
81 * The node id of the failed node.
83 public final byte[] node_id;
85 * Whether the node should be permanently removed from consideration or can be restored
86 * when a new `channel_update` message is received.
88 public final boolean is_permanent;
89 private NodeFailure(long ptr, bindings.LDKNetworkUpdate.NodeFailure obj) {
91 this.node_id = obj.node_id;
92 this.is_permanent = obj.is_permanent;
96 long ret = bindings.NetworkUpdate_clone_ptr(this.ptr);
101 * Creates a copy of the NetworkUpdate
103 public NetworkUpdate clone() {
104 long ret = bindings.NetworkUpdate_clone(this.ptr);
105 if (ret >= 0 && ret <= 4096) { return null; }
106 NetworkUpdate ret_hu_conv = NetworkUpdate.constr_from_ptr(ret);
107 ret_hu_conv.ptrs_to.add(this);
112 * Utility method to constructs a new ChannelUpdateMessage-variant NetworkUpdate
114 public static NetworkUpdate channel_update_message(ChannelUpdate msg) {
115 long ret = bindings.NetworkUpdate_channel_update_message(msg == null ? 0 : msg.ptr & ~1);
116 if (ret >= 0 && ret <= 4096) { return null; }
117 NetworkUpdate ret_hu_conv = NetworkUpdate.constr_from_ptr(ret);
118 ret_hu_conv.ptrs_to.add(ret_hu_conv);
123 * Utility method to constructs a new ChannelClosed-variant NetworkUpdate
125 public static NetworkUpdate channel_closed(long short_channel_id, boolean is_permanent) {
126 long ret = bindings.NetworkUpdate_channel_closed(short_channel_id, is_permanent);
127 if (ret >= 0 && ret <= 4096) { return null; }
128 NetworkUpdate ret_hu_conv = NetworkUpdate.constr_from_ptr(ret);
129 ret_hu_conv.ptrs_to.add(ret_hu_conv);
134 * Utility method to constructs a new NodeFailure-variant NetworkUpdate
136 public static NetworkUpdate node_failure(byte[] node_id, boolean is_permanent) {
137 long ret = bindings.NetworkUpdate_node_failure(InternalUtils.check_arr_len(node_id, 33), is_permanent);
138 if (ret >= 0 && ret <= 4096) { return null; }
139 NetworkUpdate ret_hu_conv = NetworkUpdate.constr_from_ptr(ret);
140 ret_hu_conv.ptrs_to.add(ret_hu_conv);
145 * Serialize the NetworkUpdate object into a byte array which can be read by NetworkUpdate_read
147 public byte[] write() {
148 byte[] ret = bindings.NetworkUpdate_write(this.ptr);