Fix RoundToSignificant for negative amounts (#7782)
What changed, and why it matters
This commit fixes a math bug in how BTCPay Server rounds very small negative amounts. Previously, a helper called RoundToSignificant divided by the signed value instead of its absolute value, so negative numbers behaved differently from positive ones. Tiny negative amounts could be rounded to zero (for example, -0.0004 became -0.00) while the equivalent positive amount kept its precision. The same patch also makes sure transaction fee bumps are never rounded down below the Bitcoin network's minimum relay-fee increase, preventing a fee-bump transaction from being rejected for paying too little.
Review other callers of RoundToSignificant to confirm no other negative-amount logic depends on the old behavior, and verify that the new fee-bump floor is applied consistently across wallet implementations. Consider adding explicit integration tests for RBF replacement transactions near the minimum incremental relay fee boundary.
Security signals we found
Rounding/truncation bug in financial amount handling
Negative-amount handling asymmetry that could alter displayed or computed balances
Fee-bump calculation could fall below Bitcoin incremental relay fee requirement
Replacement transaction could be rejected by the network due to underpayment
Evidence from the diff
The core fix changes RoundToSignificant in BTCPayServer.Rating/Extensions.cs from Math.Abs(rounded - value) / value to Math.Abs(rounded - value) / Math.Abs(value). For negative value, the old expression produced a negative relative error, so the loop’s acceptance test was always true and the first rounding was accepted. That caused small-magnitude negative values to collapse toward zero. The patch also adds a guard in BTCPayWallet.CalculateBumpResult so that newTotalFee is at least oldTotalFee + GetFeeRoundUp(IncrementalRelayFee, Entry.VirtualSizeBytes), compensating for truncation in FeeRate to sat/kvB that could otherwise make a replacement transaction one satoshi short of the required incremental relay fee. Tests are added for both behaviors.
Changed components
BTCPayServer.Rating/Extensions.cs (RoundToSignificant helper)BTCPayServer/Services/Wallets/BTCPayWallet.cs (CalculateBumpResult fee-bump logic)BTCPayServer.Tests/FastTests.cs (unit tests)Inspect captured patch +30 / −2
### BTCPayServer.Rating/Extensions.cs
@@ -38,7 +38,7 @@ public static decimal RoundToSignificant(this decimal value, ref int divisibilit
while (true)
{
var rounded = decimal.Round(value, divisibility, MidpointRounding.AwayFromZero);
- if ((Math.Abs(rounded - value) / value) < 0.01m)
+ if ((Math.Abs(rounded - value) / Math.Abs(value)) < 0.01m)
{
value = rounded;
break;
### BTCPayServer.Tests/FastTests.cs
@@ -646,6 +646,27 @@ public void CalculateMinFeeBump()
Assert.Equal(new FeeRate(Money.Satoshis(3350), 350), minFeeRate);
Assert.Equal(Money.Satoshis(1350), bump.NewTxFee);
+ // Truncating 17200 / 171 to 100.584 sat/vB before adding 0.1 sat/vB
+ // gives a 17-satoshi increase, but the node requires ceil(171 * 0.1) = 18.
+ replacementInfo = new ReplacementInfo
+ (
+ new MempoolEntry()
+ {
+ BaseFee = Money.Satoshis(17200),
+ VirtualSizeBytes = 171,
+ DescendantFees = Money.Satoshis(17200),
+ DescendantVirtualSizeBytes = 171,
+ AncestorFees = Money.Satoshis(17200),
+ AncestorVirtualSizeBytes = 171,
+ },
+ IncrementalRelayFee: new FeeRate(0.1m),
+ MinMempoolFeeRate: new FeeRate(0.1m)
+ );
+ minFeeRate = replacementInfo.CalculateNewMinFeeRate();
+ bump = replacementInfo.CalculateBumpResult(minFeeRate);
+ Assert.Equal(Money.Satoshis(18), bump.BumpTxFee);
+ Assert.Equal(Money.Satoshis(17218), bump.NewTxFee);
+
// Check MinMempoolFee is respected
replacementInfo = new ReplacementInfo
(
@@ -1087,7 +1108,8 @@ public void RoundupCurrenciesCorrectly()
DisplayFormatter displayFormatter = new(GetCurrencyNameTable());
foreach (var test in new[]
{
- (0.0005m, "0.0005 USD", "USD"), (0.001m, "0.001 USD", "USD"), (0.01m, "0.01 USD", "USD"),
+ (0.0005m, "0.0005 USD", "USD"), (-0.0005m, "-0.0005 USD", "USD"),
+ (0.001m, "0.001 USD", "USD"), (0.01m, "0.01 USD", "USD"),
(0.1m, "0.10 USD", "USD"), (0.1m, "0,10 EUR", "EUR"), (1000m, "1,000 JPY", "JPY"),
(1000.0001m, "1,000.00 INR", "INR"),
(0.0m, "0.00 USD", "USD"), (1m, "1 COP", "COP"), (1m, "1 ARS", "ARS")
@@ -1097,6 +1119,9 @@ public void RoundupCurrenciesCorrectly()
actual = actual.Replace("¥", "¥"); // Hack so JPY test pass on linux as well
Assert.Equal(test.Item2, actual);
}
+ // Negative amounts keep the same significant digits as positive ones
+ Assert.Equal(-0.0005m, (-0.0005m).RoundToSignificant(2));
+ Assert.Equal(-1000.00m, (-1000.0001m).RoundToSignificant(2));
Assert.Equal(0, GetCurrencyNameTable().GetNumberFormatInfo("ARS").CurrencyDecimalDigits);
Assert.Equal(0, GetCurrencyNameTable().GetNumberFormatInfo("COP").CurrencyDecimalDigits);
}
### BTCPayServer/Services/Wallets/BTCPayWallet.cs
@@ -540,6 +540,9 @@ public BumpResult CalculateBumpResult(FeeRate newEffectiveFeeRate)
{
var newTotalFee = GetFeeRoundUp(newEffectiveFeeRate, GetPackageVirtualSize());
var oldTotalFee = GetPackageFee();
+ // FeeRate truncates to sat/kvB, so rounding the total fee alone can leave
+ // the increase one satoshi below the incremental relay requirement.
+ newTotalFee = Money.Max(newTotalFee, oldTotalFee + GetFeeRoundUp(IncrementalRelayFee, Entry.VirtualSizeBytes));
var bump = newTotalFee - oldTotalFee;
var newTxFee = Entry.BaseFee + bump;
var newTxFeeRate = new FeeRate(newTxFee, Entry.VirtualSizeBytes);Why this scored 51/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.