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