Skip to content

Fee calculation rounding in wallet anchor #1354

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 5, 2025
Merged

Fee calculation rounding in wallet anchor #1354

merged 1 commit into from
Feb 5, 2025

Conversation

gijswijs
Copy link
Contributor

@gijswijs gijswijs commented Feb 5, 2025

This PR fixes an error with fee calculation in the wallet anchor. In cases where we pass a fee as sat/kvb we divide by a thousand
because that's what FundPsbt expects. However, with real world values like 1952 sat/kvb, this division results in a rounded down fee of 1
sat/vbyte, which then can be below the min_relay_fee value.

The itest has been updated to include the correct fee calculation when using sat/kvb values that would previously result in rounding errors. This ensures that the fee is always above the min_relay_fee value.

fixes: #1171

@gijswijs gijswijs requested review from ffranr and guggero February 5, 2025 13:26
wallet_anchor.go Outdated
// expects. We add 999 to round up to the nearest 1000 to prevent issues
// where the fee doesn't meet the min_relay_fee because of rounding
// down.
satPerVByte := uint64((feeRate.FeePerKVByte() + 999) / 1000)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about just: uint64(math.Ceil(float64(feeRate.FeePerKVByte()) / 1000))?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You like that more? I thought this was pretty elegant. It would have prevented an extra import if it weren't for the fact that we already use math. It also doesn't need the cast to float, which is obviously a huge performance gain. 😉

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, it is elegant. But I tend to always go with the more clear and easy to understand version. IMO with the "elegant" version you need the comment above (why are we adding 999?) while math.Ceil() by definition/name says: Rounding up.
But not going to block the PR on it...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm with Oli on this. I need to think less with math.Ceil. But Gij's + 999 is way more elegant. But I love to think less.

@coveralls
Copy link

coveralls commented Feb 5, 2025

Pull Request Test Coverage Report for Build 13161173239

Details

  • 0 of 3 (0.0%) changed or added relevant lines in 1 file are covered.
  • 29 unchanged lines in 6 files lost coverage.
  • Overall coverage increased (+0.005%) to 40.733%

Changes Missing Coverage Covered Lines Changed/Added Lines %
wallet_anchor.go 0 3 0.0%
Files with Coverage Reduction New Missed Lines %
commitment/tap.go 1 84.09%
tappsbt/create.go 2 53.22%
tapchannel/aux_leaf_signer.go 5 43.08%
tapchannel/aux_invoice_manager.go 5 83.13%
tapdb/multiverse.go 6 68.21%
universe/interface.go 10 52.81%
Totals Coverage Status
Change from base Build 13127541505: 0.005%
Covered Lines: 26773
Relevant Lines: 65728

💛 - Coveralls

Copy link
Contributor Author

@gijswijs gijswijs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way, FundPsbt already supports sat_per_kw in the lnd repo. This is slated for release with v0.19. So we could remove the rounding fix after we bump tapd to lnd v0.19. lightningnetwork/lnd#9013

This commit fixes the an error with fee calculation in the wallet
anchor. In cases where we pass a fee as sat/kvb we divide by a thousand
because that's what `FundPsbt` expects. However, with real world values
like 1952 sat/kvb, this division results in a rounded down fee of 1
sat/vbyte, whihc then can be below the min_relay_fee value.

The itest has been updated to include the correct fee calculation when
using sat/kvb values that would previously result in rounding errors.
This ensures that the fee is always above the min_relay_fee value.
Copy link
Contributor

@ffranr ffranr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fantastic investigation to find this! 👏

@ffranr ffranr added this pull request to the merge queue Feb 5, 2025
Merged via the queue into main with commit 9202f08 Feb 5, 2025
18 checks passed
@guggero guggero deleted the fee-rounding branch February 5, 2025 17:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: ✅ Done
Development

Successfully merging this pull request may close these issues.

[bug]: On signet, min-relay-fee is not met if fee is not specified. Fee will be 1 sat short
4 participants