@@ -92,6 +92,16 @@ enum BlindedPaymentTlvsRef<'a> {
92
92
Receive ( & ' a ReceiveTlvs ) ,
93
93
}
94
94
95
+ /// A wrapper struct that stores the largest packet length in the given [`BlindedPath`].
96
+ /// This helps us calculate the appropriate padding size for the [`BlindedPaymentTlvs`]
97
+ /// at the time of writing them.
98
+ pub ( crate ) struct PaymentLengthTlvs < ' a > {
99
+ /// Length of the packet with the largest size in the [`BlindedPath`].
100
+ max_length : usize ,
101
+ /// The current packet's TLVs.
102
+ tlvs : BlindedPaymentTlvsRef < ' a > ,
103
+ }
104
+
95
105
/// Parameters for relaying over a given [`BlindedHop`].
96
106
///
97
107
/// [`BlindedHop`]: crate::blinded_path::BlindedHop
@@ -235,16 +245,16 @@ impl<'a> Writeable for BlindedPaymentTlvsRef<'a> {
235
245
}
236
246
}
237
247
238
- impl < ' a > Writeable for ( usize , BlindedPaymentTlvsRef < ' a > ) {
248
+ impl < ' a > Writeable for PaymentLengthTlvs < ' a > {
239
249
fn write < W : Writer > ( & self , writer : & mut W ) -> Result < ( ) , io:: Error > {
240
- let length = self . 0 - self . 1 . serialized_length ( ) ;
250
+ let length = self . max_length - self . tlvs . serialized_length ( ) ;
241
251
let padding = Some ( Padding :: new ( length) ) ;
242
252
243
253
encode_tlv_stream ! ( writer, {
244
254
( 1 , padding, option)
245
255
} ) ;
246
256
247
- self . 1 . write ( writer)
257
+ self . tlvs . write ( writer)
248
258
}
249
259
}
250
260
@@ -297,7 +307,7 @@ pub(super) fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>(
297
307
. max ( )
298
308
. unwrap_or ( 0 ) ;
299
309
300
- let length_tlvs = tlvs. map ( |tlv| ( max_length, tlv ) ) ;
310
+ let length_tlvs = tlvs. map ( |tlvs| PaymentLengthTlvs { max_length, tlvs } ) ;
301
311
302
312
utils:: construct_blinded_hops ( secp_ctx, pks, length_tlvs, session_priv)
303
313
}
0 commit comments