Skip to content

Commit a38c9c4

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 af85171 commit a38c9c4

File tree

1 file changed

+121
-118
lines changed

1 file changed

+121
-118
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 121 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -3171,134 +3171,137 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
31713171
continue;
31723172
}
31733173
};
3174-
if let hash_map::Entry::Occupied(mut chan) = channel_state.by_id.entry(forward_chan_id) {
3175-
let mut add_htlc_msgs = Vec::new();
3176-
let mut fail_htlc_msgs = Vec::new();
3177-
for forward_info in pending_forwards.drain(..) {
3178-
match forward_info {
3179-
HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info: PendingHTLCInfo {
3180-
routing: PendingHTLCRouting::Forward {
3181-
onion_packet, ..
3182-
}, incoming_shared_secret, payment_hash, amt_to_forward, outgoing_cltv_value },
3183-
prev_funding_outpoint } => {
3184-
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);
3185-
let htlc_source = HTLCSource::PreviousHopData(HTLCPreviousHopData {
3186-
short_channel_id: prev_short_channel_id,
3187-
outpoint: prev_funding_outpoint,
3188-
htlc_id: prev_htlc_id,
3189-
incoming_packet_shared_secret: incoming_shared_secret,
3190-
// Phantom payments are only PendingHTLCRouting::Receive.
3191-
phantom_shared_secret: None,
3192-
});
3193-
match chan.get_mut().send_htlc(amt_to_forward, payment_hash, outgoing_cltv_value, htlc_source.clone(), onion_packet, &self.logger) {
3194-
Err(e) => {
3195-
if let ChannelError::Ignore(msg) = e {
3196-
log_trace!(self.logger, "Failed to forward HTLC with payment_hash {}: {}", log_bytes!(payment_hash.0), msg);
3197-
} else {
3198-
panic!("Stated return value requirements in send_htlc() were not met");
3199-
}
3200-
let (failure_code, data) = self.get_htlc_temp_fail_err_and_data(0x1000|7, short_chan_id, chan.get());
3201-
failed_forwards.push((htlc_source, payment_hash,
3202-
HTLCFailReason::Reason { failure_code, data },
3203-
HTLCDestination::NextHopChannel { node_id: Some(chan.get().get_counterparty_node_id()), channel_id: forward_chan_id }
3204-
));
3205-
continue;
3206-
},
3207-
Ok(update_add) => {
3208-
match update_add {
3209-
Some(msg) => { add_htlc_msgs.push(msg); },
3210-
None => {
3211-
// Nothing to do here...we're waiting on a remote
3212-
// revoke_and_ack before we can add anymore HTLCs. The Channel
3213-
// will automatically handle building the update_add_htlc and
3214-
// commitment_signed messages when we can.
3215-
// TODO: Do some kind of timer to set the channel as !is_live()
3216-
// as we don't really want others relying on us relaying through
3217-
// this channel currently :/.
3174+
match channel_state.by_id.entry(forward_chan_id) {
3175+
hash_map::Entry::Vacant(_) => {
3176+
forwarding_channel_not_found!();
3177+
continue;
3178+
},
3179+
hash_map::Entry::Occupied(mut chan) => {
3180+
let mut add_htlc_msgs = Vec::new();
3181+
let mut fail_htlc_msgs = Vec::new();
3182+
for forward_info in pending_forwards.drain(..) {
3183+
match forward_info {
3184+
HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info: PendingHTLCInfo {
3185+
routing: PendingHTLCRouting::Forward {
3186+
onion_packet, ..
3187+
}, incoming_shared_secret, payment_hash, amt_to_forward, outgoing_cltv_value },
3188+
prev_funding_outpoint } => {
3189+
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);
3190+
let htlc_source = HTLCSource::PreviousHopData(HTLCPreviousHopData {
3191+
short_channel_id: prev_short_channel_id,
3192+
outpoint: prev_funding_outpoint,
3193+
htlc_id: prev_htlc_id,
3194+
incoming_packet_shared_secret: incoming_shared_secret,
3195+
// Phantom payments are only PendingHTLCRouting::Receive.
3196+
phantom_shared_secret: None,
3197+
});
3198+
match chan.get_mut().send_htlc(amt_to_forward, payment_hash, outgoing_cltv_value, htlc_source.clone(), onion_packet, &self.logger) {
3199+
Err(e) => {
3200+
if let ChannelError::Ignore(msg) = e {
3201+
log_trace!(self.logger, "Failed to forward HTLC with payment_hash {}: {}", log_bytes!(payment_hash.0), msg);
3202+
} else {
3203+
panic!("Stated return value requirements in send_htlc() were not met");
3204+
}
3205+
let (failure_code, data) = self.get_htlc_temp_fail_err_and_data(0x1000|7, short_chan_id, chan.get());
3206+
failed_forwards.push((htlc_source, payment_hash,
3207+
HTLCFailReason::Reason { failure_code, data },
3208+
HTLCDestination::NextHopChannel { node_id: Some(chan.get().get_counterparty_node_id()), channel_id: forward_chan_id }
3209+
));
3210+
continue;
3211+
},
3212+
Ok(update_add) => {
3213+
match update_add {
3214+
Some(msg) => { add_htlc_msgs.push(msg); },
3215+
None => {
3216+
// Nothing to do here...we're waiting on a remote
3217+
// revoke_and_ack before we can add anymore HTLCs. The Channel
3218+
// will automatically handle building the update_add_htlc and
3219+
// commitment_signed messages when we can.
3220+
// TODO: Do some kind of timer to set the channel as !is_live()
3221+
// as we don't really want others relying on us relaying through
3222+
// this channel currently :/.
3223+
}
32183224
}
32193225
}
32203226
}
3221-
}
3222-
},
3223-
HTLCForwardInfo::AddHTLC { .. } => {
3224-
panic!("short_channel_id != 0 should imply any pending_forward entries are of type Forward");
3225-
},
3226-
HTLCForwardInfo::FailHTLC { htlc_id, err_packet } => {
3227-
log_trace!(self.logger, "Failing HTLC back to channel with short id {} (backward HTLC ID {}) after delay", short_chan_id, htlc_id);
3228-
match chan.get_mut().get_update_fail_htlc(htlc_id, err_packet, &self.logger) {
3229-
Err(e) => {
3230-
if let ChannelError::Ignore(msg) = e {
3231-
log_trace!(self.logger, "Failed to fail HTLC with ID {} backwards to short_id {}: {}", htlc_id, short_chan_id, msg);
3232-
} else {
3233-
panic!("Stated return value requirements in get_update_fail_htlc() were not met");
3227+
},
3228+
HTLCForwardInfo::AddHTLC { .. } => {
3229+
panic!("short_channel_id != 0 should imply any pending_forward entries are of type Forward");
3230+
},
3231+
HTLCForwardInfo::FailHTLC { htlc_id, err_packet } => {
3232+
log_trace!(self.logger, "Failing HTLC back to channel with short id {} (backward HTLC ID {}) after delay", short_chan_id, htlc_id);
3233+
match chan.get_mut().get_update_fail_htlc(htlc_id, err_packet, &self.logger) {
3234+
Err(e) => {
3235+
if let ChannelError::Ignore(msg) = e {
3236+
log_trace!(self.logger, "Failed to fail HTLC with ID {} backwards to short_id {}: {}", htlc_id, short_chan_id, msg);
3237+
} else {
3238+
panic!("Stated return value requirements in get_update_fail_htlc() were not met");
3239+
}
3240+
// fail-backs are best-effort, we probably already have one
3241+
// pending, and if not that's OK, if not, the channel is on
3242+
// the chain and sending the HTLC-Timeout is their problem.
3243+
continue;
3244+
},
3245+
Ok(Some(msg)) => { fail_htlc_msgs.push(msg); },
3246+
Ok(None) => {
3247+
// Nothing to do here...we're waiting on a remote
3248+
// revoke_and_ack before we can update the commitment
3249+
// transaction. The Channel will automatically handle
3250+
// building the update_fail_htlc and commitment_signed
3251+
// messages when we can.
3252+
// We don't need any kind of timer here as they should fail
3253+
// the channel onto the chain if they can't get our
3254+
// update_fail_htlc in time, it's not our problem.
32343255
}
3235-
// fail-backs are best-effort, we probably already have one
3236-
// pending, and if not that's OK, if not, the channel is on
3237-
// the chain and sending the HTLC-Timeout is their problem.
3238-
continue;
3239-
},
3240-
Ok(Some(msg)) => { fail_htlc_msgs.push(msg); },
3241-
Ok(None) => {
3242-
// Nothing to do here...we're waiting on a remote
3243-
// revoke_and_ack before we can update the commitment
3244-
// transaction. The Channel will automatically handle
3245-
// building the update_fail_htlc and commitment_signed
3246-
// messages when we can.
3247-
// We don't need any kind of timer here as they should fail
3248-
// the channel onto the chain if they can't get our
3249-
// update_fail_htlc in time, it's not our problem.
32503256
}
3251-
}
3252-
},
3257+
},
3258+
}
32533259
}
3254-
}
32553260

3256-
if !add_htlc_msgs.is_empty() || !fail_htlc_msgs.is_empty() {
3257-
let (commitment_msg, monitor_update) = match chan.get_mut().send_commitment(&self.logger) {
3258-
Ok(res) => res,
3259-
Err(e) => {
3260-
// We surely failed send_commitment due to bad keys, in that case
3261-
// close channel and then send error message to peer.
3262-
let counterparty_node_id = chan.get().get_counterparty_node_id();
3263-
let err: Result<(), _> = match e {
3264-
ChannelError::Ignore(_) | ChannelError::Warn(_) => {
3265-
panic!("Stated return value requirements in send_commitment() were not met");
3266-
}
3267-
ChannelError::Close(msg) => {
3268-
log_trace!(self.logger, "Closing channel {} due to Close-required error: {}", log_bytes!(chan.key()[..]), msg);
3269-
let mut channel = remove_channel!(self, chan);
3270-
// ChannelClosed event is generated by handle_error for us.
3271-
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()))
3272-
},
3273-
};
3274-
handle_errors.push((counterparty_node_id, err));
3275-
continue;
3276-
}
3277-
};
3278-
match self.chain_monitor.update_channel(chan.get().get_funding_txo().unwrap(), monitor_update) {
3279-
ChannelMonitorUpdateStatus::Completed => {},
3280-
e => {
3281-
handle_errors.push((chan.get().get_counterparty_node_id(), handle_monitor_update_res!(self, e, chan, RAACommitmentOrder::CommitmentFirst, false, true)));
3282-
continue;
3261+
if !add_htlc_msgs.is_empty() || !fail_htlc_msgs.is_empty() {
3262+
let (commitment_msg, monitor_update) = match chan.get_mut().send_commitment(&self.logger) {
3263+
Ok(res) => res,
3264+
Err(e) => {
3265+
// We surely failed send_commitment due to bad keys, in that case
3266+
// close channel and then send error message to peer.
3267+
let counterparty_node_id = chan.get().get_counterparty_node_id();
3268+
let err: Result<(), _> = match e {
3269+
ChannelError::Ignore(_) | ChannelError::Warn(_) => {
3270+
panic!("Stated return value requirements in send_commitment() were not met");
3271+
}
3272+
ChannelError::Close(msg) => {
3273+
log_trace!(self.logger, "Closing channel {} due to Close-required error: {}", log_bytes!(chan.key()[..]), msg);
3274+
let mut channel = remove_channel!(self, chan);
3275+
// ChannelClosed event is generated by handle_error for us.
3276+
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()))
3277+
},
3278+
};
3279+
handle_errors.push((counterparty_node_id, err));
3280+
continue;
3281+
}
3282+
};
3283+
match self.chain_monitor.update_channel(chan.get().get_funding_txo().unwrap(), monitor_update) {
3284+
ChannelMonitorUpdateStatus::Completed => {},
3285+
e => {
3286+
handle_errors.push((chan.get().get_counterparty_node_id(), handle_monitor_update_res!(self, e, chan, RAACommitmentOrder::CommitmentFirst, false, true)));
3287+
continue;
3288+
}
32833289
}
3290+
log_debug!(self.logger, "Forwarding HTLCs resulted in a commitment update with {} HTLCs added and {} HTLCs failed for channel {}",
3291+
add_htlc_msgs.len(), fail_htlc_msgs.len(), log_bytes!(chan.get().channel_id()));
3292+
channel_state.pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs {
3293+
node_id: chan.get().get_counterparty_node_id(),
3294+
updates: msgs::CommitmentUpdate {
3295+
update_add_htlcs: add_htlc_msgs,
3296+
update_fulfill_htlcs: Vec::new(),
3297+
update_fail_htlcs: fail_htlc_msgs,
3298+
update_fail_malformed_htlcs: Vec::new(),
3299+
update_fee: None,
3300+
commitment_signed: commitment_msg,
3301+
},
3302+
});
32843303
}
3285-
log_debug!(self.logger, "Forwarding HTLCs resulted in a commitment update with {} HTLCs added and {} HTLCs failed for channel {}",
3286-
add_htlc_msgs.len(), fail_htlc_msgs.len(), log_bytes!(chan.get().channel_id()));
3287-
channel_state.pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs {
3288-
node_id: chan.get().get_counterparty_node_id(),
3289-
updates: msgs::CommitmentUpdate {
3290-
update_add_htlcs: add_htlc_msgs,
3291-
update_fulfill_htlcs: Vec::new(),
3292-
update_fail_htlcs: fail_htlc_msgs,
3293-
update_fail_malformed_htlcs: Vec::new(),
3294-
update_fee: None,
3295-
commitment_signed: commitment_msg,
3296-
},
3297-
});
32983304
}
3299-
} else {
3300-
forwarding_channel_not_found!();
3301-
continue;
33023305
}
33033306
} else {
33043307
for forward_info in pending_forwards.drain(..) {

0 commit comments

Comments
 (0)