Prefactor: Introduce `PaymentQueueEntry`
What changed, and why it matters
This commit is a simple internal code cleanup in the LSPS2 liquidity module. It replaces an inline tuple (payment hash + list of HTLCs) with a named struct called PaymentQueueEntry. No behavior changes, no security fixes, and no user-facing changes are visible in the diff.
No security action needed. Treat as normal refactoring review.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch refactors PaymentQueue to use a new PaymentQueueEntry struct instead of Vec<(PaymentHash, Vec
Changed components
lightning-liquidity/src/lsps2/payment_queue.rslightning-liquidity/src/lsps2/service.rsInspect captured patch +49 / −45
diff --git a/lightning-liquidity/src/lsps2/payment_queue.rs b/lightning-liquidity/src/lsps2/payment_queue.rs
index 3041353..aff9f51 100644
--- a/lightning-liquidity/src/lsps2/payment_queue.rs
+++ b/lightning-liquidity/src/lsps2/payment_queue.rs
@@ -8,7 +8,7 @@ use lightning_types::payment::PaymentHash;
/// remaining payments forwarded.
#[derive(Clone, Default, PartialEq, Eq, Debug)]
pub(crate) struct PaymentQueue {
- payments: Vec<(PaymentHash, Vec<InterceptedHTLC>)>,
+ payments: Vec<PaymentQueueEntry>,
}
impl PaymentQueue {
@@ -17,37 +17,48 @@ impl PaymentQueue {
}
pub(crate) fn add_htlc(&mut self, new_htlc: InterceptedHTLC) -> (u64, usize) {
- let payment = self.payments.iter_mut().find(|(p, _)| p == &new_htlc.payment_hash);
- if let Some((payment_hash, htlcs)) = payment {
+ let payment =
+ self.payments.iter_mut().find(|entry| entry.payment_hash == new_htlc.payment_hash);
+ if let Some(entry) = payment {
// HTLCs within a payment should have the same payment hash.
- debug_assert!(htlcs.iter().all(|htlc| htlc.payment_hash == *payment_hash));
+ debug_assert!(entry.htlcs.iter().all(|htlc| htlc.payment_hash == entry.payment_hash));
// The given HTLC should not already be present.
- debug_assert!(htlcs.iter().all(|htlc| htlc.intercept_id != new_htlc.intercept_id));
- htlcs.push(new_htlc);
+ debug_assert!(entry
+ .htlcs
+ .iter()
+ .all(|htlc| htlc.intercept_id != new_htlc.intercept_id));
+ entry.htlcs.push(new_htlc);
let total_expected_outbound_amount_msat =
- htlcs.iter().map(|htlc| htlc.expected_outbound_amount_msat).sum();
- (total_expected_outbound_amount_msat, htlcs.len())
+ entry.htlcs.iter().map(|htlc| htlc.expected_outbound_amount_msat).sum();
+ (total_expected_outbound_amount_msat, entry.htlcs.len())
} else {
let expected_outbound_amount_msat = new_htlc.expected_outbound_amount_msat;
- self.payments.push((new_htlc.payment_hash, vec![new_htlc]));
+ let entry =
+ PaymentQueueEntry { payment_hash: new_htlc.payment_hash, htlcs: vec![new_htlc] };
+ self.payments.push(entry);
(expected_outbound_amount_msat, 1)
}
}
- pub(crate) fn pop_greater_than_msat(
- &mut self, amount_msat: u64,
- ) -> Option<(PaymentHash, Vec<InterceptedHTLC>)> {
- let position = self.payments.iter().position(|(_payment_hash, htlcs)| {
- htlcs.iter().map(|htlc| htlc.expected_outbound_amount_msat).sum::<u64>() >= amount_msat
+ pub(crate) fn pop_greater_than_msat(&mut self, amount_msat: u64) -> Option<PaymentQueueEntry> {
+ let position = self.payments.iter().position(|entry| {
+ entry.htlcs.iter().map(|htlc| htlc.expected_outbound_amount_msat).sum::<u64>()
+ >= amount_msat
});
position.map(|position| self.payments.remove(position))
}
pub(crate) fn clear(&mut self) -> Vec<InterceptedHTLC> {
- self.payments.drain(..).map(|(_k, v)| v).flatten().collect()
+ self.payments.drain(..).map(|entry| entry.htlcs).flatten().collect()
}
}
+#[derive(Clone, PartialEq, Eq, Debug)]
+pub(crate) struct PaymentQueueEntry {
+ pub(crate) payment_hash: PaymentHash,
+ pub(crate) htlcs: Vec<InterceptedHTLC>,
+}
+
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub(crate) struct InterceptedHTLC {
pub(crate) intercept_id: InterceptId,
@@ -90,24 +101,23 @@ mod tests {
}),
(500_000_000, 2),
);
- assert_eq!(
- payment_queue.pop_greater_than_msat(500_000_000),
- Some((
- PaymentHash([100; 32]),
- vec![
- InterceptedHTLC {
- intercept_id: InterceptId([0; 32]),
- expected_outbound_amount_msat: 200_000_000,
- payment_hash: PaymentHash([100; 32]),
- },
- InterceptedHTLC {
- intercept_id: InterceptId([2; 32]),
- expected_outbound_amount_msat: 300_000_000,
- payment_hash: PaymentHash([100; 32]),
- },
- ]
- ))
- );
+
+ let expected_entry = PaymentQueueEntry {
+ payment_hash: PaymentHash([100; 32]),
+ htlcs: vec![
+ InterceptedHTLC {
+ intercept_id: InterceptId([0; 32]),
+ expected_outbound_amount_msat: 200_000_000,
+ payment_hash: PaymentHash([100; 32]),
+ },
+ InterceptedHTLC {
+ intercept_id: InterceptId([2; 32]),
+ expected_outbound_amount_msat: 300_000_000,
+ payment_hash: PaymentHash([100; 32]),
+ },
+ ],
+ };
+ assert_eq!(payment_queue.pop_greater_than_msat(500_000_000), Some(expected_entry),);
assert_eq!(
payment_queue.clear(),
vec![InterceptedHTLC {
diff --git a/lightning-liquidity/src/lsps2/service.rs b/lightning-liquidity/src/lsps2/service.rs
index 309d7ae..114ed8b 100644
--- a/lightning-liquidity/src/lsps2/service.rs
+++ b/lightning-liquidity/src/lsps2/service.rs
@@ -242,12 +242,10 @@ impl OutboundJITChannelState {
} => {
let mut payment_queue = core::mem::take(payment_queue);
payment_queue.add_htlc(htlc);
- if let Some((_payment_hash, htlcs)) =
- payment_queue.pop_greater_than_msat(*opening_fee_msat)
- {
+ if let Some(entry) = payment_queue.pop_greater_than_msat(*opening_fee_msat) {
let forward_payment = HTLCInterceptedAction::ForwardPayment(
*channel_id,
- FeePayment { htlcs, opening_fee_msat: *opening_fee_msat },
+ FeePayment { htlcs: entry.htlcs, opening_fee_msat: *opening_fee_msat },
);
*self = OutboundJITChannelState::PendingPaymentForward {
payment_queue,
@@ -277,12 +275,10 @@ impl OutboundJITChannelState {
) -> Result<ForwardPaymentAction, ChannelStateError> {
match self {
OutboundJITChannelState::PendingChannelOpen { payment_queue, opening_fee_msat } => {
- if let Some((_payment_hash, htlcs)) =
- payment_queue.pop_greater_than_msat(*opening_fee_msat)
- {
+ if let Some(entry) = payment_queue.pop_greater_than_msat(*opening_fee_msat) {
let forward_payment = ForwardPaymentAction(
channel_id,
- FeePayment { opening_fee_msat: *opening_fee_msat, htlcs },
+ FeePayment { htlcs: entry.htlcs, opening_fee_msat: *opening_fee_msat },
);
*self = OutboundJITChannelState::PendingPaymentForward {
payment_queue: core::mem::take(payment_queue),
@@ -311,12 +307,10 @@ impl OutboundJITChannelState {
opening_fee_msat,
channel_id,
} => {
- if let Some((_payment_hash, htlcs)) =
- payment_queue.pop_greater_than_msat(*opening_fee_msat)
- {
+ if let Some(entry) = payment_queue.pop_greater_than_msat(*opening_fee_msat) {
let forward_payment = ForwardPaymentAction(
*channel_id,
- FeePayment { htlcs, opening_fee_msat: *opening_fee_msat },
+ FeePayment { htlcs: entry.htlcs, opening_fee_msat: *opening_fee_msat },
);
*self = OutboundJITChannelState::PendingPaymentForward {
payment_queue: core::mem::take(payment_queue),
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.