Daniel

Posted on Jun 25, 2022Read on Mirror.xyz

手把手教学|自建一个ETH2验证者

ETH2.0合并在即,如何自己做一个验证者呢?

看完此文可以学会自建ETH节点,ETH2节点,验证者节点

当然上面熟练了BNB,Matic等各种链的节点也能快速上手

测试网教学,无代币成本无收益

前提硬件条件

服务器

系统Ubuntu20.4

CPU 4核以上 内存16G 硬盘SSD 500G(200G够用)

需要开放端口,如果要自己调用RPC也需要把端口打开

Geth 30303 TCP/UDP

Prysm13000 TCP, 12000 UDP

节点原理

节点是去中心化的保证,可以验证每个区块中的所有交易,从而确保网络安全和数据准确。无论是POW还是POS都需要节点来与链上交互,对于用户来说,Chainlist添加网络和小狐狸里面配置RPC URL最容易理解。有了自己的节点有什么好处呢,延迟低,QPS几乎无额外限制,无论是读链上数据还是发送消息都会比公用节点快且不卡(抢开盘抢NFT之类的都是必备)。也是为去中心化作出贡献。

搭建一个ETH2验证者的流程,首先需要同步执行层网络,即拥有一个ETH1同步好的节点。然后同步一个信标节点,即拥有一个ETH2同步好的节点,然后打入存款生成一个验证者钱包,运行验证者。

ETH1节点

目前有很多客户端,GethOpenEthereumNethermindBesu等,任意选择一个客户端均可搭建节点加入到eth1的网络当中。

这里用Geth举例。以下命令安装Geth

sudo add-apt-repository -y ppa:ethereum/ethereum
sudo apt-get update
sudo apt-get install ethereum

配置geth,这里演示的是测试网goerli。

geth --goerli --syncmode "snap" dumpconfig > goerli.toml

会在当前目录生成一个goerli.toml文件,需要vim进去修改相应的配置。主要修改下面配置。

[Eth]
NetworkId = 5      ## 注意goerli的id是5 
SyncMode = "snap"   ##硬盘大可以用full,快速的话用snap
[Node]
DataDir = "/home/data/goerli"  ##节点数据存储地址
HTTPHost = "0.0.0.0"           ##任何ip都可访问  
HTTPPort = 8545              
HTTPVirtualHosts = ["0.0.0.0"]
HTTPModules = ["net", "web3", "eth"]
AuthAddr = "localhost"
AuthPort = 8551
AuthVirtualHosts = ["localhost"]
WSHost = ""
WSPort = 8546
WSModules = ["net", "web3", "eth"]
GraphQLVirtualHosts = ["localhost"]

检查配置无问题之后可以运行起来,等待同步结束(大概需要几个小时,根据网络情况)

geth --goerli --config goerli.toml

这里放上geth自动启动命令,无论重启系统,关停服务都可以自动重启,注意配置goerli.toml路径

#!/bin/bash
sudo apt-get install supervisor -y

cat > /home/start_goerli.sh <<EOF
#!/bin/bash
source /etc/profile
ulimit -n 1048576
exec geth --goerli --config goerli.toml  > /home/goerli_daemon.log 2>&1 
sleep 5
EOF
chmod  +x /home/start_goerli.sh

cat > /etc/supervisor/conf.d/goerli.conf <<EOF
; /etc/supervisor/conf.d/goerli.conf

[program:goerli]
command=bash /home/start_goerli.sh
autostart=true
autorestart=true
startretries=5
numprocs=1
startsecs=0
process_name=%(program_name)s_%(process_num)02d
stderr_logfile=/var/log/supervisor/%(program_name)s_stderr.log
stderr_logfile_maxbytes=10MB
stdout_logfile=/var/log/supervisor/%(program_name)s_stdout.log
stdout_logfile_maxbytes=10MB
EOF
chmod  +r /etc/supervisor/conf.d/goerli.conf

##启动
supervisorctl reread
supervisorctl update
supervisorctl reload goerli

#停掉
supervisorctl stop goerli

ETH2节点

共识客户端也有很多 PrysmNimbusLighthouseTekuLodestar。这里选择Prysm做演示。(如果快速同步需要从源码编译,直接安装不行)

安装Prysm

mkdir prysm && cd prysm
curl https://raw.githubusercontent.com/prysmaticlabs/prysm/master/prysm.sh --output prysm.sh && chmod +x prysm.sh
wget https://github.com/eth-clients/eth2-networks/raw/master/shared/prater/genesis.ssz

创建本地配置文件prysm.yaml,文件内容如下

datadir: '/home/data/prysm' ##节点数据存储位置
http-web3provider: "http://127.0.0.1:8545"  ## eth1节点本地ip和端口
genesis-state: "/home/data/prysm/genesis.ssz" ##下载的创世文件路径
suggested-fee-recipient: "0x6ead271a45acc328af22b369870509471a46f59d" ##fee接收钱包地址
grpc-gateway-port: "7500"
grpc-gateway-host: "0.0.0.0"
## 启动并且同步eth2节点
./prysm.sh beacon-chain --prater --config-file=prysm.yaml

如果想要快速同步需要拿到checkpoint的数据,可以从infura拿到api然后下载,

从源码编译之后
go run github.com/prysmaticlabs/prysm/cmd/prysmctl checkpoint save --beacon-node-host=API
然后在配置文件中加入下载好的文件的路径即可。
checkpoint-block: "/home/data/prysm/block_prater_altair_3303360-0xff191dd6f5eb096171344c94f88b708f30baa443404363ae25359e6ddcb86812.ssz"
checkpoint-state: "/home/data/prysm/state_prater_altair_3303360-0xfe5ab02a48e6af7f90e0cf8d995590b216f1efebca115fe3e3df747b81d530ed.ssz"

验证者

ETH2节点同步好之后即可做验证者。

1.需要用stakeing deposit生成一个验证器密钥,下载地址如下

https://github.com/ethereum/staking-deposit-cli

./deposit new-mnemonic --num_validators=1 --mnemonic_language=english --chain=prater

2.导入到prysm账户里面

./prysm.sh validator accounts import --keys-dir=<YOUR_FOLDER_PATH> --prater

3.在官方平台进行eth1链上的存款存入,这个时候需要32个测试ETH,可以去各种水龙头领也可以去社区请求支援。

https://prater.launchpad.ethereum.org/en/overview

4.运行验证者

./prysm.sh validator --wallet-dir=<YOUR_FOLDER_PATH> --prater

总结: 对于小白用户难度极高,非兴趣爱好者请勿尝试,其中的各种坑不少,文档更新不及时。但是如果研究明白搭建其他任意链节点都相对容易,很多激励测试网也是搭建节点的玩法,熊市多学习总会有用的~