@@ -19,7 +19,7 @@ use chain::channelmonitor::{ChannelMonitor, CLTV_CLAIM_BUFFER, LATENCY_GRACE_PER
1919use chain:: transaction:: OutPoint ;
2020use chain:: keysinterface:: { KeysInterface , BaseSign } ;
2121use ln:: channel:: { COMMITMENT_TX_BASE_WEIGHT , COMMITMENT_TX_WEIGHT_PER_HTLC } ;
22- use ln:: channelmanager:: { ChannelManager , ChannelManagerReadArgs , RAACommitmentOrder , PaymentPreimage , PaymentHash , PaymentSendFailure , BREAKDOWN_TIMEOUT } ;
22+ use ln:: channelmanager:: { ChannelManager , ChannelManagerReadArgs , RAACommitmentOrder , PaymentPreimage , PaymentSecret , PaymentHash , PaymentSendFailure , BREAKDOWN_TIMEOUT } ;
2323use ln:: channel:: { Channel , ChannelError } ;
2424use ln:: { chan_utils, onion_utils} ;
2525use routing:: router:: { Route , RouteHop , get_route} ;
@@ -8128,6 +8128,164 @@ fn test_simple_mpp() {
81288128 claim_payment_along_route ( & nodes[ 0 ] , & [ & [ & nodes[ 1 ] , & nodes[ 3 ] ] , & [ & nodes[ 2 ] , & nodes[ 3 ] ] ] , false , payment_preimage) ;
81298129}
81308130
8131+ #[ test]
8132+ fn test_preimage_storage ( ) {
8133+ // Simple test of payment preimage storage allowing no client-side storage to claim payments
8134+ let chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
8135+ let node_cfgs = create_node_cfgs ( 2 , & chanmon_cfgs) ;
8136+ let node_chanmgrs = create_node_chanmgrs ( 2 , & node_cfgs, & [ None , None ] ) ;
8137+ let nodes = create_network ( 2 , & node_cfgs, & node_chanmgrs) ;
8138+
8139+ create_announced_chan_between_nodes ( & nodes, 0 , 1 , InitFeatures :: known ( ) , InitFeatures :: known ( ) ) . 0 . contents . short_channel_id ;
8140+
8141+ {
8142+ let ( payment_hash, payment_secret) = nodes[ 1 ] . node . get_payment_secret_preimage ( Some ( 100_000 ) , 1008 , 42 ) ;
8143+
8144+ let logger = test_utils:: TestLogger :: new ( ) ;
8145+ let net_graph_msg_handler = & nodes[ 0 ] . net_graph_msg_handler ;
8146+ let route = get_route ( & nodes[ 0 ] . node . get_our_node_id ( ) , & net_graph_msg_handler. network_graph . read ( ) . unwrap ( ) , & nodes[ 1 ] . node . get_our_node_id ( ) , Some ( InvoiceFeatures :: known ( ) ) , None , & [ ] , 100_000 , TEST_FINAL_CLTV , & logger) . unwrap ( ) ;
8147+ nodes[ 0 ] . node . send_payment ( & route, payment_hash, & Some ( payment_secret) ) . unwrap ( ) ;
8148+ check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
8149+ let mut events = nodes[ 0 ] . node . get_and_clear_pending_msg_events ( ) ;
8150+ let mut payment_event = SendEvent :: from_event ( events. pop ( ) . unwrap ( ) ) ;
8151+ nodes[ 1 ] . node . handle_update_add_htlc ( & nodes[ 0 ] . node . get_our_node_id ( ) , & payment_event. msgs [ 0 ] ) ;
8152+ commitment_signed_dance ! ( nodes[ 1 ] , nodes[ 0 ] , payment_event. commitment_msg, false ) ;
8153+ }
8154+ // Note that after leaving the above scope we have no knowledge of any arguments or return
8155+ // values from previous calls.
8156+ expect_pending_htlcs_forwardable ! ( nodes[ 1 ] ) ;
8157+ let events = nodes[ 1 ] . node . get_and_clear_pending_events ( ) ;
8158+ assert_eq ! ( events. len( ) , 1 ) ;
8159+ match events[ 0 ] {
8160+ Event :: PaymentReceived { payment_preimage, user_payment_id, .. } => {
8161+ assert_eq ! ( user_payment_id, 42 ) ;
8162+ claim_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] ] , payment_preimage. unwrap ( ) ) ;
8163+ } ,
8164+ _ => panic ! ( "Unexpected event" ) ,
8165+ }
8166+ }
8167+
8168+ #[ test]
8169+ fn test_secret_timeout ( ) {
8170+ // Simple test of payment secret storage time outs
8171+ let chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
8172+ let node_cfgs = create_node_cfgs ( 2 , & chanmon_cfgs) ;
8173+ let node_chanmgrs = create_node_chanmgrs ( 2 , & node_cfgs, & [ None , None ] ) ;
8174+ let nodes = create_network ( 2 , & node_cfgs, & node_chanmgrs) ;
8175+
8176+ create_announced_chan_between_nodes ( & nodes, 0 , 1 , InitFeatures :: known ( ) , InitFeatures :: known ( ) ) . 0 . contents . short_channel_id ;
8177+
8178+ let ( payment_hash, payment_secret_1) = nodes[ 1 ] . node . get_payment_secret_preimage ( Some ( 100_000 ) , 2 , 0 ) ;
8179+
8180+ // We should fail to register the same payment hash twice, at least until we've connected two
8181+ // blocks.
8182+ if let Err ( APIError :: APIMisuseError { err } ) = nodes[ 1 ] . node . get_payment_secret ( payment_hash, Some ( 100_000 ) , 2 , 0 ) {
8183+ assert_eq ! ( err, "Duplicate payment hash" ) ;
8184+ } else { panic ! ( ) ; }
8185+ connect_blocks ( & nodes[ 1 ] , 1 ) ;
8186+ if let Err ( APIError :: APIMisuseError { err } ) = nodes[ 1 ] . node . get_payment_secret ( payment_hash, Some ( 100_000 ) , 2 , 0 ) {
8187+ assert_eq ! ( err, "Duplicate payment hash" ) ;
8188+ } else { panic ! ( ) ; }
8189+
8190+ // If we then connect the second block, we should be able to register the same payment hash
8191+ // again with a different user_payment_id (this time getting a new payment secret).
8192+ connect_blocks ( & nodes[ 1 ] , 1 ) ;
8193+ let our_payment_secret = nodes[ 1 ] . node . get_payment_secret ( payment_hash, Some ( 100_000 ) , 2 , 42 ) . unwrap ( ) ;
8194+ assert_ne ! ( payment_secret_1, our_payment_secret) ;
8195+
8196+ {
8197+ let logger = test_utils:: TestLogger :: new ( ) ;
8198+ let net_graph_msg_handler = & nodes[ 0 ] . net_graph_msg_handler ;
8199+ let route = get_route ( & nodes[ 0 ] . node . get_our_node_id ( ) , & net_graph_msg_handler. network_graph . read ( ) . unwrap ( ) , & nodes[ 1 ] . node . get_our_node_id ( ) , Some ( InvoiceFeatures :: known ( ) ) , None , & [ ] , 100_000 , TEST_FINAL_CLTV , & logger) . unwrap ( ) ;
8200+ nodes[ 0 ] . node . send_payment ( & route, payment_hash, & Some ( our_payment_secret) ) . unwrap ( ) ;
8201+ check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
8202+ let mut events = nodes[ 0 ] . node . get_and_clear_pending_msg_events ( ) ;
8203+ let mut payment_event = SendEvent :: from_event ( events. pop ( ) . unwrap ( ) ) ;
8204+ nodes[ 1 ] . node . handle_update_add_htlc ( & nodes[ 0 ] . node . get_our_node_id ( ) , & payment_event. msgs [ 0 ] ) ;
8205+ commitment_signed_dance ! ( nodes[ 1 ] , nodes[ 0 ] , payment_event. commitment_msg, false ) ;
8206+ }
8207+ // Note that after leaving the above scope we have no knowledge of any arguments or return
8208+ // values from previous calls.
8209+ expect_pending_htlcs_forwardable ! ( nodes[ 1 ] ) ;
8210+ let events = nodes[ 1 ] . node . get_and_clear_pending_events ( ) ;
8211+ assert_eq ! ( events. len( ) , 1 ) ;
8212+ match events[ 0 ] {
8213+ Event :: PaymentReceived { payment_preimage, payment_secret, user_payment_id, .. } => {
8214+ assert ! ( payment_preimage. is_none( ) ) ;
8215+ assert_eq ! ( user_payment_id, 42 ) ;
8216+ assert_eq ! ( payment_secret, our_payment_secret) ;
8217+ // We don't actually have the payment preimage with which to claim this payment!
8218+ } ,
8219+ _ => panic ! ( "Unexpected event" ) ,
8220+ }
8221+ }
8222+
8223+ #[ test]
8224+ fn test_bad_secret_hash ( ) {
8225+ // Simple test of unregistered payment hash/invalid payment secret handling
8226+ let chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
8227+ let node_cfgs = create_node_cfgs ( 2 , & chanmon_cfgs) ;
8228+ let node_chanmgrs = create_node_chanmgrs ( 2 , & node_cfgs, & [ None , None ] ) ;
8229+ let nodes = create_network ( 2 , & node_cfgs, & node_chanmgrs) ;
8230+
8231+ create_announced_chan_between_nodes ( & nodes, 0 , 1 , InitFeatures :: known ( ) , InitFeatures :: known ( ) ) . 0 . contents . short_channel_id ;
8232+
8233+ let random_payment_hash = PaymentHash ( [ 42 ; 32 ] ) ;
8234+ let random_payment_secret = PaymentSecret ( [ 43 ; 32 ] ) ;
8235+ let ( our_payment_hash, our_payment_secret) = nodes[ 1 ] . node . get_payment_secret_preimage ( Some ( 100_000 ) , 2 , 0 ) ;
8236+
8237+ let logger = test_utils:: TestLogger :: new ( ) ;
8238+ let net_graph_msg_handler = & nodes[ 0 ] . net_graph_msg_handler ;
8239+ let route = get_route ( & nodes[ 0 ] . node . get_our_node_id ( ) , & net_graph_msg_handler. network_graph . read ( ) . unwrap ( ) , & nodes[ 1 ] . node . get_our_node_id ( ) , Some ( InvoiceFeatures :: known ( ) ) , None , & [ ] , 100_000 , TEST_FINAL_CLTV , & logger) . unwrap ( ) ;
8240+
8241+ // All the below cases should end up being handled exactly identically, so we macro the
8242+ // resulting events.
8243+ macro_rules! handle_unknown_invalid_payment_data {
8244+ ( ) => {
8245+ check_added_monitors!( nodes[ 0 ] , 1 ) ;
8246+ let mut events = nodes[ 0 ] . node. get_and_clear_pending_msg_events( ) ;
8247+ let payment_event = SendEvent :: from_event( events. pop( ) . unwrap( ) ) ;
8248+ nodes[ 1 ] . node. handle_update_add_htlc( & nodes[ 0 ] . node. get_our_node_id( ) , & payment_event. msgs[ 0 ] ) ;
8249+ commitment_signed_dance!( nodes[ 1 ] , nodes[ 0 ] , payment_event. commitment_msg, false ) ;
8250+
8251+ // We have to forward pending HTLCs once to process the receipt of the HTLC and then
8252+ // again to process the pending backwards-failure of the HTLC
8253+ expect_pending_htlcs_forwardable!( nodes[ 1 ] ) ;
8254+ expect_pending_htlcs_forwardable!( nodes[ 1 ] ) ;
8255+ check_added_monitors!( nodes[ 1 ] , 1 ) ;
8256+
8257+ // We should fail the payment back
8258+ let mut events = nodes[ 1 ] . node. get_and_clear_pending_msg_events( ) ;
8259+ match events. pop( ) . unwrap( ) {
8260+ MessageSendEvent :: UpdateHTLCs { node_id: _, updates: msgs:: CommitmentUpdate { update_fail_htlcs, commitment_signed, .. } } => {
8261+ nodes[ 0 ] . node. handle_update_fail_htlc( & nodes[ 1 ] . node. get_our_node_id( ) , & update_fail_htlcs[ 0 ] ) ;
8262+ commitment_signed_dance!( nodes[ 0 ] , nodes[ 1 ] , commitment_signed, false ) ;
8263+ } ,
8264+ _ => panic!( "Unexpected event" ) ,
8265+ }
8266+ }
8267+ }
8268+
8269+ let expected_error_code = 0x4000 |15 ; // incorrect_or_unknown_payment_details
8270+ // Error data is the HTLC value (100,000) and current block height
8271+ let expected_error_data = [ 0 , 0 , 0 , 0 , 0 , 1 , 0x86 , 0xa0 , 0 , 0 , 0 , CHAN_CONFIRM_DEPTH as u8 ] ;
8272+
8273+ // Send a payment with the right payment hash but the wrong payment secret
8274+ nodes[ 0 ] . node . send_payment ( & route, our_payment_hash, & Some ( random_payment_secret) ) . unwrap ( ) ;
8275+ handle_unknown_invalid_payment_data ! ( ) ;
8276+ expect_payment_failed ! ( nodes[ 0 ] , our_payment_hash, true , expected_error_code, expected_error_data) ;
8277+
8278+ // Send a payment with a random payment hash, but the right payment secret
8279+ nodes[ 0 ] . node . send_payment ( & route, random_payment_hash, & Some ( our_payment_secret) ) . unwrap ( ) ;
8280+ handle_unknown_invalid_payment_data ! ( ) ;
8281+ expect_payment_failed ! ( nodes[ 0 ] , random_payment_hash, true , expected_error_code, expected_error_data) ;
8282+
8283+ // Send a payment with a random payment hash and random payment secret
8284+ nodes[ 0 ] . node . send_payment ( & route, random_payment_hash, & Some ( random_payment_secret) ) . unwrap ( ) ;
8285+ handle_unknown_invalid_payment_data ! ( ) ;
8286+ expect_payment_failed ! ( nodes[ 0 ] , random_payment_hash, true , expected_error_code, expected_error_data) ;
8287+ }
8288+
81318289#[ test]
81328290fn test_update_err_monitor_lockdown ( ) {
81338291 // Our monitor will lock update of local commitment transaction if a broadcastion condition
0 commit comments