Skip to content

Commit c711502

Browse files
committed
review: Small nits, documentation, and fixups
Small review items that are noncontroversial and can be rolled into a single commit.
1 parent 0f8a74c commit c711502

File tree

2 files changed

+33
-13
lines changed

2 files changed

+33
-13
lines changed

lightning/src/ln/peers/handler.rs

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ use ln::peers::transport::{PayloadQueuer, Transport};
4343
use bitcoin::hashes::core::iter::Filter;
4444
use std::collections::hash_map::IterMut;
4545

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;
4748

4849
/// Interface PeerManager uses to interact with the Transport object
4950
pub(super) trait ITransport: MessageQueuer {
@@ -56,7 +57,8 @@ pub(super) trait ITransport: MessageQueuer {
5657
/// Instantiate a new inbound Transport
5758
fn new_inbound(responder_static_private_key: &SecretKey, responder_ephemeral_private_key: &SecretKey) -> Self;
5859

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.
6062
fn process_input(&mut self, input: &[u8], output_buffer: &mut impl PayloadQueuer) -> Result<bool, String>;
6163

6264
/// Returns true if the connection is established and encrypted messages can be sent.
@@ -65,19 +67,20 @@ pub(super) trait ITransport: MessageQueuer {
6567
/// Returns the node_id of the remote node. Panics if not connected.
6668
fn get_their_node_id(&self) -> PublicKey;
6769

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
6971
fn drain_messages<L: Deref>(&mut self, logger: L) -> Result<Vec<Message>, PeerHandleError> where L::Target: Logger;
7072
}
7173

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.
7476
pub(super) trait MessageQueuer {
7577
/// Encodes, encrypts, and enqueues a message to the outbound queue. Panics if the connection is
7678
/// not established yet.
7779
fn enqueue_message<M: Encode + Writeable, Q: PayloadQueuer, L: Deref>(&mut self, message: &M, output_buffer: &mut Q, logger: L) where L::Target: Logger;
7880
}
7981

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.
8184
pub(super) trait SocketDescriptorFlusher {
8285
/// Write previously enqueued data to the SocketDescriptor. A return of false indicates the
8386
/// underlying SocketDescriptor could not fulfill the send_data() call and the blocked state
@@ -198,14 +201,14 @@ impl<TransportImpl: ITransport> Peer<TransportImpl> {
198201
fn new(outbound: bool, transport: TransportImpl) -> Self {
199202
Self {
200203
outbound,
201-
outbound_queue: OutboundQueue::new(MSG_BUFF_SIZE),
204+
outbound_queue: OutboundQueue::new(OUTBOUND_QUEUE_SIZE),
202205
post_init_state: None,
203206
transport
204207
}
205208
}
206209

207210
/// 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).
209212
fn is_initialized(&self) -> bool {
210213
self.post_init_state.is_some()
211214
}
@@ -251,6 +254,9 @@ struct PeerHolder<Descriptor: SocketDescriptor, TransportImpl: ITransport> {
251254
}
252255

253256
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.
254260
fn initialized_peer_by_node_id_mut(&mut self, node_id: &PublicKey) -> Option<(Descriptor, &mut Peer<TransportImpl>)> {
255261
match self.node_id_to_descriptor.get_mut(node_id) {
256262
None => None,
@@ -730,7 +736,17 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref, TransportImpl
730736
peer.transport.enqueue_message(&resp, &mut peer.outbound_queue, &*self.logger);
731737
}
732738

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.
734750
fn process_init_message(&self, message: Message, descriptor: &Descriptor, peer: &mut Peer<TransportImpl>, node_id_to_descriptor: &mut HashMap<PublicKey, Descriptor>) -> Result<(), PeerHandleError> {
735751
let their_node_id = peer.transport.get_their_node_id();
736752

lightning/src/ln/peers/transport.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ pub trait IPeerHandshake {
3737
fn process_act(&mut self, input: &[u8]) -> Result<(Option<Vec<u8>>, Option<(Conduit, PublicKey)>), String>;
3838
}
3939

40-
/// Trait representing a container that allows enqueuing of Vec<[u8]>
40+
/// Trait representing a container that allows enqueuing of Vec<[u8]>. Used by Transport to enqueue
41+
/// NOISE handshake messages and post-NOISE encrypted Lightning Messages to be sent to a peer.
4142
pub(super) trait PayloadQueuer {
4243
/// Enqueue item to the queue
4344
fn push_back(&mut self, item: Vec<u8>);
@@ -49,6 +50,8 @@ pub(super) trait PayloadQueuer {
4950
fn queue_space(&self) -> usize;
5051
}
5152

53+
/// Used by the PeerManager to work with a BOLT8 authenticated transport layer. Abstracts the NOISE
54+
/// handshake as well as post-NOISE encrypted Lightning Message encryption/decryption.
5255
pub(super) struct Transport<PeerHandshakeImpl: IPeerHandshake=PeerHandshake> {
5356
pub(super) conduit: Option<Conduit>,
5457
handshake: PeerHandshakeImpl,
@@ -76,6 +79,7 @@ impl<PeerHandshakeImpl: IPeerHandshake> ITransport for Transport<PeerHandshakeIm
7679
}
7780
}
7881

82+
// Returns Ok(true) on the first call that results in a connected state, Ok(false) otherwise.
7983
fn process_input(&mut self, input: &[u8], output_buffer: &mut impl PayloadQueuer) -> Result<bool, String> {
8084
match self.conduit {
8185
// Continue handshake
@@ -91,14 +95,14 @@ impl<PeerHandshakeImpl: IPeerHandshake> ITransport for Transport<PeerHandshakeIm
9195
if let Some((conduit, remote_pubkey)) = conduit_and_remote_pubkey_option {
9296
self.conduit = Some(conduit);
9397
self.their_node_id = Some(remote_pubkey);
94-
Ok(true) // newly connected
98+
Ok(true)
9599
} else {
96-
Ok(false) // newly connected
100+
Ok(false)
97101
}
98102
}
99103
Some(ref mut conduit) => {
100104
conduit.read(input);
101-
Ok(false) // newly connected
105+
Ok(false)
102106
}
103107
}
104108
}

0 commit comments

Comments
 (0)