@@ -8503,3 +8503,187 @@ fn test_htlc_no_detection() {
8503
8503
connect_blocks ( & nodes[ 0 ] , ANTI_REORG_DELAY - 1 , 201 , true , header_201. block_hash ( ) ) ;
8504
8504
expect_payment_failed ! ( nodes[ 0 ] , our_payment_hash, true ) ;
8505
8505
}
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 { ref node_id, .. } => {
8585
+ assert_eq ! ( * node_id, nodes[ 0 ] . node. get_our_node_id( ) ) ;
8586
+ } ,
8587
+ _ => panic ! ( "Unexpected event" ) ,
8588
+ } ;
8589
+ }
8590
+ nodes[ 1 ] . node . handle_commitment_signed ( & nodes[ 2 ] . node . get_our_node_id ( ) , & carol_updates. commitment_signed ) ;
8591
+ // One monitor update for the preimage to update the Bob<->Alice channel, one monitor update
8592
+ // Carol<->Bob's updated commitment transaction info.
8593
+ check_added_monitors ! ( nodes[ 1 ] , 2 ) ;
8594
+
8595
+ let events = nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
8596
+ assert_eq ! ( events. len( ) , 2 ) ;
8597
+ let bob_revocation = match events[ 0 ] {
8598
+ MessageSendEvent :: SendRevokeAndACK { ref node_id, ref msg } => {
8599
+ assert_eq ! ( * node_id, nodes[ 2 ] . node. get_our_node_id( ) ) ;
8600
+ ( * msg) . clone ( )
8601
+ } ,
8602
+ _ => panic ! ( "Unexpected event" ) ,
8603
+ } ;
8604
+ let bob_updates = match events[ 1 ] {
8605
+ MessageSendEvent :: UpdateHTLCs { ref node_id, ref updates } => {
8606
+ assert_eq ! ( * node_id, nodes[ 2 ] . node. get_our_node_id( ) ) ;
8607
+ ( * updates) . clone ( )
8608
+ } ,
8609
+ _ => panic ! ( "Unexpected event" ) ,
8610
+ } ;
8611
+
8612
+ nodes[ 2 ] . node . handle_revoke_and_ack ( & nodes[ 1 ] . node . get_our_node_id ( ) , & bob_revocation) ;
8613
+ check_added_monitors ! ( nodes[ 2 ] , 1 ) ;
8614
+ nodes[ 2 ] . node . handle_commitment_signed ( & nodes[ 1 ] . node . get_our_node_id ( ) , & bob_updates. commitment_signed ) ;
8615
+ check_added_monitors ! ( nodes[ 2 ] , 1 ) ;
8616
+
8617
+ let events = nodes[ 2 ] . node . get_and_clear_pending_msg_events ( ) ;
8618
+ assert_eq ! ( events. len( ) , 1 ) ;
8619
+ let carol_revocation = match events[ 0 ] {
8620
+ MessageSendEvent :: SendRevokeAndACK { ref node_id, ref msg } => {
8621
+ assert_eq ! ( * node_id, nodes[ 1 ] . node. get_our_node_id( ) ) ;
8622
+ ( * msg) . clone ( )
8623
+ } ,
8624
+ _ => panic ! ( "Unexpected event" ) ,
8625
+ } ;
8626
+ nodes[ 1 ] . node . handle_revoke_and_ack ( & nodes[ 2 ] . node . get_our_node_id ( ) , & carol_revocation) ;
8627
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
8628
+
8629
+ // If this test requires the force-closed channel to not be on-chain until after the fulfill,
8630
+ // here's where we put said channel's commitment tx on-chain.
8631
+ let mut txn_to_broadcast = alice_txn. clone ( ) ;
8632
+ if !broadcast_alice { txn_to_broadcast = get_local_commitment_txn ! ( nodes[ 1 ] , chan_ab. 2 ) ; }
8633
+ if !go_onchain_before_fulfill {
8634
+ let header = BlockHeader { version : 0x20000000 , prev_blockhash : Default :: default ( ) , merkle_root : Default :: default ( ) , time : 42 , bits : 42 , nonce : 42 } ;
8635
+ connect_block ( & nodes[ 1 ] , & Block { header, txdata : vec ! [ txn_to_broadcast[ 0 ] . clone( ) ] } , 1 ) ;
8636
+ // If Bob was the one to force-close, he will have already passed these checks earlier.
8637
+ if broadcast_alice {
8638
+ check_closed_broadcast ! ( nodes[ 1 ] , false ) ;
8639
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
8640
+ }
8641
+ let mut bob_txn = nodes[ 1 ] . tx_broadcaster . txn_broadcasted . lock ( ) . unwrap ( ) ;
8642
+ if broadcast_alice {
8643
+ // In `connect_block()`, the ChainMonitor and ChannelManager are separately notified about a
8644
+ // new block being connected. The ChannelManager being notified triggers a monitor update,
8645
+ // which triggers broadcasting our commitment tx and an HTLC-claiming tx. The ChainMonitor
8646
+ // being notified triggers the HTLC-claiming tx redundantly, resulting in 3 total txs being
8647
+ // broadcasted.
8648
+ assert_eq ! ( bob_txn. len( ) , 3 ) ;
8649
+ check_spends ! ( bob_txn[ 1 ] , chan_ab. 3 ) ;
8650
+ } else {
8651
+ assert_eq ! ( bob_txn. len( ) , 2 ) ;
8652
+ check_spends ! ( bob_txn[ 0 ] , chan_ab. 3 ) ;
8653
+ }
8654
+ }
8655
+
8656
+ // Step (6):
8657
+ // Finally, check that Bob broadcasted a preimage-claiming transaction for the HTLC output on the
8658
+ // broadcasted commitment transaction.
8659
+ {
8660
+ let bob_txn = nodes[ 1 ] . tx_broadcaster . txn_broadcasted . lock ( ) . unwrap ( ) . clone ( ) ;
8661
+ if go_onchain_before_fulfill {
8662
+ // Bob should now have an extra broadcasted tx, for the preimage-claiming transaction.
8663
+ assert_eq ! ( bob_txn. len( ) , 2 ) ;
8664
+ }
8665
+ let script_weight = match broadcast_alice {
8666
+ true => OFFERED_HTLC_SCRIPT_WEIGHT ,
8667
+ false => ACCEPTED_HTLC_SCRIPT_WEIGHT
8668
+ } ;
8669
+ // If Alice force-closed and Bob didn't receive her commitment transaction until after he
8670
+ // received Carol's fulfill, he broadcasts the HTLC-output-claiming transaction first. Else if
8671
+ // Bob force closed or if he found out about Alice's commitment tx before receiving Carol's
8672
+ // fulfill, then he broadcasts the HTLC-output-claiming transaction second.
8673
+ if broadcast_alice && !go_onchain_before_fulfill {
8674
+ check_spends ! ( bob_txn[ 0 ] , txn_to_broadcast[ 0 ] ) ;
8675
+ assert_eq ! ( bob_txn[ 0 ] . input[ 0 ] . witness. last( ) . unwrap( ) . len( ) , script_weight) ;
8676
+ } else {
8677
+ check_spends ! ( bob_txn[ 1 ] , txn_to_broadcast[ 0 ] ) ;
8678
+ assert_eq ! ( bob_txn[ 1 ] . input[ 0 ] . witness. last( ) . unwrap( ) . len( ) , script_weight) ;
8679
+ }
8680
+ }
8681
+ }
8682
+
8683
+ #[ test]
8684
+ fn test_onchain_htlc_settlement_after_close ( ) {
8685
+ do_test_onchain_htlc_settlement_after_close ( true , true ) ;
8686
+ do_test_onchain_htlc_settlement_after_close ( false , true ) ; // Technically redundant, but may as well
8687
+ do_test_onchain_htlc_settlement_after_close ( true , false ) ;
8688
+ do_test_onchain_htlc_settlement_after_close ( false , false ) ;
8689
+ }
0 commit comments