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