如何使用開源工具建立一個私有的以太坊區塊鏈,瞭解區塊鏈技術可以增加價值的一些市場和行業。
在第一部分中,我花了很多時間探索加密貨幣以及使其成為可能的機制:區塊鏈。我詳細介紹了一下關於區塊鏈如何工作以及為什麼它如此安全和強大。在第二部分中,我將介紹如何使用開源工具設定和配置您自己的私有以太坊區塊鏈。我也看在這個技術可以帶來一些價值或幫助重新定義人們的方式在更開放的網路上進行交易。建立您自己的私有區塊鏈網路在本節中,我將探討基於以太坊的區塊鏈網路機制 - 具體來說,如何建立一個私有的以太坊區塊鏈,一個託管和共享此區塊鏈的私人網路,一個帳戶,然後如何做一些有趣的事情。什麼是以太坊?以太坊是一個開源和公共區塊鏈具有智慧合約(即指令碼)功能的平臺。它類似於比特幣,但不同之處在於它超越了貨幣交易。智慧合約是用程式語言編寫的,例如Solidity(類似於C和JavaScript),Serpent(類似於Python),LLL(a類似Lisp的語言)和Mutan(Go-based)。編制智慧合約進入EVM(見下文)位元組碼並部署在以太坊區塊鏈中執行。智慧合約有助於交換金錢,財產,股票或任何有價值的東西,它在透明和無衝突的情況下這樣做避開傳統中間人的方式。如果你還記得第一部分,任何一個典型的佈局區塊鏈是所有節點都連線到每個其他節點的區塊鏈,建立一個網格。在以太坊的世界中,這些節點被引用作為以太坊虛擬機器(EVM),每個EVM將託管一份副本整個區塊鏈。每個EVM也將競爭下一個阻止或驗證交易。一旦新塊被附加到區塊鏈,更新傳播到整個網路,這樣每個節點都是同步的。要成為以太坊網路上的EVM節點,您需要下載並安裝適當的軟體。要做到這一點,你會使用Geth(Go Ethereum)。 Geth是Go的官方實施以太坊協議。它是三個這樣的實現工具之一;另外兩個是用C ++和Python編寫的。這些開源軟體軟體包根據GNU寬通用公共許可證(LGPL)獲得第三個許可版本.所有人的獨立Geth客戶端軟體包支援的作業系統和體系結構(包括Linux)可在這裡使用,軟體包的原始碼託管在GitHub上。Geth是一個用於通訊的命令列介面(CLI)工具與以太坊網路。它旨在充當您的計算機和以太坊網路中的所有其他節點。當一塊正在由網路上的另一個節點,即Geth安裝中挖礦將通知更新,然後將資訊傳遞給更新區塊鏈的本地副本。有了Geth實用程式,你就可以了能夠挖掘以太幣(類似於比特幣,但以太幣可以在兩個地址之間轉移資金,創造智慧合約等等。下載和安裝在我的示例中,我正在配置此以太坊區塊鏈最新的LTS版本的Ubuntu。 請注意,工具本身是不限於此分發或釋出。從專案網站下載和安裝二進位制檔案下載最新的穩定版本,將其解壓縮並複製到適當的版本目錄:
$ wget https://gethstore.blob.core.windows.net/builds/ ↪geth-linux-amd64-1.7.3-4bb3c89d.tar.gz $ tar xzf geth-linux-amd64-1.7.3-4bb3c89d.tar.gz $ cd geth-linux-amd64-1.7.3-4bb3c89d/ $ sudo cp geth /usr/bin/ |
$ sudo apt-get install -y build-essential golang |
$ make geth |
從公共儲存庫安裝
如果你在Ubuntu上執行並決定從一個安裝包
公共儲存庫,執行以下命令:
$ sudo apt-get install software-properties-common $ sudo add-apt-repository -y ppa:ethereum/ethereum $ sudo apt-get update $ sudo apt-get install ethereum |
$ mkdir ~/eth-evm $ cd ~/eth-evm $ mkdir config data $ cd config |
{ "config": { "chainId": 999, "homesteadBlock": 0, "eip155Block": 0, "eip158Block": 0 }, "difficulty": "0x400", "gasLimit": "0x8000000", "alloc": {} } |
$ geth --datadir /home/petros/eth-evm/data/PrivateBlockchain ↪init /home/petros/eth-evm/config/Genesis.json WARN [02-10|15:11:41] No etherbase set and no accounts found ↪as default INFO [02-10|15:11:41] Allocated cache and file handles ↪database=/home/petros/eth-evm/data/PrivateBlockchain/ ↪geth/chaindata cache=16 handles=16 INFO [02-10|15:11:41] Writing custom genesis block INFO [02-10|15:11:41] Successfully wrote genesis state ↪database=chaindata hash=d1a12d...4c8725 INFO [02-10|15:11:41] Allocated cache and file handles ↪database=/home/petros/eth-evm/data/PrivateBlockchain/ ↪geth/lightchaindata cache=16 handles=16 INFO [02-10|15:11:41] Writing custom genesis block INFO [02-10|15:11:41] Successfully wrote genesis state ↪database=lightchaindata |
$ ls -R /home/petros/eth-evm/ .: config data ./config: Genesis.json ./data: PrivateBlockchain ./data/PrivateBlockchain: geth keystore ./data/PrivateBlockchain/geth: chaindata lightchaindata LOCK nodekey nodes transactions.rlp ./data/PrivateBlockchain/geth/chaindata: 000002.ldb 000003.log CURRENT LOCK LOG MANIFEST-000004 ./data/PrivateBlockchain/geth/lightchaindata: 000001.log CURRENT LOCK LOG MANIFEST-000000 ./data/PrivateBlockchain/geth/nodes: 000001.log CURRENT LOCK LOG MANIFEST-000000 ./data/PrivateBlockchain/keystore: |
admin@chaindaily:~/eth-evm$ geth --datadir ↪/home/petros/eth-evm/data/PrivateBlockchain --networkid 9999 WARN [02-10|15:11:59] No etherbase set and no accounts found ↪as default INFO [02-10|15:11:59] Starting peer-to-peer node ↪instance=Geth/v1.7.3-stable-4bb3c89d/linux-amd64/go1.9.2 INFO [02-10|15:11:59] Allocated cache and file handles ↪database=/home/petros/eth-evm/data/PrivateBlockchain/ ↪geth/chaindata cache=128 handles=1024 WARN [02-10|15:11:59] Upgrading database to use lookup entries INFO [02-10|15:11:59] Initialised chain configuration ↪config="{ChainID: 999 Homestead: 0 DAO: <nil> DAOSupport: ↪false EIP150: <nil> EIP155: 0 EIP158: 0 Byzantium: <nil> ↪Engine: unknown}" INFO [02-10|15:11:59] Disk storage enabled for ethash caches ↪dir=/home/petros/eth-evm/data/PrivateBlockchain/ ↪geth/ethash count=3 INFO [02-10|15:11:59] Disk storage enabled for ethash DAGs ↪dir=/home/petros/.ethash count=2 INFO [02-10|15:11:59] Initialising Ethereum protocol ↪versions="[63 62]" network=9999 INFO [02-10|15:11:59] Database deduplication successful ↪deduped=0 INFO [02-10|15:11:59] Loaded most recent local header ↪number=0 hash=d1a12d...4c8725 td=1024 INFO [02-10|15:11:59] Loaded most recent local full block ↪number=0 hash=d1a12d...4c8725 td=1024 INFO [02-10|15:11:59] Loaded most recent local fast block ↪number=0 hash=d1a12d...4c8725 td=1024 INFO [02-10|15:11:59] Regenerated local transaction journal ↪transactions=0 accounts=0 INFO [02-10|15:11:59] Starting P2P networking INFO [02-10|15:12:01] UDP listener up ↪self=enode://f51957cd4441f19d187f9601541d66dcbaf560 ↪770d3da1db4e71ce5ba3ebc66e60ffe73c2ff01ae683be0527b77c0f96 ↪admin@chaindaily[::]:30303 INFO [02-10|15:12:01] IPC endpoint opened: /home/petros/eth-evm/ ↪data/PrivateBlockchain/geth.ipc INFO [02-10|15:12:01] RLPx listener up ↪self=enode://f51957cd4441f19d187f9601541d66dcbaf560 ↪770d3da1db4e71ce5ba3ebc66e60ffe73c2ff01ae683be0527b77c0f96 ↪admin@chaindaily[::]:30303 |
$ geth --datadir /home/petros/eth-evm/data/PrivateBlockchain ↪init /home/petros/eth-evm/config/Genesis.json $ geth --datadir /home/petros/eth-evm/data/PrivateBlockchain ↪--networkid 9999 |
配置使用者帳戶
那麼,既然您的私有區塊鏈網路正在執行,您就可以開始與它互動。 但為了做到這一點你需要附加執行Geth流程。 開啟第二個終端視窗。該以下命令將附加到第一個執行的例項終端視窗並帶您進入JavaScript控制檯:
$ geth attach /home/petros/eth-evm/data/PrivateBlockchain/geth.ipc Welcome to the Geth JavaScript console! instance: Geth/v1.7.3-stable-4bb3c89d/linux-amd64/go1.9.2 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 > |
> personal.newAccount() Passphrase: Repeat passphrase: "0x92619f0bf91c9a786b8e7570cc538995b163652d" |
> eth.coinbase "0x92619f0bf91c9a786b8e7570cc538995b163652d" |
> eth.getBalance("0x92619f0bf91c9a786b8e7570cc538995b163652d") 0 |
> eth.getBalance(eth.coinbase) 0 |
> miner.start() Null |
INFO [02-10|15:14:47] Updated mining threads ↪threads=0 INFO [02-10|15:14:47] Transaction pool price threshold ↪updated price=18000000000 INFO [02-10|15:14:47] Starting mining operation INFO [02-10|15:14:47] Commit new mining work ↪number=1 txs=0 uncles=0 elapsed=186.855us INFO [02-10|15:14:57] Generating DAG in progress ↪epoch=1 percentage=0 elapsed=7.083s INFO [02-10|15:14:59] Successfully sealed new block ↪number=1 hash=c81539...dc9691 INFO [02-10|15:14:59] mined potential block ↪number=1 hash=c81539...dc9691 INFO [02-10|15:14:59] Commit new mining work ↪number=2 txs=0 uncles=0 elapsed=211.208us INFO [02-10|15:15:04] Generating DAG in progress ↪epoch=1 percentage=1 elapsed=13.690s INFO [02-10|15:15:06] Successfully sealed new block ↪number=2 hash=d26dda...e3b26c INFO [02-10|15:15:06] mined potential block ↪number=2 hash=d26dda...e3b26c INFO [02-10|15:15:06] Commit new mining work ↪number=3 txs=0 uncles=0 elapsed=510.357us [ ... ] INFO [02-10|15:15:52] Generating DAG in progress ↪epoch=1 percentage=8 elapsed=1m2.166s INFO [02-10|15:15:55] Successfully sealed new block ↪number=15 hash=d7979f...e89610 INFO [02-10|15:15:55] block reached canonical chain ↪number=10 hash=aedd46...913b66 INFO [02-10|15:15:55] mined potential block ↪number=15 hash=d7979f...e89610 INFO [02-10|15:15:55] Commit new mining work ↪number=16 txs=0 uncles=0 elapsed=105.111us INFO [02-10|15:15:57] Successfully sealed new block ↪number=16 hash=61cf68...b16bf2 INFO [02-10|15:15:57] block reached canonical chain ↪number=11 hash=6b89ff...de8f88 INFO [02-10|15:15:57] mined potential block ↪number=16 hash=61cf68...b16bf2 INFO [02-10|15:15:57] Commit new mining work ↪number=17 txs=0 uncles=0 elapsed=147.31us |
回到第二個終端視窗等待10-20秒,然後在JavaScript控制檯上開始檢查餘額:
> eth.getBalance(eth.coinbase) 10000000000000000000 |
> eth.getBalance(eth.coinbase) 75000000000000000000 |
> miner.stop() True |