@@ -2117,7 +2117,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
2117
2117
2118
2118
/// Used to fulfill holding_cell_htlcs when we get a remote ack (or implicitly get it by them
2119
2119
/// fulfilling or failing the last pending HTLC)
2120
- fn free_holding_cell_htlcs < L : Deref > ( & mut self , logger : & L ) -> Result < Option < ( msgs:: CommitmentUpdate , ChannelMonitorUpdate ) > , ChannelError > where L :: Target : Logger {
2120
+ fn free_holding_cell_htlcs < L : Deref > ( & mut self , logger : & L ) -> Result < ( Option < ( msgs:: CommitmentUpdate , ChannelMonitorUpdate ) > , Vec < ( HTLCSource , PaymentHash ) > ) , ChannelError > where L :: Target : Logger {
2121
2121
assert_eq ! ( self . channel_state & ChannelState :: MonitorUpdateFailed as u32 , 0 ) ;
2122
2122
if self . holding_cell_htlc_updates . len ( ) != 0 || self . holding_cell_update_fee . is_some ( ) {
2123
2123
log_trace ! ( logger, "Freeing holding cell with {} HTLC updates{}" , self . holding_cell_htlc_updates. len( ) , if self . holding_cell_update_fee. is_some( ) { " and a fee update" } else { "" } ) ;
@@ -2132,6 +2132,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
2132
2132
let mut update_add_htlcs = Vec :: with_capacity ( htlc_updates. len ( ) ) ;
2133
2133
let mut update_fulfill_htlcs = Vec :: with_capacity ( htlc_updates. len ( ) ) ;
2134
2134
let mut update_fail_htlcs = Vec :: with_capacity ( htlc_updates. len ( ) ) ;
2135
+ let mut htlcs_to_fail = Vec :: new ( ) ;
2135
2136
let mut err = None ;
2136
2137
for htlc_update in htlc_updates. drain ( ..) {
2137
2138
// Note that this *can* fail, though it should be due to rather-rare conditions on
@@ -2150,6 +2151,13 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
2150
2151
match e {
2151
2152
ChannelError :: Ignore ( ref msg) => {
2152
2153
log_info ! ( logger, "Failed to send HTLC with payment_hash {} due to {}" , log_bytes!( payment_hash. 0 ) , msg) ;
2154
+ // If we fail to send here, then this HTLC should
2155
+ // be failed backwards. Failing to send here
2156
+ // indicates that this HTLC may keep being put back
2157
+ // into the holding cell without ever being
2158
+ // successfully forwarded/failed/fulfilled, causing
2159
+ // our counterparty to eventually close on us.
2160
+ htlcs_to_fail. push ( ( source. clone ( ) , * payment_hash) ) ;
2153
2161
} ,
2154
2162
_ => {
2155
2163
log_info ! ( logger, "Failed to send HTLC with payment_hash {} resulting in a channel closure during holding_cell freeing" , log_bytes!( payment_hash. 0 ) ) ;
@@ -2188,24 +2196,27 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
2188
2196
} ,
2189
2197
}
2190
2198
if err. is_some ( ) {
2191
- self . holding_cell_htlc_updates . push ( htlc_update) ;
2192
2199
if let Some ( ChannelError :: Ignore ( _) ) = err {
2193
2200
// If we failed to add the HTLC, but got an Ignore error, we should
2194
2201
// still send the new commitment_signed, so reset the err to None.
2202
+ // If we failed to fail or fulfill an HTLC, but got an Ignore error,
2203
+ // it's OK to drop the error because these errors are caused by
2204
+ // the ChannelManager generating duplicate claim/fail events during
2205
+ // block rescan.
2195
2206
err = None ;
2207
+ } else {
2208
+ self . holding_cell_htlc_updates . push ( htlc_update) ;
2196
2209
}
2197
2210
}
2198
2211
}
2199
2212
}
2200
- //TODO: Need to examine the type of err - if it's a fee issue or similar we may want to
2201
- //fail it back the route, if it's a temporary issue we can ignore it...
2202
2213
match err {
2203
2214
None => {
2204
2215
if update_add_htlcs. is_empty ( ) && update_fulfill_htlcs. is_empty ( ) && update_fail_htlcs. is_empty ( ) && self . holding_cell_update_fee . is_none ( ) {
2205
- // This should never actually happen and indicates we got some Errs back
2206
- // from update_fulfill_htlc/ update_fail_htlc, but we handle it anyway in
2207
- // case there is some strange way to hit duplicate HTLC removes.
2208
- return Ok ( None ) ;
2216
+ // Hitting this case indicates that we got some Errs back from update_fulfill_htlc
2217
+ // or update_fail_htlc.
2218
+ log_warn ! ( logger , "Attempted to fulfill or fail an HTLC that was already removed" ) ;
2219
+ return Ok ( ( None , htlcs_to_fail ) ) ;
2209
2220
}
2210
2221
let update_fee = if let Some ( feerate) = self . holding_cell_update_fee {
2211
2222
self . pending_update_fee = self . holding_cell_update_fee . take ( ) ;
@@ -2223,19 +2234,19 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
2223
2234
self . latest_monitor_update_id = monitor_update. update_id ;
2224
2235
monitor_update. updates . append ( & mut additional_update. updates ) ;
2225
2236
2226
- Ok ( Some ( ( msgs:: CommitmentUpdate {
2237
+ Ok ( ( Some ( ( msgs:: CommitmentUpdate {
2227
2238
update_add_htlcs,
2228
2239
update_fulfill_htlcs,
2229
2240
update_fail_htlcs,
2230
2241
update_fail_malformed_htlcs : Vec :: new ( ) ,
2231
2242
update_fee : update_fee,
2232
2243
commitment_signed,
2233
- } , monitor_update) ) )
2244
+ } , monitor_update) ) , htlcs_to_fail ) )
2234
2245
} ,
2235
2246
Some ( e) => Err ( e)
2236
2247
}
2237
2248
} else {
2238
- Ok ( None )
2249
+ Ok ( ( None , Vec :: new ( ) ) )
2239
2250
}
2240
2251
}
2241
2252
@@ -2244,7 +2255,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
2244
2255
/// waiting on this revoke_and_ack. The generation of this new commitment_signed may also fail,
2245
2256
/// generating an appropriate error *after* the channel state has been updated based on the
2246
2257
/// revoke_and_ack message.
2247
- pub fn revoke_and_ack < F : Deref , L : Deref > ( & mut self , msg : & msgs:: RevokeAndACK , fee_estimator : & F , logger : & L ) -> Result < ( Option < msgs:: CommitmentUpdate > , Vec < ( PendingHTLCInfo , u64 ) > , Vec < ( HTLCSource , PaymentHash , HTLCFailReason ) > , Option < msgs:: ClosingSigned > , ChannelMonitorUpdate ) , ChannelError >
2258
+ pub fn revoke_and_ack < F : Deref , L : Deref > ( & mut self , msg : & msgs:: RevokeAndACK , fee_estimator : & F , logger : & L ) -> Result < ( Option < msgs:: CommitmentUpdate > , Vec < ( PendingHTLCInfo , u64 ) > , Vec < ( HTLCSource , PaymentHash , HTLCFailReason ) > , Option < msgs:: ClosingSigned > , ChannelMonitorUpdate , Vec < ( HTLCSource , PaymentHash ) > ) , ChannelError >
2248
2259
where F :: Target : FeeEstimator ,
2249
2260
L :: Target : Logger ,
2250
2261
{
@@ -2419,11 +2430,11 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
2419
2430
}
2420
2431
self . monitor_pending_forwards . append ( & mut to_forward_infos) ;
2421
2432
self . monitor_pending_failures . append ( & mut revoked_htlcs) ;
2422
- return Ok ( ( None , Vec :: new ( ) , Vec :: new ( ) , None , monitor_update) )
2433
+ return Ok ( ( None , Vec :: new ( ) , Vec :: new ( ) , None , monitor_update, Vec :: new ( ) ) )
2423
2434
}
2424
2435
2425
2436
match self . free_holding_cell_htlcs ( logger) ? {
2426
- Some ( ( mut commitment_update, mut additional_update) ) => {
2437
+ ( Some ( ( mut commitment_update, mut additional_update) ) , htlcs_to_fail ) => {
2427
2438
commitment_update. update_fail_htlcs . reserve ( update_fail_htlcs. len ( ) ) ;
2428
2439
for fail_msg in update_fail_htlcs. drain ( ..) {
2429
2440
commitment_update. update_fail_htlcs . push ( fail_msg) ;
@@ -2438,9 +2449,9 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
2438
2449
self . latest_monitor_update_id = monitor_update. update_id ;
2439
2450
monitor_update. updates . append ( & mut additional_update. updates ) ;
2440
2451
2441
- Ok ( ( Some ( commitment_update) , to_forward_infos, revoked_htlcs, None , monitor_update) )
2452
+ Ok ( ( Some ( commitment_update) , to_forward_infos, revoked_htlcs, None , monitor_update, htlcs_to_fail ) )
2442
2453
} ,
2443
- None => {
2454
+ ( None , htlcs_to_fail ) => {
2444
2455
if require_commitment {
2445
2456
let ( commitment_signed, mut additional_update) = self . send_commitment_no_status_check ( logger) ?;
2446
2457
@@ -2456,9 +2467,9 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
2456
2467
update_fail_malformed_htlcs,
2457
2468
update_fee : None ,
2458
2469
commitment_signed
2459
- } ) , to_forward_infos, revoked_htlcs, None , monitor_update) )
2470
+ } ) , to_forward_infos, revoked_htlcs, None , monitor_update, htlcs_to_fail ) )
2460
2471
} else {
2461
- Ok ( ( None , to_forward_infos, revoked_htlcs, self . maybe_propose_first_closing_signed ( fee_estimator) , monitor_update) )
2472
+ Ok ( ( None , to_forward_infos, revoked_htlcs, self . maybe_propose_first_closing_signed ( fee_estimator) , monitor_update, htlcs_to_fail ) )
2462
2473
}
2463
2474
}
2464
2475
}
@@ -2727,7 +2738,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
2727
2738
2728
2739
/// May panic if some calls other than message-handling calls (which will all Err immediately)
2729
2740
/// have been called between remove_uncommitted_htlcs_and_mark_paused and this call.
2730
- pub fn channel_reestablish < L : Deref > ( & mut self , msg : & msgs:: ChannelReestablish , logger : & L ) -> Result < ( Option < msgs:: FundingLocked > , Option < msgs:: RevokeAndACK > , Option < msgs:: CommitmentUpdate > , Option < ChannelMonitorUpdate > , RAACommitmentOrder , Option < msgs:: Shutdown > ) , ChannelError > where L :: Target : Logger {
2741
+ pub fn channel_reestablish < L : Deref > ( & mut self , msg : & msgs:: ChannelReestablish , logger : & L ) -> Result < ( Option < msgs:: FundingLocked > , Option < msgs:: RevokeAndACK > , Option < msgs:: CommitmentUpdate > , Option < ChannelMonitorUpdate > , Vec < ( HTLCSource , PaymentHash ) > , RAACommitmentOrder , Option < msgs:: Shutdown > ) , ChannelError > where L :: Target : Logger {
2731
2742
if self . channel_state & ( ChannelState :: PeerDisconnected as u32 ) == 0 {
2732
2743
// While BOLT 2 doesn't indicate explicitly we should error this channel here, it
2733
2744
// almost certainly indicates we are going to end up out-of-sync in some way, so we
@@ -2778,15 +2789,15 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
2778
2789
return Err ( ChannelError :: Close ( "Peer claimed they saw a revoke_and_ack but we haven't sent funding_locked yet" . to_owned ( ) ) ) ;
2779
2790
}
2780
2791
// Short circuit the whole handler as there is nothing we can resend them
2781
- return Ok ( ( None , None , None , None , RAACommitmentOrder :: CommitmentFirst , shutdown_msg) ) ;
2792
+ return Ok ( ( None , None , None , None , Vec :: new ( ) , RAACommitmentOrder :: CommitmentFirst , shutdown_msg) ) ;
2782
2793
}
2783
2794
2784
2795
// We have OurFundingLocked set!
2785
2796
let next_per_commitment_point = self . local_keys . get_per_commitment_point ( self . cur_local_commitment_transaction_number , & self . secp_ctx ) ;
2786
2797
return Ok ( ( Some ( msgs:: FundingLocked {
2787
2798
channel_id : self . channel_id ( ) ,
2788
2799
next_per_commitment_point : next_per_commitment_point,
2789
- } ) , None , None , None , RAACommitmentOrder :: CommitmentFirst , shutdown_msg) ) ;
2800
+ } ) , None , None , None , Vec :: new ( ) , RAACommitmentOrder :: CommitmentFirst , shutdown_msg) ) ;
2790
2801
}
2791
2802
2792
2803
let required_revoke = if msg. next_remote_commitment_number + 1 == INITIAL_COMMITMENT_NUMBER - self . cur_local_commitment_transaction_number {
@@ -2834,11 +2845,11 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
2834
2845
match self . free_holding_cell_htlcs ( logger) {
2835
2846
Err ( ChannelError :: Close ( msg) ) => return Err ( ChannelError :: Close ( msg) ) ,
2836
2847
Err ( ChannelError :: Ignore ( _) ) | Err ( ChannelError :: CloseDelayBroadcast ( _) ) => panic ! ( "Got non-channel-failing result from free_holding_cell_htlcs" ) ,
2837
- Ok ( Some ( ( commitment_update, monitor_update) ) ) => return Ok ( ( resend_funding_locked, required_revoke, Some ( commitment_update) , Some ( monitor_update) , self . resend_order . clone ( ) , shutdown_msg) ) ,
2838
- Ok ( None ) => return Ok ( ( resend_funding_locked, required_revoke, None , None , self . resend_order . clone ( ) , shutdown_msg) ) ,
2848
+ Ok ( ( Some ( ( commitment_update, monitor_update) ) , htlcs_to_fail ) ) => return Ok ( ( resend_funding_locked, required_revoke, Some ( commitment_update) , Some ( monitor_update) , htlcs_to_fail , self . resend_order . clone ( ) , shutdown_msg) ) ,
2849
+ Ok ( ( None , htlcs_to_fail ) ) => return Ok ( ( resend_funding_locked, required_revoke, None , None , htlcs_to_fail , self . resend_order . clone ( ) , shutdown_msg) ) ,
2839
2850
}
2840
2851
} else {
2841
- return Ok ( ( resend_funding_locked, required_revoke, None , None , self . resend_order . clone ( ) , shutdown_msg) ) ;
2852
+ return Ok ( ( resend_funding_locked, required_revoke, None , None , Vec :: new ( ) , self . resend_order . clone ( ) , shutdown_msg) ) ;
2842
2853
}
2843
2854
} else if msg. next_local_commitment_number == our_next_remote_commitment_number - 1 {
2844
2855
if required_revoke. is_some ( ) {
@@ -2849,10 +2860,10 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
2849
2860
2850
2861
if self . channel_state & ( ChannelState :: MonitorUpdateFailed as u32 ) != 0 {
2851
2862
self . monitor_pending_commitment_signed = true ;
2852
- return Ok ( ( resend_funding_locked, None , None , None , self . resend_order . clone ( ) , shutdown_msg) ) ;
2863
+ return Ok ( ( resend_funding_locked, None , None , None , Vec :: new ( ) , self . resend_order . clone ( ) , shutdown_msg) ) ;
2853
2864
}
2854
2865
2855
- return Ok ( ( resend_funding_locked, required_revoke, Some ( self . get_last_commitment_update ( logger) ) , None , self . resend_order . clone ( ) , shutdown_msg) ) ;
2866
+ return Ok ( ( resend_funding_locked, required_revoke, Some ( self . get_last_commitment_update ( logger) ) , None , Vec :: new ( ) , self . resend_order . clone ( ) , shutdown_msg) ) ;
2856
2867
} else {
2857
2868
return Err ( ChannelError :: Close ( "Peer attempted to reestablish channel with a very old remote commitment transaction" . to_owned ( ) ) ) ;
2858
2869
}
0 commit comments