Geth是Go Ethereum开源项目的简称,它是使用Go语言编写且实现了Ethereum协议的客户端软件,也是目前用户最多,使用最广泛的客户端。通过Geth客户端与以太坊网络进行连接和交互可以实现账户管理、合约部署、挖矿等众多有趣且实用的功能。
系统环境:ubuntu18+SSD硬盘
add-apt-repository ppa:git-core/ppa
apt-get update
apt-get install -y openjdk-8-jdk unzip make gcc
注意:
1.如geth使用最新版本go环境也尽量使用最新版本,避免安装出错,这里以go1.14.2 ,geth1.9为例;
2.硬盘必须ssd或其他云盘等比较快的磁盘,不可使用普通SATA盘,不然会出现一直同步不完情况;
3.Fast模式下当前数据量(2020-05)为220G左右,由于同步数据量不断增加,当前安装建议硬盘不小于300G(Full模式同步建议使用1T及以上硬盘),有条件应尽量增加硬盘大小或使用动态可扩展大小的云硬盘;
时区调整:
timedatectl set-timezone 'Asia/Shanghai'
golang环境安装:
wget http://mirror.cnop.net/go/go1.14.2.linux-amd64.tar.gz
tar zxvf go1.14.2.linux-amd64.tar.gz && mv go /usr/local/
加入环境变量:
vi /etc/profile
export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin
立即生效:
source /etc/profile
查看版本:
go version
geth安装:
mkdir /data && cd /data
wget http://mirror.cnop.net/geth/v1.9.13.tar.gz
tar zxvf v1.9.13.tar.gz
mv go-ethereum-1.9.13 go-ethereum
cd go-ethereum
make geth
加入环境变量,尾部加入:
vi /etc/profile
export PATH=$PATH:/data/go-ethereum/build/bin
立即生效:
source /etc/profile
查看版本:
geth version
启动(ip更换成自己的),以下模式任选一种:
安装screen,后期可以使用-r参数随时进入同步界面:
apt install screen -y
screen -S geth
full 模式:
geth -rpc -rpcaddr 0.0.0.0 -rpcport 8585 --allow-insecure-unlock -rpcapi personal,eth,net,web3,admin -datadir /data/chaindata -syncmode full -maxpeers 9999 -cache 1024 >/data/geth.log
Fast模式:
geth -rpc -rpcaddr 0.0.0.0 -rpcport 8585 --allow-insecure-unlock -rpcapi personal,eth,net,web3,admin -datadir /data/chaindata -syncmode fast -maxpeers 9999 -cache 1024 >/data/geth.log
全模式:
获取区块的报头
获取区块的身体
从创始块开始校验每一个元素
下载所有区块数据信息
快速模式:
获取区块的报头
获取区块的身体
在同步到当前块之前不处理任何事务,然后获得一个快照,像全节点一样进行后面的同步操作。沿着区块下载最近数据库中的交易,有可能丢失历史数据。比如,你的账户地址甲上面有10个ETH,但转入的的交易存在于较老的历史交易中,此同步模式无法获取到交易的详细情况。
光模式:
仅获取当前状态。验证元素需要向全节点发起相应的请求。
停止:
screen -r geth #进入之前同步窗口。
ctrl+z #退出,切勿 kill -9 命令强制退出,以免损坏数据库。
常用命令
进入控制台:geth attach http://127.0.0.1:8585
查询同步情况:
eth.syncing
最新区块的高度:
eth.getBlock("latest").number
或
eth.blockNumber
查余额:
eth.getBalance
客户端所连接的对端节点数量:
net.peerCount