Skip to content

Commit 582b827

Browse files
Refactor HTLCForwardInfo::AddHTLC for intercept forwards
In upcoming commit(s), we'll want to store intercepted HTLC forwards in ChannelManager before the user signals that they should be forwarded. It wouldn't make sense to store a HTLCForwardInfo as-is because the FailHTLC variant doesn't make sense, so we refactor out the ::AddHTLC contents into its own struct for storage. Co-authored-by: John Cantrell <johncantrell97@gmail.com> Co-authored-by: Valentine Wallace <vwallace@protonmail.com>
1 parent 505102d commit 582b827

File tree

2 files changed

+58
-45
lines changed

2 files changed

+58
-45
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 49 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -129,20 +129,22 @@ pub(super) enum PendingHTLCStatus {
129129
Fail(HTLCFailureMsg),
130130
}
131131

132-
pub(super) enum HTLCForwardInfo {
133-
AddHTLC {
134-
forward_info: PendingHTLCInfo,
132+
pub(super) struct PendingAddHTLCInfo {
133+
pub(super) forward_info: PendingHTLCInfo,
134+
135+
// These fields are produced in `forward_htlcs()` and consumed in
136+
// `process_pending_htlc_forwards()` for constructing the
137+
// `HTLCSource::PreviousHopData` for failed and forwarded
138+
// HTLCs.
139+
//
140+
// Note that this may be an outbound SCID alias for the associated channel.
141+
prev_short_channel_id: u64,
142+
prev_htlc_id: u64,
143+
prev_funding_outpoint: OutPoint,
144+
}
135145

136-
// These fields are produced in `forward_htlcs()` and consumed in
137-
// `process_pending_htlc_forwards()` for constructing the
138-
// `HTLCSource::PreviousHopData` for failed and forwarded
139-
// HTLCs.
140-
//
141-
// Note that this may be an outbound SCID alias for the associated channel.
142-
prev_short_channel_id: u64,
143-
prev_htlc_id: u64,
144-
prev_funding_outpoint: OutPoint,
145-
},
146+
pub(super) enum HTLCForwardInfo {
147+
AddHTLC(PendingAddHTLCInfo),
146148
FailHTLC {
147149
htlc_id: u64,
148150
err_packet: msgs::OnionErrorPacket,
@@ -3149,9 +3151,13 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
31493151
() => {
31503152
for forward_info in pending_forwards.drain(..) {
31513153
match forward_info {
3152-
HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info: PendingHTLCInfo {
3153-
routing, incoming_shared_secret, payment_hash, amt_to_forward, outgoing_cltv_value },
3154-
prev_funding_outpoint } => {
3154+
HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo {
3155+
prev_short_channel_id, prev_htlc_id, prev_funding_outpoint,
3156+
forward_info: PendingHTLCInfo {
3157+
routing, incoming_shared_secret, payment_hash, amt_to_forward,
3158+
outgoing_cltv_value
3159+
}
3160+
}) => {
31553161
macro_rules! failure_handler {
31563162
($msg: expr, $err_code: expr, $err_data: expr, $phantom_ss: expr, $next_hop_unknown: expr) => {
31573163
log_info!(self.logger, "Failed to accept/forward incoming HTLC: {}", $msg);
@@ -3252,11 +3258,13 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
32523258
let mut fail_htlc_msgs = Vec::new();
32533259
for forward_info in pending_forwards.drain(..) {
32543260
match forward_info {
3255-
HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info: PendingHTLCInfo {
3256-
routing: PendingHTLCRouting::Forward {
3257-
onion_packet, ..
3258-
}, incoming_shared_secret, payment_hash, amt_to_forward, outgoing_cltv_value },
3259-
prev_funding_outpoint } => {
3261+
HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo {
3262+
prev_short_channel_id, prev_htlc_id, prev_funding_outpoint ,
3263+
forward_info: PendingHTLCInfo {
3264+
incoming_shared_secret, payment_hash, amt_to_forward, outgoing_cltv_value,
3265+
routing: PendingHTLCRouting::Forward { onion_packet, .. },
3266+
},
3267+
}) => {
32603268
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);
32613269
let htlc_source = HTLCSource::PreviousHopData(HTLCPreviousHopData {
32623270
short_channel_id: prev_short_channel_id,
@@ -3377,9 +3385,12 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
33773385
} else {
33783386
for forward_info in pending_forwards.drain(..) {
33793387
match forward_info {
3380-
HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info: PendingHTLCInfo {
3381-
routing, incoming_shared_secret, payment_hash, amt_to_forward, .. },
3382-
prev_funding_outpoint } => {
3388+
HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo {
3389+
prev_short_channel_id, prev_htlc_id, prev_funding_outpoint,
3390+
forward_info: PendingHTLCInfo {
3391+
routing, incoming_shared_secret, payment_hash, amt_to_forward, ..
3392+
}
3393+
}) => {
33833394
let (cltv_expiry, onion_payload, payment_data, phantom_shared_secret) = match routing {
33843395
PendingHTLCRouting::Receive { payment_data, incoming_cltv_expiry, phantom_shared_secret } => {
33853396
let _legacy_hop_data = Some(payment_data.clone());
@@ -5089,12 +5100,12 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
50895100
PendingHTLCRouting::ReceiveKeysend { .. } => 0,
50905101
}) {
50915102
hash_map::Entry::Occupied(mut entry) => {
5092-
entry.get_mut().push(HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_funding_outpoint,
5093-
prev_htlc_id, forward_info });
5103+
entry.get_mut().push(HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo {
5104+
prev_short_channel_id, prev_funding_outpoint, prev_htlc_id, forward_info }));
50945105
},
50955106
hash_map::Entry::Vacant(entry) => {
5096-
entry.insert(vec!(HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_funding_outpoint,
5097-
prev_htlc_id, forward_info }));
5107+
entry.insert(vec!(HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo {
5108+
prev_short_channel_id, prev_funding_outpoint, prev_htlc_id, forward_info })));
50985109
}
50995110
}
51005111
}
@@ -6681,18 +6692,20 @@ impl_writeable_tlv_based_enum!(HTLCFailReason,
66816692
},
66826693
;);
66836694

6695+
impl_writeable_tlv_based!(PendingAddHTLCInfo, {
6696+
(0, forward_info, required),
6697+
(2, prev_short_channel_id, required),
6698+
(4, prev_htlc_id, required),
6699+
(6, prev_funding_outpoint, required),
6700+
});
6701+
66846702
impl_writeable_tlv_based_enum!(HTLCForwardInfo,
6685-
(0, AddHTLC) => {
6686-
(0, forward_info, required),
6687-
(2, prev_short_channel_id, required),
6688-
(4, prev_htlc_id, required),
6689-
(6, prev_funding_outpoint, required),
6690-
},
66916703
(1, FailHTLC) => {
66926704
(0, htlc_id, required),
66936705
(2, err_packet, required),
6694-
},
6695-
;);
6706+
};
6707+
(0, AddHTLC)
6708+
);
66966709

66976710
impl_writeable_tlv_based!(PendingInboundPayment, {
66986711
(0, payment_secret, required),

lightning/src/ln/onion_route_tests.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::chain::channelmonitor::{ChannelMonitor, CLTV_CLAIM_BUFFER, LATENCY_GR
1515
use crate::chain::keysinterface::{KeysInterface, Recipient};
1616
use crate::ln::{PaymentHash, PaymentSecret};
1717
use crate::ln::channel::EXPIRE_PREV_CONFIG_TICKS;
18-
use crate::ln::channelmanager::{self, ChannelManager, ChannelManagerReadArgs, HTLCForwardInfo, CLTV_FAR_FAR_AWAY, MIN_CLTV_EXPIRY_DELTA, PendingHTLCInfo, PendingHTLCRouting, PaymentId};
18+
use crate::ln::channelmanager::{self, ChannelManager, ChannelManagerReadArgs, HTLCForwardInfo, CLTV_FAR_FAR_AWAY, MIN_CLTV_EXPIRY_DELTA, PendingAddHTLCInfo, PendingHTLCInfo, PendingHTLCRouting, PaymentId};
1919
use crate::ln::onion_utils;
2020
use crate::routing::gossip::{NetworkUpdate, RoutingFees, NodeId};
2121
use crate::routing::router::{get_route, PaymentParameters, Route, RouteHint, RouteHintHop};
@@ -554,7 +554,7 @@ fn test_onion_failure() {
554554
for (_, pending_forwards) in nodes[1].node.forward_htlcs.lock().unwrap().iter_mut() {
555555
for f in pending_forwards.iter_mut() {
556556
match f {
557-
&mut HTLCForwardInfo::AddHTLC { ref mut forward_info, .. } =>
557+
&mut HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo { ref mut forward_info, .. }) =>
558558
forward_info.outgoing_cltv_value += 1,
559559
_ => {},
560560
}
@@ -567,7 +567,7 @@ fn test_onion_failure() {
567567
for (_, pending_forwards) in nodes[1].node.forward_htlcs.lock().unwrap().iter_mut() {
568568
for f in pending_forwards.iter_mut() {
569569
match f {
570-
&mut HTLCForwardInfo::AddHTLC { ref mut forward_info, .. } =>
570+
&mut HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo { ref mut forward_info, .. }) =>
571571
forward_info.amt_to_forward -= 1,
572572
_ => {},
573573
}
@@ -1035,12 +1035,12 @@ fn test_phantom_onion_hmac_failure() {
10351035
let mut forward_htlcs = nodes[1].node.forward_htlcs.lock().unwrap();
10361036
let mut pending_forward = forward_htlcs.get_mut(&phantom_scid).unwrap();
10371037
match pending_forward[0] {
1038-
HTLCForwardInfo::AddHTLC {
1038+
HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo {
10391039
forward_info: PendingHTLCInfo {
10401040
routing: PendingHTLCRouting::Forward { ref mut onion_packet, .. },
10411041
..
10421042
}, ..
1043-
} => {
1043+
}) => {
10441044
onion_packet.hmac[onion_packet.hmac.len() - 1] ^= 1;
10451045
Sha256::hash(&onion_packet.hop_data).into_inner().to_vec()
10461046
},
@@ -1095,12 +1095,12 @@ fn test_phantom_invalid_onion_payload() {
10951095
for (_, pending_forwards) in nodes[1].node.forward_htlcs.lock().unwrap().iter_mut() {
10961096
for f in pending_forwards.iter_mut() {
10971097
match f {
1098-
&mut HTLCForwardInfo::AddHTLC {
1098+
&mut HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo {
10991099
forward_info: PendingHTLCInfo {
11001100
routing: PendingHTLCRouting::Forward { ref mut onion_packet, .. },
11011101
..
11021102
}, ..
1103-
} => {
1103+
}) => {
11041104
// Construct the onion payloads for the entire route and an invalid amount.
11051105
let height = nodes[0].best_block_info().1;
11061106
let session_priv = SecretKey::from_slice(&session_priv).unwrap();
@@ -1166,9 +1166,9 @@ fn test_phantom_final_incorrect_cltv_expiry() {
11661166
for (_, pending_forwards) in nodes[1].node.forward_htlcs.lock().unwrap().iter_mut() {
11671167
for f in pending_forwards.iter_mut() {
11681168
match f {
1169-
&mut HTLCForwardInfo::AddHTLC {
1169+
&mut HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo {
11701170
forward_info: PendingHTLCInfo { ref mut outgoing_cltv_value, .. }, ..
1171-
} => {
1171+
}) => {
11721172
*outgoing_cltv_value += 1;
11731173
},
11741174
_ => panic!("Unexpected forward"),

0 commit comments

Comments
 (0)