Skip to content

Commit 2a763d7

Browse files
committed
f: Introduce LengthTlvs, wrapper struct
1 parent 6233678 commit 2a763d7

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
@@ -22,7 +22,7 @@ use crate::io;
2222
use crate::io::Cursor;
2323
use crate::ln::channelmanager::PaymentId;
2424
use crate::ln::onion_utils;
25-
use crate::onion_message::packet::ControlTlvs;
25+
use crate::onion_message::packet::{ControlTlvs, LengthTlvs};
2626
use crate::sign::{NodeSigner, Recipient};
2727
use crate::crypto::streams::ChaChaPolyReadAdapter;
2828
use crate::util::ser::{FixedLengthReader, LengthReadableArgs, Writeable, Writer};
@@ -153,7 +153,7 @@ pub(super) fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>(
153153
.max()
154154
.unwrap_or(0);
155155

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

158158
utils::construct_blinded_hops(secp_ctx, pks, length_tlvs, session_priv)
159159
}

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)