3.1 在geth上用CPU采矿

优质
小牛编辑
122浏览
2023-12-01

在Geth上用CPU采矿

在Ethereum的第一个发行版Frontier上,你只需要一个CPU和一个Ethereum客户端geth,你只需要一个GPU和b)一个Ethereum客户端,Geth。就可以用CPU采矿,但是效率太低,无法保持任何价值。 目前,Geth只包括一名CPU矿工,团队正在测试GPU miner branch,但这不会是Frontier的一部分。

Ethereum的C++版本实现还提供了一个GPU矿工,它们都是Eth(其CLI),AlethZero(其GUI)和EthMiner(独立矿工)的一部分。

注意:在采矿之前,确保你的blockchain完全和主链同步 否则您无法在主链上进行采矿。 当您用geth启动您的ethereum节点时,默认情况下不会采矿。要在采矿模式下启动,您可以使用--mine []命令行选项](https://github.com/ethereum/go-ethereum/wiki/Command-Line-Options)。`-minerthreads`参数可用于设置并行采矿的线程数(默认为处理器核心总数

geth --mine --minerthreads=4

您还可以使用控制台在运行时启动和停止CPU挖掘。miner.start采用可选参数控制采矿者的线程数量。

> miner.start(8)
true
> miner.stop()
true

请注意,如果您与网络同步(因为您在我们的共识块之上挖矿),那么挖掘真正的ether才是有意义的。因此,eth blockchain下载器/同步器将延迟采矿直到同步完成,然后在采矿自动启动,除非您用miner.stop()取消。

为了获得ether,您必须具有您的 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字节)到您的开采块上。按惯例,这被解释为一个unicode字符串,所以你可以设置你的简短的vanity tag

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ΞЯΞ
...
}

可以参考这个提案

After you successfully mined some blocks, you can check the ether balance of your etherbase account. Now assuming your etherbase is a local account:

eth.getBalance(eth.coinbase).toNumber(); '34698870000000'

您可以使用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余额时,很有可能会发现它可能波动很大。

log日志显示5块后被确认的当地采矿块。目前,您可能会发现从这些日志中生成开采块的列表更为方便快捷。

采矿成功取决于设定块的难度。块难度动态调整每个块,以调节网络散列能力,产生12秒的阻塞时间。因此,您找到一个块的可能性与您相对于难度的哈希率有关。你发现一个块需要等待的预计时间可以用以下代码来估计:

粗略估计:

etm = eth.getBlock("latest").difficulty/miner.hashrate; // estimated time in seconds
Math.floor(etm / 3600.) + "h " + Math.floor((etm % 3600)/60) + "m " +  Math.floor(etm % 60) + "s";
// 1h 3m 30s