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