@@ -8503,3 +8503,185 @@ fn test_htlc_no_detection() {
85038503 connect_blocks ( & nodes[ 0 ] , ANTI_REORG_DELAY - 1 , 201 , true , header_201. block_hash ( ) ) ;
85048504 expect_payment_failed ! ( nodes[ 0 ] , our_payment_hash, true ) ;
85058505}
8506+
8507+ fn do_test_onchain_htlc_settlement_after_close ( broadcast_alice : bool , go_onchain_before_fulfill : bool ) {
8508+ // If we route an HTLC, then learn the HTLC's preimage after the upstream channel has been
8509+ // force-closed, we must claim that HTLC on-chain. (Given an HTLC forwarded from Alice --> Bob -->
8510+ // Carol, Alice would be the upstream node, and Carol the downstream.)
8511+ //
8512+ // Steps of the test:
8513+ // 1) Alice sends a HTLC to Carol through Bob.
8514+ // 2) Carol doesn't settle the HTLC.
8515+ // 3) If broadcast_alice is true, Alice force-closes her channel with Bob. Else Bob force closes.
8516+ // Steps 4 and 5 may be reordered depending on go_onchain_before_fulfill.
8517+ // 4) Bob sees the Alice's commitment on his chain or vice versa. An offered output is present
8518+ // but can't be claimed as Bob doesn't have yet knowledge of the preimage.
8519+ // 5) Carol release the preimage to Bob off-chain.
8520+ // 6) Bob claims the offered output on the broadcasted commitment.
8521+ let chanmon_cfgs = create_chanmon_cfgs ( 3 ) ;
8522+ let node_cfgs = create_node_cfgs ( 3 , & chanmon_cfgs) ;
8523+ let node_chanmgrs = create_node_chanmgrs ( 3 , & node_cfgs, & [ None , None , None ] ) ;
8524+ let nodes = create_network ( 3 , & node_cfgs, & node_chanmgrs) ;
8525+
8526+ // Create some initial channels
8527+ let chan_ab = create_announced_chan_between_nodes_with_value ( & nodes, 0 , 1 , 100000 , 10001 , InitFeatures :: known ( ) , InitFeatures :: known ( ) ) ;
8528+ create_announced_chan_between_nodes_with_value ( & nodes, 1 , 2 , 100000 , 10001 , InitFeatures :: known ( ) , InitFeatures :: known ( ) ) ;
8529+
8530+ // Steps (1) and (2):
8531+ // Send an HTLC Alice --> Bob --> Carol, but Carol doesn't settle the HTLC back.
8532+ let ( payment_preimage, _payment_hash) = route_payment ( & nodes[ 0 ] , & vec ! ( & nodes[ 1 ] , & nodes[ 2 ] ) , 3_000_000 ) ;
8533+
8534+ // Check that Alice's commitment transaction now contains an output for this HTLC.
8535+ let alice_txn = get_local_commitment_txn ! ( nodes[ 0 ] , chan_ab. 2 ) ;
8536+ check_spends ! ( alice_txn[ 0 ] , chan_ab. 3 ) ;
8537+ assert_eq ! ( alice_txn[ 0 ] . output. len( ) , 2 ) ;
8538+ check_spends ! ( alice_txn[ 1 ] , alice_txn[ 0 ] ) ; // 2nd transaction is a non-final HTLC-timeout
8539+ assert_eq ! ( alice_txn[ 1 ] . input[ 0 ] . witness. last( ) . unwrap( ) . len( ) , OFFERED_HTLC_SCRIPT_WEIGHT ) ;
8540+ assert_eq ! ( alice_txn. len( ) , 2 ) ;
8541+
8542+ // Steps (3) and (4):
8543+ // If `go_onchain_before_fufill`, broadcast the relevant commitment transaction and check that Bob
8544+ // responds by (1) broadcasting a channel update and (2) adding a new ChannelMonitor.
8545+ let mut force_closing_node = 0 ; // Alice force-closes
8546+ if !broadcast_alice { force_closing_node = 1 ; } // Bob force-closes
8547+ nodes[ force_closing_node] . node . force_close_channel ( & chan_ab. 2 ) ;
8548+ check_closed_broadcast ! ( nodes[ force_closing_node] , false ) ;
8549+ check_added_monitors ! ( nodes[ force_closing_node] , 1 ) ;
8550+ if go_onchain_before_fulfill {
8551+ let txn_to_broadcast = match broadcast_alice {
8552+ true => alice_txn. clone ( ) ,
8553+ false => get_local_commitment_txn ! ( nodes[ 1 ] , chan_ab. 2 )
8554+ } ;
8555+ let header = BlockHeader { version : 0x20000000 , prev_blockhash : Default :: default ( ) , merkle_root : Default :: default ( ) , time : 42 , bits : 42 , nonce : 42 } ;
8556+ connect_block ( & nodes[ 1 ] , & Block { header, txdata : vec ! [ txn_to_broadcast[ 0 ] . clone( ) ] } , 1 ) ;
8557+ let mut bob_txn = nodes[ 1 ] . tx_broadcaster . txn_broadcasted . lock ( ) . unwrap ( ) ;
8558+ if broadcast_alice {
8559+ check_closed_broadcast ! ( nodes[ 1 ] , false ) ;
8560+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
8561+ }
8562+ assert_eq ! ( bob_txn. len( ) , 1 ) ;
8563+ check_spends ! ( bob_txn[ 0 ] , chan_ab. 3 ) ;
8564+ }
8565+
8566+ // Step (5):
8567+ // Carol then claims the funds and sends an update_fulfill message to Bob, and they go through the
8568+ // process of removing the HTLC from their commitment transactions.
8569+ assert ! ( nodes[ 2 ] . node. claim_funds( payment_preimage, & None , 3_000_000 ) ) ;
8570+ check_added_monitors ! ( nodes[ 2 ] , 1 ) ;
8571+ let carol_updates = get_htlc_update_msgs ! ( nodes[ 2 ] , nodes[ 1 ] . node. get_our_node_id( ) ) ;
8572+ assert ! ( carol_updates. update_add_htlcs. is_empty( ) ) ;
8573+ assert ! ( carol_updates. update_fail_htlcs. is_empty( ) ) ;
8574+ assert ! ( carol_updates. update_fail_malformed_htlcs. is_empty( ) ) ;
8575+ assert ! ( carol_updates. update_fee. is_none( ) ) ;
8576+ assert_eq ! ( carol_updates. update_fulfill_htlcs. len( ) , 1 ) ;
8577+
8578+ nodes[ 1 ] . node . handle_update_fulfill_htlc ( & nodes[ 2 ] . node . get_our_node_id ( ) , & carol_updates. update_fulfill_htlcs [ 0 ] ) ;
8579+ // If Alice broadcasted but Bob doesn't know yet, here he prepares to tell her about the preimage.
8580+ if !go_onchain_before_fulfill && broadcast_alice {
8581+ let events = nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
8582+ assert_eq ! ( events. len( ) , 1 ) ;
8583+ match events[ 0 ] {
8584+ MessageSendEvent :: UpdateHTLCs { .. } => { } ,
8585+ _ => panic ! ( "Unexpected event" ) ,
8586+ } ;
8587+ }
8588+ nodes[ 1 ] . node . handle_commitment_signed ( & nodes[ 2 ] . node . get_our_node_id ( ) , & carol_updates. commitment_signed ) ;
8589+ // One monitor update for the preimage to update the Bob<->Alice channel, one monitor update
8590+ // Carol<->Bob's updated commitment transaction info.
8591+ check_added_monitors ! ( nodes[ 1 ] , 2 ) ;
8592+
8593+ let events = nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
8594+ assert_eq ! ( events. len( ) , 2 ) ;
8595+ let bob_revocation = match events[ 0 ] {
8596+ MessageSendEvent :: SendRevokeAndACK { ref node_id, ref msg } => {
8597+ assert_eq ! ( * node_id, nodes[ 2 ] . node. get_our_node_id( ) ) ;
8598+ ( * msg) . clone ( )
8599+ } ,
8600+ _ => panic ! ( "Unexpected event" ) ,
8601+ } ;
8602+ let bob_updates = match events[ 1 ] {
8603+ MessageSendEvent :: UpdateHTLCs { ref node_id, ref updates } => {
8604+ assert_eq ! ( * node_id, nodes[ 2 ] . node. get_our_node_id( ) ) ;
8605+ ( * updates) . clone ( )
8606+ } ,
8607+ _ => panic ! ( "Unexpected event" ) ,
8608+ } ;
8609+
8610+ nodes[ 2 ] . node . handle_revoke_and_ack ( & nodes[ 1 ] . node . get_our_node_id ( ) , & bob_revocation) ;
8611+ check_added_monitors ! ( nodes[ 2 ] , 1 ) ;
8612+ nodes[ 2 ] . node . handle_commitment_signed ( & nodes[ 1 ] . node . get_our_node_id ( ) , & bob_updates. commitment_signed ) ;
8613+ check_added_monitors ! ( nodes[ 2 ] , 1 ) ;
8614+
8615+ let events = nodes[ 2 ] . node . get_and_clear_pending_msg_events ( ) ;
8616+ assert_eq ! ( events. len( ) , 1 ) ;
8617+ let carol_revocation = match events[ 0 ] {
8618+ MessageSendEvent :: SendRevokeAndACK { ref node_id, ref msg } => {
8619+ assert_eq ! ( * node_id, nodes[ 1 ] . node. get_our_node_id( ) ) ;
8620+ ( * msg) . clone ( )
8621+ } ,
8622+ _ => panic ! ( "Unexpected event" ) ,
8623+ } ;
8624+ nodes[ 1 ] . node . handle_revoke_and_ack ( & nodes[ 2 ] . node . get_our_node_id ( ) , & carol_revocation) ;
8625+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
8626+
8627+ // If this test requires the force-closed channel to not be on-chain until after the fulfill,
8628+ // here's where we put said channel's commitment tx on-chain.
8629+ let mut txn_to_broadcast = alice_txn. clone ( ) ;
8630+ if !broadcast_alice { txn_to_broadcast = get_local_commitment_txn ! ( nodes[ 1 ] , chan_ab. 2 ) ; }
8631+ if !go_onchain_before_fulfill {
8632+ let header = BlockHeader { version : 0x20000000 , prev_blockhash : Default :: default ( ) , merkle_root : Default :: default ( ) , time : 42 , bits : 42 , nonce : 42 } ;
8633+ connect_block ( & nodes[ 1 ] , & Block { header, txdata : vec ! [ txn_to_broadcast[ 0 ] . clone( ) ] } , 1 ) ;
8634+ // If Bob was the one to force-close, he will have already passed these checks earlier.
8635+ if broadcast_alice {
8636+ check_closed_broadcast ! ( nodes[ 1 ] , false ) ;
8637+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
8638+ }
8639+ let mut bob_txn = nodes[ 1 ] . tx_broadcaster . txn_broadcasted . lock ( ) . unwrap ( ) ;
8640+ if broadcast_alice {
8641+ // In `connect_block()`, the ChainMonitor and ChannelManager are separately notified about a
8642+ // new block being connected. The ChannelManager being notified triggers a monitor update,
8643+ // which triggers broadcasting our commitment tx and an HTLC-claiming tx. The ChainMonitor
8644+ // being notified triggers the HTLC-claiming tx redundantly, resulting in 3 total txs being
8645+ // broadcasted.
8646+ assert_eq ! ( bob_txn. len( ) , 3 ) ;
8647+ check_spends ! ( bob_txn[ 1 ] , chan_ab. 3 ) ;
8648+ } else {
8649+ assert_eq ! ( bob_txn. len( ) , 2 ) ;
8650+ check_spends ! ( bob_txn[ 0 ] , chan_ab. 3 ) ;
8651+ }
8652+ }
8653+
8654+ // Step (6):
8655+ // Finally, check that Bob broadcasted a preimage-claiming transaction for the HTLC output on the
8656+ // broadcasted commitment transaction.
8657+ {
8658+ let bob_txn = nodes[ 1 ] . tx_broadcaster . txn_broadcasted . lock ( ) . unwrap ( ) . clone ( ) ;
8659+ if go_onchain_before_fulfill {
8660+ // Bob should now have an extra broadcasted tx, for the preimage-claiming transaction.
8661+ assert_eq ! ( bob_txn. len( ) , 2 ) ;
8662+ }
8663+ let script_weight = match broadcast_alice {
8664+ true => OFFERED_HTLC_SCRIPT_WEIGHT ,
8665+ false => ACCEPTED_HTLC_SCRIPT_WEIGHT
8666+ } ;
8667+ // If Alice force-closed and Bob didn't receive her commitment transaction until after he
8668+ // received Carol's fulfill, he broadcasts the HTLC-output-claiming transaction first. Else if
8669+ // Bob force closed or if he found out about Alice's commitment tx before receiving Carol's
8670+ // fulfill, then he broadcasts the HTLC-output-claiming transaction second.
8671+ if broadcast_alice && !go_onchain_before_fulfill {
8672+ check_spends ! ( bob_txn[ 0 ] , txn_to_broadcast[ 0 ] ) ;
8673+ assert_eq ! ( bob_txn[ 0 ] . input[ 0 ] . witness. last( ) . unwrap( ) . len( ) , script_weight) ;
8674+ } else {
8675+ check_spends ! ( bob_txn[ 1 ] , txn_to_broadcast[ 0 ] ) ;
8676+ assert_eq ! ( bob_txn[ 1 ] . input[ 0 ] . witness. last( ) . unwrap( ) . len( ) , script_weight) ;
8677+ }
8678+ }
8679+ }
8680+
8681+ #[ test]
8682+ fn test_onchain_htlc_settlement_after_close1 ( ) {
8683+ do_test_onchain_htlc_settlement_after_close ( true , true ) ;
8684+ do_test_onchain_htlc_settlement_after_close ( false , true ) ;
8685+ do_test_onchain_htlc_settlement_after_close ( true , false ) ;
8686+ do_test_onchain_htlc_settlement_after_close ( false , false ) ;
8687+ }
0 commit comments