@@ -160,10 +160,15 @@ pub struct MessageHandler<CM: Deref, RM: Deref> where
160160 CM :: Target : ChannelMessageHandler ,
161161 RM :: Target : RoutingMessageHandler {
162162 /// A message handler which handles messages specific to channels. Usually this is just a
163- /// ChannelManager object or a ErroringMessageHandler.
163+ /// [`ChannelManager`] object or a [`ErroringMessageHandler`].
164+ ///
165+ /// [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
164166 pub chan_handler : CM ,
165167 /// A message handler which handles messages updating our knowledge of the network channel
166- /// graph. Usually this is just a NetGraphMsgHandlerMonitor object or an IgnoringMessageHandler.
168+ /// graph. Usually this is just a [`NetGraphMsgHandler`] object or an
169+ /// [`IgnoringMessageHandler`].
170+ ///
171+ /// [`NetGraphMsgHandler`]: crate::routing::network_graph::NetGraphMsgHandler
167172 pub route_handler : RM ,
168173}
169174
@@ -173,26 +178,30 @@ pub struct MessageHandler<CM: Deref, RM: Deref> where
173178///
174179/// For efficiency, Clone should be relatively cheap for this type.
175180///
176- /// You probably want to just extend an int and put a file descriptor in a struct and implement
177- /// send_data. Note that if you are using a higher-level net library that may call close() itself,
178- /// be careful to ensure you don't have races whereby you might register a new connection with an
179- /// fd which is the same as a previous one which has yet to be removed via
180- /// PeerManager::socket_disconnected().
181+ /// Two descriptors may compare equal (by [`cmp::Eq`] and [`hash::Hash`]) as long as the original
182+ /// has been disconnected, the [`PeerManager`] informed of the disconnection (either by it having
183+ /// triggered the disconnection or a call to [`PeerManager::socket_disconnected`]), and no further
184+ /// calls to the [`PeerManager`] related to the original socket occur. This allows you to use a
185+ /// file descriptor for your SocketDescriptor directly, however for simplicity you may wish to
186+ /// simply use another value which is guaranteed to be globally unique instead.
181187pub trait SocketDescriptor : cmp:: Eq + hash:: Hash + Clone {
182188 /// Attempts to send some data from the given slice to the peer.
183189 ///
184190 /// Returns the amount of data which was sent, possibly 0 if the socket has since disconnected.
185- /// Note that in the disconnected case, socket_disconnected must still fire and further write
186- /// attempts may occur until that time.
191+ /// Note that in the disconnected case, [`PeerManager:: socket_disconnected`] must still be
192+ /// called and further write attempts may occur until that time.
187193 ///
188- /// If the returned size is smaller than data.len(), a write_available event must
189- /// trigger the next time more data can be written. Additionally, until the a send_data event
190- /// completes fully, no further read_events should trigger on the same peer!
194+ /// If the returned size is smaller than data.len(), a
195+ /// [`PeerManager::write_buffer_space_avail`] call must be made the next time more data can be
196+ /// written. Additionally, until a send_data event completes fully, no further
197+ /// [`PeerManager::read_event`] calls should be made for the same peer! Because this is to
198+ /// prevent denial-of-service issues, you should not read or buffer any data from the socket
199+ /// until then.
191200 ///
192- /// If a read_event on this descriptor had previously returned true (indicating that read
193- /// events should be paused to prevent DoS in the send buffer), resume_read may be set
194- /// indicating that read events on this descriptor should resume. A resume_read of false does
195- /// *not* imply that further read events should be paused.
201+ /// If a [`PeerManager:: read_event`] call on this descriptor had previously returned true
202+ /// (indicating that read events should be paused to prevent DoS in the send buffer),
203+ /// resume_read may be set indicating that read events on this descriptor should resume. A
204+ /// resume_read of false does *not* imply on its own that further read events should be paused.
196205 fn send_data ( & mut self , data : & [ u8 ] , resume_read : bool ) -> usize ;
197206 /// Disconnect the socket pointed to by this SocketDescriptor.
198207 /// No [`PeerManager::socket_disconnected`] call need be generated as a result of this call.
@@ -309,14 +318,25 @@ pub type SimpleArcPeerManager<SD, M, T, F, C, L> = PeerManager<SD, Arc<SimpleArc
309318/// helps with issues such as long function definitions.
310319pub type SimpleRefPeerManager < ' a , ' b , ' c , ' d , ' e , ' f , ' g , SD , M , T , F , C , L > = PeerManager < SD , SimpleRefChannelManager < ' a , ' b , ' c , ' d , ' e , M , T , F , L > , & ' e NetGraphMsgHandler < & ' g C , & ' f L > , & ' f L > ;
311320
312- /// A PeerManager manages a set of peers, described by their SocketDescriptor and marshalls socket
313- /// events into messages which it passes on to its MessageHandlers.
321+ /// A PeerManager manages a set of peers, described by their [`SocketDescriptor`] and marshalls
322+ /// socket events into messages which it passes on to its MessageHandlers.
323+ ///
324+ /// Locks are taken internally, so you must never assume that reentrancy from a
325+ /// [`SocketDescriptor`] call back into [`PeerManager`] methods will not deadlock.
326+ ///
327+ /// Calls to [`read_event`] will decode relevant messages and pass them to the
328+ /// [`ChannelMessageHandler`], likely doing message processing in-line. Thus, the primary form of
329+ /// parallelism in Rust-Lightning is parallelism in calls to [`read_event`]. Note, however, that
330+ /// calls to any [`PeerManager`] functions related to the same connection must occur only in
331+ /// serial, making new calls only after previous ones have returned.
314332///
315333/// Rather than using a plain PeerManager, it is preferable to use either a SimpleArcPeerManager
316334/// a SimpleRefPeerManager, for conciseness. See their documentation for more details, but
317335/// essentially you should default to using a SimpleRefPeerManager, and use a
318336/// SimpleArcPeerManager when you require a PeerManager with a static lifetime, such as when
319337/// you're using lightning-net-tokio.
338+ ///
339+ /// [`read_event`]: PeerManager::read_event
320340pub struct PeerManager < Descriptor : SocketDescriptor , CM : Deref , RM : Deref , L : Deref > where
321341 CM :: Target : ChannelMessageHandler ,
322342 RM :: Target : RoutingMessageHandler ,
@@ -397,8 +417,6 @@ impl<Descriptor: SocketDescriptor, RM: Deref, L: Deref> PeerManager<Descriptor,
397417 }
398418}
399419
400- /// Manages and reacts to connection events. You probably want to use file descriptors as PeerIds.
401- /// PeerIds may repeat, but only after socket_disconnected() has been called.
402420impl < Descriptor : SocketDescriptor , CM : Deref , RM : Deref , L : Deref > PeerManager < Descriptor , CM , RM , L > where
403421 CM :: Target : ChannelMessageHandler ,
404422 RM :: Target : RoutingMessageHandler ,
@@ -458,8 +476,10 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref> PeerManager<D
458476 ///
459477 /// Returns a small number of bytes to send to the remote node (currently always 50).
460478 ///
461- /// Panics if descriptor is duplicative with some other descriptor which has not yet had a
462- /// socket_disconnected().
479+ /// Panics if descriptor is duplicative with some other descriptor which has not yet been
480+ /// [`socket_disconnected()`].
481+ ///
482+ /// [`socket_disconnected()`]: PeerManager::socket_disconnected
463483 pub fn new_outbound_connection ( & self , their_node_id : PublicKey , descriptor : Descriptor ) -> Result < Vec < u8 > , PeerHandleError > {
464484 let mut peer_encryptor = PeerChannelEncryptor :: new_outbound ( their_node_id. clone ( ) , self . get_ephemeral_key ( ) ) ;
465485 let res = peer_encryptor. get_act_one ( ) . to_vec ( ) ;
@@ -495,8 +515,10 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref> PeerManager<D
495515 /// call socket_disconnected for the new descriptor but must disconnect the connection
496516 /// immediately.
497517 ///
498- /// Panics if descriptor is duplicative with some other descriptor which has not yet had
499- /// socket_disconnected called.
518+ /// Panics if descriptor is duplicative with some other descriptor which has not yet been
519+ /// [`socket_disconnected()`].
520+ ///
521+ /// [`socket_disconnected()`]: PeerManager::socket_disconnected
500522 pub fn new_inbound_connection ( & self , descriptor : Descriptor ) -> Result < ( ) , PeerHandleError > {
501523 let peer_encryptor = PeerChannelEncryptor :: new_inbound ( & self . our_node_secret ) ;
502524 let pending_read_buffer = [ 0 ; 50 ] . to_vec ( ) ; // Noise act one is 50 bytes
@@ -604,12 +626,14 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref> PeerManager<D
604626 ///
605627 /// May return an Err to indicate that the connection should be closed.
606628 ///
607- /// Will most likely call send_data on the descriptor passed in (or the descriptor handed into
608- /// new_*\_connection) before returning. Thus, be very careful with reentrancy issues! The
609- /// invariants around calling write_buffer_space_avail in case a write did not fully complete
610- /// must still hold - be ready to call write_buffer_space_avail again if a write call generated
611- /// here isn't sufficient! Panics if the descriptor was not previously registered in a
612- /// new_\*_connection event.
629+ /// Will most likely call [`send_data`] on the descriptor passed in (or the descriptor handed
630+ /// into new_*\_connection) before returning. Thus, be very careful with reentrancy issues! The
631+ /// invariants around calling [`write_buffer_space_avail`] in case a write did not fully
632+ /// complete must still hold - be ready to call `[write_buffer_space_avail`] again if a write
633+ /// call generated here isn't sufficient!
634+ ///
635+ /// [`send_data`]: SocketDescriptor::send_data
636+ /// [`write_buffer_space_avail`]: PeerManager::write_buffer_space_avail
613637 pub fn write_buffer_space_avail ( & self , descriptor : & mut Descriptor ) -> Result < ( ) , PeerHandleError > {
614638 let mut peers = self . peers . lock ( ) . unwrap ( ) ;
615639 match peers. peers . get_mut ( descriptor) {
@@ -631,13 +655,15 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref> PeerManager<D
631655 ///
632656 /// May return an Err to indicate that the connection should be closed.
633657 ///
634- /// Will *not* call back into send_data on any descriptors to avoid reentrancy complexity.
635- /// Thus, however, you almost certainly want to call process_events() after any read_event to
636- /// generate send_data calls to handle responses.
658+ /// Will *not* call back into [` send_data`] on any descriptors to avoid reentrancy complexity.
659+ /// Thus, however, you almost certainly want to call [` process_events`] after any read_event
660+ /// to generate [` send_data`] calls to handle responses.
637661 ///
638- /// If Ok(true) is returned, further read_events should not be triggered until a send_data call
639- /// on this file descriptor has resume_read set (preventing DoS issues in the send buffer).
662+ /// If Ok(true) is returned, further read_events should not be triggered until a [` send_data`]
663+ /// call on this descriptor has resume_read set (preventing DoS issues in the send buffer).
640664 ///
665+ /// [`send_data`]: SocketDescriptor::send_data
666+ /// [`process_events`]: PeerManager::process_events
641667 pub fn read_event ( & self , peer_descriptor : & mut Descriptor , data : & [ u8 ] ) -> Result < bool , PeerHandleError > {
642668 match self . do_read_event ( peer_descriptor, data) {
643669 Ok ( res) => Ok ( res) ,
@@ -1085,7 +1111,14 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref> PeerManager<D
10851111
10861112 /// Checks for any events generated by our handlers and processes them. Includes sending most
10871113 /// response messages as well as messages generated by calls to handler functions directly (eg
1088- /// functions like ChannelManager::process_pending_htlc_forward or send_payment).
1114+ /// functions like [`ChannelManager::process_pending_htlc_forwards`] or [`send_payment`]).
1115+ ///
1116+ /// Will most likely call [`send_data`] on descriptors previously provided to
1117+ /// new_*\_connection. Thus, be very careful with reentrancy issues!
1118+ ///
1119+ /// [`send_payment`]: crate::ln::channelmanager::ChannelManager::send_payment
1120+ /// [`ChannelManager::process_pending_htlc_forwards`]: crate::ln::channelmanager::ChannelManager::process_pending_htlc_forwards
1121+ /// [`send_data`]: SocketDescriptor::send_data
10891122 pub fn process_events ( & self ) {
10901123 {
10911124 // TODO: There are some DoS attacks here where you can flood someone's outbound send
@@ -1297,10 +1330,6 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref> PeerManager<D
12971330 }
12981331
12991332 /// Indicates that the given socket descriptor's connection is now closed.
1300- ///
1301- /// This need only be called if the socket has been disconnected by the peer or your own
1302- /// decision to disconnect it and may be skipped in any case where other parts of this library
1303- /// (eg PeerHandleError, explicit disconnect_socket calls) instruct you to disconnect the peer.
13041333 pub fn socket_disconnected ( & self , descriptor : & Descriptor ) {
13051334 self . disconnect_event_internal ( descriptor, false ) ;
13061335 }
@@ -1331,8 +1360,10 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref> PeerManager<D
13311360 /// Set no_connection_possible to true to prevent any further connection with this peer,
13321361 /// force-closing any channels we have with it.
13331362 ///
1334- /// If a peer is connected, this will call `disconnect_socket` on the descriptor for the peer,
1335- /// so be careful about reentrancy issues.
1363+ /// If a peer is connected, this will call [`disconnect_socket`] on the descriptor for the
1364+ /// peer. Thus, be very careful about reentrancy issues.
1365+ ///
1366+ /// [`disconnect_socket`]: SocketDescriptor::disconnect_socket
13361367 pub fn disconnect_by_node_id ( & self , node_id : PublicKey , no_connection_possible : bool ) {
13371368 let mut peers_lock = self . peers . lock ( ) . unwrap ( ) ;
13381369 if let Some ( mut descriptor) = peers_lock. node_id_to_descriptor . remove ( & node_id) {
@@ -1345,8 +1376,11 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref> PeerManager<D
13451376
13461377 /// This function should be called roughly once every 30 seconds.
13471378 /// It will send pings to each peer and disconnect those which did not respond to the last round of pings.
1348-
1349- /// Will most likely call send_data on all of the registered descriptors, thus, be very careful with reentrancy issues!
1379+ ///
1380+ /// Will most likely call [`send_data`] all descriptors previously provided to
1381+ /// new_*\_connection. Thus, be very careful with reentrancy issues!
1382+ ///
1383+ /// [`send_data`]: SocketDescriptor::send_data
13501384 pub fn timer_tick_occurred ( & self ) {
13511385 let mut peers_lock = self . peers . lock ( ) . unwrap ( ) ;
13521386 {
0 commit comments