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

Free lesson

A toy rollup: batches and state roots

After this lesson: I can apply a batch of transfers to a state and compute its state root.

65 min lesson2 commandsRollup lab: batches, fraud proofs, data availability and exits in Python

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

WordWhat it meansExample
Stateevery account's balance at one moment
Transfera sender paying an amount to a receiver("ana", "ben", 30)
Sequencerthe one who orders the transfers and posts the batch
State rootone hash that stands for the whole state
Replayrunning 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

Command
docker exec jupyter python3 steps/batch.py

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

Worked example: what the lab printed

StepTransferana, ben, chen, devRoot starts
Startnone100, 50, 20, 043bde9ff8fa6
1ana pays ben 3070, 80, 20, 030d76225196a
2ben pays chen 7070, 10, 90, 06642265fce11
3chen pays dev 500, skipped70, 10, 90, 06642265fce11
4chen pays dev 1570, 10, 75, 15772f7dedbc41

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

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

Command
docker exec jupyter python3 steps/replay.py

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

About this lab and its source: Rollup lab: batches, fraud proofs, data availability and exits in Python

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