@@ -8,10 +8,13 @@ use ldk_node::{
88} ;
99
1010use lightning:: ln:: msgs:: SocketAddress ;
11+ use lightning:: ln:: { PaymentHash , PaymentPreimage } ;
1112use lightning:: util:: persist:: KVStore ;
1213use lightning:: util:: test_utils:: TestStore ;
1314use lightning_persister:: fs_store:: FilesystemStore ;
1415
16+ use bitcoin:: hashes:: sha256:: Hash as Sha256 ;
17+ use bitcoin:: hashes:: Hash ;
1518use bitcoin:: { Address , Amount , Network , OutPoint , Txid } ;
1619
1720use bitcoincore_rpc:: bitcoincore_rpc_json:: AddressType ;
@@ -99,6 +102,31 @@ macro_rules! expect_payment_received_event {
99102
100103pub ( crate ) use expect_payment_received_event;
101104
105+ macro_rules! expect_payment_claimable_event {
106+ ( $node: expr, $payment_id: expr, $payment_hash: expr, $claimable_amount_msat: expr) => { {
107+ match $node. wait_next_event( ) {
108+ ref e @ Event :: PaymentClaimable {
109+ payment_id,
110+ payment_hash,
111+ claimable_amount_msat,
112+ ..
113+ } => {
114+ println!( "{} got event {:?}" , std:: stringify!( $node) , e) ;
115+ assert_eq!( payment_hash, $payment_hash) ;
116+ assert_eq!( payment_id, $payment_id) ;
117+ assert_eq!( claimable_amount_msat, $claimable_amount_msat) ;
118+ $node. event_handled( ) ;
119+ claimable_amount_msat
120+ } ,
121+ ref e => {
122+ panic!( "{} got unexpected event!: {:?}" , std:: stringify!( $node) , e) ;
123+ } ,
124+ }
125+ } } ;
126+ }
127+
128+ pub ( crate ) use expect_payment_claimable_event;
129+
102130macro_rules! expect_payment_successful_event {
103131 ( $node: expr, $payment_id: expr, $fee_paid_msat: expr) => { {
104132 match $node. wait_next_event( ) {
@@ -378,7 +406,7 @@ pub(crate) fn do_channel_full_cycle<E: ElectrumApi>(
378406 let addr_a = node_a. onchain_payment ( ) . new_address ( ) . unwrap ( ) ;
379407 let addr_b = node_b. onchain_payment ( ) . new_address ( ) . unwrap ( ) ;
380408
381- let premine_amount_sat = if expect_anchor_channel { 125_000 } else { 100_000 } ;
409+ let premine_amount_sat = if expect_anchor_channel { 2_125_000 } else { 2_100_000 } ;
382410
383411 premine_and_distribute_funds (
384412 & bitcoind,
@@ -396,7 +424,7 @@ pub(crate) fn do_channel_full_cycle<E: ElectrumApi>(
396424 assert_eq ! ( node_b. next_event( ) , None ) ;
397425
398426 println ! ( "\n A -- connect_open_channel -> B" ) ;
399- let funding_amount_sat = 80_000 ;
427+ let funding_amount_sat = 2_080_000 ;
400428 let push_msat = ( funding_amount_sat / 2 ) * 1000 ; // balance the channel
401429 node_a
402430 . connect_open_channel (
@@ -580,6 +608,89 @@ pub(crate) fn do_channel_full_cycle<E: ElectrumApi>(
580608 assert_eq ! ( node_b. payment( & payment_id) . unwrap( ) . amount_msat, Some ( determined_amount_msat) ) ;
581609 assert ! ( matches!( node_b. payment( & payment_id) . unwrap( ) . kind, PaymentKind :: Bolt11 { .. } ) ) ;
582610
611+ // Test claiming manually registered payments.
612+ let invoice_amount_3_msat = 5_532_000 ;
613+ let manual_preimage = PaymentPreimage ( [ 42u8 ; 32 ] ) ;
614+ let manual_payment_hash = PaymentHash ( Sha256 :: hash ( & manual_preimage. 0 ) . to_byte_array ( ) ) ;
615+ let manual_invoice = node_b
616+ . bolt11_payment ( )
617+ . receive_for_hash ( invoice_amount_3_msat, & "asdf" , 9217 , manual_payment_hash)
618+ . unwrap ( ) ;
619+ let manual_payment_id = node_a. bolt11_payment ( ) . send ( & manual_invoice) . unwrap ( ) ;
620+
621+ let claimable_amount_msat = expect_payment_claimable_event ! (
622+ node_b,
623+ manual_payment_id,
624+ manual_payment_hash,
625+ invoice_amount_3_msat
626+ ) ;
627+ node_b
628+ . bolt11_payment ( )
629+ . claim_for_hash ( manual_payment_hash, claimable_amount_msat, manual_preimage)
630+ . unwrap ( ) ;
631+ expect_payment_received_event ! ( node_b, claimable_amount_msat) ;
632+ expect_payment_successful_event ! ( node_a, Some ( manual_payment_id) , None ) ;
633+ assert_eq ! ( node_a. payment( & manual_payment_id) . unwrap( ) . status, PaymentStatus :: Succeeded ) ;
634+ assert_eq ! ( node_a. payment( & manual_payment_id) . unwrap( ) . direction, PaymentDirection :: Outbound ) ;
635+ assert_eq ! (
636+ node_a. payment( & manual_payment_id) . unwrap( ) . amount_msat,
637+ Some ( invoice_amount_3_msat)
638+ ) ;
639+ assert ! ( matches!( node_a. payment( & manual_payment_id) . unwrap( ) . kind, PaymentKind :: Bolt11 { .. } ) ) ;
640+ assert_eq ! ( node_b. payment( & manual_payment_id) . unwrap( ) . status, PaymentStatus :: Succeeded ) ;
641+ assert_eq ! ( node_b. payment( & manual_payment_id) . unwrap( ) . direction, PaymentDirection :: Inbound ) ;
642+ assert_eq ! (
643+ node_b. payment( & manual_payment_id) . unwrap( ) . amount_msat,
644+ Some ( invoice_amount_3_msat)
645+ ) ;
646+ assert ! ( matches!( node_b. payment( & manual_payment_id) . unwrap( ) . kind, PaymentKind :: Bolt11 { .. } ) ) ;
647+
648+ // Test failing manually registered payments.
649+ let invoice_amount_4_msat = 5_532_000 ;
650+ let manual_fail_preimage = PaymentPreimage ( [ 43u8 ; 32 ] ) ;
651+ let manual_fail_payment_hash =
652+ PaymentHash ( Sha256 :: hash ( & manual_fail_preimage. 0 ) . to_byte_array ( ) ) ;
653+ let manual_fail_invoice = node_b
654+ . bolt11_payment ( )
655+ . receive_for_hash ( invoice_amount_3_msat, & "asdf" , 9217 , manual_fail_payment_hash)
656+ . unwrap ( ) ;
657+ let manual_fail_payment_id = node_a. bolt11_payment ( ) . send ( & manual_fail_invoice) . unwrap ( ) ;
658+
659+ expect_payment_claimable_event ! (
660+ node_b,
661+ manual_fail_payment_id,
662+ manual_fail_payment_hash,
663+ invoice_amount_4_msat
664+ ) ;
665+ node_b. bolt11_payment ( ) . fail_for_hash ( manual_fail_payment_hash) . unwrap ( ) ;
666+ expect_event ! ( node_a, PaymentFailed ) ;
667+ assert_eq ! ( node_a. payment( & manual_fail_payment_id) . unwrap( ) . status, PaymentStatus :: Failed ) ;
668+ assert_eq ! (
669+ node_a. payment( & manual_fail_payment_id) . unwrap( ) . direction,
670+ PaymentDirection :: Outbound
671+ ) ;
672+ assert_eq ! (
673+ node_a. payment( & manual_fail_payment_id) . unwrap( ) . amount_msat,
674+ Some ( invoice_amount_4_msat)
675+ ) ;
676+ assert ! ( matches!(
677+ node_a. payment( & manual_fail_payment_id) . unwrap( ) . kind,
678+ PaymentKind :: Bolt11 { .. }
679+ ) ) ;
680+ assert_eq ! ( node_b. payment( & manual_fail_payment_id) . unwrap( ) . status, PaymentStatus :: Failed ) ;
681+ assert_eq ! (
682+ node_b. payment( & manual_fail_payment_id) . unwrap( ) . direction,
683+ PaymentDirection :: Inbound
684+ ) ;
685+ assert_eq ! (
686+ node_b. payment( & manual_fail_payment_id) . unwrap( ) . amount_msat,
687+ Some ( invoice_amount_4_msat)
688+ ) ;
689+ assert ! ( matches!(
690+ node_b. payment( & manual_fail_payment_id) . unwrap( ) . kind,
691+ PaymentKind :: Bolt11 { .. }
692+ ) ) ;
693+
583694 // Test spontaneous/keysend payments
584695 println ! ( "\n A send_spontaneous_payment" ) ;
585696 let keysend_amount_msat = 2500_000 ;
@@ -611,8 +722,8 @@ pub(crate) fn do_channel_full_cycle<E: ElectrumApi>(
611722 node_b. payment( & keysend_payment_id) . unwrap( ) . kind,
612723 PaymentKind :: Spontaneous { .. }
613724 ) ) ;
614- assert_eq ! ( node_a. list_payments( ) . len( ) , 4 ) ;
615- assert_eq ! ( node_b. list_payments( ) . len( ) , 5 ) ;
725+ assert_eq ! ( node_a. list_payments( ) . len( ) , 6 ) ;
726+ assert_eq ! ( node_b. list_payments( ) . len( ) , 7 ) ;
616727
617728 println ! ( "\n B close_channel (force: {})" , force_close) ;
618729 if force_close {
@@ -715,6 +826,7 @@ pub(crate) fn do_channel_full_cycle<E: ElectrumApi>(
715826 let sum_of_all_payments_sat = ( push_msat
716827 + invoice_amount_1_msat
717828 + overpaid_amount_msat
829+ + invoice_amount_3_msat
718830 + determined_amount_msat
719831 + keysend_amount_msat)
720832 / 1000 ;
0 commit comments