@@ -7,14 +7,18 @@ use common::{
77 setup_node, setup_two_nodes, wait_for_tx, TestSyncStore ,
88} ;
99
10+ use ldk_node:: payment:: PaymentKind ;
1011use ldk_node:: { Builder , Event , NodeError } ;
1112
13+ use lightning:: ln:: channelmanager:: PaymentId ;
1214use lightning:: util:: persist:: KVStore ;
1315
1416use bitcoin:: { Amount , Network } ;
1517
1618use std:: sync:: Arc ;
1719
20+ use crate :: common:: expect_channel_ready_event;
21+
1822#[ test]
1923fn channel_full_cycle ( ) {
2024 let ( bitcoind, electrsd) = setup_bitcoind_and_electrsd ( ) ;
@@ -138,7 +142,7 @@ fn multi_hop_sending() {
138142
139143 let payment_id = expect_payment_received_event ! ( & nodes[ 4 ] , 2_500_000 ) ;
140144 let fee_paid_msat = Some ( 2000 ) ;
141- expect_payment_successful_event ! ( nodes[ 0 ] , payment_id, fee_paid_msat) ;
145+ expect_payment_successful_event ! ( nodes[ 0 ] , payment_id, Some ( fee_paid_msat) ) ;
142146}
143147
144148#[ test]
@@ -366,3 +370,121 @@ fn concurrent_connections_succeed() {
366370 h. join ( ) . unwrap ( ) ;
367371 }
368372}
373+
374+ #[ test]
375+ fn simple_bolt12_send_receive ( ) {
376+ let ( bitcoind, electrsd) = setup_bitcoind_and_electrsd ( ) ;
377+ let ( node_a, node_b) = setup_two_nodes ( & electrsd, false ) ;
378+
379+ let address_a = node_a. onchain_payment ( ) . new_address ( ) . unwrap ( ) ;
380+ let premine_amount_sat = 5_000_000 ;
381+ premine_and_distribute_funds (
382+ & bitcoind. client ,
383+ & electrsd. client ,
384+ vec ! [ address_a] ,
385+ Amount :: from_sat ( premine_amount_sat) ,
386+ ) ;
387+
388+ node_a. sync_wallets ( ) . unwrap ( ) ;
389+ open_channel ( & node_a, & node_b, 4_000_000 , true , & electrsd) ;
390+
391+ generate_blocks_and_wait ( & bitcoind. client , & electrsd. client , 6 ) ;
392+
393+ node_a. sync_wallets ( ) . unwrap ( ) ;
394+ node_b. sync_wallets ( ) . unwrap ( ) ;
395+
396+ expect_channel_ready_event ! ( node_a, node_b. node_id( ) ) ;
397+ expect_channel_ready_event ! ( node_b, node_a. node_id( ) ) ;
398+
399+ // Sleep until we broadcasted a node announcement.
400+ while node_b. status ( ) . latest_node_announcement_broadcast_timestamp . is_none ( ) {
401+ std:: thread:: sleep ( std:: time:: Duration :: from_millis ( 10 ) ) ;
402+ }
403+
404+ // Sleep one more sec to make sure the node announcement propagates.
405+ std:: thread:: sleep ( std:: time:: Duration :: from_secs ( 1 ) ) ;
406+
407+ let expected_amount_msat = 100_000_000 ;
408+ let offer = node_b. bolt12_payment ( ) . receive ( expected_amount_msat, "asdf" ) . unwrap ( ) ;
409+ let payment_id = node_a. bolt12_payment ( ) . send ( & offer, None ) . unwrap ( ) ;
410+
411+ expect_payment_successful_event ! ( node_a, Some ( payment_id) , None ) ;
412+ let node_a_payments = node_a. list_payments ( ) ;
413+ assert_eq ! ( node_a_payments. len( ) , 1 ) ;
414+ match node_a_payments. first ( ) . unwrap ( ) . kind {
415+ PaymentKind :: Bolt12Offer { hash, preimage, secret : _, offer_id } => {
416+ assert ! ( hash. is_some( ) ) ;
417+ assert ! ( preimage. is_some( ) ) ;
418+ assert_eq ! ( offer_id, offer. id( ) ) ;
419+ //TODO: We should eventually set and assert the secret sender-side, too, but the BOLT12
420+ //API currently doesn't allow to do that.
421+ } ,
422+ _ => {
423+ panic ! ( "Unexpected payment kind" ) ;
424+ } ,
425+ }
426+ assert_eq ! ( node_a_payments. first( ) . unwrap( ) . amount_msat, Some ( expected_amount_msat) ) ;
427+
428+ expect_payment_received_event ! ( node_b, expected_amount_msat) ;
429+ let node_b_payments = node_b. list_payments ( ) ;
430+ assert_eq ! ( node_b_payments. len( ) , 1 ) ;
431+ match node_b_payments. first ( ) . unwrap ( ) . kind {
432+ PaymentKind :: Bolt12Offer { hash, preimage, secret, offer_id } => {
433+ assert ! ( hash. is_some( ) ) ;
434+ assert ! ( preimage. is_some( ) ) ;
435+ assert ! ( secret. is_some( ) ) ;
436+ assert_eq ! ( offer_id, offer. id( ) ) ;
437+ } ,
438+ _ => {
439+ panic ! ( "Unexpected payment kind" ) ;
440+ } ,
441+ }
442+ assert_eq ! ( node_b_payments. first( ) . unwrap( ) . amount_msat, Some ( expected_amount_msat) ) ;
443+
444+ // Test send_using_amount
445+ let offer_amount_msat = 100_000_000 ;
446+ let less_than_offer_amount = offer_amount_msat - 10_000 ;
447+ let expected_amount_msat = offer_amount_msat + 10_000 ;
448+ let offer = node_b. bolt12_payment ( ) . receive ( offer_amount_msat, "asdf" ) . unwrap ( ) ;
449+ assert ! ( node_a
450+ . bolt12_payment( )
451+ . send_using_amount( & offer, None , less_than_offer_amount)
452+ . is_err( ) ) ;
453+ let payment_id =
454+ node_a. bolt12_payment ( ) . send_using_amount ( & offer, None , expected_amount_msat) . unwrap ( ) ;
455+
456+ expect_payment_successful_event ! ( node_a, Some ( payment_id) , None ) ;
457+ let node_a_payments = node_a. list_payments_with_filter ( |p| p. id == payment_id) ;
458+ assert_eq ! ( node_a_payments. len( ) , 1 ) ;
459+ let payment_hash = match node_a_payments. first ( ) . unwrap ( ) . kind {
460+ PaymentKind :: Bolt12Offer { hash, preimage, secret : _, offer_id } => {
461+ assert ! ( hash. is_some( ) ) ;
462+ assert ! ( preimage. is_some( ) ) ;
463+ assert_eq ! ( offer_id, offer. id( ) ) ;
464+ //TODO: We should eventually set and assert the secret sender-side, too, but the BOLT12
465+ //API currently doesn't allow to do that.
466+ hash. unwrap ( )
467+ } ,
468+ _ => {
469+ panic ! ( "Unexpected payment kind" ) ;
470+ } ,
471+ } ;
472+ assert_eq ! ( node_a_payments. first( ) . unwrap( ) . amount_msat, Some ( expected_amount_msat) ) ;
473+
474+ expect_payment_received_event ! ( node_b, expected_amount_msat) ;
475+ let node_b_payment_id = PaymentId ( payment_hash. 0 ) ;
476+ let node_b_payments = node_b. list_payments_with_filter ( |p| p. id == node_b_payment_id) ;
477+ assert_eq ! ( node_b_payments. len( ) , 1 ) ;
478+ match node_b_payments. first ( ) . unwrap ( ) . kind {
479+ PaymentKind :: Bolt12Offer { hash, preimage, secret, offer_id } => {
480+ assert ! ( hash. is_some( ) ) ;
481+ assert ! ( preimage. is_some( ) ) ;
482+ assert ! ( secret. is_some( ) ) ;
483+ assert_eq ! ( offer_id, offer. id( ) ) ;
484+ } ,
485+ _ => {
486+ panic ! ( "Unexpected payment kind" ) ;
487+ } ,
488+ }
489+ assert_eq ! ( node_b_payments. first( ) . unwrap( ) . amount_msat, Some ( expected_amount_msat) ) ;
490+ }
0 commit comments