以太坊學習 一) 利用Geth搭建私有鏈

2021-08-08 03:24:42 字數 3140 閱讀 1833

在以太坊的多種語言實現的客戶端中,推薦使用go語言版本的,也是使用最廣的乙個版本go-ethereum。geth是go-ethereum的縮寫。

本次開發平台為ubuntu 16.04版本64位的虛擬機器。

一、安裝geth

安裝文件:

ubuntu下的安裝:

sudo add-apt-repository -y ppa:ethereum/ethereum
sudo apt-get update
sudo apt-get install ethereum
安裝結束可以輸入 geth version 檢視版本資訊。

本次安裝使用的geth version: 1.6.7-stable

二、建立乙個新目錄和新增創世塊檔案genesis.json

利用mkdir 建立乙個新的目錄,進入目錄,建立檔案genesis.json 。檔案內容如下: ,

"difficulty": "10000",

"gaslimit": "2100000",

"alloc": ,

"f41c74c9ae680c1aa78f42e5647a62f353b7bdde": }}

這是官方給的乙個示例檔案。技術更新較快,不知道過多久,這個配置也許就不行了。

三、執行命令,建立創世塊

geth --datadir "./" init genesis.json
geth中的命令較多,只提到部分要使用的引數。其餘請讀者自己檢視文件。

datadir 指定資料儲存的路徑

init 初始化創世節點

此時當前目錄下面會新增出兩個資料夾geth和keystore

geth         中儲存的是區塊鏈的相關資料

keystore  中儲存的是該鏈條中的使用者資訊

@ubuntu:~/share/testchain$ geth --datadir "./" init genesis.json

warn [09-12|22:28:10] no etherbase set and no accounts found as default

info [09-12|22:28:10] allocated cache and file handles database=/home/lqp/share/testchain/geth/chaindata cache=16 handles=16

info [09-12|22:28:10] writing custom genesis block

info [09-12|22:28:10] successfully wrote genesis state database=chaindata hash=416ae0…aaffbd

info [09-12|22:28:10] allocated cache and file handles database=/home/lqp/share/testchain/geth/lightchaindata cache=16 handles=16

info [09-12|22:28:10] writing custom genesis block

info [09-12|22:28:10] successfully wrote genesis state database=lightchaindata hash=416ae0…aaffbd

四、執行命令,建立自己的私有鏈

geth --datadir "./" --nodiscover console 2>>geth.log
該命令是最簡單的生成方式,

nodiscover是指不被其他節點自動發現(可以手動新增節點),

console 是啟動geth 控制台,不加該選項,geth啟動之後成為乙個後台程序不會自動結束,

最後把geth中的log資訊匯入geth.log中,方便了解geth執行的情況。

如果想在同一臺物理機上面生成不同的節點,一起構建乙個私有鏈,不同的節點只需在第四步建立私有鏈的時候,不同的節點指定不同的埠:

geth --identity "phoenix"  --rpc  --rpccorsdomain "*" --datadir "./" --port "30303"  --rpcapi "db,eth,net,web3" --networkid 98888 console 2>>geth.log

通過admin.nodeinfo 獲取節點資訊,已經通過手動admin.addpeer()新增節點,也可以通過建立靜態節點檔案,手動新增。

注意:請勿直接執行 geth 命令,因為會自動連入生產網路,會自動同步公有鏈中的幾十萬的節點資訊。會顯示的比較凌亂。

在私有鏈執行的時候,會生成乙個geth.ipc的檔案(停止執行就消失),此檔案是以太坊錢包mist鏈結私有鏈時候的介面。

五、新開乙個終端,輸出geth.log日誌資訊

開啟另乙個終端,找到geth.log的所在目錄,執行命令

tail -f geth.log從而持續的輸出以太坊的日誌

六、建立新的賬戶

介紹幾個常用的命令:

eth.accounts 以,為界限列舉出所有賬戶的位址(公匙);

personal.newaccount("***") 新建立乙個賬戶,***為密碼,會加密生成乙個私匙;

personal.unlockaccount("address")解鎖乙個位址,任何涉及貨幣交易的事件都需要先解鎖賬戶,要輸入密碼。

更多命令請參考 geth help 和 web3.js介面

利用personal.newaccount("***")生成乙個使用者,重複執行可以生成多個賬號對應eth.accounts[0..n]。

七、開始挖礦

以太坊私鏈搭建

搭建私有鏈官方文件 network id 隔離網路 ethereum options networkid value network identifier integer,1 frontier,2 morden disused 3 ropsten,4 rinkeby default 1 networ...

以太坊學習 geth相關開發

一 原始碼安裝 1 從github中獲取相應的go ethereum作為乙太網客戶端 git clone2 構建geth,切換到源 目錄使用make命令 cd go ethereum make geth3 在build bin中執行geth version檢視返回,確定安裝成功 root mike ...

搭建一條以太坊私鏈

建立乙個json檔案,放到你想要放的位置,內容如下 difficulty 200000000 gaslimit 2100000 alloc f41c74c9ae680c1aa78f42e5647a62f353b7bdde 我在這裡建立了乙個叫test genesis.json的檔案,內容就是上面的 ...