Repository navigation
sweep: keep budget-bound fees publishable - #11534
ziggie1984 wants to merge 4 commits into
Conversation
🔴 PR Severity: CRITICAL
🔴 Critical (3 files)
🟢 Low (3 files)
AnalysisThis PR modifies fee rate calculation ( To override, add a |
fe5a2fe to
52cc319
Compare
| if requiredOutput == 0 { | ||
| return 0, noChange, noLocktime, ErrTxNoOutput | ||
| return nil, ErrTxNoOutput | ||
| } |
There was a problem hiding this comment.
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.
- The floored rate is 130,378 sat/kw, and the fee is 99,999 sat.
- The change is 2 sat, below the 330 sat dust limit.
- There's no required output, so
prepareSweepTxreturnsErrTxNoOutput.
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.
🟢 PR Severity: LOW
A AnalysisFor reference, since the last bot comment the file set changed: three files were added ( To override, add a |
Amirhosein
left a comment
There was a problem hiding this comment.
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 = changeFloorSince changeAmt = totalInput - txFee when requiredOutput == 0, feeReduction simplifies to txFee - (totalInput - changeFloor):
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.totalInput - changeFloorbelow 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 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?
TechLateef
left a comment
There was a problem hiding this comment.
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 in
TestCreateAndCheckTxAtBudgetBoundary 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 |
Confirmed both edges with a throwaway test against
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. |
MPins
left a comment
There was a problem hiding this comment.
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.
| return 0 | ||
| } | ||
|
|
||
| return SatPerKWeight(fee * 1000 / btcutil.Amount(wu)) |
There was a problem hiding this comment.
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.
| // If there is no required output, discarding dust change would leave | ||
| // the transaction without any outputs. Reduce the fee just enough to | ||
| // create a standard change output instead. | ||
| case changeAmt < changeFloor && requiredOutput == 0: | ||
| feeReduction := changeFloor - changeAmt | ||
| if feeReduction > txFee { | ||
| return nil, ErrTxNoOutput | ||
| } | ||
|
|
||
| log.Infof("Change amt %v below dustlimit %v, reducing fee "+ | ||
| "by %v to retain the change output", changeAmt, | ||
| changeFloor, feeReduction) | ||
|
|
||
| txFee -= feeReduction | ||
| changeAmt = changeFloor | ||
|
|
||
| // If the change amount is dust and another output remains, move the | ||
| // change into the fee and omit the change output. |
There was a problem hiding this comment.
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:
- each RBF attempt pays the same absolute fee (only the locktime changes), so it ends in
ErrInsufficientFeeandhandleReplacementTxErrorreturnsNoneevery block; BumpResult.FeeRatestill reports the nominalf.FeeRate(), which also feedslastFeeRate/StartingFeeRate, whileFeereports 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?
| // anchor CPFP at its budget-derived maximum fee rate remains publishable when | ||
| // its change would otherwise be dust. Since there is no required output, the | ||
| // fee must be reduced just enough to retain a standard change output. | ||
| func TestPrepareSweepTxAtBudgetBoundaryWithoutRequiredOutput(t *testing.T) { |
There was a problem hiding this comment.
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.
Change
Keep budget-bound sweep transactions publishable at the fee boundary:
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