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