Skip to content

lightning-liquidity: Pre-/Refactors to prepare for persistence #4008

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lightning-liquidity/src/lsps1/msgs.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
// This file is Copyright its original authors, visible in version control
// history.
//
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
// You may not use this file except in accordance with one or both of these
// licenses.

//! Message, request, and other primitive types used to implement bLIP-51 / LSPS1.

use alloc::string::String;
Expand Down
3 changes: 2 additions & 1 deletion lightning-liquidity/src/lsps2/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
//
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option. You may not use this file except in accordance with one or both of these
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
// You may not use this file except in accordance with one or both of these
// licenses.

//! Contains the main bLIP-52 / LSPS2 client object, [`LSPS2ClientHandler`].
Expand Down
9 changes: 9 additions & 0 deletions lightning-liquidity/src/lsps2/msgs.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
// This file is Copyright its original authors, visible in version control
// history.
//
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
// You may not use this file except in accordance with one or both of these
// licenses.

//! Message, request, and other primitive types used to implement bLIP-52 / LSPS2.

use alloc::string::String;
Expand Down
85 changes: 52 additions & 33 deletions lightning-liquidity/src/lsps2/payment_queue.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
// This file is Copyright its original authors, visible in version control
// history.
//
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
// You may not use this file except in accordance with one or both of these
// licenses.

use alloc::vec::Vec;

use lightning::ln::channelmanager::InterceptId;
Expand All @@ -8,7 +17,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 {
Expand All @@ -17,37 +26,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,
Expand Down Expand Up @@ -90,24 +110,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 {
Expand Down
18 changes: 6 additions & 12 deletions lightning-liquidity/src/lsps2/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down
7 changes: 7 additions & 0 deletions lightning-liquidity/src/lsps2/utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
// This file is Copyright its original authors, visible in version control history.
//
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
// http://opensource.org/licenses/MIT>, at your option.
// You may not use this file except in accordance with one or both of these licenses.

//! Utilities for implementing the bLIP-52 / LSPS2 standard.

use crate::lsps2::msgs::LSPS2OpeningFeeParams;
Expand Down
Loading
Loading