Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

The mempool and why transaction order matters

After this lesson: I can explain why the order of transactions in a block changes what each user gets.

60 min lesson2 commandsBlock building and MEV lab: mempool, sandwiches, auctions and defences in Python

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

WordWhat it means
Transactiona signed request to change the chain
Mempoolthe public queue of transactions waiting for a block
Block builderwho chooses which transactions go in a block, and their order
Tipwhat a sender offers the builder to be included
Front-runningplacing a transaction just before one you have seen
Back-runningplacing 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

Command
docker exec jupyter python3 steps/peek.py

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Worked example: what the lab printed

IdSenderGivesLeast backTipArrived
t1ana500 red021
t2ben300 blue28052
t3chen200 red19013
t4dev800 red70054
t5eli100 blue9035

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

Command
docker exec jupyter python3 steps/order.py

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Worked example: two orders, two results

RuleOrderana getsdev gets
First come, first servedt1, t2, t3, t4, t5476 blue715 blue
Highest tip firstt2, t4, t5, t1, t3441 blue784 blue

The cells students fill in are left blank in this preview.

Try it: who gains from going first

Code: python
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.

About this lab and its source: Block building and MEV lab: mempool, sandwiches, auctions and defences in Python

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.