Who decides the order of a block?
On a public blockchain, transactions wait in a queue that anybody can read before they go into a block. Whoever builds the block decides their order, and the order can change what each user gets. This lab shows that harm on a toy chain, so you can understand the defences.
By the end of this lesson
Note
I can explain how the order of transactions in a block can harm a user.
Learn to defend, never to attack
Warning
Everything here is simulated, on invented tokens worth nothing, with no way out to the internet. The attack is measured only to show the harm and test the defences. Never use what you learn against real people.
Key words
- Mempool: the public queue of transactions waiting for a block.
- Block builder: who chooses which transactions go in a block, and their order.
- Swap pool: a program that swaps one token for another at a rate set by its stock.
- Sandwich: a swap just before a user's and one just after, to profit from the user.
- Slippage limit: the least a user will accept back, or the swap fails.
- Commitment: a sealed value that can be opened later and cannot be changed.
Starter: what you already know
Note
Answer in your head. 1. What is a block in a blockchain? 2. If two people ask for the last ticket, who should get it? 3. What does a hash do to a message? Answers: a group of transactions added to the chain together; usually whoever asked first; turns it into a short fingerprint.
Before you start
Note
Press Start the lab on the bar at the top of this lesson and wait until it says it is running. There is one container, jupyter, and the terminal reaches its Python with docker exec jupyter, which runs one command inside it. The JupyterLab link opens its web page, port 8888. Each Run button below types its command into your lab's terminal, one after another in the same session. To watch them, or to type your own, press Open terminal on the bar.
The mempool is a public queue
The script lists the five transactions waiting for a block: who sent each, how much it swaps, the least it accepts back, its tip to the builder and when it arrived. Anybody connected can read all of them. Ana set no limit, so she accepts any amount back.
docker exec jupyter python3 steps/peek.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
A swap pool gives a worse rate for a bigger swap
The pool holds 10000 red and 10000 blue and keeps red times blue the same. A swap of 500 red would get 476 blue, but 5000 red only 3333. After one swap of 500, the same swap again gets 432, because the pool now holds less blue.
docker exec jupyter python3 steps/swap.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
A sandwich takes value from the user
Ana swaps 500 red. A bot that read the mempool swaps 1000 red just before her and swaps back just after. Alone she gets 476 blue; sandwiched she gets 395, so she loses 81. The bot ends with 88 more red than it started with.
docker exec jupyter python3 steps/sandwich.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Tip
Why could the bot place its swap just before Ana's? Answer: it read her transaction in the public mempool, and the builder put the bot's first.
Builders bid for the right to build the block
Four builders send sealed bids to the proposer through a relay. The relay refuses east, whose payment is more than its block is worth, and south, whose block holds 6 transactions when 4 is the most. North pays 90 and wins; it keeps 30.
docker exec jupyter python3 steps/auction.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
A slippage limit shrinks the attack
With no limit the largest front the bot can use is 3000 red, gaining 205. With a limit of 1%, Ana accepts no less than 471, and the largest front falls to 56, gaining only 4. A front of 1000 against that limit makes her swap fail with None instead.
docker exec jupyter python3 steps/slippage.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
A commitment hides an order until the order of the block is fixed
The script publishes only the hash of Ana's order and a secret salt, so a bot has nothing to act on. When the order is revealed it checks out as True, a changed order as False, and a guess without the salt as False.
docker exec jupyter python3 steps/commitreveal.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
A common mistake
Warning
Many people think a tighter slippage limit is always better. Too tight, and an ordinary busy block makes the swap fail, so the user tries again and waits. Choose the loosest limit you can afford to lose, not zero.
The same work in a notebook
Open JupyterLab from the bar and double-click start.ipynb. It asks for a quote, measures a sandwich and finds the largest attack under a limit. Click a cell and press Shift and Enter to run it. The last cell is where your own question goes, and mev.py is short enough to read in full.
Exit check
Tip
Answer without looking back. 1. Why can anybody see a transaction before it is in a block? 2. What does a sandwich do to the user? 3. Who chooses the winning bid for a block? 4. How does a slippage limit help? Answers: it waits in the public mempool; it gives them less back; the proposer; it caps how much an attack can take.
For teachers
Tip
This lesson is a small example of a lesson worked in a lab: the steps and the Run buttons are the lesson, and the lab is each student's own copy. The lab is one service, JupyterLab with a toy chain written in the Python standard library, and nothing here marks itself: the student hands in a notebook of their own, downloaded from JupyterLab, and a sentence on what it showed. Key learning points: 1. The mempool is public, so pending transactions can be read. 2. The order inside a block changes what each user gets. 3. A slippage limit and hiding the order are defences. Common mistake: thinking a tighter limit is always better. The tokens are invented and worth nothing, and the lesson is defensive. Equipment: the lab and a browser. Tip: act out a queue for the last ticket in the room before the first Run. Copy it into your own module and change it, or build one from scratch with the Lab menu in the lesson builder.