在作一些測試工做的時候, 爲了方便控制以及更快的進入真正的測試工做,可能須要搭建一個私有的以太坊網絡.html
而以太坊節點之間可以互相連接須要知足1)相同的協議版本2)相同的networkid,因此搭建私有網絡最方便的方法就是經過geth命令中的--networkid選項,設置一個與主網不一樣的networkid(主網的networkid爲1),這也是官方推薦的方法.node
下面開始創建私有以太坊網絡:git
mkdir private-geth cd private-geth
創建創世紀區塊文件,是一個json格式的文件: github
vim genesis.json
在創世紀區塊的json文件中填入如下內容, 並保存.json
{ "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目錄:vim
geth --datadir ./data/00 init genesis.json
啓動節點, 加上console 表示啓動後,啓用命令行:bash
geth --datadir ./data/00 --networkid 15 console
如今私有網絡就搭建成功,下面就能夠在這個剛剛搭建出來的私有以太坊網絡中執行挖礦操做了,網絡
挖礦首先必須有一個帳戶,輸入下面的命令,查看當前node中的全部帳戶:app
> 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
下一篇,我會講一講如何搭建 私有網絡節點羣:測試
參考文檔:
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/