Skip to content

Commit ef8bec7

Browse files
DRY PendingAddHTLCInfo creation in forward_htlcs
Without this DRYing, we would be repeating the same code to instantiate the PendingAddHTLCInfo several more times in this method, in upcoming commits.
1 parent bb53e9d commit ef8bec7

File tree

1 file changed

+24
-52
lines changed

1 file changed

+24
-52
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 24 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -10625,28 +10625,30 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1062510625
let is_our_scid = self.short_to_chan_info.read().unwrap().contains_key(&scid);
1062610626

1062710627
let mut forward_htlcs = self.forward_htlcs.lock().unwrap();
10628+
let payment_hash = forward_info.payment_hash;
10629+
let pending_add = PendingAddHTLCInfo {
10630+
prev_short_channel_id,
10631+
prev_counterparty_node_id,
10632+
prev_funding_outpoint,
10633+
prev_channel_id,
10634+
prev_htlc_id,
10635+
prev_user_channel_id,
10636+
forward_info,
10637+
};
1062810638
match forward_htlcs.entry(scid) {
1062910639
hash_map::Entry::Occupied(mut entry) => {
10630-
entry.get_mut().push(HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo {
10631-
prev_short_channel_id,
10632-
prev_counterparty_node_id,
10633-
prev_funding_outpoint,
10634-
prev_channel_id,
10635-
prev_htlc_id,
10636-
prev_user_channel_id,
10637-
forward_info,
10638-
}));
10640+
entry.get_mut().push(HTLCForwardInfo::AddHTLC(pending_add));
1063910641
},
1064010642
hash_map::Entry::Vacant(entry) => {
1064110643
if !is_our_scid
10642-
&& forward_info.incoming_amt_msat.is_some()
10644+
&& pending_add.forward_info.incoming_amt_msat.is_some()
1064310645
&& fake_scid::is_valid_intercept(
1064410646
&self.fake_scid_rand_bytes,
1064510647
scid,
1064610648
&self.chain_hash,
1064710649
) {
1064810650
let intercept_id = InterceptId(
10649-
Sha256::hash(&forward_info.incoming_shared_secret)
10651+
Sha256::hash(&pending_add.forward_info.incoming_shared_secret)
1065010652
.to_byte_array(),
1065110653
);
1065210654
let mut pending_intercepts =
@@ -10656,57 +10658,35 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1065610658
new_intercept_events.push_back((
1065710659
events::Event::HTLCIntercepted {
1065810660
requested_next_hop_scid: scid,
10659-
payment_hash: forward_info.payment_hash,
10660-
inbound_amount_msat: forward_info
10661+
payment_hash,
10662+
inbound_amount_msat: pending_add
10663+
.forward_info
1066110664
.incoming_amt_msat
1066210665
.unwrap(),
10663-
expected_outbound_amount_msat: forward_info
10666+
expected_outbound_amount_msat: pending_add
10667+
.forward_info
1066410668
.outgoing_amt_msat,
1066510669
intercept_id,
1066610670
},
1066710671
None,
1066810672
));
10669-
entry.insert(PendingAddHTLCInfo {
10670-
prev_short_channel_id,
10671-
prev_counterparty_node_id,
10672-
prev_funding_outpoint,
10673-
prev_channel_id,
10674-
prev_htlc_id,
10675-
prev_user_channel_id,
10676-
forward_info,
10677-
});
10673+
entry.insert(pending_add);
1067810674
},
1067910675
hash_map::Entry::Occupied(_) => {
1068010676
let logger = WithContext::from(
1068110677
&self.logger,
1068210678
None,
1068310679
Some(prev_channel_id),
10684-
Some(forward_info.payment_hash),
10680+
Some(payment_hash),
1068510681
);
1068610682
log_info!(
1068710683
logger,
1068810684
"Failed to forward incoming HTLC: detected duplicate intercepted payment over short channel id {}",
1068910685
scid
1069010686
);
10691-
let routing = &forward_info.routing;
10692-
let htlc_source =
10693-
HTLCSource::PreviousHopData(HTLCPreviousHopData {
10694-
short_channel_id: prev_short_channel_id,
10695-
user_channel_id: Some(prev_user_channel_id),
10696-
counterparty_node_id: Some(
10697-
prev_counterparty_node_id,
10698-
),
10699-
outpoint: prev_funding_outpoint,
10700-
channel_id: prev_channel_id,
10701-
htlc_id: prev_htlc_id,
10702-
incoming_packet_shared_secret: forward_info
10703-
.incoming_shared_secret,
10704-
phantom_shared_secret: None,
10705-
blinded_failure: routing.blinded_failure(),
10706-
cltv_expiry: routing.incoming_cltv_expiry(),
10707-
});
10708-
10709-
let payment_hash = forward_info.payment_hash;
10687+
let htlc_source = HTLCSource::PreviousHopData(
10688+
pending_add.htlc_previous_hop_data(),
10689+
);
1071010690
let reason = HTLCFailReason::from_failure_code(
1071110691
LocalHTLCFailureReason::UnknownNextPeer,
1071210692
);
@@ -10723,15 +10703,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1072310703
},
1072410704
}
1072510705
} else {
10726-
entry.insert(vec![HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo {
10727-
prev_short_channel_id,
10728-
prev_counterparty_node_id,
10729-
prev_funding_outpoint,
10730-
prev_channel_id,
10731-
prev_htlc_id,
10732-
prev_user_channel_id,
10733-
forward_info,
10734-
})]);
10706+
entry.insert(vec![HTLCForwardInfo::AddHTLC(pending_add)]);
1073510707
}
1073610708
},
1073710709
}

0 commit comments

Comments
 (0)