Skip to content

Commit

Permalink
Improve Fee estimation based on mempool histogram (WalletWasabi#13279)
Browse files Browse the repository at this point in the history
* Use cumulative count to skip fee groups

* Remove SELECT1+N.

* Fix unit test.

The test was never removing any element from the histogram because the fee rate groups were wrongly built and because the mempool.Size was always zero.

---------

Co-authored-by: Turbolay <[email protected]>
Co-authored-by: Lucas Ontivero <[email protected]>
  • Loading branch information
3 people authored Jul 29, 2024
1 parent 69b03f3 commit 00e8f0f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
26 changes: 15 additions & 11 deletions WalletWasabi.Tests/UnitTests/AllFeeEstimateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,19 +235,22 @@ public async Task FixObviouslyWrongEstimationsAsync()
{
var mockRpc = CreateAndConfigureRpcClient(hasPeersInfo: true);

var histogram = MempoolInfoGenerator.FeeRanges.Reverse().Select((x, i) => new FeeRateGroup
{
Count = (uint) (100 * Math.Pow(i + 1, 2)),
Sizes = (uint) (40 * 100 * (i + 1)),
From = new FeeRate((decimal) x.from),
To = new FeeRate((decimal) x.to),
Fees = Money.Zero,
Group = x.from
}).ToArray();

mockRpc.OnGetMempoolInfoAsync = () =>
Task.FromResult(new MemPoolInfo
{
MemPoolMinFee = 0.00001000, // 1 s/b (default value)
Histogram = MempoolInfoGenerator.FeeRanges.Select((x, i) => new FeeRateGroup
{
Count = (uint)(200 * (i + 1)),
Sizes = (uint)(40 * 100 * (i + 1)),
From = new FeeRate((decimal)x.from),
To = new FeeRate((decimal)x.to),
Fees = Money.Zero,
Group = x.from
}).ToArray()
Histogram = histogram,
Size = (int)histogram.Sum(x => x.Count)
});

mockRpc.OnEstimateSmartFeeAsync = (target, _) =>
Expand All @@ -263,11 +266,12 @@ public async Task FixObviouslyWrongEstimationsAsync()
};

var allFee = await mockRpc.EstimateAllFeeAsync();
Assert.Equal(3_000, allFee.Estimations[2]);
Assert.True(allFee.Estimations[3] > 500);
Assert.Equal(140, allFee.Estimations[2]);
Assert.Equal(124, allFee.Estimations[144]);
Assert.True(allFee.Estimations[1008] > 1);
}


[Fact]
public async Task WorksWithBitcoinCoreEstimationsAsync()
{
Expand Down
10 changes: 7 additions & 3 deletions WalletWasabi/Extensions/RPCClientExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,13 @@ private static FeeRateByConfirmationTarget GetFeeEstimationsFromMempoolInfo(MemP
// Filter those groups with very high fee transactions (less than 0.1%).
// This is because in case a few transactions pay unreasonably high fees
// then we don't want our estimations to be affected by those rare cases.
var relevantFeeGroups = mempoolInfo.Histogram
.OrderByDescending(x => x.Group)
.SkipWhile(x => x.Count < mempoolInfo.Size / 1_000);
var histogram = mempoolInfo.Histogram.OrderByDescending(x => x.Group).ToArray();
var relevantFeeGroups = histogram
.Scan(0u, (acc, g) => acc + g.Count)
.Zip(histogram, (x, y) => (AccumulativeCount: x, Group: y))
.SkipWhile(x => x.AccumulativeCount < mempoolInfo.Size / 1_000)
.Select(x => x.Group)
.ToList();

// Splits multi-megabyte fee rate groups in 1mb chunk
// We need to count blocks (or 1MvB transaction chunks) so, in case fee
Expand Down

0 comments on commit 00e8f0f

Please sign in to comment.