@@ -518,13 +518,7 @@ impl ChannelManager {
518
518
for tx in local_txn {
519
519
self . tx_broadcaster . broadcast_transaction ( & tx) ;
520
520
}
521
- //TODO: We need to have a way where outbound HTLC claims can result in us claiming the
522
- //now-on-chain HTLC output for ourselves (and, thereafter, passing the HTLC backwards).
523
- //TODO: We need to handle monitoring of pending offered HTLCs which just hit the chain and
524
- //may be claimed, resulting in us claiming the inbound HTLCs (and back-failing after
525
- //timeouts are hit and our claims confirm).
526
- //TODO: In any case, we need to make sure we remove any pending htlc tracking (via
527
- //fail_backwards or claim_funds) eventually for all HTLCs that were in the channel
521
+
528
522
}
529
523
530
524
/// Force closes a channel, immediately broadcasting the latest local commitment transaction to
@@ -1207,11 +1201,16 @@ impl ChannelManager {
1207
1201
1208
1202
let mut add_htlc_msgs = Vec :: new ( ) ;
1209
1203
for HTLCForwardInfo { prev_short_channel_id, prev_htlc_id, forward_info } in pending_forwards. drain ( ..) {
1210
- let htlc_source = HTLCSource :: PreviousHopData ( HTLCPreviousHopData {
1204
+ let prev_hop_data = HTLCPreviousHopData {
1211
1205
short_channel_id : prev_short_channel_id,
1212
1206
htlc_id : prev_htlc_id,
1213
1207
incoming_packet_shared_secret : forward_info. incoming_shared_secret ,
1214
- } ) ;
1208
+ } ;
1209
+ match channel_state. claimable_htlcs . entry ( forward_info. payment_hash ) {
1210
+ hash_map:: Entry :: Occupied ( mut entry) => entry. get_mut ( ) . push ( prev_hop_data. clone ( ) ) ,
1211
+ hash_map:: Entry :: Vacant ( entry) => { entry. insert ( vec ! [ prev_hop_data. clone( ) ] ) ; } ,
1212
+ } ;
1213
+ let htlc_source = HTLCSource :: PreviousHopData ( prev_hop_data) ;
1215
1214
match forward_chan. send_htlc ( forward_info. amt_to_forward , forward_info. payment_hash , forward_info. outgoing_cltv_value , htlc_source. clone ( ) , forward_info. onion_packet . unwrap ( ) ) {
1216
1215
Err ( _e) => {
1217
1216
let chan_update = self . get_channel_update ( forward_chan) . unwrap ( ) ;
@@ -1731,34 +1730,47 @@ impl ChannelManager {
1731
1730
1732
1731
fn internal_update_fulfill_htlc ( & self , their_node_id : & PublicKey , msg : & msgs:: UpdateFulfillHTLC ) -> Result < ( ) , MsgHandleErrInternal > {
1733
1732
let mut channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
1734
- let htlc_source = match channel_state. by_id . get_mut ( & msg. channel_id ) {
1733
+ let ( htlc_source, payment_hash ) = match channel_state. by_id . get_mut ( & msg. channel_id ) {
1735
1734
Some ( chan) => {
1736
1735
if chan. get_their_node_id ( ) != * their_node_id {
1737
1736
//TODO: here and below MsgHandleErrInternal, #153 case
1738
1737
return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Got a message for a channel from the wrong node!" , msg. channel_id ) ) ;
1739
1738
}
1740
- chan. update_fulfill_htlc ( & msg)
1741
- . map_err ( |e| MsgHandleErrInternal :: from_chan_maybe_close ( e, msg. channel_id ) ) ?. clone ( )
1739
+ let ( htlc_source, payment_hash) = chan. update_fulfill_htlc ( & msg)
1740
+ . map_err ( |e| MsgHandleErrInternal :: from_chan_maybe_close ( e, msg. channel_id ) ) ?;
1741
+ ( htlc_source. clone ( ) , payment_hash)
1742
1742
} ,
1743
1743
None => return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Failed to find corresponding channel" , msg. channel_id ) )
1744
1744
} ;
1745
+ if let Some ( sources) = channel_state. claimable_htlcs . get_mut ( & payment_hash) {
1746
+ if let HTLCSource :: PreviousHopData ( previous_hop_data) = htlc_source. clone ( ) {
1747
+ sources. retain ( |ref source| !( source. short_channel_id == previous_hop_data. short_channel_id && source. htlc_id == previous_hop_data. htlc_id ) ) ;
1748
+ }
1749
+ }
1745
1750
self . claim_funds_internal ( channel_state, htlc_source, msg. payment_preimage . clone ( ) ) ;
1746
1751
Ok ( ( ) )
1747
1752
}
1748
1753
1749
1754
fn internal_update_fail_htlc ( & self , their_node_id : & PublicKey , msg : & msgs:: UpdateFailHTLC ) -> Result < Option < msgs:: HTLCFailChannelUpdate > , MsgHandleErrInternal > {
1750
- let mut channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
1751
- let htlc_source = match channel_state. by_id . get_mut ( & msg. channel_id ) {
1755
+ let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
1756
+ let channel_state = channel_state_lock. borrow_parts ( ) ;
1757
+ let ( htlc_source, payment_hash) = match channel_state. by_id . get_mut ( & msg. channel_id ) {
1752
1758
Some ( chan) => {
1753
1759
if chan. get_their_node_id ( ) != * their_node_id {
1754
1760
//TODO: here and below MsgHandleErrInternal, #153 case
1755
1761
return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Got a message for a channel from the wrong node!" , msg. channel_id ) ) ;
1756
1762
}
1757
- chan. update_fail_htlc ( & msg, HTLCFailReason :: ErrorPacket { err : msg. reason . clone ( ) } )
1758
- . map_err ( |e| MsgHandleErrInternal :: from_chan_maybe_close ( e, msg. channel_id ) )
1763
+ let ( htlc_source, payment_hash) = chan. update_fail_htlc ( & msg, HTLCFailReason :: ErrorPacket { err : msg. reason . clone ( ) } )
1764
+ . map_err ( |e| MsgHandleErrInternal :: from_chan_maybe_close ( e, msg. channel_id ) ) ?;
1765
+ ( htlc_source, payment_hash)
1759
1766
} ,
1760
1767
None => return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Failed to find corresponding channel" , msg. channel_id ) )
1761
- } ?;
1768
+ } ;
1769
+ if let Some ( sources) = channel_state. claimable_htlcs . get_mut ( & payment_hash) {
1770
+ if let HTLCSource :: PreviousHopData ( previous_hop_data) = htlc_source. clone ( ) {
1771
+ sources. retain ( |ref source| !( source. short_channel_id == previous_hop_data. short_channel_id && source. htlc_id == previous_hop_data. htlc_id ) ) ;
1772
+ }
1773
+ }
1762
1774
1763
1775
match htlc_source {
1764
1776
& HTLCSource :: OutboundRoute { ref route, ref session_priv, .. } => {
@@ -1821,7 +1833,7 @@ impl ChannelManager {
1821
1833
1822
1834
fn internal_update_fail_malformed_htlc ( & self , their_node_id : & PublicKey , msg : & msgs:: UpdateFailMalformedHTLC ) -> Result < ( ) , MsgHandleErrInternal > {
1823
1835
let mut channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
1824
- match channel_state. by_id . get_mut ( & msg. channel_id ) {
1836
+ let ( htlc_source , payment_hash ) = match channel_state. by_id . get_mut ( & msg. channel_id ) {
1825
1837
Some ( chan) => {
1826
1838
if chan. get_their_node_id ( ) != * their_node_id {
1827
1839
//TODO: here and below MsgHandleErrInternal, #153 case
@@ -1830,12 +1842,18 @@ impl ChannelManager {
1830
1842
if ( msg. failure_code & 0x8000 ) != 0 {
1831
1843
return Err ( MsgHandleErrInternal :: send_err_msg_close_chan ( "Got update_fail_malformed_htlc with BADONION set" , msg. channel_id ) ) ;
1832
1844
}
1833
- chan. update_fail_malformed_htlc ( & msg, HTLCFailReason :: Reason { failure_code : msg. failure_code , data : Vec :: new ( ) } )
1845
+ let ( htlc_source , payment_hash ) = chan. update_fail_malformed_htlc ( & msg, HTLCFailReason :: Reason { failure_code : msg. failure_code , data : Vec :: new ( ) } )
1834
1846
. map_err ( |e| MsgHandleErrInternal :: from_chan_maybe_close ( e, msg. channel_id ) ) ?;
1835
- Ok ( ( ) )
1847
+ ( htlc_source . clone ( ) , payment_hash )
1836
1848
} ,
1837
1849
None => return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Failed to find corresponding channel" , msg. channel_id ) )
1850
+ } ;
1851
+ if let Some ( sources) = channel_state. claimable_htlcs . get_mut ( & payment_hash) {
1852
+ if let HTLCSource :: PreviousHopData ( previous_hop_data) = htlc_source {
1853
+ sources. retain ( |ref source| !( source. short_channel_id == previous_hop_data. short_channel_id && source. htlc_id == previous_hop_data. htlc_id ) ) ;
1854
+ }
1838
1855
}
1856
+ Ok ( ( ) )
1839
1857
}
1840
1858
1841
1859
fn internal_commitment_signed ( & self , their_node_id : & PublicKey , msg : & msgs:: CommitmentSigned ) -> Result < ( msgs:: RevokeAndACK , Option < msgs:: CommitmentSigned > ) , MsgHandleErrInternal > {
@@ -2040,6 +2058,7 @@ impl ChainListener for ChannelManager {
2040
2058
fn block_connected ( & self , header : & BlockHeader , height : u32 , txn_matched : & [ & Transaction ] , indexes_of_txn_matched : & [ u32 ] ) {
2041
2059
let mut new_events = Vec :: new ( ) ;
2042
2060
let mut failed_channels = Vec :: new ( ) ;
2061
+ let mut hash_to_remove = Vec :: new ( ) ;
2043
2062
{
2044
2063
let mut channel_lock = self . channel_state . lock ( ) . unwrap ( ) ;
2045
2064
let channel_state = channel_lock. borrow_parts ( ) ;
@@ -2102,10 +2121,33 @@ impl ChainListener for ChannelManager {
2102
2121
}
2103
2122
true
2104
2123
} ) ;
2124
+
2125
+ for tx in txn_matched {
2126
+ if let Some ( payments_data) = self . monitor . is_resolving_output ( & tx) {
2127
+ for payment_data in payments_data {
2128
+ hash_to_remove. push ( ( payment_data. 0 , payment_data. 1 ) ) ;
2129
+ }
2130
+ }
2131
+ }
2105
2132
}
2106
2133
for failure in failed_channels. drain ( ..) {
2107
2134
self . finish_force_close_channel ( failure) ;
2108
2135
}
2136
+
2137
+ {
2138
+ let mut channel_state = Some ( self . channel_state . lock ( ) . unwrap ( ) ) ;
2139
+ for ( preimage, hash) in hash_to_remove {
2140
+ if let Some ( preimage) = preimage {
2141
+ if channel_state. is_none ( ) { channel_state = Some ( self . channel_state . lock ( ) . unwrap ( ) ) ; }
2142
+ if let Some ( mut sources) = channel_state. as_mut ( ) . unwrap ( ) . claimable_htlcs . remove ( & hash) {
2143
+ for source in sources. drain ( ..) {
2144
+ self . claim_funds_internal ( channel_state. take ( ) . unwrap ( ) , HTLCSource :: PreviousHopData ( source) , preimage) ;
2145
+ }
2146
+ }
2147
+ }
2148
+ }
2149
+ }
2150
+
2109
2151
let mut pending_events = self . pending_events . lock ( ) . unwrap ( ) ;
2110
2152
for funding_locked in new_events. drain ( ..) {
2111
2153
pending_events. push ( funding_locked) ;
@@ -3817,7 +3859,7 @@ mod tests {
3817
3859
}
3818
3860
3819
3861
/// Tests that the given node has broadcast a claim transaction against the provided revoked
3820
- /// HTLC transaction.
3862
+ /// HTLC transaction issued from a revoked commitment tx
3821
3863
fn test_revoked_htlc_claim_txn_broadcast ( node : & Node , revoked_tx : Transaction ) {
3822
3864
let mut node_txn = node. tx_broadcaster . txn_broadcasted . lock ( ) . unwrap ( ) ;
3823
3865
assert_eq ! ( node_txn. len( ) , 1 ) ;
@@ -4205,6 +4247,105 @@ mod tests {
4205
4247
assert_eq ! ( nodes[ 1 ] . node. list_channels( ) . len( ) , 0 ) ;
4206
4248
}
4207
4249
4250
+ #[ test]
4251
+ fn test_htlc_on_chain_success ( ) {
4252
+ // Test that in case of an unilateral close onchain, we detect the state of output thanks to
4253
+ // ChainWatchInterface and pass the preimage backward accordingly. So here we test that ChannelManager is
4254
+ // broadcasting the right event to other nodes in payment path.
4255
+ // A --------------------> B ----------------------> C (preimage)
4256
+ // A's commitment tx C's commitment tx
4257
+ // \ \
4258
+ // B's preimage tx C's HTLC Success tx
4259
+
4260
+ let nodes = create_network ( 3 ) ;
4261
+
4262
+ // Create some initial channels
4263
+ let chan_1 = create_announced_chan_between_nodes ( & nodes, 0 , 1 ) ;
4264
+ let chan_2 = create_announced_chan_between_nodes ( & nodes, 1 , 2 ) ;
4265
+
4266
+ // Rebalance the network a bit by relaying one payment through all the channels...
4267
+ send_payment ( & nodes[ 0 ] , & vec ! ( & nodes[ 1 ] , & nodes[ 2 ] ) [ ..] , 8000000 ) ;
4268
+ send_payment ( & nodes[ 0 ] , & vec ! ( & nodes[ 1 ] , & nodes[ 2 ] ) [ ..] , 8000000 ) ;
4269
+
4270
+ let ( payment_preimage, _payment_hash) = route_payment ( & nodes[ 0 ] , & vec ! ( & nodes[ 1 ] , & nodes[ 2 ] ) , 3000000 ) ;
4271
+ let header = BlockHeader { version : 0x20000000 , prev_blockhash : Default :: default ( ) , merkle_root : Default :: default ( ) , time : 42 , bits : 42 , nonce : 42 } ;
4272
+
4273
+ // Broadcast legit commitment tx from C on B's chain
4274
+ // Broadcast HTLC Success transation by C on received output from C's commitment tx on B's chain
4275
+ let commitment_tx = nodes[ 2 ] . node . channel_state . lock ( ) . unwrap ( ) . by_id . get ( & chan_2. 2 ) . unwrap ( ) . last_local_commitment_txn . clone ( ) ;
4276
+ nodes[ 2 ] . node . claim_funds ( payment_preimage) ;
4277
+ {
4278
+ let mut added_monitors = nodes[ 2 ] . chan_monitor . added_monitors . lock ( ) . unwrap ( ) ;
4279
+ assert_eq ! ( added_monitors. len( ) , 1 ) ;
4280
+ added_monitors. clear ( ) ;
4281
+ }
4282
+ let events = nodes[ 2 ] . node . get_and_clear_pending_events ( ) ;
4283
+ assert_eq ! ( events. len( ) , 1 ) ;
4284
+ match events[ 0 ] {
4285
+ Event :: UpdateHTLCs { ref node_id, updates : msgs:: CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, .. } } => {
4286
+ assert ! ( update_add_htlcs. is_empty( ) ) ;
4287
+ assert ! ( update_fail_htlcs. is_empty( ) ) ;
4288
+ assert ! ( !update_fulfill_htlcs. is_empty( ) ) ;
4289
+ assert ! ( update_fail_malformed_htlcs. is_empty( ) ) ;
4290
+ assert_eq ! ( nodes[ 1 ] . node. get_our_node_id( ) , * node_id) ;
4291
+ } ,
4292
+ _ => panic ! ( "Unexpected event" ) ,
4293
+ } ;
4294
+ nodes[ 2 ] . chain_monitor . block_connected_with_filtering ( & Block { header, txdata : vec ! [ commitment_tx[ 0 ] . clone( ) ] } , 1 ) ;
4295
+ let events = nodes[ 2 ] . node . get_and_clear_pending_events ( ) ;
4296
+ assert_eq ! ( events. len( ) , 1 ) ;
4297
+ match events[ 0 ] {
4298
+ Event :: BroadcastChannelUpdate { msg : msgs:: ChannelUpdate { .. } } => { } ,
4299
+ _ => panic ! ( "Unexpected event" ) ,
4300
+ }
4301
+ let node_txn = nodes[ 2 ] . tx_broadcaster . txn_broadcasted . lock ( ) . unwrap ( ) . clone ( ) ;
4302
+
4303
+ // Verify that B's ChannelManager is able to extract preimage from HTLC Success tx and pass it backward
4304
+ nodes[ 1 ] . chain_monitor . block_connected_with_filtering ( & Block { header, txdata : node_txn} , 1 ) ;
4305
+ {
4306
+ let mut added_monitors = nodes[ 1 ] . chan_monitor . added_monitors . lock ( ) . unwrap ( ) ;
4307
+ assert_eq ! ( added_monitors. len( ) , 1 ) ;
4308
+ added_monitors. clear ( ) ;
4309
+ }
4310
+ let events = nodes[ 1 ] . node . get_and_clear_pending_events ( ) ;
4311
+ assert_eq ! ( events. len( ) , 2 ) ;
4312
+ match events[ 0 ] {
4313
+ Event :: BroadcastChannelUpdate { msg : msgs:: ChannelUpdate { .. } } => { } ,
4314
+ _ => panic ! ( "Unexpected event" ) ,
4315
+ }
4316
+ match events[ 1 ] {
4317
+ Event :: UpdateHTLCs { ref node_id, updates : msgs:: CommitmentUpdate { ref update_add_htlcs, ref update_fail_htlcs, ref update_fulfill_htlcs, ref update_fail_malformed_htlcs, .. } } => {
4318
+ assert ! ( update_add_htlcs. is_empty( ) ) ;
4319
+ assert ! ( update_fail_htlcs. is_empty( ) ) ;
4320
+ assert ! ( !update_fulfill_htlcs. is_empty( ) ) ;
4321
+ assert ! ( update_fail_malformed_htlcs. is_empty( ) ) ;
4322
+ assert_eq ! ( nodes[ 0 ] . node. get_our_node_id( ) , * node_id) ;
4323
+ } ,
4324
+ _ => panic ! ( "Unexpected event" ) ,
4325
+ } ;
4326
+
4327
+ // Broadcast legit commitment tx from A on B's chain
4328
+ // Broadcast preimage tx by B on offered output from A commitment tx on A's chain
4329
+ let commitment_tx = nodes[ 0 ] . node . channel_state . lock ( ) . unwrap ( ) . by_id . get ( & chan_1. 2 ) . unwrap ( ) . last_local_commitment_txn . clone ( ) ;
4330
+ nodes[ 1 ] . chain_monitor . block_connected_with_filtering ( & Block { header, txdata : vec ! [ commitment_tx[ 0 ] . clone( ) ] } , 1 ) ;
4331
+ let events = nodes[ 1 ] . node . get_and_clear_pending_events ( ) ;
4332
+ assert_eq ! ( events. len( ) , 1 ) ;
4333
+ match events[ 0 ] {
4334
+ Event :: BroadcastChannelUpdate { msg : msgs:: ChannelUpdate { .. } } => { } ,
4335
+ _ => panic ! ( "Unexpected event" ) ,
4336
+ }
4337
+ let node_txn = nodes[ 1 ] . tx_broadcaster . txn_broadcasted . lock ( ) . unwrap ( ) . clone ( ) ;
4338
+
4339
+ // Verify that A's ChannelManager is able to extract preimage from preimage tx and pass it backward
4340
+ nodes[ 0 ] . chain_monitor . block_connected_with_filtering ( & Block { header, txdata : node_txn } , 1 ) ;
4341
+ let events = nodes[ 0 ] . node . get_and_clear_pending_events ( ) ;
4342
+ assert_eq ! ( events. len( ) , 1 ) ;
4343
+ match events[ 0 ] {
4344
+ Event :: BroadcastChannelUpdate { msg : msgs:: ChannelUpdate { .. } } => { } ,
4345
+ _ => panic ! ( "Unexpected event" ) ,
4346
+ }
4347
+ }
4348
+
4208
4349
#[ test]
4209
4350
fn test_htlc_ignore_latest_remote_commitment ( ) {
4210
4351
// Test that HTLC transactions spending the latest remote commitment transaction are simply
0 commit comments