docs: rework mining page (#25035)

This PR reworks mining page, including adding introduction, links and warning about impending merge.

Co-authored-by: Marius van der Wijden <m.vanderwijden@live.de>
Co-authored-by: Marius van der Wijden <m.vanderwijden@live.de>
Co-authored-by: rjl493456442 <garyrong0905@gmail.com>
pull/25655/head
Joseph Cook 2 years ago committed by GitHub
parent 1d9bf73b50
commit d0600282fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 285
      docs/_interface/mining.md

@ -1,191 +1,208 @@
---
title: Mining
sort_key: B
sort_key: F
---
This document explains how to set up geth for mining. The Ethereum wiki also has a [page
about mining](https://eth.wiki/en/fundamentals/mining), be sure to check that one as well.
Mining is the process through which new blocks are created. Geth actually creates new
blocks all the time, but these blocks need to be secured through proof-of-work so they
will be accepted by other nodes. Mining is all about creating these proof-of-work values.
The Ethereum blockchain grows when nodes add blocks and distribute them to their peers. Nodes
that add blocks are rewarded with ether payouts. This creates competition for the right to add
blocks to the blockchain. This means some mechanism must exist to select a single block for each
position in the blockchain that all nodes can agree upon. The mechanism that currently achieves
this for Ethereum is "proof-of-work" (PoW). This involved computing a certain value that can
only be calculated using repeated random guesses. Only if a node can demonstrate that they have
calculated this value, and therefore expended energy, will their block be accepted by other nodes.
This secures the network. This process of creating blocks and securing them using proof-of-work
is known as "mining".
The proof-of-work computation can be performed in multiple ways. Geth includes a CPU
miner, which does mining within the geth process. We discourage using the CPU miner with
the Ethereum mainnet. If you want to mine real ether, use GPU mining. Your best option for
doing that is the [ethminer](https://github.com/ethereum-mining/ethminer) software.
Much more information about mining, including details about the specific algorithm ("Ethash") used by
Ethereum nodes is available on
[ethereum.org](https://ethereum.org/en/developers/docs/consensus-mechanisms/pow/mining).
Always ensure your blockchain is fully synchronised with the chain before starting to
mine, otherwise you will not be mining on the correct chain and your block rewards will
not be valueable.
## GPU mining
## Mining and the Merge
The ethash algorithm is memory hard and in order to fit the DAG into memory, it needs
1-2GB of RAM on each GPU. If you get `Error GPU mining. GPU memory fragmentation?` you
don't have enough memory.
[The Merge](https://ethereum.org/en/upgrades/merge) is an upcoming upgrade to Ethereum that
will swap the existing PoW for a [proof-of-stake (PoS)](https://ethereum.org/en/developers/docs/consensus-mechanisms/pos) consensus mechanism. This marks the end of mining on Ethereum. Instead, nodes can [stake ether](https://ethereum.org/en/staking/solo/#get-started-on-the-staking-launchpad) directly and earn ether rewards by running [validators](https://ethereum.org/en/developers/docs/consensus-mechanisms/pos/#validators). The merge is expected to happen in the second half of 2022. Until then, Ethereum will continue to be secured by PoW miners. It is no longer recommended to purchase new hardware to participate in Ethereum mining because the chances of returning a profit before The Merge are low.
### Installing ethminer
To get ethminer, you need to install the ethminer binary package or build it from source.
See <https://github.com/ethereum-mining/ethminer/#build> for the official ethminer
build/install instructions. At the time of writing, ethminer only provides a binary for
Microsoft Windows.
## CPU vs GPU
### Using ethminer with geth
Participating in Ethereum's PoW mining requires running an algorithm called
["Ethash"](https://ethereum.org/en/developers/docs/consensus-mechanisms/pow/mining-algorithms/ethash). Geth includes
a CPU miner which runs Ethash within the Geth process. This might be useful for mining on some testnets. However, this is CPU mining is not viable on Ethereum Mainnet because CPU miners are easily out-competed by more efficient GPU miners. GPU mining is the recommended method for mining real ether on Ethereum Mainnet, but it is not part of the standard Geth installation. To mine using GPUs an additional piece of third-party software is required. The recommended GPU mining software is [Ethminer](https://github.com/ethereum-mining/ethminer).
First create an account to hold your block rewards.
Regardless of the mining method, the blockchain must be fully synced before mining is started, otherwise the miner will build on an outdated side chain, meaning block rewards will not be recognized by the main network.
geth account new
Follow the prompts and enter a good password. **DO NOT FORGET YOUR PASSWORD**. Also take
note of the public Ethereum address which is printed at the end of the account creation
process. In the following examples, we will use 0xC95767AC46EA2A9162F0734651d6cF17e5BfcF10
as the example address.
## GPU Mining
Now start geth and wait for it to sync the blockchain. This will take quite a while.
### Installing Ethminer
geth --http --miner.etherbase 0xC95767AC46EA2A9162F0734651d6cF17e5BfcF10
The Ethminer software can be installed from a downloaded binary or built from source. The relevant downloads
and installation instructions are available from the [Ethminer Github](https://github.com/ethereum-mining/ethminer/#build). Standalone executables are available for Linux, macOS and Windows.
To monitor the syncing, in another terminal you can `attach` the geth JavaScript console to the running node like so:
### Using Ethminer with Geth
geth attach http://127.0.0.1:8545
and then at the > prompt type
An account to receive block rewards must first be defined. The address of the account is all that is required to start mining - the mining rewards will be credited to that address. This can be an existing address or one that is newly created by Geth. More detailed instructions on creating and importing accounts are available on the [Account Management](/docs/interface/managing-your-accounts) page.
eth.syncing
The account address can be provided to `--mining.etherbase` when Geth is started. This instructs Geth to direct any block rewards to this address. Once started, Geth will sync the blockchain. If Geth has not connected to this network before, or if the data directory has been deleted, this can take several days. Also, enable HTTP traffic with the `--http` command.
You'll see something like the example output below -- it's a two stage process as described in much more detail in our [FAQ](../FAQ).
In the first stage, the difference between the "currentBlock" and the "highestBlock" will decrease until they are almost equal. It will then look stuck and appear as never becoming equal. But you should see "pulledStates" rising to equal "knownStates." When both are equal, you are synced.
```shell
geth --http --miner.etherbase 0xC95767AC46EA2A9162F0734651d6cF17e5BfcF10
```
Example output of first stage of block downloading:
The progress of the blockchain syncing can be monitored by attaching a JavaScript console in another terminal. More detailed information about the console can be found on the [Javascript Console](/docs/interface/javascript-console) page. To attach and open a console:
{
currentBlock: 10707814,
highestBlock: 13252182,
knownStates: 0,
pulledStates: 0,
startingBlock: 3809258
}
```shell
geth attach http://127.0.0.1:8545
```
You will import up to the `highestBlock` and `knownStates`. Block importing will stop `~64` blocks behind head and finish importing states.
Then in the console, to check the sync progress:
Once all `states` are downloaded, `geth` will switch into a full node and sync the remaining `~64` blocks fully, as well as new ones. In this context, `eth.syncing` returns false once synced.
```shell
eth.syncing
```
Now we're ready to start mining. In a new terminal session, run ethminer and connect it to geth:
If the sync is progressing correctly the output will look similar to the following:
OpenCL
```terminal
{
currentBlock: 13891665,
healedBytecodeBytes: 0,
healedBytecodes: 0,
healedTrienodeBytes: 0,
healedTrienodes: 0,
healingBytecode: 0,
healingTrienodes: 0,
highestBlock: 14640000,
startingBlock: 13891665,
syncedAccountBytes: 0,
syncedAccounts: 0,
syncedBytecodeBytes: 0,
syncedBytecodes: 0,
syncedStorage: 0,
syncedStorageBytes: 0
}
```
ethminer -G -P http://127.0.0.1:8545
Once the blockchain is sync'd, mining can begin. In order to begin mining, Ethminer must be run and connected to Geth in a new terminal. OpenCL can be used for a wide range of GPUs, CUDA can be used specifically for Nvidia GPUs:
CUDA
```shell
#OpenCL
ethminer -v 9 -G -P http://127.0.0.1:8545
```
ethminer -U -P http://127.0.0.1:8545
```shell
#CUDA
ethminer -v -U -P http://127.0.0.1:8545
```
`ethminer` communicates with geth on port 8545 (the default RPC port in geth). You can
change this by giving the [`--http.port` option](../rpc/server) to `geth`. Ethminer will find
geth on any port. You also need to set the port on `ethminer` with `-P
http://127.0.0.1:3301`. Setting up custom ports is necessary if you want several instances
mining on the same computer. If you are testing on a private cluster, we recommend you use
CPU mining instead.
Ethminer communicates with Geth on port 8545 (Geth's default RPC port) but this can be changed by providing a custom
port to the `http.port` command. The corresponding port must also be configured in Ethminer by providing
`-P http://127.0.0.1:<port-number>`. This is necessary when multiple instances of Geth/Ethminer will coexist on the same machine.
If the default for `ethminer` does not work try to specify the OpenCL device with:
`--opencl-device X` where X is 0, 1, 2, etc. When running `ethminer` with `-M`
(benchmark), you should see something like:
If using OpenCL and the default for `ethminer` does not work, specifying the device using the `--opencl--device X` command is a common fix. `X` is an integer `1`, `2`, `3` etc. The Ethminer `-M` (benchmark) command should display something that looks like:
Benchmarking on platform: { "platform": "NVIDIA CUDA", "device": "GeForce GTX 750 Ti", "version": "OpenCL 1.1 CUDA" }
```terminal
Benchmarking on platform: { "platform": "NVIDIA CUDA", "device": "GeForce GTX 750 Ti", "version": "OpenCL 1.1 CUDA" }
Benchmarking on platform: { "platform": "Apple", "device": "Intel(R) Xeon(R) CPU E5-1620 v2 @ 3.70GHz", "version": "OpenCL 1.2 " }
Benchmarking on platform: { "platform": "Apple", "device": "Intel(R) Xeon(R) CPU E5-1620 v2 @ 3.70GHz", "version": "OpenCL 1.2 " }
```
**Note** hashrate info is not available in `geth` when GPU mining. Check your hashrate
with `ethminer`, `miner.hashrate` will always report 0.
Note that the Geth command `miner.hashrate` only works for CPU mining - it always reports zero for GPU mining. To check the GPU mining hashrate, check the logs `ethminer` displays to its terminal. More verbose logs can be configured using `-v` and a value between 0-9.
## CPU Mining with Geth
The Ethash algorithm is [memory-hard](https://crypto.stackexchange.com/questions/84002/memory-hard-vs-memory-bound-functions) and requires a large dataset to be loaded into memory. Each GPU requires 4-5 GB of RAM. The error message `Error GPU mining. GPU memory fragmentation?` indicates that there is insufficient memory available.
When you start up your ethereum node with `geth` it is not mining by default. To start it
in mining mode, you use the `--mine` command-line flag. The `--miner.threads` parameter can
be used to set the number parallel mining threads (defaulting to the total number of
processor cores).
geth --mine --miner.threads=4
## CPU Mining with Geth
You can also start and stop CPU mining at runtime using the
[console](../interface/javascript-console). `miner.start` takes an optional parameter for
the number of miner threads.
When Geth is started it is not mining by default. Unless it is specifically instructed to mine, it acts only as a node, not a miner. Geth starts as a (CPU) miner if the `--mine` flag is provided. The `--miner.threads` parameter can
be used to set the number parallel mining threads (defaulting to the total number of processor cores).
> miner.start(8)
true
> miner.stop()
true
```shell
geth --mine --miner.threads=4
```
Note that mining for real ether only makes sense if you are in sync with the network
(since you mine on top of the consensus block). Therefore the eth blockchain
downloader/synchroniser will delay mining until syncing is complete, and after that mining
automatically starts unless you cancel your intention with `miner.stop()`.
CPU mining can also be started and stopped at runtime using the [console](/docs/interface/javascript-console). The command `miner.start` takes an optional parameter for the number of miner threads.
In order to earn ether you must have your **etherbase** (or **coinbase**) address set.
This etherbase defaults to your [primary account](../interface/managing-your-accounts). If
you don't have an etherbase address, then `geth --mine` will not start up.
```js
miner.start(8)
true
miner.stop()
true
```
You can set your etherbase on the command line:
Note that mining for real ether only makes sense if you are in sync with the network (since you mine on top of the consensus block). Therefore the Ethereum blockchain downloader/synchroniser will delay mining until syncing is complete, and after that mining automatically starts unless you cancel your intention with `miner.stop()`.
geth --miner.etherbase '0xC95767AC46EA2A9162F0734651d6cF17e5BfcF10' --mine 2>> geth.log
Like with GPU mining, an etherbase account must be set. This defaults to the primary account in the keystore but can be set to an alternative address using the `--miner.etherbase` command:
You can reset your etherbase on the console too:
```shell
geth --miner.etherbase '0xC95767AC46EA2A9162F0734651d6cF17e5BfcF10' --mine
```
If there is no account available an account wil be created and automatically configured to be the coinbase. The Javascript console can be used to reset the etherbase account at runtime:
> miner.setEtherbase(eth.accounts[2])
```shell
miner.setEtherbase(eth.accounts[2])
```
Note that your etherbase does not need to be an address of a local account, just an
existing one.
Note that your etherbase does not need to be an address of a local account, it just has to be set to an existing one.
There is an option [to add extra data](../interface/javascript-console) (32 bytes only) to
your mined blocks. By convention this is interpreted as a unicode string, so you can set
your short vanity tag.
There is an option to add extra data (32 bytes only) to the mined blocks. By convention this is interpreted as a unicode string, so it can be used to add a short vanity tag using `miner.setExtra` in the Javascript console.
> miner.setExtra("ΞTHΞЯSPHΞЯΞ")
```shell
miner.setExtra("ΞTHΞЯSPHΞЯΞ")
```
You can check your hashrate with [miner.hashrate](../interface/javascript-console), the
result is in H/s (Hash operations per second).
The console can also be used to check the current hashrate in units H/s (Hash operations per second):
> eth.hashrate
712000
```shell
eth.hashrate
712000
```
After you successfully mined some blocks, you can check the ether balance of your
etherbase account. Now assuming your etherbase is a local account:
After some blocks have been mined, the etherbase account balance with be >0. Assuming the etherbase is a local account:
> eth.getBalance(eth.coinbase).toNumber();
'34698870000000'
```shell
eth.getBalance(eth.coinbase).toNumber();
'34698870000000'
```
You can check which blocks are mined by a particular miner (address) with the following
code snippet on the console:
It is also possible to check which blocks were mined by a particular miner (address) using the following code snippet in the Javascript console:
> 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
```js
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)
}
// 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]
Note that it will happen often that you find a block yet it never makes it to the
canonical chain. This means when you locally include your mined block, the current state
will show the mining reward credited to your account, however, after a while, the better
chain is discovered and we switch to a chain in which your block is not included and
therefore no mining reward is credited. Therefore it is quite possible that as a miner
monitoring their coinbase balance will find that it may fluctuate quite a bit.
The logs show locally mined blocks confirmed after 5 blocks. At the moment you may find it
easier and faster to generate the list of your mined blocks from these logs.
[eth-wiki-mining]: https://github.com/ethereum/wiki/wiki/Mining
[ethminer]: https://github.com/ethereum-mining/ethminer
}
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]
```
The etherbase balance will fluctuate because quite often a mined block may be re-org'd out
of the canonical chain. This means that when the local Geth node includes the mined block
in its own local blockchain the account balance appears higher because the block rewards are
applied. When the node switches to another version of the chain due to information received
from peers, that block may not be included and the block rewards are not applied.
The logs show locally mined blocks confirmed after 5 blocks.
## Summary
The page describes how to start Geth as a mining node. Mining can be done on CPUs - in which case Geth's built-in
miner can be used - or on GPUs which requires third party software. GPUs are required to mine real ether on Ethereum
Mainnet. It is important to note that Ethereum will swap its consensus mechanism from PoW to PoS in the second half of 2022. This swap, known as "The Merge" will end mining on Ethereum.

Loading…
Cancel
Save