1 package org.ldk.structs;
3 import org.ldk.impl.bindings;
4 import org.ldk.enums.*;
6 import java.util.Arrays;
7 import java.lang.ref.Reference;
8 import javax.annotation.Nullable;
12 * A PeerManager manages a set of peers, described by their [`SocketDescriptor`] and marshalls
13 * socket events into messages which it passes on to its [`MessageHandler`].
15 * Locks are taken internally, so you must never assume that reentrancy from a
16 * [`SocketDescriptor`] call back into [`PeerManager`] methods will not deadlock.
18 * Calls to [`read_event`] will decode relevant messages and pass them to the
19 * [`ChannelMessageHandler`], likely doing message processing in-line. Thus, the primary form of
20 * parallelism in Rust-Lightning is in calls to [`read_event`]. Note, however, that calls to any
21 * [`PeerManager`] functions related to the same connection must occur only in serial, making new
22 * calls only after previous ones have returned.
24 * Rather than using a plain PeerManager, it is preferable to use either a SimpleArcPeerManager
25 * a SimpleRefPeerManager, for conciseness. See their documentation for more details, but
26 * essentially you should default to using a SimpleRefPeerManager, and use a
27 * SimpleArcPeerManager when you require a PeerManager with a static lifetime, such as when
28 * you're using lightning-net-tokio.
30 * [`read_event`]: PeerManager::read_event
32 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
33 public class PeerManager extends CommonBase {
34 PeerManager(Object _dummy, long ptr) { super(ptr); }
35 @Override @SuppressWarnings("deprecation")
36 protected void finalize() throws Throwable {
38 if (ptr != 0) { bindings.PeerManager_free(ptr); }
42 * Constructs a new PeerManager with the given message handlers and node_id secret key
43 * ephemeral_random_data is used to derive per-connection ephemeral keys and must be
44 * cryptographically secure random bytes.
46 * `current_time` is used as an always-increasing counter that survives across restarts and is
47 * incremented irregularly internally. In general it is best to simply use the current UNIX
48 * timestamp, however if it is not available a persistent counter that increases once per
49 * minute should suffice.
51 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, long current_time, byte[] ephemeral_random_data, Logger logger, CustomMessageHandler custom_message_handler) {
52 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);
53 Reference.reachabilityFence(message_handler_chan_handler_arg);
54 Reference.reachabilityFence(message_handler_route_handler_arg);
55 Reference.reachabilityFence(message_handler_onion_message_handler_arg);
56 Reference.reachabilityFence(our_node_secret);
57 Reference.reachabilityFence(current_time);
58 Reference.reachabilityFence(ephemeral_random_data);
59 Reference.reachabilityFence(logger);
60 Reference.reachabilityFence(custom_message_handler);
61 if (ret >= 0 && ret <= 4096) { return null; }
62 org.ldk.structs.PeerManager ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.PeerManager(null, ret); }
63 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
64 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(message_handler_chan_handler_arg); };
65 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(message_handler_route_handler_arg); };
66 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(message_handler_onion_message_handler_arg); };
67 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(logger); };
68 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(custom_message_handler); };
73 * Get the list of node ids for peers which have completed the initial handshake.
75 * For outbound connections, this will be the same as the their_node_id parameter passed in to
76 * new_outbound_connection, however entries will only appear once the initial handshake has
77 * completed and we are sure the remote peer has the private key for the given node_id.
79 public byte[][] get_peer_node_ids() {
80 byte[][] ret = bindings.PeerManager_get_peer_node_ids(this.ptr);
81 Reference.reachabilityFence(this);
86 * Indicates a new outbound connection has been established to a node with the given node_id
87 * and an optional remote network address.
89 * The remote network address adds the option to report a remote IP address back to a connecting
90 * peer using the init message.
91 * The user should pass the remote network address of the host they are connected to.
93 * If an `Err` is returned here you must disconnect the connection immediately.
95 * Returns a small number of bytes to send to the remote node (currently always 50).
97 * Panics if descriptor is duplicative with some other descriptor which has not yet been
98 * [`socket_disconnected()`].
100 * [`socket_disconnected()`]: PeerManager::socket_disconnected
102 public Result_CVec_u8ZPeerHandleErrorZ new_outbound_connection(byte[] their_node_id, SocketDescriptor descriptor, Option_NetAddressZ remote_network_address) {
103 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);
104 Reference.reachabilityFence(this);
105 Reference.reachabilityFence(their_node_id);
106 Reference.reachabilityFence(descriptor);
107 Reference.reachabilityFence(remote_network_address);
108 if (ret >= 0 && ret <= 4096) { return null; }
109 Result_CVec_u8ZPeerHandleErrorZ ret_hu_conv = Result_CVec_u8ZPeerHandleErrorZ.constr_from_ptr(ret);
110 if (this != null) { this.ptrs_to.add(descriptor); };
115 * Indicates a new inbound connection has been established to a node with an optional remote
118 * The remote network address adds the option to report a remote IP address back to a connecting
119 * peer using the init message.
120 * The user should pass the remote network address of the host they are connected to.
122 * May refuse the connection by returning an Err, but will never write bytes to the remote end
123 * (outbound connector always speaks first). If an `Err` is returned here you must disconnect
124 * the connection immediately.
126 * Panics if descriptor is duplicative with some other descriptor which has not yet been
127 * [`socket_disconnected()`].
129 * [`socket_disconnected()`]: PeerManager::socket_disconnected
131 public Result_NonePeerHandleErrorZ new_inbound_connection(SocketDescriptor descriptor, Option_NetAddressZ remote_network_address) {
132 long ret = bindings.PeerManager_new_inbound_connection(this.ptr, descriptor == null ? 0 : descriptor.ptr, remote_network_address.ptr);
133 Reference.reachabilityFence(this);
134 Reference.reachabilityFence(descriptor);
135 Reference.reachabilityFence(remote_network_address);
136 if (ret >= 0 && ret <= 4096) { return null; }
137 Result_NonePeerHandleErrorZ ret_hu_conv = Result_NonePeerHandleErrorZ.constr_from_ptr(ret);
138 if (this != null) { this.ptrs_to.add(descriptor); };
143 * Indicates that there is room to write data to the given socket descriptor.
145 * May return an Err to indicate that the connection should be closed.
147 * May call [`send_data`] on the descriptor passed in (or an equal descriptor) before
148 * returning. Thus, be very careful with reentrancy issues! The invariants around calling
149 * [`write_buffer_space_avail`] in case a write did not fully complete must still hold - be
150 * ready to call `[write_buffer_space_avail`] again if a write call generated here isn't
153 * [`send_data`]: SocketDescriptor::send_data
154 * [`write_buffer_space_avail`]: PeerManager::write_buffer_space_avail
156 public Result_NonePeerHandleErrorZ write_buffer_space_avail(SocketDescriptor descriptor) {
157 long ret = bindings.PeerManager_write_buffer_space_avail(this.ptr, descriptor == null ? 0 : descriptor.ptr);
158 Reference.reachabilityFence(this);
159 Reference.reachabilityFence(descriptor);
160 if (ret >= 0 && ret <= 4096) { return null; }
161 Result_NonePeerHandleErrorZ ret_hu_conv = Result_NonePeerHandleErrorZ.constr_from_ptr(ret);
166 * Indicates that data was read from the given socket descriptor.
168 * May return an Err to indicate that the connection should be closed.
170 * Will *not* call back into [`send_data`] on any descriptors to avoid reentrancy complexity.
171 * Thus, however, you should call [`process_events`] after any `read_event` to generate
172 * [`send_data`] calls to handle responses.
174 * If `Ok(true)` is returned, further read_events should not be triggered until a
175 * [`send_data`] call on this descriptor has `resume_read` set (preventing DoS issues in the
178 * [`send_data`]: SocketDescriptor::send_data
179 * [`process_events`]: PeerManager::process_events
181 public Result_boolPeerHandleErrorZ read_event(SocketDescriptor peer_descriptor, byte[] data) {
182 long ret = bindings.PeerManager_read_event(this.ptr, peer_descriptor == null ? 0 : peer_descriptor.ptr, data);
183 Reference.reachabilityFence(this);
184 Reference.reachabilityFence(peer_descriptor);
185 Reference.reachabilityFence(data);
186 if (ret >= 0 && ret <= 4096) { return null; }
187 Result_boolPeerHandleErrorZ ret_hu_conv = Result_boolPeerHandleErrorZ.constr_from_ptr(ret);
192 * Checks for any events generated by our handlers and processes them. Includes sending most
193 * response messages as well as messages generated by calls to handler functions directly (eg
194 * functions like [`ChannelManager::process_pending_htlc_forwards`] or [`send_payment`]).
196 * May call [`send_data`] on [`SocketDescriptor`]s. Thus, be very careful with reentrancy
199 * You don't have to call this function explicitly if you are using [`lightning-net-tokio`]
200 * or one of the other clients provided in our language bindings.
202 * Note that if there are any other calls to this function waiting on lock(s) this may return
203 * without doing any work. All available events that need handling will be handled before the
204 * other calls return.
206 * [`send_payment`]: crate::ln::channelmanager::ChannelManager::send_payment
207 * [`ChannelManager::process_pending_htlc_forwards`]: crate::ln::channelmanager::ChannelManager::process_pending_htlc_forwards
208 * [`send_data`]: SocketDescriptor::send_data
210 public void process_events() {
211 bindings.PeerManager_process_events(this.ptr);
212 Reference.reachabilityFence(this);
216 * Indicates that the given socket descriptor's connection is now closed.
218 public void socket_disconnected(SocketDescriptor descriptor) {
219 bindings.PeerManager_socket_disconnected(this.ptr, descriptor == null ? 0 : descriptor.ptr);
220 Reference.reachabilityFence(this);
221 Reference.reachabilityFence(descriptor);
225 * Disconnect a peer given its node id.
227 * Set `no_connection_possible` to true to prevent any further connection with this peer,
228 * force-closing any channels we have with it.
230 * If a peer is connected, this will call [`disconnect_socket`] on the descriptor for the
231 * peer. Thus, be very careful about reentrancy issues.
233 * [`disconnect_socket`]: SocketDescriptor::disconnect_socket
235 public void disconnect_by_node_id(byte[] node_id, boolean no_connection_possible) {
236 bindings.PeerManager_disconnect_by_node_id(this.ptr, InternalUtils.check_arr_len(node_id, 33), no_connection_possible);
237 Reference.reachabilityFence(this);
238 Reference.reachabilityFence(node_id);
239 Reference.reachabilityFence(no_connection_possible);
243 * Disconnects all currently-connected peers. This is useful on platforms where there may be
244 * an indication that TCP sockets have stalled even if we weren't around to time them out
245 * using regular ping/pongs.
247 public void disconnect_all_peers() {
248 bindings.PeerManager_disconnect_all_peers(this.ptr);
249 Reference.reachabilityFence(this);
253 * Send pings to each peer and disconnect those which did not respond to the last round of
256 * This may be called on any timescale you want, however, roughly once every ten seconds is
257 * preferred. The call rate determines both how often we send a ping to our peers and how much
258 * time they have to respond before we disconnect them.
260 * May call [`send_data`] on all [`SocketDescriptor`]s. Thus, be very careful with reentrancy
263 * [`send_data`]: SocketDescriptor::send_data
265 public void timer_tick_occurred() {
266 bindings.PeerManager_timer_tick_occurred(this.ptr);
267 Reference.reachabilityFence(this);
271 * Generates a signed node_announcement from the given arguments, sending it to all connected
272 * peers. Note that peers will likely ignore this message unless we have at least one public
273 * channel which has at least six confirmations on-chain.
275 * `rgb` is a node \"color\" and `alias` is a printable human-readable string to describe this
276 * node to humans. They carry no in-protocol meaning.
278 * `addresses` represent the set (possibly empty) of socket addresses on which this node
279 * accepts incoming connections. These will be included in the node_announcement, publicly
280 * tying these addresses together and to this node. If you wish to preserve user privacy,
281 * addresses should likely contain only Tor Onion addresses.
283 * Panics if `addresses` is absurdly large (more than 100).
285 * [`get_and_clear_pending_msg_events`]: MessageSendEventsProvider::get_and_clear_pending_msg_events
287 public void broadcast_node_announcement(byte[] rgb, byte[] alias, NetAddress[] addresses) {
288 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);
289 Reference.reachabilityFence(this);
290 Reference.reachabilityFence(rgb);
291 Reference.reachabilityFence(alias);
292 Reference.reachabilityFence(addresses);