@@ -8503,3 +8503,119 @@ 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
+ #[ test]
8508
+ fn test_unordered_onchain_htlc_settlement ( ) {
8509
+ // If we route an HTLC, then learn the HTLC's preimage after the upstream
8510
+ // channel has been force-closed by our counterparty, we must claim that HTLC
8511
+ // on-chain. (Given an HTLC forwarded from Alice --> Bob --> Carol, Alice
8512
+ // would be the upstream node, and Carol the downstream.)
8513
+ //
8514
+ // Steps of the test:
8515
+ // 1) Alice sends a HTLC to Carol through Bob.
8516
+ // 2) Carol doesn't settle the HTLC.
8517
+ // 3) Alice force-closes her channel with Bob.
8518
+ // 4) Bob sees the Alice's commitment on his chain. An offered output is present but can't
8519
+ // be claimed as Bob doesn't have yet knowledge of the preimage.
8520
+ // 5) Carol release the preimage to Bob off-chain.
8521
+ // 6) Bob claims the offered output on Alice's commitment.
8522
+ // TODO: reverse the test with Bob's commitment reaching the chain instead of Alice's one.
8523
+
8524
+ let chanmon_cfgs = create_chanmon_cfgs ( 3 ) ;
8525
+ let node_cfgs = create_node_cfgs ( 3 , & chanmon_cfgs) ;
8526
+ let node_chanmgrs = create_node_chanmgrs ( 3 , & node_cfgs, & [ None , None , None ] ) ;
8527
+ let nodes = create_network ( 3 , & node_cfgs, & node_chanmgrs) ;
8528
+
8529
+ // Create some initial channels
8530
+ let chan_ab = create_announced_chan_between_nodes_with_value ( & nodes, 0 , 1 , 100000 , 10001 , InitFeatures :: known ( ) , InitFeatures :: known ( ) ) ;
8531
+ create_announced_chan_between_nodes_with_value ( & nodes, 1 , 2 , 100000 , 10001 , InitFeatures :: known ( ) , InitFeatures :: known ( ) ) ;
8532
+
8533
+ // Steps (1) and (2):
8534
+ // Send an HTLC Alice --> Bob --> Carol, but Carol doesn't settle the HTLC back.
8535
+ let ( payment_preimage, _payment_hash) = route_payment ( & nodes[ 0 ] , & vec ! ( & nodes[ 1 ] , & nodes[ 2 ] ) , 3_000_000 ) ;
8536
+
8537
+ // Check that Alice's commitment transaction now contains an output for this HTLC.
8538
+ let alice_txn = get_local_commitment_txn ! ( nodes[ 0 ] , chan_ab. 2 ) ;
8539
+ check_spends ! ( alice_txn[ 0 ] , chan_ab. 3 ) ;
8540
+ assert_eq ! ( alice_txn[ 0 ] . output. len( ) , 2 ) ;
8541
+ check_spends ! ( alice_txn[ 1 ] , alice_txn[ 0 ] ) ; // 2nd transaction is a non-final HTLC-timeout
8542
+ assert_eq ! ( alice_txn[ 1 ] . input[ 0 ] . witness. last( ) . unwrap( ) . len( ) , OFFERED_HTLC_SCRIPT_WEIGHT ) ;
8543
+ assert_eq ! ( alice_txn. len( ) , 2 ) ;
8544
+
8545
+ // Steps (3) and (4):
8546
+ // Broadcast Alice's commitment transaction and check that Bob responds by (1) broadcasting
8547
+ // a channel update and (2) adding a new ChannelMonitor.
8548
+ let header = BlockHeader { version : 0x20000000 , prev_blockhash : Default :: default ( ) , merkle_root : Default :: default ( ) , time : 42 , bits : 42 , nonce : 42 } ;
8549
+ connect_block ( & nodes[ 1 ] , & Block { header, txdata : vec ! [ alice_txn[ 0 ] . clone( ) ] } , 1 ) ;
8550
+ check_closed_broadcast ! ( nodes[ 1 ] , false ) ;
8551
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
8552
+ {
8553
+ let mut bob_txn = nodes[ 1 ] . tx_broadcaster . txn_broadcasted . lock ( ) . unwrap ( ) ; // ChannelManager : 1 (commitment tx)
8554
+ assert_eq ! ( bob_txn. len( ) , 1 ) ;
8555
+ check_spends ! ( bob_txn[ 0 ] , chan_ab. 3 ) ;
8556
+ bob_txn. clear ( ) ;
8557
+ }
8558
+
8559
+ // Step (5):
8560
+ // Carol then claims the funds and sends an update_fulfill message to Bob, and they go
8561
+ // through the process of removing the HTLC from their commitment transactions.
8562
+ assert ! ( nodes[ 2 ] . node. claim_funds( payment_preimage, & None , 3_000_000 ) ) ;
8563
+ check_added_monitors ! ( nodes[ 2 ] , 1 ) ;
8564
+ let carol_updates = get_htlc_update_msgs ! ( nodes[ 2 ] , nodes[ 1 ] . node. get_our_node_id( ) ) ;
8565
+ assert ! ( carol_updates. update_add_htlcs. is_empty( ) ) ;
8566
+ assert ! ( carol_updates. update_fail_htlcs. is_empty( ) ) ;
8567
+ assert ! ( carol_updates. update_fail_malformed_htlcs. is_empty( ) ) ;
8568
+ assert ! ( carol_updates. update_fee. is_none( ) ) ;
8569
+ assert_eq ! ( carol_updates. update_fulfill_htlcs. len( ) , 1 ) ;
8570
+
8571
+ nodes[ 1 ] . node . handle_update_fulfill_htlc ( & nodes[ 2 ] . node . get_our_node_id ( ) , & carol_updates. update_fulfill_htlcs [ 0 ] ) ;
8572
+ nodes[ 1 ] . node . handle_commitment_signed ( & nodes[ 2 ] . node . get_our_node_id ( ) , & carol_updates. commitment_signed ) ;
8573
+ // One monitor update for the preimage, one monitor update for the new commitment tx info
8574
+ check_added_monitors ! ( nodes[ 1 ] , 2 ) ;
8575
+
8576
+ let events = nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
8577
+ assert_eq ! ( events. len( ) , 2 ) ;
8578
+ let bob_revocation = match events[ 0 ] {
8579
+ MessageSendEvent :: SendRevokeAndACK { ref node_id, ref msg } => {
8580
+ assert_eq ! ( * node_id, nodes[ 2 ] . node. get_our_node_id( ) ) ;
8581
+ ( * msg) . clone ( )
8582
+ } ,
8583
+ _ => panic ! ( "Unexpected event" ) ,
8584
+ } ;
8585
+ let bob_updates = match events[ 1 ] {
8586
+ MessageSendEvent :: UpdateHTLCs { ref node_id, ref updates } => {
8587
+ assert_eq ! ( * node_id, nodes[ 2 ] . node. get_our_node_id( ) ) ;
8588
+ ( * updates) . clone ( )
8589
+ } ,
8590
+ _ => panic ! ( "Unexpected event" ) ,
8591
+ } ;
8592
+
8593
+ nodes[ 2 ] . node . handle_revoke_and_ack ( & nodes[ 1 ] . node . get_our_node_id ( ) , & bob_revocation) ;
8594
+ check_added_monitors ! ( nodes[ 2 ] , 1 ) ;
8595
+ nodes[ 2 ] . node . handle_commitment_signed ( & nodes[ 1 ] . node . get_our_node_id ( ) , & bob_updates. commitment_signed ) ;
8596
+ check_added_monitors ! ( nodes[ 2 ] , 1 ) ;
8597
+
8598
+ let events = nodes[ 2 ] . node . get_and_clear_pending_msg_events ( ) ;
8599
+ assert_eq ! ( events. len( ) , 1 ) ;
8600
+ let carol_revocation = match events[ 0 ] {
8601
+ MessageSendEvent :: SendRevokeAndACK { ref node_id, ref msg } => {
8602
+ assert_eq ! ( * node_id, nodes[ 1 ] . node. get_our_node_id( ) ) ;
8603
+ ( * msg) . clone ( )
8604
+ } ,
8605
+ _ => panic ! ( "Unexpected event" ) ,
8606
+ } ;
8607
+ nodes[ 1 ] . node . handle_revoke_and_ack ( & nodes[ 2 ] . node . get_our_node_id ( ) , & carol_revocation) ;
8608
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
8609
+
8610
+ let header = BlockHeader { version : 0x20000000 , prev_blockhash : Default :: default ( ) , merkle_root : Default :: default ( ) , time : 42 , bits : 42 , nonce : 42 } ;
8611
+ connect_block ( & nodes[ 1 ] , & Block { header, txdata : vec ! [ ] } , 1 ) ;
8612
+ // Step (6):
8613
+ // Finally, check that Bob broadcasted a preimage-claiming transaction for the HTLC
8614
+ // output on Alice's broadcasted commitment transaction.
8615
+ {
8616
+ let bob_txn = nodes[ 1 ] . tx_broadcaster . txn_broadcasted . lock ( ) . unwrap ( ) . clone ( ) ; // ChannelMonitor : 1 (htlc-preimage tx)
8617
+ assert_eq ! ( bob_txn. len( ) , 1 ) ;
8618
+ check_spends ! ( bob_txn[ 0 ] , alice_txn[ 0 ] ) ;
8619
+ assert_eq ! ( bob_txn[ 0 ] . input[ 0 ] . witness. last( ) . unwrap( ) . len( ) , OFFERED_HTLC_SCRIPT_WEIGHT ) ;
8620
+ }
8621
+ }
0 commit comments