@@ -624,13 +624,7 @@ impl ChannelManager {
624
624
for tx in local_txn {
625
625
self . tx_broadcaster . broadcast_transaction ( & tx) ;
626
626
}
627
- //TODO: We need to have a way where outbound HTLC claims can result in us claiming the
628
- //now-on-chain HTLC output for ourselves (and, thereafter, passing the HTLC backwards).
629
- //TODO: We need to handle monitoring of pending offered HTLCs which just hit the chain and
630
- //may be claimed, resulting in us claiming the inbound HTLCs (and back-failing after
631
- //timeouts are hit and our claims confirm).
632
- //TODO: In any case, we need to make sure we remove any pending htlc tracking (via
633
- //fail_backwards or claim_funds) eventually for all HTLCs that were in the channel
627
+
634
628
}
635
629
636
630
/// Force closes a channel, immediately broadcasting the latest local commitment transaction to
@@ -2661,6 +2655,17 @@ impl ChainListener for ChannelManager {
2661
2655
for failure in failed_channels. drain ( ..) {
2662
2656
self . finish_force_close_channel ( failure) ;
2663
2657
}
2658
+ {
2659
+ let mut channel_state = Some ( self . channel_state . lock ( ) . unwrap ( ) ) ;
2660
+ for ( _, payment_preimage, htlc_source) in self . monitor . fetch_pending_htlc_updated ( ) {
2661
+ if let Some ( source) = htlc_source {
2662
+ if let Some ( preimage) = payment_preimage {
2663
+ if channel_state. is_none ( ) { channel_state = Some ( self . channel_state . lock ( ) . unwrap ( ) ) ; }
2664
+ self . claim_funds_internal ( channel_state. take ( ) . unwrap ( ) , source, preimage) ;
2665
+ }
2666
+ }
2667
+ }
2668
+ }
2664
2669
self . latest_block_height . store ( height as usize , Ordering :: Release ) ;
2665
2670
* self . last_block_hash . try_lock ( ) . expect ( "block_(dis)connected must not be called in parallel" ) = header. bitcoin_hash ( ) ;
2666
2671
}
@@ -6064,6 +6069,105 @@ mod tests {
6064
6069
assert_eq ! ( nodes[ 1 ] . node. list_channels( ) . len( ) , 0 ) ;
6065
6070
}
6066
6071
6072
+ #[ test]
6073
+ fn test_htlc_on_chain_success ( ) {
6074
+ // Test that in case of an unilateral close onchain, we detect the state of output thanks to
6075
+ // ChainWatchInterface and pass the preimage backward accordingly. So here we test that ChannelManager is
6076
+ // broadcasting the right event to other nodes in payment path.
6077
+ // A --------------------> B ----------------------> C (preimage)
6078
+ // A's commitment tx C's commitment tx
6079
+ // \ \
6080
+ // B's preimage tx C's HTLC Success tx
6081
+
6082
+ let nodes = create_network ( 3 ) ;
6083
+
6084
+ // Create some initial channels
6085
+ let chan_1 = create_announced_chan_between_nodes ( & nodes, 0 , 1 ) ;
6086
+ let chan_2 = create_announced_chan_between_nodes ( & nodes, 1 , 2 ) ;
6087
+
6088
+ // Rebalance the network a bit by relaying one payment through all the channels...
6089
+ send_payment ( & nodes[ 0 ] , & vec ! ( & nodes[ 1 ] , & nodes[ 2 ] ) [ ..] , 8000000 ) ;
6090
+ send_payment ( & nodes[ 0 ] , & vec ! ( & nodes[ 1 ] , & nodes[ 2 ] ) [ ..] , 8000000 ) ;
6091
+
6092
+ let ( payment_preimage, _payment_hash) = route_payment ( & nodes[ 0 ] , & vec ! ( & nodes[ 1 ] , & nodes[ 2 ] ) , 3000000 ) ;
6093
+ let header = BlockHeader { version : 0x20000000 , prev_blockhash : Default :: default ( ) , merkle_root : Default :: default ( ) , time : 42 , bits : 42 , nonce : 42 } ;
6094
+
6095
+ // Broadcast legit commitment tx from C on B's chain
6096
+ // Broadcast HTLC Success transation by C on received output from C's commitment tx on B's chain
6097
+ let commitment_tx = nodes[ 2 ] . node . channel_state . lock ( ) . unwrap ( ) . by_id . get ( & chan_2. 2 ) . unwrap ( ) . last_local_commitment_txn . clone ( ) ;
6098
+ nodes[ 2 ] . node . claim_funds ( payment_preimage) ;
6099
+ {
6100
+ let mut added_monitors = nodes[ 2 ] . chan_monitor . added_monitors . lock ( ) . unwrap ( ) ;
6101
+ assert_eq ! ( added_monitors. len( ) , 1 ) ;
6102
+ added_monitors. clear ( ) ;
6103
+ }
6104
+ let events = nodes[ 2 ] . node . get_and_clear_pending_msg_events ( ) ;
6105
+ assert_eq ! ( events. len( ) , 1 ) ;
6106
+ match events[ 0 ] {
6107
+ 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, .. } } => {
6108
+ assert ! ( update_add_htlcs. is_empty( ) ) ;
6109
+ assert ! ( update_fail_htlcs. is_empty( ) ) ;
6110
+ assert ! ( !update_fulfill_htlcs. is_empty( ) ) ;
6111
+ assert ! ( update_fail_malformed_htlcs. is_empty( ) ) ;
6112
+ assert_eq ! ( nodes[ 1 ] . node. get_our_node_id( ) , * node_id) ;
6113
+ } ,
6114
+ _ => panic ! ( "Unexpected event" ) ,
6115
+ } ;
6116
+ nodes[ 2 ] . chain_monitor . block_connected_with_filtering ( & Block { header, txdata : vec ! [ commitment_tx[ 0 ] . clone( ) ] } , 1 ) ;
6117
+ let events = nodes[ 2 ] . node . get_and_clear_pending_msg_events ( ) ;
6118
+ assert_eq ! ( events. len( ) , 1 ) ;
6119
+ match events[ 0 ] {
6120
+ MessageSendEvent :: BroadcastChannelUpdate { .. } => { } ,
6121
+ _ => panic ! ( "Unexpected event" ) ,
6122
+ }
6123
+ let node_txn = nodes[ 2 ] . tx_broadcaster . txn_broadcasted . lock ( ) . unwrap ( ) . clone ( ) ;
6124
+
6125
+ // Verify that B's ChannelManager is able to extract preimage from HTLC Success tx and pass it backward
6126
+ nodes[ 1 ] . chain_monitor . block_connected_with_filtering ( & Block { header, txdata : node_txn} , 1 ) ;
6127
+ {
6128
+ let mut added_monitors = nodes[ 1 ] . chan_monitor . added_monitors . lock ( ) . unwrap ( ) ;
6129
+ assert_eq ! ( added_monitors. len( ) , 1 ) ;
6130
+ added_monitors. clear ( ) ;
6131
+ }
6132
+ let events = nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
6133
+ assert_eq ! ( events. len( ) , 2 ) ;
6134
+ match events[ 0 ] {
6135
+ MessageSendEvent :: BroadcastChannelUpdate { .. } => { } ,
6136
+ _ => panic ! ( "Unexpected event" ) ,
6137
+ }
6138
+ match events[ 1 ] {
6139
+ MessageSendEvent :: UpdateHTLCs { ref node_id, updates : msgs:: CommitmentUpdate { ref update_add_htlcs, ref update_fail_htlcs, ref update_fulfill_htlcs, ref update_fail_malformed_htlcs, .. } } => {
6140
+ assert ! ( update_add_htlcs. is_empty( ) ) ;
6141
+ assert ! ( update_fail_htlcs. is_empty( ) ) ;
6142
+ assert ! ( !update_fulfill_htlcs. is_empty( ) ) ;
6143
+ assert ! ( update_fail_malformed_htlcs. is_empty( ) ) ;
6144
+ assert_eq ! ( nodes[ 0 ] . node. get_our_node_id( ) , * node_id) ;
6145
+ } ,
6146
+ _ => panic ! ( "Unexpected event" ) ,
6147
+ } ;
6148
+
6149
+ // Broadcast legit commitment tx from A on B's chain
6150
+ // Broadcast preimage tx by B on offered output from A commitment tx on A's chain
6151
+ let commitment_tx = nodes[ 0 ] . node . channel_state . lock ( ) . unwrap ( ) . by_id . get ( & chan_1. 2 ) . unwrap ( ) . last_local_commitment_txn . clone ( ) ;
6152
+ nodes[ 1 ] . chain_monitor . block_connected_with_filtering ( & Block { header, txdata : vec ! [ commitment_tx[ 0 ] . clone( ) ] } , 1 ) ;
6153
+ let events = nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
6154
+ assert_eq ! ( events. len( ) , 1 ) ;
6155
+ match events[ 0 ] {
6156
+ MessageSendEvent :: BroadcastChannelUpdate { .. } => { } ,
6157
+ _ => panic ! ( "Unexpected event" ) ,
6158
+ }
6159
+ let node_txn = nodes[ 1 ] . tx_broadcaster . txn_broadcasted . lock ( ) . unwrap ( ) . clone ( ) ;
6160
+
6161
+ // Verify that A's ChannelManager is able to extract preimage from preimage tx and pass it backward
6162
+ nodes[ 0 ] . chain_monitor . block_connected_with_filtering ( & Block { header, txdata : node_txn } , 1 ) ;
6163
+ let events = nodes[ 0 ] . node . get_and_clear_pending_msg_events ( ) ;
6164
+ assert_eq ! ( events. len( ) , 1 ) ;
6165
+ match events[ 0 ] {
6166
+ MessageSendEvent :: BroadcastChannelUpdate { .. } => { } ,
6167
+ _ => panic ! ( "Unexpected event" ) ,
6168
+ }
6169
+ }
6170
+
6067
6171
#[ test]
6068
6172
fn test_htlc_ignore_latest_remote_commitment ( ) {
6069
6173
// Test that HTLC transactions spending the latest remote commitment transaction are simply
0 commit comments