@@ -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,17 @@ 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
2153
+ // this HTLC should be failed
2154
+ // backwards. Failing to send here
2155
+ // indicates that this HTLC may keep
2156
+ // being put back into the holding
2157
+ // cell without ever being
2158
+ // successfully
2159
+ // forwarded/failed/fulfilled,
2160
+ // causing our counterparty to
2161
+ // eventually close on us.
2162
+ htlcs_to_fail. push ( ( source. clone ( ) , * payment_hash) ) ;
2151
2163
} ,
2152
2164
_ => {
2153
2165
log_info ! ( logger, "Failed to send HTLC with payment_hash {} resulting in a channel closure during holding_cell freeing" , log_bytes!( payment_hash. 0 ) ) ;
@@ -2199,10 +2211,10 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
2199
2211
//fail it back the route, if it's a temporary issue we can ignore it...
2200
2212
match err {
2201
2213
None => {
2202
- 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.
2214
+ if update_add_htlcs. is_empty ( ) && update_fulfill_htlcs. is_empty ( ) && update_fail_htlcs. is_empty ( ) && self . holding_cell_update_fee . is_none ( ) && htlcs_to_fail . is_empty ( ) {
2215
+ // Hitting this case indicates that we got some Errs back from update_fulfill_htlc
2216
+ // or update_fail_htlc.
2217
+ log_warn ! ( logger , "Attempted to fulfill or fail an HTLC that was already removed" ) ;
2206
2218
return Ok ( None ) ;
2207
2219
}
2208
2220
let update_fee = if let Some ( feerate) = self . holding_cell_update_fee {
@@ -2228,7 +2240,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
2228
2240
update_fail_malformed_htlcs : Vec :: new ( ) ,
2229
2241
update_fee : update_fee,
2230
2242
commitment_signed,
2231
- } , monitor_update) ) )
2243
+ } , monitor_update, htlcs_to_fail ) ) )
2232
2244
} ,
2233
2245
Some ( e) => Err ( e)
2234
2246
}
@@ -2242,7 +2254,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
2242
2254
/// waiting on this revoke_and_ack. The generation of this new commitment_signed may also fail,
2243
2255
/// generating an appropriate error *after* the channel state has been updated based on the
2244
2256
/// 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 >
2257
+ 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
2258
where F :: Target : FeeEstimator ,
2247
2259
L :: Target : Logger ,
2248
2260
{
@@ -2417,11 +2429,11 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
2417
2429
}
2418
2430
self . monitor_pending_forwards . append ( & mut to_forward_infos) ;
2419
2431
self . monitor_pending_failures . append ( & mut revoked_htlcs) ;
2420
- return Ok ( ( None , Vec :: new ( ) , Vec :: new ( ) , None , monitor_update) )
2432
+ return Ok ( ( None , Vec :: new ( ) , Vec :: new ( ) , None , monitor_update, Vec :: new ( ) ) )
2421
2433
}
2422
2434
2423
2435
match self . free_holding_cell_htlcs ( logger) ? {
2424
- Some ( ( mut commitment_update, mut additional_update) ) => {
2436
+ Some ( ( mut commitment_update, mut additional_update, htlcs_to_fail ) ) => {
2425
2437
commitment_update. update_fail_htlcs . reserve ( update_fail_htlcs. len ( ) ) ;
2426
2438
for fail_msg in update_fail_htlcs. drain ( ..) {
2427
2439
commitment_update. update_fail_htlcs . push ( fail_msg) ;
@@ -2436,7 +2448,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
2436
2448
self . latest_monitor_update_id = monitor_update. update_id ;
2437
2449
monitor_update. updates . append ( & mut additional_update. updates ) ;
2438
2450
2439
- Ok ( ( Some ( commitment_update) , to_forward_infos, revoked_htlcs, None , monitor_update) )
2451
+ Ok ( ( Some ( commitment_update) , to_forward_infos, revoked_htlcs, None , monitor_update, htlcs_to_fail ) )
2440
2452
} ,
2441
2453
None => {
2442
2454
if require_commitment {
@@ -2454,9 +2466,9 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
2454
2466
update_fail_malformed_htlcs,
2455
2467
update_fee : None ,
2456
2468
commitment_signed
2457
- } ) , to_forward_infos, revoked_htlcs, None , monitor_update) )
2469
+ } ) , to_forward_infos, revoked_htlcs, None , monitor_update, Vec :: new ( ) ) )
2458
2470
} else {
2459
- Ok ( ( None , to_forward_infos, revoked_htlcs, self . maybe_propose_first_closing_signed ( fee_estimator) , monitor_update) )
2471
+ Ok ( ( None , to_forward_infos, revoked_htlcs, self . maybe_propose_first_closing_signed ( fee_estimator) , monitor_update, Vec :: new ( ) ) )
2460
2472
}
2461
2473
}
2462
2474
}
@@ -2726,7 +2738,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
2726
2738
2727
2739
/// May panic if some calls other than message-handling calls (which will all Err immediately)
2728
2740
/// 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 {
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 > , Option < Vec < ( HTLCSource , PaymentHash ) > > , RAACommitmentOrder , Option < msgs:: Shutdown > ) , ChannelError > where L :: Target : Logger {
2730
2742
if self . channel_state & ( ChannelState :: PeerDisconnected as u32 ) == 0 {
2731
2743
// While BOLT 2 doesn't indicate explicitly we should error this channel here, it
2732
2744
// almost certainly indicates we are going to end up out-of-sync in some way, so we
@@ -2774,7 +2786,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
2774
2786
return Err ( ChannelError :: Close ( "Peer claimed they saw a revoke_and_ack but we haven't sent funding_locked yet" ) ) ;
2775
2787
}
2776
2788
// Short circuit the whole handler as there is nothing we can resend them
2777
- return Ok ( ( None , None , None , None , RAACommitmentOrder :: CommitmentFirst , shutdown_msg) ) ;
2789
+ return Ok ( ( None , None , None , None , None , RAACommitmentOrder :: CommitmentFirst , shutdown_msg) ) ;
2778
2790
}
2779
2791
2780
2792
// We have OurFundingLocked set!
@@ -2783,7 +2795,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
2783
2795
return Ok ( ( Some ( msgs:: FundingLocked {
2784
2796
channel_id : self . channel_id ( ) ,
2785
2797
next_per_commitment_point : next_per_commitment_point,
2786
- } ) , None , None , None , RAACommitmentOrder :: CommitmentFirst , shutdown_msg) ) ;
2798
+ } ) , None , None , None , None , RAACommitmentOrder :: CommitmentFirst , shutdown_msg) ) ;
2787
2799
}
2788
2800
2789
2801
let required_revoke = if msg. next_remote_commitment_number + 1 == INITIAL_COMMITMENT_NUMBER - self . cur_local_commitment_transaction_number {
@@ -2832,11 +2844,11 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
2832
2844
match self . free_holding_cell_htlcs ( logger) {
2833
2845
Err ( ChannelError :: Close ( msg) ) => return Err ( ChannelError :: Close ( msg) ) ,
2834
2846
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) ) ,
2847
+ Ok ( Some ( ( commitment_update, monitor_update, htlcs_to_fail ) ) ) => return Ok ( ( resend_funding_locked, required_revoke, Some ( commitment_update) , Some ( monitor_update) , Some ( htlcs_to_fail ) , self . resend_order . clone ( ) , shutdown_msg) ) ,
2848
+ Ok ( None ) => return Ok ( ( resend_funding_locked, required_revoke, None , None , None , self . resend_order . clone ( ) , shutdown_msg) ) ,
2837
2849
}
2838
2850
} else {
2839
- return Ok ( ( resend_funding_locked, required_revoke, None , None , self . resend_order . clone ( ) , shutdown_msg) ) ;
2851
+ return Ok ( ( resend_funding_locked, required_revoke, None , None , None , self . resend_order . clone ( ) , shutdown_msg) ) ;
2840
2852
}
2841
2853
} else if msg. next_local_commitment_number == our_next_remote_commitment_number - 1 {
2842
2854
if required_revoke. is_some ( ) {
@@ -2847,10 +2859,10 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
2847
2859
2848
2860
if self . channel_state & ( ChannelState :: MonitorUpdateFailed as u32 ) != 0 {
2849
2861
self . monitor_pending_commitment_signed = true ;
2850
- return Ok ( ( resend_funding_locked, None , None , None , self . resend_order . clone ( ) , shutdown_msg) ) ;
2862
+ return Ok ( ( resend_funding_locked, None , None , None , None , self . resend_order . clone ( ) , shutdown_msg) ) ;
2851
2863
}
2852
2864
2853
- return Ok ( ( resend_funding_locked, required_revoke, Some ( self . get_last_commitment_update ( logger) ) , None , self . resend_order . clone ( ) , shutdown_msg) ) ;
2865
+ return Ok ( ( resend_funding_locked, required_revoke, Some ( self . get_last_commitment_update ( logger) ) , None , None , self . resend_order . clone ( ) , shutdown_msg) ) ;
2854
2866
} else {
2855
2867
return Err ( ChannelError :: Close ( "Peer attempted to reestablish channel with a very old remote commitment transaction" ) ) ;
2856
2868
}
0 commit comments