Skip to content

Commit 46622df

Browse files
committed
Modify Peer to use ChunkedMessageQueue
Replace `pending_outbound_buffer`, `pending_outbound_buffer_first_msg_offset`, and `gossip_broadcast_buffer` fields in `Peer` with a single `message_queue: ChunkedMessageQueue`. Update `should_read` to check `message_queue.pending_bytes()` against the buffer limit. Update `should_buffer_onion_message` and `should_buffer_gossip_broadcast` to drop the `pending_outbound_buffer.is_empty()` check (the contiguous chunk buffer no longer has a meaningful "queue empty" concept). Update `buffer_full_drop_gossip_broadcast` to use `message_queue.total_buffered_bytes()`. Rewrite `do_attempt_write_data` to: send full chunks first, then buffer messages (onion -> gossip broadcast -> backfill), then pad+finalize any partial chunk with Ping padding before sending. During handshake, send raw bytes without chunking. Update `enqueue_message` to use `encrypt_and_push_message`. Update handshake act handling to use `push_raw`. Update gossip broadcast sites to use `message_queue.gossip_broadcast_buffer`. Co-Authored-By: HAL 9000
1 parent 3306125 commit 46622df

File tree

4 files changed

+127
-118
lines changed

4 files changed

+127
-118
lines changed

fuzz/src/full_stack.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ use lightning::ln::functional_test_utils::*;
4747
use lightning::ln::inbound_payment::ExpandedKey;
4848
use lightning::ln::outbound_payment::{RecipientOnionFields, Retry};
4949
use lightning::ln::peer_handler::{
50-
IgnoringMessageHandler, MessageHandler, PeerManager, SocketDescriptor,
50+
IgnoringMessageHandler, MessageHandler, PeerManager, SocketDescriptor, CHUNK_SIZE,
5151
};
5252
use lightning::ln::script::ShutdownScript;
5353
use lightning::ln::types::ChannelId;
@@ -201,6 +201,15 @@ struct Peer<'a> {
201201
}
202202
impl<'a> SocketDescriptor for Peer<'a> {
203203
fn send_data(&mut self, data: &[u8], _continue_read: bool) -> usize {
204+
// After the Noise handshake, all writes must be exactly CHUNK_SIZE bytes
205+
// (or 0 for empty force-writes). The only non-chunk write is 50 bytes for
206+
// the handshake act_two sent by an inbound peer before encryption is ready.
207+
assert!(
208+
data.len() == 0 || data.len() == 50 || data.len() == CHUNK_SIZE,
209+
"Unexpected send_data size: {} (expected 0, 50, or {})",
210+
data.len(),
211+
CHUNK_SIZE,
212+
);
204213
data.len()
205214
}
206215
fn disconnect_socket(&mut self) {

fuzz/src/peer_crypt.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,11 @@ pub fn do_test(data: &[u8]) {
7979
let mut buf = [0; 65536 + 16];
8080
loop {
8181
if get_slice!(1)[0] == 0 {
82-
crypter.encrypt_buffer(MessageBuf::from_encoded(&get_slice!(slice_to_be16(
83-
get_slice!(2)
84-
))));
82+
let mut dest = Vec::new();
83+
crypter.encrypt_buffer_into(
84+
&mut dest,
85+
MessageBuf::from_encoded(&get_slice!(slice_to_be16(get_slice!(2)))),
86+
);
8587
} else {
8688
let len = match crypter.decrypt_length_header(get_slice!(16 + 2)) {
8789
Ok(len) => len,

lightning/src/ln/peer_channel_encryptor.rs

Lines changed: 5 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use bitcoin::secp256k1::{PublicKey, SecretKey};
2828

2929
use crate::crypto::chacha20poly1305rfc::ChaCha20Poly1305RFC;
3030
use crate::crypto::utils::hkdf_extract_expand_twice;
31-
use crate::util::ser::{VecWriter, Writer};
31+
use crate::util::ser::Writer;
3232

3333
/// Maximum Lightning message data length according to
3434
/// [BOLT-8](https://github.com/lightning/bolts/blob/v1.0/08-transport.md#lightning-message-specification)
@@ -540,41 +540,6 @@ impl PeerChannelEncryptor {
540540
}
541541
}
542542

543-
/// Builds sendable bytes for a message.
544-
///
545-
/// `msgbuf` must begin with 16 + 2 dummy/0 bytes, which will be filled with the encrypted
546-
/// message length and its MAC. It should then be followed by the message bytes themselves
547-
/// (including the two byte message type).
548-
///
549-
/// For efficiency, the [`Vec::capacity`] should be at least 16 bytes larger than the
550-
/// [`Vec::len`], to avoid reallocating for the message MAC, which will be appended to the vec.
551-
fn encrypt_message_with_header_0s(&mut self, msgbuf: &mut Vec<u8>) {
552-
self.encrypt_with_header_0s_at(msgbuf, 0);
553-
}
554-
555-
/// Encrypts the given pre-serialized message, returning the encrypted version.
556-
/// panics if msg.len() > 65535 or Noise handshake has not finished.
557-
pub fn encrypt_buffer(&mut self, mut msg: MessageBuf) -> Vec<u8> {
558-
self.encrypt_message_with_header_0s(&mut msg.0);
559-
msg.0
560-
}
561-
562-
/// Encrypts the given message, returning the encrypted version.
563-
/// panics if the length of `message`, once encoded, is greater than 65535 or if the Noise
564-
/// handshake has not finished.
565-
pub fn encrypt_message<T: wire::Type>(&mut self, message: wire::Message<T>) -> Vec<u8> {
566-
// Allocate a buffer with 2KB, fitting most common messages. Reserve the first 16+2 bytes
567-
// for the 2-byte message type prefix and its MAC.
568-
let mut res = VecWriter(Vec::with_capacity(MSG_BUF_ALLOC_SIZE));
569-
res.0.resize(16 + 2, 0);
570-
571-
message.type_id().write(&mut res).expect("In-memory messages must never fail to serialize");
572-
message.write(&mut res).expect("In-memory messages must never fail to serialize");
573-
574-
self.encrypt_message_with_header_0s(&mut res.0);
575-
res.0
576-
}
577-
578543
/// Encrypts the given message directly into the destination buffer, appending encrypted bytes.
579544
///
580545
/// This avoids the intermediate `Vec` allocation that [`Self::encrypt_message`] creates.
@@ -1056,7 +1021,8 @@ mod tests {
10561021

10571022
for i in 0..1005 {
10581023
let msg = [0x68, 0x65, 0x6c, 0x6c, 0x6f];
1059-
let mut res = outbound_peer.encrypt_buffer(MessageBuf::from_encoded(&msg));
1024+
let mut res = Vec::new();
1025+
outbound_peer.encrypt_buffer_into(&mut res, MessageBuf::from_encoded(&msg));
10601026
assert_eq!(res.len(), 5 + 2 * 16 + 2);
10611027

10621028
let len_header = res[0..2 + 16].to_vec();
@@ -1101,7 +1067,8 @@ mod tests {
11011067
fn max_message_len_encryption() {
11021068
let mut outbound_peer = get_outbound_peer_for_initiator_test_vectors();
11031069
let msg = [4u8; LN_MAX_MSG_LEN + 1];
1104-
outbound_peer.encrypt_buffer(MessageBuf::from_encoded(&msg));
1070+
let mut dest = Vec::new();
1071+
outbound_peer.encrypt_buffer_into(&mut dest, MessageBuf::from_encoded(&msg));
11051072
}
11061073

11071074
#[test]

0 commit comments

Comments
 (0)