How Does Crypto Mining Work?

Since the rise of Bitcoin in 2009, up until today, there have been thousands (4,000+) of cryptocurrencies as of 2021. Bitcoin remains, however, the major player. Alongside Bitcoin, many cryptocurrencies use “Proof of Work” to secure transactions validity. Before understanding crypto mining, we first need to understand what “Proof of Work” is and how it is used to secure the Bitcoin blockchain, and in general any proof of work-based cryptocurrency blockchain.


How does Proof of Work work?

Cryptocurrencies like Bitcoin, use a decentralised ledger to store all transactions that ever happened. This ledger, in cryptocurrency terms, is referred to as “blockchain”. The reason why it’s called blockchain is that transactions are grouped together in blocks and added to this ledger. Let’s have a look at how transactions are stored in the “blockchain” (ie. ledger). “Bob transfers 1btc to Alice”. This will be similar to how a transaction will be stored in the “blockchain”. Any node in the network will be able to have the full list of transactions in the network and verify whether those transactions are valid or not. To verify that a a transaction is valid, a node must verify that the transactions block contains the proof that certain computations have been completed.


Hashing and Hash Functions

These “computations” are known as “hashing” operations, and in the case of Bitcoin the hashing algorithm is known as SHA-256.

Bitcoin proof-of-work was inspired by Hashcash

Let’s have a look at how HashCash works in general.

We have an input source, an operation (called “hashing”) and an output. The output length is always the same, regardless of the input size.

input:  "bob transferred 1btc to alice"
                       |
                <hash function>
                       |
output: "addceafc3bfafcba467a3132fc5098b392b5bc5470e2761422215c3ad257b9fe"

Hash functions are relatively complex, they generally have 2 rules.

  • Any given input yields a fixed-length output
  • Two different input never generate the same output - ie. there are no “collisions”

Even changing one character in our input data, we will obtain a radically different output. As shown below.

input:  "bob transferred 1btc to alice"
                       |
                <hash function>
                       |
output: "addceafc3bfafcba467a3132fc5098b392b5bc5470e2761422215c3ad257b9fe"
input:  "bob transferred 2btc to alice"
                       |
                <hash function>
                       |
output: "0ba113311d3ff48f9e9be161a340639233aef7fe5621a108e72c3bae791acd57"

So is it enough to hash a transaction like we did? It is already enough work per se. However, the idea behind crypto mining is that a node, that carries out the operation of creating a new block, will need to obtain an output with at least a certain number of zeroes. This can be obtained by adding an additional input string to our transaction information. For example

input:  "bob transferred 1btc to alice" + "<variable nonce>"
                       |
                <hash function>
                       |
output: "0ba113311d3ff48f9e9be161a340639233aef7fe5621a108e72c3bae791acd57"

In this way we will be able to generate many outputs for the same transaction, by changing the variable nonce. The variable nonce is just a random string which is added to the transaction itself. Millions of hash operations will be performed with different nonce values. Based on the HashCash idea, to be valid, the output must contains at least a certain - reasonably high - number of leading zeroes. The higher the number of leading zeroes, the higher the difficulty to find the right nonce.

In Bitcoin, as opposed to HashCash, there is a little variation to the algorithm. Instead for the target to have a certain number of leading zeroes, the hash is considered valid, only if, interpreted as an integer, is greater than a given threshold. This allows Bitcoin to adjust the target more precisely than by just specifying the number of leading zeroes.


Mining Difficulty

The number of leading zeroes is variable, and it’s called “difficulty” in bitcoin terms. The more zeroes required, the higher the difficulty, the more power and time is required to find the “magic” nonce value and create a new block. You can check the Bitcoin difficulty in real time by following this link https://www.blockchain.com/charts/difficulty

The difficulty is adjusted every 2016 blocks (every 2 weeks approximately) so that the average time between each block remains 10 minutes.

As of 2021, the average difficulty is directly related to the total mining power of the Bitcoin network which is at around 18 TH/s. Capable of generating 18 Trillion of Hashes per second.


Bitcoin Block(s)

The Hashing operation that we have described above, is not performed on a single transaction. But for performance reasons, many transactions are bundled together into what is known as a “Block”. As of 2021, Bitcoin averages around 2,000 transactions per block.. The block also averages around 1Mb in size.


Bitcoin Miners VS Bitcoin Nodes

In the case of Bitcoin, or other PoW (Proof of Work) based cryptocurrencies, there are different players. Generally all partecipants in the network are known as nodes. All nodes download the entire blockchain on their local storage device and read and share incoming transactions across the network. Miners are a subset of these nodes. Miners are nodes, who also try to create new blocks, by performing the arduous activity of finding a valid block by performing hash operations on a set of transactions, by changing and guessing the right nonce. Once a miner finds a valid nonce and broadcast a valid block, it will be granted a block reward.


Bitcoin Block Reward

Once a miner successfully mines a block, as discussed above, the miner will receive a block reward. Currently the block reward is 6 BTC. This has originally 50 BTC, but it is reduced every 210,000 blocks mined. Roughly halved every 4 years. The amount is expected to be zero around 2140. Once the block reward will be zero, the miners in the network will be rewarded with transaction fees, which will keep the network running. Here you can see the amount of USD earned by Bitcoin miners


Mining Pools

We have seen how mining difficulty has risen to very high levels - 18 Trillion of Hashes / second - and how that is pushing miners to come together and try to have better odds at finding the right hash to mine new blocks. Many miner nodes come together to put their computational power in what is known as mining pools. Once a valid block is mined, the block reward is split among the participant of the Mining Pool. Bitcoin mining is dominated by a handful of mining pools, as reported on this Hashrate Distribution Chart