Merge pull request #120 from TheBlueMatt/main
[ldk-java] / src / main / java / org / ldk / structs / NetworkUpdate.java
index 4c48c3056f40ce4ce187321571cad58d7d229fa5..60d3bfd58f71f93eae85353a03e812ba7110ffee 100644 (file)
@@ -4,6 +4,7 @@ import org.ldk.impl.bindings;
 import org.ldk.enums.*;
 import org.ldk.util.*;
 import java.util.Arrays;
+import java.lang.ref.Reference;
 import javax.annotation.Nullable;
 
 
@@ -11,7 +12,7 @@ import javax.annotation.Nullable;
  * Update to the [`NetworkGraph`] based on payment failure information conveyed via the Onion
  * return packet by a node along the route. See [BOLT #4] for details.
  * 
- * [BOLT #4]: https://github.com/lightningnetwork/lightning-rfc/blob/master/04-onion-routing.md
+ * [BOLT #4]: https://github.com/lightning/bolts/blob/master/04-onion-routing.md
  */
 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
 public class NetworkUpdate extends CommonBase {
@@ -26,8 +27,8 @@ public class NetworkUpdate extends CommonBase {
                if (raw_val.getClass() == bindings.LDKNetworkUpdate.ChannelUpdateMessage.class) {
                        return new ChannelUpdateMessage(ptr, (bindings.LDKNetworkUpdate.ChannelUpdateMessage)raw_val);
                }
-               if (raw_val.getClass() == bindings.LDKNetworkUpdate.ChannelClosed.class) {
-                       return new ChannelClosed(ptr, (bindings.LDKNetworkUpdate.ChannelClosed)raw_val);
+               if (raw_val.getClass() == bindings.LDKNetworkUpdate.ChannelFailure.class) {
+                       return new ChannelFailure(ptr, (bindings.LDKNetworkUpdate.ChannelFailure)raw_val);
                }
                if (raw_val.getClass() == bindings.LDKNetworkUpdate.NodeFailure.class) {
                        return new NodeFailure(ptr, (bindings.LDKNetworkUpdate.NodeFailure)raw_val);
@@ -35,20 +36,28 @@ public class NetworkUpdate extends CommonBase {
                assert false; return null; // Unreachable without extending the (internal) bindings interface
        }
 
+       /**
+        * An error indicating a `channel_update` messages should be applied via
+        * [`NetworkGraph::update_channel`].
+        */
        public final static class ChannelUpdateMessage extends NetworkUpdate {
                /**
                 * The update to apply via [`NetworkGraph::update_channel`].
                */
-               public final ChannelUpdate msg;
+               public final org.ldk.structs.ChannelUpdate msg;
                private ChannelUpdateMessage(long ptr, bindings.LDKNetworkUpdate.ChannelUpdateMessage obj) {
                        super(null, ptr);
                        long msg = obj.msg;
-                       ChannelUpdate msg_hu_conv = new ChannelUpdate(null, msg);
-                       msg_hu_conv.ptrs_to.add(this);
+                       org.ldk.structs.ChannelUpdate msg_hu_conv = null; if (msg < 0 || msg > 4096) { msg_hu_conv = new org.ldk.structs.ChannelUpdate(null, msg); }
+                       if (msg_hu_conv != null) { msg_hu_conv.ptrs_to.add(this); };
                        this.msg = msg_hu_conv;
                }
        }
-       public final static class ChannelClosed extends NetworkUpdate {
+       /**
+        * An error indicating that a channel failed to route a payment, which should be applied via
+        * [`NetworkGraph::channel_failed`].
+        */
+       public final static class ChannelFailure extends NetworkUpdate {
                /**
                 * The short channel id of the closed channel.
                */
@@ -58,12 +67,16 @@ public class NetworkUpdate extends CommonBase {
                 * `channel_update` message is received.
                */
                public final boolean is_permanent;
-               private ChannelClosed(long ptr, bindings.LDKNetworkUpdate.ChannelClosed obj) {
+               private ChannelFailure(long ptr, bindings.LDKNetworkUpdate.ChannelFailure obj) {
                        super(null, ptr);
                        this.short_channel_id = obj.short_channel_id;
                        this.is_permanent = obj.is_permanent;
                }
        }
+       /**
+        * An error indicating that a node failed to route a payment, which should be applied via
+        * [`NetworkGraph::node_failed_permanent`] if permanent.
+        */
        public final static class NodeFailure extends NetworkUpdate {
                /**
                 * The node id of the failed node.
@@ -80,14 +93,21 @@ public class NetworkUpdate extends CommonBase {
                        this.is_permanent = obj.is_permanent;
                }
        }
+       long clone_ptr() {
+               long ret = bindings.NetworkUpdate_clone_ptr(this.ptr);
+               Reference.reachabilityFence(this);
+               return ret;
+       }
+
        /**
         * Creates a copy of the NetworkUpdate
         */
        public NetworkUpdate clone() {
                long ret = bindings.NetworkUpdate_clone(this.ptr);
-               if (ret >= 0 && ret < 1024) { return null; }
-               NetworkUpdate ret_hu_conv = NetworkUpdate.constr_from_ptr(ret);
-               ret_hu_conv.ptrs_to.add(this);
+               Reference.reachabilityFence(this);
+               if (ret >= 0 && ret <= 4096) { return null; }
+               org.ldk.structs.NetworkUpdate ret_hu_conv = org.ldk.structs.NetworkUpdate.constr_from_ptr(ret);
+               if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
                return ret_hu_conv;
        }
 
@@ -95,21 +115,25 @@ public class NetworkUpdate extends CommonBase {
         * Utility method to constructs a new ChannelUpdateMessage-variant NetworkUpdate
         */
        public static NetworkUpdate channel_update_message(ChannelUpdate msg) {
-               long ret = bindings.NetworkUpdate_channel_update_message(msg == null ? 0 : msg.ptr & ~1);
-               if (ret >= 0 && ret < 1024) { return null; }
-               NetworkUpdate ret_hu_conv = NetworkUpdate.constr_from_ptr(ret);
-               ret_hu_conv.ptrs_to.add(ret_hu_conv);
+               long ret = bindings.NetworkUpdate_channel_update_message(msg == null ? 0 : msg.ptr);
+               Reference.reachabilityFence(msg);
+               if (ret >= 0 && ret <= 4096) { return null; }
+               org.ldk.structs.NetworkUpdate ret_hu_conv = org.ldk.structs.NetworkUpdate.constr_from_ptr(ret);
+               if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
+               if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(msg); };
                return ret_hu_conv;
        }
 
        /**
-        * Utility method to constructs a new ChannelClosed-variant NetworkUpdate
+        * Utility method to constructs a new ChannelFailure-variant NetworkUpdate
         */
-       public static NetworkUpdate channel_closed(long short_channel_id, boolean is_permanent) {
-               long ret = bindings.NetworkUpdate_channel_closed(short_channel_id, is_permanent);
-               if (ret >= 0 && ret < 1024) { return null; }
-               NetworkUpdate ret_hu_conv = NetworkUpdate.constr_from_ptr(ret);
-               ret_hu_conv.ptrs_to.add(ret_hu_conv);
+       public static NetworkUpdate channel_failure(long short_channel_id, boolean is_permanent) {
+               long ret = bindings.NetworkUpdate_channel_failure(short_channel_id, is_permanent);
+               Reference.reachabilityFence(short_channel_id);
+               Reference.reachabilityFence(is_permanent);
+               if (ret >= 0 && ret <= 4096) { return null; }
+               org.ldk.structs.NetworkUpdate ret_hu_conv = org.ldk.structs.NetworkUpdate.constr_from_ptr(ret);
+               if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
                return ret_hu_conv;
        }
 
@@ -117,18 +141,36 @@ public class NetworkUpdate extends CommonBase {
         * Utility method to constructs a new NodeFailure-variant NetworkUpdate
         */
        public static NetworkUpdate node_failure(byte[] node_id, boolean is_permanent) {
-               long ret = bindings.NetworkUpdate_node_failure(node_id, is_permanent);
-               if (ret >= 0 && ret < 1024) { return null; }
-               NetworkUpdate ret_hu_conv = NetworkUpdate.constr_from_ptr(ret);
-               ret_hu_conv.ptrs_to.add(ret_hu_conv);
+               long ret = bindings.NetworkUpdate_node_failure(InternalUtils.check_arr_len(node_id, 33), is_permanent);
+               Reference.reachabilityFence(node_id);
+               Reference.reachabilityFence(is_permanent);
+               if (ret >= 0 && ret <= 4096) { return null; }
+               org.ldk.structs.NetworkUpdate ret_hu_conv = org.ldk.structs.NetworkUpdate.constr_from_ptr(ret);
+               if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
                return ret_hu_conv;
        }
 
+       /**
+        * Checks if two NetworkUpdates contain equal inner contents.
+        * This ignores pointers and is_owned flags and looks at the values in fields.
+        */
+       public boolean eq(NetworkUpdate b) {
+               boolean ret = bindings.NetworkUpdate_eq(this.ptr, b == null ? 0 : b.ptr);
+               Reference.reachabilityFence(this);
+               Reference.reachabilityFence(b);
+               return ret;
+       }
+
+       @Override public boolean equals(Object o) {
+               if (!(o instanceof NetworkUpdate)) return false;
+               return this.eq((NetworkUpdate)o);
+       }
        /**
         * Serialize the NetworkUpdate object into a byte array which can be read by NetworkUpdate_read
         */
        public byte[] write() {
                byte[] ret = bindings.NetworkUpdate_write(this.ptr);
+               Reference.reachabilityFence(this);
                return ret;
        }