A queue that anybody can read
The mempool is the public waiting room where blockchain transactions sit before a block includes them. Anybody connected can read every one, and the block builder chooses their order.
That order decides who gets what. Today you read a mempool on a toy chain and see one order help one user and hurt another.
By the end of this lesson
Note
I can explain why the order of transactions in a block changes what each user gets.
Key words
| Word | What it means |
|---|---|
| Transaction | a signed request to change the chain |
| Mempool | the public queue of transactions waiting for a block |
| Block builder | who chooses which transactions go in a block, and their order |
| Tip | what a sender offers the builder to be included |
| Front-running | placing a transaction just before one you have seen |
| Back-running | placing a transaction just after one you have seen |
The cells students fill in are left blank in this preview.
The mempool is a public queue
When you send a transaction, it does not go straight into a block. It is passed from node to node and waits in each node's mempool.
Waiting in public is how the network shares work. But it also means anybody can read your transaction before it counts, including how much you swap and the least you accept back.
Read the toy mempool
docker exec jupyter python3 steps/peek.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Worked example: what the lab printed
| Id | Sender | Gives | Least back | Tip | Arrived |
|---|---|---|---|---|---|
| t1 | ana | 500 red | 0 | 2 | 1 |
| t2 | ben | 300 blue | 280 | 5 | 2 |
| t3 | chen | 200 red | 190 | 1 | 3 |
| t4 | dev | 800 red | 700 | 5 | 4 |
| t5 | eli | 100 blue | 90 | 3 | 5 |
The cells students fill in are left blank in this preview.
The same transactions in another order
A builder can put transactions in any order it likes. Two simple rules are first come, first served, and highest tip first.
On a swap pool, every swap moves the rate for the next one. So the same five swaps give different results in a different order. In the lab, ana gets 476 blue in arrival order but only 441 in tip order. Chen's swap fails both times, because his limit of 190 is not met.
Run one mempool in two orders
docker exec jupyter python3 steps/order.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Worked example: two orders, two results
| Rule | Order | ana gets | dev gets |
|---|---|---|---|
| First come, first served | t1, t2, t3, t4, t5 | 476 blue | 715 blue |
| Highest tip first | t2, t4, t5, t1, t3 | 441 blue | 784 blue |
The cells students fill in are left blank in this preview.
Try it: who gains from going first
def swap(pool, amount):
out = pool["blue"] * amount // (pool["red"] + amount)
pool["red"] = pool["red"] + amount
pool["blue"] = pool["blue"] - out
return out
pool = {"red": 1000, "blue": 1000}
kim = swap(pool, 100)
lee = swap(pool, 100)
print("kim first gets", kim, "then lee gets", lee)
pool = {"red": 1000, "blue": 1000}
lee = swap(pool, 100)
kim = swap(pool, 100)
print("lee first gets", lee, "then kim gets", kim)
# Change the 100s to 300 and run again. How much is going first worth now?
Front-running and back-running
Because the queue is public and the order is a choice, somebody can watch for a transaction and place their own around it. Placing one just before is front-running. Placing one just after is back-running.
The value somebody can take by choosing the order is called maximal extractable value, MEV. This module measures it on a toy chain so that you can design systems that protect users from it.
A common mistake
Warning
Many people think a blockchain serves transactions first come, first served. No rule forces that. The builder chooses the order, and many builders sort by tip or by what earns them most.