X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fldk%2Fstructs%2FPeerManager.java;h=c12f1d159c57587c0602d994aaf1fadc41ad47ee;hb=07d5d868dfe064aadb28a7f7ca6002c16be9723d;hp=e44bc321e8849cab1f8e47af419828d999ea4671;hpb=8aa8a96fc6d8fcdd3fbb419b4b4c12482af14938;p=ldk-java diff --git a/src/main/java/org/ldk/structs/PeerManager.java b/src/main/java/org/ldk/structs/PeerManager.java index e44bc321..c12f1d15 100644 --- a/src/main/java/org/ldk/structs/PeerManager.java +++ b/src/main/java/org/ldk/structs/PeerManager.java @@ -21,10 +21,10 @@ import javax.annotation.Nullable; * [`PeerManager`] functions related to the same connection must occur only in serial, making new * calls only after previous ones have returned. * - * Rather than using a plain PeerManager, it is preferable to use either a SimpleArcPeerManager - * a SimpleRefPeerManager, for conciseness. See their documentation for more details, but - * essentially you should default to using a SimpleRefPeerManager, and use a - * SimpleArcPeerManager when you require a PeerManager with a static lifetime, such as when + * Rather than using a plain [`PeerManager`], it is preferable to use either a [`SimpleArcPeerManager`] + * a [`SimpleRefPeerManager`], for conciseness. See their documentation for more details, but + * essentially you should default to using a [`SimpleRefPeerManager`], and use a + * [`SimpleArcPeerManager`] when you require a `PeerManager` with a static lifetime, such as when * you're using lightning-net-tokio. * * [`read_event`]: PeerManager::read_event @@ -39,8 +39,9 @@ public class PeerManager extends CommonBase { } /** - * Constructs a new PeerManager with the given message handlers and node_id secret key - * ephemeral_random_data is used to derive per-connection ephemeral keys and must be + * Constructs a new `PeerManager` with the given message handlers. + * + * `ephemeral_random_data` is used to derive per-connection ephemeral keys and must be * cryptographically secure random bytes. * * `current_time` is used as an always-increasing counter that survives across restarts and is @@ -48,42 +49,56 @@ public class PeerManager extends CommonBase { * timestamp, however if it is not available a persistent counter that increases once per * minute should suffice. */ - public static PeerManager of(ChannelMessageHandler message_handler_chan_handler_arg, RoutingMessageHandler message_handler_route_handler_arg, OnionMessageHandler message_handler_onion_message_handler_arg, byte[] our_node_secret, int current_time, byte[] ephemeral_random_data, org.ldk.structs.Logger logger, org.ldk.structs.CustomMessageHandler custom_message_handler) { - long ret = bindings.PeerManager_new(bindings.MessageHandler_new(message_handler_chan_handler_arg == null ? 0 : message_handler_chan_handler_arg.ptr, message_handler_route_handler_arg == null ? 0 : message_handler_route_handler_arg.ptr, message_handler_onion_message_handler_arg == null ? 0 : message_handler_onion_message_handler_arg.ptr), InternalUtils.check_arr_len(our_node_secret, 32), current_time, InternalUtils.check_arr_len(ephemeral_random_data, 32), logger == null ? 0 : logger.ptr, custom_message_handler == null ? 0 : custom_message_handler.ptr); + public static PeerManager of(ChannelMessageHandler message_handler_chan_handler_arg, RoutingMessageHandler message_handler_route_handler_arg, OnionMessageHandler message_handler_onion_message_handler_arg, CustomMessageHandler message_handler_custom_message_handler_arg, int current_time, byte[] ephemeral_random_data, org.ldk.structs.Logger logger, org.ldk.structs.NodeSigner node_signer) { + long ret = bindings.PeerManager_new(bindings.MessageHandler_new(message_handler_chan_handler_arg.ptr, message_handler_route_handler_arg.ptr, message_handler_onion_message_handler_arg.ptr, message_handler_custom_message_handler_arg.ptr), current_time, InternalUtils.check_arr_len(ephemeral_random_data, 32), logger.ptr, node_signer.ptr); Reference.reachabilityFence(message_handler_chan_handler_arg); Reference.reachabilityFence(message_handler_route_handler_arg); Reference.reachabilityFence(message_handler_onion_message_handler_arg); - Reference.reachabilityFence(our_node_secret); + Reference.reachabilityFence(message_handler_custom_message_handler_arg); Reference.reachabilityFence(current_time); Reference.reachabilityFence(ephemeral_random_data); Reference.reachabilityFence(logger); - Reference.reachabilityFence(custom_message_handler); + Reference.reachabilityFence(node_signer); if (ret >= 0 && ret <= 4096) { return null; } org.ldk.structs.PeerManager ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.PeerManager(null, 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(message_handler_chan_handler_arg); }; if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(message_handler_route_handler_arg); }; if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(message_handler_onion_message_handler_arg); }; + if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(message_handler_custom_message_handler_arg); }; if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(logger); }; - if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(custom_message_handler); }; + if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(node_signer); }; return ret_hu_conv; } /** - * Get the list of node ids for peers which have completed the initial handshake. + * Get a list of tuples mapping from node id to network addresses for peers which have + * completed the initial handshake. + * + * For outbound connections, the [`PublicKey`] will be the same as the `their_node_id` parameter + * passed in to [`Self::new_outbound_connection`], however entries will only appear once the initial + * handshake has completed and we are sure the remote peer has the private key for the given + * [`PublicKey`]. * - * For outbound connections, this will be the same as the their_node_id parameter passed in to - * new_outbound_connection, however entries will only appear once the initial handshake has - * completed and we are sure the remote peer has the private key for the given node_id. + * The returned `Option`s will only be `Some` if an address had been previously given via + * [`Self::new_outbound_connection`] or [`Self::new_inbound_connection`]. */ - public byte[][] get_peer_node_ids() { - byte[][] ret = bindings.PeerManager_get_peer_node_ids(this.ptr); + public TwoTuple_PublicKeyCOption_SocketAddressZZ[] get_peer_node_ids() { + long[] ret = bindings.PeerManager_get_peer_node_ids(this.ptr); Reference.reachabilityFence(this); - return ret; + int ret_conv_43_len = ret.length; + TwoTuple_PublicKeyCOption_SocketAddressZZ[] ret_conv_43_arr = new TwoTuple_PublicKeyCOption_SocketAddressZZ[ret_conv_43_len]; + for (int r = 0; r < ret_conv_43_len; r++) { + long ret_conv_43 = ret[r]; + TwoTuple_PublicKeyCOption_SocketAddressZZ ret_conv_43_hu_conv = new TwoTuple_PublicKeyCOption_SocketAddressZZ(null, ret_conv_43); + if (ret_conv_43_hu_conv != null) { ret_conv_43_hu_conv.ptrs_to.add(this); }; + ret_conv_43_arr[r] = ret_conv_43_hu_conv; + } + return ret_conv_43_arr; } /** - * Indicates a new outbound connection has been established to a node with the given node_id + * Indicates a new outbound connection has been established to a node with the given `node_id` * and an optional remote network address. * * The remote network address adds the option to report a remote IP address back to a connecting @@ -95,12 +110,12 @@ public class PeerManager extends CommonBase { * Returns a small number of bytes to send to the remote node (currently always 50). * * Panics if descriptor is duplicative with some other descriptor which has not yet been - * [`socket_disconnected()`]. + * [`socket_disconnected`]. * - * [`socket_disconnected()`]: PeerManager::socket_disconnected + * [`socket_disconnected`]: PeerManager::socket_disconnected */ - public Result_CVec_u8ZPeerHandleErrorZ new_outbound_connection(byte[] their_node_id, org.ldk.structs.SocketDescriptor descriptor, org.ldk.structs.Option_NetAddressZ remote_network_address) { - long ret = bindings.PeerManager_new_outbound_connection(this.ptr, InternalUtils.check_arr_len(their_node_id, 33), descriptor == null ? 0 : descriptor.ptr, remote_network_address.ptr); + public Result_CVec_u8ZPeerHandleErrorZ new_outbound_connection(byte[] their_node_id, org.ldk.structs.SocketDescriptor descriptor, org.ldk.structs.Option_SocketAddressZ remote_network_address) { + long ret = bindings.PeerManager_new_outbound_connection(this.ptr, InternalUtils.check_arr_len(their_node_id, 33), descriptor.ptr, remote_network_address.ptr); Reference.reachabilityFence(this); Reference.reachabilityFence(their_node_id); Reference.reachabilityFence(descriptor); @@ -108,6 +123,7 @@ public class PeerManager extends CommonBase { if (ret >= 0 && ret <= 4096) { return null; } Result_CVec_u8ZPeerHandleErrorZ ret_hu_conv = Result_CVec_u8ZPeerHandleErrorZ.constr_from_ptr(ret); if (this != null) { this.ptrs_to.add(descriptor); }; + if (this != null) { this.ptrs_to.add(remote_network_address); }; return ret_hu_conv; } @@ -124,18 +140,19 @@ public class PeerManager extends CommonBase { * the connection immediately. * * Panics if descriptor is duplicative with some other descriptor which has not yet been - * [`socket_disconnected()`]. + * [`socket_disconnected`]. * - * [`socket_disconnected()`]: PeerManager::socket_disconnected + * [`socket_disconnected`]: PeerManager::socket_disconnected */ - public Result_NonePeerHandleErrorZ new_inbound_connection(org.ldk.structs.SocketDescriptor descriptor, org.ldk.structs.Option_NetAddressZ remote_network_address) { - long ret = bindings.PeerManager_new_inbound_connection(this.ptr, descriptor == null ? 0 : descriptor.ptr, remote_network_address.ptr); + public Result_NonePeerHandleErrorZ new_inbound_connection(org.ldk.structs.SocketDescriptor descriptor, org.ldk.structs.Option_SocketAddressZ remote_network_address) { + long ret = bindings.PeerManager_new_inbound_connection(this.ptr, descriptor.ptr, remote_network_address.ptr); Reference.reachabilityFence(this); Reference.reachabilityFence(descriptor); Reference.reachabilityFence(remote_network_address); if (ret >= 0 && ret <= 4096) { return null; } Result_NonePeerHandleErrorZ ret_hu_conv = Result_NonePeerHandleErrorZ.constr_from_ptr(ret); if (this != null) { this.ptrs_to.add(descriptor); }; + if (this != null) { this.ptrs_to.add(remote_network_address); }; return ret_hu_conv; } @@ -147,14 +164,14 @@ public class PeerManager extends CommonBase { * May call [`send_data`] on the descriptor passed in (or an equal descriptor) before * returning. Thus, be very careful with reentrancy issues! The invariants around calling * [`write_buffer_space_avail`] in case a write did not fully complete must still hold - be - * ready to call `[write_buffer_space_avail`] again if a write call generated here isn't + * ready to call [`write_buffer_space_avail`] again if a write call generated here isn't * sufficient! * * [`send_data`]: SocketDescriptor::send_data * [`write_buffer_space_avail`]: PeerManager::write_buffer_space_avail */ public Result_NonePeerHandleErrorZ write_buffer_space_avail(org.ldk.structs.SocketDescriptor descriptor) { - long ret = bindings.PeerManager_write_buffer_space_avail(this.ptr, descriptor == null ? 0 : descriptor.ptr); + long ret = bindings.PeerManager_write_buffer_space_avail(this.ptr, descriptor.ptr); Reference.reachabilityFence(this); Reference.reachabilityFence(descriptor); if (ret >= 0 && ret <= 4096) { return null; } @@ -175,11 +192,14 @@ public class PeerManager extends CommonBase { * [`send_data`] call on this descriptor has `resume_read` set (preventing DoS issues in the * send buffer). * + * In order to avoid processing too many messages at once per peer, `data` should be on the + * order of 4KiB. + * * [`send_data`]: SocketDescriptor::send_data * [`process_events`]: PeerManager::process_events */ public Result_boolPeerHandleErrorZ read_event(org.ldk.structs.SocketDescriptor peer_descriptor, byte[] data) { - long ret = bindings.PeerManager_read_event(this.ptr, peer_descriptor == null ? 0 : peer_descriptor.ptr, data); + long ret = bindings.PeerManager_read_event(this.ptr, peer_descriptor.ptr, data); Reference.reachabilityFence(this); Reference.reachabilityFence(peer_descriptor); Reference.reachabilityFence(data); @@ -216,7 +236,7 @@ public class PeerManager extends CommonBase { * Indicates that the given socket descriptor's connection is now closed. */ public void socket_disconnected(org.ldk.structs.SocketDescriptor descriptor) { - bindings.PeerManager_socket_disconnected(this.ptr, descriptor == null ? 0 : descriptor.ptr); + bindings.PeerManager_socket_disconnected(this.ptr, descriptor.ptr); Reference.reachabilityFence(this); Reference.reachabilityFence(descriptor); } @@ -224,19 +244,15 @@ public class PeerManager extends CommonBase { /** * Disconnect a peer given its node id. * - * Set `no_connection_possible` to true to prevent any further connection with this peer, - * force-closing any channels we have with it. - * * If a peer is connected, this will call [`disconnect_socket`] on the descriptor for the * peer. Thus, be very careful about reentrancy issues. * * [`disconnect_socket`]: SocketDescriptor::disconnect_socket */ - public void disconnect_by_node_id(byte[] node_id, boolean no_connection_possible) { - bindings.PeerManager_disconnect_by_node_id(this.ptr, InternalUtils.check_arr_len(node_id, 33), no_connection_possible); + public void disconnect_by_node_id(byte[] node_id) { + bindings.PeerManager_disconnect_by_node_id(this.ptr, InternalUtils.check_arr_len(node_id, 33)); Reference.reachabilityFence(this); Reference.reachabilityFence(node_id); - Reference.reachabilityFence(no_connection_possible); } /** @@ -284,12 +300,13 @@ public class PeerManager extends CommonBase { * * [`get_and_clear_pending_msg_events`]: MessageSendEventsProvider::get_and_clear_pending_msg_events */ - public void broadcast_node_announcement(byte[] rgb, byte[] alias, NetAddress[] addresses) { - bindings.PeerManager_broadcast_node_announcement(this.ptr, InternalUtils.check_arr_len(rgb, 3), InternalUtils.check_arr_len(alias, 32), addresses != null ? Arrays.stream(addresses).mapToLong(addresses_conv_12 -> addresses_conv_12.ptr).toArray() : null); + public void broadcast_node_announcement(byte[] rgb, byte[] alias, SocketAddress[] addresses) { + bindings.PeerManager_broadcast_node_announcement(this.ptr, InternalUtils.check_arr_len(rgb, 3), InternalUtils.check_arr_len(alias, 32), addresses != null ? Arrays.stream(addresses).mapToLong(addresses_conv_15 -> addresses_conv_15.ptr).toArray() : null); Reference.reachabilityFence(this); Reference.reachabilityFence(rgb); Reference.reachabilityFence(alias); Reference.reachabilityFence(addresses); + for (SocketAddress addresses_conv_15: addresses) { if (this != null) { this.ptrs_to.add(addresses_conv_15); }; }; } }