Skip to content

Commit 9e341c8

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 e2a0fc8 commit 9e341c8

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

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

0 commit comments

Comments
 (0)