Reading — step 1 of 5
Read
Mempool & Fee Selection
The mempool is each node's local queue of transactions that are valid but not yet in a block. When a miner builds a candidate block, they pick the highest-paying transactions from their mempool.
Fee market
Block space is scarce (1 MB / 4 Mweight in Bitcoin). Demand spikes drive up fees. Miners are profit-maximisers — they pick transactions by fee-rate (satoshis per byte), not by absolute fee.
A 200-byte tx paying 2000 sats has a fee-rate of 10 sat/byte. A 1000-byte tx paying 5000 sats has a fee-rate of 5 sat/byte.
The miner prefers the small high-rate one even though the big one pays more.
Selection algorithm (greedy)
ranked = sorted(mempool, key=lambda t: t.fee/t.size, reverse=True)
picked = []
remaining = block_capacity_bytes
for t in ranked:
if t.size <= remaining:
picked.append(t)
remaining -= t.size
This greedy strategy is not optimal (it's the knapsack problem) but it's fast and works fine for typical fee distributions.
Real implementations also handle:
- CPFP (Child Pays For Parent): a high-fee child justifies including its low-fee parent.
- RBF (Replace By Fee, BIP-125): a tx flagged as RBF can be replaced by a conflicting tx paying higher fee.
- Package relay: propagate parent+child as a unit.
- Min-relay-fee filtering: drop dust below ~1 sat/vbyte.
Eviction
Mempool is bounded (Bitcoin Core default ≈ 300 MB). When full, evict the lowest fee-rate transactions to make room for higher-paying ones.
This exercise builds a mempool with greedy fee-rate selection and lowest-rate eviction.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…