之前用的是CENTOS7,後邊發現有好多搞不定的小坑,果斷換用ubuntu繼續學習。
環境:ubuntu16.04
安裝git
之後的安裝都需要依賴Git
sudo add-apt-repository ppa:git-core/ppa
sudo apt-get update
sudo apt-get install git
查看版本號碼
git--version
git version 2.18.0
1.安裝go語言環境
(1)下載linux下go的安裝包
$ wget https://studygolang.com/dl/golang/go1.10.3.linux-amd64.tar.gz
(2)下載完後,進入到下載目錄,將其解壓到 /usr/local 檔案夾下並在$HOME下建立一個檔案夾go:
$ sudo tar -C /usr/local -xzf go1.10.3.linux-amd64.tar.gz
$ mkdir go
(3)配置環境變數
使用如下命令開啟環境變數設定檔
$ sudo vim ~/.bashrc
將下列代碼複製粘貼到檔案最後,wq命令儲存退出
export GOROOT=/usr/local/go
export GOBIN=$GOROOT/bin
export GOPATH=$HOME/go
export PATH=$PATH:$GOBIN
輸入如下命令使環境變數生效(注意:不用加sudo)
$ source ~/.bashrc
至此,go語言環境已經安裝完成,輸入go version查看安裝是否成功
$ go version
go version go1.10.3 linux/amd64
2.安裝以太坊用戶端Ethereum
使用如下命令進行安裝
$ 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)建立一個檔案夾,例如mychain,用來儲存私人鏈的資料。建立一個初始化的設定檔,genesis.json
$ mkdir mychain
$ vim genesis.json
然後將如下的配置資訊寫入到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)初始化創世區塊,並指定資料存放路徑
$ geth --datadir Mychain init genesis.json
(3)啟動私人鏈節點,並將日誌輸入到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
具體參數含義請參考這個部落格:geth命令
啟動成功後進入控制台介面,如下所示:
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 下以太坊私人鏈環境已經搭好,接下在進行簡單測試。
4.測試
(1)建立新賬戶
personal.newAccount("密碼")
建立成功將返回賬戶的地址
> personal.newAccount("123")
"0xe6319357003ff9889b7a849f5ec66b2903d08289"
> user1=eth.accounts[0]
"0x3e46c05151dcce7c994aaeb6f7366fc39ddfc694"
>user2=eth.accounts[1]
"0x611b1c6a1a859da53ed6154aeeec797ea03b9289"
(2)查詢賬戶餘額
>eth.getBalance("賬戶地址")
0
>eth.getBalance(user1) //兩種查詢方式都可以
0
由於建立賬戶,沒有餘額,所以查詢的結果為 0
> eth.getBalance("0xe6319357003ff9889b7a849f5ec66b2903d08289")
0
(3)開啟/停止挖礦,第一次開起需要等待一段時間。
miner.start()
第一次要等一陣時間,大約5分鐘左右
開始都如第一行所示,出現加粗則是挖礦成功了
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()
查看挖礦日誌,可以新開啟一個終端,輸入下面命令進行查看
$ tail -f mychain.log
(4)轉賬
挖礦成功後,預設會將以太幣給第一個賬戶,查看餘額
>eth.getBalance(eth.accounts[0])
135000000000000000000
eth.accounts:以數組形式返回為賬戶列表,當前只有一個賬戶,可用eth.accounts[0]表示
建立一個賬戶,查看餘額
>personal.newAccount("111")
"0x866e46b71923d54e6fee26f42ececc216ca76c1d"
>eth.getBalance(eth.accounts[1])
0
從accounts[0]向accounts[1]發送1個以太幣,首先需要解鎖accounts[0],使用如下命令
>personal.unlockAccount(eth.accounts[0],"123",0)
true
三個參數分別為accounts[0]的地址,建立時的密碼及解鎖時間(毫秒為單位)。0 代表長時間解鎖
轉賬操作
>eth.sendTransaction({from:eth.accounts[0],to:eth.accounts[1],value:web3.toWei(1,'ether')})
開啟挖礦等待區塊驗證,轉賬成功後,查詢accounts[1]餘額,預設單位是Wei
>eth.getBalance(eth.accounts[1])
100000000000000000
可以使用web3.fromWei()命令將wei轉為ether。
轉自:https://blog.csdn.net/qq_27317475/article/details/80882034