Skip to content

Commit debf857

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` The logic gets the available liquidity by summing up the  `next_outbound_htlc_limit_msat` from all usable channels. Then, compares the total liquidity amount with the total payment amount. If the payment amount is > the available liquidity, the function logs an error and returns a  Bolt12SemanticError::InsufficientLiquidity error.
1 parent 78c0eaa commit debf857

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ use crate::ln::wire::Encode;
6464
use crate::offers::invoice::{BlindedPayInfo, Bolt12Invoice, DEFAULT_RELATIVE_EXPIRY, DerivedSigningPubkey, ExplicitSigningPubkey, InvoiceBuilder, UnsignedBolt12Invoice};
6565
use crate::offers::invoice_error::InvoiceError;
6666
use crate::offers::invoice_request::{DerivedPayerId, InvoiceRequestBuilder};
67-
use crate::offers::offer::{Offer, OfferBuilder};
67+
use crate::offers::offer::{Offer, OfferBuilder, Amount};
6868
use crate::offers::parse::Bolt12SemanticError;
6969
use crate::offers::refund::{Refund, RefundBuilder};
7070
use crate::onion_message::async_payments::{AsyncPaymentsMessage, HeldHtlcAvailable, ReleaseHeldHtlc, AsyncPaymentsMessageHandler};
@@ -8816,6 +8816,26 @@ where
88168816
None => builder,
88178817
Some(payer_note) => builder.payer_note(payer_note),
88188818
};
8819+
8820+
let total_liquidity: u64 = self.list_usable_channels().iter().map(|channel| channel.next_outbound_htlc_limit_msat).sum();
8821+
let total_amount_msats = match amount_msats {
8822+
Some(amount_msats) => Some(amount_msats),
8823+
None => match offer.amount() {
8824+
Some(Amount::Bitcoin {amount_msats}) => Some(amount_msats),
8825+
_ => None,
8826+
},
8827+
};
8828+
8829+
if let Some(amount) = total_amount_msats {
8830+
return if amount > total_liquidity {
8831+
log_error!(self.logger, "Insufficient liquidity for payment with payment id: {}", payment_id);
8832+
Err(Bolt12SemanticError::InsufficientLiquidity)
8833+
} else {
8834+
log_error!(self.logger, "Offer amount is missing for payment with id: {}", payment_id);
8835+
Err(Bolt12SemanticError::InvalidAmount)
8836+
}
8837+
}
8838+
88198839
let invoice_request = builder.build_and_sign()?;
88208840

88218841
let context = OffersContext::OutboundPayment { payment_id };

lightning/src/offers/parse.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ pub enum Bolt12SemanticError {
193193
UnexpectedPaymentHash,
194194
/// A signature was expected but was missing.
195195
MissingSignature,
196+
/// There is insufficient liquidity to complete the payment.
197+
InsufficientLiquidity,
196198
}
197199

198200
impl From<bech32::Error> for Bolt12ParseError {

0 commit comments

Comments
 (0)