mempool: fixes

This commit is contained in:
nym21
2026-04-30 12:38:34 +02:00
parent 43f3be4924
commit 9b42b40a36
14 changed files with 408 additions and 79 deletions

View File

@@ -4,13 +4,28 @@ from _lib import assert_same_structure, assert_same_values, show
def test_block_v1_extras_all_values(brk, mempool, block):
"""Every shared extras field must match exposes computation differences."""
"""Every shared extras field must match - exposes computation differences.
Excluded fields:
- medianFee, feeRange, feePercentiles: mempool computes each entry with
a different algorithm (1st/99th percentile + first/last 2% of block
order for the feeRange bounds, unweighted positional p10/p25/p50/p75/p90
for the inner feeRange entries and for feePercentiles, and a vsize-
weighted middle-0.25%-of-block-weight slice for medianFee). brk
computes them all from a single vsize-weighted percentile distribution,
so they diverge anywhere tx sizes vary widely.
- avgFeeRate: mempool returns Bitcoin Core's getblockstats.avgfeerate
(integer sat/vB), brk returns the float version. Same formula, brk
keeps decimal precision.
"""
path = f"/api/v1/block/{block.hash}"
b = brk.get_json(path)["extras"]
m = mempool.get_json(path)["extras"]
show("GET", f"{path} [extras]", b, m, max_lines=50)
assert_same_structure(b, m)
assert_same_values(b, m)
assert_same_values(
b, m, exclude={"medianFee", "feeRange", "feePercentiles", "avgFeeRate"}
)
def test_block_v1_extras_pool(brk, mempool, block):

View File

@@ -4,11 +4,28 @@ from _lib import assert_same_values, show
def test_blocks_v1_from_height(brk, mempool, block):
"""v1 blocks from a confirmed height all values must match."""
"""v1 blocks from a confirmed height - all values must match.
Excluded fields:
- medianFee, feeRange, feePercentiles: mempool computes each entry with
a different algorithm (1st/99th percentile + first/last 2% of block
order for the feeRange bounds, unweighted positional p10/p25/p50/p75/p90
for the inner feeRange entries and for feePercentiles, and a vsize-
weighted middle-0.25%-of-block-weight slice for medianFee). brk
computes them all from a single vsize-weighted percentile distribution,
so they diverge anywhere tx sizes vary widely.
- avgFeeRate: mempool returns Bitcoin Core's getblockstats.avgfeerate
(integer sat/vB), brk returns the float version. Same formula, brk
keeps decimal precision.
"""
path = f"/api/v1/blocks/{block.height}"
b = brk.get_json(path)
m = mempool.get_json(path)
show("GET", path, f"({len(b)} blocks)", f"({len(m)} blocks)")
assert len(b) == len(m)
if b and m:
assert_same_values(b[0], m[0])
assert_same_values(
b[0],
m[0],
exclude={"medianFee", "feeRange", "feePercentiles", "avgFeeRate"},
)

View File

@@ -35,12 +35,19 @@ def test_mempool_txids_unique(brk):
def test_mempool_txids_count_matches_summary(brk):
"""`/api/mempool/txids` length must match `/api/mempool`'s `count` field."""
"""`/api/mempool/txids` length must roughly track `/api/mempool`'s `count`.
The two endpoints are independent reads against a live mempool, so
arrivals / evictions between fetches cause drift. We only assert the
counts are in the same ballpark - exact equality would be flaky.
"""
txids = brk.get_json("/api/mempool/txids")
summary = brk.get_json("/api/mempool")
show("GET", "/api/mempool/txids", f"len={len(txids)}", f"count={summary.get('count')}")
# Allow a small drift (1-2) since the mempool is updated asynchronously
# between the two fetches.
assert abs(len(txids) - summary["count"]) <= 5, (
f"txids={len(txids)} vs /api/mempool.count={summary['count']}"
assert isinstance(summary["count"], int) and summary["count"] > 0
assert len(txids) > 0
# 1% tolerance covers normal mempool churn between the two fetches.
drift = abs(len(txids) - summary["count"])
assert drift <= max(50, summary["count"] // 100), (
f"txids={len(txids)} vs /api/mempool.count={summary['count']} (drift={drift})"
)