From: Matt Corallo Date: Wed, 13 Apr 2022 17:06:39 +0000 (+0000) Subject: [Java] Update LDK batteries to latest upstream API X-Git-Tag: v0.0.106.0~1^2~10 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=1ef2dc8b8a3e0e244ad82ac08e286df489eca4b7;p=ldk-java [Java] Update LDK batteries to latest upstream API --- diff --git a/src/main/java/org/ldk/batteries/ChannelManagerConstructor.java b/src/main/java/org/ldk/batteries/ChannelManagerConstructor.java index c24d0bd3..122fcff8 100644 --- a/src/main/java/org/ldk/batteries/ChannelManagerConstructor.java +++ b/src/main/java/org/ldk/batteries/ChannelManagerConstructor.java @@ -62,10 +62,17 @@ public class ChannelManagerConstructor { @Nullable public InvoicePayer payer; private final ChainMonitor chain_monitor; - @Nullable private final NetworkGraph net_graph; + + /** + * The `NetworkGraph` deserialized from the byte given to the constructor when deserializing or the `NetworkGraph` + * given explicitly to the new-object constructor. + */ + @Nullable public final NetworkGraph net_graph; @Nullable private final NetGraphMsgHandler graph_msg_handler; private final Logger logger; + private final byte[] router_rand_bytes; + /** * Deserializes a channel manager and a set of channel monitors from the given serialized copies and interface implementations * @@ -75,7 +82,7 @@ public class ChannelManagerConstructor { */ public ChannelManagerConstructor(byte[] channel_manager_serialized, byte[][] channel_monitors_serialized, UserConfig config, KeysInterface keys_interface, FeeEstimator fee_estimator, ChainMonitor chain_monitor, - @Nullable Filter filter, @Nullable NetworkGraph net_graph, + @Nullable Filter filter, @Nullable byte[] net_graph_serialized, BroadcasterInterface tx_broadcaster, Logger logger) throws InvalidSerializedDataException { final IgnoringMessageHandler no_custom_messages = IgnoringMessageHandler.of(); final ChannelMonitor[] monitors = new ChannelMonitor[channel_monitors_serialized.length]; @@ -95,7 +102,7 @@ public class ChannelManagerConstructor { Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ res = UtilMethods.C2Tuple_BlockHashChannelManagerZ_read(channel_manager_serialized, keys_interface, fee_estimator, chain_monitor.as_Watch(), tx_broadcaster, logger, config, monitors); - if (res instanceof Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ.Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ_Err) { + if (!res.is_ok()) { throw new InvalidSerializedDataException("Serialized ChannelManager was corrupt"); } this.channel_manager = ((Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ.Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ_OK)res).res.get_b(); @@ -103,7 +110,15 @@ public class ChannelManagerConstructor { this.chain_monitor = chain_monitor; this.logger = logger; byte[] random_data = keys_interface.get_secure_random_bytes(); - this.net_graph = net_graph; + if (net_graph_serialized != null) { + Result_NetworkGraphDecodeErrorZ graph_res = NetworkGraph.read(net_graph_serialized); + if (!graph_res.is_ok()) { + throw new InvalidSerializedDataException("Serialized Network Graph was corrupt"); + } + this.net_graph = ((Result_NetworkGraphDecodeErrorZ.Result_NetworkGraphDecodeErrorZ_OK)graph_res).res; + } else { + this.net_graph = null; + } Result_SecretKeyNoneZ node_secret = keys_interface.get_node_secret(Recipient.LDKRecipient_Node); assert node_secret.is_ok(); if (net_graph != null) { @@ -131,6 +146,7 @@ public class ChannelManagerConstructor { monitor.load_outputs_to_watch(filter); } } + router_rand_bytes = keys_interface.get_secure_random_bytes(); } /** @@ -172,6 +188,7 @@ public class ChannelManagerConstructor { throw new IllegalStateException("We should never fail to construct nio objects unless we're on a platform that cannot run LDK."); } this.nio_peer_handler = nio_peer_handler; + router_rand_bytes = keys_interface.get_secure_random_bytes(); } /** @@ -181,6 +198,7 @@ public class ChannelManagerConstructor { public interface EventHandler { void handle_event(Event events); void persist_manager(byte[] channel_manager_bytes); + void persist_network_graph(byte[] network_graph); } BackgroundProcessor background_processor = null; @@ -199,15 +217,24 @@ public class ChannelManagerConstructor { } org.ldk.structs.EventHandler ldk_handler = org.ldk.structs.EventHandler.new_impl(event_handler::handle_event); if (this.net_graph != null && scorer != null) { - Router router = DefaultRouter.of(net_graph, logger).as_Router(); + Router router = DefaultRouter.of(net_graph, logger, router_rand_bytes).as_Router(); this.payer = InvoicePayer.of(this.channel_manager.as_Payer(), router, scorer, this.logger, ldk_handler, RetryAttempts.of(3)); assert this.payer != null; ldk_handler = this.payer.as_EventHandler(); } - background_processor = BackgroundProcessor.start(org.ldk.structs.ChannelManagerPersister.new_impl(channel_manager -> { - event_handler.persist_manager(channel_manager.write()); - return Result_NoneErrorZ.ok(); + background_processor = BackgroundProcessor.start(Persister.new_impl(new Persister.PersisterInterface() { + @Override + public Result_NoneErrorZ persist_manager(ChannelManager channel_manager) { + event_handler.persist_manager(channel_manager.write()); + return Result_NoneErrorZ.ok(); + } + + @Override + public Result_NoneErrorZ persist_graph(NetworkGraph network_graph) { + event_handler.persist_network_graph(network_graph.write()); + return Result_NoneErrorZ.ok(); + } }), ldk_handler, this.chain_monitor, this.channel_manager, this.graph_msg_handler, this.peer_manager, this.logger); } diff --git a/src/main/java/org/ldk/batteries/NioPeerHandler.java b/src/main/java/org/ldk/batteries/NioPeerHandler.java index 5963506f..2d387264 100644 --- a/src/main/java/org/ldk/batteries/NioPeerHandler.java +++ b/src/main/java/org/ldk/batteries/NioPeerHandler.java @@ -6,9 +6,8 @@ import org.ldk.structs.*; import java.io.IOException; import java.lang.reflect.Field; import java.lang.ref.Reference; +import java.net.*; import java.util.LinkedList; -import java.net.SocketAddress; -import java.net.StandardSocketOptions; import java.nio.Buffer; import java.nio.ByteBuffer; import java.nio.channels.*; @@ -121,6 +120,19 @@ public class NioPeerHandler { long socket_id; volatile boolean shutdown = false; + private static Option_NetAddressZ get_netaddr_from_sockaddr(SocketAddress sockaddr) { + if (sockaddr instanceof InetSocketAddress) { + InetAddress addr = ((InetSocketAddress) sockaddr).getAddress(); + short port = (short) ((InetSocketAddress) sockaddr).getPort(); + if (addr instanceof Inet4Address) { + return Option_NetAddressZ.some(NetAddress.ipv4(addr.getAddress(), port)); + } else if (addr instanceof Inet6Address) { + return Option_NetAddressZ.some(NetAddress.ipv6(addr.getAddress(), port)); + } + } + return Option_NetAddressZ.none(); + } + /** * Constructs a new peer handler, spawning a thread to monitor for socket events. * @@ -176,7 +188,8 @@ public class NioPeerHandler { try { Peer peer = setup_socket(chan); peer.key = chan.register(this.selector, SelectionKey.OP_READ, peer); - Result_NonePeerHandleErrorZ res = this.peer_manager.new_inbound_connection(peer.descriptor); + Option_NetAddressZ netaddr = get_netaddr_from_sockaddr(chan.getRemoteAddress()); + Result_NonePeerHandleErrorZ res = this.peer_manager.new_inbound_connection(peer.descriptor, netaddr); if (res instanceof Result_NonePeerHandleErrorZ.Result_NonePeerHandleErrorZ_Err) { peer.descriptor.disconnect_socket(); } @@ -285,7 +298,7 @@ public class NioPeerHandler { } Peer peer = setup_socket(chan); do_selector_action(() -> peer.key = chan.register(this.selector, SelectionKey.OP_READ, peer)); - Result_CVec_u8ZPeerHandleErrorZ res = this.peer_manager.new_outbound_connection(their_node_id, peer.descriptor); + Result_CVec_u8ZPeerHandleErrorZ res = this.peer_manager.new_outbound_connection(their_node_id, peer.descriptor, get_netaddr_from_sockaddr(remote)); if (res instanceof Result_CVec_u8ZPeerHandleErrorZ.Result_CVec_u8ZPeerHandleErrorZ_OK) { byte[] initial_bytes = ((Result_CVec_u8ZPeerHandleErrorZ.Result_CVec_u8ZPeerHandleErrorZ_OK) res).res; if (chan.write(ByteBuffer.wrap(initial_bytes)) != initial_bytes.length) {