Skip to content

Commit dc67685

Browse files
committed
Prefactor: Introduce PaymentQueueEntry
.. which streamlines the `PaymentQueue` API a bit, but most importantly can more easily get persisted using macros in the next step.
1 parent 3b16c77 commit dc67685

File tree

2 files changed

+49
-45
lines changed

2 files changed

+49
-45
lines changed

lightning-liquidity/src/lsps2/payment_queue.rs

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use lightning_types::payment::PaymentHash;
88
/// remaining payments forwarded.
99
#[derive(Clone, Default, PartialEq, Eq, Debug)]
1010
pub(crate) struct PaymentQueue {
11-
payments: Vec<(PaymentHash, Vec<InterceptedHTLC>)>,
11+
payments: Vec<PaymentQueueEntry>,
1212
}
1313

1414
impl PaymentQueue {
@@ -17,37 +17,48 @@ impl PaymentQueue {
1717
}
1818

1919
pub(crate) fn add_htlc(&mut self, new_htlc: InterceptedHTLC) -> (u64, usize) {
20-
let payment = self.payments.iter_mut().find(|(p, _)| p == &new_htlc.payment_hash);
21-
if let Some((payment_hash, htlcs)) = payment {
20+
let payment =
21+
self.payments.iter_mut().find(|entry| entry.payment_hash == new_htlc.payment_hash);
22+
if let Some(entry) = payment {
2223
// HTLCs within a payment should have the same payment hash.
23-
debug_assert!(htlcs.iter().all(|htlc| htlc.payment_hash == *payment_hash));
24+
debug_assert!(entry.htlcs.iter().all(|htlc| htlc.payment_hash == entry.payment_hash));
2425
// The given HTLC should not already be present.
25-
debug_assert!(htlcs.iter().all(|htlc| htlc.intercept_id != new_htlc.intercept_id));
26-
htlcs.push(new_htlc);
26+
debug_assert!(entry
27+
.htlcs
28+
.iter()
29+
.all(|htlc| htlc.intercept_id != new_htlc.intercept_id));
30+
entry.htlcs.push(new_htlc);
2731
let total_expected_outbound_amount_msat =
28-
htlcs.iter().map(|htlc| htlc.expected_outbound_amount_msat).sum();
29-
(total_expected_outbound_amount_msat, htlcs.len())
32+
entry.htlcs.iter().map(|htlc| htlc.expected_outbound_amount_msat).sum();
33+
(total_expected_outbound_amount_msat, entry.htlcs.len())
3034
} else {
3135
let expected_outbound_amount_msat = new_htlc.expected_outbound_amount_msat;
32-
self.payments.push((new_htlc.payment_hash, vec![new_htlc]));
36+
let entry =
37+
PaymentQueueEntry { payment_hash: new_htlc.payment_hash, htlcs: vec![new_htlc] };
38+
self.payments.push(entry);
3339
(expected_outbound_amount_msat, 1)
3440
}
3541
}
3642

37-
pub(crate) fn pop_greater_than_msat(
38-
&mut self, amount_msat: u64,
39-
) -> Option<(PaymentHash, Vec<InterceptedHTLC>)> {
40-
let position = self.payments.iter().position(|(_payment_hash, htlcs)| {
41-
htlcs.iter().map(|htlc| htlc.expected_outbound_amount_msat).sum::<u64>() >= amount_msat
43+
pub(crate) fn pop_greater_than_msat(&mut self, amount_msat: u64) -> Option<PaymentQueueEntry> {
44+
let position = self.payments.iter().position(|entry| {
45+
entry.htlcs.iter().map(|htlc| htlc.expected_outbound_amount_msat).sum::<u64>()
46+
>= amount_msat
4247
});
4348
position.map(|position| self.payments.remove(position))
4449
}
4550

4651
pub(crate) fn clear(&mut self) -> Vec<InterceptedHTLC> {
47-
self.payments.drain(..).map(|(_k, v)| v).flatten().collect()
52+
self.payments.drain(..).map(|entry| entry.htlcs).flatten().collect()
4853
}
4954
}
5055

56+
#[derive(Clone, PartialEq, Eq, Debug)]
57+
pub(crate) struct PaymentQueueEntry {
58+
pub(crate) payment_hash: PaymentHash,
59+
pub(crate) htlcs: Vec<InterceptedHTLC>,
60+
}
61+
5162
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
5263
pub(crate) struct InterceptedHTLC {
5364
pub(crate) intercept_id: InterceptId,
@@ -90,24 +101,23 @@ mod tests {
90101
}),
91102
(500_000_000, 2),
92103
);
93-
assert_eq!(
94-
payment_queue.pop_greater_than_msat(500_000_000),
95-
Some((
96-
PaymentHash([100; 32]),
97-
vec![
98-
InterceptedHTLC {
99-
intercept_id: InterceptId([0; 32]),
100-
expected_outbound_amount_msat: 200_000_000,
101-
payment_hash: PaymentHash([100; 32]),
102-
},
103-
InterceptedHTLC {
104-
intercept_id: InterceptId([2; 32]),
105-
expected_outbound_amount_msat: 300_000_000,
106-
payment_hash: PaymentHash([100; 32]),
107-
},
108-
]
109-
))
110-
);
104+
105+
let expected_entry = PaymentQueueEntry {
106+
payment_hash: PaymentHash([100; 32]),
107+
htlcs: vec![
108+
InterceptedHTLC {
109+
intercept_id: InterceptId([0; 32]),
110+
expected_outbound_amount_msat: 200_000_000,
111+
payment_hash: PaymentHash([100; 32]),
112+
},
113+
InterceptedHTLC {
114+
intercept_id: InterceptId([2; 32]),
115+
expected_outbound_amount_msat: 300_000_000,
116+
payment_hash: PaymentHash([100; 32]),
117+
},
118+
],
119+
};
120+
assert_eq!(payment_queue.pop_greater_than_msat(500_000_000), Some(expected_entry),);
111121
assert_eq!(
112122
payment_queue.clear(),
113123
vec![InterceptedHTLC {

lightning-liquidity/src/lsps2/service.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -242,12 +242,10 @@ impl OutboundJITChannelState {
242242
} => {
243243
let mut payment_queue = core::mem::take(payment_queue);
244244
payment_queue.add_htlc(htlc);
245-
if let Some((_payment_hash, htlcs)) =
246-
payment_queue.pop_greater_than_msat(*opening_fee_msat)
247-
{
245+
if let Some(entry) = payment_queue.pop_greater_than_msat(*opening_fee_msat) {
248246
let forward_payment = HTLCInterceptedAction::ForwardPayment(
249247
*channel_id,
250-
FeePayment { htlcs, opening_fee_msat: *opening_fee_msat },
248+
FeePayment { htlcs: entry.htlcs, opening_fee_msat: *opening_fee_msat },
251249
);
252250
*self = OutboundJITChannelState::PendingPaymentForward {
253251
payment_queue,
@@ -277,12 +275,10 @@ impl OutboundJITChannelState {
277275
) -> Result<ForwardPaymentAction, ChannelStateError> {
278276
match self {
279277
OutboundJITChannelState::PendingChannelOpen { payment_queue, opening_fee_msat } => {
280-
if let Some((_payment_hash, htlcs)) =
281-
payment_queue.pop_greater_than_msat(*opening_fee_msat)
282-
{
278+
if let Some(entry) = payment_queue.pop_greater_than_msat(*opening_fee_msat) {
283279
let forward_payment = ForwardPaymentAction(
284280
channel_id,
285-
FeePayment { opening_fee_msat: *opening_fee_msat, htlcs },
281+
FeePayment { htlcs: entry.htlcs, opening_fee_msat: *opening_fee_msat },
286282
);
287283
*self = OutboundJITChannelState::PendingPaymentForward {
288284
payment_queue: core::mem::take(payment_queue),
@@ -311,12 +307,10 @@ impl OutboundJITChannelState {
311307
opening_fee_msat,
312308
channel_id,
313309
} => {
314-
if let Some((_payment_hash, htlcs)) =
315-
payment_queue.pop_greater_than_msat(*opening_fee_msat)
316-
{
310+
if let Some(entry) = payment_queue.pop_greater_than_msat(*opening_fee_msat) {
317311
let forward_payment = ForwardPaymentAction(
318312
*channel_id,
319-
FeePayment { htlcs, opening_fee_msat: *opening_fee_msat },
313+
FeePayment { htlcs: entry.htlcs, opening_fee_msat: *opening_fee_msat },
320314
);
321315
*self = OutboundJITChannelState::PendingPaymentForward {
322316
payment_queue: core::mem::take(payment_queue),

0 commit comments

Comments
 (0)