X-Git-Url: http://git.bitcoin.ninja/index.cgi?p=ldk-java;a=blobdiff_plain;f=ts%2Fstructs%2FPeerManager.mts;h=fdcd5c2e95de1470af4ca9e7f9be1577123cd2c5;hp=353f9f66e5c3ce1fb11f37f66cf8adef10924a64;hb=c629a01650402c8e2f9b9db8ced9ed63ce687727;hpb=d1d0121c000b713c10fd0bedd249eb8dda2e4db7 diff --git a/ts/structs/PeerManager.mts b/ts/structs/PeerManager.mts index 353f9f66..fdcd5c2e 100644 --- a/ts/structs/PeerManager.mts +++ b/ts/structs/PeerManager.mts @@ -281,12 +281,38 @@ import CommonBase from './CommonBase.mjs'; import * as bindings from '../bindings.mjs' +/** + * A PeerManager manages a set of peers, described by their [`SocketDescriptor`] and marshalls + * socket events into messages which it passes on to its [`MessageHandler`]. + * + * Locks are taken internally, so you must never assume that reentrancy from a + * [`SocketDescriptor`] call back into [`PeerManager`] methods will not deadlock. + * + * Calls to [`read_event`] will decode relevant messages and pass them to the + * [`ChannelMessageHandler`], likely doing message processing in-line. Thus, the primary form of + * parallelism in Rust-Lightning is in calls to [`read_event`]. Note, however, that calls to any + * [`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 + * you're using lightning-net-tokio. + * + * [`read_event`]: PeerManager::read_event + */ export class PeerManager extends CommonBase { /* @internal */ public constructor(_dummy: object, ptr: number) { super(ptr, bindings.PeerManager_free); } + /** + * 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 + * cryptographically secure random bytes. + */ public static constructor_new(message_handler_chan_handler_arg: ChannelMessageHandler, message_handler_route_handler_arg: RoutingMessageHandler, our_node_secret: Uint8Array, ephemeral_random_data: Uint8Array, logger: Logger, custom_message_handler: CustomMessageHandler): PeerManager { const ret: number = bindings.PeerManager_new(bindings.MessageHandler_new(message_handler_chan_handler_arg == null ? 0 : CommonBase.get_ptr_of(message_handler_chan_handler_arg), message_handler_route_handler_arg == null ? 0 : CommonBase.get_ptr_of(message_handler_route_handler_arg)), bindings.encodeUint8Array(bindings.check_arr_len(our_node_secret, 32)), bindings.encodeUint8Array(bindings.check_arr_len(ephemeral_random_data, 32)), logger == null ? 0 : CommonBase.get_ptr_of(logger), custom_message_handler == null ? 0 : CommonBase.get_ptr_of(custom_message_handler)); const ret_hu_conv: PeerManager = new PeerManager(null, ret); @@ -298,6 +324,13 @@ export class PeerManager extends CommonBase { return ret_hu_conv; } + /** + * Get the list of node ids for peers which have completed the initial handshake. + * + * 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. + */ public get_peer_node_ids(): Uint8Array[] { const ret: number = bindings.PeerManager_get_peer_node_ids(this.ptr); const ret_conv_12_len: number = bindings.getArrayLength(ret); @@ -307,9 +340,22 @@ export class PeerManager extends CommonBase { const ret_conv_12_conv: Uint8Array = bindings.decodeUint8Array(ret_conv_12); ret_conv_12_arr[m] = ret_conv_12_conv; } + bindings.freeWasmMemory(ret) return ret_conv_12_arr; } + /** + * Indicates a new outbound connection has been established to a node with the given node_id. + * Note that if an Err is returned here you MUST NOT call socket_disconnected for the new + * descriptor but must disconnect the connection immediately. + * + * 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()`]: PeerManager::socket_disconnected + */ public new_outbound_connection(their_node_id: Uint8Array, descriptor: SocketDescriptor): Result_CVec_u8ZPeerHandleErrorZ { const ret: number = bindings.PeerManager_new_outbound_connection(this.ptr, bindings.encodeUint8Array(bindings.check_arr_len(their_node_id, 33)), descriptor == null ? 0 : CommonBase.get_ptr_of(descriptor)); const ret_hu_conv: Result_CVec_u8ZPeerHandleErrorZ = Result_CVec_u8ZPeerHandleErrorZ.constr_from_ptr(ret); @@ -317,6 +363,19 @@ export class PeerManager extends CommonBase { return ret_hu_conv; } + /** + * Indicates a new inbound connection has been established. + * + * May refuse the connection by returning an Err, but will never write bytes to the remote end + * (outbound connector always speaks first). Note that if an Err is returned here you MUST NOT + * call socket_disconnected for the new descriptor but must disconnect the connection + * immediately. + * + * Panics if descriptor is duplicative with some other descriptor which has not yet been + * [`socket_disconnected()`]. + * + * [`socket_disconnected()`]: PeerManager::socket_disconnected + */ public new_inbound_connection(descriptor: SocketDescriptor): Result_NonePeerHandleErrorZ { const ret: number = bindings.PeerManager_new_inbound_connection(this.ptr, descriptor == null ? 0 : CommonBase.get_ptr_of(descriptor)); const ret_hu_conv: Result_NonePeerHandleErrorZ = Result_NonePeerHandleErrorZ.constr_from_ptr(ret); @@ -324,34 +383,111 @@ export class PeerManager extends CommonBase { return ret_hu_conv; } + /** + * Indicates that there is room to write data to the given socket descriptor. + * + * May return an Err to indicate that the connection should be closed. + * + * 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 + * sufficient! + * + * [`send_data`]: SocketDescriptor::send_data + * [`write_buffer_space_avail`]: PeerManager::write_buffer_space_avail + */ public write_buffer_space_avail(descriptor: SocketDescriptor): Result_NonePeerHandleErrorZ { const ret: number = bindings.PeerManager_write_buffer_space_avail(this.ptr, descriptor == null ? 0 : CommonBase.get_ptr_of(descriptor)); const ret_hu_conv: Result_NonePeerHandleErrorZ = Result_NonePeerHandleErrorZ.constr_from_ptr(ret); return ret_hu_conv; } + /** + * Indicates that data was read from the given socket descriptor. + * + * May return an Err to indicate that the connection should be closed. + * + * Will *not* call back into [`send_data`] on any descriptors to avoid reentrancy complexity. + * Thus, however, you should call [`process_events`] after any `read_event` to generate + * [`send_data`] calls to handle responses. + * + * If `Ok(true)` is returned, further read_events should not be triggered until a + * [`send_data`] call on this descriptor has `resume_read` set (preventing DoS issues in the + * send buffer). + * + * [`send_data`]: SocketDescriptor::send_data + * [`process_events`]: PeerManager::process_events + */ public read_event(peer_descriptor: SocketDescriptor, data: Uint8Array): Result_boolPeerHandleErrorZ { const ret: number = bindings.PeerManager_read_event(this.ptr, peer_descriptor == null ? 0 : CommonBase.get_ptr_of(peer_descriptor), bindings.encodeUint8Array(data)); const ret_hu_conv: Result_boolPeerHandleErrorZ = Result_boolPeerHandleErrorZ.constr_from_ptr(ret); return ret_hu_conv; } + /** + * Checks for any events generated by our handlers and processes them. Includes sending most + * response messages as well as messages generated by calls to handler functions directly (eg + * functions like [`ChannelManager::process_pending_htlc_forwards`] or [`send_payment`]). + * + * May call [`send_data`] on [`SocketDescriptor`]s. Thus, be very careful with reentrancy + * issues! + * + * You don't have to call this function explicitly if you are using [`lightning-net-tokio`] + * or one of the other clients provided in our language bindings. + * + * [`send_payment`]: crate::ln::channelmanager::ChannelManager::send_payment + * [`ChannelManager::process_pending_htlc_forwards`]: crate::ln::channelmanager::ChannelManager::process_pending_htlc_forwards + * [`send_data`]: SocketDescriptor::send_data + */ public process_events(): void { bindings.PeerManager_process_events(this.ptr); } + /** + * Indicates that the given socket descriptor's connection is now closed. + */ public socket_disconnected(descriptor: SocketDescriptor): void { bindings.PeerManager_socket_disconnected(this.ptr, descriptor == null ? 0 : CommonBase.get_ptr_of(descriptor)); } + /** + * 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 disconnect_by_node_id(node_id: Uint8Array, no_connection_possible: boolean): void { bindings.PeerManager_disconnect_by_node_id(this.ptr, bindings.encodeUint8Array(bindings.check_arr_len(node_id, 33)), no_connection_possible); } + /** + * Disconnects all currently-connected peers. This is useful on platforms where there may be + * an indication that TCP sockets have stalled even if we weren't around to time them out + * using regular ping/pongs. + */ public disconnect_all_peers(): void { bindings.PeerManager_disconnect_all_peers(this.ptr); } + /** + * Send pings to each peer and disconnect those which did not respond to the last round of + * pings. + * + * This may be called on any timescale you want, however, roughly once every five to ten + * seconds is preferred. The call rate determines both how often we send a ping to our peers + * and how much time they have to respond before we disconnect them. + * + * May call [`send_data`] on all [`SocketDescriptor`]s. Thus, be very careful with reentrancy + * issues! + * + * [`send_data`]: SocketDescriptor::send_data + */ public timer_tick_occurred(): void { bindings.PeerManager_timer_tick_occurred(this.ptr); }