环境:
环境准备
node1 搭建
- 关闭防火墙,确保后续节点之间能相互通讯,数据同步。
su root
关闭防火墙
systemctl disable firewalld
修改/etc/selinux/config 文件
将SELINUX=enforcing改为SELINUX=disabled
reboot
- 安装git
yum install git
- 安装golang
wget https://golang.org/dl/go1.15.linux-amd64.tar.gz
tar -zxvf go1.15.linux-amd64.tar.gz
mv go /usr/local/
- 设置go环境变量
vi /etc/profile
export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin
source /etc/profile
- 安装node
curl -sL https://rpm.nodesource.com/setup_10.x | sudo bash -
yum install nodejs
编译以太坊源码
- 下载以太坊源码(go)
wget https://github.com/ethereum/go-ethereum/archive/refs/tags/v1.9.24.tar.gz
解压
tar -zxvf v1.9.24.tar.gz
- 编译以太坊
cd go-ethereum-1.9.24
make all
设置环境变量
echo 'export PATH=$PATH:/root/go-ethereum-1.9.24/build/bin' >> /etc/profile
source /etc/profile
创建初始化创世区块的文件
cd ~
vi genesis.json
输入以下配置
{
"config": {
"chainId": 1234,
"homesteadBlock": 0,
"eip150Block": 0,
"eip150Hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"eip155Block": 0,
"eip158Block": 0,
"byzantiumBlock": 0,
"constantinopleBlock": 0,
"petersburgBlock": 0,
"istanbulBlock": 0,
"ethash": {}
},
"nonce": "0x0",
"timestamp": "0x5ddf8f3e",
"extraData": "0x0000000000000000000000000000000000000000000000000000000000000000",
"gasLimit": "0x47b760",
"difficulty": "0x400",
"mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"coinbase": "0x0000000000000000000000000000000000000000",
"alloc": {
"此处填需要初始eth的钱包地址" : {"balance" : "此处为你想初始多少wei的eth 余额给前面的地址"}
},
"number": "0x0",
"gasUsed": "0x0",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000"
}
退出并保存文件
初始化
geth --datadir "/root/ethereumData" init /root/genesis.json
创世文件解释
chainID 该链的ID。在用geth 启动区块链时,还需要指定一个network 参数。只有当network、chainID、创世区块配置都相同时,才是同一条链。
alloc 代表初始资产配置,在该区块链产生时,就预先赋予这些账户一定数额的WEI(不是ETH)。
nonce 预定一个随机数,这是一个与PoW 机制有关的值。
difficulty 定义了每次挖矿时,最终确定nonce 的难度,私链建议难度低一点,否则需要gpu才能挖矿。
mixhash 一个与PoW 机制有关的值。
coinbase 每挖出一个区块,都会获得奖励。该值指定默认情况下把奖励给到哪个账户。实际上,我们每次挖矿开始之前,都会自己指定miner.setEtherbase(UserAddress),一般都会把奖励给自己timestamp 时间戳,规定创世区块开始的时间。
parentHash 在区块链中,区块是相连的,parentHash 指定了本区块的上一个区块Hash。对于创世区块来说,parentHash 为0。
extraData 在Clique 机制下,新区块只能被签名人(singers)挖掘,区块链生长过程中,可以通过投票来选举或者免除签名人。在区块链开始运行时,需要定义一个初始singer。
gasLimit 规定该区块链中,gas 的上限,私链建议多一点,可以在交易中打包更大的数据。
- 启动geth
geth --networkid 1234 --nodiscover --datadir /root/ethereumData --rpccorsdomain "*" --rpc --rpcapi "admin,eth,debug,miner,net,txpool,personal,web3" --rpcaddr 0.0.0.0 --rpcport 8545 --port 30001 console 2 --dev.period 1 --allow-insecure-unlock
--nodiscover 关闭节点发现。
--allow-insecure-unlock 启用账户解锁,不开启账户将无法解锁进行转账。
创建账号(参与挖矿)
personal.newAccount('password')
账号可以多创建几个,第一个默认为挖矿账户。
获取节点信息
geth 启动成功后会输出节点信息,也可以通过admin.nodeInfo 查看,内容如下:
enode://73f68dbed3206fa341919a057cd583cdceeaee9855de31bebc7c9cd347c99a30ec58ccaf882b559645e6690da3b4581258d9ce40b3e37088c99de2d851b6c84c@127.0.0.1:30001?discport=0
把127.0.0.1改为节点一IP,并记录节点信息。
开启挖矿
miner.start()
搭建以太坊区块链浏览器
- 下载编译 explorer
cd ~
git clone https://github.com/etherparty/explorer
cd explorer
用 npm 安装 bower
npm install -g bower -y
初始化 bower
bower init
bower 安装
bower install --allow-root
bower install angular --save-dev --allow-root
修改配置并启动浏览器
修改配置,让浏览器其他机器可以访问
vi app/app.js
修改 var eth_node_url = 'http://localhost:8545'; // TODO: remote URL 为
var eth_node_url = 'http://节点一IP:8545'; // TODO: remote URL
vi package.json
修改 "start": "http-server ./app -a localhost -p 8000 -c-1", 为
"start": "http-server ./app -a 节点一IP -p 8000 -c-1",
- 启动浏览器
npm start
访问浏览器
http://节点一IP:8000
-
GETH 常用命令
查询账户余额,单位WEI
eth.getBalance("address1")
解锁账户(300秒),只有解锁后才能转账到其它账户,
web3.personal.unlockAccount("address1")
输入密码
转账ETH到其他账户,单位WEI
eth.sendTransaction({from: "address1",to: "address2", value: "10000"})
节点二同步
node2重复node1的步骤,在成功启动geth后可以通过以下命令加入节点一,形成集群。
admin.addPeer("enode://73f68dbed3206fa341919a057cd583cdceeaee9855de31bebc7c9cd347c99a30ec58ccaf882b559645e6690da3b4581258d9ce40b3e37088c99de2d851b6c84c@nnode1IP:3301?discport=0")
此节点信息为node1 geth启动后输出,也可以在node1 geth命令行中通过admin.nodeInfo 查看
最终可以在node1和node2 geth命令行中 通过eth 命令查看两边数据同步状态。
查看节点信息命令:
admin.peers()
当加入节点信息成功后就能在node2中看到node1的节点信息,node1中也能看到node2的节点信息。
开启node2 挖矿
miner.start()
至此,以太坊私链的搭建就已经完成。