Skip to content

Commit 56a01d4

Browse files
shaavanjkczyz
andcommitted
f: Update Payment Tlvs to also contain the Option<Padding>
Co-authored-by: Jeffrey Czyz <[email protected]>
1 parent e670584 commit 56a01d4

File tree

6 files changed

+58
-4
lines changed

6 files changed

+58
-4
lines changed

lightning/src/blinded_path/payment.rs

+46-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use bitcoin::secp256k1::{self, PublicKey, Secp256k1, SecretKey};
1515

1616
use crate::blinded_path::{BlindedHop, BlindedPath, IntroductionNode, NodeIdLookUp};
17-
use crate::blinded_path::utils;
17+
use crate::blinded_path::utils::{self, Padding};
1818
use crate::crypto::streams::ChaChaPolyReadAdapter;
1919
use crate::io;
2020
use crate::io::Cursor;
@@ -50,6 +50,8 @@ pub struct ForwardNode {
5050
/// Data to construct a [`BlindedHop`] for forwarding a payment.
5151
#[derive(Clone, Debug)]
5252
pub struct ForwardTlvs {
53+
/// The padding data used to make all packets of a Blinded Path of same size
54+
pub padding: Option<Padding>,
5355
/// The short channel id this payment should be forwarded out over.
5456
pub short_channel_id: u64,
5557
/// Payment parameters for relaying over [`Self::short_channel_id`].
@@ -67,6 +69,8 @@ pub struct ForwardTlvs {
6769
/// may not be valid if received by another lightning implementation.
6870
#[derive(Clone, Debug)]
6971
pub struct ReceiveTlvs {
72+
/// The padding data used to make all packets of a Blinded Path of same size
73+
pub padding: Option<Padding>,
7074
/// Used to authenticate the sender of a payment to the receiver and tie MPP HTLCs together.
7175
pub payment_secret: PaymentSecret,
7276
/// Constraints for the receiver of this payment.
@@ -78,13 +82,29 @@ pub struct ReceiveTlvs {
7882
/// Data to construct a [`BlindedHop`] for sending a payment over.
7983
///
8084
/// [`BlindedHop`]: crate::blinded_path::BlindedHop
85+
#[derive(Clone)]
8186
pub(crate) enum BlindedPaymentTlvs {
8287
/// This blinded payment data is for a forwarding node.
8388
Forward(ForwardTlvs),
8489
/// This blinded payment data is for the receiving node.
8590
Receive(ReceiveTlvs),
8691
}
8792

93+
impl BlindedPaymentTlvs {
94+
pub(crate) fn pad_tlvs(mut self, max_length: usize) -> Self {
95+
let length = max_length.checked_sub(self.serialized_length());
96+
debug_assert!(length.is_some(), "Size of this packet should not be larger than the size of largest packet.");
97+
let padding = Some(Padding::new(length.unwrap()));
98+
99+
match &mut self {
100+
BlindedPaymentTlvs::Forward(tlvs) => tlvs.padding = padding,
101+
BlindedPaymentTlvs::Receive(tlvs) => tlvs.padding = padding,
102+
}
103+
104+
self
105+
}
106+
}
107+
88108
/// Parameters for relaying over a given [`BlindedHop`].
89109
///
90110
/// [`BlindedHop`]: crate::blinded_path::BlindedHop
@@ -198,6 +218,7 @@ impl Writeable for ForwardTlvs {
198218
if self.features == BlindedHopFeatures::empty() { None }
199219
else { Some(&self.features) };
200220
encode_tlv_stream!(w, {
221+
(1, self.padding, option),
201222
(2, self.short_channel_id, required),
202223
(10, self.payment_relay, required),
203224
(12, self.payment_constraints, required),
@@ -210,6 +231,7 @@ impl Writeable for ForwardTlvs {
210231
impl Writeable for ReceiveTlvs {
211232
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
212233
encode_tlv_stream!(w, {
234+
(1, self.padding, option),
213235
(12, self.payment_constraints, required),
214236
(65536, self.payment_secret, required),
215237
(65537, self.payment_context, required)
@@ -246,6 +268,7 @@ impl Readable for BlindedPaymentTlvs {
246268
return Err(DecodeError::InvalidValue)
247269
}
248270
Ok(BlindedPaymentTlvs::Forward(ForwardTlvs {
271+
padding: None,
249272
short_channel_id,
250273
payment_relay: payment_relay.ok_or(DecodeError::InvalidValue)?,
251274
payment_constraints: payment_constraints.0.unwrap(),
@@ -254,6 +277,7 @@ impl Readable for BlindedPaymentTlvs {
254277
} else {
255278
if payment_relay.is_some() || features.is_some() { return Err(DecodeError::InvalidValue) }
256279
Ok(BlindedPaymentTlvs::Receive(ReceiveTlvs {
280+
padding: None,
257281
payment_secret: payment_secret.ok_or(DecodeError::InvalidValue)?,
258282
payment_constraints: payment_constraints.0.unwrap(),
259283
payment_context: payment_context.0.unwrap(),
@@ -272,7 +296,14 @@ pub(super) fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>(
272296
let tlvs = intermediate_nodes.iter().map(|node| BlindedPaymentTlvs::Forward(node.tlvs.clone()))
273297
.chain(core::iter::once(BlindedPaymentTlvs::Receive(payee_tlvs)));
274298

275-
utils::construct_blinded_hops(secp_ctx, pks, tlvs, session_priv)
299+
let max_length = tlvs.clone()
300+
.map(|tlv| tlv.serialized_length())
301+
.max()
302+
.unwrap_or(0);
303+
304+
let length_tlvs = tlvs.map(|tlv| tlv.pad_tlvs(max_length));
305+
306+
utils::construct_blinded_hops(secp_ctx, pks, length_tlvs, session_priv)
276307
}
277308

278309
// Advance the blinded onion payment path by one hop, so make the second hop into the new
@@ -484,6 +515,7 @@ mod tests {
484515
let intermediate_nodes = vec![ForwardNode {
485516
node_id: dummy_pk,
486517
tlvs: ForwardTlvs {
518+
padding: None,
487519
short_channel_id: 0,
488520
payment_relay: PaymentRelay {
489521
cltv_expiry_delta: 144,
@@ -500,6 +532,7 @@ mod tests {
500532
}, ForwardNode {
501533
node_id: dummy_pk,
502534
tlvs: ForwardTlvs {
535+
padding: None,
503536
short_channel_id: 0,
504537
payment_relay: PaymentRelay {
505538
cltv_expiry_delta: 144,
@@ -515,6 +548,7 @@ mod tests {
515548
htlc_maximum_msat: u64::max_value(),
516549
}];
517550
let recv_tlvs = ReceiveTlvs {
551+
padding: None,
518552
payment_secret: PaymentSecret([0; 32]),
519553
payment_constraints: PaymentConstraints {
520554
max_cltv_expiry: 0,
@@ -534,6 +568,7 @@ mod tests {
534568
#[test]
535569
fn compute_payinfo_1_hop() {
536570
let recv_tlvs = ReceiveTlvs {
571+
padding: None,
537572
payment_secret: PaymentSecret([0; 32]),
538573
payment_constraints: PaymentConstraints {
539574
max_cltv_expiry: 0,
@@ -557,6 +592,7 @@ mod tests {
557592
let intermediate_nodes = vec![ForwardNode {
558593
node_id: dummy_pk,
559594
tlvs: ForwardTlvs {
595+
padding: None,
560596
short_channel_id: 0,
561597
payment_relay: PaymentRelay {
562598
cltv_expiry_delta: 0,
@@ -573,6 +609,7 @@ mod tests {
573609
}, ForwardNode {
574610
node_id: dummy_pk,
575611
tlvs: ForwardTlvs {
612+
padding: None,
576613
short_channel_id: 0,
577614
payment_relay: PaymentRelay {
578615
cltv_expiry_delta: 0,
@@ -588,6 +625,7 @@ mod tests {
588625
htlc_maximum_msat: u64::max_value()
589626
}];
590627
let recv_tlvs = ReceiveTlvs {
628+
padding: None,
591629
payment_secret: PaymentSecret([0; 32]),
592630
payment_constraints: PaymentConstraints {
593631
max_cltv_expiry: 0,
@@ -608,6 +646,7 @@ mod tests {
608646
let intermediate_nodes = vec![ForwardNode {
609647
node_id: dummy_pk,
610648
tlvs: ForwardTlvs {
649+
padding: None,
611650
short_channel_id: 0,
612651
payment_relay: PaymentRelay {
613652
cltv_expiry_delta: 0,
@@ -624,6 +663,7 @@ mod tests {
624663
}, ForwardNode {
625664
node_id: dummy_pk,
626665
tlvs: ForwardTlvs {
666+
padding: None,
627667
short_channel_id: 0,
628668
payment_relay: PaymentRelay {
629669
cltv_expiry_delta: 0,
@@ -639,6 +679,7 @@ mod tests {
639679
htlc_maximum_msat: u64::max_value()
640680
}];
641681
let recv_tlvs = ReceiveTlvs {
682+
padding: None,
642683
payment_secret: PaymentSecret([0; 32]),
643684
payment_constraints: PaymentConstraints {
644685
max_cltv_expiry: 0,
@@ -663,6 +704,7 @@ mod tests {
663704
let intermediate_nodes = vec![ForwardNode {
664705
node_id: dummy_pk,
665706
tlvs: ForwardTlvs {
707+
padding: None,
666708
short_channel_id: 0,
667709
payment_relay: PaymentRelay {
668710
cltv_expiry_delta: 0,
@@ -679,6 +721,7 @@ mod tests {
679721
}, ForwardNode {
680722
node_id: dummy_pk,
681723
tlvs: ForwardTlvs {
724+
padding: None,
682725
short_channel_id: 0,
683726
payment_relay: PaymentRelay {
684727
cltv_expiry_delta: 0,
@@ -694,6 +737,7 @@ mod tests {
694737
htlc_maximum_msat: 10_000
695738
}];
696739
let recv_tlvs = ReceiveTlvs {
740+
padding: None,
697741
payment_secret: PaymentSecret([0; 32]),
698742
payment_constraints: PaymentConstraints {
699743
max_cltv_expiry: 0,

lightning/src/ln/blinded_payment_tests.rs

+5
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ fn blinded_payment_path(
3939
intermediate_nodes.push(ForwardNode {
4040
node_id: *node_id,
4141
tlvs: ForwardTlvs {
42+
padding: None,
4243
short_channel_id: chan_upd.short_channel_id,
4344
payment_relay: PaymentRelay {
4445
cltv_expiry_delta: chan_upd.cltv_expiry_delta,
@@ -57,6 +58,7 @@ fn blinded_payment_path(
5758
});
5859
}
5960
let payee_tlvs = ReceiveTlvs {
61+
padding: None,
6062
payment_secret,
6163
payment_constraints: PaymentConstraints {
6264
max_cltv_expiry: u32::max_value(),
@@ -104,6 +106,7 @@ fn do_one_hop_blinded_path(success: bool) {
104106
let amt_msat = 5000;
105107
let (payment_preimage, payment_hash, payment_secret) = get_payment_preimage_hash(&nodes[1], Some(amt_msat), None);
106108
let payee_tlvs = ReceiveTlvs {
109+
padding: None,
107110
payment_secret,
108111
payment_constraints: PaymentConstraints {
109112
max_cltv_expiry: u32::max_value(),
@@ -148,6 +151,7 @@ fn mpp_to_one_hop_blinded_path() {
148151
let amt_msat = 15_000_000;
149152
let (payment_preimage, payment_hash, payment_secret) = get_payment_preimage_hash(&nodes[3], Some(amt_msat), None);
150153
let payee_tlvs = ReceiveTlvs {
154+
padding: None,
151155
payment_secret,
152156
payment_constraints: PaymentConstraints {
153157
max_cltv_expiry: u32::max_value(),
@@ -1293,6 +1297,7 @@ fn custom_tlvs_to_blinded_path() {
12931297
let amt_msat = 5000;
12941298
let (payment_preimage, payment_hash, payment_secret) = get_payment_preimage_hash(&nodes[1], Some(amt_msat), None);
12951299
let payee_tlvs = ReceiveTlvs {
1300+
padding: None,
12961301
payment_secret,
12971302
payment_constraints: PaymentConstraints {
12981303
max_cltv_expiry: u32::max_value(),

lightning/src/ln/channelmanager.rs

+1
Original file line numberDiff line numberDiff line change
@@ -9345,6 +9345,7 @@ where
93459345
let max_cltv_expiry = self.best_block.read().unwrap().height + CLTV_FAR_FAR_AWAY
93469346
+ LATENCY_GRACE_PERIOD_BLOCKS;
93479347
let payee_tlvs = ReceiveTlvs {
9348+
padding: None,
93489349
payment_secret,
93499350
payment_constraints: PaymentConstraints {
93509351
max_cltv_expiry,

lightning/src/ln/max_payment_path_len_tests.rs

+1
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ fn one_hop_blinded_path_with_custom_tlv() {
159159
let amt_msat = 100_000;
160160
let (payment_preimage, payment_hash, payment_secret) = get_payment_preimage_hash(&nodes[2], Some(amt_msat), None);
161161
let payee_tlvs = ReceiveTlvs {
162+
padding: None,
162163
payment_secret,
163164
payment_constraints: PaymentConstraints {
164165
max_cltv_expiry: u32::max_value(),

lightning/src/ln/msgs.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -2758,7 +2758,8 @@ impl<NS: Deref> ReadableArgs<(Option<PublicKey>, &NS)> for InboundOnionPayload w
27582758
let mut reader = FixedLengthReader::new(&mut s, enc_tlvs.len() as u64);
27592759
match ChaChaPolyReadAdapter::read(&mut reader, rho)? {
27602760
ChaChaPolyReadAdapter { readable: BlindedPaymentTlvs::Forward(ForwardTlvs {
2761-
short_channel_id, payment_relay, payment_constraints, features
2761+
padding: _, short_channel_id, payment_relay,
2762+
payment_constraints, features
27622763
})} => {
27632764
if amt.is_some() || cltv_value.is_some() || total_msat.is_some() ||
27642765
keysend_preimage.is_some()
@@ -2774,7 +2775,8 @@ impl<NS: Deref> ReadableArgs<(Option<PublicKey>, &NS)> for InboundOnionPayload w
27742775
})
27752776
},
27762777
ChaChaPolyReadAdapter { readable: BlindedPaymentTlvs::Receive(ReceiveTlvs {
2777-
payment_secret, payment_constraints, payment_context
2778+
padding: _, payment_secret, payment_constraints,
2779+
payment_context
27782780
})} => {
27792781
if total_msat.unwrap_or(0) > MAX_VALUE_MSAT { return Err(DecodeError::InvalidValue) }
27802782
Ok(Self::BlindedReceive {

lightning/src/routing/router.rs

+1
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ impl<G: Deref<Target = NetworkGraph<L>> + Clone, L: Deref, ES: Deref, S: Deref,
146146
};
147147
Some(payment::ForwardNode {
148148
tlvs: ForwardTlvs {
149+
padding: None,
149150
short_channel_id,
150151
payment_relay,
151152
payment_constraints,

0 commit comments

Comments
 (0)