以太坊--私有鏈--挖礦及轉裝

查看帳戶餘額   web

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

0區塊鏈

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

0線程

啓動&中止挖礦對象

    經過miner.start()來啓動挖礦get

> miner.start(1)
 ```hash

其中start的參數表示挖礦使用的線程數。第一次啓動挖礦會先生成挖礦所需的DAG文件,這個過程有點慢,等進度達到100%後,就會開始挖礦,此時屏幕會被挖礦信息刷屏。io

若是想中止挖礦,在js console中輸入miner.stop():console

> miner.stop()
注意:輸入的字符會被挖礦刷屏信息沖掉,沒有關係,只要輸入完整的miner.stop()以後回車,便可中止挖礦。

挖到一個區塊會獎勵5個以太幣,挖礦所得的獎勵會進入礦工的帳戶,這個帳戶叫作coinbase,默認狀況下coinbase是本地帳戶中的第一個帳戶:

> eth.coinbase
"0xc232e2add308136571bb8f9197ba4ae4e5ba9836"
如今的coinbase是帳戶0,要想使挖礦獎勵進入其餘帳戶,經過miner.setEtherbase()將其餘帳戶設置成coinbase便可:

> miner.setEtherbase(eth.accounts[1])
true
> eth.coinbase
"0x814d39aa21f3eed069f2b21da7b5f857f7343afa"
咱們仍是以帳戶0做爲coinbase,挖到區塊之後,帳戶0裏面應該就有餘額了:

> eth.getBalance(eth.accounts[0])
160000000000000000000
getBalance()返回值的單位是wei,wei是以太幣的最小單位,1個以太幣=10的18次方個wei。要查看有多少個以太幣,能夠用web3.fromWei()將返回值換算成以太幣:

> web3.fromWei(eth.getBalance(eth.accounts[0]),'ether')
160
4、發送交易

目前,帳戶一的餘額仍是0:

> eth.getBalance(eth.accounts[1])
0
能夠經過發送一筆交易,從帳戶0轉移5個以太幣到帳戶1:

> amount = web3.toWei(5,'ether')
"5000000000000000000"
> eth.sendTransaction({from:eth.accounts[0],to:eth.accounts[1],value:amount})
Error: account is locked
    at web3.js:3119:20
    at web3.js:6023:15
    at web3.js:4995:36
    at <anonymous>:1:1
這裏報錯了,緣由是帳戶每隔一段時間就會被鎖住,要發送交易,必須先解鎖帳戶,因爲咱們要從帳戶0發送交易,因此要解鎖帳戶0:

> personal.unlockAccount(eth.accounts[0])
Unlock account 0xc232e2add308136571bb8f9197ba4ae4e5ba9836
Passphrase: 
true
輸入建立帳戶時設置的密碼,就能夠成功解鎖帳戶。而後再發送交易:

> amount = web3.toWei(5,'ether')
"5000000000000000000"
> eth.sendTransaction({from:eth.accounts[0],to:eth.accounts[1],value:amount})
I0322 19:39:36.300675 internal/ethapi/api.go:1047] Tx(0x0c59f431068937cbe9e230483bc79f59bd7146edc8ff5ec37fea6710adcab825) to: 0x814d39aa21f3eed069f2b21da7b5f857f7343afa
"0x0c59f431068937cbe9e230483bc79f59bd7146edc8ff5ec37fea6710adcab825"
此時交易已經提交到區塊鏈,返回了交易的hash,但還未被處理,這能夠經過查看txpool來驗證:

> txpool.status
{
  pending: 1,
  queued: 0
}
其中有一條pending的交易,pending表示已提交但還未被處理的交易。

要使交易被處理,必需要挖礦。這裏咱們啓動挖礦,而後等待挖到一個區塊以後就中止挖礦:

> miner.start(1);admin.sleepBlocks(1);miner.stop();
當miner.stop()返回true後,txpool中pending的交易數量應該爲0了,說明交易已經被處理了:

> txpool.status
{
  pending: 0,
  queued: 0
}
此時,交易已經生效,帳戶一應該已經收到了5個以太幣了:

> web3.fromWei(eth.getBalance(eth.accounts[1]),'ether')
5
5、查看交易和區塊

eth對象封裝了查看交易和區塊信息的方法。

查看當前區塊總數:

> eth.blockNumber 33 經過交易hash查看交易:

相關文章
相關標籤/搜索