Skip to content

Commit 7a8f948

Browse files
committed
Add test ensuring de/ser compatibility with old format
.. as we changed quite a bit and moved required fields, we add a test here that adds the old `PaymentDetails` as a test struct and ensures we're able to parse them just fine
1 parent fc57327 commit 7a8f948

File tree

1 file changed

+157
-1
lines changed

1 file changed

+157
-1
lines changed

src/payment/payment_store.rs

Lines changed: 157 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,9 +373,36 @@ where
373373
#[cfg(test)]
374374
mod tests {
375375
use super::*;
376-
use lightning::util::test_utils::{TestLogger, TestStore};
376+
use lightning::util::{
377+
ser::Readable,
378+
test_utils::{TestLogger, TestStore},
379+
};
380+
use std::io::Cursor;
377381
use std::sync::Arc;
378382

383+
/// We refactored `PaymentDetails` to hold a payment id and moved some required fields into
384+
/// `PaymentKind`. Here, we keep the old layout available in order test de/ser compatibility.
385+
#[derive(Clone, Debug, PartialEq, Eq)]
386+
struct OldPaymentDetails {
387+
pub hash: PaymentHash,
388+
pub preimage: Option<PaymentPreimage>,
389+
pub secret: Option<PaymentSecret>,
390+
pub amount_msat: Option<u64>,
391+
pub direction: PaymentDirection,
392+
pub status: PaymentStatus,
393+
pub lsp_fee_limits: Option<LSPFeeLimits>,
394+
}
395+
396+
impl_writeable_tlv_based!(OldPaymentDetails, {
397+
(0, hash, required),
398+
(1, lsp_fee_limits, option),
399+
(2, preimage, required),
400+
(4, secret, required),
401+
(6, amount_msat, required),
402+
(8, direction, required),
403+
(10, status, required)
404+
});
405+
379406
#[test]
380407
fn payment_info_is_persisted() {
381408
let store: Arc<DynStore> = Arc::new(TestStore::new(false));
@@ -424,4 +451,133 @@ mod tests {
424451

425452
assert_eq!(PaymentStatus::Succeeded, payment_store.get(&id).unwrap().status);
426453
}
454+
455+
#[test]
456+
fn old_payment_details_deser_compat() {
457+
// We refactored `PaymentDetails` to hold a payment id and moved some required fields into
458+
// `PaymentKind`. Here, we test compatibility with the old layout.
459+
let hash = PaymentHash([42u8; 32]);
460+
let preimage = Some(PaymentPreimage([43u8; 32]));
461+
let secret = Some(PaymentSecret([44u8; 32]));
462+
let amount_msat = Some(45_000_000);
463+
464+
// Test `Bolt11` de/ser
465+
{
466+
let old_bolt11_payment = OldPaymentDetails {
467+
hash,
468+
preimage,
469+
secret,
470+
amount_msat,
471+
direction: PaymentDirection::Inbound,
472+
status: PaymentStatus::Pending,
473+
lsp_fee_limits: None,
474+
};
475+
476+
let old_bolt11_encoded = old_bolt11_payment.encode();
477+
assert_eq!(
478+
old_bolt11_payment,
479+
OldPaymentDetails::read(&mut Cursor::new(old_bolt11_encoded.clone())).unwrap()
480+
);
481+
482+
let bolt11_decoded =
483+
PaymentDetails::read(&mut Cursor::new(old_bolt11_encoded)).unwrap();
484+
let bolt11_reencoded = bolt11_decoded.encode();
485+
assert_eq!(
486+
bolt11_decoded,
487+
PaymentDetails::read(&mut Cursor::new(bolt11_reencoded)).unwrap()
488+
);
489+
490+
match bolt11_decoded.kind {
491+
PaymentKind::Bolt11 { hash: h, preimage: p, secret: s } => {
492+
assert_eq!(hash, h);
493+
assert_eq!(preimage, p);
494+
assert_eq!(secret, s);
495+
},
496+
_ => {
497+
panic!("Unexpected kind!");
498+
},
499+
}
500+
}
501+
502+
// Test `Bolt11Jit` de/ser
503+
{
504+
let lsp_fee_limits = Some(LSPFeeLimits {
505+
max_total_opening_fee_msat: Some(46_000),
506+
max_proportional_opening_fee_ppm_msat: Some(47_000),
507+
});
508+
509+
let old_bolt11_jit_payment = OldPaymentDetails {
510+
hash,
511+
preimage,
512+
secret,
513+
amount_msat,
514+
direction: PaymentDirection::Inbound,
515+
status: PaymentStatus::Pending,
516+
lsp_fee_limits,
517+
};
518+
519+
let old_bolt11_jit_encoded = old_bolt11_jit_payment.encode();
520+
assert_eq!(
521+
old_bolt11_jit_payment,
522+
OldPaymentDetails::read(&mut Cursor::new(old_bolt11_jit_encoded.clone())).unwrap()
523+
);
524+
525+
let bolt11_jit_decoded =
526+
PaymentDetails::read(&mut Cursor::new(old_bolt11_jit_encoded)).unwrap();
527+
let bolt11_jit_reencoded = bolt11_jit_decoded.encode();
528+
assert_eq!(
529+
bolt11_jit_decoded,
530+
PaymentDetails::read(&mut Cursor::new(bolt11_jit_reencoded)).unwrap()
531+
);
532+
533+
match bolt11_jit_decoded.kind {
534+
PaymentKind::Bolt11Jit { hash: h, preimage: p, secret: s, lsp_fee_limits: l } => {
535+
assert_eq!(hash, h);
536+
assert_eq!(preimage, p);
537+
assert_eq!(secret, s);
538+
assert_eq!(lsp_fee_limits, Some(l));
539+
},
540+
_ => {
541+
panic!("Unexpected kind!");
542+
},
543+
}
544+
}
545+
546+
// Test `Spontaneous` de/ser
547+
{
548+
let old_spontaneous_payment = OldPaymentDetails {
549+
hash,
550+
preimage,
551+
secret: None,
552+
amount_msat,
553+
direction: PaymentDirection::Inbound,
554+
status: PaymentStatus::Pending,
555+
lsp_fee_limits: None,
556+
};
557+
558+
let old_spontaneous_encoded = old_spontaneous_payment.encode();
559+
assert_eq!(
560+
old_spontaneous_payment,
561+
OldPaymentDetails::read(&mut Cursor::new(old_spontaneous_encoded.clone())).unwrap()
562+
);
563+
564+
let spontaneous_decoded =
565+
PaymentDetails::read(&mut Cursor::new(old_spontaneous_encoded)).unwrap();
566+
let spontaneous_reencoded = spontaneous_decoded.encode();
567+
assert_eq!(
568+
spontaneous_decoded,
569+
PaymentDetails::read(&mut Cursor::new(spontaneous_reencoded)).unwrap()
570+
);
571+
572+
match spontaneous_decoded.kind {
573+
PaymentKind::Spontaneous { hash: h, preimage: p } => {
574+
assert_eq!(hash, h);
575+
assert_eq!(preimage, p);
576+
},
577+
_ => {
578+
panic!("Unexpected kind!");
579+
},
580+
}
581+
}
582+
}
427583
}

0 commit comments

Comments
 (0)