运行 geth 节点

>![eth](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/169726/dbc62e41-7483-9231-a472-a93d7b3c924f.jpeg)

## ppa 安装 geth

```shell
sudo apt install software-properties-common

sudo add-apt-repository -y ppa:ethereum/ethereum

sudo apt update
sudo apt install ethereum

geth version # 查看 geth 版本
```


## 运行 geth

```shell
geth

INFO [03-12|21:50:36] Maximum peer count                       ETH=25 LES=0 total=25
INFO [03-12|21:50:36] Initialised chain configuration          config="{ChainID: 1 Homestead: 1150000 DAO: 1920000 DAOSupport: true EIP150: 2463000 EIP155: 2675000 EIP158: 2675000 Byzantium: 4370000 Engine: ethash}"
INFO [03-12|21:50:36] Disk storage enabled for ethash caches   dir=~/.ethereum/geth/ethash count=3
INFO [03-12|21:50:36] Disk storage enabled for ethash DAGs     dir=~/.ethash               count=2
INFO [03-12|21:50:36] Initialising Ethereum protocol           versions="[63 62]" network=1
INFO [03-12|21:50:36] Loaded most recent local header          number=0 hash=d4e567…cb8fa3 td=17179869184
INFO [03-12|21:50:36] Loaded most recent local full block      number=0 hash=d4e567…cb8fa3 td=17179869184
INFO [03-12|21:50:36] Loaded most recent local fast block      number=0 hash=d4e567…cb8fa3 td=17179869184
INFO [03-12|21:50:36] Loaded local transaction journal         transactions=0 dropped=0
INFO [03-12|21:50:36] Regenerated local transaction journal    transactions=0 accounts=0
INFO [03-12|21:50:36] Starting P2P networking
INFO [03-12|21:50:36] Mapped network port                      proto=udp extport=30303 intport=30303 interface=NAT-PMP(192.168.1.1)
INFO [03-12|21:50:36] UDP listener up                          self=enode://67d9b38f8a2f9874ef60d53a52492efecaaad5f662d08ef@192.168.1.1:30303
INFO [03-12|21:50:36] RLPx listener up                         self=enode://67d9b38f8a2f9874ef60d53a52492efecaaad5f662d08ef@192.168.1.1:30303
INFO [03-12|21:50:36] IPC endpoint opened                      url=~/.ethereum/geth.ipc
INFO [03-12|21:50:36] Mapped network port                      proto=tcp extport=30303 intport=30303 interface=NAT-PMP(192.168.1.1)
```

### 设定 datadir

`./build/bin/geth --datadir /mnt/eth`

### geth syncmode

```
--syncmode value               (default: snap)
  Blockchain sync mode ("snap", "full" or "light")
```

syncmode 目前有 3 种方式：

- snap，默认为 snap，fast 方式已经废弃
- light
- full


### geth gcmode

```
--gcmode value                 (default: "full")
  Blockchain garbage collection mode ("full", "archive")
```

gcmode 目前有 2 种方式：

- full，默认为 full
- archive


### 同步方式组合

```shell
geth --gcmode archive --syncmode full # 占用的磁盘空间最多，也是同步 eth 全量历史数据的方式
```

### geth 运行命令

```shell
geth --datadir ./data --gcmode archive --syncmode full --txlookuplimit 0
--http --http.addr 0.0.0.0 --http.api web3,eth,net,txpool,debug

# --http 开启rpc
# --http.api web3,eth,net,txpool,debug 开启访功能模块
```

## 守护进程

https://hiddentao.com/archives/2016/05/04/setting-up-geth-ethereum-node-to-run-automatically-on-ubuntu/

```ini
#/etc/supervisor/conf.d/gethd.conf

[program:gethd]
command=/usr/local/bin/geth --datadir /mnt/eth/data --syncmode full --rpc
user=deployer # deployer 用户启动任务
autostart=true
autorestart=true
stderr_logfile=/mnt/eth/data/geth_err.log
stdout_logfile=/mnt/eth/data/geth.log
```

`sudo supervisorctl reload`

---

## 编译 ethereum

### 安装 golang

**go-ethereum 要求 go 版本不低于 1.17**

```shell
sudo apt install -y snapd

sudo snap install go --classic

go version # 查看 go 版本
```

### 编译

```shell
git clone https://github.com/ethereum/go-ethereum
cd go-ethereum
git checkout v1.10.0
make geth #编译
```

生成执行文件 `./build/bin/geth`


## 卸载低版本 golang

```
sudo apt remove golang-go
sudo apt remove --auto-remove golang-go
```

---

参考：

https://github.com/golang/go/wiki/Ubuntu

https://geth.ethereum.org/docs/install-and-build/installing-geth