Ubuntu 16.04 快速搭建以太坊私有鏈開(kāi)發(fā)環(huán)境

之前用的是CENTOS7,后邊發(fā)現(xiàn)有好多搞不定的小坑,果斷換用ubuntu繼續(xù)學(xué)習(xí)。

環(huán)境:ubuntu16.04

另外跑挖礦1H1G是不行的,至少要2G

安裝git

之后的安裝都需要依賴Git

sudo add-apt-repository ppa:git-core/ppa

sudo apt-get update

sudo apt-get install git

查看版本號(hào)

git --version

git version 2.18.0

1.安裝go語(yǔ)言環(huán)境

(1)下載linux下go的安裝包

$ wget https://studygolang.com/dl/golang/go1.10.3.linux-amd64.tar.gz

(2)下載完后,進(jìn)入到下載目錄,將其解壓到 /usr/local 文件夾下并在$HOME下新建一個(gè)文件夾go:

$ sudo tar -C /usr/local -xzf go1.10.3.linux-amd64.tar.gz

$ mkdir go

?? (3)配置環(huán)境變量

??? 使用如下命令打開(kāi)環(huán)境變量配置文件

$ sudo vim ~/.bashrc

????將下列代碼復(fù)制粘貼到文件最后,wq命令保存退出

export GOROOT=/usr/local/go

export GOBIN=$GOROOT/bin

export GOPATH=$HOME/go

export PATH=$PATH:$GOBIN

????輸入如下命令使環(huán)境變量生效(注意:不用加sudo)

$ source ~/.bashrc

????至此,go語(yǔ)言環(huán)境已經(jīng)安裝完成,輸入go version查看安裝是否成功

$ go version

go version go1.10.3 linux/amd64

2.安裝以太坊客戶端Ethereum

????使用如下命令進(jìn)行安裝

$ sudo apt-get install software-properties-common

$ sudo add-apt-repository -y ppa:ethereum/ethereum

$ sudo apt-get update

$ sudo apt-get install ethereum

????安裝完后,輸入geth version查看安裝是否成功

$ geth version

Geth Version: 1.8.11-stable

3.以太坊私有鏈搭建

(1)新建一個(gè)文件夾,例如mychain,用來(lái)保存私有鏈的數(shù)據(jù)。創(chuàng)建一個(gè)初始化的配置文件,genesis.json

$ mkdir mychain

$ vim genesis.json

然后將如下的配置信息寫(xiě)入到genesis.json文件中(注意:chainId不能為0)

{

? "config": {

? ? ? ? "chainId": 3,

? ? ? ? "homesteadBlock": 0,

? ? ? ? "eip155Block": 0,

? ? ? ? "eip158Block": 0

? ? },

? "alloc"? ? ? : {},

? "coinbase"? : "0x0000000000000000000000000000000000000000",

? "difficulty" : "0x200",

? "extraData"? : "",

? "gasLimit"? : "0x2fefd8",

? "nonce"? ? ? : "0x0000000000000042",

? "mixhash"? ? : "0x0000000000000000000000000000000000000000000000000000000000000000",

? "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",

? "timestamp"? : "0x00"

}

(2)初始化創(chuàng)世區(qū)塊,并指定數(shù)據(jù)存放路徑

$ geth --datadir mychain init genesis.json

(3)啟動(dòng)私有鏈節(jié)點(diǎn),并將日志輸入到mychain.log

$ geth --networkid 3 --datadir mychain --identity "mychain" --rpc --rpcaddr 0.0.0.0 --rpcport 8545 --rpcapi "admin,debug,eth,miner,net,personal,shh,txpool,web3" --port 30303 --rpccorsdomain "*" --nodiscover console 2>>mychain.log

具體參數(shù)含義請(qǐng)參考這個(gè)博客:geth命令

啟動(dòng)成功后進(jìn)入控制臺(tái)界面,如下所示:

Welcome to the Geth JavaScript console!

instance: Geth/mychain/v1.8.11-stable-dea1ce05/linux-amd64/go1.10

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

>

????至此,ubuntu 16.04 下以太坊私有鏈環(huán)境已經(jīng)搭好,接下在進(jìn)行簡(jiǎn)單測(cè)試。

4.測(cè)試

(1)創(chuàng)建新賬戶

personal.newAccount("密碼")

????創(chuàng)建成功將返回賬戶的地址

> personal.newAccount("123")

"0xe6319357003ff9889b7a849f5ec66b2903d08289"

> user1=eth.accounts[0]

"0x3e46c05151dcce7c994aaeb6f7366fc39ddfc694"

>user2=eth.accounts[1]

"0x611b1c6a1a859da53ed6154aeeec797ea03b9289"

(2)查詢賬戶余額

>eth.getBalance("賬戶地址")

0

>eth.getBalance(user1)? //兩種查詢方式都可以

0

? ? 由于新建賬戶,沒(méi)有余額,所以查詢的結(jié)果為 0

> eth.getBalance("0xe6319357003ff9889b7a849f5ec66b2903d08289")

0

(3)開(kāi)啟/停止挖礦,第一次開(kāi)起需要等待一段時(shí)間。

miner.start()

第一次要等一陣時(shí)間,大約5分鐘左右

開(kāi)始都如第一行所示,出現(xiàn)加粗則是挖礦成功了

INFO [07-26|18:15:21.550] Generating DAG in progress? ? ? ? ? ? ? epoch=1 percentage=16 elapsed=3m25.200s

INFO [07-26|18:15:33.338] Generating DAG in progress? ? ? ? ? ? ? epoch=1 percentage=17 elapsed=3m36.988s

INFO [07-26|18:15:45.307] Generating DAG in progress? ? ? ? ? ? ? epoch=1 percentage=18 elapsed=3m48.956s

INFO [07-26|18:15:57.158] Generating DAG in progress? ? ? ? ? ? ? epoch=1 percentage=19 elapsed=4m0.808s

INFO [07-26|18:15:59.828] Successfully sealed new block? ? ? ? ? ? number=30 hash=2b5e96…217cae

INFO [07-26|18:15:59.828]? block reached canonical chain? ? ? ? ? number=25 hash=37bbe5…bcba21

INFO [07-26|18:15:59.829]? mined potential block? ? ? ? ? ? ? ? ? number=30 hash=2b5e96…217cae

INFO [07-26|18:16:20.992] Generating DAG in progress epoch=1 percentage=21 elapsed=4m24.642s

INFO [07-26|18:16:28.468] Successfully sealed new block? ? ? ? ? ? number=34 hash=bac86c…464986

INFO [07-26|18:16:28.468]? block reached canonical chain? ? ? ? ? number=29 hash=d874de…8cfc4c

INFO [07-26|18:16:28.469]? mined potential block? ? ? ? ? ? ? ? ? number=34 hash=bac86c…464986

INFO [07-26|18:16:28.489] Commit new mining work? ? ? ? ? ? ? ? ? number=35 txs=0 uncles=0 elapsed=176.283μs

INFO [07-26|18:16:28.541] Successfully sealed new block? ? ? ? ? ? number=35 hash=421888…c52d31

INFO [07-26|18:16:28.541]? block reached canonical chain? ? ? ? ? number=30 hash=2b5e96…217cae

INFO [07-26|18:16:28.542]? mined potential block? ? ? ? ? ? ? ? ? number=35 hash=421888…c52d31

INFO [07-26|18:16:28.543] Commit new mining work? ? ? ? ? ? ? ? ? number=36 txs=0 uncles=0 elapsed=500.117μs

INFO [07-26|18:16:33.099] Generating DAG in progress? ? ? ? ? ? ? epoch=1 percentage=22 elapsed=4m36.749s

INFO [07-26|18:16:34.837] Successfully sealed new block? ? ? ? ? ? number=36 hash=f9ec16…7feafa

INFO [07-26|18:16:34.838]? block reached canonical chain? ? ? ? ? number=31 hash=0ef145…b841bb

INFO [07-26|18:16:34.838]? mined potential block? ? ? ? ? ? ? ? ? number=36 hash=f9ec16…7feafa

INFO [07-26|18:16:34.840] Commit new mining work? ? ? ? ? ? ? ? ? number=37 txs=0 uncles=0 elapsed=498.032μs

INFO [07-26|18:16:40.194] Successfully sealed new block? ? ? ? ? ? number=37 hash=549c29…93f046


miner.stop()

????查看挖礦日志,可以新開(kāi)啟一個(gè)終端,輸入下面命令進(jìn)行查看

$ tail -f mychain.log

(4)轉(zhuǎn)賬

????挖礦成功后,默認(rèn)會(huì)將以太幣給第一個(gè)賬戶,查看余額

>eth.getBalance(eth.accounts[0])

135000000000000000000

????eth.accounts:以數(shù)組形式返回為賬戶列表,當(dāng)前只有一個(gè)賬戶,可用eth.accounts[0]表示

????新建一個(gè)賬戶,查看余額

>personal.newAccount("111")

"0x866e46b71923d54e6fee26f42ececc216ca76c1d"

>eth.getBalance(eth.accounts[1])

0

????從accounts[0]向accounts[1]發(fā)送1個(gè)以太幣,首先需要解鎖accounts[0],使用如下命令

>personal.unlockAccount(eth.accounts[0],"123",0)

true

????三個(gè)參數(shù)分別為accounts[0]的地址,創(chuàng)建時(shí)的密碼及解鎖時(shí)間(毫秒為單位)。0 代表長(zhǎng)時(shí)間解鎖

轉(zhuǎn)賬操作

>eth.sendTransaction({from:eth.accounts[0],to:eth.accounts[1],value:web3.toWei(1,'ether')})

????開(kāi)啟挖礦等待區(qū)塊驗(yàn)證,轉(zhuǎn)賬成功后,查詢accounts[1]余額,默認(rèn)單位是Wei

>eth.getBalance(eth.accounts[1])

100000000000000000

可以使用web3.fromWei()命令將wei轉(zhuǎn)為ether。

轉(zhuǎn)自:https://blog.csdn.net/qq_27317475/article/details/80882034

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容