Skip to content

Commit 99d3661

Browse files
committed
f: Introduce LengthTlvs, wrapper struct
1 parent 266c049 commit 99d3661

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

lightning/src/blinded_path/message.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use crate::io::Cursor;
2323
use crate::ln::channelmanager::PaymentId;
2424
use crate::ln::onion_utils;
2525
use crate::offers::nonce::Nonce;
26-
use crate::onion_message::packet::ControlTlvs;
26+
use crate::onion_message::packet::{ControlTlvs, LengthTlvs};
2727
use crate::sign::{NodeSigner, Recipient};
2828
use crate::crypto::streams::ChaChaPolyReadAdapter;
2929
use crate::util::ser::{FixedLengthReader, LengthReadableArgs, Writeable, Writer};
@@ -192,7 +192,7 @@ pub(super) fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>(
192192
.max()
193193
.unwrap_or(0);
194194

195-
let length_tlvs = tlvs.map(|tlv| (max_length, tlv));
195+
let length_tlvs = tlvs.map(|tlvs| LengthTlvs { max_length, tlvs });
196196

197197
utils::construct_blinded_hops(secp_ctx, pks, length_tlvs, session_priv)
198198
}

lightning/src/onion_message/packet.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,16 @@ pub(crate) enum ControlTlvs {
314314
Receive(ReceiveTlvs),
315315
}
316316

317+
/// A wrapper struct that stores the largest packet length in the given [`BlindedPath`].
318+
/// This helps us calculate the appropriate padding size for the [`ControlTlvs`] at the time
319+
/// of writing them.
320+
pub(crate) struct LengthTlvs {
321+
/// Length of the packet with the largest size in the [`BlindedPath`].
322+
pub(crate) max_length: usize,
323+
/// The current packet's TLVs.
324+
pub(crate) tlvs: ControlTlvs,
325+
}
326+
317327
impl Readable for ControlTlvs {
318328
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
319329
_init_and_read_tlv_stream!(r, {
@@ -361,15 +371,15 @@ impl Writeable for ControlTlvs {
361371
}
362372
}
363373

364-
impl Writeable for (usize, ControlTlvs) {
374+
impl Writeable for LengthTlvs {
365375
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
366-
let length = self.0 - self.1.serialized_length();
376+
let length = self.max_length - self.tlvs.serialized_length();
367377
let padding = Some(Padding::new(length));
368378

369379
encode_tlv_stream!(writer, {
370380
(1, padding, option)
371381
});
372382

373-
self.1.write(writer)
383+
self.tlvs.write(writer)
374384
}
375385
}

0 commit comments

Comments
 (0)