Skip to content

Commit 913daf8

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 1ba7e73 commit 913daf8

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

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

0 commit comments

Comments
 (0)