@@ -43,7 +43,8 @@ use ln::peers::transport::{PayloadQueuer, Transport};
43
43
use bitcoin:: hashes:: core:: iter:: Filter ;
44
44
use std:: collections:: hash_map:: IterMut ;
45
45
46
- const MSG_BUFF_SIZE : usize = 10 ;
46
+ // Number of items that can exist in the OutboundQueue before Sync message flow control is triggered
47
+ const OUTBOUND_QUEUE_SIZE : usize = 10 ;
47
48
48
49
/// Interface PeerManager uses to interact with the Transport object
49
50
pub ( super ) trait ITransport : MessageQueuer {
@@ -56,7 +57,8 @@ pub(super) trait ITransport: MessageQueuer {
56
57
/// Instantiate a new inbound Transport
57
58
fn new_inbound ( responder_static_private_key : & SecretKey , responder_ephemeral_private_key : & SecretKey ) -> Self ;
58
59
59
- /// Process input data similar to reading it off a descriptor directly.
60
+ /// Process input data similar to reading it off a descriptor directly. Returns true on the first call
61
+ /// that results in the transport being newly connected.
60
62
fn process_input ( & mut self , input : & [ u8 ] , output_buffer : & mut impl PayloadQueuer ) -> Result < bool , String > ;
61
63
62
64
/// Returns true if the connection is established and encrypted messages can be sent.
@@ -65,19 +67,20 @@ pub(super) trait ITransport: MessageQueuer {
65
67
/// Returns the node_id of the remote node. Panics if not connected.
66
68
fn get_their_node_id ( & self ) -> PublicKey ;
67
69
68
- /// Returns all Messages that have been received and can be parsed by the Transport
70
+ /// Returns all Messages that have been received and can be successfully parsed by the Transport
69
71
fn drain_messages < L : Deref > ( & mut self , logger : L ) -> Result < Vec < Message > , PeerHandleError > where L :: Target : Logger ;
70
72
}
71
73
72
- /// Interface PeerManager uses to queue message to send. Used primarily to restrict the interface in
73
- /// specific contexts. e.g. Only queueing during read_event(). No flushing allowed .
74
+ /// Interface PeerManager uses to queue message to send. Implemented by Transport to handle
75
+ /// encryption/decryption post-NOISE .
74
76
pub ( super ) trait MessageQueuer {
75
77
/// Encodes, encrypts, and enqueues a message to the outbound queue. Panics if the connection is
76
78
/// not established yet.
77
79
fn enqueue_message < M : Encode + Writeable , Q : PayloadQueuer , L : Deref > ( & mut self , message : & M , output_buffer : & mut Q , logger : L ) where L :: Target : Logger ;
78
80
}
79
81
80
- /// Trait representing a container that can try to flush data through a SocketDescriptor
82
+ /// Trait representing a container that can try to flush data through a SocketDescriptor. Used by the
83
+ /// PeerManager to handle flushing the outbound queue and flow control.
81
84
pub ( super ) trait SocketDescriptorFlusher {
82
85
/// Write previously enqueued data to the SocketDescriptor. A return of false indicates the
83
86
/// underlying SocketDescriptor could not fulfill the send_data() call and the blocked state
@@ -198,14 +201,14 @@ impl<TransportImpl: ITransport> Peer<TransportImpl> {
198
201
fn new ( outbound : bool , transport : TransportImpl ) -> Self {
199
202
Self {
200
203
outbound,
201
- outbound_queue : OutboundQueue :: new ( MSG_BUFF_SIZE ) ,
204
+ outbound_queue : OutboundQueue :: new ( OUTBOUND_QUEUE_SIZE ) ,
202
205
post_init_state : None ,
203
206
transport
204
207
}
205
208
}
206
209
207
210
/// Returns true if an INIT message has been received from this peer. Implies that this node
208
- /// can send and receive encrypted messages.
211
+ /// can send and receive encrypted messages (self.transport.is_connected() == true) .
209
212
fn is_initialized ( & self ) -> bool {
210
213
self . post_init_state . is_some ( )
211
214
}
@@ -251,6 +254,9 @@ struct PeerHolder<Descriptor: SocketDescriptor, TransportImpl: ITransport> {
251
254
}
252
255
253
256
impl < Descriptor : SocketDescriptor , TransportImpl : ITransport > PeerHolder < Descriptor , TransportImpl > {
257
+
258
+ // Returns an Option<(Descriptor, Peer)> for a node by node_id. A node is initialized after it
259
+ // has completed the NOISE handshake AND received an INIT message.
254
260
fn initialized_peer_by_node_id_mut ( & mut self , node_id : & PublicKey ) -> Option < ( Descriptor , & mut Peer < TransportImpl > ) > {
255
261
match self . node_id_to_descriptor . get_mut ( node_id) {
256
262
None => None ,
@@ -730,7 +736,17 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref, TransportImpl
730
736
peer. transport . enqueue_message ( & resp, & mut peer. outbound_queue , & * self . logger ) ;
731
737
}
732
738
733
- // Process an incoming Init message and set Peer and PeerManager state accordingly
739
+ // Process an incoming Init message and set Peer and PeerManager state.
740
+ //
741
+ // For an inbound connection, this will respond with an INIT message.
742
+ //
743
+ // In the event an INIT has already been seen from this node_id, the current peer connection
744
+ // will be disconnected, but the first connection will remain available.
745
+ //
746
+ // In the event this message is not an INIT the peer will be disconnected.
747
+ //
748
+ // On successful processing of the INIT message, the peer_connected() callback on the
749
+ // ChannelMessageHandler will be called with the remote's node_id and INIT message contents.
734
750
fn process_init_message ( & self , message : Message , descriptor : & Descriptor , peer : & mut Peer < TransportImpl > , node_id_to_descriptor : & mut HashMap < PublicKey , Descriptor > ) -> Result < ( ) , PeerHandleError > {
735
751
let their_node_id = peer. transport . get_their_node_id ( ) ;
736
752
0 commit comments