Skip to content

Commit ab59642

Browse files
extract PendingInterceptedPayment from HTLCForwardInfo::AddHTLC
1 parent 592b47a commit ab59642

File tree

1 file changed

+50
-48
lines changed

1 file changed

+50
-48
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 50 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,14 @@ pub(super) enum PendingHTLCStatus {
131131
Fail(HTLCFailureMsg),
132132
}
133133

134+
pub(super) struct PendingInterceptedHTLC {
135+
forward_info: PendingHTLCInfo,
136+
prev_short_channel_id: u64,
137+
prev_htlc_id: u64,
138+
prev_funding_outpoint: OutPoint,
139+
prev_htlc_amount: u64
140+
}
141+
134142
pub(super) enum HTLCForwardInfo {
135143
AddHTLC {
136144
forward_info: PendingHTLCInfo,
@@ -741,9 +749,9 @@ pub struct ChannelManager<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref,
741749
/// Locked *after* channel_state.
742750
pending_outbound_payments: Mutex<HashMap<PaymentId, PendingOutboundPayment>>,
743751

744-
/// Storage for HTLCForwardInfo's that have been intercepted and bubbled up to the user.
752+
/// Storage for PendingInterceptedHTLC's that have been intercepted and bubbled up to the user.
745753
/// We hold them here until the user tells us what we should to with them.
746-
pending_intercepted_payments: Mutex<HashMap<InterceptId, HTLCForwardInfo>>,
754+
pending_intercepted_payments: Mutex<HashMap<InterceptId, PendingInterceptedHTLC>>,
747755

748756
/// The set of outbound SCID aliases across all our channels, including unconfirmed channels
749757
/// and some closed channels which reached a usable state prior to being closed. This is used
@@ -3090,19 +3098,16 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
30903098
};
30913099

30923100
if let Some(payment) = pending_intercept {
3093-
if let HTLCForwardInfo::AddHTLC { forward_info, prev_short_channel_id, prev_htlc_id, prev_funding_outpoint, .. } = payment {
3094-
3095-
let htlc_source = HTLCSource::PreviousHopData(HTLCPreviousHopData {
3096-
short_channel_id: prev_short_channel_id,
3097-
outpoint: prev_funding_outpoint,
3098-
htlc_id: prev_htlc_id,
3099-
incoming_packet_shared_secret: forward_info.incoming_shared_secret,
3100-
phantom_shared_secret: None,
3101-
});
3101+
let htlc_source = HTLCSource::PreviousHopData(HTLCPreviousHopData {
3102+
short_channel_id: payment.prev_short_channel_id,
3103+
outpoint: payment.prev_funding_outpoint,
3104+
htlc_id: payment.prev_htlc_id,
3105+
incoming_packet_shared_secret: payment.forward_info.incoming_shared_secret,
3106+
phantom_shared_secret: None,
3107+
});
31023108

3103-
let failure_reason = HTLCFailReason::Reason { failure_code: 0x4000 | 10, data: Vec::new() };
3104-
self.fail_htlc_backwards_internal(self.channel_state.lock().unwrap(), htlc_source, &forward_info.payment_hash, failure_reason);
3105-
}
3109+
let failure_reason = HTLCFailReason::Reason { failure_code: 0x4000 | 10, data: Vec::new() };
3110+
self.fail_htlc_backwards_internal(self.channel_state.lock().unwrap(), htlc_source, &payment.forward_info.payment_hash, failure_reason);
31063111
}
31073112
}
31083113

@@ -3119,30 +3124,22 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
31193124
match pending_intercept {
31203125
None => Err(APIError::APIMisuseError { err: "Payment with that InterceptId not found".to_string() }),
31213126
Some(payment) => {
3122-
match payment {
3123-
HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info, prev_funding_outpoint, prev_htlc_amount } => {
3124-
3125-
let routing = match forward_info.routing {
3126-
PendingHTLCRouting::Forward { onion_packet, .. } => {
3127-
PendingHTLCRouting::Forward { onion_packet, short_channel_id: scid }
3128-
},
3129-
_ => forward_info.routing
3130-
};
3131-
3132-
let pending_htlc_info = PendingHTLCInfo {
3133-
amt_to_forward,
3134-
routing,
3135-
..forward_info
3136-
};
3137-
3138-
let mut per_source_pending_forward = vec![(prev_short_channel_id, prev_funding_outpoint, vec![(pending_htlc_info, prev_htlc_id, prev_htlc_amount)])];
3127+
let routing = match payment.forward_info.routing {
3128+
PendingHTLCRouting::Forward { onion_packet, .. } => {
3129+
PendingHTLCRouting::Forward { onion_packet, short_channel_id: scid }
3130+
},
3131+
_ => payment.forward_info.routing
3132+
};
31393133

3140-
self.forward_htlcs(&mut per_source_pending_forward);
3134+
let pending_htlc_info = PendingHTLCInfo {
3135+
amt_to_forward,
3136+
routing,
3137+
..payment.forward_info
3138+
};
31413139

3142-
Ok(())
3143-
},
3144-
_ => Err(APIError::APIMisuseError { err: "impossible".to_string() })
3145-
}
3140+
let mut per_source_pending_forward = vec![(payment.prev_short_channel_id, payment.prev_funding_outpoint, vec![(pending_htlc_info, payment.prev_htlc_id, Some(payment.prev_htlc_amount))])];
3141+
self.forward_htlcs(&mut per_source_pending_forward);
3142+
Ok(())
31463143
}
31473144
}
31483145
}
@@ -3169,9 +3166,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
31693166
None => {
31703167
for forward_info in pending_forwards.drain(..) {
31713168
match forward_info {
3172-
HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info: PendingHTLCInfo {
3173-
ref routing, incoming_shared_secret, payment_hash, amt_to_forward, outgoing_cltv_value },
3174-
prev_funding_outpoint, prev_htlc_amount } => {
3169+
HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info, prev_funding_outpoint, prev_htlc_amount } => {
31753170
macro_rules! fail_forward {
31763171
($msg: expr, $err_code: expr, $err_data: expr, $phantom_ss: expr) => {
31773172
{
@@ -3180,21 +3175,21 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
31803175
short_channel_id: prev_short_channel_id,
31813176
outpoint: prev_funding_outpoint,
31823177
htlc_id: prev_htlc_id,
3183-
incoming_packet_shared_secret: incoming_shared_secret,
3178+
incoming_packet_shared_secret: forward_info.incoming_shared_secret,
31843179
phantom_shared_secret: $phantom_ss,
31853180
});
3186-
failed_forwards.push((htlc_source, payment_hash,
3181+
failed_forwards.push((htlc_source, forward_info.payment_hash,
31873182
HTLCFailReason::Reason { failure_code: $err_code, data: $err_data }
31883183
));
31893184
continue;
31903185
}
31913186
}
31923187
}
3193-
if let PendingHTLCRouting::Forward { onion_packet, .. } = routing {
3188+
if let PendingHTLCRouting::Forward { ref onion_packet, .. } = forward_info.routing {
31943189
let phantom_secret_res = self.keys_manager.get_node_secret(Recipient::PhantomNode);
31953190
if phantom_secret_res.is_ok() && fake_scid::is_valid_phantom(&self.fake_scid_rand_bytes, short_chan_id) {
31963191
let phantom_shared_secret = SharedSecret::new(&onion_packet.public_key.unwrap(), &phantom_secret_res.unwrap()).secret_bytes();
3197-
let next_hop = match onion_utils::decode_next_hop(phantom_shared_secret, &onion_packet.hop_data, onion_packet.hmac, payment_hash) {
3192+
let next_hop = match onion_utils::decode_next_hop(phantom_shared_secret, &onion_packet.hop_data, onion_packet.hmac, forward_info.payment_hash) {
31983193
Ok(res) => res,
31993194
Err(onion_utils::OnionDecodeErr::Malformed { err_msg, err_code }) => {
32003195
let sha256_of_onion = Sha256::hash(&onion_packet.hop_data).into_inner();
@@ -3210,24 +3205,31 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
32103205
};
32113206
match next_hop {
32123207
onion_utils::Hop::Receive(hop_data) => {
3213-
match self.construct_recv_pending_htlc_info(hop_data, incoming_shared_secret, payment_hash, amt_to_forward, outgoing_cltv_value, Some(phantom_shared_secret)) {
3208+
match self.construct_recv_pending_htlc_info(hop_data, forward_info.incoming_shared_secret, forward_info.payment_hash, forward_info.amt_to_forward, forward_info.outgoing_cltv_value, Some(phantom_shared_secret)) {
32143209
Ok(info) => phantom_receives.push((prev_short_channel_id, prev_funding_outpoint, vec![(info, prev_htlc_id, prev_htlc_amount)])),
32153210
Err(ReceiveError { err_code, err_data, msg }) => fail_forward!(msg, err_code, err_data, Some(phantom_shared_secret))
32163211
}
32173212
},
32183213
_ => panic!(),
32193214
}
32203215
} else if prev_htlc_amount.is_some() && fake_scid::is_valid_intercept(&self.fake_scid_rand_bytes, short_chan_id) {
3221-
let intercept_id = InterceptId(Sha256::hash(&incoming_shared_secret).into_inner());
3216+
let intercept_id = InterceptId(Sha256::hash(&forward_info.incoming_shared_secret).into_inner());
32223217
let mut pending_intercepts = self.pending_intercepted_payments.lock().unwrap();
32233218
match pending_intercepts.entry(intercept_id) {
32243219
hash_map::Entry::Vacant(entry) => {
3225-
entry.insert(forward_info);
3220+
let pending_intercepted_payment = PendingInterceptedHTLC {
3221+
forward_info: forward_info.clone(),
3222+
prev_short_channel_id,
3223+
prev_htlc_id,
3224+
prev_funding_outpoint,
3225+
prev_htlc_amount: prev_htlc_amount.unwrap()
3226+
};
3227+
entry.insert(pending_intercepted_payment);
32263228
new_events.push(events::Event::PaymentIntercepted {
32273229
short_channel_id: short_chan_id,
3228-
payment_hash,
3230+
payment_hash: forward_info.payment_hash,
32293231
inbound_amount_msats: prev_htlc_amount.unwrap(),
3230-
expected_outbound_amount_msats: amt_to_forward,
3232+
expected_outbound_amount_msats: forward_info.amt_to_forward,
32313233
intercept_id
32323234
});
32333235
},

0 commit comments

Comments
 (0)