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