← All writing

Looking into Cluster Mempool

Bitcoin Core 31 shipped a mempool update, so I looked into it.

How the mempool used to work

The mempool is where transactions wait before they get mined. Every node has its own mempool, and what’s in it depends on which transactions it has received, so they can all look a bit different. An unconfirmed transaction in the mempool either gets mined by a miner, or gets kicked out when the mempool is full.

Before, eviction was based on descendant feerate, and selection was based on ancestor feerate. When the mempool overflows, the transaction with the lowest descendant feerate gets evicted along with all of its descendants. For a block, transaction bundles are picked one by one, highest ancestor feerate first.

That’s a quick summary of how the old mempool worked. I kind of assumed this was already optimal, but then 31.x introduced something called cluster mempool, which was supposed to fix some inefficient parts of the old design. So I looked into it.

What was the problem?

At first glance it sounds fine. Everything is sorted by feerate, so what could go wrong? And in the usual case, where someone just creates one transaction, sorting by feerate and then selecting or evicting works fine. At least that’s how I understood it.

But looking at the problems people pointed out, there’s no guarantee that the transaction being evicted is the one that would be selected last. In other words, there are cases where the mempool evicts a transaction that could actually get mined.

When the mining order isn’t the exact opposite of the eviction order

Transaction graph: A (3 sat/vB) has children B, C and E, and D is a child of both B and C

(Example from: An overview of the cluster mempool proposal, Delving Bitcoin)

In the diagram above, A has the lowest descendant feerate, so A gets evicted when the mempool is full. But if you look at ancestor feerates, E is the highest, so the first thing to get mined is E together with its ancestor A. So from a mining-incentive point of view, A shouldn’t be evicted, but from a mempool-capacity point of view, it’s first in line. In other words, transactions aren’t lined up in one single order from mining to eviction.

When RBF accepts a replacement that’s worse for miners

A (1 sat/vB, 100 vB) → B (99 sat/vB, 100 vB), and A’ (3 sat/vB, 10000 vB) replacing A

Here A -> B is unconfirmed, and A’ wants to replace A. RBF compares the feerates of the transactions being swapped (A and A’) and the total absolute fee. 3 sat/vB > 1 sat/vB, and 30,000 sat > 10,000 sat, so it passes both checks and the replacement is accepted.

But if you looked at A-B as a whole, the feerate was 50 sat/vB. After the replacement it drops to about 3 sat/vB. The mempool ends up with a lower-feerate graph, so it doesn’t give miners the best incentive.

When RBF rejects a replacement that’s better for miners

A (1 sat/vB, 10000 vB) → B (100 sat/vB, 100 vB), and B’ (49 sat/vB, 10000 vB) replacing B

This time, B’ is 49 sat/vB, lower than B, so it’s rejected. But A-B is about 2 sat/vB, while A-B’ would be 25 sat/vB. The miner would earn more, and it still gets rejected.

What did cluster mempool change?

So I think the core idea was that this can only be fixed if eviction and mining are exact opposites. You need a total order that lines up every transaction, from the most attractive to mine to the most suitable to evict.

Then you might think, why not just keep the whole transaction graph listed in that order? But every time a new transaction comes in, that costs O(d·n·log n), where d is the descendant limit and n is the number of transactions. That’s way too much. Roughly, if d is at most 25 and n is around 100,000, that’s about 40 million operations. It could take 0.1 to 1 second for every transaction you receive. Not realistic.

To actually keep a total order of all transactions, you have to limit the size of each connected component in the transaction graph. That’s how you cap the work. So where the old design had ancestor and descendant limits, cluster mempool calls one of these parent-child graphs a cluster and limits its size instead (64 transactions, 101 kvB).

With that, sorting the whole mempool using clusters comes down to:

  1. Sorting inside each cluster
  2. Merging the sorted clusters

For 1, it’s nice that you don’t have to stick with the ancestor feerate approach. Version 31 apparently uses SFL (Spanning-Forest Linearization). (I’ll cover it separately if I get the chance.)

For 2, you merge many clusters into one. Since each cluster is already sorted, you compare them by prefix and take the best bundle first. You cut off the highest-feerate bundle from the front, and that’s called a chunk.

And since everything is already sorted, when a new transaction comes in you only need to re-insert the chunks of the cluster that changed. That’s a huge win.

Now miners just pick chunks in descending chunk feerate. And the mempool can evict in exactly the opposite way, starting from the lowest-feerate chunk at the end.

Feerate diagram of old and new: new is ahead at 200 vB with 700 sat, old is ahead at 300 vB with 950 sat, and at 400 vB new has 1150 sat and old 1050 sat, so the two lines cross

(Graph from: An overview of the cluster mempool proposal, Delving Bitcoin)

(This graph shows the feerate diagram before (old) and after (new) an RBF replacement. The lines cross, so they’re incomparable, and the replacement is rejected.)

RBF can also follow the best incentive in the same way. To explain a bit more: with RBF, whether a replacement is a gain or a loss can depend on transaction size. If you line up the chunks in feerate order and plot cumulative fee against cumulative size, you get a feerate diagram. If the two graphs cross, so there’s a point where gain and loss flip, they’re called incomparable. On the other hand, if the new one is equal or higher everywhere and higher in at least one place, it’s called “strictly better,” and replacements are only accepted in that case. That’s how it got fixed.

CPFP carve-out is no longer possible

When you close a Lightning channel, you broadcast the commitment transaction. It was signed a while ago, so its fee is based on the fee situation back then. If fees have changed, it might not get mined. That’s why it has a small output called an anchor, so you can use CPFP later to get it mined.

The old mempool had a descendant limit of 25 and 101 kvB, so the other party could abuse it by filling up all the descendant slots. Then my CPFP gets rejected for going over the limit. This is called pinning. Basically, it’s an attack that stops me from force-closing.

The carve-out was an exception rule to prevent this. Even if the limit was full, it accepted one more child. But if someone pins at the cluster level, all 64 slots fill up, and 64 is the hard cap that keeps the sorting cost bounded. You can’t add an exception to get around it, so the CPFP carve-out rule was removed.

Apparently this is now handled by TRUC and sibling eviction. (I’ll try to cover that in another post.)

Conclusion

This idea immediately reminded me of LSM-Trees. SSTables are already sorted, and compaction does a k-way merge over them. Since every file is already sorted, you only compare the fronts, which felt really similar too.

Also, I think the most attractive and important philosophy in Bitcoin is that everyone chasing their own best interest actually makes the system more robust. This update fits that too. Miners can collect better fees from now on, so I think it’s a good update.

References