Repository navigation
Count the funding fee in the anchor reserve check for splice-ins and fee bumps - #1223
Conversation
|
👋 Thanks for assigning @jkczyz as a reviewer! |
afede9a to
39ef2ad
Compare
39ef2ad to
0c1cba9
Compare
0689499 to
915b7dd
Compare
tnull
left a comment
There was a problem hiding this comment.
Some Claude review:
- A fee bump of an exact splice-in can still pay from the reserve (src/lib.rs:2505)
- bump_channel_funding_fee rebuilds a coin-selected contribution through rbf_prior_contribution(...) with the wallet and goes straight to funding_contributed.
- Nothing on that path checks the anchor reserve.
- So splice_in at a low fee rate followed by a bump at a high one reaches the state this PR is meant to prevent.
- The commit says "Part of #1166", so this may be a planned follow-up. If so, the changelog entry is fine as scoped, but it is worth confirming with the author.
- The check uses feerate, but the contribution may pay up to max_feerate (src/lib.rs:1972-1976, question for the author)
- The splice_in docs say we may pay up to 1.5 times the feerate when the counterparty initiates a splice concurrently and our contribution is added to theirs.
- If LDK takes that extra fee from our change output, the reserve can be undercut by the difference after this check has passed.
- I did not verify how LDK adjusts the contribution in that case, as the LDK sources are not in the local cargo cache.
- If it does reduce change, checking against the fee at max_feerate would close the gap.
Seems we'd need to at the very least also do the check in bump_channel_funding_fee.
915b7dd to
dd9b622
Compare
|
Thanks!
|
tnull
left a comment
There was a problem hiding this comment.
Can we rearrange the commits so we don't first add logic that we then extract into a helper? Seems we can get rid of commit 3 if we just add the logic to the helper directly?
Also claude has some feedback:
- A bump before the wallet has synced the pending splice can be rejected for no reason (src/lib.rs:2530-2533)
- The delta check is exact once the wallet has seen the pending splice transaction: the prior inputs are spent and the prior change counts as spendable.
- Before that sync, the prior inputs are only locked. The spendable balance then excludes them and has no change to credit, so it understates by the prior change.
- Example: a single 1,000,000 sat UTXO, splice_in of 100,000, then an immediate bump. Spendable reads 0, so any bump fails with InsufficientFunds although about 875,000 sats are free.
- This errs on the safe side and clears with the next wallet sync. On Esplora or Electrum, though, that leaves a window in which a bump is refused.
- Both new tests, and every existing bump test I checked, call sync_wallets() between the splice and the bump, so none covers this.
- Options: credit the prior change when the prior inputs are still locked and unspent, or document that a bump needs a wallet sync first. Either way, a test that bumps without syncing would pin the behaviour.
- The check still uses the selected feerate, not max_feerate (src/lib.rs:1667-1676, carried over)
- LDK can raise the contribution's fee up to max_feerate out of its change when the counterparty initiates concurrently or the RBF minimum has moved.
- The reserve can therefore still be undercut by up to half the fee after the check passes.
- This applies to both splice_in and the bump. Adding estimated_fee * (max_feerate - feerate) / feerate as headroom would close it; otherwise the docs should say so.
dd9b622 to
1f5c87a
Compare
|
Thanks, reworked:
|
jkczyz
left a comment
There was a problem hiding this comment.
Some comments and a Claude review.
| fn check_sufficient_onchain_funds_for_contribution( | ||
| &self, contribution: FundingContribution, prior_contribution: Option<&FundingContribution>, | ||
| confirmed_unlocked_sats: u64, | ||
| ) -> Result<FundingContribution, Error> { |
There was a problem hiding this comment.
Could you document that contribution is released via release_inputs_and_outputs on error?
There was a problem hiding this comment.
Done, added it to the doc.
| expect_channel_ready_event!(node_b, node_a.node_id()); | ||
|
|
||
| let anchor_reserve_sat = 25_000; | ||
| assert_eq!(node_a.list_balances().total_anchor_channels_reserve_sats, anchor_reserve_sat); |
There was a problem hiding this comment.
🤖 This fails on CI with left: 24999, right: 25000, and it will on every run: open_channel_with_all leaves 24,999 on-chain, and list_balances caps the reported reserve at the on-chain balance. open_channel_variants_reserve_funds_for_anchor_peers already expects anchor_reserve_sat - 1 here (L9334-9335).
There was a problem hiding this comment.
Good catch, fixed. It now expects anchor_reserve_sat - 1, same as open_channel_variants_reserve_funds_for_anchor_peers.
| })?, | ||
| FundingAmount::Exact { amount_sats } => { | ||
| // Coin selection locks the inputs it picks, so read our funds before it. | ||
| let confirmed_unlocked_sats = self.wallet.get_confirmed_unlocked_amount_sats(); |
There was a problem hiding this comment.
🤖 The pre-check in check_sufficient_onchain_funds counts unconfirmed change, but this one doesn't. So with unconfirmed change from an on-chain send, spendable_onchain_balance_sats can cover the splice and it still fails with InsufficientFunds after coin selection. That seems defensible since anchor bumping only spends confirmed outputs, but is it intended? If so, should the pre-check use the same funds so it fails before coin selection?
There was a problem hiding this comment.
Yes, intended, since splices only spend confirmed outputs. Agreed on the pre-check though: for splice-ins it now uses the same confirmed funds, so a plain lack of funds fails before coin selection again.
| "{change_sat} sats of change after a splice fee of {fee_sat} sats" | ||
| ); | ||
|
|
||
| // We don't sync node_b's wallet before bumping, so it may not have seen the splice transaction |
There was a problem hiding this comment.
🤖 With a bitcoind chain source, the node polls the mempool every 2 seconds, so the wallet may have seen the splice by the time we bump. Only Esplora and Electrum keep it unsynced. Could this test pin one of those so the unsynced case is always covered?
There was a problem hiding this comment.
Makes sense, pinned it to Esplora.
There was a problem hiding this comment.
Hmm, generally we try to keep all tests on random_chain_source to make sure to get coverage independently of chain source. Do we see a straightforward alternative that would allow us to test this in a robust way without pinning one specific chain source?
There was a problem hiding this comment.
Fair, switched it back to random_chain_source. The unsynced case is now covered by a wallet unit test instead: it checks that the funds we check against stay the same before and after the wallet sees the splice transaction.
|
|
||
| let dust_limit_sats = change_output.script_pubkey.minimal_non_dust().to_sat(); | ||
| let min_change_sats = match change_output.value.to_sat().checked_sub(max_fee_increase_sats) { | ||
| Some(change_sats) if change_sats >= dust_limit_sats => change_sats, |
There was a problem hiding this comment.
🤖 No test drops the change below dust here. test_funding_contribution_with_parts in pending_payment_store.rs could build the contribution for a unit test, though it sets max_feerate to feerate, so it would need a parameter for that.
There was a problem hiding this comment.
Added unit tests for max_wallet_spend_sats, including change landing exactly on the dust limit and one sat below it. The max_feerate parameter went into a small prefactor commit.
1f5c87a to
30ec67f
Compare
The helper used the input-selection feerate as the contribution's maximum feerate too. Tests of how much a contribution's fee may still rise need the two to differ, so take the maximum as its own parameter. The existing callers pass their feerate, as before. Part of lightningdevkit#1166. Developed with assistance from Claude Code.
Splicing in an exact amount only checked that our on-chain funds cover the amount on top of the anchor reserve. The fee for our contribution to the splice transaction is paid from the same funds, though, and coin selection doesn't keep the reserve aside, so the fee could be taken from it. LDK may also raise that fee later, up to the contribution's maximum feerate, when our contribution joins a splice the counterparty initiated or the RBF minimum feerate rises. It pays the difference from our change. Once the contribution is built, check in `check_sufficient_onchain_funds_for_contribution` that the most it may take from our wallet, its inputs less the least change it may return, leaves the anchor reserve of our confirmed funds that aren't locked for a pending funding. These are read before coin selection, which locks the inputs it picks. Unconfirmed change is left out, so the check doesn't depend on whether wallet sync has seen a pending splice's transaction yet. If the contribution doesn't fit, release what it reserved and fail with `ChannelSplicingFailed`, as splices do for other failures. Splices only spend confirmed outputs, so check the amount alone against the same confirmed funds too, right before coin selection, where a plain lack of funds still fails with `InsufficientFunds`. It now runs after the check for a pending splice of ours, whose locked inputs would otherwise make it fail for lack of funds first. The method takes the contribution being replaced as `prior_contribution`, for the fee bump check that follows: its inputs are ours again unless the replacement spends them, and a replacement that takes no more than it is fine. Part of lightningdevkit#1166. Developed with assistance from Claude Code.
`bump_channel_funding_fee` didn't check the anchor reserve at all. A bump of an exact splice-in pays the higher fee from our wallet: LDK shrinks the pending contribution's change or, past its maximum feerate, re-runs coin selection. A bump of a splice-out whose channel balance can't cover the higher fee falls back to funding the whole withdrawal and fees from our wallet. Neither keeps the reserve aside. Read our confirmed, unlocked funds before LDK builds the replacement, and check it with `check_sufficient_onchain_funds_for_contribution` against the pending contribution. Once the replacement confirms, the pending contribution's inputs are ours again unless the replacement spends them too, and the most the replacement may take has to leave the anchor reserve of all that. A replacement that takes no more than the pending contribution may still passes. That keeps bumps of `splice_in_with_all`, and splice-out bumps the channel pays for, working when the wallet holds less than the reserve. If the check fails, release what the replacement reserved and fail with `ChannelSplicingFailed`. The check leaves out unconfirmed change, so it gives the same result whether wallet sync has seen the pending splice's transaction, only an earlier round of it, or neither. Part of lightningdevkit#1166. Developed with assistance from Claude Code.
30ec67f to
15d0afc
Compare
Splicing in only checked that our on-chain funds cover the amount on top of the anchor reserve. The fee for our contribution to the splice transaction is paid from the same funds, though, and coin selection doesn't keep the reserve aside, so the fee could be taken from it. Once coin selection has picked our inputs, compute what the splice takes from our wallet, its inputs less the change LDK returns, and check that it leaves the anchor reserve of our confirmed funds. Splices only spend confirmed outputs, so check the amount alone against the same funds before coin selection too. Backport of lightningdevkit#1223 (6fb3d52), adapted to LDK 0.2: `splice_channel` takes the exact feerate and computes our change in the same call, so the check runs before it, with a copy of LDK's change calculation in `splice_in_wallet_spend_sats`, as LDK's input values and weight constants are crate-private. There's no later fee increase to leave room for, and no inputs to release, as BDK 2.2 doesn't lock them. Unlike upstream, a fee that would spend part of the reserve fails with `InsufficientFunds` rather than `ChannelSplicingFailed`, consistently with the other splice-in fee shortfalls on this line: as the fee here is exact, the reserve would certainly be spent.
…nfirmed_unlocked_amount_sats Follow-up to the previous commit, the same patch. The wallet's sum becomes upstream's Wallet::get_confirmed_unlocked_amount_sats (ldk-node lightningdevkit#1223, 6fb3d52) word for word, taken alone with its unit test, so that the Blocknil part is the public wrapper (Node::confirmed_spendable_onchain_balance_sats, less the anchor reserve) and moving to upstream drops the copy. Upstream's sum leaves out what ours also skipped (immature coinbase outputs, a coin whose transaction is too large for tx_add_input); neither is on a provider's wallet, and either would only make the figure higher than coin selection can use, as before lightningdevkit#211's fix. The rest of lightningdevkit#1223 (the splice anchor-reserve checks) isn't taken: it conflicts with our change-keeping splice_in in splice_channel. Tests: confirmed_unlocked_amount_does_not_depend_on_seeing_a_splice (upstream's; the splice is signed here for bdk_wallet 3.2.1, ldk3-bdk-3.2.1). The integration test drops its "in a block but not synced" assertion: the node's background sync can run first, so it raced (failed once).
The helper used the input-selection feerate as the contribution's maximum feerate too. Tests of how much a contribution's fee may still rise need the two to differ, so take the maximum as its own parameter. The existing callers pass their feerate, as before. Part of lightningdevkit#1166. Developed with assistance from Claude Code. (cherry picked from commit eca8587) Upstream-PR: lightningdevkit#1223
Splicing in an exact amount only checked that our on-chain funds cover the amount on top of the anchor reserve. The fee for our contribution to the splice transaction is paid from the same funds, though, and coin selection doesn't keep the reserve aside, so the fee could be taken from it. LDK may also raise that fee later, up to the contribution's maximum feerate, when our contribution joins a splice the counterparty initiated or the RBF minimum feerate rises. It pays the difference from our change. Once the contribution is built, check in `check_sufficient_onchain_funds_for_contribution` that the most it may take from our wallet, its inputs less the least change it may return, leaves the anchor reserve of our confirmed funds that aren't locked for a pending funding. These are read before coin selection, which locks the inputs it picks. Unconfirmed change is left out, so the check doesn't depend on whether wallet sync has seen a pending splice's transaction yet. If the contribution doesn't fit, release what it reserved and fail with `ChannelSplicingFailed`, as splices do for other failures. Splices only spend confirmed outputs, so check the amount alone against the same confirmed funds too, right before coin selection, where a plain lack of funds still fails with `InsufficientFunds`. It now runs after the check for a pending splice of ours, whose locked inputs would otherwise make it fail for lack of funds first. The method takes the contribution being replaced as `prior_contribution`, for the fee bump check that follows: its inputs are ours again unless the replacement spends them, and a replacement that takes no more than it is fine. Part of lightningdevkit#1166. Developed with assistance from Claude Code. Merged by hand into Blocknil's splice_in_inner: the Exact arm keeps both wallets (the change-keeping one behind the Blocknil patch Node::splice_in_with_change), and both get upstream's checks: the amount against confirmed funds before coin selection, and the built contribution against the anchor reserve after it. get_confirmed_unlocked_amount_sats and its test were already here from ldk3-confirmed-balance; only their Blocknil doc lines change. (cherry picked from commit 6fb3d52) Upstream-PR: lightningdevkit#1223
`bump_channel_funding_fee` didn't check the anchor reserve at all. A bump of an exact splice-in pays the higher fee from our wallet: LDK shrinks the pending contribution's change or, past its maximum feerate, re-runs coin selection. A bump of a splice-out whose channel balance can't cover the higher fee falls back to funding the whole withdrawal and fees from our wallet. Neither keeps the reserve aside. Read our confirmed, unlocked funds before LDK builds the replacement, and check it with `check_sufficient_onchain_funds_for_contribution` against the pending contribution. Once the replacement confirms, the pending contribution's inputs are ours again unless the replacement spends them too, and the most the replacement may take has to leave the anchor reserve of all that. A replacement that takes no more than the pending contribution may still passes. That keeps bumps of `splice_in_with_all`, and splice-out bumps the channel pays for, working when the wallet holds less than the reserve. If the check fails, release what the replacement reserved and fail with `ChannelSplicingFailed`. The check leaves out unconfirmed change, so it gives the same result whether wallet sync has seen the pending splice's transaction, only an earlier round of it, or neither. Part of lightningdevkit#1166. Developed with assistance from Claude Code. (cherry picked from commit 15d0afc) Upstream-PR: lightningdevkit#1223
…nd a reserve refusal in splice_in_with_change as InsufficientFunds From the review of r11, for the provider's splice on arrival (bitcoin-wallet lightningdevkit#211). - Node::confirmed_spendable_utxo_values: the value of each coin confirmed_spendable_onchain_balance_sats counts (Wallet::get_confirmed_unlocked_utxo_values, the filter of upstream's get_confirmed_unlocked_amount_sats). The provider sized a splice's fee for a flat 300 vB, so a wallet of many small coins was quoted, then refused after the customer's yes; with the values it can size the fee for the coins selection would use, largest first. - In splice_in_inner's Exact arm, when keep_change (splice_in_with_change), the anchor-reserve check after coin selection (upstream lightningdevkit#1223) fails with InsufficientFunds, as that method does when no coins leave change. It returned ChannelSplicingFailed, which a caller can't tell from a splice that can't start. Upstream's splice_in keeps ChannelSplicingFailed. Tests: splice_in_with_change_reports_a_fee_into_the_anchor_reserve_as_insufficient_funds (fails with ChannelSplicingFailed without the mapping, checked) and confirmed_spendable_balance_leaves_out_unconfirmed_change, which now also checks the values at each step.
Splicing in only checked that our on-chain funds cover the amount on top of the anchor reserve. The fee for our contribution to the splice transaction is paid from the same funds, though, and coin selection doesn't keep the reserve aside, so the fee could be taken from it. Once coin selection has picked our inputs, compute what the splice takes from our wallet, its inputs less the change LDK returns, and check that it leaves the anchor reserve of our confirmed funds. Splices only spend confirmed outputs, so check the amount alone against the same funds before coin selection too. Backport of lightningdevkit#1223 (6fb3d52), adapted to LDK 0.2: `splice_channel` takes the exact feerate and computes our change in the same call, so the check runs before it, with a copy of LDK's change calculation in `splice_in_wallet_spend_sats`, as LDK's input values and weight constants are crate-private. There's no later fee increase to leave room for, and no inputs to release, as BDK 2.2 doesn't lock them. Unlike upstream, a fee that would spend part of the reserve fails with `InsufficientFunds` rather than `ChannelSplicingFailed`, consistently with the other splice-in fee shortfalls on this line: as the fee here is exact, the reserve would certainly be spent.
Part of #1166.
When splicing in an exact amount, we checked that our on-chain funds cover the amount on top of the anchor reserve. We didn't check the fee for our contribution, which is paid from the same funds, or that LDK may raise that fee later, up to the contribution's maximum feerate, paying the difference from our change. Coin selection doesn't keep the reserve aside, so the fee could come out of it.
bump_channel_funding_feehad no reserve check at all. A bump of an exact splice-in pays the higher fee from our wallet: LDK shrinks the pending contribution's change or, past its maximum feerate, re-runs coin selection. A bump of a splice-out whose channel balance can't cover the higher fee falls back to funding the whole withdrawal and fees from our wallet, as discussed in #1181.Changes
test_funding_contribution_with_partstakes amax_feerate, for unit tests of how much a contribution's fee may still rise.check_sufficient_onchain_funds_for_contribution, run once LDK has built a contribution:max_feerateout of the change, when our contribution joins a splice the counterparty initiated or the RBF minimum feerate rises. It drops the change if less than dust would be left.max_wallet_spend_satsbounds this from the contribution'sestimated_fee,feerateandmax_feerate.splice_in_with_alland splice-out bumps the channel pays for keep working when the wallet holds less than the reserve.reserved_inputsandreserved_outputsreport, never what the contribution inherited from a pending splice, and fails withChannelSplicingFailed, as splices do for other failures.Wallet::get_confirmed_unlocked_amount_sats. So the result doesn't depend on whether wallet sync has seen a pending splice's transaction, or only an earlier round of it.splice_in: runs the check with no prior contribution. The amount's pre-check incheck_sufficient_onchain_fundsnow uses the same confirmed, unlocked funds for splice-ins, so a plain lack of funds still fails before coin selection, withInsufficientFundsas before. It runs right before coin selection, after the check for a pending splice of ours, which would otherwise fail for lack of funds first. Channel opens are unchanged.bump_channel_funding_fee: runs the check against the pending contribution. Bumps ofsplice_in_with_allthat rebuild the splice are unaffected, as they already keep the reserve as their own output (Buildsplice_in_with_allfrom manually selected inputs #1176).splice_in,bump_channel_funding_feeandsplice_out_max, and CHANGELOG entries.Testing
confirmed_unlocked_amount_does_not_depend_on_seeing_a_splice(wallet unit test): after coin selection locks a splice's inputs, the funds the check uses stay the same once the wallet sees the splice transaction, while the spendable balance picks up its change. This covers the case of a bump before wallet sync independently of the chain source.max_wallet_spend_sats: no change, the rounded-up room for a higher fee, no room to raise the feerate, change left exactly at the dust limit and one sat below it, and change too small to cover the higher fee.splice_in_fee_does_not_spend_anchor_reserve: node B holds 1,000,000 sats with a 25,000 sat anchor reserve.ChannelSplicingFailed: the fee alone would come out of the reserve.splice_in_bump_fee_does_not_spend_anchor_reserve, without syncing B's wallet between the splice and the bumps:ChannelSplicingFailed.splice_out_bump_fee_does_not_spend_anchor_reserve:ChannelSplicingFailed, and the inputs coin selection locked are released.Open question
Locked inputs while a replacement is pending. When a bump of a splice-in re-runs coin selection, the replacement can't reuse the pending splice's inputs, and those stay locked until one candidate confirms. The check makes sure the reserve holds whichever candidate confirms, but until then part of it can be tied up in those locked inputs. Should we hold back for that too, or is the window acceptable?
With this, the cases in #1166 are covered, apart from the question above.
This change was developed with the assistance of Claude Code.