區塊鏈入門(2):搭建以太坊私人鏈(private network of ethereum),以及挖礦的操作..

來源:互聯網
上載者:User

標籤:tle   accounts   ted   line   設定   檔案中   cal   com   UI   

在做一些測試工作的時候, 為了方便控制以及更快的進入真正的測試工作,可能需要搭建一個私人的以太坊網路.

而以太坊節點之間能夠互相連結需要滿足1)相同的協議版本2)相同的networkid,所以搭建私人網路最方便的方法就是通過geth命令中的--networkid選項,設定一個與主網不同的networkid(主網的networkid為1),這也是官方推薦的方法.

下面開始建立私人以太坊網路:

mkdir private-geth

cd private-geth

建立創世紀區塊檔案,是一個json格式的檔案:

vim genesis.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目錄:

geth --datadir ./data/00 init genesis.json

啟動節點, 加上console 表示啟動後,啟用命令列:

geth --datadir ./data/00 --networkid 15 console

 

 在有一個賬戶之前,什麼事情都不能幹....,先繼續在命令列中輸入命令,查看當前節點中的賬戶:

> 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.715s

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

 

下一篇,我會講一講如何搭建 私人網路節點群:

Setting up private network or local cluster

參考文檔:

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/

區塊鏈入門(2):搭建以太坊私人鏈(private network of ethereum),以及挖礦的操作..

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.