手把手教你在 macOS 上搭建属于自己的 Geth 私链

@TOC

一、安装 ethereum 客户端

brew tap ethereum/ethereum
添加以太坊的 Homebrew 软件仓库，为后续安装做准备

brew install ethereum
从以太坊仓库中安装 Ethereum 客户端（主要是 Geth）

二、创建所需文件夹，配置 genesis.json 文件

1. 新建一个 blockchain 文件夹来存储本地 geth，里面分别创建 data0、geth 文件夹，以及 genesis.json 文件，如下图所示

data0 文件夹：
存放区块链数据（如区块、交易、账户状态）以及相关配置

geth 文件夹：
是 Geth 生成的子目录，存储具体的区块数据、日志、密钥库等信息

genesis.json 文件：
创世区块配置文件，用于定义私链的初始状态（如难度、gasLimit、预置账户等）
下面是 genesis.json 文件的代码

Copy
{
"config": {
"chainId": 1337,
"homesteadBlock": 0,
"eip150Block": 0,
"eip155Block": 0,
"eip158Block": 0,
"byzantiumBlock": 0,
"constantinopleBlock": 0,
"petersburgBlock": 0,
"istanbulBlock": 0,
"muirGlacierBlock": 0,
"berlinBlock": 0,
"londonBlock": 0,
"arrowGlacierBlock": 0,
"grayGlacierBlock": 0,
"shanghaiTime": 0,
"terminalTotalDifficulty": 0,
"terminalTotalDifficultyPassed": true
},
"nonce": "0x0",
"timestamp": "0x0",
"extraData": "0x",
"gasLimit": "0xaf79e0",
"difficulty": "0x1",
"mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"coinbase": "0x0000000000000000000000000000000000000000",
"alloc": {
"0000000000000000000000000000000000000003": {
"balance": "0x1"
},
"0000000000000000000000000000000000000003": {
"balance": "0x1"
},
"0000000000000000000000000000000000000003": {
"balance": "0x1"
},
"0000000000000000000000000000000000000004": {
"balance": "0x1"
},
"0000000000000000000000000000000000000005": {
"balance": "0x1"
},
"0000000000000000000000000000000000000006": {
"balance": "0x1"
},
"0000000000000000000000000000000000000007": {
"balance": "0x1"
},
"0000000000000000000000000000000000000008": {
"balance": "0x1"
},
"0000000000000000000000000000000000000009": {
"balance": "0x1"
}
},
"number": "0x0",
"gasUsed": "0x0",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"baseFeePerGas": "0x3b9aca00",
"excessBlobGas": null,
"blobGasUsed": null
}

三、生成区块链的创世区块

1. 初始化区块链数据目录并生成创世区块
geth --datadir data0 init genesis.json

其中 geth/chaindata 中存放的是区块数据，keystore 中存放的是账户数据，但现在还是为空

2. 启动本地私链
geth --datadir data0 --networkid 10 --http --http.port 8545 --http.api personal,db,eth,net,web3,miner,admin console

3. 查询账户
eth.accounts

4. 创建账号
右键再打开一个终端
geth --datadir data0 account new

![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/61bdad54161747d58ecd48187cdeef84.png#pic_center =300x) 5. 再次查询账户
eth.accounts

6. 查询账户余额
eth.getBalance(eth.accounts[0])

发现余额为 0，且挖矿后，余额也没有变化

== 这里要初始化账号的金额，因为从 geth 的 13 版本后，就要在 genesis.json 里面设置账号金额了，所以回到 genesis.json 文件，重新修改代码，我们要替换成刚刚创建的账户私钥，并设置金额 ==

至于为什么一开始不提前在 genesis.json 文件里面设置好，因为要先创建账户呀

7. 重新修改 genesis.json 文件

Copy
{
"config": {
"chainId": 1337,
"homesteadBlock": 0,
"eip150Block": 0,
"eip155Block": 0,
"eip158Block": 0,
"byzantiumBlock": 0,
"constantinopleBlock": 0,
"petersburgBlock": 0,
"istanbulBlock": 0,
"muirGlacierBlock": 0,
"berlinBlock": 0,
"londonBlock": 0,
"arrowGlacierBlock": 0,
"grayGlacierBlock": 0,
"shanghaiTime": 0,
"terminalTotalDifficulty": 0,
"terminalTotalDifficultyPassed": true
},
"nonce": "0x0",
"timestamp": "0x0",
"extraData": "0x",
"gasLimit": "0xaf79e0",
"difficulty": "0x1",
"mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"coinbase": "0x0000000000000000000000000000000000000000",
"alloc": {
"8f3D6d2d65948cFE4470cCD478F14194f630e813": {
"balance": "0x100000000000000000000000000"
},
"0000000000000000000000000000000000000003": {
"balance": "0x1"
},
"0000000000000000000000000000000000000003": {
"balance": "0x1"
},
"0000000000000000000000000000000000000004": {
"balance": "0x1"
},
"0000000000000000000000000000000000000005": {
"balance": "0x1"
},
"0000000000000000000000000000000000000006": {
"balance": "0x1"
},
"0000000000000000000000000000000000000007": {
"balance": "0x1"
},
"0000000000000000000000000000000000000008": {
"balance": "0x1"
},
"0000000000000000000000000000000000000009": {
"balance": "0x1"
}
},
"number": "0x0",
"gasUsed": "0x0",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"baseFeePerGas": "0x3b9aca00",
"excessBlobGas": null,
"blobGasUsed": null
}

8. 重新初始化
== 删除 geth 文件夹，目的是清空数据目录，因为 data0 数据目录中已经存在一个创世区块（genesis）==

重新初始化
geth --datadir data0 init genesis.json

9. 再次启动本地私链，查询账户余额
geth --datadir data0 --networkid 10 --http --http.port 8545 --http.api personal,db,eth,net,web3,miner,admin console

查询余额
eth.getBalance(eth.accounts[0])

至此，我们已经成功在 macOS 上搭建了一个属于自己的 Geth 私链。从环境安装到私链初始化，再到节点启动和账户查询

希望这篇教程对你有所帮助，带你迈出区块链开发的重要一步！如果遇到问题，不妨多尝试，多学习，相信你一定能玩转 Geth！