@@ -160,10 +160,15 @@ pub struct MessageHandler<CM: Deref, RM: Deref> where
160
160
CM :: Target : ChannelMessageHandler ,
161
161
RM :: Target : RoutingMessageHandler {
162
162
/// 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
164
166
pub chan_handler : CM ,
165
167
/// 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
167
172
pub route_handler : RM ,
168
173
}
169
174
@@ -173,29 +178,35 @@ pub struct MessageHandler<CM: Deref, RM: Deref> where
173
178
///
174
179
/// For efficiency, Clone should be relatively cheap for this type.
175
180
///
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.
181
187
pub trait SocketDescriptor : cmp:: Eq + hash:: Hash + Clone {
182
188
/// Attempts to send some data from the given slice to the peer.
183
189
///
184
190
/// 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.
187
193
///
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.
191
200
///
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 .
196
205
fn send_data ( & mut self , data : & [ u8 ] , resume_read : bool ) -> usize ;
197
206
/// 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).
199
210
fn disconnect_socket ( & mut self ) ;
200
211
}
201
212
@@ -309,14 +320,25 @@ pub type SimpleArcPeerManager<SD, M, T, F, C, L> = PeerManager<SD, Arc<SimpleArc
309
320
/// helps with issues such as long function definitions.
310
321
pub 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 > ;
311
322
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.
314
334
///
315
335
/// Rather than using a plain PeerManager, it is preferable to use either a SimpleArcPeerManager
316
336
/// a SimpleRefPeerManager, for conciseness. See their documentation for more details, but
317
337
/// essentially you should default to using a SimpleRefPeerManager, and use a
318
338
/// SimpleArcPeerManager when you require a PeerManager with a static lifetime, such as when
319
339
/// you're using lightning-net-tokio.
340
+ ///
341
+ /// [`read_event`]: PeerManager::read_event
320
342
pub struct PeerManager < Descriptor : SocketDescriptor , CM : Deref , RM : Deref , L : Deref > where
321
343
CM :: Target : ChannelMessageHandler ,
322
344
RM :: Target : RoutingMessageHandler ,
@@ -397,8 +419,6 @@ impl<Descriptor: SocketDescriptor, RM: Deref, L: Deref> PeerManager<Descriptor,
397
419
}
398
420
}
399
421
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.
402
422
impl < Descriptor : SocketDescriptor , CM : Deref , RM : Deref , L : Deref > PeerManager < Descriptor , CM , RM , L > where
403
423
CM :: Target : ChannelMessageHandler ,
404
424
RM :: Target : RoutingMessageHandler ,
@@ -458,8 +478,10 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref> PeerManager<D
458
478
///
459
479
/// Returns a small number of bytes to send to the remote node (currently always 50).
460
480
///
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
463
485
pub fn new_outbound_connection ( & self , their_node_id : PublicKey , descriptor : Descriptor ) -> Result < Vec < u8 > , PeerHandleError > {
464
486
let mut peer_encryptor = PeerChannelEncryptor :: new_outbound ( their_node_id. clone ( ) , self . get_ephemeral_key ( ) ) ;
465
487
let res = peer_encryptor. get_act_one ( ) . to_vec ( ) ;
@@ -495,8 +517,10 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref> PeerManager<D
495
517
/// call socket_disconnected for the new descriptor but must disconnect the connection
496
518
/// immediately.
497
519
///
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
500
524
pub fn new_inbound_connection ( & self , descriptor : Descriptor ) -> Result < ( ) , PeerHandleError > {
501
525
let peer_encryptor = PeerChannelEncryptor :: new_inbound ( & self . our_node_secret ) ;
502
526
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
604
628
///
605
629
/// May return an Err to indicate that the connection should be closed.
606
630
///
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
613
639
pub fn write_buffer_space_avail ( & self , descriptor : & mut Descriptor ) -> Result < ( ) , PeerHandleError > {
614
640
let mut peers = self . peers . lock ( ) . unwrap ( ) ;
615
641
match peers. peers . get_mut ( descriptor) {
@@ -631,13 +657,16 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref> PeerManager<D
631
657
///
632
658
/// May return an Err to indicate that the connection should be closed.
633
659
///
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.
637
663
///
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).
640
667
///
668
+ /// [`send_data`]: SocketDescriptor::send_data
669
+ /// [`process_events`]: PeerManager::process_events
641
670
pub fn read_event ( & self , peer_descriptor : & mut Descriptor , data : & [ u8 ] ) -> Result < bool , PeerHandleError > {
642
671
match self . do_read_event ( peer_descriptor, data) {
643
672
Ok ( res) => Ok ( res) ,
@@ -1085,7 +1114,14 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref> PeerManager<D
1085
1114
1086
1115
/// Checks for any events generated by our handlers and processes them. Includes sending most
1087
1116
/// 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
1089
1125
pub fn process_events ( & self ) {
1090
1126
{
1091
1127
// 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
1297
1333
}
1298
1334
1299
1335
/// 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.
1304
1336
pub fn socket_disconnected ( & self , descriptor : & Descriptor ) {
1305
1337
self . disconnect_event_internal ( descriptor, false ) ;
1306
1338
}
@@ -1328,11 +1360,13 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref> PeerManager<D
1328
1360
1329
1361
/// Disconnect a peer given its node id.
1330
1362
///
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,
1332
1364
/// force-closing any channels we have with it.
1333
1365
///
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
1336
1370
pub fn disconnect_by_node_id ( & self , node_id : PublicKey , no_connection_possible : bool ) {
1337
1371
let mut peers_lock = self . peers . lock ( ) . unwrap ( ) ;
1338
1372
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
1344
1378
}
1345
1379
1346
1380
/// 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
1350
1388
pub fn timer_tick_occurred ( & self ) {
1351
1389
let mut peers_lock = self . peers . lock ( ) . unwrap ( ) ;
1352
1390
{
0 commit comments