iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 20
0
Blockchain

D30 Block Chain 系列 第 20

D20 以太坊的挖礦

創建個資料夾把私有鏈的資料放入其中:

 mkdir data

創建一個genesis.json文檔並將如下的配置資訊寫入其中,而檔案是放在你所創的資料夾中:

{
 "config": {
       "chainId": xxxx(修改任意數字,不能為0),
       "homesteadBlock": 0,
       "eip155Block": 0,
       "eip158Block": 0
   },
 "alloc": {},
 "coinbase" : "0x0000000000000000000000000000000000000000",
 "difficulty" : "0x20000"(可修改難度值),
 "extraData" : "",
 "gasLimit": "0x2fefd8",
 "nonce": "0x0000000000000042",
 "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
 "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
 "timestamp"  : "0x00"
}

初始化創世區塊,並指定把資料存放進去的資料夾相對路徑:

 geth --datadir "./data" --networkid 31415926 --rpc --rpccorsdomain "*" init ./genesis.json

啟動私有鏈節點,並將日誌輸入到xxx.log:

 geth –datadir "./data" --networkid 31415926 --rpc --rpccorsdomain "*" --nodiscover console 2>>xxx.log

便會進入到geth控制介面

創建新賬戶:

 >personal.newAccount("密碼")

創建成功將返回賬戶的地址

用一個名稱來代替納長串的地址:

 >user1=eth.accounts[0]

查看帳戶:

 >eth.accounts
 OR
 >web3.eth.getAccounts

查詢賬戶餘額:

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

開啟/停止挖礦,第一次開起需要等待一段時間:

 >miner.start(1) 表1個線程挖礦

 >miner.stop()

如果我們想要把挖到的礦存入其他帳戶:

 >miner.setEtherbase(eth.accounts[1])
true

也可以新開啟一個終端查看挖礦日誌:

 tail -f xxx.log

挖礦成功後,默認會將以太幣給第一個賬戶,查看餘額:

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

查看當前的區塊高度:

 >eth.blockNumber

查看想看的區塊:

 >eth.blockNumber(N)

需要解鎖accounts[0]才能轉帳 :

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

0 代表長時間解鎖(單位為毫秒)。

轉賬:

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

查詢accounts[1]餘額,默認單位是Wei:

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

可以使用web3.fromWei()命令將wei轉為ether:

 >web3.fromWei(eth.getBalance(eth.accounts[0]), 'ether')

1 ether = 10^18 wei

文章另會分享在stars blog中,歡迎一起交流。


上一篇
D19 以太坊的安裝環境
下一篇
D21 區塊鏈中的智能合約
系列文
D30 Block Chain 30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言