Skip to content

chentihe/zk-rollup-lite

Repository files navigation

zk-rollup-lite

This is a demo app of simple implementation of a layer2.
We can see how layer2 works and what's the meaning of the rollup.

zk-rollup-lite

0. Prerequisite

Make sure you have installed NVM, Go, Circom, Docker before you jump into the following steps.

1. Installation

git clone [email protected]:chentihe/zk-rollup-lite.git

cd zk-rollup-lite

2. Build the circuit files

2.0 Install packages

~/zk-rollup-lite: cd prover
~/prover: npm install

Since installing snarkjs will also install circom, but the version is very old, we need to delete it manually. Otherwise errors will be occurred when we execute the compile script.

~/prover: sudo rm -rf node_modules/circom

2.1 Generate powersOfTau

You can see powers of tau ceremony for details.

~/prover: ./powers-of-tau.sh 15
~/prover: ./powers-of-tau.sh 17

2.2 Compile circuits

The compile script will create build folder on operator directory because layer2 app need circuit files to generate zkp(zero knowledge proof), we need to copy these files into the docker image.

It would take times to generate circuit related files.

  • Compile withdraw circuit
~/prover: ./compile-circuit.sh circuits/withdraw.circom trusted_setup/pot15_final.ptau
  • Compile processTx circuit
~/prover: ./compile-circuits.sh circuits/tx.circom trusted_setup/pot15_final.ptau
  • Compile rollupTx circuit
~/prover: ./compile-circuits.sh circuits/rollupTx.circom trusted_setup/pot17_final.ptau

If you face the permission issue, execute below command to change the permission.

sudo chmod +x compile-circuits.sh

3. Start all services

docker compose up command starts containers including postgres, redis, anvil(ethereum node), layer2 app. Except for that, when layer2 app starts, it will deploy necessary contracts(tx verifier, withdraw verifier, rollup) at the same time.

~/prover: cd ..
~/zk-rollup-lite: docker compose up

We can navigate to postgres gui and redis gui to check the database.

4. APIs

There are apis to query the account and the contract.

Accounts

Rollup Contract

5. Do a rollup

There are two mock users on env.yaml, and a rollup script to execute 2 rollup transactions.

Open a new terminal and navigate to operator folder to execute the rollup script.

~/operator: ./rollup.sh

When you see the terminal of docker containers indicates below message, you can check if the data is updated on both postgres and redis.

zk-rollup-lite-app-1 | 2023/08/04 03:31:27 Rollup success: 0x3327a5267c77fc272ad754881e9f608869b12fa185f9fb13d1ae170ccef4a18a

You will see that the value of last-inserted is 5 and the value of rolluped-txs is 4 on redis commander, because this app only rollup 2 transactions at once. The 5th transaction will be rolluped once the new transaction is created.

6. Try it yourself

First, navigate to cmd folder.

~/operator: cd cmd

There are some commands you can try to interact with the demo app.

6.1 Deposit

There are only two mock users to choose, 0 for the first user, 1 for the second user.

The default balance for both users is 1000ETH.

~/cmd: go run main.go deposit --account 0 --amount 1 # 1 for 1ETH

6.2 Withdraw

Before you withdraw the ethers from layer2, you need to check the balance first, then type how many ethers you want to withdraw.

~/cmd: curl -s -X GET localhost:8000/api/v1/accounts/1

{
    "AccountIndex": 1,
    "PublicKey": "c433f7a696b7aa3a5224efb3993baf0ccd9e92eecee0c29a3f6c8208a9e81d9e",
    "L1Address": "0x70997970C51812dc3A010C7d01b50e0d17dc79C8",
    "Nonce": 0,
    "Balance": 10000000000000000000
}
~/cmd: go run main.go withdraw --account 0 --amount 0.2

6.3 Transfer ethers on layer2

In this demo app, you are only able to transfer ethers to the other user. You should notice that every transaction on layer2 may incur the transaction fee to the layer2 platform, hence, we will charge 0.05ETH on every transction.

~/cmd: go run main.go sendtx --account 0 --amount 0.5