在做一些测试工作的时候, 为了方便控制以及更快的进入真正的测试工作,可能需要搭建一个私有的以太坊网络.
而以太坊节点之间能够互相链接需要满足1)相同的协议版本2)相同的networkid,所以搭建私有网络最方便的方法就是通过geth命令中的--networkid选项,设置一个与主网不同的networkid(主网的networkid为1),这也是官方推荐的方法.
下面开始建立私有以太坊网络:
1
2
|
mkdir
private-geth
cd
private-geth
|
建立创世纪区块文件,是一个json格式的文件:
1
|
vim
genesis.json
|
在创世纪区块的json文件中填入以下内容, 并保存.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
{
"config" :
{
"chainId" :
15,
"homesteadBlock" :
0,
"eip155Block" :
0,
"eip158Block" :
0
},
"coinbase"
: "0x0000000000000000000000000000000000000000" ,
"difficulty"
: "0x40000" ,
"extraData"
: "" ,
"gasLimit"
: "0xffffffff" ,
"nonce"
: "0x0000000000000042" ,
"mixhash"
: "0x0000000000000000000000000000000000000000000000000000000000000000" ,
"parentHash"
: "0x0000000000000000000000000000000000000000000000000000000000000000" ,
"timestamp"
: "0x00" ,
"alloc" :
{ }
}
|
初始化创世纪节点,并设置data目录:
1
|
geth
--datadir . /data/00
init genesis.json
|
启动节点, 加上console 表示启动后,启用命令行:
1
|
geth
--datadir . /data/00
--networkid 15 console
|
现在私有网络就搭建成功,下面就可以在这个刚刚搭建出来的私有以太坊网络中执行挖矿操作了,
挖矿首先必须有一个账户,输入下面的命令,查看当前node中的所有账户:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
>
eth.accounts
[]
###输出了一个"[]",说明毛都么有一根,更别说账户了.既然没有,那就来创建一个,继续输入命令:
>
personal.newAccount( "123456" )
###正常情况,命令行会输出:
INFO
[05-15|23:59:29] New wallet appeared url=keystore: ///home/zl/Documents/p …
status=Locked
"0x5b901c2495c077f515ae84fc53206f2eb3bf8296"
###再用之前查看账户的命令看看:
>
eth.accounts
[ "0x5b901c2495c077f515ae84fc53206f2eb3bf8296" ]
###有输出了"0x5b.......96"这一串就是账户的地址.
###账户有了,接下来做点更有意思的事,开始挖矿....继续在命令行书输出:
>
miner.start()
INFO
[05-16|00:07:25] Updated mining threads threads=0
INFO
[05-16|00:07:25] Starting mining operation
null
INFO
[05-16|00:07:25] Commit new mining work number=1 txs=0 uncles=0 elapsed=38.053ms
INFO
[05-16|00:07:28] Generating DAG in
progress epoch=0 percentage=0 elapsed=1.715
INFO
[05-16|00:07:30] Generating DAG in
progress epoch=0 percentage=1 elapsed=3.448s
INFO
[05-16|00:07:31] Generating DAG in
progress epoch=0 percentage=2 elapsed=5.059s
INFO
[05-16|00:07:33] Generating DAG in
progress epoch=0 percentage=3 elapsed=6.799s
INFO
[05-16|00:07:35] Generating DAG in
progress epoch=0 percentage=4 elapsed=8.373s
...................................这个percenage=100,也就是100%以后.,就不断的出现下面的提示.............
INFO
[05-16|00:24:54] Successfully sealed new block number=36 hash =95fdfd…1411ee
INFO
[05-16|00:24:54] block reached canonical chain number=31 hash =438022…f6592e
.................................好多这个提示,挖到好多矿................................
INFO
[05-16|00:24:54] mined potential block number=36 hash =95fdfd…1411ee
INFO
[05-16|00:24:54] Commit new mining work number=37 txs=0 uncles=0 elapsed=122.202µs
###然后停止挖矿:
>miner.stop(
true
###然后再来看看我们账户钱包有多少矿了:
>
eth.getBalance(eth.accounts[0])
355000000000000000000
|
下一篇,我会讲一讲如何搭建 私有网络节点群:
Setting up private network or local cluster
参考文档:
http://ethdocs.org/en/latest/network/test-networks.html?highlight=private%20chain
https://github.com/ethereum/go-ethereum/wiki/Private-network
https://github.com/ethereum/go-ethereum/wiki/Setting-up-private-network-or-local-cluster
http://chainskills.com/2017/03/10/part-3-setup-the-private-chain-miners/