Skip to content

Commit 11a4f3e

Browse files
committed
ln/fix: return incorrect payment details for incorrect keysend preimage
Per: https://github.com/lightning/blips/blob/master/blip-0003.md While we're here, move creation of data into a helper function.
1 parent c4d23bc commit 11a4f3e

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ 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+
use super::onion_payment::invalid_payment_err_data;
91+
9092
#[cfg(async_payments)] use {
9193
crate::offers::offer::Amount,
9294
crate::offers::static_invoice::{DEFAULT_RELATIVE_EXPIRY as STATIC_INVOICE_DEFAULT_RELATIVE_EXPIRY, StaticInvoice, StaticInvoiceBuilder},
@@ -6262,10 +6264,7 @@ where
62626264
macro_rules! fail_htlc {
62636265
($htlc: expr, $payment_hash: expr) => {
62646266
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-
);
6267+
let err_data = invalid_payment_err_data($htlc.value, self.best_block.read().unwrap().height);
62696268
failed_forwards.push((HTLCSource::PreviousHopData(HTLCPreviousHopData {
62706269
short_channel_id: $htlc.prev_hop.short_channel_id,
62716270
user_channel_id: $htlc.prev_hop.user_channel_id,
@@ -6278,7 +6277,7 @@ where
62786277
blinded_failure,
62796278
cltv_expiry: Some(cltv_expiry),
62806279
}), payment_hash,
6281-
HTLCFailReason::reason(0x4000 | 15, htlc_msat_height_data),
6280+
HTLCFailReason::reason(0x4000 | 15, err_data),
62826281
HTLCDestination::FailedPayment { payment_hash: $payment_hash },
62836282
));
62846283
continue 'next_forwardable_htlc;
@@ -7231,10 +7230,9 @@ where
72317230
}
72327231
} else {
72337232
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());
7233+
let err_data = invalid_payment_err_data(htlc.value, self.best_block.read().unwrap().height);
72367234
let source = HTLCSource::PreviousHopData(htlc.prev_hop);
7237-
let reason = HTLCFailReason::reason(0x4000 | 15, htlc_msat_height_data);
7235+
let reason = HTLCFailReason::reason(0x4000 | 15, err_data);
72387236
let receiver = HTLCDestination::FailedPayment { payment_hash };
72397237
self.fail_htlc_backwards_internal(&source, &payment_hash, &reason, receiver);
72407238
}
@@ -11822,11 +11820,8 @@ where
1182211820
// number of blocks we generally consider it to take to do a commitment update,
1182311821
// just give up on it and fail the HTLC.
1182411822
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-
1182811823
timed_out_htlcs.push((HTLCSource::PreviousHopData(htlc.prev_hop.clone()), payment_hash.clone(),
11829-
HTLCFailReason::reason(0x4000 | 15, htlc_msat_height_data),
11824+
HTLCFailReason::reason(0x4000 | 15, invalid_payment_err_data(htlc.value, height)),
1183011825
HTLCDestination::FailedPayment { payment_hash: payment_hash.clone() }));
1183111826
false
1183211827
} else { true }

lightning/src/ln/onion_payment.rs

Lines changed: 12 additions & 6 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
}
@@ -361,8 +367,8 @@ pub(super) fn create_recv_pending_htlc_info(
361367
let hashed_preimage = PaymentHash(Sha256::hash(&payment_preimage.0).to_byte_array());
362368
if hashed_preimage != payment_hash {
363369
return Err(InboundHTLCErr {
364-
err_code: 0x4000|22,
365-
err_data: Vec::new(),
370+
err_code: 0x4000 | 15,
371+
err_data: invalid_payment_err_data(amt_msat, current_height),
366372
msg: "Payment preimage didn't match payment hash",
367373
});
368374
}

0 commit comments

Comments
 (0)