通過一段時間關於以太坊的學習,博主也算是對區塊鏈以及以太坊有了一個初步的瞭解,整理一篇關於以太坊搭建私有鏈的方法。node
若想以太坊節點之間可以相互鏈接須要知足必定的條件:linux
搭建私有鏈最簡單的方式就是使用geth裏的 --network 選項,設置一個與主網不一樣的network id (以太坊主網id是1)web
1、創建創世區塊json
打開終端輸入如下命令vim
1 mkdir privatechain #創建文件夾,名字自擬 2 cd privatechain #終端進入創建的文件夾
創建創世區塊文件genesis.jsonbash
1 vim genesis.json #注:沒有安裝vim 能夠用 vi 命令
在創世區塊的json文件中填入如下內容:網絡
1 { 2 "config": { 3 "chainId": 20, 4 "homesteadBlock": 0, 5 "eip155Block": 0, 6 "eip158Block": 0 7 }, 8 "coinbase" : "0x0000000000000000000000000000000000000000", 9 "difficulty" : "0x100", #爲方便挖礦,咱們這裏將難度值設置的很小 10 "extraData" : "", 11 "gasLimit" : "0xffffffff", 12 "nonce" : "0x0000000000000042", 13 "mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000", 14 "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000", 15 "timestamp" : "0x00", 16 "alloc": { } 17 }
各個參數的介紹tcp
參數名稱 | 參數詳解 |
chainId |
指定了獨立的區塊鏈網絡id,網絡id在鏈接到其它節點的時候會用到,以太坊公網id是1,不一樣的id網絡的節點沒法相互鏈接 |
homesteadBlock |
HomesteadBlock是以太坊的第二個主要版本,第一個是Frontier,這個值設置爲「0」表示目前正在使用Homestead版本 |
eip155Block |
eip是ethereum improvement proposal的縮寫,咱們的鏈不會提議分叉,因此設置爲「0」便可 |
eip158Block |
eip是ethereum improvement proposal的縮寫,咱們的鏈不會提議分叉,因此設置爲「0」便可 |
coinbase |
礦工的帳號(隨便填) |
difficulty |
設置當前區塊的難度,若是難度過大,cpu挖礦就很難,這裏設置較小難度 |
extraData |
能夠寫入32byte大小的任意數據,每一個block都會有,由挖出block的miner來決定要不要寫點什麼 |
gasLimit |
該值設置對gas的消耗總量限制,用來限制區塊能包含的交易信息和,由於咱們是私有鏈,因此填最大 |
nonce |
一個用於挖礦的64位隨機數 |
mixhash |
與nonce配合用於挖礦,由上一個區塊的一部分生成的哈希,注意它和nonce的設置須要知足以太坊的yellow paper |
parentHash |
上一個區塊的哈希值,由於是創世區塊,因此這個值是0 |
timestamp |
設置創世區塊的時間戳性能 |
alloc |
給某個帳戶預分配以太幣學習 |
2、初始化創世節點,而且設置data目錄
在privatechain目錄終端下輸入以下命令
1 geth --datadir ./data/00 init genesis.json 2 3 #--datadir data 中data是文件夾的名稱,私有鏈的數據會存儲在這個文件夾內。若存在其餘節點文件名要不一樣。能夠經過「--datadir 文件名」來區分不一樣的私有鏈節點,若沒有設置--datadir,則會讀取以太坊主網的數據,默認位置在/root/.ethereum
出現以日誌說明初始化成功
INFO [06-26|21:02:01.682] Maximum peer count ETH=25 LES=0 total=25 INFO [06-26|21:02:01.697] Allocated cache and file handles database=/home/milo/private-geth/data/00/geth/chaindata cache=16 handles=16 INFO [06-26|21:02:01.866] Writing custom genesis block INFO [06-26|21:02:01.866] Persisted trie from memory database nodes=0 size=0.00B time=4.212µs gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B INFO [06-26|21:02:01.867] Successfully wrote genesis state database=chaindata hash=696bed…1d07ed INFO [06-26|21:02:01.867] Allocated cache and file handles database=/home/milo/private-geth/data/00/geth/lightchaindata cache=16 handles=16 INFO [06-26|21:02:01.956] Writing custom genesis block INFO [06-26|21:02:01.956] Persisted trie from memory database nodes=0 size=0.00B time=3.013µs gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B INFO [06-26|21:02:01.957] Successfully wrote genesis state database=lightchaindata hash=696bed…1d07ed
3、啓動節點
1 geth --datadir ./data/00 --networkid 100 console 2 #100爲network id,1爲主網id,能夠選擇除1之外的隨機數
milo@milo-K46CB:~/private-geth$ tree 終端輸入tree,查看生成的目錄 . ├── data │ └── 00 │ ├── geth │ │ ├── chaindata │ │ │ ├── 000002.ldb │ │ │ ├── 000003.log │ │ │ ├── CURRENT │ │ │ ├── CURRENT.bak │ │ │ ├── LOCK │ │ │ ├── LOG │ │ │ └── MANIFEST-000004 │ │ ├── lightchaindata │ │ │ ├── 000001.log │ │ │ ├── CURRENT │ │ │ ├── LOCK │ │ │ ├── LOG │ │ │ └── MANIFEST-000000 │ │ ├── LOCK │ │ ├── nodekey │ │ ├── nodes │ │ │ ├── 000001.log │ │ │ ├── CURRENT │ │ │ ├── LOCK │ │ │ ├── LOG │ │ │ └── MANIFEST-000000 │ │ └── transactions.rlp │ ├── geth.ipc │ └── keystore │ └── UTC--2019-06-26T13-02-31.335901324Z--8830397771710ade101f0080f0da076181bad374 └── genesis.json
INFO [06-26|21:02:06.602] Maximum peer count ETH=25 LES=0 total=25 INFO [06-26|21:02:06.604] Starting peer-to-peer node instance=Geth/v1.8.27-stable-4bcc0a37/linux-amd64/go1.10.4 INFO [06-26|21:02:06.604] Allocated cache and file handles database=/home/milo/private-geth/data/00/geth/chaindata cache=512 handles=2048 INFO [06-26|21:02:06.792] Initialised chain configuration config="{ChainID: 15 Homestead: 0 DAO: <nil> DAOSupport: false EIP150: <nil> EIP155: 0 EIP158: 0 Byzantium: <nil> Constantinople: <nil> ConstantinopleFix: <nil> Engine: unknown}" INFO [06-26|21:02:06.792] Disk storage enabled for ethash caches dir=/home/milo/private-geth/data/00/geth/ethash count=3 INFO [06-26|21:02:06.792] Disk storage enabled for ethash DAGs dir=/home/milo/.ethash count=2 INFO [06-26|21:02:06.792] Initialising Ethereum protocol versions="[63 62]" network=100 INFO [06-26|21:02:06.863] Loaded most recent local header number=0 hash=696bed…1d07ed td=256 age=50y2mo1w INFO [06-26|21:02:06.863] Loaded most recent local full block number=0 hash=696bed…1d07ed td=256 age=50y2mo1w INFO [06-26|21:02:06.863] Loaded most recent local fast block number=0 hash=696bed…1d07ed td=256 age=50y2mo1w INFO [06-26|21:02:06.863] Regenerated local transaction journal transactions=0 accounts=0 INFO [06-26|21:02:07.009] New local node record seq=1 id=1b933112e1333f9a ip=127.0.0.1 udp=30303 tcp=30303 INFO [06-26|21:02:07.015] Started P2P networking self=enode://bfad31051f4ba04bf5bbb2fdd455bfcbf924479991b029a8604ab5abb386431b7c7f7d1edbba0bf6db66ad172b632ca45518239979634f190a0c26c0a9b9864b@127.0.0.1:30303 INFO [06-26|21:02:07.016] IPC endpoint opened url=/home/milo/private-geth/data/00/geth.ipc Welcome to the Geth JavaScript console! instance: Geth/v1.8.27-stable-4bcc0a37/linux-amd64/go1.10.4 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 >
出現這樣的日誌說明私有網絡就搭建成功了,下一步咱們來在這個私有網絡挖礦。
4、挖礦
要挖礦首先必需要有一個帳戶,根據如下命令。
> eth.accounts #查看node中的全部帳戶 [] ##輸出了一個「[]」,說明沒有帳戶 > personal.newAccount("123456") ##建立新帳戶,123456是帳戶密碼,通常會輸出如下結果 "0x8830397771710ade101f0080f0da076181bad374" > eth.accounts ["0x8830397771710ade101f0080f0da076181bad374"] 此時帳戶已經有了,咱們能夠進行挖礦了,輸入以ixa命令 > miner.start(3) ##miner.start()是啓動挖礦,3是啓動線程數,爲避免影響電腦性能,這裏咱們就用3線程
到這裏時會出現一些問題,通常來講,第一次啓動挖礦,應當會出現如下日誌
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 ##當percentage=100時,也就是加載到100%時,會開始挖礦,這個過程可能會比較慢,要耐心等待。 INFO [06-26|21:10:53.002] Successfully sealed new block number=1 sealhash=dac366…ad5c8d hash=3cb840…442e46 elapsed=8m9.375s INFO [06-26|21:10:53.042] 🔨 mined potential block number=1 hash=3cb840…442e46 INFO [06-26|21:10:53.103] Commit new mining work number=2 sealhash=088254…66c59d uncles=0 txs=0 gas=0 fees=0 elapsed=60.592ms INFO [06-26|21:10:53.906] Successfully sealed new block number=2 sealhash=088254…66c59d hash=446602…93d76b elapsed=803.002ms INFO [06-26|21:10:53.906] 🔨 mined potential block number=2 hash=446602…93d76b INFO [06-26|21:10:53.906] Commit new mining work number=3 sealhash=6c39db…66d2bf uncles=0 txs=0 gas=0 fees=0 elapsed=178.137µs INFO [06-26|21:10:54.406] Successfully sealed new block number=3 sealhash=6c39db…66d2bf hash=fa550b…95d423 elapsed=500.446ms INFO [06-26|21:10:54.407] 🔨 mined potential block number=3 hash=fa550b…95d423 ##這個就是表明挖到礦了
正常來說,第一次搭建私有鏈到這裏都沒什麼問題,而當我第二次開啓節點挖礦時出現了問題
> miner.start(3) INFO [06-26|21:02:43.616] Updated mining threads threads=3 INFO [06-26|21:02:43.626] Transaction pool price threshold updated price=1000000000 INFO [06-26|21:02:43.627] Etherbase automatically configured address=0x8830397771710ADE101f0080f0da076181Bad374 null > INFO [06-26|21:02:43.627] Commit new mining work number=1 sealhash=dac366…ad5c8d uncles=0 txs=0 gas=0 fees=0 elapsed=373.028µs ##當我再次輸入挖礦命令時,狀況陷入了僵局,此時會一直停留在Commit new mining work ##我在網上搜索過許多,大多沒有解決辦法,也沒有指出問題的緣由,反反覆覆試過不少次,自第一次搭建私有鏈後,後續的都一直是這樣。 ##因此我打算在另外一臺電腦上安裝虛擬機,從新再來一遍試試,而這個我也一直放着一直加載。 ##當我在通過一系列猛如虎的操做下,裝虛擬機,裝ethereum開發環境,終於也到了加載DGA的那一步 ##這時再看一直僵在Commit new mining work的電腦時,它忽然開始挖礦了。。。。。 ##因此到這個步驟時,並非什麼錯誤,咱們應當耐心等待 > INFO [06-26|21:02:43.627] Commit new mining work number=1 sealhash=dac366…ad5c8d uncles=0 txs=0 gas=0 fees=0 elapsed=373.028µs INFO [06-26|21:10:53.002] Successfully sealed new block number=1 sealhash=dac366…ad5c8d hash=3cb840…442e46 elapsed=8m9.375s INFO [06-26|21:10:53.042] 🔨 mined potential block number=1 hash=3cb840…442e46 INFO [06-26|21:10:53.103] Commit new mining work number=2 sealhash=088254…66c59d uncles=0 txs=0 gas=0 fees=0 elapsed=60.592ms INFO [06-26|21:10:53.906] Successfully sealed new block number=2 sealhash=088254…66c59d hash=446602…93d76b elapsed=803.002ms INFO [06-26|21:10:53.906] 🔨 mined potential block number=2 hash=446602…93d76b INFO [06-26|21:10:53.906] Commit new mining work number=3 sealhash=6c39db…66d2bf uncles=0 txs=0 gas=0 fees=0 elapsed=178.137µs INFO [06-26|21:10:54.406] Successfully sealed new block number=3 sealhash=6c39db…66d2bf hash=fa550b…95d423 elapsed=500.446ms INFO [06-26|21:10:54.407] 🔨 mined potential block number=3 hash=fa550b…95d423 INFO [06-26|21:10:54.457] Commit new mining work number=4 sealhash=f170b2…be929e uncles=0 txs=0 gas=0 fees=0 elapsed=50.737ms INFO [06-26|21:10:54.946] Successfully sealed new block number=4 sealhash=f170b2…be929e hash=190e69…4fbc8b elapsed=488.870ms
查看挖到的區塊
1 > miner.stop() 2 null 3 > eth.getBalance(eth.accounts[0]) 4 155000000000000000000 5 > eth.blockNumber 6 31
到這裏,一個基於以太坊的私有鏈就搭建完成了