Skip to content

Commit b22a843

Browse files
committed
Abort RefundBuilder creation on insufficient liquidity
Add a liquidity check in `create_refund_builder` to ensure that refund creation is aborted if there is insufficient outbound liquidity. This change prevents attempts to create a refund when the available liquidity is below the required amount, ensuring that refunds are only created when there is adequate liquidity.
1 parent 15653e8 commit b22a843

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9231,6 +9231,27 @@ macro_rules! create_refund_builder { ($self: ident, $builder: ty) => {
92319231
.and_then(|paths| paths.into_iter().next().ok_or(()))
92329232
.map_err(|()| Bolt12RequestError::BlindedPathCreationFailed)?;
92339233

9234+
let total_liquidity: u64 = {
9235+
let per_peer_state = &$self.per_peer_state.read().unwrap();
9236+
let mut sum: u64 = 0;
9237+
9238+
per_peer_state.iter().for_each(|(_, peer_state_mutex)| {
9239+
let peer_state_lock = peer_state_mutex.lock().unwrap();
9240+
let peer_state = &*peer_state_lock;
9241+
9242+
sum += peer_state.channel_by_id
9243+
.iter()
9244+
.map(|(_, phase)| phase.context().get_available_balances(&$self.fee_estimator).outbound_capacity_msat)
9245+
.sum::<u64>();
9246+
});
9247+
sum
9248+
};
9249+
9250+
if amount_msats > total_liquidity {
9251+
log_error!($self.logger, "Insufficient liquidity for payment with payment id: {}", payment_id);
9252+
return Err(Bolt12RequestError::InsufficientLiquidity);
9253+
}
9254+
92349255
let builder = RefundBuilder::deriving_signing_pubkey(
92359256
node_id, expanded_key, nonce, secp_ctx, amount_msats, payment_id
92369257
)?

0 commit comments

Comments
 (0)