Home | 簡體中文 | 繁體中文 | 雜文 | 知乎專欄 | Github | OSChina 博客 | 雲社區 | 雲棲社區 | Facebook | Linkedin | 視頻教程 | 打賞(Donations) | About
知乎專欄多維度架構 | 微信號 netkiller-ebook | QQ群:128659835 請註明“讀者”

10.2. 創世區塊

		
cd ~
mkdir -p ethereum
cd ethereum		
		
		

10.2.1. 初始化創世區塊

創建檔案 genesis.json

			
{
  "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": { }
}
			
			



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的消耗總量限制,用來限制區塊能包含的交易信息總和,因為我們是私有鏈,所以填最大。

初始化創世區塊

			
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		
			
			

預設目錄是 /home/neo/.ethereum/ 你可以通過 --datadir 參數指定目錄

			
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		
			
			

目錄結構

			
data
├── geth
│  ├── chaindata
│  │  ├── 000001.log
│  │  ├── CURRENT
│  │  ├── LOCK
│  │  ├── LOG
│  │  └── MANIFEST-000000
│  └── lightchaindata
│      ├── 000001.log
│      ├── CURRENT
│      ├── LOCK
│      ├── LOG
│      └── MANIFEST-000000
└── keystore		
		
			

10.2.2. 創建主賬號

			
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: {39211a30bfe33d4b12fcbd786472c8a552b93389}			
			
			
[提示]提示

密碼可以直接回車,但是後面 Ethereum Wallet 遇到了麻煩, Ethereum Wallet 要求密碼必須是8為數

10.2.3. 啟動節點

			
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.8.10-stable-4bb3c89d/linux-amd64/go1.9.5
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.8.10-stable-4bb3c89d/linux-amd64/go1.9.5
 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"			
			
			



identity: 區塊鏈的標示,隨便填寫,用於標示目前網絡的名字

init: 指定創世塊檔案的位置,並創建初始塊

datadir: 設置當前區塊鏈網絡數據存放的位置

port: 網絡監聽連接埠

rpc: 啟動rpc通信,可以進行智能合約的部署和調試

rpcapi: 設置允許連接的rpc的客戶端,一般為db,eth,net,web3

networkid: 設置當前區塊鏈的網絡ID,用於區分不同的網絡,是一個數字

console: 啟動命令行模式,可以在Geth中執行命令

10.2.4. 使用節點進行挖礦

10.2.4.1. 啟動礦工開始挖礦

				
> miner.start(1)		
				
				

這裡的1表示只使用一個綫程運行,第一次運行時將開始創建DAG檔案,只需等待進度條到100,則將開始挖礦。 實際你看到的挖礦速度很快,這是因為我們已經在初始化創世區塊時配置為:"nonce": "0x0000000000000042"。 “0x42”難度能讓你在私有測試網鏈上快速挖以太幣。

[提示]提示

挖礦時必然有礦工賬戶,而系統預設使用創建的第一個賬號。

				
> 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
				
				

10.2.4.2. 停止挖礦

				
> miner.stop()
true
>
				
				

10.2.4.3. 查看所挖金額

				
> eth.getBalance(eth.accounts[0])
70000000000000000000				
				
				

10.2.5. 在創世鏈中制定礦工賬號併為它充值

			
  "alloc": {
    "0xe8abf98484325fd6afc59b804ac15804b978e607": {
      "balance": "300000"
    },
    "0x013b5e735e1b48421dd3de3b931d6f03e769e22b": {
      "balance": "400000"
    }
  }