@@ -3170,7 +3170,6 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
3170
3170
let mut new_events = Vec :: new ( ) ;
3171
3171
let mut failed_forwards = Vec :: new ( ) ;
3172
3172
let mut phantom_receives: Vec < ( u64 , OutPoint , u128 , Vec < ( PendingHTLCInfo , u64 ) > ) > = Vec :: new ( ) ;
3173
- let mut handle_errors = Vec :: new ( ) ;
3174
3173
{
3175
3174
let mut forward_htlcs = HashMap :: new ( ) ;
3176
3175
mem:: swap ( & mut forward_htlcs, & mut self . forward_htlcs . lock ( ) . unwrap ( ) ) ;
@@ -3286,8 +3285,6 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
3286
3285
continue ;
3287
3286
} ,
3288
3287
hash_map:: Entry :: Occupied ( mut chan) => {
3289
- let mut add_htlc_msgs = Vec :: new ( ) ;
3290
- let mut fail_htlc_msgs = Vec :: new ( ) ;
3291
3288
for forward_info in pending_forwards. drain ( ..) {
3292
3289
match forward_info {
3293
3290
HTLCForwardInfo :: AddHTLC ( PendingAddHTLCInfo {
@@ -3306,112 +3303,44 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
3306
3303
// Phantom payments are only PendingHTLCRouting::Receive.
3307
3304
phantom_shared_secret : None ,
3308
3305
} ) ;
3309
- match chan. get_mut ( ) . send_htlc ( outgoing_amt_msat, payment_hash, outgoing_cltv_value, htlc_source. clone ( ) , onion_packet, & self . logger ) {
3310
- Err ( e) => {
3311
- if let ChannelError :: Ignore ( msg) = e {
3312
- log_trace ! ( self . logger, "Failed to forward HTLC with payment_hash {}: {}" , log_bytes!( payment_hash. 0 ) , msg) ;
3313
- } else {
3314
- panic ! ( "Stated return value requirements in send_htlc() were not met" ) ;
3315
- }
3316
- let ( failure_code, data) = self . get_htlc_temp_fail_err_and_data ( 0x1000 |7 , short_chan_id, chan. get ( ) ) ;
3317
- failed_forwards. push ( ( htlc_source, payment_hash,
3318
- HTLCFailReason :: reason ( failure_code, data) ,
3319
- HTLCDestination :: NextHopChannel { node_id : Some ( chan. get ( ) . get_counterparty_node_id ( ) ) , channel_id : forward_chan_id }
3320
- ) ) ;
3321
- continue ;
3322
- } ,
3323
- Ok ( update_add) => {
3324
- match update_add {
3325
- Some ( msg) => { add_htlc_msgs. push ( msg) ; } ,
3326
- None => {
3327
- // Nothing to do here...we're waiting on a remote
3328
- // revoke_and_ack before we can add anymore HTLCs. The Channel
3329
- // will automatically handle building the update_add_htlc and
3330
- // commitment_signed messages when we can.
3331
- // TODO: Do some kind of timer to set the channel as !is_live()
3332
- // as we don't really want others relying on us relaying through
3333
- // this channel currently :/.
3334
- }
3335
- }
3306
+ if let Err ( e) = chan. get_mut ( ) . queue_htlc ( outgoing_amt_msat,
3307
+ payment_hash, outgoing_cltv_value, htlc_source. clone ( ) ,
3308
+ onion_packet, & self . logger )
3309
+ {
3310
+ if let ChannelError :: Ignore ( msg) = e {
3311
+ log_trace ! ( self . logger, "Failed to forward HTLC with payment_hash {}: {}" , log_bytes!( payment_hash. 0 ) , msg) ;
3312
+ } else {
3313
+ panic ! ( "Stated return value requirements in send_htlc() were not met" ) ;
3336
3314
}
3315
+ let ( failure_code, data) = self . get_htlc_temp_fail_err_and_data ( 0x1000 |7 , short_chan_id, chan. get ( ) ) ;
3316
+ failed_forwards. push ( ( htlc_source, payment_hash,
3317
+ HTLCFailReason :: reason ( failure_code, data) ,
3318
+ HTLCDestination :: NextHopChannel { node_id : Some ( chan. get ( ) . get_counterparty_node_id ( ) ) , channel_id : forward_chan_id }
3319
+ ) ) ;
3320
+ continue ;
3337
3321
}
3338
3322
} ,
3339
3323
HTLCForwardInfo :: AddHTLC { .. } => {
3340
3324
panic ! ( "short_channel_id != 0 should imply any pending_forward entries are of type Forward" ) ;
3341
3325
} ,
3342
3326
HTLCForwardInfo :: FailHTLC { htlc_id, err_packet } => {
3343
3327
log_trace ! ( self . logger, "Failing HTLC back to channel with short id {} (backward HTLC ID {}) after delay" , short_chan_id, htlc_id) ;
3344
- match chan. get_mut ( ) . get_update_fail_htlc ( htlc_id, err_packet, & self . logger ) {
3345
- Err ( e) => {
3346
- if let ChannelError :: Ignore ( msg) = e {
3347
- log_trace ! ( self . logger, "Failed to fail HTLC with ID {} backwards to short_id {}: {}" , htlc_id, short_chan_id, msg) ;
3348
- } else {
3349
- panic ! ( "Stated return value requirements in get_update_fail_htlc() were not met" ) ;
3350
- }
3351
- // fail-backs are best-effort, we probably already have one
3352
- // pending, and if not that's OK, if not, the channel is on
3353
- // the chain and sending the HTLC-Timeout is their problem.
3354
- continue ;
3355
- } ,
3356
- Ok ( Some ( msg) ) => { fail_htlc_msgs. push ( msg) ; } ,
3357
- Ok ( None ) => {
3358
- // Nothing to do here...we're waiting on a remote
3359
- // revoke_and_ack before we can update the commitment
3360
- // transaction. The Channel will automatically handle
3361
- // building the update_fail_htlc and commitment_signed
3362
- // messages when we can.
3363
- // We don't need any kind of timer here as they should fail
3364
- // the channel onto the chain if they can't get our
3365
- // update_fail_htlc in time, it's not our problem.
3328
+ if let Err ( e) = chan. get_mut ( ) . queue_fail_htlc (
3329
+ htlc_id, err_packet, & self . logger
3330
+ ) {
3331
+ if let ChannelError :: Ignore ( msg) = e {
3332
+ log_trace ! ( self . logger, "Failed to fail HTLC with ID {} backwards to short_id {}: {}" , htlc_id, short_chan_id, msg) ;
3333
+ } else {
3334
+ panic ! ( "Stated return value requirements in queue_fail_htlc() were not met" ) ;
3366
3335
}
3336
+ // fail-backs are best-effort, we probably already have one
3337
+ // pending, and if not that's OK, if not, the channel is on
3338
+ // the chain and sending the HTLC-Timeout is their problem.
3339
+ continue ;
3367
3340
}
3368
3341
} ,
3369
3342
}
3370
3343
}
3371
-
3372
- if !add_htlc_msgs. is_empty ( ) || !fail_htlc_msgs. is_empty ( ) {
3373
- let ( commitment_msg, monitor_update) = match chan. get_mut ( ) . send_commitment ( & self . logger ) {
3374
- Ok ( res) => res,
3375
- Err ( e) => {
3376
- // We surely failed send_commitment due to bad keys, in that case
3377
- // close channel and then send error message to peer.
3378
- let counterparty_node_id = chan. get ( ) . get_counterparty_node_id ( ) ;
3379
- let err: Result < ( ) , _ > = match e {
3380
- ChannelError :: Ignore ( _) | ChannelError :: Warn ( _) => {
3381
- panic ! ( "Stated return value requirements in send_commitment() were not met" ) ;
3382
- }
3383
- ChannelError :: Close ( msg) => {
3384
- log_trace ! ( self . logger, "Closing channel {} due to Close-required error: {}" , log_bytes!( chan. key( ) [ ..] ) , msg) ;
3385
- let mut channel = remove_channel ! ( self , chan) ;
3386
- // ChannelClosed event is generated by handle_error for us.
3387
- Err ( MsgHandleErrInternal :: from_finish_shutdown ( msg, channel. channel_id ( ) , channel. get_user_id ( ) , channel. force_shutdown ( true ) , self . get_channel_update_for_broadcast ( & channel) . ok ( ) ) )
3388
- } ,
3389
- } ;
3390
- handle_errors. push ( ( counterparty_node_id, err) ) ;
3391
- continue ;
3392
- }
3393
- } ;
3394
- match self . chain_monitor . update_channel ( chan. get ( ) . get_funding_txo ( ) . unwrap ( ) , monitor_update) {
3395
- ChannelMonitorUpdateStatus :: Completed => { } ,
3396
- e => {
3397
- handle_errors. push ( ( chan. get ( ) . get_counterparty_node_id ( ) , handle_monitor_update_res ! ( self , e, chan, RAACommitmentOrder :: CommitmentFirst , false , true ) ) ) ;
3398
- continue ;
3399
- }
3400
- }
3401
- log_debug ! ( self . logger, "Forwarding HTLCs resulted in a commitment update with {} HTLCs added and {} HTLCs failed for channel {}" ,
3402
- add_htlc_msgs. len( ) , fail_htlc_msgs. len( ) , log_bytes!( chan. get( ) . channel_id( ) ) ) ;
3403
- channel_state. pending_msg_events . push ( events:: MessageSendEvent :: UpdateHTLCs {
3404
- node_id : chan. get ( ) . get_counterparty_node_id ( ) ,
3405
- updates : msgs:: CommitmentUpdate {
3406
- update_add_htlcs : add_htlc_msgs,
3407
- update_fulfill_htlcs : Vec :: new ( ) ,
3408
- update_fail_htlcs : fail_htlc_msgs,
3409
- update_fail_malformed_htlcs : Vec :: new ( ) ,
3410
- update_fee : None ,
3411
- commitment_signed : commitment_msg,
3412
- } ,
3413
- } ) ;
3414
- }
3415
3344
}
3416
3345
}
3417
3346
} else {
@@ -3615,9 +3544,11 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
3615
3544
}
3616
3545
self . forward_htlcs ( & mut phantom_receives) ;
3617
3546
3618
- for ( counterparty_node_id, err) in handle_errors. drain ( ..) {
3619
- let _ = handle_error ! ( self , err, counterparty_node_id) ;
3620
- }
3547
+ // Freeing the holding cell here is relatively redundant - in practice we'll do it when we
3548
+ // next get a `get_and_clear_pending_msg_events` call, but some tests rely on it, and it's
3549
+ // nice to do the work now if we can rather than while we're trying to get messages in the
3550
+ // network stack.
3551
+ self . check_free_holding_cells ( ) ;
3621
3552
3622
3553
if new_events. is_empty ( ) { return }
3623
3554
let mut events = self . pending_events . lock ( ) . unwrap ( ) ;
@@ -5568,11 +5499,6 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
5568
5499
/// Check the holding cell in each channel and free any pending HTLCs in them if possible.
5569
5500
/// Returns whether there were any updates such as if pending HTLCs were freed or a monitor
5570
5501
/// update was applied.
5571
- ///
5572
- /// This should only apply to HTLCs which were added to the holding cell because we were
5573
- /// waiting on a monitor update to finish. In that case, we don't want to free the holding cell
5574
- /// directly in `channel_monitor_updated` as it may introduce deadlocks calling back into user
5575
- /// code to inform them of a channel monitor update.
5576
5502
fn check_free_holding_cells ( & self ) -> bool {
5577
5503
let mut has_monitor_update = false ;
5578
5504
let mut failed_htlcs = Vec :: new ( ) ;
0 commit comments