Skip to content

Commit ecab2b9

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 d3b636c commit ecab2b9

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
@@ -10621,28 +10621,30 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1062110621
let is_our_scid = self.short_to_chan_info.read().unwrap().contains_key(&scid);
1062210622

1062310623
let mut forward_htlcs = self.forward_htlcs.lock().unwrap();
10624+
let payment_hash = forward_info.payment_hash;
10625+
let pending_add = PendingAddHTLCInfo {
10626+
prev_short_channel_id,
10627+
prev_counterparty_node_id,
10628+
prev_funding_outpoint,
10629+
prev_channel_id,
10630+
prev_htlc_id,
10631+
prev_user_channel_id,
10632+
forward_info,
10633+
};
1062410634
match forward_htlcs.entry(scid) {
1062510635
hash_map::Entry::Occupied(mut entry) => {
10626-
entry.get_mut().push(HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo {
10627-
prev_short_channel_id,
10628-
prev_counterparty_node_id,
10629-
prev_funding_outpoint,
10630-
prev_channel_id,
10631-
prev_htlc_id,
10632-
prev_user_channel_id,
10633-
forward_info,
10634-
}));
10636+
entry.get_mut().push(HTLCForwardInfo::AddHTLC(pending_add));
1063510637
},
1063610638
hash_map::Entry::Vacant(entry) => {
1063710639
if !is_our_scid
10638-
&& forward_info.incoming_amt_msat.is_some()
10640+
&& pending_add.forward_info.incoming_amt_msat.is_some()
1063910641
&& fake_scid::is_valid_intercept(
1064010642
&self.fake_scid_rand_bytes,
1064110643
scid,
1064210644
&self.chain_hash,
1064310645
) {
1064410646
let intercept_id = InterceptId(
10645-
Sha256::hash(&forward_info.incoming_shared_secret)
10647+
Sha256::hash(&pending_add.forward_info.incoming_shared_secret)
1064610648
.to_byte_array(),
1064710649
);
1064810650
let mut pending_intercepts =
@@ -10652,57 +10654,35 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1065210654
new_intercept_events.push_back((
1065310655
events::Event::HTLCIntercepted {
1065410656
requested_next_hop_scid: scid,
10655-
payment_hash: forward_info.payment_hash,
10656-
inbound_amount_msat: forward_info
10657+
payment_hash,
10658+
inbound_amount_msat: pending_add
10659+
.forward_info
1065710660
.incoming_amt_msat
1065810661
.unwrap(),
10659-
expected_outbound_amount_msat: forward_info
10662+
expected_outbound_amount_msat: pending_add
10663+
.forward_info
1066010664
.outgoing_amt_msat,
1066110665
intercept_id,
1066210666
},
1066310667
None,
1066410668
));
10665-
entry.insert(PendingAddHTLCInfo {
10666-
prev_short_channel_id,
10667-
prev_counterparty_node_id,
10668-
prev_funding_outpoint,
10669-
prev_channel_id,
10670-
prev_htlc_id,
10671-
prev_user_channel_id,
10672-
forward_info,
10673-
});
10669+
entry.insert(pending_add);
1067410670
},
1067510671
hash_map::Entry::Occupied(_) => {
1067610672
let logger = WithContext::from(
1067710673
&self.logger,
1067810674
None,
1067910675
Some(prev_channel_id),
10680-
Some(forward_info.payment_hash),
10676+
Some(payment_hash),
1068110677
);
1068210678
log_info!(
1068310679
logger,
1068410680
"Failed to forward incoming HTLC: detected duplicate intercepted payment over short channel id {}",
1068510681
scid
1068610682
);
10687-
let routing = &forward_info.routing;
10688-
let htlc_source =
10689-
HTLCSource::PreviousHopData(HTLCPreviousHopData {
10690-
short_channel_id: prev_short_channel_id,
10691-
user_channel_id: Some(prev_user_channel_id),
10692-
counterparty_node_id: Some(
10693-
prev_counterparty_node_id,
10694-
),
10695-
outpoint: prev_funding_outpoint,
10696-
channel_id: prev_channel_id,
10697-
htlc_id: prev_htlc_id,
10698-
incoming_packet_shared_secret: forward_info
10699-
.incoming_shared_secret,
10700-
phantom_shared_secret: None,
10701-
blinded_failure: routing.blinded_failure(),
10702-
cltv_expiry: routing.incoming_cltv_expiry(),
10703-
});
10704-
10705-
let payment_hash = forward_info.payment_hash;
10683+
let htlc_source = HTLCSource::PreviousHopData(
10684+
pending_add.htlc_previous_hop_data(),
10685+
);
1070610686
let reason = HTLCFailReason::from_failure_code(
1070710687
LocalHTLCFailureReason::UnknownNextPeer,
1070810688
);
@@ -10719,15 +10699,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1071910699
},
1072010700
}
1072110701
} else {
10722-
entry.insert(vec![HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo {
10723-
prev_short_channel_id,
10724-
prev_counterparty_node_id,
10725-
prev_funding_outpoint,
10726-
prev_channel_id,
10727-
prev_htlc_id,
10728-
prev_user_channel_id,
10729-
forward_info,
10730-
})]);
10702+
entry.insert(vec![HTLCForwardInfo::AddHTLC(pending_add)]);
1073110703
}
1073210704
},
1073310705
}

0 commit comments

Comments
 (0)