Skip to content

Commit 5b2aaa4

Browse files
committed
Introduce Padding for blinded message paths.
This commit make the size of each onion packet in a blinded path of the same size, that is equal to the size of that largest onion packet.
1 parent f683449 commit 5b2aaa4

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

lightning/src/blinded_path/message.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ pub struct ForwardNode {
4343

4444
/// TLVs to encode in an intermediate onion message packet's hop data. When provided in a blinded
4545
/// route, they are encoded into [`BlindedHop::encrypted_payload`].
46+
#[derive(Clone)]
4647
pub(crate) struct ForwardTlvs {
4748
/// The next hop in the onion message's path.
4849
pub(crate) next_hop: NextMessageHop,
@@ -52,6 +53,7 @@ pub(crate) struct ForwardTlvs {
5253
}
5354

5455
/// Similar to [`ForwardTlvs`], but these TLVs are for the final node.
56+
#[derive(Clone)]
5557
pub(crate) struct ReceiveTlvs {
5658
/// If `context` is `Some`, it is used to identify the blinded path that this onion message is
5759
/// sending to. This is useful for receivers to check that said blinded path is being used in
@@ -65,7 +67,6 @@ impl Writeable for ForwardTlvs {
6567
NextMessageHop::NodeId(pubkey) => (Some(pubkey), None),
6668
NextMessageHop::ShortChannelId(scid) => (None, Some(scid)),
6769
};
68-
// TODO: write padding
6970
encode_tlv_stream!(writer, {
7071
(2, short_channel_id, option),
7172
(4, next_node_id, option),
@@ -77,7 +78,6 @@ impl Writeable for ForwardTlvs {
7778

7879
impl Writeable for ReceiveTlvs {
7980
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
80-
// TODO: write padding
8181
encode_tlv_stream!(writer, {
8282
(65537, self.context, option),
8383
});
@@ -148,7 +148,14 @@ pub(super) fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>(
148148
.map(|next_hop| ControlTlvs::Forward(ForwardTlvs { next_hop, next_blinding_override: None }))
149149
.chain(core::iter::once(ControlTlvs::Receive(ReceiveTlvs{ context: Some(context) })));
150150

151-
utils::construct_blinded_hops(secp_ctx, pks, tlvs, session_priv)
151+
let max_length = tlvs.clone()
152+
.max_by_key(|c| c.serialized_length())
153+
.map(|c| c.serialized_length())
154+
.unwrap_or(0);
155+
156+
let length_tlvs = tlvs.map(|tlv| (max_length, tlv));
157+
158+
utils::construct_blinded_hops(secp_ctx, pks, length_tlvs, session_priv)
152159
}
153160

154161
// Advance the blinded onion message path by one hop, so make the second hop into the new

lightning/src/blinded_path/utils.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ impl Padding {
151151
}
152152

153153
impl Readable for Padding {
154+
/// Reads and discards the padding data.
154155
#[inline]
155156
fn read<R: io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
156157
loop {

lightning/src/onion_message/packet.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ for Payload<ParsedOnionMessageContents<<H as CustomOnionMessageHandler>::CustomM
306306
/// or received. Thus we read a `ControlTlvs` rather than reading a [`ForwardTlvs`] or
307307
/// [`ReceiveTlvs`] directly. Also useful on the encoding side to keep forward and receive TLVs in
308308
/// the same iterator.
309+
#[derive(Clone)]
309310
pub(crate) enum ControlTlvs {
310311
/// This onion message is intended to be forwarded.
311312
Forward(ForwardTlvs),
@@ -359,3 +360,16 @@ impl Writeable for ControlTlvs {
359360
}
360361
}
361362
}
363+
364+
impl Writeable for (usize, ControlTlvs) {
365+
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
366+
let length = self.0 - self.1.serialized_length();
367+
let padding = Some(Padding::new(length));
368+
369+
encode_tlv_stream!(writer, {
370+
(1, padding, option)
371+
});
372+
373+
self.1.write(writer)
374+
}
375+
}

0 commit comments

Comments
 (0)