@@ -372,9 +372,36 @@ where
372372#[ cfg( test) ]
373373mod tests {
374374 use super :: * ;
375- use lightning:: util:: test_utils:: { TestLogger , TestStore } ;
375+ use lightning:: util:: {
376+ ser:: Readable ,
377+ test_utils:: { TestLogger , TestStore } ,
378+ } ;
379+ use std:: io:: Cursor ;
376380 use std:: sync:: Arc ;
377381
382+ /// We refactored `PaymentDetails` to hold a payment id and moved some required fields into
383+ /// `PaymentKind`. Here, we keep the old layout available in order test de/ser compatibility.
384+ #[ derive( Clone , Debug , PartialEq , Eq ) ]
385+ struct OldPaymentDetails {
386+ pub hash : PaymentHash ,
387+ pub preimage : Option < PaymentPreimage > ,
388+ pub secret : Option < PaymentSecret > ,
389+ pub amount_msat : Option < u64 > ,
390+ pub direction : PaymentDirection ,
391+ pub status : PaymentStatus ,
392+ pub lsp_fee_limits : Option < LSPFeeLimits > ,
393+ }
394+
395+ impl_writeable_tlv_based ! ( OldPaymentDetails , {
396+ ( 0 , hash, required) ,
397+ ( 1 , lsp_fee_limits, option) ,
398+ ( 2 , preimage, required) ,
399+ ( 4 , secret, required) ,
400+ ( 6 , amount_msat, required) ,
401+ ( 8 , direction, required) ,
402+ ( 10 , status, required)
403+ } ) ;
404+
378405 #[ test]
379406 fn payment_info_is_persisted ( ) {
380407 let store: Arc < DynStore > = Arc :: new ( TestStore :: new ( false ) ) ;
@@ -422,4 +449,133 @@ mod tests {
422449
423450 assert_eq ! ( PaymentStatus :: Succeeded , payment_store. get( & id) . unwrap( ) . status) ;
424451 }
452+
453+ #[ test]
454+ fn old_payment_details_deser_compat ( ) {
455+ // We refactored `PaymentDetails` to hold a payment id and moved some required fields into
456+ // `PaymentKind`. Here, we test compatibility with the old layout.
457+ let hash = PaymentHash ( [ 42u8 ; 32 ] ) ;
458+ let preimage = Some ( PaymentPreimage ( [ 43u8 ; 32 ] ) ) ;
459+ let secret = Some ( PaymentSecret ( [ 44u8 ; 32 ] ) ) ;
460+ let amount_msat = Some ( 45_000_000 ) ;
461+
462+ // Test `Bolt11` de/ser
463+ {
464+ let old_bolt11_payment = OldPaymentDetails {
465+ hash,
466+ preimage,
467+ secret,
468+ amount_msat,
469+ direction : PaymentDirection :: Inbound ,
470+ status : PaymentStatus :: Pending ,
471+ lsp_fee_limits : None ,
472+ } ;
473+
474+ let old_bolt11_encoded = old_bolt11_payment. encode ( ) ;
475+ assert_eq ! (
476+ old_bolt11_payment,
477+ OldPaymentDetails :: read( & mut Cursor :: new( old_bolt11_encoded. clone( ) ) ) . unwrap( )
478+ ) ;
479+
480+ let bolt11_decoded =
481+ PaymentDetails :: read ( & mut Cursor :: new ( old_bolt11_encoded) ) . unwrap ( ) ;
482+ let bolt11_reencoded = bolt11_decoded. encode ( ) ;
483+ assert_eq ! (
484+ bolt11_decoded,
485+ PaymentDetails :: read( & mut Cursor :: new( bolt11_reencoded) ) . unwrap( )
486+ ) ;
487+
488+ match bolt11_decoded. kind {
489+ PaymentKind :: Bolt11 { hash : h, preimage : p, secret : s } => {
490+ assert_eq ! ( hash, h) ;
491+ assert_eq ! ( preimage, p) ;
492+ assert_eq ! ( secret, s) ;
493+ } ,
494+ _ => {
495+ panic ! ( "Unexpected kind!" ) ;
496+ } ,
497+ }
498+ }
499+
500+ // Test `Bolt11Jit` de/ser
501+ {
502+ let lsp_fee_limits = Some ( LSPFeeLimits {
503+ max_total_opening_fee_msat : Some ( 46_000 ) ,
504+ max_proportional_opening_fee_ppm_msat : Some ( 47_000 ) ,
505+ } ) ;
506+
507+ let old_bolt11_jit_payment = OldPaymentDetails {
508+ hash,
509+ preimage,
510+ secret,
511+ amount_msat,
512+ direction : PaymentDirection :: Inbound ,
513+ status : PaymentStatus :: Pending ,
514+ lsp_fee_limits,
515+ } ;
516+
517+ let old_bolt11_jit_encoded = old_bolt11_jit_payment. encode ( ) ;
518+ assert_eq ! (
519+ old_bolt11_jit_payment,
520+ OldPaymentDetails :: read( & mut Cursor :: new( old_bolt11_jit_encoded. clone( ) ) ) . unwrap( )
521+ ) ;
522+
523+ let bolt11_jit_decoded =
524+ PaymentDetails :: read ( & mut Cursor :: new ( old_bolt11_jit_encoded) ) . unwrap ( ) ;
525+ let bolt11_jit_reencoded = bolt11_jit_decoded. encode ( ) ;
526+ assert_eq ! (
527+ bolt11_jit_decoded,
528+ PaymentDetails :: read( & mut Cursor :: new( bolt11_jit_reencoded) ) . unwrap( )
529+ ) ;
530+
531+ match bolt11_jit_decoded. kind {
532+ PaymentKind :: Bolt11Jit { hash : h, preimage : p, secret : s, lsp_fee_limits : l } => {
533+ assert_eq ! ( hash, h) ;
534+ assert_eq ! ( preimage, p) ;
535+ assert_eq ! ( secret, s) ;
536+ assert_eq ! ( lsp_fee_limits, Some ( l) ) ;
537+ } ,
538+ _ => {
539+ panic ! ( "Unexpected kind!" ) ;
540+ } ,
541+ }
542+ }
543+
544+ // Test `Spontaneous` de/ser
545+ {
546+ let old_spontaneous_payment = OldPaymentDetails {
547+ hash,
548+ preimage,
549+ secret : None ,
550+ amount_msat,
551+ direction : PaymentDirection :: Inbound ,
552+ status : PaymentStatus :: Pending ,
553+ lsp_fee_limits : None ,
554+ } ;
555+
556+ let old_spontaneous_encoded = old_spontaneous_payment. encode ( ) ;
557+ assert_eq ! (
558+ old_spontaneous_payment,
559+ OldPaymentDetails :: read( & mut Cursor :: new( old_spontaneous_encoded. clone( ) ) ) . unwrap( )
560+ ) ;
561+
562+ let spontaneous_decoded =
563+ PaymentDetails :: read ( & mut Cursor :: new ( old_spontaneous_encoded) ) . unwrap ( ) ;
564+ let spontaneous_reencoded = spontaneous_decoded. encode ( ) ;
565+ assert_eq ! (
566+ spontaneous_decoded,
567+ PaymentDetails :: read( & mut Cursor :: new( spontaneous_reencoded) ) . unwrap( )
568+ ) ;
569+
570+ match spontaneous_decoded. kind {
571+ PaymentKind :: Spontaneous { hash : h, preimage : p } => {
572+ assert_eq ! ( hash, h) ;
573+ assert_eq ! ( preimage, p) ;
574+ } ,
575+ _ => {
576+ panic ! ( "Unexpected kind!" ) ;
577+ } ,
578+ }
579+ }
580+ }
425581}
0 commit comments