Skip to content

Commit 9ffe63f

Browse files
committed
Abort if insufficient liquidity in pay_for_offer
This commit introduces a check to abort early if there's insufficient liquidity when fulfilling the payment in `pay_for_offer`
1 parent 952da4e commit 9ffe63f

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ use crate::offers::invoice::{BlindedPayInfo, Bolt12Invoice, DEFAULT_RELATIVE_EXP
6565
use crate::offers::invoice_error::InvoiceError;
6666
use crate::offers::invoice_request::{DerivedPayerId, InvoiceRequestBuilder};
6767
use crate::offers::nonce::Nonce;
68-
use crate::offers::offer::{Offer, OfferBuilder};
68+
use crate::offers::offer::{Amount, Offer, OfferBuilder};
6969
use crate::offers::parse::Bolt12SemanticError;
7070
use crate::offers::refund::{Refund, RefundBuilder};
7171
use crate::onion_message::async_payments::{AsyncPaymentsMessage, HeldHtlcAvailable, ReleaseHeldHtlc, AsyncPaymentsMessageHandler};
@@ -9038,6 +9038,22 @@ where
90389038
let reply_paths = self.create_blinded_paths(context)
90399039
.map_err(|_| Bolt12CreationError::BlindedPathCreationFailed)?;
90409040

9041+
let total_liquidity: u64 = self.list_usable_channels().iter().map(|channel| channel.next_outbound_htlc_limit_msat).sum();
9042+
let total_amount_msats = match invoice_request.amount_msats() {
9043+
Some(amount_msats) => Some(amount_msats),
9044+
None => match offer.amount() {
9045+
Some(Amount::Bitcoin { amount_msats }) => Some(amount_msats),
9046+
_ => None,
9047+
},
9048+
};
9049+
9050+
if let Some(amount) = total_amount_msats {
9051+
if amount > total_liquidity {
9052+
log_error!(self.logger, "Insufficient liquidity for payment with payment id: {}", payment_id);
9053+
return Err(Bolt12CreationError::InsufficientLiquidity);
9054+
}
9055+
}
9056+
90419057
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
90429058

90439059
let expiration = StaleExpiration::TimerTicks(1);

0 commit comments

Comments
 (0)