Skip to content

Commit f198172

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 d638308 commit f198172

File tree

1 file changed

+155
-1
lines changed

1 file changed

+155
-1
lines changed

src/payment/payment_store.rs

Lines changed: 155 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,131 @@ 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+
match bolt11_jit_decoded.kind {
533+
PaymentKind::Bolt11Jit { hash: h, preimage: p, secret: s, lsp_fee_limits: l } => {
534+
assert_eq!(hash, h);
535+
assert_eq!(preimage, p);
536+
assert_eq!(secret, s);
537+
assert_eq!(lsp_fee_limits, Some(l));
538+
},
539+
_ => {
540+
panic!("Unexpected kind!");
541+
},
542+
}
543+
}
544+
545+
// Test `Spontaneous` de/ser
546+
{
547+
let old_spontaneous_payment = OldPaymentDetails {
548+
hash,
549+
preimage,
550+
secret: None,
551+
amount_msat,
552+
direction: PaymentDirection::Inbound,
553+
status: PaymentStatus::Pending,
554+
lsp_fee_limits: None,
555+
};
556+
557+
let old_spontaneous_encoded = old_spontaneous_payment.encode();
558+
assert_eq!(
559+
old_spontaneous_payment,
560+
OldPaymentDetails::read(&mut Cursor::new(old_spontaneous_encoded.clone())).unwrap()
561+
);
562+
563+
let spontaneous_decoded =
564+
PaymentDetails::read(&mut Cursor::new(old_spontaneous_encoded)).unwrap();
565+
let spontaneous_reencoded = spontaneous_decoded.encode();
566+
assert_eq!(
567+
spontaneous_decoded,
568+
PaymentDetails::read(&mut Cursor::new(spontaneous_reencoded)).unwrap()
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+
}
427581
}

0 commit comments

Comments
 (0)