@@ -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 an [`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,29 +178,35 @@ 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`] has been informed of the disconnection (either by it
183+ /// having triggered the disconnection or a call to [`PeerManager::socket_disconnected`]), and no
184+ /// further calls to the [`PeerManager`] related to the original socket occur. This allows you to
185+ /// use a file descriptor for your SocketDescriptor directly, however for simplicity you may wish
186+ /// to 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 carries no meaning, and should not cause any action .
196205 fn send_data ( & mut self , data : & [ u8 ] , resume_read : bool ) -> usize ;
197206 /// Disconnect the socket pointed to by this SocketDescriptor.
198- /// No [`PeerManager::socket_disconnected`] call need be generated as a result of this call.
207+ ///
208+ /// You do *not* need to call [`PeerManager::socket_disconnected`] with this socket after this
209+ /// call (doing so is a noop).
199210 fn disconnect_socket ( & mut self ) ;
200211}
201212
@@ -309,14 +320,25 @@ pub type SimpleArcPeerManager<SD, M, T, F, C, L> = PeerManager<SD, Arc<SimpleArc
309320/// helps with issues such as long function definitions.
310321pub 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 > ;
311322
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.
323+ /// A PeerManager manages a set of peers, described by their [`SocketDescriptor`] and marshalls
324+ /// socket events into messages which it passes on to its [`MessageHandler`].
325+ ///
326+ /// Locks are taken internally, so you must never assume that reentrancy from a
327+ /// [`SocketDescriptor`] call back into [`PeerManager`] methods will not deadlock.
328+ ///
329+ /// Calls to [`read_event`] will decode relevant messages and pass them to the
330+ /// [`ChannelMessageHandler`], likely doing message processing in-line. Thus, the primary form of
331+ /// parallelism in Rust-Lightning is in calls to [`read_event`]. Note, however, that calls to any
332+ /// [`PeerManager`] functions related to the same connection must occur only in serial, making new
333+ /// calls only after previous ones have returned.
314334///
315335/// Rather than using a plain PeerManager, it is preferable to use either a SimpleArcPeerManager
316336/// a SimpleRefPeerManager, for conciseness. See their documentation for more details, but
317337/// essentially you should default to using a SimpleRefPeerManager, and use a
318338/// SimpleArcPeerManager when you require a PeerManager with a static lifetime, such as when
319339/// you're using lightning-net-tokio.
340+ ///
341+ /// [`read_event`]: PeerManager::read_event
320342pub struct PeerManager < Descriptor : SocketDescriptor , CM : Deref , RM : Deref , L : Deref > where
321343 CM :: Target : ChannelMessageHandler ,
322344 RM :: Target : RoutingMessageHandler ,
@@ -397,8 +419,6 @@ impl<Descriptor: SocketDescriptor, RM: Deref, L: Deref> PeerManager<Descriptor,
397419 }
398420}
399421
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.
402422impl < Descriptor : SocketDescriptor , CM : Deref , RM : Deref , L : Deref > PeerManager < Descriptor , CM , RM , L > where
403423 CM :: Target : ChannelMessageHandler ,
404424 RM :: Target : RoutingMessageHandler ,
@@ -458,8 +478,10 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref> PeerManager<D
458478 ///
459479 /// Returns a small number of bytes to send to the remote node (currently always 50).
460480 ///
461- /// Panics if descriptor is duplicative with some other descriptor which has not yet had a
462- /// socket_disconnected().
481+ /// Panics if descriptor is duplicative with some other descriptor which has not yet been
482+ /// [`socket_disconnected()`].
483+ ///
484+ /// [`socket_disconnected()`]: PeerManager::socket_disconnected
463485 pub fn new_outbound_connection ( & self , their_node_id : PublicKey , descriptor : Descriptor ) -> Result < Vec < u8 > , PeerHandleError > {
464486 let mut peer_encryptor = PeerChannelEncryptor :: new_outbound ( their_node_id. clone ( ) , self . get_ephemeral_key ( ) ) ;
465487 let res = peer_encryptor. get_act_one ( ) . to_vec ( ) ;
@@ -495,8 +517,10 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref> PeerManager<D
495517 /// call socket_disconnected for the new descriptor but must disconnect the connection
496518 /// immediately.
497519 ///
498- /// Panics if descriptor is duplicative with some other descriptor which has not yet had
499- /// socket_disconnected called.
520+ /// Panics if descriptor is duplicative with some other descriptor which has not yet been
521+ /// [`socket_disconnected()`].
522+ ///
523+ /// [`socket_disconnected()`]: PeerManager::socket_disconnected
500524 pub fn new_inbound_connection ( & self , descriptor : Descriptor ) -> Result < ( ) , PeerHandleError > {
501525 let peer_encryptor = PeerChannelEncryptor :: new_inbound ( & self . our_node_secret ) ;
502526 let pending_read_buffer = [ 0 ; 50 ] . to_vec ( ) ; // Noise act one is 50 bytes
@@ -604,12 +628,14 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref> PeerManager<D
604628 ///
605629 /// May return an Err to indicate that the connection should be closed.
606630 ///
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.
631+ /// May call [`send_data`] on the descriptor passed in (or an equal descriptor) before
632+ /// returning. Thus, be very careful with reentrancy issues! The invariants around calling
633+ /// [`write_buffer_space_avail`] in case a write did not fully complete must still hold - be
634+ /// ready to call `[write_buffer_space_avail`] again if a write call generated here isn't
635+ /// sufficient!
636+ ///
637+ /// [`send_data`]: SocketDescriptor::send_data
638+ /// [`write_buffer_space_avail`]: PeerManager::write_buffer_space_avail
613639 pub fn write_buffer_space_avail ( & self , descriptor : & mut Descriptor ) -> Result < ( ) , PeerHandleError > {
614640 let mut peers = self . peers . lock ( ) . unwrap ( ) ;
615641 match peers. peers . get_mut ( descriptor) {
@@ -631,13 +657,16 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref> PeerManager<D
631657 ///
632658 /// May return an Err to indicate that the connection should be closed.
633659 ///
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.
660+ /// Will *not* call back into [` send_data`] on any descriptors to avoid reentrancy complexity.
661+ /// Thus, however, you should call [` process_events`] after any ` read_event` to generate
662+ /// [` send_data`] calls to handle responses.
637663 ///
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).
664+ /// If `Ok(true)` is returned, further read_events should not be triggered until a
665+ /// [`send_data`] call on this descriptor has `resume_read` set (preventing DoS issues in the
666+ /// send buffer).
640667 ///
668+ /// [`send_data`]: SocketDescriptor::send_data
669+ /// [`process_events`]: PeerManager::process_events
641670 pub fn read_event ( & self , peer_descriptor : & mut Descriptor , data : & [ u8 ] ) -> Result < bool , PeerHandleError > {
642671 match self . do_read_event ( peer_descriptor, data) {
643672 Ok ( res) => Ok ( res) ,
@@ -1085,7 +1114,14 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref> PeerManager<D
10851114
10861115 /// Checks for any events generated by our handlers and processes them. Includes sending most
10871116 /// 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).
1117+ /// functions like [`ChannelManager::process_pending_htlc_forwards`] or [`send_payment`]).
1118+ ///
1119+ /// May call [`send_data`] on [`SocketDescriptor`]s. Thus, be very careful with reentrancy
1120+ /// issues!
1121+ ///
1122+ /// [`send_payment`]: crate::ln::channelmanager::ChannelManager::send_payment
1123+ /// [`ChannelManager::process_pending_htlc_forwards`]: crate::ln::channelmanager::ChannelManager::process_pending_htlc_forwards
1124+ /// [`send_data`]: SocketDescriptor::send_data
10891125 pub fn process_events ( & self ) {
10901126 {
10911127 // TODO: There are some DoS attacks here where you can flood someone's outbound send
@@ -1297,10 +1333,6 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref> PeerManager<D
12971333 }
12981334
12991335 /// 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.
13041336 pub fn socket_disconnected ( & self , descriptor : & Descriptor ) {
13051337 self . disconnect_event_internal ( descriptor, false ) ;
13061338 }
@@ -1328,11 +1360,13 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref> PeerManager<D
13281360
13291361 /// Disconnect a peer given its node id.
13301362 ///
1331- /// Set no_connection_possible to true to prevent any further connection with this peer,
1363+ /// Set ` no_connection_possible` to true to prevent any further connection with this peer,
13321364 /// force-closing any channels we have with it.
13331365 ///
1334- /// If a peer is connected, this will call `disconnect_socket` on the descriptor for the peer,
1335- /// so be careful about reentrancy issues.
1366+ /// If a peer is connected, this will call [`disconnect_socket`] on the descriptor for the
1367+ /// peer. Thus, be very careful about reentrancy issues.
1368+ ///
1369+ /// [`disconnect_socket`]: SocketDescriptor::disconnect_socket
13361370 pub fn disconnect_by_node_id ( & self , node_id : PublicKey , no_connection_possible : bool ) {
13371371 let mut peers_lock = self . peers . lock ( ) . unwrap ( ) ;
13381372 if let Some ( mut descriptor) = peers_lock. node_id_to_descriptor . remove ( & node_id) {
@@ -1344,9 +1378,13 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref> PeerManager<D
13441378 }
13451379
13461380 /// This function should be called roughly once every 30 seconds.
1347- /// 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!
1381+ /// It will send pings to each peer and disconnect those which did not respond to the last
1382+ /// round of pings.
1383+ ///
1384+ /// May call [`send_data`] on all [`SocketDescriptor`]s. Thus, be very careful with reentrancy
1385+ /// issues!
1386+ ///
1387+ /// [`send_data`]: SocketDescriptor::send_data
13501388 pub fn timer_tick_occurred ( & self ) {
13511389 let mut peers_lock = self . peers . lock ( ) . unwrap ( ) ;
13521390 {
0 commit comments