目录
数字货币
云计算
编程
网络
其他
架构设计
密码学
搭建以太坊私有网络
1. 编译ethereum源代码
安装g1.8版本到/opt/go,并且设置go运行环境变量,在go工作目录${HOME}/go执行:
export GOROOT=/opt/go
export PATH=$PATH:$GOROOT/bin
export GOPATH=$PWD
将go-ethereum源代码拷贝到${HOME}/go目录下,进入${HOME}/go/go-ethereum/目录下执行:
make
生成的geth可执行文件在build/bin/geth
2. 编写初始块数据
genesis.json文件内容如下:
{
"config": {
"chainId": 10,
"homesteadBlock": 0,
"eip155Block": 0,
"eip158Block": 0
},
"nonce":"0x0000000000000042",
"mixhash":"0x0000000000000000000000000000000000000000000000000000000000000000",
"difficulty": "0x1",
"alloc": {
},
"coinbase":"0x0000000000000000000000000000000000000000",
"timestamp": "0x00",
"parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000",
"extraData": "GenesisBlock",
"gasLimit":"0xffffffff"
}
3. 初始化
编写初始化脚本geth_init.sh
#!/bin/bash
BASE_PATH=$(cd `dirname $0`; pwd)
geth --datadir "$BASE_PATH/chain" init genesis.json
#run geth in testnet
#geth --identity "ethereum-002" --rpc --rpcaddr "192.168.0.117" --rpcport "8080" --rpccorsdomain "*" --datadir "$BASE_PATH/chain" --port "30303" --rpcapi "db,eth,net,web3" --testnet --networkid 2288 init genesis.json
4. 启动
编写启动脚本geth_start.sh
#!/bin/bash
BASE_PATH=$(cd `dirname $0`; pwd)
#geth --datadir "$BASE_PATH/chain" init genesis.json
geth --identity "ethereum" --rpc --rpccorsdomain "*" --datadir "$BASE_PATH/chain" --port "30303" --rpcapi "db,eth,net,web3" --networkid 2088 console
#run geth in testnet
#geth --identity "ethereum-002" --rpc --rpcaddr "192.168.0.117" --rpcport "8080" --rpccorsdomain "*" --datadir "$BASE_PATH/chain" --port "30303" --rpcapi "db,eth,net,web3" --testnet --networkid 2288 init genesis.json
#geth --identity "ethereum-002" --rpc --rpcaddr "192.168.0.178" --rpcport "8080" --rpccorsdomain "*" --datadir "$BASE_PATH/chain" --port "30303" --rpcapi "db,eth,net,web3" --testnet --networkid 2288 console
5. 使用源代码调试ethereum
在编译go-ethereum后,build目录下会多出一个目录_workspace,该目录下建立go语言工作环境,按照如下编写脚本启动go:
#!/bin/bash
BASE_PATH=$(cd `dirname $0`; pwd)
export GOPATH="${HOME}/go/go-ethereum-1.6.0/build/_workspace"
GETH_PATH=${GOPATH}/src/github.com/ethereum/go-ethereum/cmd/geth
GETH=${GETH_PATH}/*[^_test].go
chmod +x ${GETH}
go run ${GETH} --datadir "$BASE_PATH/chain" init genesis.json
go run ${GETH} --identity "ethereum" --rpc --rpccorsdomain "*" --datadir "$BASE_PATH/chain" --port "30303" --rpcapi "db,eth,net,web3" --networkid 2088 console
在genesis.json同级目录下执行该脚本,可达到与执行文件同样的效果。
6. 编译后使用gdb调试以太坊
在go-ethereum代码中嵌入调试断点,使用gdb启动调试,插入代码断点:
import "runtime"
runtime.Breakpoint()
gdb启动的shell脚步内容如下:
#!/bin/bash
BASE_PATH=$(cd `dirname $0`; pwd)
source /opt/go/env.sh
ETHEREUM_ROOT=${HOME}/gocode/go-ethereum-1.6.7
GETH=${ETHEREUM_ROOT}/build/bin/geth
cd ${ETHEREUM_ROOT}
make
cd ${BASE_PATH}
#gdb --args ${GETH} --datadir "$BASE_PATH/chain" init genesis.json
gdb --args ${GETH} --debug --identity "etherum" --rpc --rpccorsdomain "*" --datadir "$BASE_PATH/chain" --port "30303" --rpcapi "db,eth,net,web3" --networkid 2088 console