@@ -92,6 +92,16 @@ enum BlindedPaymentTlvsRef<'a> {
9292 Receive ( & ' a ReceiveTlvs ) ,
9393}
9494
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+
95105/// Parameters for relaying over a given [`BlindedHop`].
96106///
97107/// [`BlindedHop`]: crate::blinded_path::BlindedHop
@@ -235,16 +245,16 @@ impl<'a> Writeable for BlindedPaymentTlvsRef<'a> {
235245 }
236246}
237247
238- impl < ' a > Writeable for ( usize , BlindedPaymentTlvsRef < ' a > ) {
248+ impl < ' a > Writeable for PaymentLengthTlvs < ' a > {
239249 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 ( ) ;
241251 let padding = Some ( Padding :: new ( length) ) ;
242252
243253 encode_tlv_stream ! ( writer, {
244254 ( 1 , padding, option)
245255 } ) ;
246256
247- self . 1 . write ( writer)
257+ self . tlvs . write ( writer)
248258 }
249259}
250260
@@ -297,7 +307,7 @@ pub(super) fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>(
297307 . max ( )
298308 . unwrap_or ( 0 ) ;
299309
300- let length_tlvs = tlvs. map ( |tlv| ( max_length, tlv ) ) ;
310+ let length_tlvs = tlvs. map ( |tlvs| PaymentLengthTlvs { max_length, tlvs } ) ;
301311
302312 utils:: construct_blinded_hops ( secp_ctx, pks, length_tlvs, session_priv)
303313}
0 commit comments