Skip to content

Commit 3e3d954

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 a7a6eb8 commit 3e3d954

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

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

0 commit comments

Comments
 (0)