-
Notifications
You must be signed in to change notification settings - Fork 87
Apply the safe minimum fee floor to all wallet transactions #4192
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
Open
piotr-roslaniec
wants to merge
4
commits into
main
Choose a base branch
from
feat/floor-all-wallet-tx-fees
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ad89b38
Apply the safe minimum sweep-fee floor to all wallet transactions
mswilkison f76f4ba
docs: correct fee-floor per-request cap claims
piotr-roslaniec 6a70608
chore(tbtcpg): address fee-floor review feedback
piotr-roslaniec 1128d43
chore(tbtcpg): sharpen fee-floor error typing and per-request fee war…
piotr-roslaniec File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| package tbtcpg | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| ) | ||
|
|
||
| // ErrMaxFeeTooLow indicates that the Bridge maximum total fee is too low to | ||
| // build a wallet transaction at the safe minimum fee rate, so a non-underpriced | ||
| // transaction cannot be constructed. It mirrors the raw-estimate-too-high | ||
| // sentinels (ErrFeeTooHigh, ErrSweepTxFeeTooHigh): both signal an unserviceable | ||
| // fee configuration, one bounding from above and one from below. | ||
| var ErrMaxFeeTooLow = errors.New( | ||
| "minimum safe transaction fee exceeds the maximum fee", | ||
| ) | ||
|
|
||
| // minWalletTxSatPerVByteFee is the minimum fee rate, in sat/vByte, applied to | ||
| // wallet Bitcoin transactions (deposit sweeps, redemptions, moving funds, moved | ||
| // funds sweeps). A fee oracle can return an unusably low estimate (down to the | ||
| // 1 sat/vByte relay floor enforced by the Electrum client) in an uncongested | ||
| // mempool. Because these transactions spend or consolidate significant wallet | ||
| // value and are not RBF-enabled, they cannot be replaced once broadcast, so a | ||
| // floor-rate transaction can get stuck in the mempool and jam the wallet: no | ||
| // new wallet transaction can be built while the previous one is unconfirmed. | ||
| // This minimum keeps the fee safely above the relay floor while remaining far | ||
| // below the Bridge's maximum fee. The value is intentionally conservative and | ||
| // could be made configurable; see threshold-network/keep-core#4171. | ||
| // | ||
| // NOTE: this static floor and the 25% buffer applied in applyWalletTxFeeFloor | ||
| // are a stopgap for the current fire-and-forget, non-RBF wallet transaction | ||
| // path: because a stuck transaction cannot be fee-bumped, the fee must be right | ||
| // on the first broadcast. Once RBF / fee-bumping lands (Part B, tracked in | ||
| // #4171) the safety net shifts to monitor-and-bump, and this policy should be | ||
| // revisited rather than carried forward unchanged: the defensive buffer can be | ||
| // dropped and the floor relaxed toward the live estimate, keeping only a small | ||
| // relay-propagation minimum. | ||
| const minWalletTxSatPerVByteFee = 5 | ||
|
|
||
| // applyWalletTxFeeFloor raises a raw oracle fee estimate to a safe value for a | ||
| // non-RBF wallet transaction. It: | ||
| // - adds a 25% buffer over the oracle estimate so there is margin during the | ||
| // estimate-to-broadcast delay and the fee stays adaptive under congestion, | ||
| // - enforces a floor of minWalletTxSatPerVByteFee sat/vByte, and | ||
| // - bounds the result by maxTotalFee (the Bridge maximum total fee for the | ||
| // transaction). | ||
| // | ||
| // It returns ErrMaxFeeTooLow if the minimum floor alone would exceed | ||
| // maxTotalFee - a safe transaction cannot be built, so the caller must not | ||
| // broadcast an underpriced one. estimatedFee is the raw oracle fee in satoshis | ||
| // and txVsize is the estimated transaction virtual size in vBytes. | ||
| // | ||
| // The 25% buffer is applied to the truncated per-vByte rate | ||
| // (estimatedFee / txVsize). This is lossless only because EstimateFee returns | ||
| // the fee as satPerVByteFee * txVsize (an exact multiple of the vsize), so the | ||
| // integer division recovers the exact rate. If that contract ever changes so | ||
| // estimatedFee is no longer an exact multiple of txVsize, apply the buffer to | ||
| // estimatedFee directly instead of to the truncated rate; otherwise up to | ||
| // txVsize-1 sat is silently dropped before buffering and the tx is underpriced. | ||
| // | ||
| // maxTotalFee bounds only the total transaction fee. Where the Bridge also | ||
| // enforces a per-request cap (e.g. the redemption TxMaxFee), satisfying that | ||
| // cap is the caller's or on-chain validation's responsibility; this helper is | ||
| // unaware of it. Callers are expected to reject a raw estimate already above | ||
| // maxTotalFee before calling (all current callers do); the result is in any | ||
| // case clamped down to maxTotalFee. | ||
| // | ||
| // The buffer and floor are applied to the estimated vsize; a transaction whose | ||
| // real on-wire vsize is larger than estimated (e.g. a deposit sweep containing | ||
| // legacy P2SH inputs) can land slightly below the intended rate, but still far | ||
| // above the relay floor this guards against. | ||
| func applyWalletTxFeeFloor( | ||
| estimatedFee int64, | ||
| txVsize int64, | ||
| maxTotalFee uint64, | ||
| ) (int64, error) { | ||
| if txVsize <= 0 { | ||
| return 0, fmt.Errorf("invalid transaction virtual size [%d]", txVsize) | ||
| } | ||
|
|
||
| // If even the minimum floor exceeds the Bridge maximum, a safe transaction | ||
| // cannot be constructed; error rather than silently broadcast underpriced. | ||
| if uint64(minWalletTxSatPerVByteFee*txVsize) > maxTotalFee { | ||
| return 0, fmt.Errorf( | ||
| "%w: minimum fee [%d], maximum fee [%d]", | ||
| ErrMaxFeeTooLow, | ||
| minWalletTxSatPerVByteFee*txVsize, | ||
| maxTotalFee, | ||
| ) | ||
| } | ||
|
|
||
| rate := estimatedFee / txVsize | ||
| rate = (rate*5 + 3) / 4 // ceil(rate * 1.25) | ||
| if rate < minWalletTxSatPerVByteFee { | ||
| rate = minWalletTxSatPerVByteFee | ||
| } | ||
|
|
||
| // Clamp down to the Bridge maximum total fee. This can never drop the fee | ||
| // below the floor: the floor-vs-cap guard above already guaranteed | ||
| // maxTotalFee is at least the minimum floor total. | ||
| totalFee := rate * txVsize | ||
| if uint64(totalFee) > maxTotalFee { | ||
| totalFee = int64(maxTotalFee) | ||
| } | ||
|
|
||
| return totalFee, nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| package tbtcpg | ||
|
|
||
| import ( | ||
| "strings" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestApplyWalletTxFeeFloor(t *testing.T) { | ||
| const vsize = 200 | ||
|
|
||
| tests := map[string]struct { | ||
| estimatedFee int64 | ||
| txVsize int64 | ||
| maxTotalFee uint64 | ||
| expectedFee int64 | ||
| expectErrorContains string | ||
| }{ | ||
| "estimate above the floor is buffered by 25%": { | ||
| estimatedFee: 4000, // rate 20 sat/vByte | ||
| txVsize: vsize, | ||
| maxTotalFee: 100000, | ||
| expectedFee: 5000, // ceil(20*1.25)=25 sat/vByte * 200 | ||
| }, | ||
| "low estimate is raised to the minimum floor": { | ||
| estimatedFee: vsize, // rate 1 sat/vByte | ||
| txVsize: vsize, | ||
| maxTotalFee: 100000, | ||
| expectedFee: 1000, // max(5, ceil(1*1.25)=2)=5 sat/vByte * 200 | ||
| }, | ||
| "buffered fee above the cap is bounded to the cap": { | ||
| estimatedFee: 4000, // rate 20 -> buffered 25 sat/vByte * 200 = 5000 | ||
| txVsize: vsize, | ||
| maxTotalFee: 4500, // below the buffered 5000 | ||
| expectedFee: 4500, | ||
| }, | ||
| "buffered fee exactly at the cap is not clamped": { | ||
| estimatedFee: 4000, // rate 20 -> buffered 25 sat/vByte * 200 = 5000 | ||
| txVsize: vsize, | ||
| maxTotalFee: 5000, // exactly the buffered total | ||
| expectedFee: 5000, | ||
| }, | ||
| "minimum floor exactly at the cap is allowed": { | ||
| estimatedFee: 100, // rate 0 -> floored to 5 sat/vByte | ||
| txVsize: vsize, | ||
| maxTotalFee: 1000, // exactly the 5 sat/vByte floor total (5*200) | ||
| expectedFee: 1000, | ||
| }, | ||
| "minimum floor above the cap returns an error": { | ||
| estimatedFee: 100, | ||
| txVsize: vsize, | ||
| maxTotalFee: 800, // below the 5 sat/vByte floor (1000) | ||
| expectErrorContains: "minimum safe transaction fee", | ||
| }, | ||
| "non-positive virtual size returns an error": { | ||
| estimatedFee: 1000, | ||
| txVsize: 0, | ||
| maxTotalFee: 100000, | ||
| expectErrorContains: "invalid transaction virtual size", | ||
| }, | ||
| } | ||
|
|
||
| for name, tc := range tests { | ||
| t.Run(name, func(t *testing.T) { | ||
| fee, err := applyWalletTxFeeFloor( | ||
| tc.estimatedFee, | ||
| tc.txVsize, | ||
| tc.maxTotalFee, | ||
| ) | ||
|
|
||
| if tc.expectErrorContains != "" { | ||
| if err == nil { | ||
| t.Fatalf("expected an error, got fee [%d]", fee) | ||
| } | ||
| if !strings.Contains(err.Error(), tc.expectErrorContains) { | ||
| t.Fatalf( | ||
| "expected error containing [%s]; got [%v]", | ||
| tc.expectErrorContains, err, | ||
| ) | ||
| } | ||
| return | ||
| } | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: [%v]", err) | ||
| } | ||
| if fee != tc.expectedFee { | ||
| t.Errorf( | ||
| "unexpected fee\nexpected: [%d]\nactual: [%d]", | ||
| tc.expectedFee, fee, | ||
| ) | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟠 Major | 🏗️ Heavy lift
Do not knowingly underprice legacy-deposit sweeps.
The estimator charges the floor against an all-P2WSH vsize, while this comment confirms P2SH inputs can make the serialized transaction larger. Those sweeps can therefore be broadcast below 5 sat/vByte—the condition this change is intended to prevent. Calculate vsize from the actual deposit input types (or use a conservative size) before applying the floor.
🤖 Prompt for AI Agents