Skip to content

Commit

Permalink
refactor(protocol): restructure solidity code to match compilation ta…
Browse files Browse the repository at this point in the history
…rgets (#18059)

Co-authored-by: dantaik <dantaik@users.noreply.github.com>
Co-authored-by: gavin <gavin@taiko.xyz>
Co-authored-by: David <david@taiko.xyz>
  • Loading branch information
4 people authored Sep 12, 2024
1 parent 078d336 commit adc47f4
Show file tree
Hide file tree
Showing 266 changed files with 1,230 additions and 1,477 deletions.
16 changes: 4 additions & 12 deletions .github/workflows/protocol.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,16 @@ jobs:

- name: Unit tests
working-directory: ./packages/protocol
run: pnpm clean && pnpm test

- name: Format solidity && update contract layout table
working-directory: ./packages/protocol
run: pnpm layout && forge fmt
run: pnpm clean && forge fmt && pnpm test:l1 && pnpm layout:l1 && pnpm test:l2 && pnpm layout:l2

- name: Commit contract layout table
uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: "forge fmt & update contract layout table"
commit_message: "forge fmt & update contract layout tables"

- name: Generate genesis
working-directory: ./packages/protocol
run: pnpm test:genesis

- name: Run snapshot (Foundry)
working-directory: ./packages/protocol
run: pnpm snapshot
run: pnpm genesis:test

- name: Deploy L1 contracts
working-directory: ./packages/protocol
Expand All @@ -60,4 +52,4 @@ jobs:
while ! nc -z localhost 8545; do
sleep 1
done
pnpm test:deploy
pnpm test:deploy:l1
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ __pycache__/

# VSCode
.vscode/launch.json
packages/protocol/config.js
.vercel

# Idea
Expand Down
2 changes: 1 addition & 1 deletion .release-please-manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"packages/fork-diff": "0.6.0",
"packages/guardian-prover-health-check": "0.1.0",
"packages/guardian-prover-health-check-ui": "0.2.0",
"packages/protocol": "1.9.0",
"packages/protocol": "1.8.0",
"packages/relayer": "0.12.0",
"packages/snaefell-ui": "1.1.0",
"packages/supplementary-contracts": "1.0.0",
Expand Down
4 changes: 1 addition & 3 deletions _typos.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ extend-exclude = [
"go.mod",
"CHANGELOG.md",
"packages/protocol/audit/code4rena-2024-03-taiko-final-report.md",
"packages/protocol/contracts/automata-attestation/**",
"packages/protocol/contracts/thirdparty/**",
"packages/protocol/contracts/compiled/**",
"packages/protocol/contracts/layer1/automata-attestation/**",
"packages/protocol/deployments/**",
# TODO: fix typos and remove these
"packages/taiko-client/**",
Expand Down
5 changes: 3 additions & 2 deletions packages/protocol/.eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ artifacts
cache
coverage
lib
contracts/test/thirdparty/
contracts/automata-attestation/
test/shared/thirdparty/
contracts/layer1/automata-attestation/
monitors/
1 change: 1 addition & 0 deletions packages/protocol/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ yarn-error.log*
package-lock.json
npm-debug.log*
typechain-types
test/genesis/generate/data

# Hardhat files
cache
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol/.solhint.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"rules": {
"avoid-low-level-calls": "off",
"avoid-tx-origin": "off",
"compiler-version": ["error", "0.8.27"],
"compiler-version": ["error", "^0.8.24"],
"func-name-mixedcase": "off",
"func-visibility": ["error", { "ignoreConstructors": true }],
"max-line-length": ["error", 120],
Expand Down
8 changes: 2 additions & 6 deletions packages/protocol/.solhintignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
node_modules/
lib/
contracts/test/TestLibRLPReader.sol
**/contracts/thirdparty/**/*.sol
/contracts/automata-attestation/
test/thirdparty/
test/GasComparison.t.sol
test/TestLn.sol
contracts/layer1/automata-attestation/
test/shared/thirdparty/
115 changes: 0 additions & 115 deletions packages/protocol/CHANGELOG.md

Large diffs are not rendered by default.

162 changes: 84 additions & 78 deletions packages/protocol/README.md
Original file line number Diff line number Diff line change
@@ -1,139 +1,145 @@
# Taiko Protocol

This package contains rollup contracts on both L1 and L2, along with other assisting code. Taiko L2's chain ID is [167](https://github.com/ethereum-lists/chains/pull/1611).
This repository contains the Taiko Based Contestable Rollup (BCR) protocol and supporting tools. The project is managed using `pnpm` and `foundry`.

## Getting Started
## Prerequisites

Before compiling smart contracts, ensure all necessary dependencies are installed:
Before compiling the smart contracts, ensure the following are installed and up to date:

```sh
pnpm install
- [Foundry](https://book.getfoundry.sh/)
- [pnpm](https://pnpm.io/)

To install dependencies:

```bash
foundryup && pnpm install
```

Then, compile the smart contracts:
## Compilation and Testing

```sh
pnpm compile
Taiko’s protocol is split between Layer 1 (L1) and Layer 2 (L2). The smart contracts need to be compiled and tested separately for each layer:

### Layer 1 (Ethereum, Duncan Hardfork)

To compile, run tests, and generate the storage layout for L1:

```bash
pnpm compile:l1
pnpm test:l1
pnpm layout:l1
```

If you run into `Error: Unknown version provided`, you should upgrade your foundry installation by running `curl -L https://foundry.paradigm.xyz | bash`.
### Layer 2 (Taiko, Shanghai Hardfork)

## Style Guide
Similarly, for L2:

```bash
pnpm compile:l2
pnpm test:l2
pnpm layout:l2
```

### Compile and Test for Both Layers

To compile and test contracts for both L1 and L2 at once:

```bash
pnpm compile
pnpm test
pnpm layout
```

Please see [CONTRIBUTING.md](../../CONTRIBUTING.md) for the source code style guidelines to adhere to.
## Layer 2 Genesis Block

## Generate L2 Genesis JSON's `alloc` Field
### Generating a Dummy Genesis Block

Create a `config.js` with the following structure:
To generate dummy data for the L2 genesis block, create a configuration file at `./test/genesis/data/genesis_config.js` with the following content:

```javascript
module.exports = {
// Owner address of the pre-deployed L2 contracts.
contractOwner: "0xDf08F82De32B8d460adbE8D72043E3a7e25A3B39",
// Chain ID of the Taiko L2 network.
chainId: 167,
// Account address and pre-mint ETH amount as key-value pairs.
seedAccounts: [
{ "0xDf08F82De32B8d460adbE8D72043E3a7e25A3B39": 1024 },
{ "0x79fcdef22feed20eddacbb2587640e45491b757f": 1024 },
],
// Owner Chain ID, Security Council, and Timelock Controller
l1ChainId: 31337,
ownerSecurityCouncil: "0xDf08F82De32B8d460adbE8D72043E3a7e25A3B39",
ownerTimelockController: "0xDf08F82De32B8d460adbE8D72043E3a7e25A3B39",
// L2 EIP-1559 baseFee calculation related fields.
param1559: {
gasExcess: 1,
},
// Option to pre-deploy an ERC-20 token.
predeployERC20: true,
};
```

Then, execute the generation script:
Then compile the L2 contracts and generate the genesis block:

```sh
pnpm compile && pnpm generate:genesis config.js
```bash
pnpm compile:l2
pnpm genesis:gen
```

The script will output two JSON files under `./deployments`:

- `l2_genesis_alloc.json`: the `alloc` field which will be used in L2 genesis JSON file
- `l2_genesis_storage_layout.json`: the storage layout of those pre-deployed contracts

## Using Foundry
This generates the following JSON files in `./test/genesis/data/`:

This project also integrates with Foundry for building and testing contracts.
- `l2_genesis_alloc.json`: Contains the `alloc` field for the L2 genesis block. Use this in a `geth` or `taiko-geth` genesis block following [this guide](https://geth.ethereum.org/docs/fundamentals/private-network#creating-genesis-block).
- `l2_genesis_storage_layout.json`: Displays the storage layout of the pre-deployed contracts.

- To compile using foundry: `forge build` or `pnpm compile`
- To run foundry tests: `forge test --gas-report -vvv` or `pnpm test:foundry`
To validate the genesis data:

## Generating and Running the L2 Genesis Block

The generation of the L2 genesis block and obtaining its hash involves a series of steps, including constructing the genesis JSON, followed by the actual generation and retrieval of the genesis block hash. A test can be executed to comprehend this process.

### Testing Genesis Block Creation

To understand how the `genesis.json` is built from deployment files and how to generate the genesis block and its hash, you can use the `test:genesis` command. This test serves as a learning tool:

```sh
pnpm test:genesis
```bash
pnpm genesis:test
```

This test, defined in `./genesis/generate_genesis.test.sh`, compiles the contracts, generates the genesis JSON, and initiates a Geth node using Docker to simulate the deployment of the genesis block. Reviewing this script and its output can help you grasp the steps required to create and initiate a genesis block for the Taiko Protocol.

### Generating the Actual Genesis Block

After understanding the process from the test, proceed to generate the actual `genesis.json` and the genesis block:

1. **Build the Genesis JSON:** Use the information learned from the `test:genesis` to build the `genesis.json` file from the files in the `/deployments/` directory. The `generate_genesis.test.sh` script contains the necessary commands to create this file.

2. **Run Geth to Generate the Genesis Block:** You can use Geth to initialize and run a private network with the genesis block. You can start Geth with the following commands:
This runs tests using Docker and `taiko-geth` to simulate the L2 genesis block deployment, and generates a `genesis.json` file in `./test/genesis/data/`.

```sh
geth --datadir ~/taiko-l2-network/node init /deployments/genesis.json
geth --datadir ~/taiko-l2-network/node --networkid 167 --http --http.addr 127.0.0.1 --http.port 8552 --http.corsdomain "*"
```
### Generating an Actual Genesis Block

For details refer to the Geth documentation on [creating a genesis block](https://geth.ethereum.org/docs/fundamentals/private-network#creating-genesis-block).
To generate the actual L2 genesis block, create a `genesis.json` file based on `l2_genesis_alloc.json`, following [this guide](https://geth.ethereum.org/docs/fundamentals/private-network#creating-genesis-block).

3. **Retrieve the Genesis Block Hash:** Connect to the Geth node using the command:
Next, initialize `taiko-geth` with the generated `genesis.json`:

```sh
geth attach ~/taiko-l2-network/node/geth.ipc
```
```bash
geth --datadir ~/taiko-l2-network/node init test/layer2/genesis/data/genesis.json
geth --datadir ~/taiko-l2-network/node --networkid 167 --http --http.addr 127.0.0.1 --http.port 8552 --http.corsdomain "*"
```

In the Geth console, use `eth.getBlock(0)` to obtain the hash of the genesis block.
You can retrieve the genesis block hash by attaching to the `geth` instance:

4. **Update `test_deploy_on_l1.sh` File:** Update the `L2_GENESIS_HASH` variable in the `test_deploy_on_l1.sh` script with the obtained genesis block hash.
```bash
geth attach ~/taiko-l2-network/node/geth.ipc
```

By following these steps, you will successfully generate the L2 genesis block for the Taiko Protocol, retrieve its hash, and prepare for the L1 contract deployment.
Then run:

## Deploying the L1 Contracts
```bash
eth.getBlock(0)
```

To deploy L1 contracts for Taiko Protocol, you can use any Ethereum network. This guide illustrates the process using a Hardhat local network, but it's adaptable to others. The deployment relies on `script/test_deploy_on_l1.sh`, which targets a node at `http://localhost:8545` by default.
Copy the genesis block hash and replace the `L2_GENESIS_HASH` variable in `deploy_protocol_on_l1.sh` with this value.

Here’s how you can proceed:
### Deploying Taiko BCR on Layer 1

1. **Ensure Sufficient ETH:** Check that the address associated with the private key in `script/test_deploy_on_l1.sh` has enough ETH for deploying contracts on the Hardhat network.
To deploy Taiko BCR on L1, start a local L1 network:

2. **Update Contract Addresses:** After running the genesis block generation script (`pnpm test:genesis`), you will receive a list of pre-computed contract addresses. These addresses need to be added to the `test_deploy_on_l1.sh` file. Make sure to update this file with the correct contract addresses before proceeding with the deployment.
```bash
anvil --hardfork cancun
```

3. **Start a Local Development Network:** While this guide uses Hardhat as an example, you can use any Ethereum network. If you choose to use Hardhat, start a local Ethereum network for development and testing:
Make sure you have sufficient ether for transactions, then deploy the contracts:

```sh
pnpm hardhat node
```bash
pnpm test:deploy:l1
```

4. **Deploy Contracts Using Foundry:** Once your network is running, open a new terminal window and execute the deployment scripts using Foundry:
This command runs the deployment script located at `script/deploy_protocol_on_l1.sh`, assuming L1 is accessible at `http://localhost:8545`.

```sh
pnpm test:deploy
```
## Style Guide

This command will deploy the L1 contracts using the settings and addresses you’ve provided in the `test_deploy_on_l1.sh` script.
Refer to [CONTRIBUTING.md](https://www.notion.so/CONTRIBUTING.md) for code style guidelines.

## Running slither
Before committing code, format and lint it using:

1. Install the latest [slither](https://github.com/crytic/slither?tab=readme-ov-file#how-to-install).
2. From `protocol/`, execute `slither . --checklist > checklist.md` to re-generate the checklist.
```bash
pnpm fmt:sol
```
Loading

0 comments on commit adc47f4

Please sign in to comment.