Four transfers and one short hash
A rollup takes a list of transfers, runs them in order and ends with new balances. It then posts the list and one short hash of the balances on the main chain.
Today you watch a toy sequencer do exactly that, then rebuild its balances yourself from what it posted.
By the end of this lesson
Note
I can apply a batch of transfers to a state and compute its state root.
Key words
| Word | What it means | Example |
|---|---|---|
| State | every account's balance at one moment | |
| Transfer | a sender paying an amount to a receiver | ("ana", "ben", 30) |
| Sequencer | the one who orders the transfers and posts the batch | |
| State root | one hash that stands for the whole state | |
| Replay | running the posted transfers again to check the result |
The cells students fill in are left blank in this preview.
A sequencer applies transfers in order
The sequencer starts from the last state and applies each transfer in turn. A transfer moves the amount from the sender to the receiver.
A transfer the sender cannot pay is skipped, and the state stays as it was. That is a rule of the rollup, so everybody who replays the batch skips it too. Order matters: the same transfers in another order can end differently.
Run a batch of four transfers
docker exec jupyter python3 steps/batch.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Worked example: what the lab printed
| Step | Transfer | ana, ben, chen, dev | Root starts |
|---|---|---|---|
| Start | none | 100, 50, 20, 0 | 43bde9ff8fa6 |
| 1 | ana pays ben 30 | 70, 80, 20, 0 | 30d76225196a |
| 2 | ben pays chen 70 | 70, 10, 90, 0 | 6642265fce11 |
| 3 | chen pays dev 500, skipped | 70, 10, 90, 0 | 6642265fce11 |
| 4 | chen pays dev 15 | 70, 10, 75, 15 | 772f7dedbc41 |
The cells students fill in are left blank in this preview.
The root fixes the state
The lab writes the balances sorted by name as one line, such as ana:70,ben:10, and hashes it. Change any balance and the root changes completely.
The rollup's contract on the main chain keeps each batch's roots. It refuses a batch that does not start from the last root it holds, so nobody can skip or rewrite a batch. The lab's second post was refused and printed None.
Try it: a state root by hand
import hashlib
state = {"ben": 5, "ana": 10}
text = ",".join(name + ":" + str(state[name]) for name in sorted(state))
print(text)
print(hashlib.sha256(text.encode()).hexdigest()[:12])
state["ben"] = 6
text = ",".join(name + ":" + str(state[name]) for name in sorted(state))
print(text)
print(hashlib.sha256(text.encode()).hexdigest()[:12])
# Swap the order of ana and ben in the first line. Does the root change?
A common mistake
Warning
Many people think the state root stores the balances. It is a fingerprint, so nobody can read a balance from it. To know the balances you need the posted data, which you replay and then check against the root.
Rebuild the state from the posted data
docker exec jupyter python3 steps/replay.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.