Skip to content

Commit 8173ac0

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 84c997d commit 8173ac0

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

1063010630
let mut forward_htlcs = self.forward_htlcs.lock().unwrap();
10631+
let payment_hash = forward_info.payment_hash;
10632+
let pending_add = PendingAddHTLCInfo {
10633+
prev_short_channel_id,
10634+
prev_counterparty_node_id,
10635+
prev_funding_outpoint,
10636+
prev_channel_id,
10637+
prev_htlc_id,
10638+
prev_user_channel_id,
10639+
forward_info,
10640+
};
1063110641
match forward_htlcs.entry(scid) {
1063210642
hash_map::Entry::Occupied(mut entry) => {
10633-
entry.get_mut().push(HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo {
10634-
prev_short_channel_id,
10635-
prev_counterparty_node_id,
10636-
prev_funding_outpoint,
10637-
prev_channel_id,
10638-
prev_htlc_id,
10639-
prev_user_channel_id,
10640-
forward_info,
10641-
}));
10643+
entry.get_mut().push(HTLCForwardInfo::AddHTLC(pending_add));
1064210644
},
1064310645
hash_map::Entry::Vacant(entry) => {
1064410646
if !is_our_scid
10645-
&& forward_info.incoming_amt_msat.is_some()
10647+
&& pending_add.forward_info.incoming_amt_msat.is_some()
1064610648
&& fake_scid::is_valid_intercept(
1064710649
&self.fake_scid_rand_bytes,
1064810650
scid,
1064910651
&self.chain_hash,
1065010652
) {
1065110653
let intercept_id = InterceptId(
10652-
Sha256::hash(&forward_info.incoming_shared_secret)
10654+
Sha256::hash(&pending_add.forward_info.incoming_shared_secret)
1065310655
.to_byte_array(),
1065410656
);
1065510657
let mut pending_intercepts =
@@ -10659,57 +10661,35 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1065910661
new_intercept_events.push_back((
1066010662
events::Event::HTLCIntercepted {
1066110663
requested_next_hop_scid: scid,
10662-
payment_hash: forward_info.payment_hash,
10663-
inbound_amount_msat: forward_info
10664+
payment_hash,
10665+
inbound_amount_msat: pending_add
10666+
.forward_info
1066410667
.incoming_amt_msat
1066510668
.unwrap(),
10666-
expected_outbound_amount_msat: forward_info
10669+
expected_outbound_amount_msat: pending_add
10670+
.forward_info
1066710671
.outgoing_amt_msat,
1066810672
intercept_id,
1066910673
},
1067010674
None,
1067110675
));
10672-
entry.insert(PendingAddHTLCInfo {
10673-
prev_short_channel_id,
10674-
prev_counterparty_node_id,
10675-
prev_funding_outpoint,
10676-
prev_channel_id,
10677-
prev_htlc_id,
10678-
prev_user_channel_id,
10679-
forward_info,
10680-
});
10676+
entry.insert(pending_add);
1068110677
},
1068210678
hash_map::Entry::Occupied(_) => {
1068310679
let logger = WithContext::from(
1068410680
&self.logger,
1068510681
None,
1068610682
Some(prev_channel_id),
10687-
Some(forward_info.payment_hash),
10683+
Some(payment_hash),
1068810684
);
1068910685
log_info!(
1069010686
logger,
1069110687
"Failed to forward incoming HTLC: detected duplicate intercepted payment over short channel id {}",
1069210688
scid
1069310689
);
10694-
let routing = &forward_info.routing;
10695-
let htlc_source =
10696-
HTLCSource::PreviousHopData(HTLCPreviousHopData {
10697-
short_channel_id: prev_short_channel_id,
10698-
user_channel_id: Some(prev_user_channel_id),
10699-
counterparty_node_id: Some(
10700-
prev_counterparty_node_id,
10701-
),
10702-
outpoint: prev_funding_outpoint,
10703-
channel_id: prev_channel_id,
10704-
htlc_id: prev_htlc_id,
10705-
incoming_packet_shared_secret: forward_info
10706-
.incoming_shared_secret,
10707-
phantom_shared_secret: None,
10708-
blinded_failure: routing.blinded_failure(),
10709-
cltv_expiry: routing.incoming_cltv_expiry(),
10710-
});
10711-
10712-
let payment_hash = forward_info.payment_hash;
10690+
let htlc_source = HTLCSource::PreviousHopData(
10691+
pending_add.htlc_previous_hop_data(),
10692+
);
1071310693
let reason = HTLCFailReason::from_failure_code(
1071410694
LocalHTLCFailureReason::UnknownNextPeer,
1071510695
);
@@ -10726,15 +10706,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1072610706
},
1072710707
}
1072810708
} else {
10729-
entry.insert(vec![HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo {
10730-
prev_short_channel_id,
10731-
prev_counterparty_node_id,
10732-
prev_funding_outpoint,
10733-
prev_channel_id,
10734-
prev_htlc_id,
10735-
prev_user_channel_id,
10736-
forward_info,
10737-
})]);
10709+
entry.insert(vec![HTLCForwardInfo::AddHTLC(pending_add)]);
1073810710
}
1073910711
},
1074010712
}

0 commit comments

Comments
 (0)