本文介绍基于macbook pro环境、 go-ethereum搭建以太坊ETH私有链环境
一、安装编译geth(以太坊ETH客户端工具)
1、基于源代码本地调试编译生成geth
(1)下载源码 :go-ethereum源码下载地址
~/go/src/github.com/ethereum/go-ethereum$ls
AUTHORS cmd ethdb node
COPYING common ethstats p2p
COPYING.LESSER consensus event params
Dockerfile console interfaces.go rlp
Dockerfile.alltools containers internal rpc
Makefile contracts les signer
README.md core light swarm
accounts crypto log tests
appveyor.yml dashboard metrics trie
build eth miner vendor
circle.yml ethclient mobile whisper
~/go/src/github.com/ethereum/go-ethereum$m
(2)编译源码:make geth (只编译geth)
~/go/src/github.com/ethereum/go-ethereum$make geth
build/env.sh go run build/ci.go install ./cmd/geth
>>> /usr/local/Cellar/go/1.10.3/libexec/bin/go install -ldflags -X main.gitCommit=11d0ff6578c34b724436dbeeede726b31b41c8b8 -s -v ./cmd/geth
Done building.
Run "/Users/wujinquan/go/src/github.com/ethereum/go-ethereum/build/bin/geth" to launch geth.
~/go/src/github.com/ethereum/go-ethereum$
(3)生成geth
经过(2)后生成geth位于 ethereum/go-ethereum/build/bin/路径下
2、可通过安装形式安装geth (相当安装软件)
此处不做冗述
二、建立私有以太坊网络
1、创建私有目录 private_chain
~/eth_workspace$ls
~/eth_workspace$mkdir private_chain/
~/eth_workspace$ls
private_chain
~/eth_workspace$cd private_chain/
~/eth_workspace/private_chain$
2、准备创世块文件
~/eth_workspace/private_chain$vim genesis.json
{
"config": {
"chainId": 10,
"homesteadBlock": 0,
"eip155Block": 0,
"eip158Block": 0
},
"alloc" : {},
"coinbase" : "0x0000000000000000000000000000000000000000",
"difficulty" : "0x20000",
"extraData" : "",
"gasLimit" : "0x2fefd8",
"nonce" : "0x0000000000000042",
"mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"timestamp" : "0x00"
}
每个参数含义:
参数名称 | 参数描述 |
---|---|
chainId | 链类型ID,私链:10,testnet: ,mainnet: |
alloc | 预置账号以及对应以太币数量,因为私有链挖矿容易,故不需要预置有币的账号,需要的时候自己创建即可以 |
coinbase | 矿工的账号,随便填 |
difficulty | 设置当前区块的难度,如果难度过大,cpu挖矿就很难,这里设置较小难度 |
extraData | 附加信息,随便填,可以填你的个性信息 |
gasLimit | 该值设置对GAS的消耗总量限制,用来限制区块能包含的交易信息总和,因为我们是私有链,所以填最大 |
nonce | nonce nonce就是一个64位随机数,用于挖矿,注意他和mixhash的设置需要满足以太坊的Yellow paper, 4.3.4. Block Header Validity, (44)章节所描述的条件 |
mixhash | 与nonce配合用于挖矿,由上一个区块的一部分生成的hash。注意他和nonce的设置需要满足以太坊的Yellow paper, 4.3.4. Block Header Validity, (44)章节所描述的条件。 |
parentHash | 上一个区块的hash值(父hash),因为是创世块,所以这个值是0 |
timestamp | 设置创世块的时间戳 |
3、初始化创世区块
命令:geth init ./genesis.json --datadir ./data/
~/eth_workspace/private_chain$./../../go/src/github.com/ethereum/go-ethereum/build/bin/geth init ./genesis.json --datadir ./data/
INFO [11-08|17:22:05.173] Maximum peer count ETH=25 LES=0 total=25
keydir=/Users/wujinquan/eth_workspace/private_chain/data/keystore
INFO [11-08|17:22:05.185] Allocated cache and file handles database=/Users/wujinquan/eth_workspace/private_chain/data/geth/chaindata cache=16 handles=16
INFO [11-08|17:22:05.188] Writing custom genesis block
INFO [11-08|17:22:05.189] Persisted trie from memory database nodes=0 size=0.00B time=13.017µs gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [11-08|17:22:05.189] Successfully wrote genesis state database=chaindata hash=5e1fc7…d790e0
INFO [11-08|17:22:05.189] Allocated cache and file handles database=/Users/wujinquan/eth_workspace/private_chain/data/geth/lightchaindata cache=16 handles=16
INFO [11-08|17:22:05.192] Writing custom genesis block
INFO [11-08|17:22:05.192] Persisted trie from memory database nodes=0 size=0.00B time=4.084µs gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [11-08|17:22:05.192] Successfully wrote genesis state database=lightchaindata hash=5e1fc7…d790e0
~/eth_workspace/private_chain$
初始化之后自动生成data文件夹
~/eth_workspace/private_chain$tree
.
├── data
│ ├── geth
│ │ ├── chaindata
│ │ │ ├── 000001.log
│ │ │ ├── CURRENT
│ │ │ ├── LOCK
│ │ │ ├── LOG
│ │ │ └── MANIFEST-000000
│ │ └── lightchaindata
│ │ ├── 000001.log
│ │ ├── CURRENT
│ │ ├── LOCK
│ │ ├── LOG
│ │ └── MANIFEST-000000
│ └── keystore
└── genesis.json
5 directories, 11 files
~/eth_workspace/private_chain$
4、启动私有链
命令:--rpc 启动rpc服务器,具体参数用 geth -h查看
geth --datadir "/Users/wujinquan/eth_workspace/private_chain/data" --networkid 314590 --rpc console 2>> ./debug.log
~/eth_workspace/private_chain$./../../go/src/github.com/ethereum/go-ethereum/build/bin/geth --datadir "/Users/wujinquan/eth_workspace/private_chain/data" --networkid 314590 --rpc console 2>> ./debug.log
keydir=/Users/wujinquan/eth_workspace/private_chain/data/keystore
Welcome to the Geth JavaScript console!
instance: Geth/v1.8.17-unstable-11d0ff65/darwin-amd64/go1.10.3
modules: admin:1.0 debug:1.0 eth:1.0 ethash:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0
>
查看log日志
~/eth_workspace/private_chain$rm debug.log
~/eth_workspace/private_chain$cat debug.log
INFO [11-08|17:40:20.112] Maximum peer count ETH=25 LES=0 total=25
INFO [11-08|17:40:20.138] Starting peer-to-peer node instance=Geth/v1.8.17-unstable-11d0ff65/darwin-amd64/go1.10.3
INFO [11-08|17:40:20.138] Allocated cache and file handles database=/Users/wujinquan/eth_workspace/private_chain/data/geth/chaindata cache=768 handles=1024
INFO [11-08|17:40:20.155] Initialised chain configuration config="{ChainID: 10 Homestead: 0 DAO: <nil> DAOSupport: false EIP150: <nil> EIP155: 0 EIP158: 0 Byzantium: <nil> Constantinople: <nil> Engine: unknown}"
INFO [11-08|17:40:20.155] Disk storage enabled for ethash caches dir=/Users/wujinquan/eth_workspace/private_chain/data/geth/ethash count=3
INFO [11-08|17:40:20.156] Disk storage enabled for ethash DAGs dir=/Users/wujinquan/.ethash count=2
INFO [11-08|17:40:20.156] Initialising Ethereum protocol versions="[63 62]" network=314590
INFO [11-08|17:40:20.156] Loaded most recent local header number=0 hash=5e1fc7…d790e0 td=131072 age=49y6mo3w
INFO [11-08|17:40:20.156] Loaded most recent local full block number=0 hash=5e1fc7…d790e0 td=131072 age=49y6mo3w
INFO [11-08|17:40:20.156] Loaded most recent local fast block number=0 hash=5e1fc7…d790e0 td=131072 age=49y6mo3w
INFO [11-08|17:40:20.157] Loaded local transaction journal transactions=0 dropped=0
INFO [11-08|17:40:20.157] Regenerated local transaction journal transactions=0 accounts=0
INFO [11-08|17:40:20.157] Starting P2P networking
INFO [11-08|17:40:22.281] UDP listener up self=enode://d73c11c71cc49ec1f435df321fe2616c35203154ad37debb1287c0a3657e233312d6dafe59669cb75ff7b83ed5d587a2b0e2124607fe02d1a27025b0844d64c2@[::]:30303
INFO [11-08|17:40:22.281] RLPx listener up self=enode://d73c11c71cc49ec1f435df321fe2616c35203154ad37debb1287c0a3657e233312d6dafe59669cb75ff7b83ed5d587a2b0e2124607fe02d1a27025b0844d64c2@[::]:30303
INFO [11-08|17:40:22.283] IPC endpoint opened url=/Users/wujinquan/eth_workspace/private_chain/data/geth.ipc
INFO [11-08|17:40:22.283] HTTP endpoint opened url=http://127.0.0.1:8545 cors= vhosts=localhost
~/eth_workspace/private_chain$
三、启动挖矿
现在私有网络就搭建成功,下面就可以在这个刚刚搭建出来的私有以太坊网络中执行挖矿操作了,
挖矿首先必须有一个账户,输入下面的命令,查看当前节点中的所有账户:
> eth.accounts
[]
#输出了一个"[]",说明毛都么有一根,更别说账户了.既然没有,那就来创建一个,继续输入命令:
> personal.newAccount("12345678")
"0xa1430e46d5da67f60f5b0c2bb817a6ac5a2f31d6" //此处成功创建一个地址,默认第一个地址为coinbase账户
> eth.accounts
["0xa1430e46d5da67f60f5b0c2bb817a6ac5a2f31d6"]
> miner.start()
null
> eth.blockNumber
109
> miner.stop()
null
> eth.getBalance("0xa1430e46d5da67f60f5b0c2bb817a6ac5a2f31d6") //查询账户余额
640000000000000000000
> eth.coinbase //查询coinbase账户,
"0xa1430e46d5da67f60f5b0c2bb817a6ac5a2f31d6"
查看log,可看到区块hash,区块高度等信息
NFO [11-08|17:48:09.409] Updated mining threads threads=8
INFO [11-08|17:48:09.409] Transaction pool price threshold updated price=1000000000
INFO [11-08|17:48:09.409] Etherbase automatically configured address=0xa1430e46D5da67F60f5B0c2bb817A6aC5A2F31d6
INFO [11-08|17:48:09.409] Commit new mining work number=1 sealhash=f3fa96…83a0f8 uncles=0 txs=0 gas=0 fees=0 elapsed=157.722µs
INFO [11-08|17:48:10.752] Generating DAG in progress epoch=0 percentage=0 elapsed=664.794ms
INFO [11-08|17:48:11.636] Generating DAG in progress epoch=0 percentage=1 elapsed=1.548s
INFO [11-08|17:48:12.357] Generating DAG in progress epoch=0 percentage=2 elapsed=2.269s
...................................这个percenage=100,也就是100%以后.,就不断的出现下面的提示.............
INFO [11-08|17:49:27.143] Generating DAG in progress epoch=0 percentage=96 elapsed=1m17.055s
INFO [11-08|17:49:27.900] Generating DAG in progress epoch=0 percentage=97 elapsed=1m17.812s
INFO [11-08|17:49:28.660] Generating DAG in progress epoch=0 percentage=98 elapsed=1m18.572s
INFO [11-08|17:49:29.643] Generating DAG in progress epoch=0 percentage=99 elapsed=1m19.555s
INFO [11-08|17:49:29.646] Generated ethash verification cache epoch=0 elapsed=1m19.558s
INFO [11-08|17:49:30.592] Successfully sealed new block number=1 sealhash=f3fa96…83a0f8 hash=105a28…f84f4a elapsed=1m21.182s
INFO [11-08|17:49:30.592] 🔨 mined potential block number=1 hash=105a28…f84f4a
INFO [11-08|17:49:30.592] Commit new mining work number=2 sealhash=aa8877…008b35 uncles=0 txs=0 gas=0 fees=0 elapsed=120.273µs
INFO [11-08|17:49:30.946] Successfully sealed new block number=2 sealhash=aa8877…008b35 hash=86d9ab…7204d1 elapsed=354.259ms
INFO [11-08|17:49:30.946] 🔨 mined potential block number=2 hash=86d9ab…7204d1
INFO [11-08|17:49:30.947] Commit new mining work number=3 sealhash=03689c…72e9ad uncles=0 txs=0 gas=0 fees=0 elapsed=126.99µs
INFO [11-08|17:49:30.969] Successfully sealed new block number=3 sealhash=03689c…72e9ad hash=1a23c4…2caa7e elapsed=22.541ms
INFO [11-08|17:49:30.969] 🔨 mined potential block number=3 hash=1a23c4…2caa7e
INFO [11-08|17:49:30.969] Mining too far in the future wait=2s
INFO [11-08|17:49:32.073] Generating DAG in progress epoch=1 percentage=0 elapsed=763.140ms
INFO [11-08|17:49:32.962] Generating DAG in progress epoch=1 percentage=1 elapsed=1.652s
INFO [11-08|17:49:32.977] Commit new mining work number=4 sealhash=3c207e…34ffe9 uncles=0 txs=0 gas=0 fees=0 elapsed=2.007s
INFO [11-08|17:49:33.833] Successfully sealed new block number=4 sealhash=3c207e…34ffe9 hash=453e88…0ad8c5 elapsed=856.617ms
INFO [11-08|17:49:33.833] 🔨 mined potential block number=4 hash=453e88…0ad8c5
INFO [11-08|17:49:33.845] Commit new mining work number=5 sealhash=d9c064…4c004c uncles=0 txs=0 gas=0 fees=0 elapsed=194.536µs
INFO [11-08|17:49:34.581] Generating DAG in progress epoch=1 percentage=2 elapsed=3.271s
.................................好多这个提示,挖到好多矿................................
INFO [11-08|17:51:53.036] Successfully sealed new block number=127 sealhash=928a99…83b9d5 hash=32b091…91aa9f elapsed=608.969ms
INFO [11-08|17:51:53.037] 🔗 block reached canonical chain number=120 hash=28e4b8…8fdff1
INFO [11-08|17:51:53.037] 🔨 mined potential block number=127 hash=32b091…91aa9f
INFO [11-08|17:51:53.037] Commit new mining work number=128 sealhash=6ad711…aa321b uncles=0 txs=0 gas=0 fees=0 elapsed=126.24µs
INFO [11-08|17:51:53.634] Successfully sealed new block number=128 sealhash=6ad711…aa321b hash=503f6e…8db68c elapsed=597.437ms
INFO [11-08|17:51:53.634] 🔗 block reached canonical chain number=121 hash=c1f25b…f11d46
INFO [11-08|17:51:53.634] 🔨 mined potential block number=128 hash=503f6e…8db68c
四、至此以太坊的私有节点搭建完成,继续讲解用Postman于ETH的RPC接口进行通信
(1) 以太坊rpc接口文档、扩展接口
(2) 用postman工具查询rpc常用的方法
在启动私有节点时我们已经知道rpc端口为默认的8545
五、总结
以太坊私链搭建难度总体不大,需要有一定的软件相关经验。截止目前区块链研究人员已经不少,过程中遇到问题稍加搜索即可解决
网友评论