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.
44 * `ephemeral_random_data` is used to derive per-connection ephemeral keys and must be
45 * cryptographically secure random bytes.
47 * `current_time` is used as an always-increasing counter that survives across restarts and is
48 * incremented irregularly internally. In general it is best to simply use the current UNIX
49 * timestamp, however if it is not available a persistent counter that increases once per
50 * minute should suffice.
52 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) {
53 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);
54 Reference.reachabilityFence(message_handler_chan_handler_arg);
55 Reference.reachabilityFence(message_handler_route_handler_arg);
56 Reference.reachabilityFence(message_handler_onion_message_handler_arg);
57 Reference.reachabilityFence(message_handler_custom_message_handler_arg);
58 Reference.reachabilityFence(current_time);
59 Reference.reachabilityFence(ephemeral_random_data);
60 Reference.reachabilityFence(logger);
61 Reference.reachabilityFence(node_signer);
62 if (ret >= 0 && ret <= 4096) { return null; }
63 org.ldk.structs.PeerManager ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.PeerManager(null, ret); }
64 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
65 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(message_handler_chan_handler_arg); };
66 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(message_handler_route_handler_arg); };
67 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(message_handler_onion_message_handler_arg); };
68 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(message_handler_custom_message_handler_arg); };
69 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(logger); };
70 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(node_signer); };
75 * Get a list of tuples mapping from node id to network addresses for peers which have
76 * completed the initial handshake.
78 * For outbound connections, the [`PublicKey`] will be the same as the `their_node_id` parameter
79 * passed in to [`Self::new_outbound_connection`], however entries will only appear once the initial
80 * handshake has completed and we are sure the remote peer has the private key for the given
83 * The returned `Option`s will only be `Some` if an address had been previously given via
84 * [`Self::new_outbound_connection`] or [`Self::new_inbound_connection`].
86 public TwoTuple_PublicKeyCOption_SocketAddressZZ[] get_peer_node_ids() {
87 long[] ret = bindings.PeerManager_get_peer_node_ids(this.ptr);
88 Reference.reachabilityFence(this);
89 int ret_conv_43_len = ret.length;
90 TwoTuple_PublicKeyCOption_SocketAddressZZ[] ret_conv_43_arr = new TwoTuple_PublicKeyCOption_SocketAddressZZ[ret_conv_43_len];
91 for (int r = 0; r < ret_conv_43_len; r++) {
92 long ret_conv_43 = ret[r];
93 TwoTuple_PublicKeyCOption_SocketAddressZZ ret_conv_43_hu_conv = new TwoTuple_PublicKeyCOption_SocketAddressZZ(null, ret_conv_43);
94 if (ret_conv_43_hu_conv != null) { ret_conv_43_hu_conv.ptrs_to.add(this); };
95 ret_conv_43_arr[r] = ret_conv_43_hu_conv;
97 return ret_conv_43_arr;
101 * Indicates a new outbound connection has been established to a node with the given `node_id`
102 * and an optional remote network address.
104 * The remote network address adds the option to report a remote IP address back to a connecting
105 * peer using the init message.
106 * The user should pass the remote network address of the host they are connected to.
108 * If an `Err` is returned here you must disconnect the connection immediately.
110 * Returns a small number of bytes to send to the remote node (currently always 50).
112 * Panics if descriptor is duplicative with some other descriptor which has not yet been
113 * [`socket_disconnected`].
115 * [`socket_disconnected`]: PeerManager::socket_disconnected
117 public Result_CVec_u8ZPeerHandleErrorZ new_outbound_connection(byte[] their_node_id, org.ldk.structs.SocketDescriptor descriptor, org.ldk.structs.Option_SocketAddressZ remote_network_address) {
118 long ret = bindings.PeerManager_new_outbound_connection(this.ptr, InternalUtils.check_arr_len(their_node_id, 33), descriptor.ptr, remote_network_address.ptr);
119 Reference.reachabilityFence(this);
120 Reference.reachabilityFence(their_node_id);
121 Reference.reachabilityFence(descriptor);
122 Reference.reachabilityFence(remote_network_address);
123 if (ret >= 0 && ret <= 4096) { return null; }
124 Result_CVec_u8ZPeerHandleErrorZ ret_hu_conv = Result_CVec_u8ZPeerHandleErrorZ.constr_from_ptr(ret);
125 if (this != null) { this.ptrs_to.add(descriptor); };
126 if (this != null) { this.ptrs_to.add(remote_network_address); };
131 * Indicates a new inbound connection has been established to a node with an optional remote
134 * The remote network address adds the option to report a remote IP address back to a connecting
135 * peer using the init message.
136 * The user should pass the remote network address of the host they are connected to.
138 * May refuse the connection by returning an Err, but will never write bytes to the remote end
139 * (outbound connector always speaks first). If an `Err` is returned here you must disconnect
140 * the connection immediately.
142 * Panics if descriptor is duplicative with some other descriptor which has not yet been
143 * [`socket_disconnected`].
145 * [`socket_disconnected`]: PeerManager::socket_disconnected
147 public Result_NonePeerHandleErrorZ new_inbound_connection(org.ldk.structs.SocketDescriptor descriptor, org.ldk.structs.Option_SocketAddressZ remote_network_address) {
148 long ret = bindings.PeerManager_new_inbound_connection(this.ptr, descriptor.ptr, remote_network_address.ptr);
149 Reference.reachabilityFence(this);
150 Reference.reachabilityFence(descriptor);
151 Reference.reachabilityFence(remote_network_address);
152 if (ret >= 0 && ret <= 4096) { return null; }
153 Result_NonePeerHandleErrorZ ret_hu_conv = Result_NonePeerHandleErrorZ.constr_from_ptr(ret);
154 if (this != null) { this.ptrs_to.add(descriptor); };
155 if (this != null) { this.ptrs_to.add(remote_network_address); };
160 * Indicates that there is room to write data to the given socket descriptor.
162 * May return an Err to indicate that the connection should be closed.
164 * May call [`send_data`] on the descriptor passed in (or an equal descriptor) before
165 * returning. Thus, be very careful with reentrancy issues! The invariants around calling
166 * [`write_buffer_space_avail`] in case a write did not fully complete must still hold - be
167 * ready to call [`write_buffer_space_avail`] again if a write call generated here isn't
170 * [`send_data`]: SocketDescriptor::send_data
171 * [`write_buffer_space_avail`]: PeerManager::write_buffer_space_avail
173 public Result_NonePeerHandleErrorZ write_buffer_space_avail(org.ldk.structs.SocketDescriptor descriptor) {
174 long ret = bindings.PeerManager_write_buffer_space_avail(this.ptr, descriptor.ptr);
175 Reference.reachabilityFence(this);
176 Reference.reachabilityFence(descriptor);
177 if (ret >= 0 && ret <= 4096) { return null; }
178 Result_NonePeerHandleErrorZ ret_hu_conv = Result_NonePeerHandleErrorZ.constr_from_ptr(ret);
183 * Indicates that data was read from the given socket descriptor.
185 * May return an Err to indicate that the connection should be closed.
187 * Will *not* call back into [`send_data`] on any descriptors to avoid reentrancy complexity.
188 * Thus, however, you should call [`process_events`] after any `read_event` to generate
189 * [`send_data`] calls to handle responses.
191 * If `Ok(true)` is returned, further read_events should not be triggered until a
192 * [`send_data`] call on this descriptor has `resume_read` set (preventing DoS issues in the
195 * In order to avoid processing too many messages at once per peer, `data` should be on the
198 * [`send_data`]: SocketDescriptor::send_data
199 * [`process_events`]: PeerManager::process_events
201 public Result_boolPeerHandleErrorZ read_event(org.ldk.structs.SocketDescriptor peer_descriptor, byte[] data) {
202 long ret = bindings.PeerManager_read_event(this.ptr, peer_descriptor.ptr, data);
203 Reference.reachabilityFence(this);
204 Reference.reachabilityFence(peer_descriptor);
205 Reference.reachabilityFence(data);
206 if (ret >= 0 && ret <= 4096) { return null; }
207 Result_boolPeerHandleErrorZ ret_hu_conv = Result_boolPeerHandleErrorZ.constr_from_ptr(ret);
212 * Checks for any events generated by our handlers and processes them. Includes sending most
213 * response messages as well as messages generated by calls to handler functions directly (eg
214 * functions like [`ChannelManager::process_pending_htlc_forwards`] or [`send_payment`]).
216 * May call [`send_data`] on [`SocketDescriptor`]s. Thus, be very careful with reentrancy
219 * You don't have to call this function explicitly if you are using [`lightning-net-tokio`]
220 * or one of the other clients provided in our language bindings.
222 * Note that if there are any other calls to this function waiting on lock(s) this may return
223 * without doing any work. All available events that need handling will be handled before the
224 * other calls return.
226 * [`send_payment`]: crate::ln::channelmanager::ChannelManager::send_payment
227 * [`ChannelManager::process_pending_htlc_forwards`]: crate::ln::channelmanager::ChannelManager::process_pending_htlc_forwards
228 * [`send_data`]: SocketDescriptor::send_data
230 public void process_events() {
231 bindings.PeerManager_process_events(this.ptr);
232 Reference.reachabilityFence(this);
236 * Indicates that the given socket descriptor's connection is now closed.
238 public void socket_disconnected(org.ldk.structs.SocketDescriptor descriptor) {
239 bindings.PeerManager_socket_disconnected(this.ptr, descriptor.ptr);
240 Reference.reachabilityFence(this);
241 Reference.reachabilityFence(descriptor);
245 * Disconnect a peer given its node id.
247 * If a peer is connected, this will call [`disconnect_socket`] on the descriptor for the
248 * peer. Thus, be very careful about reentrancy issues.
250 * [`disconnect_socket`]: SocketDescriptor::disconnect_socket
252 public void disconnect_by_node_id(byte[] node_id) {
253 bindings.PeerManager_disconnect_by_node_id(this.ptr, InternalUtils.check_arr_len(node_id, 33));
254 Reference.reachabilityFence(this);
255 Reference.reachabilityFence(node_id);
259 * Disconnects all currently-connected peers. This is useful on platforms where there may be
260 * an indication that TCP sockets have stalled even if we weren't around to time them out
261 * using regular ping/pongs.
263 public void disconnect_all_peers() {
264 bindings.PeerManager_disconnect_all_peers(this.ptr);
265 Reference.reachabilityFence(this);
269 * Send pings to each peer and disconnect those which did not respond to the last round of
272 * This may be called on any timescale you want, however, roughly once every ten seconds is
273 * preferred. The call rate determines both how often we send a ping to our peers and how much
274 * time they have to respond before we disconnect them.
276 * May call [`send_data`] on all [`SocketDescriptor`]s. Thus, be very careful with reentrancy
279 * [`send_data`]: SocketDescriptor::send_data
281 public void timer_tick_occurred() {
282 bindings.PeerManager_timer_tick_occurred(this.ptr);
283 Reference.reachabilityFence(this);
287 * Generates a signed node_announcement from the given arguments, sending it to all connected
288 * peers. Note that peers will likely ignore this message unless we have at least one public
289 * channel which has at least six confirmations on-chain.
291 * `rgb` is a node \"color\" and `alias` is a printable human-readable string to describe this
292 * node to humans. They carry no in-protocol meaning.
294 * `addresses` represent the set (possibly empty) of socket addresses on which this node
295 * accepts incoming connections. These will be included in the node_announcement, publicly
296 * tying these addresses together and to this node. If you wish to preserve user privacy,
297 * addresses should likely contain only Tor Onion addresses.
299 * Panics if `addresses` is absurdly large (more than 100).
301 * [`get_and_clear_pending_msg_events`]: MessageSendEventsProvider::get_and_clear_pending_msg_events
303 public void broadcast_node_announcement(byte[] rgb, byte[] alias, SocketAddress[] addresses) {
304 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);
305 Reference.reachabilityFence(this);
306 Reference.reachabilityFence(rgb);
307 Reference.reachabilityFence(alias);
308 Reference.reachabilityFence(addresses);
309 for (SocketAddress addresses_conv_15: addresses) { if (this != null) { this.ptrs_to.add(addresses_conv_15); }; };