-
Notifications
You must be signed in to change notification settings - Fork 1
/
ethereum.xml
1826 lines (1715 loc) · 67.4 KB
/
ethereum.xml
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V5.0//EN" "/usr/share/xml/docbook/schema/dtd/5.0/docbook.dtd" [
<!ENTITY article.author.xml SYSTEM "../common/article.author.xml">
<!ENTITY book.info.legalnotice.xml SYSTEM "../common/book.info.legalnotice.xml">
<!ENTITY book.info.abstract.xml SYSTEM "../common/book.info.abstract.xml">
]>
<article xml:base="http://netkiller.github.io/journal/" xmlns="http://docbook.org/ns/docbook"
xml:lang="zh-cn">
<articleinfo>
<title>以太坊私链入门</title>
<subtitle>本文作者最近在找工作,有意向致电 13113668890</subtitle>
&article.author.xml;
&book.info.legalnotice.xml;
<abstract>
<para>本文采用碎片化写作,原文会不定期更新,请尽量阅读原文。</para>
<para>
<ulink url="http://www.netkiller.cn/journal/ethereum.html">http://www.netkiller.cn/journal/ethereum.html</ulink>
</para>
</abstract>
&book.info.abstract.xml;
<keywordset>
<keyword>ethereum</keyword>
<keyword>geth</keyword>
<keyword>browser-solidity</keyword>
<keyword>solidity</keyword>
</keywordset>
<pubdate>$Date$</pubdate>
<release>$Id$</release>
</articleinfo>
<section id="background">
<title>背景</title>
<para>区块链是什么?一句话,它是一种特殊的(非关系型)分布式数据库,这种数据库只能做插入和查找操作,并且没有管理员。</para>
<para>首先,区块链的主要作用是储存信息。任何需要保存的信息,都可以写入区块链,也可以从里面读取,所以它是数据库。</para>
<para>其次,任何人都可以架设服务器,加入区块链网络,成为一个节点。区块链的世界里面,没有中心节点,每个节点都是平等的,都保存着整个数据库。你可以向任何一个节点,写入/读取数据,因为所有节点最后都会同步,保证区块链一致。</para>
</section>
<section id="overview">
<title>软件安装与配置</title>
<section id="ubuntu">
<title>Ubuntu</title>
<section id="ubuntu.geth">
<title>安装 geth</title>
<para>安装环境</para>
<literallayout>
<![CDATA[
Ubuntu 17.10
]]>
</literallayout>
<screen>
<![CDATA[
sudo apt upgrade -y
sudo apt install software-properties-common
sudo add-apt-repository -y ppa:ethereum/ethereum
sudo apt update
sudo apt install ethereum
]]>
</screen>
<screen>
<![CDATA[
neo@netkiller ~ % geth version
Geth
Version: 1.7.3-stable
Git Commit: 4bb3c89d44e372e6a9ab85a8be0c9345265c763a
Architecture: amd64
Protocol Versions: [63 62]
Network Id: 1
Go Version: go1.9.1
Operating System: linux
GOPATH=
GOROOT=/usr/lib/go-1.9
]]>
</screen>
</section>
<section id="ubuntu.solc">
<title>安装 solc</title>
<screen>
<![CDATA[
sudo apt install solc -y
]]>
</screen>
<screen>
<![CDATA[
neo@netkiller ~ % solc --version
solc, the solidity compiler commandline interface
Version: 0.4.19+commit.c4cbbb05.Linux.g++
]]>
</screen>
</section>
</section>
<section id="centos">
<title>CentOS 7</title>
<screen>
<![CDATA[
yum update -y
yum install git wget bzip2 -y
yum install golang -y
cd /usr/local/src
git clone https://github.com/ethereum/go-ethereum.git
cd go-ethereum/
gmake all
mv build /srv/go-ethereum
echo "export PATH=$PATH:$PWD/build/bin" >> /etc/profile
source /etc/profile
]]>
</screen>
<para>
上面安装版本是 unstable 版本,如果是生产环境请使用 Release 版本
<ulink url="https://github.com/ethereum/go-ethereum/tags">https://github.com/ethereum/go-ethereum/tags</ulink>
</para>
<screen>
<![CDATA[
wget https://github.com/ethereum/go-ethereum/archive/v1.7.3.tar.gz
tar zxvf v1.7.3.tar.gz
cd go-ethereum-1.7.3/
gmake all
mv build /srv/go-ethereum-1.7.3
]]>
</screen>
</section>
<section id="windows">
<title>Windows</title>
<screen>
<![CDATA[
访问 https://geth.ethereum.org/downloads/
下载并安装 Geth for Windows
]]>
</screen>
</section>
<section id="mac">
<title>Mac OS</title>
<screen>
<![CDATA[
brew update
brew upgrade
brew tap ethereum/ethereum
brew install ethereum
brew install solidity
]]>
</screen>
<section id="Ethereum Wallet">
<title>Ethereum Wallet</title>
<para>下载安装以太坊钱包(大陆网络可能下载有问题,需要翻墙)</para>
<ulink url="https://github.com/ethereum/mist/releases/download/v0.9.3/Ethereum-Wallet-macosx-0-9-3.dmg" />
<para>参数:</para>
<screen>
<![CDATA[
neo@MacBook-Pro ~ % "/Applications/Ethereum Wallet.app/Contents/MacOS/Ethereum Wallet" --help
Usage: /Applications/Ethereum Wallet.app/Contents/MacOS/Ethereum Wallet --help
[Mist options] [Node options]
Mist options:
--mode, -m App UI mode: wallet, mist.[string] [default: "wallet"]
--node Node to use: geth, eth [string] [default: null]
--network Network to connect to: main, test
[string] [default: null]
--rpc Path to node IPC socket file OR HTTP RPC hostport (if
IPC socket file then --node-ipcpath will be set with
this value). [string]
--swarmurl URL serving the Swarm HTTP API. If null, Mist will
open a local node.
[string] [default: "http://localhost:8500"]
--gethpath Path to Geth executable to use instead of default.
[string]
--ethpath Path to Eth executable to use instead of default.
[string]
--ignore-gpu-blacklist Ignores GPU blacklist (needed for some Linux
installations). [boolean]
--reset-tabs Reset Mist tabs to their default settings. [boolean]
--logfile Logs will be written to this file in addition to the
console. [string]
--loglevel Minimum logging threshold: info, debug, error, trace
(shows all logs, including possible passwords over
IPC!). [string] [default: "info"]
--syncmode Geth synchronization mode: [fast|light|full] [string]
--version, -v Display Mist version. [boolean]
--skiptimesynccheck Disable checks for the presence of automatic time sync
on your OS. [boolean]
Node options:
- To pass options to the underlying node (e.g. Geth) use the --node- prefix,
e.g. --node-datadir
Options:
-h, --help Show help [boolean]
]]>
</screen>
<para>钱包默认是连接到下面地址。</para>
<screen>
<![CDATA[
IPC endpoint opened: /Users/neo/Library/Ethereum/geth.ipc
]]>
</screen>
<para>连接到其他ipc地址</para>
<screen>
<![CDATA[
"/Applications/Ethereum Wallet.app/Contents/MacOS/Ethereum Wallet" --rpc /Users/other/Library/Ethereum/geth.ipc
]]>
</screen>
<para>如果需要连接到远程节点上,需要使用命令行,方法如下。启动钱包并连接到远程开发环境,localhost 改为你的IP地址即可。</para>
<screen>
<![CDATA[
"/Applications/Ethereum Wallet.app/Contents/MacOS/Ethereum Wallet" --rpc http://localhost:8545
]]>
</screen>
</section>
</section>
<section id="make">
<title>编译安装</title>
<screen>
<![CDATA[
git clone https://github.com/ethereum/go-ethereum
sudo apt-get install -y build-essential golang
cd go-ethereum
make geth
]]>
</screen>
</section>
<section>
<title>Netkiller OSCM 一键安装</title>
<para>适用于 CentOS 7</para>
<screen>
<![CDATA[
curl -s https://raw.githubusercontent.com/oscm/shell/master/blockchain/ethereum/centos/go-ethereum-1.7.3.sh | bash
]]>
</screen>
<para>安装完成后使用下面命令进入控制台</para>
<screen>
<![CDATA[
[root@localhost ~]# su - ethereum
Last login: Sat Feb 3 00:23:52 EST 2018 on pts/0
[ethereum@localhost ~]$ geth attach
Welcome to the Geth JavaScript console!
instance: Geth/v1.7.3-stable/linux-amd64/go1.8.3
modules: admin:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0
>
]]>
</screen>
<para></para>
</section>
</section>
<section id="genesis.json">
<title>创世区块</title>
<screen>
<![CDATA[
cd ~
mkdir -p ethereum
cd ethereum
]]>
</screen>
<section id="genesis.json.init">
<title>初始化创世区块</title>
<para>创建文件 genesis.json</para>
<screen>
<![CDATA[
{
"nonce": "0x0000000000000042",
"difficulty": "0x020000",
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"coinbase": "0x0000000000000000000000000000000000000000",
"timestamp": "0x00",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"extraData": "0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa",
"gasLimit": "0x4c4b40",
"config": {
"chainId": 15,
"homesteadBlock": 0,
"eip155Block": 0,
"eip158Block": 0
},
"alloc": { }
}
]]>
</screen>
<literallayout>
<![CDATA[
mixhash: 与nonce配合用于挖矿,由上一个区块的一部分生成的hash。注意他和nonce的设置需要满足以太坊的Yellow paper, 4.3.4. Block Header Validity, (44)章节所描述的条件。.
nonce: nonce就是一个64位随机数,用于挖矿,注意他和mixhash的设置需要满足以太坊的Yellow paper, 4.3.4. Block Header Validity, (44)章节所描述的条件。
difficulty: 设置当前区块的难度,如果难度过大,cpu挖矿就很难,这里设置较小难度
alloc: 用来预置账号以及账号的以太币数量,因为私有链挖矿比较容易,所以我们不需要预置有币的账号,需要的时候自己创建即可以。
coinbase: 矿工的账号,随便填
timestamp: 设置创世块的时间戳
parentHash: 上一个区块的hash值,因为是创世块,所以这个值是0
extraData: 附加信息,随便填,可以填你的个性信息
gasLimit: 该值设置对GAS的消耗总量限制,用来限制区块能包含的交易信息总和,因为我们是私有链,所以填最大。
]]>
</literallayout>
<para>初始化创世区块</para>
<screen>
<![CDATA[
neo@netkiller ~/ethereum % geth init genesis.json
WARN [01-19|17:35:17] No etherbase set and no accounts found as default
INFO [01-19|17:35:17] Allocated cache and file handles database=/home/neo/.ethereum/geth/chaindata cache=16 handles=16
INFO [01-19|17:35:17] Writing custom genesis block
INFO [01-19|17:35:17] Successfully wrote genesis state database=chaindata hash=611596…424d04
INFO [01-19|17:35:17] Allocated cache and file handles database=/home/neo/.ethereum/geth/lightchaindata cache=16 handles=16
INFO [01-19|17:35:18] Writing custom genesis block
INFO [01-19|17:35:18] Successfully wrote genesis state database=lightchaindata hash=611596…424d04
]]>
</screen>
<para>默认目录是 /home/neo/.ethereum/ 你可以通过 --datadir 参数指定目录</para>
<screen>
<![CDATA[
neo@netkiller ~/ethereum % geth --datadir data init genesis.json
WARN [01-19|17:38:16] No etherbase set and no accounts found as default
INFO [01-19|17:38:16] Allocated cache and file handles database=/home/neo/ethereum/data/geth/chaindata cache=16 handles=16
INFO [01-19|17:38:17] Writing custom genesis block
INFO [01-19|17:38:17] Successfully wrote genesis state database=chaindata hash=611596…424d04
INFO [01-19|17:38:17] Allocated cache and file handles database=/home/neo/ethereum/data/geth/lightchaindata cache=16 handles=16
INFO [01-19|17:38:17] Writing custom genesis block
INFO [01-19|17:38:17] Successfully wrote genesis state database=lightchaindata hash=611596…424d04
neo@netkiller ~/ethereum % find data
data
data/keystore
data/geth
data/geth/chaindata
data/geth/chaindata/LOCK
data/geth/chaindata/LOG
data/geth/chaindata/MANIFEST-000000
data/geth/chaindata/CURRENT
data/geth/chaindata/000001.log
data/geth/lightchaindata
data/geth/lightchaindata/LOCK
data/geth/lightchaindata/LOG
data/geth/lightchaindata/MANIFEST-000000
data/geth/lightchaindata/CURRENT
data/geth/lightchaindata/000001.log
]]>
</screen>
<para>目录结构</para>
<screen>
<![CDATA[
data
├── geth
│ ├── chaindata
│ │ ├── 000001.log
│ │ ├── CURRENT
│ │ ├── LOCK
│ │ ├── LOG
│ │ └── MANIFEST-000000
│ └── lightchaindata
│ ├── 000001.log
│ ├── CURRENT
│ ├── LOCK
│ ├── LOG
│ └── MANIFEST-000000
└── keystore
]]>
</screen>
</section>
<section id="genesis.json.startup">
<title>启动节点</title>
<screen>
<![CDATA[
neo@netkiller ~/ethereum % geth --networkid 123456 --rpc --rpccorsdomain "*" --nodiscover console
WARN [01-19|17:47:06] No etherbase set and no accounts found as default
INFO [01-19|17:47:06] Starting peer-to-peer node instance=Geth/v1.7.3-stable-4bb3c89d/linux-amd64/go1.9.1
INFO [01-19|17:47:06] Allocated cache and file handles database=/home/neo/.ethereum/geth/chaindata cache=128 handles=1024
INFO [01-19|17:47:06] Initialised chain configuration config="{ChainID: 15 Homestead: 0 DAO: <nil> DAOSupport: false EIP150: <nil> EIP155: 0 EIP158: 0 Byzantium: <nil> Engine: unknown}"
INFO [01-19|17:47:06] Disk storage enabled for ethash caches dir=/home/neo/.ethereum/geth/ethash count=3
INFO [01-19|17:47:06] Disk storage enabled for ethash DAGs dir=/home/neo/.ethash count=2
INFO [01-19|17:47:06] Initialising Ethereum protocol versions="[63 62]" network=123456
INFO [01-19|17:47:06] Loaded most recent local header number=0 hash=611596…424d04 td=131072
INFO [01-19|17:47:06] Loaded most recent local full block number=0 hash=611596…424d04 td=131072
INFO [01-19|17:47:06] Loaded most recent local fast block number=0 hash=611596…424d04 td=131072
INFO [01-19|17:47:06] Loaded local transaction journal transactions=0 dropped=0
INFO [01-19|17:47:06] Regenerated local transaction journal transactions=0 accounts=0
INFO [01-19|17:47:06] Starting P2P networking
INFO [01-19|17:47:06] RLPx listener up self="enode://9f6490ffb5236f2ddc5710ae73d47c740e0a3644bbd2d67029cf4a6c4693d2f470b642fd2cc3507f7e851df60aaeb730a1270b7a477f91ec5b6b17a8a4b40527@[::]:30303?discport=0"
INFO [01-19|17:47:06] IPC endpoint opened: /home/neo/.ethereum/geth.ipc
INFO [01-19|17:47:06] HTTP endpoint opened: http://127.0.0.1:8545
Welcome to the Geth JavaScript console!
instance: Geth/v1.7.3-stable-4bb3c89d/linux-amd64/go1.9.1
modules: admin:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0
> INFO [01-19|17:47:09] Mapped network port proto=tcp extport=30303 intport=30303 interface="UPNP IGDv1-IP1"
]]>
</screen>
<literallayout>
<![CDATA[
identity: 区块链的标示,随便填写,用于标示目前网络的名字
init: 指定创世块文件的位置,并创建初始块
datadir: 设置当前区块链网络数据存放的位置
port: 网络监听端口
rpc: 启动rpc通信,可以进行智能合约的部署和调试
rpcapi: 设置允许连接的rpc的客户端,一般为db,eth,net,web3
networkid: 设置当前区块链的网络ID,用于区分不同的网络,是一个数字
console: 启动命令行模式,可以在Geth中执行命令
]]>
</literallayout>
<section id="rpcaddr">
<title>rpcaddr</title>
<para>默认是 127.0.0.1 </para>
<para>HTTP endpoint closed: http://127.0.0.1:8545</para>
<para>通过 --rpcaddr="0.0.0.0" 指定监听地址</para>
<para>HTTP endpoint opened: http://0.0.0.0:8545</para>
<screen>
<![CDATA[
neo@netkiller ~/ethereum % geth --networkid 123456 --rpc --rpcaddr="0.0.0.0" --rpccorsdomain "*" --nodiscover console
INFO [01-20|01:41:33] Starting peer-to-peer node instance=Geth/v1.7.3-stable-4bb3c89d/linux-amd64/go1.9.1
INFO [01-20|01:41:33] Allocated cache and file handles database=/home/neo/.ethereum/geth/chaindata cache=128 handles=1024
INFO [01-20|01:41:34] Initialised chain configuration config="{ChainID: 15 Homestead: 0 DAO: <nil> DAOSupport: false EIP150: <nil> EIP155: 0 EIP158: 0 Byzantium: <nil> Engine: unknown}"
INFO [01-20|01:41:34] Disk storage enabled for ethash caches dir=/home/neo/.ethereum/geth/ethash count=3
INFO [01-20|01:41:34] Disk storage enabled for ethash DAGs dir=/home/neo/.ethash count=2
INFO [01-20|01:41:34] Initialising Ethereum protocol versions="[63 62]" network=123456
INFO [01-20|01:41:34] Loaded most recent local header number=531 hash=1a2707…3a27bc td=79083846
INFO [01-20|01:41:34] Loaded most recent local full block number=531 hash=1a2707…3a27bc td=79083846
INFO [01-20|01:41:34] Loaded most recent local fast block number=531 hash=1a2707…3a27bc td=79083846
INFO [01-20|01:41:34] Loaded local transaction journal transactions=0 dropped=0
INFO [01-20|01:41:34] Regenerated local transaction journal transactions=0 accounts=0
WARN [01-20|01:41:34] Blockchain not empty, fast sync disabled
INFO [01-20|01:41:34] Starting P2P networking
INFO [01-20|01:41:34] RLPx listener up self="enode://9f6490ffb5236f2ddc5710ae73d47c740e0a3644bbd2d67029cf4a6c4693d2f470b642fd2cc3507f7e851df60aaeb730a1270b7a477f91ec5b6b17a8a4b40527@[::]:30303?discport=0"
INFO [01-20|01:41:34] IPC endpoint opened: /home/neo/.ethereum/geth.ipc
INFO [01-20|01:41:34] HTTP endpoint opened: http://0.0.0.0:8545
Welcome to the Geth JavaScript console!
instance: Geth/v1.7.3-stable-4bb3c89d/linux-amd64/go1.9.1
coinbase: 0x83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6
at block: 531 (Tue, 14 Nov 2017 17:36:05 HST)
datadir: /home/neo/.ethereum
modules: admin:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0
> INFO [01-20|01:41:40] Mapped network port proto=tcp extport=30303 intport=30303 interface="UPNP IGDv1-IP1"
]]>
</screen>
</section>
</section>
<section id="genesis.json.miner">
<title>使用节点进行挖矿</title>
<section id="genesis.json.miner.start">
<title>启动矿工开始挖矿</title>
<screen>
<![CDATA[
> miner.start(1)
]]>
</screen>
<para>这里的1表示只使用一个线程运行,第一次运行时将开始创建DAG文件,只需等待进度条到100,则将开始挖矿。
实际你看到的挖矿速度很快,这是因为我们已经在初始化创世区块时配置为:"nonce": "0x0000000000000042"。
“0x42”难度能让你在私有测试网链上快速挖以太币。</para>
<tip>
<para>挖矿时必然有矿工账户,而系统默认使用创建的第一个账号。</para>
</tip>
<screen>
<![CDATA[
> miner.start(1)
INFO [01-19|21:06:43] Updated mining threads threads=1
INFO [01-19|21:06:43] Transaction pool price threshold updated price=18000000000
INFO [01-19|21:06:43] Starting mining operation
null
> INFO [01-19|21:06:43] Commit new mining work number=1 txs=0 uncles=0 elapsed=717.552µs
INFO [01-19|21:06:46] Generating ethash verification cache epoch=0 percentage=91 elapsed=3.000s
INFO [01-19|21:06:46] Generated ethash verification cache epoch=0 elapsed=3.273s
INFO [01-19|21:06:51] Generating DAG in progress epoch=0 percentage=0 elapsed=5.056s
INFO [01-19|21:06:56] Generating DAG in progress epoch=0 percentage=1 elapsed=10.140s
INFO [01-19|21:07:01] Generating DAG in progress epoch=0 percentage=2 elapsed=15.119s
INFO [01-19|21:07:06] Generating DAG in progress epoch=0 percentage=3 elapsed=19.924s
INFO [01-19|21:07:11] Generating DAG in progress epoch=0 percentage=4 elapsed=24.739s
INFO [01-19|21:07:16] Generating DAG in progress epoch=0 percentage=5 elapsed=29.473s
INFO [01-19|21:07:22] Generating DAG in progress epoch=0 percentage=6 elapsed=35.641s
INFO [01-19|21:07:26] Generating DAG in progress epoch=0 percentage=7 elapsed=40.374s
INFO [01-19|21:07:31] Generating DAG in progress epoch=0 percentage=8 elapsed=45.134s
INFO [01-19|21:07:36] Generating DAG in progress epoch=0 percentage=9 elapsed=49.908s
INFO [01-19|21:07:41] Generating DAG in progress epoch=0 percentage=10 elapsed=54.633s
......
......
......
INFO [01-19|21:22:43] Generated ethash verification cache epoch=0 elapsed=15m57.328s
INFO [01-19|21:22:47] Generating ethash verification cache epoch=1 percentage=17 elapsed=3.031s
INFO [01-19|21:22:50] Generating ethash verification cache epoch=1 percentage=34 elapsed=6.056s
INFO [01-19|21:22:53] Generating ethash verification cache epoch=1 percentage=49 elapsed=9.562s
INFO [01-19|21:22:57] Generating ethash verification cache epoch=1 percentage=70 elapsed=13.115s
INFO [01-19|21:23:00] Generating ethash verification cache epoch=1 percentage=90 elapsed=16.123s
INFO [01-19|21:23:01] Generated ethash verification cache epoch=1 elapsed=17.576s
INFO [01-19|21:23:19] Generating DAG in progress epoch=1 percentage=0 elapsed=18.198s
INFO [01-19|21:23:32] Successfully sealed new block number=1 hash=e2b5b9…9b1bfe
INFO [01-19|21:23:32] 🔨 mined potential block number=1 hash=e2b5b9…9b1bfe
INFO [01-19|21:23:32] Commit new mining work number=2 txs=0 uncles=0 elapsed=1.188ms
INFO [01-19|21:23:37] Generating DAG in progress epoch=1 percentage=1 elapsed=35.913s
INFO [01-19|21:23:41] Successfully sealed new block number=2 hash=62db3f…e27b50
INFO [01-19|21:23:41] 🔨 mined potential block number=2 hash=62db3f…e27b50
INFO [01-19|21:23:41] Commit new mining work number=3 txs=0 uncles=0 elapsed=772.239µs
INFO [01-19|21:23:43] Successfully sealed new block number=3 hash=34384b…c387f2
INFO [01-19|21:23:43] 🔨 mined potential block number=3 hash=34384b…c387f2
INFO [01-19|21:23:43] Commit new mining work number=4 txs=0 uncles=0 elapsed=1.002ms
INFO [01-19|21:23:55] Generating DAG in progress epoch=1 percentage=2 elapsed=53.757s
INFO [01-19|21:24:13] Generating DAG in progress epoch=1 percentage=3 elapsed=1m11.561s
INFO [01-19|21:24:30] Generating DAG in progress epoch=1 percentage=4 elapsed=1m28.986s
INFO [01-19|21:24:30] Successfully sealed new block number=4 hash=681970…462135
INFO [01-19|21:24:30] 🔨 mined potential block number=4 hash=681970…462135
INFO [01-19|21:24:30] Commit new mining work number=5 txs=0 uncles=0 elapsed=833.629µs
INFO [01-19|21:24:36] Successfully sealed new block number=5 hash=7b058b…d2f07a
INFO [01-19|21:24:36] 🔨 mined potential block number=5 hash=7b058b…d2f07a
INFO [01-19|21:24:36] Commit new mining work number=6 txs=0 uncles=0 elapsed=897.815µs
INFO [01-19|21:24:43] Successfully sealed new block number=6 hash=a5fc3d…b1221e
INFO [01-19|21:24:43] 🔗 block reached canonical chain number=1 hash=e2b5b9…9b1bfe
INFO [01-19|21:24:43] 🔨 mined potential block number=6 hash=a5fc3d…b1221e
INFO [01-19|21:24:43] Commit new mining work number=7 txs=0 uncles=0 elapsed=758.061µs
INFO [01-19|21:24:47] Successfully sealed new block number=7 hash=003b02…e886fd
INFO [01-19|21:24:47] 🔗 block reached canonical chain number=2 hash=62db3f…e27b50
INFO [01-19|21:24:47] 🔨 mined potential block number=7 hash=003b02…e886fd
INFO [01-19|21:24:47] Commit new mining work number=8 txs=0 uncles=0 elapsed=920.862µs
INFO [01-19|21:24:48] Generating DAG in progress epoch=1 percentage=5 elapsed=1m46.827s
INFO [01-19|21:25:06] Generating DAG in progress epoch=1 percentage=6 elapsed=2m4.338s
INFO [01-19|21:25:23] Successfully sealed new block number=8 hash=fd23c9…361c65
INFO [01-19|21:25:23] 🔗 block reached canonical chain number=3 hash=34384b…c387f2
INFO [01-19|21:25:23] 🔨 mined potential block number=8 hash=fd23c9…361c65
INFO [01-19|21:25:23] Commit new mining work number=9 txs=0 uncles=0 elapsed=825.737µs
INFO [01-19|21:25:23] Generating DAG in progress epoch=1 percentage=7 elapsed=2m22.061s
]]>
</screen>
</section>
<section id="genesis.json.miner.stop">
<title>停止挖矿</title>
<screen>
<![CDATA[
> miner.stop()
true
>
]]>
</screen>
</section>
<section id="genesis.json.eth.getBalance">
<title>查看所挖金额</title>
<screen>
<![CDATA[
> eth.getBalance(eth.accounts[0])
70000000000000000000
]]>
</screen>
</section>
</section>
<section id="genesis.json.alloc">
<title>在创世链中制定矿工账号并为它充值</title>
<screen>
<![CDATA[
"alloc": {
"0xe8abf98484325fd6afc59b804ac15804b978e607": {
"balance": "300000"
},
"0x013b5e735e1b48421dd3de3b931d6f03e769e22b": {
"balance": "400000"
}
}
]]>
</screen>
</section>
</section>
<section id="geth">
<title>geth 命令</title>
<section id="geth.console">
<title>控制台</title>
<subtitle>Geth JavaScript console</subtitle>
<screen>
<![CDATA[
neo@netkiller ~/ethereum % geth --networkid 123456 console
INFO [01-19|22:14:52] Starting peer-to-peer node instance=Geth/v1.7.3-stable-4bb3c89d/linux-amd64/go1.9.1
INFO [01-19|22:14:52] Allocated cache and file handles database=/home/neo/.ethereum/geth/chaindata cache=128 handles=1024
INFO [01-19|22:14:52] Initialised chain configuration config="{ChainID: 15 Homestead: 0 DAO: <nil> DAOSupport: false EIP150: <nil> EIP155: 0 EIP158: 0 Byzantium: <nil> Engine: unknown}"
INFO [01-19|22:14:52] Disk storage enabled for ethash caches dir=/home/neo/.ethereum/geth/ethash count=3
INFO [01-19|22:14:52] Disk storage enabled for ethash DAGs dir=/home/neo/.ethash count=2
INFO [01-19|22:14:52] Initialising Ethereum protocol versions="[63 62]" network=123456
INFO [01-19|22:14:52] Loaded most recent local header number=14 hash=70d7f1…45850a td=1966848
INFO [01-19|22:14:52] Loaded most recent local full block number=14 hash=70d7f1…45850a td=1966848
INFO [01-19|22:14:52] Loaded most recent local fast block number=14 hash=70d7f1…45850a td=1966848
INFO [01-19|22:14:52] Loaded local transaction journal transactions=0 dropped=0
INFO [01-19|22:14:52] Regenerated local transaction journal transactions=0 accounts=0
WARN [01-19|22:14:52] Blockchain not empty, fast sync disabled
INFO [01-19|22:14:52] Starting P2P networking
INFO [01-19|22:14:56] UDP listener up self=enode://9f6490ffb5236f2ddc5710ae73d47c740e0a3644bbd2d67029cf4a6c4693d2f470b642fd2cc3507f7e851df60aaeb730a1270b7a477f91ec5b6b17a8a4b40527@101.232.64.12:30303
INFO [01-19|22:14:56] RLPx listener up self=enode://9f6490ffb5236f2ddc5710ae73d47c740e0a3644bbd2d67029cf4a6c4693d2f470b642fd2cc3507f7e851df60aaeb730a1270b7a477f91ec5b6b17a8a4b40527@101.232.64.12:30303
INFO [01-19|22:14:56] IPC endpoint opened: /home/neo/.ethereum/geth.ipc
Welcome to the Geth JavaScript console!
instance: Geth/v1.7.3-stable-4bb3c89d/linux-amd64/go1.9.1
coinbase: 0x83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6
at block: 14 (Fri, 19 Jan 2018 21:27:16 HST)
datadir: /home/neo/.ethereum
modules: admin:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0
>
> INFO [01-19|22:14:56] Mapped network port proto=udp extport=30303 intport=30303 interface="UPNP IGDv1-IP1"
]]>
</screen>
</section>
<section id="geth.attach">
<title>连接控制台</title>
<para>一般测试启动我们使用 console,如果是正式启动无需使用 console。同事我们使用&符号使其进入后台运行。</para>
<screen>
<![CDATA[
neo@netkiller ~/ethereum % geth --networkid 123456 --rpc --rpcaddr="0.0.0.0" --rpccorsdomain "*" --nodiscover &
]]>
</screen>
<para>进入控制台</para>
<screen>
<![CDATA[
neo@netkiller ~/ethereum % geth attach
Welcome to the Geth JavaScript console!
instance: Geth/v1.7.3-stable-4bb3c89d/linux-amd64/go1.9.1
coinbase: 0x83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6
at block: 531 (Tue, 14 Nov 2017 17:36:05 HST)
datadir: /home/neo/.ethereum
modules: admin:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0
]]>
</screen>
<para>退出控制台</para>
<screen>
<![CDATA[
> exit
]]>
</screen>
<section id="geth.ipcpath">
<title>指定 geth.ipc 文件位置</title>
<screen>
<![CDATA[
geth --ipcpath ~/.ethereum/geth.ipc attach
]]>
</screen>
</section>
<section id="geth.attach.ipc">
<title>IPC 方式连接</title>
<screen>
<![CDATA[
neo@netkiller ~ % geth attach ethereum/data1/geth.ipc
Welcome to the Geth JavaScript console!
instance: Geth/v1.7.3-stable-4bb3c89d/linux-amd64/go1.9.1
modules: admin:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0
>
]]>
</screen>
</section>
<section id="geth.tcp.remote">
<title>TCP 连接控制台</title>
<para>连接远程控制台</para>
<screen>
<![CDATA[
neo@netkiller ~/ethereum % geth --exec 'eth.coinbase' attach http://172.16.0.10:8545
"0x83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6"
]]>
</screen>
</section>
</section>
<section id="geth.account">
<title>账号管理</title>
<section id="new">
<title>新建账号</title>
<para>查看账号</para>
<screen>
<![CDATA[
neo@netkiller ~/ethereum % geth account list
Account #0: {83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6} keystore:///home/neo/.ethereum/keystore/UTC--2018-01-20T04-04-06.786586541Z--83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6
]]>
</screen>
<para>创建账号</para>
<screen>
<![CDATA[
neo@netkiller ~/ethereum % geth account new
Your new account is locked with a password. Please give a password. Do not forget this password.
Passphrase:
Repeat passphrase:
Address: {e8abf98484325fd6afc59b804ac15804b978e607}
]]>
</screen>
<para>指定密码</para>
<screen>
<![CDATA[
$ echo "abc123" > password
$ geth --password /path/to/password account new
]]>
</screen>
<para>账号创建在 ~/.ethereum/keystore 目录下</para>
<screen>
<![CDATA[
Mac: ~/Library/Ethereum/keystore
Linux: ~/.ethereum/keystore
Windows: %APPDATA%/Ethereum/keystore
]]>
</screen>
<programlisting>
<![CDATA[
neo@netkiller ~/.ethereum/keystore % ll
total 8.0K
-rw------- 1 neo neo 491 Jan 19 18:04 UTC--2018-01-20T04-04-06.786586541Z--83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6
-rw------- 1 neo neo 491 Jan 19 20:11 UTC--2018-01-20T06-11-23.608902164Z--e8abf98484325fd6afc59b804ac15804b978e607
]]>
</programlisting>
</section>
<section id="list">
<title>查看账号</title>
<screen>
<![CDATA[
neo@netkiller ~/ethereum % geth account list
Account #0: {83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6} keystore:///home/neo/.ethereum/keystore/UTC--2018-01-20T04-04-06.786586541Z--83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6
Account #1: {e8abf98484325fd6afc59b804ac15804b978e607} keystore:///home/neo/.ethereum/keystore/UTC--2018-01-20T06-11-23.608902164Z--e8abf98484325fd6afc59b804ac15804b978e607
]]>
</screen>
</section>
</section>
<section id="geth.rpc">
<title>api 相关参数</title>
<para>rpcapi 启动后允许连接到系统的API协议</para>
<screen>
<![CDATA[
geth --networkid 100000 --rpc --rpcapi "db,eth,net,web3" --rpccorsdomain "*" --datadir "/app/chain" --port "30303" console
]]>
</screen>
<para>系统默认监听 127.0.0.1 如果希望外部访问本机,需要通过--rpcaddr指定监听地址。</para>
<screen>
<![CDATA[
geth --networkid 123456 --rpc --rpcaddr="0.0.0.0" --rpccorsdomain "*" --nodiscover
]]>
</screen>
<para>--rpcapi 可以控制访问内容</para>
<screen>
<![CDATA[
$ geth --rpcapi personal,db,eth,net,web3 --rpc --rinkeby
]]>
</screen>
</section>
<section id="geth.password">
<title>配置自动解锁账号</title>
<para>创建password文件,在里面输入密码,每个账号一行密码如:</para>
<screen>
<![CDATA[
123456
123456
123456
]]>
</screen>
<para>启动参数</para>
<screen>
<![CDATA[
geth --rpc --rpcaddr="0.0.0.0" --rpccorsdomain="*" --unlock '0,1,2' --password ~/.ethereum/password --nodiscover --maxpeers '5' --networkid '12345' --datadir '~/.ethereum' console
]]>
</screen>
</section>
<section id="geth.exec">
<title>运行JS</title>
<screen>
<![CDATA[
neo@netkiller ~/ethereum % geth --exec "eth.blockNumber" attach
531
]]>
</screen>
<screen>
<![CDATA[
$ geth --exec 'loadScript("/tmp/checkbalances.js")' attach http://123.123.123.123:8545
$ geth --jspath "/tmp" --exec 'loadScript("checkbalances.js")' attach http://123.123.123.123:8545
]]>
</screen>
</section>
<section id="geth.bootnodes">
<title>节点管理</title>
<screen>
<![CDATA[
geth --bootnodes enode://pubkey1@ip1:port1,enode://pubkey2@ip2:port2,enode://pubkey3@ip3:port3
]]>
</screen>
</section>
<section id="geth.mine">
<title>启动挖矿</title>
<screen>
<![CDATA[
geth --mine
]]>
</screen>
<section id="geth.minerthreads">
<title>挖矿线程数</title>
<para>默认为 2</para>
<screen>
<![CDATA[
geth --mine --minerthreads=16
]]>
</screen>
</section>
<section id="geth.etherbase">
<title>指定旷工账号</title>
<para>默认是第一个账号</para>
<screen>
<![CDATA[
geth --mine --minerthreads=16 --etherbase=0x83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6
]]>
</screen>
</section>
</section>
<section id="geth.log">
<title>日志</title>
<screen>
<![CDATA[
geth --networkid 123456 --rpc --rpcaddr="0.0.0.0" --rpccorsdomain "*" 2>> /tmp/geth.log
]]>
</screen>
<para>后台运行</para>
<screen>
<![CDATA[
neo@netkiller ~ % geth --networkid 123456 --rpc --rpcaddr="0.0.0.0" --rpccorsdomain "*" 2>> /tmp/geth.log &
[1] 30075
neo@netkiller ~ % tail -f /tmp/geth.log
INFO [02-02|21:18:59] Got interrupt, shutting down...
INFO [02-02|21:18:59] HTTP endpoint closed: http://0.0.0.0:8545
INFO [02-02|21:18:59] IPC endpoint closed: /home/neo/.ethereum/geth.ipc
INFO [02-02|21:18:59] Blockchain manager stopped
INFO [02-02|21:18:59] Stopping Ethereum protocol
INFO [02-02|21:18:59] Ethereum protocol stopped
INFO [02-02|21:18:59] Transaction pool stopped
INFO [02-02|21:18:59] Database closed database=/home/neo/.ethereum/geth/chaindata
INFO [02-02|21:18:59] Mapped network port proto=tcp extport=30303 intport=30303 interface="UPNP IGDv1-IP1"
WARN [02-02|21:19:00] Already shutting down, interrupt more to panic. times=9
]]>
</screen>
</section>
</section>
<section id="javascript.console">
<title>JavaScript Console</title>
<section id="personal">
<title>personal 管理</title>
<section id="personal.newAccount">
<title>创建账号</title>
<screen>
<![CDATA[
> personal.newAccount()
Passphrase:
Repeat passphrase:
"0x83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6"
]]>
</screen>
<para>指定密码创建用户</para>
<screen>
<![CDATA[
personal.newAccount("123")
]]>
</screen>
</section>
<section id="personal.listAccounts">
<title>列出账号</title>
<para>列出所有账号</para>
<screen>
<![CDATA[
> personal.listAccounts
["0x83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6", "0xe8abf98484325fd6afc59b804ac15804b978e607"]
]]>
</screen>
<para>列出指定账号</para>
<screen>
<![CDATA[
> personal.listAccounts[1]
"0xe8abf98484325fd6afc59b804ac15804b978e607"
> personal.listAccounts[0]
"0x83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6"
]]>
</screen>
</section>
<section id="personal.unlockAccount">
<title>解锁账号</title>
<screen>
<![CDATA[
> personal.unlockAccount(eth.accounts[0],"password")
]]>
</screen>
<para>指定过期时间,单位是毫秒,下面例子是 20 分钟</para>
<screen>
<![CDATA[
> personal.unlockAccount(eth.accounts[0],"password", 1000*60*20)
]]>
</screen>
</section>
</section>
<section id="eth">
<title>eth 管理</title>
<section id="eth.coinbase">
<title>矿工账号</title>
<para>查看默认旷工账号,系统中的第一个账号。</para>
<screen>
<![CDATA[
> eth.coinbase
"0x83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6"
]]>
</screen>
<para>查看账号列表</para>
<screen>
<![CDATA[
> eth.accounts
["0x83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6", "0xe8abf98484325fd6afc59b804ac15804b978e607"]
> eth.accounts[0]
"0x83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6"
]]>
</screen>
</section>
<section id="eth.getBalance">
<title>余额</title>
<screen>
<![CDATA[
> eth.getBalance(eth.accounts[0])
70000000000000000000
]]>
</screen>
<section>
<title>单位转换</title>
<para>eth.getBalance()返回的余额是以太币的最小面额wei,将wei转换为以太币ether。</para>
<programlisting>
<![CDATA[
primary = eth.accounts[0]
balance = web3.fromWei(eth.getBalance(primary), "ether");
]]>
</programlisting>
<para>演示</para>
<screen>
<![CDATA[
> primary = eth.accounts[0]
"0x83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6"
> balance = web3.fromWei(eth.getBalance(primary), "ether");
70
]]>
</screen>
</section>
<section>
<title>一次检查所有账号余额</title>
<para>定义函数</para>
<programlisting>
<![CDATA[
function checkAllBalances() {
web3.eth.getAccounts(function(err, accounts) {
accounts.forEach(function(id) {
web3.eth.getBalance(id, function(err, balance) {
console.log("" + id + ":\tbalance: " + web3.fromWei(balance, "ether") + " ether");
});
});
});
};
]]>
</programlisting>
<para>运行函数</para>
<screen>
<![CDATA[
checkAllBalances()
]]>
</screen>
<para>输出演示</para>
<screen>
<![CDATA[
> function checkAllBalances() {
... web3.eth.getAccounts(function(err, accounts) {
......... accounts.forEach(function(id) {
............... web3.eth.getBalance(id, function(err, balance) {
..................... console.log("" + id + ":\tbalance: " + web3.fromWei(balance, "ether") + " ether");
..................... });
............... });
......... });
... };
undefined
> checkAllBalances()
0x83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6: balance: 929 ether
0xe8abf98484325fd6afc59b804ac15804b978e607: balance: 11 ether
undefined
]]>
</screen>
</section>
</section>
<section id="eth.sendTransaction">
<title>转账</title>