Skip to content

Commit fcb2902

Browse files
committed
ln/refactor: move incorrect payment data write to helper function
1 parent 481e5c7 commit fcb2902

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ use crate::types::features::{Bolt12InvoiceFeatures, ChannelFeatures, ChannelType
5757
#[cfg(any(feature = "_test_utils", test))]
5858
use crate::types::features::Bolt11InvoiceFeatures;
5959
use crate::routing::router::{BlindedTail, InFlightHtlcs, Path, Payee, PaymentParameters, RouteParameters, RouteParametersConfig, Router, FixedRouter, Route};
60-
use crate::ln::onion_payment::{check_incoming_htlc_cltv, create_recv_pending_htlc_info, create_fwd_pending_htlc_info, decode_incoming_update_add_htlc_onion, HopConnector, InboundHTLCErr, NextPacketDetails};
60+
use crate::ln::onion_payment::{check_incoming_htlc_cltv, create_recv_pending_htlc_info, create_fwd_pending_htlc_info, decode_incoming_update_add_htlc_onion, HopConnector, InboundHTLCErr, NextPacketDetails, invalid_payment_err_data};
6161
use crate::ln::msgs;
6262
use crate::ln::onion_utils::{self};
6363
use crate::ln::onion_utils::{HTLCFailReason, INVALID_ONION_BLINDING};
@@ -87,6 +87,7 @@ use crate::util::string::UntrustedString;
8787
use crate::util::ser::{BigSize, FixedLengthReader, LengthReadable, Readable, ReadableArgs, MaybeReadable, Writeable, Writer, VecWriter};
8888
use crate::util::logger::{Level, Logger, WithContext};
8989
use crate::util::errors::APIError;
90+
9091
#[cfg(async_payments)] use {
9192
crate::offers::offer::Amount,
9293
crate::offers::static_invoice::{DEFAULT_RELATIVE_EXPIRY as STATIC_INVOICE_DEFAULT_RELATIVE_EXPIRY, StaticInvoice, StaticInvoiceBuilder},
@@ -6262,10 +6263,7 @@ where
62626263
macro_rules! fail_htlc {
62636264
($htlc: expr, $payment_hash: expr) => {
62646265
debug_assert!(!committed_to_claimable);
6265-
let mut htlc_msat_height_data = $htlc.value.to_be_bytes().to_vec();
6266-
htlc_msat_height_data.extend_from_slice(
6267-
&self.best_block.read().unwrap().height.to_be_bytes(),
6268-
);
6266+
let err_data = invalid_payment_err_data($htlc.value, self.best_block.read().unwrap().height);
62696267
failed_forwards.push((HTLCSource::PreviousHopData(HTLCPreviousHopData {
62706268
short_channel_id: $htlc.prev_hop.short_channel_id,
62716269
user_channel_id: $htlc.prev_hop.user_channel_id,
@@ -6278,7 +6276,7 @@ where
62786276
blinded_failure,
62796277
cltv_expiry: Some(cltv_expiry),
62806278
}), payment_hash,
6281-
HTLCFailReason::reason(0x4000 | 15, htlc_msat_height_data),
6279+
HTLCFailReason::reason(0x4000 | 15, err_data),
62826280
HTLCDestination::FailedPayment { payment_hash: $payment_hash },
62836281
));
62846282
continue 'next_forwardable_htlc;
@@ -7231,10 +7229,9 @@ where
72317229
}
72327230
} else {
72337231
for htlc in sources {
7234-
let mut htlc_msat_height_data = htlc.value.to_be_bytes().to_vec();
7235-
htlc_msat_height_data.extend_from_slice(&self.best_block.read().unwrap().height.to_be_bytes());
7232+
let err_data = invalid_payment_err_data(htlc.value, self.best_block.read().unwrap().height);
72367233
let source = HTLCSource::PreviousHopData(htlc.prev_hop);
7237-
let reason = HTLCFailReason::reason(0x4000 | 15, htlc_msat_height_data);
7234+
let reason = HTLCFailReason::reason(0x4000 | 15, err_data);
72387235
let receiver = HTLCDestination::FailedPayment { payment_hash };
72397236
self.fail_htlc_backwards_internal(&source, &payment_hash, &reason, receiver);
72407237
}
@@ -11822,11 +11819,8 @@ where
1182211819
// number of blocks we generally consider it to take to do a commitment update,
1182311820
// just give up on it and fail the HTLC.
1182411821
if height >= htlc.cltv_expiry - HTLC_FAIL_BACK_BUFFER {
11825-
let mut htlc_msat_height_data = htlc.value.to_be_bytes().to_vec();
11826-
htlc_msat_height_data.extend_from_slice(&height.to_be_bytes());
11827-
1182811822
timed_out_htlcs.push((HTLCSource::PreviousHopData(htlc.prev_hop.clone()), payment_hash.clone(),
11829-
HTLCFailReason::reason(0x4000 | 15, htlc_msat_height_data),
11823+
HTLCFailReason::reason(0x4000 | 15, invalid_payment_err_data(htlc.value, height)),
1183011824
HTLCDestination::FailedPayment { payment_hash: payment_hash.clone() }));
1183111825
false
1183211826
} else { true }

lightning/src/ln/onion_payment.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ pub struct InboundHTLCErr {
3636
pub msg: &'static str,
3737
}
3838

39+
/// Writes payment data for invalid or unknown payment error code.
40+
pub (super) fn invalid_payment_err_data(amt_msat: u64, current_height: u32) -> Vec<u8>{
41+
let mut err_data = Vec::with_capacity(12);
42+
err_data.extend_from_slice(&amt_msat.to_be_bytes());
43+
err_data.extend_from_slice(&current_height.to_be_bytes());
44+
err_data
45+
}
46+
3947
fn check_blinded_payment_constraints(
4048
amt_msat: u64, cltv_expiry: u32, constraints: &PaymentConstraints
4149
) -> Result<(), ()> {
@@ -333,11 +341,9 @@ pub(super) fn create_recv_pending_htlc_info(
333341
// payment logic has enough time to fail the HTLC backward before our onchain logic triggers a
334342
// channel closure (see HTLC_FAIL_BACK_BUFFER rationale).
335343
if cltv_expiry <= current_height + HTLC_FAIL_BACK_BUFFER + 1 {
336-
let mut err_data = Vec::with_capacity(12);
337-
err_data.extend_from_slice(&amt_msat.to_be_bytes());
338-
err_data.extend_from_slice(&current_height.to_be_bytes());
339344
return Err(InboundHTLCErr {
340-
err_code: 0x4000 | 15, err_data,
345+
err_code: 0x4000 | 15,
346+
err_data: invalid_payment_err_data(amt_msat, current_height),
341347
msg: "The final CLTV expiry is too soon to handle",
342348
});
343349
}

0 commit comments

Comments
 (0)