Skip to content

multi: Share overflow-safe fee arithmetic and fix client caps - #1302

Open
hieblmi wants to merge 2 commits into
lightninglabs:masterfrom
hieblmi:hai/shared-safe-fee-arithmetic
Open

hieblmi wants to merge 2 commits into
lightninglabs:masterfrom
hieblmi:hai/shared-safe-fee-arithmetic

Conversation

@hieblmi

@hieblmi hieblmi commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

Reject extreme fee rates that could produce incorrect transaction fees in static Loop In and Instant Loop Out. Static Loop In receives rates from the server; instant-out rates come from the local LND estimator. Previously, multiplication could wrap before fee validation, allowing malformed rates through and producing incorrect fees.

This PR has two commits:

  1. Extract the server's overflow-safe arithmetic into utils.FeeForWeight, preserving rounding down and checking that the final fee fits btcutil.Amount.
  2. Use it for static Loop In fee validation and HTLC reconstruction, and both instant-out transaction builders. Preserve validation errors across abort notifications and include the tier and original rate in arithmetic errors.

Validation:

  • Affected package unit tests and focused race tests pass.
  • Static Loop In tests verify arithmetic causes, exactly one empty-signature abort request for the swap, invoice cancellation, and malformed backup nonce errors even when the abort notification fails.
  • Instant-out tests use a positive relay minimum and cover overflow that wraps to a small positive fee accepted by the old builders. Both regression cases fail against the original production code using a Go overlay.
  • make fmt, affected-package lint, commit-message lint, and added-line width checks pass.

Extract the server's big-integer fee arithmetic into a shared client
utility. Preserve rounding down while checking the final amount before
conversion, allowing intermediate products larger than int64.
@starius

starius commented Nov 18, 2026

Copy link
Copy Markdown
Collaborator

Findings:

  1. pushEmptySigs overwrites err, so the new errors lose their cause. The closure in InitHtlcAction assigns the outer err, and the new fee, err := lines reuse it. After a successful push the error reads "...allowed maximum: ", and a failed push replaces the cause with its own error. The swap still aborts with ErrFeeTooHigh, but the same bug already affects two existing branches: the high-fee and extreme-fee toNonces failures pass the overwritten err to returnError, and it can be nil. If startLoopInFsm's abort-early wait then sees OnError with a nil LastActionError, the StaticAddressLoopIn RPC reports success for a failed swap. Giving the closure its own variable (_, err := or pushErr) fixes all of these.

  2. The FSM test can't catch that. TestInitHtlcActionCancelsInvoiceOnFeeGuardFailure only asserts ErrorIs(ErrFeeTooHigh) and the invoice cancellation. The one-deposit HTLC weighs 444 WU, so even MaxInt64 gives an exact fee that fits in int64 and is rejected by the ordinary cap; only MaxUint64, which converts to a negative rate, reaches the new error branch. Please assert on a MaxUint64 case that the arithmetic cause survives and that the server receives exactly one empty-signature push for the swap hash; that would catch 1. The 3 × 4 matrix could then shrink to the over-cap case, MaxUint64 per tier, and one MaxInt64/weight + 1 case, which keeps a large positive rate whose product overflowed in the old code and is now rejected by the cap.

  3. The release note overstates the impact. createHtlcTx builds the HTLC tx from the same wrapped fee the check saw, so no tx could pay more than the cap; the bug let malformed rates through and produced wrong fees. Instant-out rates come from the local lnd estimator, not the server. Suggest: "Reject extreme fee rates that could produce incorrect transaction fees in static Loop In and Instant Loop Out."

Nits:

  • The three new error branches return the same error, so it doesn't say which tier failed or give the rate. The htlc fee validation debug log only runs after them.
  • instantout/fee_test.go passes a zero min relay fee. With a positive wallet-reported relay minimum, the old code already rejected the negative and zero fees these cases produce, so they don't show what the change adds in production. A rate whose product wraps to a small positive fee above that minimum would.
  • Formatting: staticaddr/loopin/actions.go:313 is 85 columns, the new branches lack the blank line before return that the rest of the function uses, and the test's nested loops push 15 lines past 80 columns.

@hieblmi
hieblmi force-pushed the hai/shared-safe-fee-arithmetic branch from 268ddbc to 27f682a Compare November 18, 2026 18:40
Use the shared fee arithmetic before applying static Loop In and instant
Loop Out fee limits. Overflowing multiplication could otherwise produce
negative or deceptively small fees that bypassed those limits.

Reject invalid server rates through the existing static Loop In abort
path, and validate reconstructed HTLC fees with the same arithmetic.
@hieblmi
hieblmi force-pushed the hai/shared-safe-fee-arithmetic branch from 27f682a to 9591b47 Compare November 18, 2026 18:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants