Skip to content

Commit cd2c71b

Browse files
committed
Introduce WithPadding struct
1. The struct acts as a wrapper for the tlvs. 2. This stuct help us to know the size of the largest packet at the time of writing and hence calculate the padding size appropriately before writing them.
1 parent 23aa06f commit cd2c71b

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

lightning/src/blinded_path/utils.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,27 @@ impl Writeable for Padding {
177177
Ok(())
178178
}
179179
}
180+
181+
182+
/// A wrapper struct that stores the largest packet size for a [`BlindedPath`].
183+
/// This helps us calculate the appropriate padding size for the tlvs when writing them.
184+
pub(super) struct WithPadding<T: Writeable> {
185+
/// Length of the packet with the largest size in the [`BlindedPath`].
186+
pub(super) max_length: usize,
187+
/// The current packet's TLVs.
188+
pub(super) tlvs: T,
189+
}
190+
191+
impl<T: Writeable> Writeable for WithPadding<T> {
192+
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
193+
let length = self.max_length.checked_sub(self.tlvs.serialized_length());
194+
debug_assert!(length.is_some(), "Size of this packet should not be larger than the size of largest packet.");
195+
let padding = Some(Padding::new(length.unwrap()));
196+
197+
encode_tlv_stream!(writer, {
198+
(1, padding, option)
199+
});
200+
201+
self.tlvs.write(writer)
202+
}
203+
}

0 commit comments

Comments
 (0)