sweep: keep budget-bound fees publishable #11534
🔴 PR Severity: CRITICAL
gh pr files | 6 files | 256 lines changed
<details>
<summary>🔴 <strong>Critical</strong> (3 files)</summary>
lnwallet/chainfee/rates.go - fee rate estimation logic used by wallet/sweep fee calculationssweep/fee_bumper.go - core fee-bumping logic for sweeping outputs on-chainsweep/sweeper.go - output sweeping / fund recovery logic</details>
<details>
<summary>🟢 <strong>Low</strong> (3 files)</summary>
docs/release-notes/unreleased/11534.md - release noteslnwallet/chainfee/rates_test.go - test-only changesweep/fee_bumper_test.go - test-only change</details>
Analysis
This PR modifies fee rate calculation (lnwallet/chainfee/rates.go) and fee-bumping/sweeping logic (sweep/fee_bumper.go, sweep/sweeper.go). Both lnwallet/ and sweep/ are CRITICAL-tier packages governing on-chain fund recovery and fee-bumping behavior, so an expert review is recommended. The change touches multiple distinct critical packages, though it remains small in scope (4 non-test files, ~105 non-test lines changed), well under the thresholds that would otherwise trigger a severity bump.
---
<sub>To override, add a severity-override-{critical,high,medium,low} label.</sub>
<!-- pr-severity-bot -->
Both cases are fixed when the set has a required output. One is left: without a required output, as in an anchor CPFP, dust change still returns ErrTxNoOutput, so a set created at its deadline fails the same way in every block.
Example: an anchor (330 sat), budget 100,000 sat, and one 99,671 sat P2WKH UTXO, 767 wu with the change output.
1. The floored rate is 130,378 sat/kw, and the fee is 99,999 sat.
2. The change is 2 sat, below the 330 sat dust limit.
3. There's no required output, so prepareSweepTx returns ErrTxNoOutput.
It needs the spendable value to land less than the dust limit above the budget, so it's rare, but once it happens it repeats. One fix: when there's no required output, keep a change output at the dust limit and lower the fee by the difference (99,671 sat here instead of 99,999). A test with this anchor set at confTarget = 1 would cover it.
⚠️ Severity changed: critical → low (files changed since last classification)
🟢 PR Severity: LOW
override label severity-override-low | 9 files | 542 lines changedA severity-override-low label is present on this PR, so automated classification was skipped and the severity is pinned to LOW per the override.
Analysis
For reference, since the last bot comment the file set changed: three files were added (lnrpc/walletrpc/walletkit.pb.go, lnrpc/walletrpc/walletkit.proto, lnrpc/walletrpc/walletkit.swagger.json), alongside the previously-flagged lnwallet/chainfee/rates.go, sweep/fee_bumper.go, and sweep/sweeper.go changes. Absent the override, this would likely classify as HIGH or CRITICAL (the PR touches sweep/ and lnwallet/, which are CRITICAL-tier packages, plus lnrpc//.proto, which are HIGH/MEDIUM-tier). Since a maintainer has explicitly overridden this to LOW, no further classification was performed.
---
<sub>To override, add a severity-override-{critical,high,medium,low} label.</sub>
<!-- pr-severity-bot -->
Reviewed all 4 commits at 1941871689f9377424fc4a533eb2a55e78507753 and ran the unit tests with -race:
go test -v -race -count=1 ./lnwallet/chainfee/... ./sweep/...
The floor constructor (NewSatPerKWeightFloor) in MaxFeeRateAllowed and tracking discardedDust through prepareSweepTx -> sweepTxCtx.exceedsBudget cleanly solve the boundary rejections when required outputs are present.
One boundary question on commit 194187168 (sweep: retain dust change for outputless sweeps) in sweep/fee_bumper.go:1825-1836:
case changeAmt < changeFloor && requiredOutput == 0:
feeReduction := changeFloor - changeAmt
if feeReduction > txFee {
return nil, ErrTxNoOutput
}
txFee -= feeReduction
changeAmt = changeFloor
Since changeAmt = totalInput - txFee when requiredOutput == 0, feeReduction simplifies to txFee - (totalInput - changeFloor):
1. totalInput == changeFloor (feeReduction == txFee):
If the input value equals changeFloor (e.g. a lone 330-sat anchor input with a 330-sat P2TR changeFloor), feeReduction == txFee for any txFee <= 330. Because the check is strict (feeReduction > txFee), prepareSweepTx no longer returns ErrTxNoOutput and instead sets txFee = 0 and changeAmt = 330.
2. totalInput - changeFloor below the relay fee floor:
When totalInput > changeFloor by less than the minimum relay fee, clamping txFee down to totalInput - changeFloor produces a fee below mempool relay requirements. In createRBFCompliantTx, CheckMempoolAcceptance rejects it with ErrMinRelayFeeNotMet, and each retry step rebuilds the same clamped txFee = totalInput - changeFloor until the fee function reaches max fee position or txFee > totalInput hits ErrNotEnoughInputs, without ever becoming publishable.
Would it make sense to use if feeReduction >= txFee (or check that the reduced txFee still covers the minimum relay fee floor) and add a unit test for the totalInput == changeFloor equality case?
Looks good overall the budget boundary is pinned from both sides
in the tests.
One note, pre-existing and not related to this PR: the comment inTestCreateAndCheckTxAtBudgetBoundary mentions that the aux
sweeper's extra output isn't counted in MaxFeeRateAllowed's
weight estimate, so a derived rate can overshoot the budget by
that output's weight. The budget check still catches it
(ErrNotEnoughBudget -> retry next block, no overspend
broadcasts), but it may be worth a TODO(issue) so the limitation
doesn't just live in a test comment. No change needed here.
@starius: review reminder
@ziggie1984, remember to re-request review from reviewers when ready
Reviewed all 4 commits at1941871689f9377424fc4a533eb2a55e78507753and ran the unit tests with-race:
>> go test -v -race -count=1 ./lnwallet/chainfee/... ./sweep/... >
> The floor constructor (NewSatPerKWeightFloor) inMaxFeeRateAllowedand trackingdiscardedDustthroughprepareSweepTx->sweepTxCtx.exceedsBudgetcleanly solve the boundary rejections when required outputs are present.
> One boundary question on commit194187168(sweep: retain dust change for outputless sweeps) insweep/fee_bumper.go:1825-1836:
>> case changeAmt < changeFloor && requiredOutput == 0: > feeReduction := changeFloor - changeAmt > if feeReduction > txFee { > return nil, ErrTxNoOutput > } > > txFee -= feeReduction > changeAmt = changeFloor >
> SincechangeAmt = totalInput - txFeewhenrequiredOutput == 0,feeReductionsimplifies totxFee - (totalInput - changeFloor):
> 1.totalInput == changeFloor(feeReduction == txFee):
If the input value equalschangeFloor(e.g. a lone 330-sat anchor input with a 330-sat P2TRchangeFloor),feeReduction == txFeefor anytxFee <= 330. Because the check is strict (feeReduction > txFee),prepareSweepTxno longer returnsErrTxNoOutputand instead setstxFee = 0andchangeAmt = 330.
> 2. totalInput - changeFloor below the relay fee floor:
WhentotalInput > changeFloorby less than the minimum relay fee, clampingtxFeedown tototalInput - changeFloorproduces a fee below mempool relay requirements. IncreateRBFCompliantTx,CheckMempoolAcceptancerejects it withErrMinRelayFeeNotMet, and each retry step rebuilds the same clampedtxFee = totalInput - changeFlooruntil the fee function reaches max fee position ortxFee > totalInputhitsErrNotEnoughInputs, without ever becoming publishable.
>
Would it make sense to useif feeReduction >= txFee(or check that the reducedtxFeestill covers the minimum relay fee floor) and add a unit test for thetotalInput == changeFloorequality case?
Confirmed both edges with a throwaway test against prepareSweepTx, and they look reachable in practice: the anchor resolver offers a lone anchor with budget 330 after the commitment confirms, so no wallet inputs get added. At every rate from 253 up to the budget max:
P2TR change (floor 330): fee = 0, the equality case.
P2WPKH change (floor 294): fee = 36 sat (81 sat/kw).
The second one suggests >= alone wouldn't be enough: there feeReduction is still below txFee, so the check doesn't fire and the tx goes out with 36 sat. The min relay check seems like the option that covers both.
+1 on the test too: as an experiment I changed the check to >= locally, and the whole sweep package still passes, so nothing pins the equality case today.
nit: fee * 1000 can wrap in int64 for a budget above ~9.22e15 sat (only reachable with an absurd RPC budget, and the resulting tx is just invalid). Capping at btcutil.MaxSatoshi would rule it out, if you think it's worth it.
A small observation on this branch, separate from what @Amirhosein already raised. Once the change hits changeFloor, the actual fee stays pinned at totalInput - changeFloor, no matter how much higher the fee rate goes. So from that point:
ErrInsufficientFee and handleReplacementTxError returns None every block;BumpResult.FeeRate still reports the nominal f.FeeRate(), which also feeds lastFeeRate/StartingFeeRate, while Fee reports what was actually paid.That's fund-safe, since there's nothing more to pay with a single output, and it's strictly better than the previous ErrTxNoOutput. A short comment noting that the bump saturates here would help, and reporting the effective rate might avoid confusing PendingSweeps output. What do you think?
nit: this test also passes if MaxFeeRateAllowed goes back to the old rounding NewSatPerKWeight: 100,000 sat * 1000 / 767 wu = 130,378.09 sat/kw, which rounds and floors to the same value. So only TestCreateAndCheckTxAtBudgetBoundary would catch a regression of the floor. If this test is also meant to guard it, a weight where the fraction is >= .5 would make the old rounding fail here.
Reviewed at 1941871689 and ran the chainfee and sweep unit tests. The floor conversion and the discardedDust accounting look right to me, and the overshoot over the budget stays bounded by the discarded dust. Left a note on the outputless branch, where the fee stops growing once the change hits changeFloor, and a +1 with some numbers on Amirhosein's min relay edge.
TechLateef
ziggie1984
Change
Keep budget-bound sweep transactions publishable at the fee boundary:
derive the maximum fee rate from the budget with explicit floor rounding, so converting it back to a fee cannot exceed the budget;
track change discarded below the dust limit separately and allow only that exact amount above the target fee budget;
retain
ErrNotEnoughBudgetfor every other unexplained overrun.Budgettherefore caps the fee targeted by the fee function. As before, change that cannot form a standard output is added to the actual transaction fee, but it no longer causes the transaction to be rejected by the final budget sanity check.Motivation
NewSatPerKWeightrounds to the nearest sat/kw. At the budget-derived maximum rate this can makeFeeForWeightexceed the budget by a few satoshis. In addition,prepareSweepTxadds dust change to the fee. The final strict budget check rejected both transactions before mempool acceptance, and a deadline sweep could deterministically fail again on every retry.The rounding fix is implemented as a reusable
chainfee.NewSatPerKWeightFloorconstructor. The dust exception carries its provenance through transaction construction instead of applying a generic dust-sized tolerance, so unrelated fee errors remain fatal to the attempt.Testing
go test ./lnwallet/chainfee ./sweep -count=1make lint-native