Skip to content

Commit 728ab01

Browse files
committed
ln/refactor: remove err_str where LocalHTLCFailureReason sufficient
In some places where we have LocalHTLCFailureReason, the string we're passing around is redundant. This isn't the case in all uses of LocalHTLCFailureReason, for example InboundHTLCError provides additional information about payload and blinding related errors that isn't captured in LocalHTLCFailureReason. In these cases we keep the error string, tolerating some duplication.
1 parent 29f0ccb commit 728ab01

File tree

4 files changed

+34
-46
lines changed

4 files changed

+34
-46
lines changed

lightning/src/ln/channel.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7838,17 +7838,15 @@ impl<SP: Deref> FundedChannel<SP> where
78387838

78397839
fn internal_htlc_satisfies_config(
78407840
&self, htlc: &msgs::UpdateAddHTLC, amt_to_forward: u64, outgoing_cltv_value: u32, config: &ChannelConfig,
7841-
) -> Result<(), (&'static str, LocalHTLCFailureReason)> {
7841+
) -> Result<(), LocalHTLCFailureReason> {
78427842
let fee = amt_to_forward.checked_mul(config.forwarding_fee_proportional_millionths as u64)
78437843
.and_then(|prop_fee| (prop_fee / 1000000).checked_add(config.forwarding_fee_base_msat as u64));
78447844
if fee.is_none() || htlc.amount_msat < fee.unwrap() ||
78457845
(htlc.amount_msat - fee.unwrap()) < amt_to_forward {
7846-
return Err(("Prior hop has deviated from specified fees parameters or origin node has obsolete ones",
7847-
LocalHTLCFailureReason::FeeInsufficient));
7846+
return Err(LocalHTLCFailureReason::FeeInsufficient);
78487847
}
78497848
if (htlc.cltv_expiry as u64) < outgoing_cltv_value as u64 + config.cltv_expiry_delta as u64 {
7850-
return Err(("Forwarding node has tampered with the intended HTLC values or origin node has an obsolete cltv_expiry_delta",
7851-
LocalHTLCFailureReason::IncorrectCLTVExpiry));
7849+
return Err(LocalHTLCFailureReason::IncorrectCLTVExpiry);
78527850
}
78537851
Ok(())
78547852
}
@@ -7858,7 +7856,7 @@ impl<SP: Deref> FundedChannel<SP> where
78587856
/// unsuccessful, falls back to the previous one if one exists.
78597857
pub fn htlc_satisfies_config(
78607858
&self, htlc: &msgs::UpdateAddHTLC, amt_to_forward: u64, outgoing_cltv_value: u32,
7861-
) -> Result<(), (&'static str, LocalHTLCFailureReason)> {
7859+
) -> Result<(), LocalHTLCFailureReason> {
78627860
self.internal_htlc_satisfies_config(&htlc, amt_to_forward, outgoing_cltv_value, &self.context.config())
78637861
.or_else(|err| {
78647862
if let Some(prev_config) = self.context.prev_config() {
@@ -7873,13 +7871,13 @@ impl<SP: Deref> FundedChannel<SP> where
78737871
/// this function determines whether to fail the HTLC, or forward / claim it.
78747872
pub fn can_accept_incoming_htlc<F: Deref, L: Deref>(
78757873
&self, msg: &msgs::UpdateAddHTLC, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: L
7876-
) -> Result<(), (&'static str, LocalHTLCFailureReason)>
7874+
) -> Result<(), LocalHTLCFailureReason>
78777875
where
78787876
F::Target: FeeEstimator,
78797877
L::Target: Logger
78807878
{
78817879
if self.context.channel_state.is_local_shutdown_sent() {
7882-
return Err(("Shutdown was already sent", LocalHTLCFailureReason::ChannelClosed))
7880+
return Err(LocalHTLCFailureReason::ChannelClosed)
78837881
}
78847882

78857883
let dust_exposure_limiting_feerate = self.context.get_dust_exposure_limiting_feerate(&fee_estimator);
@@ -7890,8 +7888,7 @@ impl<SP: Deref> FundedChannel<SP> where
78907888
// Note that the total dust exposure includes both the dust HTLCs and the excess mining fees of the counterparty commitment transaction
78917889
log_info!(logger, "Cannot accept value that would put our total dust exposure at {} over the limit {} on counterparty commitment tx",
78927890
on_counterparty_tx_dust_htlc_exposure_msat, max_dust_htlc_exposure_msat);
7893-
return Err(("Exceeded our total dust exposure limit on counterparty commitment tx",
7894-
LocalHTLCFailureReason::DustLimitCounterparty))
7891+
return Err(LocalHTLCFailureReason::DustLimitCounterparty)
78957892
}
78967893
let htlc_success_dust_limit = if self.funding.get_channel_type().supports_anchors_zero_fee_htlc_tx() {
78977894
0
@@ -7905,8 +7902,7 @@ impl<SP: Deref> FundedChannel<SP> where
79057902
if on_holder_tx_dust_htlc_exposure_msat > max_dust_htlc_exposure_msat {
79067903
log_info!(logger, "Cannot accept value that would put our exposure to dust HTLCs at {} over the limit {} on holder commitment tx",
79077904
on_holder_tx_dust_htlc_exposure_msat, max_dust_htlc_exposure_msat);
7908-
return Err(("Exceeded our dust exposure limit on holder commitment tx",
7909-
LocalHTLCFailureReason::DustLimitHolder))
7905+
return Err(LocalHTLCFailureReason::DustLimitHolder)
79107906
}
79117907
}
79127908

@@ -7944,7 +7940,7 @@ impl<SP: Deref> FundedChannel<SP> where
79447940
}
79457941
if pending_remote_value_msat.saturating_sub(self.funding.holder_selected_channel_reserve_satoshis * 1000).saturating_sub(anchor_outputs_value_msat) < remote_fee_cost_incl_stuck_buffer_msat {
79467942
log_info!(logger, "Attempting to fail HTLC due to fee spike buffer violation in channel {}. Rebalancing is required.", &self.context.channel_id());
7947-
return Err(("Fee spike buffer violation", LocalHTLCFailureReason::FeeSpikeBuffer));
7943+
return Err(LocalHTLCFailureReason::FeeSpikeBuffer);
79487944
}
79497945
}
79507946

lightning/src/ln/channelmanager.rs

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4343,25 +4343,22 @@ where
43434343

43444344
fn can_forward_htlc_to_outgoing_channel(
43454345
&self, chan: &mut FundedChannel<SP>, msg: &msgs::UpdateAddHTLC, next_packet: &NextPacketDetails
4346-
) -> Result<(), (&'static str, LocalHTLCFailureReason)> {
4346+
) -> Result<(), LocalHTLCFailureReason> {
43474347
if !chan.context.should_announce() && !self.default_configuration.accept_forwards_to_priv_channels {
43484348
// Note that the behavior here should be identical to the above block - we
43494349
// should NOT reveal the existence or non-existence of a private channel if
43504350
// we don't allow forwards outbound over them.
4351-
return Err(("Refusing to forward to a private channel based on our config.",
4352-
LocalHTLCFailureReason::PrivateChannelForward));
4351+
return Err(LocalHTLCFailureReason::PrivateChannelForward);
43534352
}
43544353
if let HopConnector::ShortChannelId(outgoing_scid) = next_packet.outgoing_connector {
43554354
if chan.funding.get_channel_type().supports_scid_privacy() && outgoing_scid != chan.context.outbound_scid_alias() {
43564355
// `option_scid_alias` (referred to in LDK as `scid_privacy`) means
43574356
// "refuse to forward unless the SCID alias was used", so we pretend
43584357
// we don't have the channel here.
4359-
return Err(("Refusing to forward over real channel SCID as our counterparty requested.",
4360-
LocalHTLCFailureReason::RealSCIDForward));
4358+
return Err(LocalHTLCFailureReason::RealSCIDForward);
43614359
}
43624360
} else {
4363-
return Err(("Cannot forward by Node ID without SCID.",
4364-
LocalHTLCFailureReason::InvalidTrampolineForward));
4361+
return Err(LocalHTLCFailureReason::InvalidTrampolineForward);
43654362
}
43664363

43674364
// Note that we could technically not return an error yet here and just hope
@@ -4371,16 +4368,13 @@ where
43714368
// on a small/per-node/per-channel scale.
43724369
if !chan.context.is_live() {
43734370
if !chan.context.is_enabled() {
4374-
return Err(("Forwarding channel has been disconnected for some time.",
4375-
LocalHTLCFailureReason::ChannelDisabled));
4371+
return Err(LocalHTLCFailureReason::ChannelDisabled);
43764372
} else {
4377-
return Err(("Forwarding channel is not in a ready state.",
4378-
LocalHTLCFailureReason::ChannelNotReady));
4373+
return Err(LocalHTLCFailureReason::ChannelNotReady);
43794374
}
43804375
}
43814376
if next_packet.outgoing_amt_msat < chan.context.get_counterparty_htlc_minimum_msat() {
4382-
return Err(("HTLC amount was below the htlc_minimum_msat",
4383-
LocalHTLCFailureReason::AmountBelowMinimum));
4377+
return Err(LocalHTLCFailureReason::AmountBelowMinimum);
43844378
}
43854379
chan.htlc_satisfies_config(msg, next_packet.outgoing_amt_msat, next_packet.outgoing_cltv_value)?;
43864380

@@ -4411,12 +4405,11 @@ where
44114405

44124406
fn can_forward_htlc(
44134407
&self, msg: &msgs::UpdateAddHTLC, next_packet_details: &NextPacketDetails
4414-
) -> Result<(), (&'static str, LocalHTLCFailureReason)> {
4408+
) -> Result<(), LocalHTLCFailureReason> {
44154409
let outgoing_scid = match next_packet_details.outgoing_connector {
44164410
HopConnector::ShortChannelId(scid) => scid,
44174411
HopConnector::Trampoline(_) => {
4418-
return Err(("Cannot forward by Node ID without SCID.",
4419-
LocalHTLCFailureReason::InvalidTrampolineForward));
4412+
return Err(LocalHTLCFailureReason::InvalidTrampolineForward);
44204413
}
44214414
};
44224415
match self.do_funded_channel_callback(outgoing_scid, |chan: &mut FundedChannel<SP>| {
@@ -4431,8 +4424,7 @@ where
44314424
fake_scid::is_valid_intercept(&self.fake_scid_rand_bytes, outgoing_scid, &self.chain_hash)) ||
44324425
fake_scid::is_valid_phantom(&self.fake_scid_rand_bytes, outgoing_scid, &self.chain_hash)
44334426
{} else {
4434-
return Err(("Don't have available channel for forwarding as requested.",
4435-
LocalHTLCFailureReason::UnknownNextPeer));
4427+
return Err(LocalHTLCFailureReason::UnknownNextPeer);
44364428
}
44374429
}
44384430
}
@@ -4444,7 +4436,7 @@ where
44444436
}
44454437

44464438
fn htlc_failure_from_update_add_err(
4447-
&self, msg: &msgs::UpdateAddHTLC, counterparty_node_id: &PublicKey, err_msg: &'static str,
4439+
&self, msg: &msgs::UpdateAddHTLC, counterparty_node_id: &PublicKey,
44484440
reason: LocalHTLCFailureReason, is_intro_node_blinded_forward: bool,
44494441
shared_secret: &[u8; 32]
44504442
) -> HTLCFailureMsg {
@@ -4468,7 +4460,7 @@ where
44684460

44694461
log_info!(
44704462
WithContext::from(&self.logger, Some(*counterparty_node_id), Some(msg.channel_id), Some(msg.payment_hash)),
4471-
"Failed to accept/forward incoming HTLC: {}", err_msg
4463+
"Failed to accept/forward incoming HTLC: {:?} - {}", reason, reason,
44724464
);
44734465
// If `msg.blinding_point` is set, we must always fail with malformed.
44744466
if msg.blinding_point.is_some() {
@@ -5810,9 +5802,9 @@ where
58105802
)
58115803
}) {
58125804
Some(Ok(_)) => {},
5813-
Some(Err((err, reason))) => {
5805+
Some(Err(reason)) => {
58145806
let htlc_fail = self.htlc_failure_from_update_add_err(
5815-
&update_add_htlc, &incoming_counterparty_node_id, err, reason,
5807+
&update_add_htlc, &incoming_counterparty_node_id, reason,
58165808
is_intro_node_blinded_forward, &shared_secret,
58175809
);
58185810
let htlc_destination = get_failed_htlc_destination(outgoing_scid_opt, update_add_htlc.payment_hash);
@@ -5825,11 +5817,11 @@ where
58255817

58265818
// Now process the HTLC on the outgoing channel if it's a forward.
58275819
if let Some(next_packet_details) = next_packet_details_opt.as_ref() {
5828-
if let Err((err, reason)) = self.can_forward_htlc(
5820+
if let Err(reason) = self.can_forward_htlc(
58295821
&update_add_htlc, next_packet_details
58305822
) {
58315823
let htlc_fail = self.htlc_failure_from_update_add_err(
5832-
&update_add_htlc, &incoming_counterparty_node_id, err, reason,
5824+
&update_add_htlc, &incoming_counterparty_node_id, reason,
58335825
is_intro_node_blinded_forward, &shared_secret,
58345826
);
58355827
let htlc_destination = get_failed_htlc_destination(outgoing_scid_opt, update_add_htlc.payment_hash);

lightning/src/ln/onion_payment.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -453,11 +453,11 @@ where
453453
}),
454454
};
455455

456-
if let Err((err_msg, reason)) = check_incoming_htlc_cltv(
456+
if let Err(reason) = check_incoming_htlc_cltv(
457457
cur_height, outgoing_cltv_value, msg.cltv_expiry,
458458
) {
459459
return Err(InboundHTLCErr {
460-
msg: err_msg,
460+
msg: "incoming cltv check failed",
461461
reason,
462462
err_data: Vec::new(),
463463
});
@@ -601,19 +601,18 @@ where
601601

602602
pub(super) fn check_incoming_htlc_cltv(
603603
cur_height: u32, outgoing_cltv_value: u32, cltv_expiry: u32
604-
) -> Result<(), (&'static str, LocalHTLCFailureReason)> {
604+
) -> Result<(), LocalHTLCFailureReason> {
605605
if (cltv_expiry as u64) < (outgoing_cltv_value) as u64 + MIN_CLTV_EXPIRY_DELTA as u64 {
606-
return Err(("Forwarding node has tampered with the intended HTLC values or origin node has an obsolete cltv_expiry_delta",
607-
LocalHTLCFailureReason::IncorrectCLTVExpiry));
606+
return Err(LocalHTLCFailureReason::IncorrectCLTVExpiry);
608607
}
609608
// Theoretically, channel counterparty shouldn't send us a HTLC expiring now,
610609
// but we want to be robust wrt to counterparty packet sanitization (see
611610
// HTLC_FAIL_BACK_BUFFER rationale).
612611
if cltv_expiry <= cur_height + HTLC_FAIL_BACK_BUFFER as u32 {
613-
return Err(("CLTV expiry is too close", LocalHTLCFailureReason::CLTVExpiryTooSoon));
612+
return Err(LocalHTLCFailureReason::CLTVExpiryTooSoon);
614613
}
615614
if cltv_expiry > cur_height + CLTV_FAR_FAR_AWAY as u32 {
616-
return Err(("CLTV expiry is too far in the future", LocalHTLCFailureReason::CLTVExpiryTooFar));
615+
return Err(LocalHTLCFailureReason::CLTVExpiryTooFar);
617616
}
618617
// If the HTLC expires ~now, don't bother trying to forward it to our
619618
// counterparty. They should fail it anyway, but we don't want to bother with
@@ -624,7 +623,7 @@ pub(super) fn check_incoming_htlc_cltv(
624623
// but there is no need to do that, and since we're a bit conservative with our
625624
// risk threshold it just results in failing to forward payments.
626625
if (outgoing_cltv_value) as u64 <= (cur_height + LATENCY_GRACE_PERIOD_BLOCKS) as u64 {
627-
return Err(("Outgoing CLTV value is too soon", LocalHTLCFailureReason::OutgoingCLTVTooSoon));
626+
return Err(LocalHTLCFailureReason::OutgoingCLTVTooSoon);
628627
}
629628

630629
Ok(())

lightning/src/ln/priv_short_conf_tests.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,8 @@ fn test_inbound_scid_privacy() {
449449
);
450450
check_added_monitors(&nodes[1], 1);
451451

452-
nodes[1].logger.assert_log_regex("lightning::ln::channelmanager", regex::Regex::new(r"Refusing to forward over real channel SCID as our counterparty requested").unwrap(), 1);
452+
nodes[1].logger.assert_log_regex("lightning::ln::channelmanager",
453+
regex::Regex::new(r"Forward using real SCID over aliased channel rejected").unwrap(), 1);
453454

454455
let mut updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
455456
nodes[0].node.handle_update_fail_htlc(nodes[1].node.get_our_node_id(), &updates.update_fail_htlcs[0]);

0 commit comments

Comments
 (0)