CPU挖掘
你可以用電腦的中央處理器(CPU)挖以太幣。自從GPU礦工的效率高出兩個數量級,它就不再盈利了。然而你可以用CPU挖掘在Morden測試網或私有鏈上挖礦,以便創建你測試合約和交易所需要的以太幣, 而無需花費實時網絡上的真實以太幣。
注意:測試網以太幣除了用於測試目的外沒有其他價值。
使用geth
用geth啟動以太坊節點時,並不是默認挖掘。在CPU挖掘模式開啟,你會用—mine命令行選項。-minerthreads參數可以用於設置平行於挖掘線程的數量(默認為處理器核心的總數)。
geth --mine --minerthreads=4
你也可以在執行期間用控制台開啟或停止CPU挖掘。miner.start取一個礦工線程數量的可選參數。
> miner.start(8) true > miner.stop() true
注意挖掘真實以太幣只有在你與網絡同步時才有意義(由於你是在共識區塊頂部挖礦)。因此以太區塊鏈下載器/同步器會延遲挖掘直到同步完成,此后挖掘自動開始,除非你用miner.stop()取消挖礦。
為了賺取以太幣,你必須有etherbase(或coinbase)地址集。這個etherbase默認為你的第一個賬戶。如果你沒有etherbase地址,geth –mine就不會開啟。
你可以在命令行重新設置etherbase:
geth --etherbase 1 --mine 2>> geth.log // 1 is index: second account by creation order OR geth --etherbase '0xa4d8e9cae4d04b093aac82e6cd355b6b963fb7ff' --mine 2>> geth.log
你也可以在控制台重新設置etherbase:
miner.setEtherbase(eth.accounts[2])
注意你的etherbase不必是本地賬戶地址,只要是現存的就可以。
有一個給你挖掘過的區塊添加額外數據的選項(只有32字節)。按照慣例,它被解釋為統一碼字符串,你可以設置短期虛榮標簽。
miner.setExtra("ΞTHΞSPHΞΞ") ... debug.printBlock(131805) BLOCK(be465b020fdbedc4063756f0912b5a89bbb4735bd1d1df84363e05ade0195cb1): Size: 531.00 B TD: 643485290485 { NoNonce: ee48752c3a0bfe3d85339451a5f3f411c21c8170353e450985e1faab0a9ac4cc Header: [ ... Coinbase: a4d8e9cae4d04b093aac82e6cd355b6b963fb7ff Number: 131805 Extra: ΞTHΞSPHΞΞ ... }
你可以用miner.hashrate檢查散表率,結果用H/s表示(每秒散表操作)。
> miner.hashrate 712000
成功挖掘一些區塊以后,你可以檢查etherbase賬戶中的以太幣余額。現在假定你的etherbase是個本地賬戶:
> eth.getBalance(eth.coinbase).toNumber(); '34698870000000'
為了花費你賺的gas來交易,你需要解鎖賬戶。
> personal.unlockAccount(eth.coinbase) Password true
你可以在控制台上用以下代碼片段,檢查哪個區塊被特殊的礦工(地址)挖掘過:
function minedBlocks(lastn, addr) { addrs = []; if (!addr) { addr = eth.coinbase } limit = eth.blockNumber - lastn for (i = eth.blockNumber; i >= limit; i--) { if (eth.getBlock(i).miner == addr) { addrs.push(i) } } return addrs } // scans the last 1000 blocks and returns the blocknumbers of blocks mined by your coinbase // (more precisely blocks the mining reward for which is sent to your coinbase). minedBlocks(1000, eth.coinbase); //[352708, 352655, 352559]
請注意,發現一個區塊但是不能把它變成典型鏈會經常發生。這意味着你在當地把挖過的區塊包括在內,當前的狀態會顯示歸於你賬戶的挖礦獎勵,然而不久后,會發現更好的鏈,我們轉換到不包含你區塊的鏈,因而不會記入任何挖礦獎勵。因此很有可能礦工監控coinbase余額的時候會發現,它發生了相當程度的浮動。
nohup geth --nodiscover --identity "meiguo110" --rpc --rpccorsdomain "*" --datadir /data/ethbase --port "30303" --rpcapi "db,net,personal,web3,eth,debug" --networkid 314590 -rpcaddr 172.26.0.3 --rpcport 8545 --mine --minerthreads=1 &