@@ -3170,7 +3170,6 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
31703170 let mut new_events = Vec :: new ( ) ;
31713171 let mut failed_forwards = Vec :: new ( ) ;
31723172 let mut phantom_receives: Vec < ( u64 , OutPoint , u128 , Vec < ( PendingHTLCInfo , u64 ) > ) > = Vec :: new ( ) ;
3173- let mut handle_errors = Vec :: new ( ) ;
31743173 {
31753174 let mut forward_htlcs = HashMap :: new ( ) ;
31763175 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
32863285 continue ;
32873286 } ,
32883287 hash_map:: Entry :: Occupied ( mut chan) => {
3289- let mut add_htlc_msgs = Vec :: new ( ) ;
3290- let mut fail_htlc_msgs = Vec :: new ( ) ;
32913288 for forward_info in pending_forwards. drain ( ..) {
32923289 match forward_info {
32933290 HTLCForwardInfo :: AddHTLC ( PendingAddHTLCInfo {
@@ -3306,112 +3303,44 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
33063303 // Phantom payments are only PendingHTLCRouting::Receive.
33073304 phantom_shared_secret : None ,
33083305 } ) ;
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_add_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" ) ;
33363314 }
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 ;
33373321 }
33383322 } ,
33393323 HTLCForwardInfo :: AddHTLC { .. } => {
33403324 panic ! ( "short_channel_id != 0 should imply any pending_forward entries are of type Forward" ) ;
33413325 } ,
33423326 HTLCForwardInfo :: FailHTLC { htlc_id, err_packet } => {
33433327 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" ) ;
33663335 }
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 ;
33673340 }
33683341 } ,
33693342 }
33703343 }
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- }
34153344 }
34163345 }
34173346 } else {
@@ -3615,9 +3544,11 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
36153544 }
36163545 self . forward_htlcs ( & mut phantom_receives) ;
36173546
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 ( ) ;
36213552
36223553 if new_events. is_empty ( ) { return }
36233554 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
55685499 /// Check the holding cell in each channel and free any pending HTLCs in them if possible.
55695500 /// Returns whether there were any updates such as if pending HTLCs were freed or a monitor
55705501 /// 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.
55765502 fn check_free_holding_cells ( & self ) -> bool {
55775503 let mut has_monitor_update = false ;
55785504 let mut failed_htlcs = Vec :: new ( ) ;
0 commit comments