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