Skip to content

Commit 900d900

Browse files
authored
Merge pull request #596 from D4nte/586-update-htlc-error-code-content
Include `height` to `incorrect_or_unknown_payment_details` failure
2 parents b9db2ec + 236887d commit 900d900

File tree

4 files changed

+54
-14
lines changed

4 files changed

+54
-14
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1733,12 +1733,19 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
17331733
}
17341734
if total_value >= msgs::MAX_VALUE_MSAT || total_value > data.total_msat {
17351735
for htlc in htlcs.iter() {
1736+
let mut htlc_msat_height_data = byte_utils::be64_to_array(htlc.value).to_vec();
1737+
htlc_msat_height_data.extend_from_slice(
1738+
&byte_utils::be32_to_array(
1739+
self.latest_block_height.load(Ordering::Acquire)
1740+
as u32,
1741+
),
1742+
);
17361743
failed_forwards.push((HTLCSource::PreviousHopData(HTLCPreviousHopData {
17371744
short_channel_id: htlc.prev_hop.short_channel_id,
17381745
htlc_id: htlc.prev_hop.htlc_id,
17391746
incoming_packet_shared_secret: htlc.prev_hop.incoming_packet_shared_secret,
17401747
}), payment_hash,
1741-
HTLCFailReason::Reason { failure_code: 0x4000 | 15, data: byte_utils::be64_to_array(htlc.value).to_vec() }
1748+
HTLCFailReason::Reason { failure_code: 0x4000 | 15, data: htlc_msat_height_data }
17421749
));
17431750
}
17441751
} else if total_value == data.total_msat {
@@ -1819,9 +1826,13 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
18191826
if let Some(mut sources) = removed_source {
18201827
for htlc in sources.drain(..) {
18211828
if channel_state.is_none() { channel_state = Some(self.channel_state.lock().unwrap()); }
1829+
let mut htlc_msat_height_data = byte_utils::be64_to_array(htlc.value).to_vec();
1830+
htlc_msat_height_data.extend_from_slice(&byte_utils::be32_to_array(
1831+
self.latest_block_height.load(Ordering::Acquire) as u32,
1832+
));
18221833
self.fail_htlc_backwards_internal(channel_state.take().unwrap(),
18231834
HTLCSource::PreviousHopData(htlc.prev_hop), payment_hash,
1824-
HTLCFailReason::Reason { failure_code: 0x4000 | 15, data: byte_utils::be64_to_array(htlc.value).to_vec() });
1835+
HTLCFailReason::Reason { failure_code: 0x4000 | 15, data: htlc_msat_height_data });
18251836
}
18261837
true
18271838
} else { false }
@@ -1845,9 +1856,9 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
18451856
match &onion_error {
18461857
&HTLCFailReason::LightningError { ref err } => {
18471858
#[cfg(test)]
1848-
let (channel_update, payment_retryable, onion_error_code) = onion_utils::process_onion_failure(&self.secp_ctx, &self.logger, &source, err.data.clone());
1859+
let (channel_update, payment_retryable, onion_error_code, onion_error_data) = onion_utils::process_onion_failure(&self.secp_ctx, &self.logger, &source, err.data.clone());
18491860
#[cfg(not(test))]
1850-
let (channel_update, payment_retryable, _) = onion_utils::process_onion_failure(&self.secp_ctx, &self.logger, &source, err.data.clone());
1861+
let (channel_update, payment_retryable, _, _) = onion_utils::process_onion_failure(&self.secp_ctx, &self.logger, &source, err.data.clone());
18511862
// TODO: If we decided to blame ourselves (or one of our channels) in
18521863
// process_onion_failure we should close that channel as it implies our
18531864
// next-hop is needlessly blaming us!
@@ -1863,13 +1874,17 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
18631874
payment_hash: payment_hash.clone(),
18641875
rejected_by_dest: !payment_retryable,
18651876
#[cfg(test)]
1866-
error_code: onion_error_code
1877+
error_code: onion_error_code,
1878+
#[cfg(test)]
1879+
error_data: onion_error_data
18671880
}
18681881
);
18691882
},
18701883
&HTLCFailReason::Reason {
18711884
#[cfg(test)]
18721885
ref failure_code,
1886+
#[cfg(test)]
1887+
ref data,
18731888
.. } => {
18741889
// we get a fail_malformed_htlc from the first hop
18751890
// TODO: We'd like to generate a PaymentFailureNetworkUpdate for temporary
@@ -1884,6 +1899,8 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
18841899
rejected_by_dest: path.len() == 1,
18851900
#[cfg(test)]
18861901
error_code: Some(*failure_code),
1902+
#[cfg(test)]
1903+
error_data: Some(data.clone()),
18871904
}
18881905
);
18891906
}
@@ -1982,12 +1999,13 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
19821999
for htlc in sources.drain(..) {
19832000
if channel_state.is_none() { channel_state = Some(self.channel_state.lock().unwrap()); }
19842001
if (is_mpp && !valid_mpp) || (!is_mpp && (htlc.value < expected_amount || htlc.value > expected_amount * 2)) {
1985-
let mut htlc_msat_data = byte_utils::be64_to_array(htlc.value).to_vec();
1986-
let mut height_data = byte_utils::be32_to_array(self.latest_block_height.load(Ordering::Acquire) as u32).to_vec();
1987-
htlc_msat_data.append(&mut height_data);
2002+
let mut htlc_msat_height_data = byte_utils::be64_to_array(htlc.value).to_vec();
2003+
htlc_msat_height_data.extend_from_slice(&byte_utils::be32_to_array(
2004+
self.latest_block_height.load(Ordering::Acquire) as u32,
2005+
));
19882006
self.fail_htlc_backwards_internal(channel_state.take().unwrap(),
19892007
HTLCSource::PreviousHopData(htlc.prev_hop), &payment_hash,
1990-
HTLCFailReason::Reason { failure_code: 0x4000|15, data: htlc_msat_data });
2008+
HTLCFailReason::Reason { failure_code: 0x4000|15, data: htlc_msat_height_data });
19912009
} else {
19922010
match self.claim_funds_from_hop(channel_state.as_mut().unwrap(), htlc.prev_hop, payment_preimage) {
19932011
Err(Some(e)) => {

lightning/src/ln/functional_tests.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5326,7 +5326,7 @@ fn run_onion_failure_test_with_fail_intercept<F1,F2,F3>(_name: &str, test_case:
53265326

53275327
let events = nodes[0].node.get_and_clear_pending_events();
53285328
assert_eq!(events.len(), 1);
5329-
if let &Event::PaymentFailed { payment_hash:_, ref rejected_by_dest, ref error_code } = &events[0] {
5329+
if let &Event::PaymentFailed { payment_hash:_, ref rejected_by_dest, ref error_code, error_data: _ } = &events[0] {
53305330
assert_eq!(*rejected_by_dest, !expected_retryable);
53315331
assert_eq!(*error_code, expected_error_code);
53325332
} else {
@@ -6914,9 +6914,20 @@ fn test_check_htlc_underpaying() {
69146914

69156915
let events = nodes[0].node.get_and_clear_pending_events();
69166916
assert_eq!(events.len(), 1);
6917-
if let &Event::PaymentFailed { payment_hash:_, ref rejected_by_dest, ref error_code } = &events[0] {
6917+
if let &Event::PaymentFailed { payment_hash:_, ref rejected_by_dest, ref error_code, ref error_data } = &events[0] {
69186918
assert_eq!(*rejected_by_dest, true);
69196919
assert_eq!(error_code.unwrap(), 0x4000|15);
6920+
// 10_000 msat as u64, followed by a height of 99 as u32
6921+
assert_eq!(&error_data.as_ref().unwrap()[..], &[
6922+
((10_000u64 >> 7*8) & 0xff) as u8,
6923+
((10_000u64 >> 6*8) & 0xff) as u8,
6924+
((10_000u64 >> 5*8) & 0xff) as u8,
6925+
((10_000u64 >> 4*8) & 0xff) as u8,
6926+
((10_000u64 >> 3*8) & 0xff) as u8,
6927+
((10_000u64 >> 2*8) & 0xff) as u8,
6928+
((10_000u64 >> 1*8) & 0xff) as u8,
6929+
((10_000u64 >> 0*8) & 0xff) as u8,
6930+
0, 0, 0, 99]);
69206931
} else {
69216932
panic!("Unexpected event");
69226933
}

lightning/src/ln/onion_utils.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -317,11 +317,13 @@ pub(super) fn build_first_hop_failure_packet(shared_secret: &[u8], failure_type:
317317
/// Process failure we got back from upstream on a payment we sent (implying htlc_source is an
318318
/// OutboundRoute).
319319
/// Returns update, a boolean indicating that the payment itself failed, and the error code.
320-
pub(super) fn process_onion_failure<T: secp256k1::Signing>(secp_ctx: &Secp256k1<T>, logger: &Arc<Logger>, htlc_source: &HTLCSource, mut packet_decrypted: Vec<u8>) -> (Option<msgs::HTLCFailChannelUpdate>, bool, Option<u16>) {
320+
#[inline]
321+
pub(super) fn process_onion_failure<T: secp256k1::Signing>(secp_ctx: &Secp256k1<T>, logger: &Arc<Logger>, htlc_source: &HTLCSource, mut packet_decrypted: Vec<u8>) -> (Option<msgs::HTLCFailChannelUpdate>, bool, Option<u16>, Option<Vec<u8>>) {
321322
if let &HTLCSource::OutboundRoute { ref path, ref session_priv, ref first_hop_htlc_msat } = htlc_source {
322323
let mut res = None;
323324
let mut htlc_msat = *first_hop_htlc_msat;
324325
let mut error_code_ret = None;
326+
let mut error_packet_ret = None;
325327
let mut next_route_hop_ix = 0;
326328
let mut is_from_final_node = false;
327329

@@ -356,6 +358,7 @@ pub(super) fn process_onion_failure<T: secp256k1::Signing>(secp_ctx: &Secp256k1<
356358

357359
let error_code = byte_utils::slice_to_be16(&error_code_slice);
358360
error_code_ret = Some(error_code);
361+
error_packet_ret = Some(err_packet.failuremsg[2..].to_vec());
359362

360363
let (debug_field, debug_field_size) = errors::get_onion_debug_field(error_code);
361364

@@ -456,11 +459,11 @@ pub(super) fn process_onion_failure<T: secp256k1::Signing>(secp_ctx: &Secp256k1<
456459
}
457460
}).expect("Route that we sent via spontaneously grew invalid keys in the middle of it?");
458461
if let Some((channel_update, payment_retryable)) = res {
459-
(channel_update, payment_retryable, error_code_ret)
462+
(channel_update, payment_retryable, error_code_ret, error_packet_ret)
460463
} else {
461464
// only not set either packet unparseable or hmac does not match with any
462465
// payment not retryable only when garbage is from the final node
463-
(None, !is_from_final_node, None)
466+
(None, !is_from_final_node, None, None)
464467
}
465468
} else { unreachable!(); }
466469
}

lightning/src/util/events.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ pub enum Event {
9696
rejected_by_dest: bool,
9797
#[cfg(test)]
9898
error_code: Option<u16>,
99+
#[cfg(test)]
100+
error_data: Option<Vec<u8>>,
99101
},
100102
/// Used to indicate that ChannelManager::process_pending_htlc_forwards should be called at a
101103
/// time in the future.
@@ -142,12 +144,16 @@ impl Writeable for Event {
142144
&Event::PaymentFailed { ref payment_hash, ref rejected_by_dest,
143145
#[cfg(test)]
144146
ref error_code,
147+
#[cfg(test)]
148+
ref error_data,
145149
} => {
146150
4u8.write(writer)?;
147151
payment_hash.write(writer)?;
148152
rejected_by_dest.write(writer)?;
149153
#[cfg(test)]
150154
error_code.write(writer)?;
155+
#[cfg(test)]
156+
error_data.write(writer)?;
151157
},
152158
&Event::PendingHTLCsForwardable { time_forwardable: _ } => {
153159
5u8.write(writer)?;
@@ -186,6 +192,8 @@ impl MaybeReadable for Event {
186192
rejected_by_dest: Readable::read(reader)?,
187193
#[cfg(test)]
188194
error_code: Readable::read(reader)?,
195+
#[cfg(test)]
196+
error_data: Readable::read(reader)?,
189197
})),
190198
5u8 => Ok(Some(Event::PendingHTLCsForwardable {
191199
time_forwardable: Duration::from_secs(0)

0 commit comments

Comments
 (0)