@@ -6159,7 +6159,7 @@ mod tests {
6159
6159
_ => panic ! ( "Unexpected event" ) ,
6160
6160
} ;
6161
6161
{
6162
- let mut node_txn = nodes[ 1 ] . tx_broadcaster . txn_broadcasted . lock ( ) . unwrap ( ) ; ; // ChannelManager : 2 (commitment tx, HTLC-Timeout tx), ChannelMonitor : 1 (timeout tx) * 2 (block-rescan)
6162
+ let mut node_txn = nodes[ 1 ] . tx_broadcaster . txn_broadcasted . lock ( ) . unwrap ( ) ; // ChannelManager : 2 (commitment tx, HTLC-Timeout tx), ChannelMonitor : 1 (timeout tx) * 2 (block-rescan)
6163
6163
assert_eq ! ( node_txn. len( ) , 4 ) ;
6164
6164
assert_eq ! ( node_txn[ 0 ] , node_txn[ 3 ] ) ;
6165
6165
check_spends ! ( node_txn[ 0 ] , commitment_tx[ 0 ] . clone( ) ) ;
@@ -6170,7 +6170,7 @@ mod tests {
6170
6170
check_spends ! ( node_txn[ 2 ] , node_txn[ 1 ] . clone( ) ) ;
6171
6171
assert_eq ! ( node_txn[ 1 ] . input[ 0 ] . witness. clone( ) . last( ) . unwrap( ) . len( ) , 71 ) ;
6172
6172
assert_eq ! ( node_txn[ 2 ] . input[ 0 ] . witness. clone( ) . last( ) . unwrap( ) . len( ) , OFFERED_HTLC_SCRIPT_WEIGHT ) ;
6173
- node_txn. clear ( )
6173
+ node_txn. clear ( ) ;
6174
6174
}
6175
6175
6176
6176
// Broadcast legit commitment tx from A on B's chain
@@ -8348,6 +8348,71 @@ mod tests {
8348
8348
check_spends ! ( spend_txn[ 4 ] , node_txn[ 3 ] . clone( ) ) ; // spending justice tx output on htlc success tx
8349
8349
}
8350
8350
8351
+ #[ test]
8352
+ fn test_onchain_to_onchain_claim ( ) {
8353
+ // Test that in case of channel closure, we detect the state of output thanks to
8354
+ // ChainWatchInterface and claim HTLC on downstream peer's remote commitment tx.
8355
+
8356
+ let nodes = create_network ( 3 ) ;
8357
+
8358
+ // Create some initial channels
8359
+ let chan_1 = create_announced_chan_between_nodes ( & nodes, 0 , 1 ) ;
8360
+ let chan_2 = create_announced_chan_between_nodes ( & nodes, 1 , 2 ) ;
8361
+
8362
+ // Rebalance the network a bit by relaying one payment through all the channels ...
8363
+ send_payment ( & nodes[ 0 ] , & vec ! ( & nodes[ 1 ] , & nodes[ 2 ] ) [ ..] , 8000000 ) ;
8364
+ send_payment ( & nodes[ 0 ] , & vec ! ( & nodes[ 1 ] , & nodes[ 2 ] ) [ ..] , 8000000 ) ;
8365
+
8366
+ let ( payment_preimage, _payment_hash) = route_payment ( & nodes[ 0 ] , & vec ! ( & nodes[ 1 ] , & nodes[ 2 ] ) , 3000000 ) ;
8367
+ let header = BlockHeader { version : 0x20000000 , prev_blockhash : Default :: default ( ) , merkle_root : Default :: default ( ) , time : 42 , bits : 42 , nonce : 42 } ;
8368
+ let commitment_tx = nodes[ 2 ] . node . channel_state . lock ( ) . unwrap ( ) . by_id . get ( & chan_2. 2 ) . unwrap ( ) . last_local_commitment_txn . clone ( ) ;
8369
+ check_spends ! ( commitment_tx[ 0 ] , chan_2. 3 . clone( ) ) ;
8370
+ nodes[ 2 ] . node . claim_funds ( payment_preimage) ;
8371
+ {
8372
+ let mut added_monitors = nodes[ 2 ] . chan_monitor . added_monitors . lock ( ) . unwrap ( ) ;
8373
+ assert_eq ! ( added_monitors. len( ) , 1 ) ;
8374
+ added_monitors. clear ( ) ;
8375
+ }
8376
+ let events = nodes[ 2 ] . node . get_and_clear_pending_msg_events ( ) ;
8377
+ assert_eq ! ( events. len( ) , 1 ) ;
8378
+ match events[ 0 ] {
8379
+ MessageSendEvent :: UpdateHTLCs { ref node_id, updates : msgs:: CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, .. } } => {
8380
+ assert ! ( update_add_htlcs. is_empty( ) ) ;
8381
+ assert ! ( update_fail_htlcs. is_empty( ) ) ;
8382
+ assert ! ( !update_fulfill_htlcs. is_empty( ) ) ;
8383
+ assert ! ( update_fail_malformed_htlcs. is_empty( ) ) ;
8384
+ assert_eq ! ( nodes[ 1 ] . node. get_our_node_id( ) , * node_id) ;
8385
+ } ,
8386
+ _ => panic ! ( "Unexpected event" ) ,
8387
+ } ;
8388
+ nodes[ 2 ] . chain_monitor . block_connected_with_filtering ( & Block { header, txdata : vec ! [ commitment_tx[ 0 ] . clone( ) ] } , 1 ) ;
8389
+ let events = nodes[ 2 ] . node . get_and_clear_pending_msg_events ( ) ;
8390
+ assert_eq ! ( events. len( ) , 1 ) ;
8391
+ match events[ 0 ] {
8392
+ MessageSendEvent :: BroadcastChannelUpdate { .. } => { } ,
8393
+ _ => panic ! ( "Unexpected event" ) ,
8394
+ }
8395
+
8396
+ let node_txn = nodes[ 2 ] . tx_broadcaster . txn_broadcasted . lock ( ) . unwrap ( ) . clone ( ) ; // ChannelManager : 2 (commitment tx, HTLC-Success tx), ChannelMonitor : 1 (HTLC-Success tx)
8397
+ assert_eq ! ( node_txn. len( ) , 3 ) ;
8398
+ check_spends ! ( node_txn[ 0 ] , commitment_tx[ 0 ] . clone( ) ) ;
8399
+ assert_eq ! ( node_txn[ 0 ] . input[ 0 ] . witness. clone( ) . last( ) . unwrap( ) . len( ) , ACCEPTED_HTLC_SCRIPT_WEIGHT ) ;
8400
+ check_spends ! ( node_txn[ 1 ] , chan_2. 3 . clone( ) ) ;
8401
+ check_spends ! ( node_txn[ 2 ] , node_txn[ 1 ] . clone( ) ) ;
8402
+ assert_eq ! ( node_txn[ 1 ] . input[ 0 ] . witness. clone( ) . last( ) . unwrap( ) . len( ) , 71 ) ;
8403
+ assert_eq ! ( node_txn[ 2 ] . input[ 0 ] . witness. clone( ) . last( ) . unwrap( ) . len( ) , ACCEPTED_HTLC_SCRIPT_WEIGHT ) ;
8404
+
8405
+ let commitment_tx = nodes[ 0 ] . node . channel_state . lock ( ) . unwrap ( ) . by_id . get ( & chan_1. 2 ) . unwrap ( ) . last_local_commitment_txn . clone ( ) ;
8406
+ nodes[ 1 ] . chan_monitor . simple_monitor . block_connected ( & header, 1 , & [ & node_txn[ 0 ] , & commitment_tx[ 0 ] ] , & [ 0 ] ) ;
8407
+ let node_txn = nodes[ 1 ] . tx_broadcaster . txn_broadcasted . lock ( ) . unwrap ( ) ;
8408
+ check_spends ! ( node_txn[ 0 ] , commitment_tx[ 0 ] . clone( ) ) ;
8409
+ let spend_txn = check_spendable_outputs ! ( nodes[ 1 ] , 1 ) ;
8410
+ assert_eq ! ( spend_txn. len( ) , 3 ) ;
8411
+ assert_eq ! ( spend_txn[ 0 ] , spend_txn[ 1 ] ) ;
8412
+ check_spends ! ( spend_txn[ 0 ] , commitment_tx[ 0 ] . clone( ) ) ;
8413
+ check_spends ! ( spend_txn[ 2 ] , node_txn[ 0 ] . clone( ) ) ;
8414
+ }
8415
+
8351
8416
#[ test]
8352
8417
fn test_dynamic_spendable_outputs_local_htlc_success_tx ( ) {
8353
8418
let nodes = create_network ( 2 ) ;
0 commit comments