Skip to content
Closed
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
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,14 @@
built after the channel had already been negotiated.
- When acting as an LSPS2 service, we likewise account for the funding transaction's fee when
checking that a JIT channel can be opened while retaining the anchor reserve.
- Splicing in an exact amount now accounts for the fee of our contribution to the splice
transaction when checking the anchor reserve, and fails with `NodeError::InsufficientFunds` if
paying it would spend part of the reserve. Previously, the fee could be paid from the reserve.
- `Node::bump_channel_funding_fee` now checks the anchor reserve as well, and fails with
`NodeError::InsufficientFunds` if what the replacement takes from our on-chain funds beyond
the pending splice would spend part of the reserve. Previously, the reserve could pay the higher
fee of a splice-in, or fund a splice-out's withdrawal when the channel balance couldn't cover the
higher fee.

# 0.7.0 - Dec. 3, 2025
This seventh minor release introduces numerous new features, bug fixes, and API improvements. In particular, it adds support for channel Splicing, Async Payments, as well as sourcing chain data from a Bitcoin Core REST backend.
Expand Down
135 changes: 114 additions & 21 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1589,10 +1589,12 @@ impl Node {
Ok(new_channel_anchor_reserve_sats(&self.config, peer_node_id, anchor_channel))
}

/// Returns the spendable on-chain balance, i.e., excluding the anchor reserve, that
/// `amount_sats` was checked against.
fn check_sufficient_onchain_funds(
&self, amount_sats: u64, peer_node_id: &PublicKey,
new_channel_with_funding_fee_rate: Option<bitcoin::FeeRate>,
) -> Result<(), Error> {
) -> Result<u64, Error> {
let action_str = if new_channel_with_funding_fee_rate.is_some() {
"create channel"
} else {
Expand Down Expand Up @@ -1645,7 +1647,58 @@ impl Node {
}
}

Ok(())
Ok(spendable_amount_sats)
}

/// Checks, now that the fee of `contribution` is known, that it leaves the anchor reserve in
/// place. Otherwise, releases what it reserved for itself and fails with
/// [`Error::InsufficientFunds`].
///
/// What a contribution takes from our wallet is its inputs minus its change, fee included.
/// When it replaces `prior_contribution`, only what it takes beyond that one counts. This must
/// fit within `spendable_amount_sats`, the spendable balance read before `contribution` was
/// built, as building it locks the inputs it selects.
fn check_sufficient_onchain_funds_for_contribution(
&self, contribution: FundingContribution, prior_contribution: Option<&FundingContribution>,
spendable_amount_sats: u64,
) -> Result<FundingContribution, Error> {
let action_str =
if prior_contribution.is_some() { "bump the splice fee" } else { "splice-in" };
let wallet_funds_sats = |contribution: &FundingContribution| {
let inputs_sats: u64 =
contribution.inputs().iter().map(|input| input.output().value.to_sat()).sum();
let change_sats =
contribution.change_output().map_or(0, |output| output.value.to_sat());
inputs_sats.saturating_sub(change_sats)
};
let required_funds_sats = wallet_funds_sats(&contribution)
.saturating_sub(prior_contribution.map_or(0, wallet_funds_sats));
if required_funds_sats <= spendable_amount_sats {
return Ok(contribution);
}

log_error!(
self.logger,
"Unable to {} due to insufficient funds. Available: {}sats, Required: {}sats (including fee)",
action_str,
spendable_amount_sats,
required_funds_sats,
);

// Return what was set aside for the contribution we drop, but nothing it inherited from a
// pending splice: that splice's transaction may still confirm.
let inputs = contribution.reserved_inputs().map(|input| input.outpoint()).collect();
let outputs = contribution.reserved_outputs().cloned().collect();
if let Err(e) =
self.runtime.block_on(self.wallet.release_inputs_and_outputs(inputs, outputs))
{
log_error!(
self.logger,
"Failed to release the inputs of a dropped splice contribution: {}",
e
);
}
Err(Error::InsufficientFunds)
}

/// Connect to a node and open a new unannounced channel.
Expand Down Expand Up @@ -1928,10 +1981,6 @@ impl Node {
});
let max_feerate = max_funding_feerate(min_feerate);

if let FundingAmount::Exact { amount_sats } = splice_amount_sats {
self.check_sufficient_onchain_funds(amount_sats, &counterparty_node_id, None)?;
}

let funding_template = self
.channel_manager
.splice_channel(&channel_details.channel_id, &counterparty_node_id)
Expand Down Expand Up @@ -1959,18 +2008,36 @@ impl Node {
};

let contribution = match splice_amount_sats {
FundingAmount::Exact { amount_sats } => self
.runtime
.block_on(funding_template.splice_in(
Amount::from_sat(amount_sats),
feerate,
max_feerate,
Arc::clone(&self.wallet),
))
.map_err(|e| {
log_error!(self.logger, "Failed to splice channel: {}", e);
Error::ChannelSplicingFailed
})?,
FundingAmount::Exact { amount_sats } => {
// Coin selection locks the inputs it picks, which takes them off the spendable
// balance, so keep the balance from before it to check the contribution against.
let spendable_amount_sats = self.check_sufficient_onchain_funds(
amount_sats,
&counterparty_node_id,
None,
)?;

let contribution = self
.runtime
.block_on(funding_template.splice_in(
Amount::from_sat(amount_sats),
feerate,
max_feerate,
Arc::clone(&self.wallet),
))
.map_err(|e| {
log_error!(self.logger, "Failed to splice channel: {}", e);
Error::ChannelSplicingFailed
})?;

// The fee for our contribution is paid from the same on-chain funds as the
// amount, and coin selection doesn't keep the anchor reserve aside.
self.check_sufficient_onchain_funds_for_contribution(
contribution,
None,
spendable_amount_sats,
)?
},
FundingAmount::Max => self.splice_in_with_all_contribution(
funding_template,
Vec::new(),
Expand Down Expand Up @@ -2150,6 +2217,10 @@ impl Node {
/// pending splice that requires a higher feerate to be replaced, or when the counterparty
/// initiates a splice concurrently and our contribution is added to theirs.
///
/// The fee for our contribution is paid from the same on-chain funds as `splice_amount_sats`,
/// so both together must fit within [`BalanceDetails::spendable_onchain_balance_sats`], which
/// excludes the anchor reserve. Otherwise, this fails with [`NodeError::InsufficientFunds`].
///
/// # Experimental API
///
/// This API is experimental. Currently, a splice-in will be marked as an outbound payment, but
Expand Down Expand Up @@ -2267,8 +2338,9 @@ impl Node {
///
/// [`Node::bump_channel_funding_fee`] preserves the destination output value, including for
/// maximum withdrawals. If the channel cannot cover the higher fee, it may fund the entire
/// output and fees from the on-chain wallet instead, leaving the channel balance unwithdrawn.
/// It does not reduce the destination output to pay the additional fee.
/// output and fees from the on-chain wallet instead, leaving the channel balance unwithdrawn,
/// or fail with [`NodeError::InsufficientFunds`] if that would spend part of the anchor
/// reserve. It does not reduce the destination output to pay the additional fee.
///
/// Success means the contribution was submitted, not that the splice has completed. Monitor
/// [`Event::SpliceNegotiated`] or [`Event::SpliceNegotiationFailed`] for the negotiation result,
Expand Down Expand Up @@ -2396,6 +2468,11 @@ impl Node {
/// fees, leaving the channel balance unwithdrawn. This also applies to withdrawals created
/// with [`Node::splice_out_max`]; the output is not reduced to cover the higher fee.
///
/// What the replacement takes from our on-chain funds beyond what the pending splice takes,
/// such as the higher fee of a splice-in or a withdrawal funded by wallet inputs, must fit
/// within [`BalanceDetails::spendable_onchain_balance_sats`], which excludes the anchor
/// reserve. Otherwise, this fails with [`NodeError::InsufficientFunds`].
///
/// A splice of all on-chain funds (see [`Node::splice_in_with_all`]) has no amount to preserve:
/// the bump is paid by adding less to the channel, or, if the pending splice's maximum feerate
/// does not allow that or less than dust would be left to add, the splice is rebuilt from all
Expand Down Expand Up @@ -2447,6 +2524,14 @@ impl Node {
return Err(Error::ChannelSplicingFailed);
};

// Coin selection for the replacement, if LDK needs one, locks the inputs it picks, which
// takes them off the spendable balance, so keep the balance from before it to check the
// replacement against.
let cur_anchor_reserve_sats =
total_anchor_channels_reserve_sats(&self.channel_manager, &self.config);
let spendable_amount_sats =
self.wallet.get_spendable_amount_sats(cur_anchor_reserve_sats)?;

let rbf_template = funding_template.clone();
let prior_value_added_sats = funding_template
.prior_contribution()
Expand Down Expand Up @@ -2475,7 +2560,15 @@ impl Node {
},
};
let contribution = match adjusted {
Some(contribution) => contribution,
// The replacement may take more from our wallet than the pending splice does: a
// splice-in pays the higher fee from its change or newly selected coins, and a
// splice-out the channel can't pay it for is funded from our wallet entirely.
// Neither keeps the anchor reserve aside.
Some(contribution) => self.check_sufficient_onchain_funds_for_contribution(
contribution,
funding_template.prior_contribution(),
spendable_amount_sats,
)?,
// The pending splice spends manually selected inputs, as a splice-in of all funds
// does, so replace it with one built the same way from all confirmed funds: its
// inputs and any coins confirmed since.
Expand Down
Loading
Loading