Centos7 搭建以太坊私链
阿里云,centos7
Geth环境安装
目录说明
/root/opt/install-package: 存放上传/下载的安装包
/root/opt/nodejs: node.js安装位置
/root/opt/cmake: cmake安装位置
/root/opt/go: golang安装位置
/root/opt/go-ethereum: geth安装位置
/root/opt/myChain: 私链存放位置
安装golang
下载
cd /root/opt/install-package
wget https://studygolang.com/dl/golang/go1.16.3.linux-amd64.tar.gz
tar -zxvf go1.16.3.linux-amd64.tar.gz -C /root/opt
配置环境变量
vim /etc/profile
export GO_HOME=/root/opt/go
export PATH=$PATH:$GO_HOME/bin
source /etc/profile
配置Golang环境变量
go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.io,direct
安装node.js
web3.js 依赖node.js
下载
cd /root/opt/install-package
wget https://nodejs.org/dist/v10.16.3/node-v10.16.3-linux-x64.tar.xz
xz -d node-v10.16.3-linux-x64.tar.xz
tar xvf node-v10.16.3-linux-x64.tar -C /root/opt
cd /root/opt
mv node-v10.16.3-linux-x64/ nodejs
配置环境变量
vim /etc/profile
export NODE_HOME=/root/opt/nodejs
export PATH=$PATH:$NODE_HOME/bin
source /etc/profile
node -v
npm -v
npm 换源
npm config ls
npm config set registry https://registry.npm.taobao.org/
npm config set registry http://www.npmjs.org
安装 gcc-c++
下载
yum install gcc-c++
安装cmake
下载
cd /root/opt/install-package
wget https://cmake.org/files/v3.15/cmake-3.15.2.tar.gz
tar -zxvf cmake-3.15.2.tar.gz -C /root/opt
cd /root/opt
mv cmake-3.15.2/ cmake
安装
cd /root/opt/cmake
./bootstrap
gmake
gmake install
cmake --versionS
安装NTP
yum install ntp
systemctl enable ntpd
systemctl start ntpd
安装Geth
下载官网:https://geth.ethereum.org/downloads/
下载
mkdir -p /root/opt/go-ethereum/
tar zxvf geth-linux-amd64-1.10.8-26675454.tar.gz -C /root/opt/go-ethereum/
cd /root/opt/go-ethereum/
mv geth-v1_10_8 bin
配置环境变量
vim /etc/profile
export GETH_HOME=/root/opt/go-ethereum/
export PATH=$PATH:$GETH_HOME/bin
source /etc/profile
geth version
搭建私链
POA 私链
数据目录创建
mkdir -p /root/opt/myChain/poa
mkdir /root/opt/myChain/poa/logs
touch /root/opt/myChain/poa/logs/outputs.log
创建私链并启动
非rpc
geth --datadir /root/opt/myChain/poa --dev console 2>/root/opt/myChain/poa/logs/outputs.log
rpc
# 端口默认8545
geth --datadir /root/opt/myChain/poa --networkid 15 --rpc --rpcaddr 0.0.0.0 --rpcport 8545 --rpcapi "db,eth,net,web3" --dev --allow-insecure-unlock console 2>/root/opt/myChain/poa/logs/outputs.log
# --dev 没有交易不会挖矿
# --dev.period 自动挖矿
# --dev.period value 开发者模式下挖矿周期 (0 = 有pending状态交易时进行挖矿) (默认: 0)
# --dev.period 1 --mine
查看日志(再开一个窗口)
tail -f /root/opt/myChain/poa/logs/outputs.log
查看余额
eth.getBalance(eth.accounts[0])
MetaMask远程连接
转账测试
eth.sendTransaction({from:eth.accounts[0], to:"0xd67c9e6183B58E256908C0627d170CF036C2A31A", value:web3.toWei(10, 'ether')})
POW私链
创建目录
mkdir -p /root/opt/myChain/pow
mkdir /root/opt/myChain/pow/logs
touch /root/opt/myChain/pow/logs/outputs.log
创建创世信息文件
vi /root/opt/myChain/pow/genesis.json
{
"config": {
"chainId": 15,
"homesteadBlock": 0,
"eip150Block": 0,
"eip155Block": 0,
"eip158Block": 0,
"byzantiumBlock": 0,
"constantinopleBlock": 0,
"petersburgBlock": 0,
"ethash": {}
},
"difficulty": "2000",
"gasLimit": "2100000",
"alloc": {
"d67c9e6183B58E256908C0627d170CF036C2A31A":{"balance":"30000000000000000000"},
"9FD09364145Ef2D6b4fa8e08c6a53EE62aC2BBa9":{"balance":"30000000000000000000"}
}
}
networkid: 区块链id, 不要与主网,测试网相冲
alloc: 指定的账户
初始化
geth --datadir /root/opt/myChain/pow init /root/opt/myChain/pow/genesis.json
启动
非rpc
geth --datadir /root/opt/myChain/pow console 2>/root/opt/myChain/pow1/outputs.log
htpp启动
geth --networkid 15 --datadir /root/opt/myChain/pow --http --http.port 8545 --http.addr "0.0.0.0" --port 30303 --http.corsdomain "*" --http.api "eth,net,web3,personal,miner" console 2>/root/opt/myChain/pow/logs/outputs.log
rpc启动
geth --datadir /root/opt/myChain/pow --networkid 15 --rpc --rpcaddr 0.0.0.0 --rpcport 8545 --rpcapi "db,eth,net,web3" --allow-insecure-unlock console 2>/root/opt/myChain/pow/logs/outputs.log
日志查看
tail -f /root/opt/myChain/pow/logs/outputs.log
rpc 测试-postman
http://ip:8545
{
jsonrpc: "1.0",
method: "eth_getLuck",
params: [],
id: 15
}
查看测试:
eth.getBalance("d67c9e6183B58E256908C0627d170CF036C2A31A")
eth.getBalance("9FD09364145Ef2D6b4fa8e08c6a53EE62aC2BBa9")
|