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

Free lesson

Commit-reveal, encrypted mempools and fair ordering

After this lesson: I can verify commit-reveal openings and put the valid orders in commit order.

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

Fix the order before anybody can read it

Commit-reveal, encrypted mempools and fair ordering are protocol designs that stop the order of a block being chosen by whoever reads the transactions. They protect every user, not only careful ones.

Each moves the problem somewhere new. Today you verify commitments by hand and order transactions by the median time the network saw them.

By the end of this lesson

Note

I can verify commit-reveal openings and put the valid orders in commit order.

Key words

WordWhat it means
Commit-revealpublish a hash first, show what it hides later
Salta secret random text hashed with the order
Encrypted mempoola queue whose transactions are unreadable until ordered
Threshold decryptionunlocking that needs most members of a group to agree
Fair orderingan order the nodes agree on, not one a builder picks
Medianthe middle value once a list is sorted

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

Commit-reveal hides an order until later

In phase one, each user publishes only a commitment: the hash of a secret salt joined to their order. The block fixes the order of the commitments.

In phase two, each user reveals the order and the salt. Anybody hashes them again and checks the result matches. A bot reading phase one sees only hashes, so it has nothing to front-run.

Commit to an order and reveal it

Command
docker exec jupyter python3 steps/commitreveal.py

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

Worked example: what the lab printed

CheckResult
Commitment the bot can readb490856763101416, the start of a hash
The true order and saltTrue
A changed order with the same saltFalse
The true order guessed without the saltFalse

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

Try it: seal and open an order

Code: python
import hashlib


def seal(order, salt):
    return hashlib.sha256((salt + "#" + order).encode()).hexdigest()


sealed = seal("swap 200 red", "m4x")
print("published:", sealed[:16])
print("honest reveal:", seal("swap 200 red", "m4x") == sealed)
print("changed reveal:", seal("swap 900 red", "m4x") == sealed)
print("guess with no salt:", seal("swap 200 red", "") == sealed)
# Change the salt to "m4y" in the honest reveal only. What does it print, and why?

An encrypted mempool does the same for everyone

Commit-reveal needs two rounds and every user to come back. An encrypted mempool does it in one. Each transaction is encrypted to a key held jointly by a group of nodes.

Once the block's order is fixed, most of the group work together to decrypt it. That is threshold decryption. The lab does not run real encryption: commit-reveal stands in for the same idea, hidden until ordered.

Fair ordering uses the median time

Another idea is to let the network decide the order. Each node reports when it first saw each transaction, and the order follows the median of those times.

In the lab, the bot's transaction reached one node early, at 9. Ordered by the first node to see it, the bot goes first. Ordered by the median time, Ana goes first, because most nodes saw hers earlier.

Order three transactions by the median

Command
docker exec jupyter python3 steps/fairorder.py

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

Worked example: three nodes, two orders

SenderTimes seenEarliestMedian
ana10, 11, 121011
bot13, 9, 14913
ben15, 16, 121215

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

A common mistake

Warning

Many people think hiding orders until the block is fixed ends the problem. If a user may skip the reveal, they can commit, watch the others reveal, then reveal only if it suits them. A design must decide what happens to an order never revealed.

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.