背景介绍
以太坊开发必须需要节点,虽然已经拥有了测试网的节点,但是我们还是最好搭建一个我们自己的本地节点,对于底层开发就更需要节点了,所以我们最好还是搭建本地节点。
环境需求
节点配置文件
以太坊私有链启动必须需要创世块文件配置,这个创世块配置文件是一个json文件。以下是一个创世块配置文件模板:
{
"config": {
"chainId": 1337,
"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": "0x00002",
"mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"coinbase": "0x0000000000000000000000000000000000000000",
"alloc": {
"0x0192a05d8B681fB99AC07e0D58C69DCAf99df0ea":{
"balance":"0xffffffffffffffff"
}
},
"number": "0x0",
"gasUsed": "0x0",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000"
}
参数 | 说明 |
---|
coinbase | 矿工地址 | alloc | 用于预设创世账户及其拥有的以太币数量,此处也可以预设合约账户 |
编译geth可执行文件
$git clone git@github.com:ethereum/go-ethereum.git
$cd ethereum
$git checkout v1.10.o
$make geth
$sudo mv ./build/bin/geth /usr/local/bin
编写管理本地私有节点脚本
#!/bin/bash
MinerAddress='0x1140dC752FF55453086564557c839cd5BBa3178C'
function print() {
echo "Usage: "
echo " run.sh <mode>"
echo " <mode> - one of 'init', 'start', 'console', 'stop'"
echo " - 'init' - init genesis block"
echo " - 'start' - start ethereum nede"
echo " - 'console' - enter console"
echo " - 'stop' - stop ethereum node"
}
MODE=$1
if [ "${MODE}" == "init" ]; then
cd deploy
geth --datadir ./data init genesis.json
elif [ "${MODE}" == "start" ]; then
nohup geth --rpc --rpcapi "eth,web3,net,ssh,db,debug,personal,miner" \
--rpcaddr 192.168.235.128 --rpcport 8545 --allow-insecure-unlock --rpccorsdomain "*" \
--dev --dev.period 1 --datadir ./data --port 30303 \
--password password --networkid 100000 \
--miner.etherbase $MinerAddress --mine \
--miner.threads=1 2>&1 &
echo
elif [ "${MODE}" == "console" ]; then
geth attach http://192.168.235.128:8545
elif [ "${MODE}" == "stop" ]; then
GETHPID=`ps -ef | grep geth | grep -v grep | awk '{print $2}'`
kill -9 $GETHPID
rm -rf nohup.out
else
print
exit 1
fi
- MinerAddress是挖矿地址
- 脚本文件中的ip地址是虚拟机的ip,为了能够让本地Windows浏览器里的MetaMask钱包能够连上区块链节点
节点管理
初始化创世区块
./run.sh init
启动节点
./run.sh start
进入控制台
./run.sh console
关闭节点
./run.sh stop
源码仓库
1、https://github.com/billchen-818/eth-dev
|