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
| Word | What it means |
|---|---|
| Commit-reveal | publish a hash first, show what it hides later |
| Salt | a secret random text hashed with the order |
| Encrypted mempool | a queue whose transactions are unreadable until ordered |
| Threshold decryption | unlocking that needs most members of a group to agree |
| Fair ordering | an order the nodes agree on, not one a builder picks |
| Median | the 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
docker exec jupyter python3 steps/commitreveal.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Worked example: what the lab printed
| Check | Result |
|---|---|
| Commitment the bot can read | b490856763101416, the start of a hash |
| The true order and salt | True |
| A changed order with the same salt | False |
| The true order guessed without the salt | False |
The cells students fill in are left blank in this preview.
Try it: seal and open an order
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
docker exec jupyter python3 steps/fairorder.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Worked example: three nodes, two orders
| Sender | Times seen | Earliest | Median |
|---|---|---|---|
| ana | 10, 11, 12 | 10 | 11 |
| bot | 13, 9, 14 | 9 | 13 |
| ben | 15, 16, 12 | 12 | 15 |
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.