Skip to content

Commit 595ee0f

Browse files
Make process_pending_htlc_forwards more readable
Refactor `process_pending_htlc_forwards` to ensure that both branches that fails `pending_forwards` are placed next to eachother for improved readability.
1 parent 919ec65 commit 595ee0f

File tree

1 file changed

+117
-114
lines changed

1 file changed

+117
-114
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 117 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -3212,131 +3212,134 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
32123212
continue;
32133213
}
32143214
};
3215-
if let hash_map::Entry::Occupied(mut chan) = channel_state.by_id.entry(forward_chan_id) {
3216-
let mut add_htlc_msgs = Vec::new();
3217-
let mut fail_htlc_msgs = Vec::new();
3218-
for forward_info in pending_forwards.drain(..) {
3219-
match forward_info {
3220-
HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info: PendingHTLCInfo {
3221-
routing: PendingHTLCRouting::Forward {
3222-
onion_packet, ..
3223-
}, incoming_shared_secret, payment_hash, amt_to_forward, outgoing_cltv_value },
3224-
prev_funding_outpoint } => {
3225-
log_trace!(self.logger, "Adding HTLC from short id {} with payment_hash {} to channel with short id {} after delay", prev_short_channel_id, log_bytes!(payment_hash.0), short_chan_id);
3226-
let htlc_source = HTLCSource::PreviousHopData(HTLCPreviousHopData {
3227-
short_channel_id: prev_short_channel_id,
3228-
outpoint: prev_funding_outpoint,
3229-
htlc_id: prev_htlc_id,
3230-
incoming_packet_shared_secret: incoming_shared_secret,
3231-
// Phantom payments are only PendingHTLCRouting::Receive.
3232-
phantom_shared_secret: None,
3233-
});
3234-
match chan.get_mut().send_htlc(amt_to_forward, payment_hash, outgoing_cltv_value, htlc_source.clone(), onion_packet, &self.logger) {
3235-
Err(e) => {
3236-
if let ChannelError::Ignore(msg) = e {
3237-
log_trace!(self.logger, "Failed to forward HTLC with payment_hash {}: {}", log_bytes!(payment_hash.0), msg);
3238-
} else {
3239-
panic!("Stated return value requirements in send_htlc() were not met");
3240-
}
3241-
let (failure_code, data) = self.get_htlc_temp_fail_err_and_data(0x1000|7, short_chan_id, chan.get());
3242-
failed_forwards.push((htlc_source, payment_hash,
3243-
HTLCFailReason::Reason { failure_code, data },
3244-
HTLCDestination::NextHopChannel { node_id: Some(chan.get().get_counterparty_node_id()), channel_id: forward_chan_id }
3245-
));
3246-
continue;
3247-
},
3248-
Ok(update_add) => {
3249-
match update_add {
3250-
Some(msg) => { add_htlc_msgs.push(msg); },
3251-
None => {
3252-
// Nothing to do here...we're waiting on a remote
3253-
// revoke_and_ack before we can add anymore HTLCs. The Channel
3254-
// will automatically handle building the update_add_htlc and
3255-
// commitment_signed messages when we can.
3256-
// TODO: Do some kind of timer to set the channel as !is_live()
3257-
// as we don't really want others relying on us relaying through
3258-
// this channel currently :/.
3215+
match channel_state.by_id.entry(forward_chan_id) {
3216+
hash_map::Entry::Vacant(_) => {
3217+
fail_pending_forwards!();
3218+
continue;
3219+
},
3220+
hash_map::Entry::Occupied(mut chan) => {
3221+
let mut add_htlc_msgs = Vec::new();
3222+
let mut fail_htlc_msgs = Vec::new();
3223+
for forward_info in pending_forwards.drain(..) {
3224+
match forward_info {
3225+
HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info: PendingHTLCInfo {
3226+
routing: PendingHTLCRouting::Forward {
3227+
onion_packet, ..
3228+
}, incoming_shared_secret, payment_hash, amt_to_forward, outgoing_cltv_value },
3229+
prev_funding_outpoint } => {
3230+
log_trace!(self.logger, "Adding HTLC from short id {} with payment_hash {} to channel with short id {} after delay", prev_short_channel_id, log_bytes!(payment_hash.0), short_chan_id);
3231+
let htlc_source = HTLCSource::PreviousHopData(HTLCPreviousHopData {
3232+
short_channel_id: prev_short_channel_id,
3233+
outpoint: prev_funding_outpoint,
3234+
htlc_id: prev_htlc_id,
3235+
incoming_packet_shared_secret: incoming_shared_secret,
3236+
// Phantom payments are only PendingHTLCRouting::Receive.
3237+
phantom_shared_secret: None,
3238+
});
3239+
match chan.get_mut().send_htlc(amt_to_forward, payment_hash, outgoing_cltv_value, htlc_source.clone(), onion_packet, &self.logger) {
3240+
Err(e) => {
3241+
if let ChannelError::Ignore(msg) = e {
3242+
log_trace!(self.logger, "Failed to forward HTLC with payment_hash {}: {}", log_bytes!(payment_hash.0), msg);
3243+
} else {
3244+
panic!("Stated return value requirements in send_htlc() were not met");
3245+
}
3246+
let (failure_code, data) = self.get_htlc_temp_fail_err_and_data(0x1000|7, short_chan_id, chan.get());
3247+
failed_forwards.push((htlc_source, payment_hash,
3248+
HTLCFailReason::Reason { failure_code, data },
3249+
HTLCDestination::NextHopChannel { node_id: Some(chan.get().get_counterparty_node_id()), channel_id: forward_chan_id }
3250+
));
3251+
continue;
3252+
},
3253+
Ok(update_add) => {
3254+
match update_add {
3255+
Some(msg) => { add_htlc_msgs.push(msg); },
3256+
None => {
3257+
// Nothing to do here...we're waiting on a remote
3258+
// revoke_and_ack before we can add anymore HTLCs. The Channel
3259+
// will automatically handle building the update_add_htlc and
3260+
// commitment_signed messages when we can.
3261+
// TODO: Do some kind of timer to set the channel as !is_live()
3262+
// as we don't really want others relying on us relaying through
3263+
// this channel currently :/.
3264+
}
32593265
}
32603266
}
32613267
}
3262-
}
3263-
},
3264-
HTLCForwardInfo::AddHTLC { .. } => {
3265-
panic!("short_channel_id != 0 should imply any pending_forward entries are of type Forward");
3266-
},
3267-
HTLCForwardInfo::FailHTLC { htlc_id, err_packet } => {
3268-
log_trace!(self.logger, "Failing HTLC back to channel with short id {} (backward HTLC ID {}) after delay", short_chan_id, htlc_id);
3269-
match chan.get_mut().get_update_fail_htlc(htlc_id, err_packet, &self.logger) {
3270-
Err(e) => {
3271-
if let ChannelError::Ignore(msg) = e {
3272-
log_trace!(self.logger, "Failed to fail HTLC with ID {} backwards to short_id {}: {}", htlc_id, short_chan_id, msg);
3273-
} else {
3274-
panic!("Stated return value requirements in get_update_fail_htlc() were not met");
3268+
},
3269+
HTLCForwardInfo::AddHTLC { .. } => {
3270+
panic!("short_channel_id != 0 should imply any pending_forward entries are of type Forward");
3271+
},
3272+
HTLCForwardInfo::FailHTLC { htlc_id, err_packet } => {
3273+
log_trace!(self.logger, "Failing HTLC back to channel with short id {} (backward HTLC ID {}) after delay", short_chan_id, htlc_id);
3274+
match chan.get_mut().get_update_fail_htlc(htlc_id, err_packet, &self.logger) {
3275+
Err(e) => {
3276+
if let ChannelError::Ignore(msg) = e {
3277+
log_trace!(self.logger, "Failed to fail HTLC with ID {} backwards to short_id {}: {}", htlc_id, short_chan_id, msg);
3278+
} else {
3279+
panic!("Stated return value requirements in get_update_fail_htlc() were not met");
3280+
}
3281+
// fail-backs are best-effort, we probably already have one
3282+
// pending, and if not that's OK, if not, the channel is on
3283+
// the chain and sending the HTLC-Timeout is their problem.
3284+
continue;
3285+
},
3286+
Ok(Some(msg)) => { fail_htlc_msgs.push(msg); },
3287+
Ok(None) => {
3288+
// Nothing to do here...we're waiting on a remote
3289+
// revoke_and_ack before we can update the commitment
3290+
// transaction. The Channel will automatically handle
3291+
// building the update_fail_htlc and commitment_signed
3292+
// messages when we can.
3293+
// We don't need any kind of timer here as they should fail
3294+
// the channel onto the chain if they can't get our
3295+
// update_fail_htlc in time, it's not our problem.
32753296
}
3276-
// fail-backs are best-effort, we probably already have one
3277-
// pending, and if not that's OK, if not, the channel is on
3278-
// the chain and sending the HTLC-Timeout is their problem.
3279-
continue;
3280-
},
3281-
Ok(Some(msg)) => { fail_htlc_msgs.push(msg); },
3282-
Ok(None) => {
3283-
// Nothing to do here...we're waiting on a remote
3284-
// revoke_and_ack before we can update the commitment
3285-
// transaction. The Channel will automatically handle
3286-
// building the update_fail_htlc and commitment_signed
3287-
// messages when we can.
3288-
// We don't need any kind of timer here as they should fail
3289-
// the channel onto the chain if they can't get our
3290-
// update_fail_htlc in time, it's not our problem.
32913297
}
3292-
}
3293-
},
3298+
},
3299+
}
32943300
}
3295-
}
32963301

3297-
if !add_htlc_msgs.is_empty() || !fail_htlc_msgs.is_empty() {
3298-
let (commitment_msg, monitor_update) = match chan.get_mut().send_commitment(&self.logger) {
3299-
Ok(res) => res,
3300-
Err(e) => {
3301-
// We surely failed send_commitment due to bad keys, in that case
3302-
// close channel and then send error message to peer.
3303-
let counterparty_node_id = chan.get().get_counterparty_node_id();
3304-
let err: Result<(), _> = match e {
3305-
ChannelError::Ignore(_) | ChannelError::Warn(_) => {
3306-
panic!("Stated return value requirements in send_commitment() were not met");
3307-
}
3308-
ChannelError::Close(msg) => {
3309-
log_trace!(self.logger, "Closing channel {} due to Close-required error: {}", log_bytes!(chan.key()[..]), msg);
3310-
let mut channel = remove_channel!(self, chan);
3311-
// ChannelClosed event is generated by handle_error for us.
3312-
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()))
3313-
},
3314-
};
3315-
handle_errors.push((counterparty_node_id, err));
3302+
if !add_htlc_msgs.is_empty() || !fail_htlc_msgs.is_empty() {
3303+
let (commitment_msg, monitor_update) = match chan.get_mut().send_commitment(&self.logger) {
3304+
Ok(res) => res,
3305+
Err(e) => {
3306+
// We surely failed send_commitment due to bad keys, in that case
3307+
// close channel and then send error message to peer.
3308+
let counterparty_node_id = chan.get().get_counterparty_node_id();
3309+
let err: Result<(), _> = match e {
3310+
ChannelError::Ignore(_) | ChannelError::Warn(_) => {
3311+
panic!("Stated return value requirements in send_commitment() were not met");
3312+
}
3313+
ChannelError::Close(msg) => {
3314+
log_trace!(self.logger, "Closing channel {} due to Close-required error: {}", log_bytes!(chan.key()[..]), msg);
3315+
let mut channel = remove_channel!(self, chan);
3316+
// ChannelClosed event is generated by handle_error for us.
3317+
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()))
3318+
},
3319+
};
3320+
handle_errors.push((counterparty_node_id, err));
3321+
continue;
3322+
}
3323+
};
3324+
if let Err(e) = self.chain_monitor.update_channel(chan.get().get_funding_txo().unwrap(), monitor_update) {
3325+
handle_errors.push((chan.get().get_counterparty_node_id(), handle_monitor_err!(self, e, chan, RAACommitmentOrder::CommitmentFirst, false, true)));
33163326
continue;
33173327
}
3318-
};
3319-
if let Err(e) = self.chain_monitor.update_channel(chan.get().get_funding_txo().unwrap(), monitor_update) {
3320-
handle_errors.push((chan.get().get_counterparty_node_id(), handle_monitor_err!(self, e, chan, RAACommitmentOrder::CommitmentFirst, false, true)));
3321-
continue;
3328+
log_debug!(self.logger, "Forwarding HTLCs resulted in a commitment update with {} HTLCs added and {} HTLCs failed for channel {}",
3329+
add_htlc_msgs.len(), fail_htlc_msgs.len(), log_bytes!(chan.get().channel_id()));
3330+
channel_state.pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs {
3331+
node_id: chan.get().get_counterparty_node_id(),
3332+
updates: msgs::CommitmentUpdate {
3333+
update_add_htlcs: add_htlc_msgs,
3334+
update_fulfill_htlcs: Vec::new(),
3335+
update_fail_htlcs: fail_htlc_msgs,
3336+
update_fail_malformed_htlcs: Vec::new(),
3337+
update_fee: None,
3338+
commitment_signed: commitment_msg,
3339+
},
3340+
});
33223341
}
3323-
log_debug!(self.logger, "Forwarding HTLCs resulted in a commitment update with {} HTLCs added and {} HTLCs failed for channel {}",
3324-
add_htlc_msgs.len(), fail_htlc_msgs.len(), log_bytes!(chan.get().channel_id()));
3325-
channel_state.pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs {
3326-
node_id: chan.get().get_counterparty_node_id(),
3327-
updates: msgs::CommitmentUpdate {
3328-
update_add_htlcs: add_htlc_msgs,
3329-
update_fulfill_htlcs: Vec::new(),
3330-
update_fail_htlcs: fail_htlc_msgs,
3331-
update_fail_malformed_htlcs: Vec::new(),
3332-
update_fee: None,
3333-
commitment_signed: commitment_msg,
3334-
},
3335-
});
33363342
}
3337-
} else {
3338-
fail_pending_forwards!();
3339-
continue;
33403343
}
33413344
} else {
33423345
for forward_info in pending_forwards.drain(..) {

0 commit comments

Comments
 (0)