构建optimism私链

目的是要搭建一条optimism链,其中L1为以太坊goerlitestnet。1、软件依赖软件版本安装命令ubuntu20.04LTSgit,curl,makegonode16.19.0yarnfoundr

目的是要搭建一条optimism链,其中L1为以太坊goerli testnet。

1、软件依赖

软件 版本 安装命令
ubuntu 20.04 LTS
git, curl, make
go
node 16.19.0
yarn
foundry 0.2.0 curl -L https://foundry.paradigm.xyz bash . \~/.bashrc foundryup

2、编译源码 optimism

git clone https\://github.com/ethereum-optimism/optimism.git
cd  optimism
yarn install
make op-node op-batcher
yarn build

op-geth

git clone https\://github.com/ethereum-optimism/op-geth.git
cd op-geth
make geth

3、通过alchemy获取访问goerli L1 节点的url。 4、准备4个有资金的地址

1、Admin有能力更新合约的地址,部署合约
Admin: 0xb51076f56ce3b72c53a6e47c6097fc0145b6fec6
Private Key: fb8c944efea1076f3d952639adfede19d810eb948cdc6************

2、Proposer: 将L2的交易结果推送给L1
Proposer: 0xc420cd834d4e238de5ca1f9fa43dfea55b65ab4f      
Private Key: 2c30ebec582167d4e1721696efdf30b576d991077fd6b************

3、Batcher: 将sequencer中的交易数据推送到L1
Batcher: 0xfa7d40f2cb3ac6ee27fa7ed6967ad47a7dd61eae       
Private Key: 81587c1bf618c1b5acf637768327e81e0e1e4ae28aba3************

4、Sequencer:签名p2p网络上的区块
Sequencer: 0x81db1a08cf0cda61582453b6146cb9c72bc261c1     
Private Key: 6b795e64b7db54b36f593182331efab7bd2e6a274ab18************

admin地址需要充goerli eth,因为需要用它部署智能合约。proposer和batcher也需要充钱,其中batcher因为要推送交易数据到L1,它会燃烧掉很多eth 建议的账户资金

proposer 0.5eth
batcher 1.0 eth

5、配置网络

  • 当下链相关配置在contract-bedrock目录 cd \~/optimism/packages/contract-bedrock
  • 先选定一个L1 块作为我们rollup的起点。最好是选定一个finalized区块。使用foundry提供的cast命令来获取所需的信息。rpc为goerli node上的。 cast block finalized --rpc-url <RPC> | grep -E "(timestamp|hash|number)" 获得起点信息
ubuntu@ubuntu:optimism/packages/contracts-bedrock$ cast block finalized --rpc-url https://eth-goerli.g.alchemy.com/v2/zkhmESllhO2QYoXuUje99N9_4gFqezsW  | grep -E "(timestamp|hash|number)"
hash                 0x938c8f11dfb4cd7fda2b91312bfcdfe85c4ecd2c57e42bfb8dfbed8d11c19e40
number               8743286
timestamp            1680168288
  • 修改配置文件deploy-config/getting-started.json
* Replace "ADMIN" with the address of the Admin account you generated earlier.
* Replace "PROPOSER" with the address of the Proposer account you generated earlier.
* Replace "BATCHER" with the address of the Batcher account you generated earlier.
* Replace "SEQUENCER" with the address of the Sequencer account you generated earlier.
* Replace "BLOCKHASH" with the blockhash you got from the cast command.
* Replace TIMESTAMP with the timestamp you got from the cast command. Note that although all the other fields are strings, this field is a number! Don’t include the quotation marks.

6、部署L1合约

  • 网络配置好后就可以部署L1合约了。在contracts-bedrock中,复制.env.example到.env中
  • .env文件中修改如下两个值
* L1_RPC — URL for your L1 node.
* PRIVATE_KEY_DEPLOYER — Private key of the Admin account.
  • 部署L1智能合约,这一步大概要执行15分钟,等待所有的合约部署完成后再进行下一步。 npx hardhat deploy --network getting-started 部署成功,输出日志(日志很长~) 7、生成L2的配置文件,在L2这一层,我们需要生成3个重要的文件:genesis.json, rollup.json, jwt.json(JSON WEB TOKEN,它允许op-node和op-geth安全交流) 进入op-node目录,替换RPC为自己的L1 RPC URL,执行如下命令
go run cmd/main.go genesis l2 \
    --deploy-config ../packages/contracts-bedrock/deploy-config/getting-started.json \
    --deployment-dir ../packages/contracts-bedrock/deployments/getting-started/ \
    --outfile.l2 genesis.json \
    --outfile.rollup rollup.json \
    --l1-rpc https://eth-goerli.g.alchemy.com/v2/ov3CnhRMyIfiJ9eg5h1qUwdksgWc7XqK

执行成功的话,在op-node目录下会看到genesis.json和rollup.json文件。

  • 生成jwt.txt openssl rand -hex 32 > jwt.json 将genesis.json和jwt.json文件拷贝到op-geth,这样我们等下才能用它来初始化和运行op-geth 8、初始化op-geth 我们将要运行一个sequencer node,需要导入sequencer的私钥,sequencer会用这个私钥来给block签名。
  • 进入op-geth目录,创建数据文件夹 mkdir datadir
  • 保存一个密码 echo "pwd" > datadir/password
  • 将squencer的秘钥放到datadir里面(秘钥不需要携带0x前缀) echo "<SEQUENCER KEY HERE>" > datadir/block-signer-key
  • 将秘钥导入op-geth中
./build/bin/geth account import --datadir=datadir --password=datadir/password datadir/block-signer-key
INFO [03-30|19:32:00.604] Maximum peer count                       ETH=50 LES=0 total=50
INFO [03-30|19:32:00.606] Smartcard socket not found, disabling    err="stat /run/pcscd/pcscd.comm: no such file or directory"
INFO [03-30|19:32:00.611] Set global gas cap                       cap=50,000,000
Address: {81db1a08cf0cda61582453b6146cb9c72bc261c1}
  • 使用genesis.json来初始化op-geth
build/bin/geth init --datadir=datadir genesis.json
INFO [03-30|19:32:37.431] Maximum peer count                       ETH=50 LES=0 total=50
INFO [03-30|19:32:37.438] Smartcard socket not found, disabling    err="stat /run/pcscd/pcscd.comm: no such file or directory"
INFO [03-30|19:32:37.445] Set global gas cap                       cap=50,000,000
INFO [03-30|19:32:37.448] Using leveldb as the backing database
INFO [03-30|19:32:37.450] Allocated cache and file handles         database=/home/ubuntu/code/layer2/op/op-geth/datadir/geth/chaindata cache=16.00MiB handles=16
INFO [03-30|19:32:37.464] Using LevelDB as the backing database
INFO [03-30|19:32:37.479] Opened ancient database                  database=/home/ubuntu/code/layer2/op/op-geth/datadir/geth/chaindata/ancient/chain readonly=false
INFO [03-30|19:32:37.479] Writing custom genesis block
INFO [03-30|19:32:37.864] Persisted trie from memory database      nodes=3116 size=448.22KiB time=9.737762ms gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [03-30|19:32:38.165] Successfully wrote genesis state         database=chaindata hash=e36ec8..6da447
INFO [03-30|19:32:38.165] Using leveldb as the backing database
INFO [03-30|19:32:38.165] Allocated cache and file handles         database=/home/ubuntu/code/layer2/op/op-geth/datadir/geth/lightchaindata cache=16.00MiB handles=16
INFO [03-30|19:32:38.179] Using LevelDB as the backing database
INFO [03-30|19:32:38.230] Opened ancient database                  database=/home/ubuntu/code/layer2/op/op-geth/datadir/geth/lightchaindata/ancient/chain readonly=false
INFO [03-30|19:32:38.234] Writing custom genesis block
INFO [03-30|19:32:38.592] Persisted trie from memory database      nodes=3116 size=448.22KiB time=15.243075ms gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [03-30|19:32:38.809] Successfully wrote genesis state         database=lightchaindata hash=e36ec8..6da447

9、运行op-geth,将如下命令中的<SEQUENCER>替换成sequencer对应的地址

    ./build/bin/geth \
    --datadir ./datadir \
    --http \
    --http.corsdomain="*" \
    --http.vhosts="*" \
    --http.addr=0.0.0.0 \
    --http.api=web3,debug,eth,txpool,net,engine \
    --ws \
    --ws.addr=0.0.0.0 \
    --ws.port=8546 \
    --ws.origins="*" \
    --ws.api=debug,eth,txpool,net,engine \
    --syncmode=full \
    --gcmode=full \
    --nodiscover \
    --maxpeers=0 \
    --networkid=42069 \
    --authrpc.vhosts="*" \
    --authrpc.addr=0.0.0.0 \
    --authrpc.port=8551 \
    --authrpc.jwtsecret=./jwt.txt \
    --rollup.disabletxpoolgossip=true \
    --password=./datadir/password \
    --allow-insecure-unlock \
    --mine \
    --miner.etherbase=&lt;SEQUENCER> \
    --unlock=&lt;SEQUENCER>

运行成功后还不会有任何区块生成,因为op-geth是由op-node驱动的,接下来我们还需要启动op-node 10、运行一个op-node 就像以太坊一样,op也有一个共识客户端(op-node)、一个执行客户端(op-geth),共识客户端通过engine api驱动执行客户端。 进入op-node包下,使用如下命令启动,<SEQUENCER>替换为sequencer账号,<RPC>替换为L1的URL

    ./bin/op-node \
    --l2=http://localhost:8551 \
    --l2.jwt-secret=./jwt.txt \
    --sequencer.enabled \
    --sequencer.l1-confs=3 \
    --verifier.l1-confs=3 \
    --rollup.config=./rollup.json \
    --rpc.addr=0.0.0.0 \
    --rpc.port=8547 \
    --p2p.disable \
    --rpc.enable-admin \
    --p2p.sequencer.key=&lt;SEQUENCERKEY> \
    --l1=&lt;RPC> \
    --l1.rpckind=&lt;RPCKIND>

该命令执行成功后,op-node开始处理L1上选定的起始块之后的所有信息,一旦op-node有了足够的信息,它就开始发送消息给op-geth,这时,将会看到op-geth上有区块被创建,成功了。 11、运行op-batcher op-batcher将sequencer上的交易发布到L1上,一旦交易到达L1,他们就成了Rollup的一部分。没有op-batcher,发送到sequencer的交易就不会被放到L1上也不会成为标准链的一部分。

  • 进入op-batcher目录 cd ~/optimism/op-batcher
  • 使用如下命令运行op-batcher,同样的需要替换<RPC>和<BATCHERKEY>,需要保证batcher地址里至少有1eth
    ./bin/op-batcher \
    --l2-eth-rpc=http://localhost:8545 \
    --rollup-rpc=http://localhost:8547 \
    --poll-interval=1s \
    --sub-safety-margin=6 \
    --num-confirmations=1 \
    --safe-abort-nonce-too-low-count=3 \
    --resubmission-timeout=30s \
    --rpc.addr=0.0.0.0 \
    --rpc.port=8548 \
    --target-l1-tx-size-bytes=2048 \
    --l1-eth-rpc=&lt;RPC> \
    --private-key=&lt;BATCHERKEY>

12、get some eth on your rollup 当连上你的钱包,你会发现你的rollup里面没有任何的钱。你需要在你的rollup里面存放一点eth用于支付gas。在你的连上最容易的质押eth方式就是往这个 OptimismPortalProxy合约里发送资金。找到这个合约的地址

    cd ~/optimism/packages/contracts-bedrock
cat deployments/getting-started/Proxy__OVM_L1StandardBridge.json.json | grep \"address\":

大致会得到这样的输出

    "address": "0x874f2E16D803c044F10314A978322da3c9b075c7",
        "internalType": "address",
        "type": "address"
        "internalType": "address",
        "type": "address"
        "internalType": "address",
        "type": "address"
        "internalType": "address",
        "type": "address"

拿到这个L1桥代理合约地址,使用你想在你的rollup上拥有eth的钱包,向这个地址发送少量的eth,大概5分钟后你l2钱包的地址上就会收到eth 13、现在你拥有了一个完整的基于evm的op stack。

点赞 4
收藏 3
分享
本文参与登链社区写作激励计划 ,好文好收益,欢迎正在阅读的你也加入。

1 条评论

请先 登录 后评论
大果园儿
大果园儿
江湖只有他的大名,没有他的介绍。