上篇介绍了以太坊开发环境,本篇介绍下如何使用私有链。

使用私有链之前,先要创建创世块genesis。

初始化私有链

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
"nonce": "0x0000000000000042",
"timestamp": "0x0",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"extraData": "0x",
"gasLimit": "0x80000000",
"difficulty": "0x3",
"coinbase": "0x3333333333333333333333333333333333333333",
"config":{
"chainId": 55,
"homesteadBlock": 0,
"eip155Block": 0
},
"alloc": {}
}

将上述内容写入文件genesis.json中,字段的解释如下:

nonce:64位随机数,用于挖矿
timestamp:创世块的时间戳
parentHash:上一个区块的hash值,因为是创世块,所以这个值是0
mixhash:与 nonce 配合用于挖矿,由上一个区块的一部分生成的 hash。(这个在bitcoin中没有,具体是干什么用的呢???)
extraData:附加信息,任意填写
gasLimit :对GAS的消耗总量限制,用来限制区块能包含的交易信息总和,因为我们就测试链,所以随意填写。
difficulty:难度值,越大越难 (bitcoin是难度值越小越难,注意下不同,随后抽空看下以太坊的难度值)
coinbase:矿工账号,第一个区块挖出后将给这个矿工账号发送奖励的以太币。(感觉貌似没用???也可能使用的方式不对,待测)
alloc: 预设账号以及账号的以太币数量,测试链挖矿比较容易可以不配置
chainId 指定了独立的区块链网络 ID,不同 ID 网络的节点无法互相连接。

创建了genesis文件之后,初始化私有链,命令为geth --datadir=data0 init genesis.json
执行结果如下:

1
2
3
4
5
6
7
8
9
hadoop@ubuntu:~/blockchain/ethereum$ geth --datadir data0 init genesis.json
WARN [08-23|08:39:07.353] Sanitizing cache to Go's GC limits provided=1024 updated=662
INFO [08-23|08:39:07.355] Maximum peer count ETH=25 LES=0 total=25
INFO [08-23|08:39:07.357] Allocated cache and file handles database=/home/hadoop/blockchain/ethereum/data0/geth/chaindata cache=16 handles=16
INFO [08-23|08:39:07.400] Persisted trie from memory database nodes=0 size=0.00B time=14.163µs gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [08-23|08:39:07.405] Successfully wrote genesis state database=chaindata hash=4a306e…543a63
INFO [08-23|08:39:07.405] Allocated cache and file handles database=/home/hadoop/blockchain/ethereum/data0/geth/lightchaindata cache=16 handles=16
INFO [08-23|08:39:07.420] Persisted trie from memory database nodes=0 size=0.00B time=3.02µs gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [08-23|08:39:07.420] Successfully wrote genesis state database=lightchaindata hash=4a306e…543a63

如上则初始化成功,下面开始使用私有链

使用私有链

执行命令geth --datadir data0 --networkid 1108 console进入geth的终端,此时就和上篇的开发环境一样了,同样的挖矿流程来一套就ok了。

别的命令不累赘了,有需要可以翻翻之前的文章,只记录下执行miner.start()命令之后的输出

miner.start()之后,先生成DAG,会展示进度,输出的关键字为Generating DAG in progress
随后在这个过程中会伴随块的产生,输出的关键字为Successfully sealed new block
具体输出如下:

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
> miner.start(2)
INFO [08-22|09:10:31.066] Updated mining threads threads=2
INFO [08-22|09:10:31.067] Transaction pool price threshold updated price=18000000000
null
> INFO [08-22|09:10:31.109] Starting mining operation
INFO [08-22|09:10:31.109] Commit new mining work number=1 txs=0 uncles=0 elapsed=118.785µs
INFO [08-22|09:10:31.369] Generating DAG in progress epoch=1 percentage=19 elapsed=2m3.630s
INFO [08-22|09:10:37.935] Generating DAG in progress epoch=1 percentage=20 elapsed=2m10.196s
INFO [08-22|09:10:44.781] Generating DAG in progress epoch=1 percentage=21 elapsed=2m17.043s
INFO [08-22|09:10:52.263] Generating DAG in progress epoch=1 percentage=22 elapsed=2m24.524s
INFO [08-22|09:10:59.633] Generating DAG in progress epoch=1 percentage=23 elapsed=2m31.895s
INFO [08-22|09:11:06.632] Generating DAG in progress epoch=1 percentage=24 elapsed=2m38.894s
INFO [08-22|09:11:13.379] Generating DAG in progress epoch=1 percentage=25 elapsed=2m45.641s
INFO [08-22|09:11:20.101] Generating DAG in progress epoch=1 percentage=26 elapsed=2m52.362s
INFO [08-22|09:11:26.798] Generating DAG in progress epoch=1 percentage=27 elapsed=2m59.059s
INFO [08-22|09:11:34.509] Generating DAG in progress epoch=1 percentage=28 elapsed=3m6.770s
INFO [08-22|09:11:41.806] Generating DAG in progress epoch=1 percentage=29 elapsed=3m14.068s
INFO [08-22|09:11:49.068] Generating DAG in progress epoch=1 percentage=30 elapsed=3m21.329s
> eth.accounts.INFO [08-22|09:11:56.419] Generating DAG in progress epoch=1 percentage=31 elapsed=3m28.680s
> eth.accounts
["0x729ef979ace85837425a46055e9b7264c32c21e6", "0xf5727a89f2b4dfb58ac199755823a22935fb9fbf"]
> INFO [08-22|09:12:03.486] Generating DAG in progress epoch=1 percentage=32 elapsed=3m35.747s
INFO [08-22|09:12:10.594] Generating DAG in progress epoch=1 percentage=33 elapsed=3m42.855s
INFO [08-22|09:12:17.769] Generating DAG in progress epoch=1 percentage=34 elapsed=3m50.031s
INFO [08-22|09:12:24.648] Generating DAG in progress epoch=1 percentage=35 elapsed=3m56.910s
INFO [08-22|09:12:31.965] Generating DAG in progress epoch=1 percentage=36 elapsed=4m4.226s
INFO [08-22|09:12:38.996] Generating DAG in progress epoch=1 percentage=37 elapsed=4m11.257s
INFO [08-22|09:12:46.210] Generating DAG in progress epoch=1 percentage=38 elapsed=4m18.472s
INFO [08-22|09:12:52.898] Generating DAG in progress epoch=1 percentage=39 elapsed=4m25.159s
INFO [08-22|09:12:59.072] Generating DAG in progress epoch=1 percentage=40 elapsed=4m31.333s
INFO [08-22|09:13:03.176] Successfully sealed new block number=1 hash=5b2050…9754cd
INFO [08-22|09:13:03.234] 🔨 mined potential block number=1 hash=5b2050…9754cd
INFO [08-22|09:13:03.237] Commit new mining work number=2 txs=0 uncles=0 elapsed=2.910ms
INFO [08-22|09:13:06.398] Successfully sealed new block number=2 hash=ecb729…78a768
INFO [08-22|09:13:06.399] 🔨 mined potential block number=2 hash=ecb729…78a768
INFO [08-22|09:13:06.399] Commit new mining work number=3 txs=0 uncles=0 elapsed=123.882µs
INFO [08-22|09:13:07.162] Generating DAG in progress epoch=1 percentage=41 elapsed=4m39.423s
INFO [08-22|09:13:08.005] Successfully sealed new block number=3 hash=265505…2113c3
INFO [08-22|09:13:08.005] 🔨 mined potential block number=3 hash=265505…2113c3
INFO [08-22|09:13:08.005] Commit new mining work number=4 txs=0 uncles=0 elapsed=93.513µs
INFO [08-22|09:13:10.197] Successfully sealed new block number=4 hash=07a7f2…9c3877
INFO [08-22|09:13:10.197] 🔨 mined potential block number=4 hash=07a7f2…9c3877
INFO [08-22|09:13:10.218] Commit new mining work number=5 txs=0 uncles=0 elapsed=118.561µs
INFO [08-22|09:13:11.572] Successfully sealed new block number=5 hash=80c62e…229bb9
INFO [08-22|09:13:11.573] 🔨 mined potential block number=5 hash=80c62e…229bb9
INFO [08-22|09:13:11.573] Commit new mining work number=6 txs=0 uncles=0 elapsed=134.57µs
INFO [08-22|09:13:13.347] Generating DAG in progress epoch=1 percentage=42 elapsed=4m45.609s
INFO [08-22|09:13:19.373] Generating DAG in progress epoch=1 percentage=43 elapsed=4m51.634s
INFO [08-22|09:13:26.582] Generating DAG in progress epoch=1 percentage=44 elapsed=4m58.844s
INFO [08-22|09:13:30.189] Successfully sealed new block number=6 hash=c8e518…fbad3c
INFO [08-22|09:13:30.190] 🔗 block reached canonical chain number=1 hash=5b2050…9754cd
INFO [08-22|09:13:30.192] 🔨 mined potential block number=6 hash=c8e518…fbad3c
INFO [08-22|09:13:30.203] Commit new mining work number=7 txs=0 uncles=0 elapsed=152.206µs
INFO [08-22|09:13:32.481] Generating DAG in progress epoch=1 percentage=45 elapsed=5m4.743s
INFO [08-22|09:13:34.301] Successfully sealed new block number=7 hash=b286af…979418
INFO [08-22|09:13:34.302] 🔗 block reached canonical chain number=2 hash=ecb729…78a768
INFO [08-22|09:13:34.302] 🔨 mined potential block number=7 hash=b286af…979418
INFO [08-22|09:13:34.302] Commit new mining work number=8 txs=0 uncles=0 elapsed=135.716µs
INFO [08-22|09:13:35.776] Successfully sealed new block number=8 hash=1496cd…e06258
INFO [08-22|09:13:35.778] 🔗 block reached canonical chain number=3 hash=265505…2113c3
INFO [08-22|09:13:35.778] 🔨 mined potential block number=8 hash=1496cd…e06258
INFO [08-22|09:13:35.796] Commit new mining work number=9 txs=0 uncles=0 elapsed=905.496µs
INFO [08-22|09:13:38.655] Generating DAG in progress epoch=1 percentage=46 elapsed=5m10.916s
INFO [08-22|09:13:40.374] Successfully sealed new block number=9 hash=8d7fa0…676de1
INFO [08-22|09:13:40.375] 🔗 block reached canonical chain number=4 hash=07a7f2…9c3877
INFO [08-22|09:13:40.375] 🔨 mined potential block number=9 hash=8d7fa0…676de1
INFO [08-22|09:13:40.386] Commit new mining work number=10 txs=0 uncles=0 elapsed=11.551ms
INFO [08-22|09:13:44.547] Generating DAG in progress epoch=1 percentage=47 elapsed=5m16.809s
INFO [08-22|09:13:52.465] Generating DAG in progress epoch=1 percentage=48 elapsed=5m24.726s
INFO [08-22|09:13:52.692] Successfully sealed new block number=10 hash=687e93…116aa7
INFO [08-22|09:13:52.693] 🔗 block reached canonical chain number=5 hash=80c62e…229bb9
INFO [08-22|09:13:52.693] 🔨 mined potential block number=10 hash=687e93…116aa7
INFO [08-22|09:13:52.693] Commit new mining work number=11 txs=0 uncles=0 elapsed=112.383µs
INFO [08-22|09:13:55.432] Successfully sealed new block number=11 hash=09a872…2054a1
INFO [08-22|09:13:55.432] 🔗 block reached canonical chain number=6 hash=c8e518…fbad3c
INFO [08-22|09:13:55.432] 🔨 mined potential block number=11 hash=09a872…2054a1
INFO [08-22|09:13:55.432] Commit new mining work number=12 txs=0 uncles=0 elapsed=124.793µs
INFO [08-22|09:13:57.145] Successfully sealed new block number=12 hash=e9f8d3…e1c6ee
INFO [08-22|09:13:57.145] 🔗 block reached canonical chain number=7 hash=b286af…979418
INFO [08-22|09:13:57.145] 🔨 mined potential block number=12 hash=e9f8d3…e1c6ee
INFO [08-22|09:13:57.145] Commit new mining work number=13 txs=0 uncles=0 elapsed=111.673µs
INFO [08-22|09:13:59.277] Generating DAG in progress epoch=1 percentage=49 elapsed=5m31.538s