使用Geth搭建Ethereum以太坊私链

本篇介绍的用Geth搭建Ethereum以太坊私链。

下载安装geth

wget https://gethstore.blob.core.windows.net/builds/geth-alltools-linux-amd64-1.9.17-748f22c1.tar.gz
tar -zxvf geth-alltools-linux-amd64-1.9.17-748f22c1.tar.gz
mv geth-alltools-linux-amd64-1.9.17-748f22c1 geth_1.9.17
# 不拷到 /usr/bin也可以,只要能访问到,或者配置环境变量
cp geth_1.9.17/geth /usr/bin

1. 创世块配置

创建一个目录,比如myeth
创世块配置,两种方式。

  1. 手动创建genesis.json,内容如下
{
    "nonce": "0x0000000000000042",
    "difficulty": "0x800",
    "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "coinbase": "0x0000000000000000000000000000000000000000",
    "timestamp": "0x00",
    "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "extraData": "0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa",
    "gasLimit": "0x4c4b4000000",
    "config": {
        "chainId": 216,
        "homesteadBlock": 0,
        "eip150Block": 0,
        "eip155Block": 0,
        "eip158Block": 0,
        "byzantiumBlock": 0,
        "constantinopleBlock": 0,
        "petersburgBlock": 0
    },
    "alloc": {}
}
  1. 使用geth tools中带的puppeth工具来生成,按提示一步步来即可,导出后可手动修改部分配置。推荐这个。

2. 初始化私链

所有节点上都需要执行,且要使用相同的创世块配置。

geth --datadir /myeth init /myeth/genesis.json

运行节点

运行节点并进入console

geth --networkid 216 --datadir /myeth --identity "myeth1" --port 30000 --rpc --rpcaddr "0.0.0.0" --rpccorsdomain "*" --rpcport 8545 --rpcapi "eth,net,web3,personal" --allow-insecure-unlock --unlock "0x..." --password "pwd_file" --nodiscover  console

--nodiscover是不被其它节点发现,需要时可以去掉。
--unlock--password是如果在创世块中指定了特定地址挖矿,需要在这里解锁。
geth不允许在开启RPC时解锁账户,需加--allow-insecure-unlock。正式环境慎用。

4. 创建地址

# 引号中的是密码
personal.newAccount('123456')

或通过 geth --datadir="./path" account new来创建

5. 挖矿

miner.start()
miner.stop()

或在启动命令中加上 --mine

子节点的操作步骤基本同主节点,不过多了下面添加节点的一个步骤。

6. 添加节点

  1. 查看主节点信息
admin.nodeInfo

得到类似如下信息

{
  enode: "enode://219bc7812a45b0f989d9c37e73d390cb208726afd95919626888af0643f98e75a444d2703ec9e6c317a20d57c80ad88307809e82316ebe8fb52a3a40987a843b@192.168.0.123:30000?discport=0",
  ...
  }
}

其中enode就是我们要的节点数据。在子节点上执行

admin.addPeer("enode://219bc7812a45b0f989d9c37e73d390cb208726afd95919626888af0643f98e75a444d2703ec9e6c317a20d57c80ad88307809e82316ebe8fb52a3a40987a843b@192.168.0.123:30000");

或者采用静态节点的方式
在/myeth/geth目录下新建 static-nodes.json,内容如下

[
"enode://219bc7812a45b0f989d9c37e73d390cb208726afd95919626888af0643f98e75a444d2703ec9e6c317a20d57c80ad88307809e82316ebe8fb52a3a40987a843b@192.168.0.123:30000",
"enode://id@ip:port"
]

7. 测试交易

  1. 首先解锁主地址

    personal.unlockAccount(eth.accounts[0])
  2. 发送交易
    ETH小数位18位,value要乘以10^18。

    eth.sendTransaction({from:eth.accounts[0],to:"0xea23482cbc16468d67e966757c10f367adcb5329",value:10000000})
  3. 查看状态

    txpool.status
  4. 查看余额

web3.fromWei(eth.getBalance("0xea23482cbc16468d67e966757c10f367adcb5329"),'ether')

8. 安全问题

  1. 创世块中配置只允许特定的地址挖矿,并给予某些地址初始金额的ETH
  2. 极端情况下,通过限制ip、端口的方式只允许特定的服务器连接节点。

1 thought on “使用Geth搭建Ethereum以太坊私链”

Leave a Comment

豫ICP备19001387号-1