Web3

2026年02月04日更新 18 人订阅
原价: ¥ 10 限时优惠
专栏简介

Go-ethereum实战笔记:从源码构建一个功能完备的私有测试网络

Go-ethereum实战笔记:从源码构建一个功能完备的私有测试网络与其在时常拥堵、水龙头枯竭的公共测试网上“看天吃饭”,不如在自己的机器上开辟一片绝对掌控的试验田。这篇实战笔记,便是我从零开始,亲手将官方Go-ethereum源码编译成一个功能完备的私有测试网络的完整记录。这里没有晦涩的理论,只

Go-ethereum实战笔记:从源码构建一个功能完备的私有测试网络

与其在时常拥堵、水龙头枯竭的公共测试网上“看天吃饭”,不如在自己的机器上开辟一片绝对掌控的试验田。这篇实战笔记,便是我从零开始,亲手将官方Go-ethereum源码编译成一个功能完备的私有测试网络的完整记录。这里没有晦涩的理论,只有一条清晰、可被任何人复现的路径,让你最终能拥有一个零成本、高效率、且配置强大的专属“兵器库”,去应对未来智能合约开发中的一切挑战。

实操

第一步:克隆代码

git clone https://github.com/ethereum/go-ethereum/

实操

git clone git@github.com:qiaopengjun5162/go-ethereum.git
正克隆到 'go-ethereum'...
remote: Enumerating objects: 126681, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 126681 (delta 0), reused 0 (delta 0), pack-reused 126678 (from 1)
接收对象中: 100% (126681/126681), 198.13 MiB | 1.01 MiB/s, 完成.
处理 delta 中: 100% (80641/80641), 完成.

第二步:切换到项目目录

cd go-ethereum

第三步:列出当前目录下的文件和文件夹

ls
AUTHORS             SECURITY.md         common              ethclient           interfaces.go       p2p                 triedb
COPYING             accounts            consensus           ethdb               internal            params              version
COPYING.LESSER      appveyor.yml        console             ethstats            log                 rlp
Dockerfile          beacon              core                event               metrics             rpc
Dockerfile.alltools build               crypto              go.mod              miner               signer
Makefile            circle.yml          docs                go.sum              node                tests
README.md           cmd                 eth                 graphql             oss-fuzz.sh         trie

ls 是一个 Linux/Unix 命令(在 Windows 的 Git Bash、WSL 或 macOS 终端中也可用),用于 列出当前目录下的文件和文件夹

常见用法:

命令 作用
ls 列出当前目录的内容(不包括隐藏文件)
ls -a 列出 所有文件(包括隐藏文件,如 .git.env
ls -l 详细列表 形式显示(权限、所有者、大小、修改时间等)
ls -la 组合 -a-l,显示所有文件的详细信息
ls /path/to/dir 列出指定目录的内容(如 ls /etc

第四步:用 Goland 编辑器打开项目

open -a Goland .

第五步:创建并切换到新分支

go-ethereum on  master via 🐹 v1.24.5 
➜ git checkout -b dev                                                                                                 
切换到一个新分支 'dev'

第六步:Build geth

go-ethereum on  dev via 🐹 v1.24.5 
➜ make geth 
go run build/ci.go install ./cmd/geth
go: downloading golang.org/x/crypto v0.36.0
go: downloading golang.org/x/net v0.38.0
go: downloading golang.org/x/text v0.23.0
>>> /opt/homebrew/opt/go/libexec/bin/go build -ldflags "--buildid=none -X github.com/ethereum/go-ethereum/internal/version.gitCommit=e94123acc2bc8283764b26f3423f5e026515c0f4 -X github.com/ethereum/go-ethereum/internal/version.gitDate=20250715 -s" -tags urfave_cli_no_docs,ckzg -trimpath -v -o /Users/qiaopengjun/Code/go/go-ethereum/build/bin/geth ./cmd/geth
go: downloading github.com/dop251/goja v0.0.0-20230605162241-28ee0ee714f3
go: downloading golang.org/x/sys v0.31.0
go: downloading github.com/ferranbt/fastssz v0.1.4
... ...
github.com/ethereum/go-ethereum/eth
github.com/ethereum/go-ethereum/eth/catalyst
github.com/ethereum/go-ethereum/cmd/utils
github.com/ethereum/go-ethereum/cmd/geth
Done building.
Run "./build/bin/geth" to launch geth.

第七步:创建 datadir 目录并配置 创世(Genesis)文件

创建 datadir 目录

mkdir datadir

配置 创世(Genesis)文件

{
  "config": {
    "chainId": 20250716,
    "homesteadBlock": 0,
    "eip150Block": 0,
    "eip155Block": 0,
    "eip158Block": 0,
    "byzantiumBlock": 0,
    "constantinopleBlock": 0,
    "petersburgBlock": 0,
    "istanbulBlock": 0,
    "muirGlacierBlock": 0,
    "berlinBlock": 0,
    "londonBlock": 0,
    "shanghaiTime": 0,
    "cancunTime": 0,
    "pragueTime": 0,
    "verkleTime": 0,
    "blobSchedule": {
      "cancun": {
        "target": 3,
        "max": 6,
        "baseFeeUpdateFraction": 3338477
      },
      "prague": {
        "target": 3,
        "max": 6,
        "baseFeeUpdateFraction": 3338477
      }
    },
    "terminalTotalDifficulty": 1700000010
  },
  "difficulty": "0x170000000",
  "gasLimit": "0x47b760",
  "alloc": {
    "0xf95D00fEE2E22829b298E49a4f7CDEc384cF9e62": {
      "balance": "0xffffffffffffffde0b6b3a7640000"
    }
  }
}

第八步:初始化 geth

go-ethereum on  dev [?] via 🐹 v1.24.5 
➜ build/bin/geth init --state.scheme=hash --datadir=datadir genesis.json 
INFO [07-16|21:38:28.500] Maximum peer count                       ETH=50 total=50
INFO [07-16|21:38:28.505] Set global gas cap                       cap=50,000,000
INFO [07-16|21:38:28.506] Initializing the KZG library             backend=gokzg
INFO [07-16|21:38:28.507] Defaulting to pebble as the backing database
INFO [07-16|21:38:28.508] Allocated cache and file handles         database=/Users/qiaopengjun/Code/go/go-ethereum/datadir/geth/chaindata cache=512.00MiB handles=5120
INFO [07-16|21:38:28.605] Opened ancient database                  database=/Users/qiaopengjun/Code/go/go-ethereum/datadir/geth/chaindata/ancient/chain readonly=false
INFO [07-16|21:38:28.606] Opened Era store                         datadir=/Users/qiaopengjun/Code/go/go-ethereum/datadir/geth/chaindata/ancient/chain/era
INFO [07-16|21:38:28.607] State scheme set by user                 scheme=hash
ERROR[07-16|21:38:28.608] Head block is not reachable
INFO [07-16|21:38:28.608] Writing custom genesis block
INFO [07-16|21:38:28.614] Persisted trie from memory database      nodes=1 size=155.00B time="4.416µs" gcnodes=0 gcsize=0.00B gctime=0s livenodes=0 livesize=0.00B
INFO [07-16|21:38:28.614] Successfully wrote genesis state         database=chaindata hash=cca79b..bd26ea

第九步:启动 geth 节点

./build/bin/geth \
  --datadir ./datadir \
  --networkid=$CHAIN_ID \
  --http \
  --http.addr=0.0.0.0 \
  --http.port 8545 \
  --http.corsdomain="*" \
  --http.vhosts="*" \
  --http.api "eth,web3,net,txpool,debug,personal" \
  --ws \
  --ws.addr=0.0.0.0 \
  --ws.port=8546 \
  --ws.origins="*" \
  --ws.api "eth,web3,net,txpool,debug" \
  --syncmode=full \
  --gcmode=archive \
  --nodiscover \
  --maxpeers=0 \
  --verbosity 3 \
  --log.format=json 

实操


go-ethereum on  dev [?] via 🐹 v1.24.5 
➜ export CHAIN_ID=20250716

go-e...

剩余60%的内容订阅专栏后可查看

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

0 条评论

请先 登录 后评论