Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / HPC / GPU

Introducing the Process of Mining in Blockchain

5.00/5 (4 votes)
24 Aug 2018CPOL14 min read 18.6K  
Bitcoin mining process
In this article, we present the process of bitcoin mining, how the blocks are added in blockchain, and how it keeps blockchain growing. We look at different tasks performed by the bitcoin miner. We also discuss how the hashing puzzle is solved.

Introduction

Mining is a process by which new blocks are added to the blockchain. Blocks contain transactions that are validated via the mining process by mining nodes on the Bitcoin network. Blocks, once mined and verified are added to the blockchain which keeps the blockchain growing. This process is resource-intensive due to the requirements of PoW where miners compete in order to find a number which is less than the difficulty target of the network. This difficulty in finding the correct value (also called sometimes the mathematical puzzle) is there to ensure that the required resources have been spent by miners before a new proposed block can be accepted. New coins are minted by the miners by solving the PoW problem, also known as partial hash inversion problem. This process consumes a high amount of resources including computing power and electricity. This process also secures the system against frauds and double spending attacks while adding more virtual currency to the Bitcoin ecosystem.

Roughly, one new block is created (mined) every 10 minutes to control the frequency of generation of bitcoins. This frequency needs to be maintained by the Bitcoin network and is encoded in the bitcoin core clients in order to control the money supply. Miners are rewarded with new coins if and when they discover new blocks by solving PoW. Miners are paid transaction fees in return for including transactions in their proposed blocks. New blocks are created at an approximate fixed rate of every 10 minutes. The rate of creation of new bitcoins decreases by 50%, every 210,000 blocks, roughly every 4 years. When bitcoin was initially introduced, the block reward was 50 bitcoins; then in 2012, this was reduced to 25 bitcoins. In July 2016, this was further reduced to 12.5 coins (12 coins) and the next reduction is estimated to be on July 4, 2020. This will reduce the coin reward further down to approximately six coins.

Approximately 144 blocks, that is, 1,728 bitcoins are generated per day. The number of actual coins can vary per day; however, the number of blocks remains at 144 per day. Bitcoin supply is also limited and in 2140, almost 21 million bitcoins will be finally created and no new bitcoins can be created after that. Bitcoin miners, however, will still be able to profit from the ecosystem by charging transaction fees.

Tasks of the Miners

Once a node connects to the bitcoin network, there are several tasks that a bitcoin miner performs:

  1. Synching up with the network: Once a new node joins the bitcoin network, it downloads the blockchain by requesting historical blocks from other nodes. This is mentioned here in the context of the bitcoin miner; however, this not necessarily a task only for a miner.
  2. Transaction validation: Transactions broadcasted on the network are validated by full nodes by verifying and validating signatures and outputs.
  3. Block validation: Miners and full nodes can start validating blocks received by them by evaluating them against certain rules. This includes the verification of each transaction in the block along with verification of the nonce value.
  4. Create a new block: Miners propose a new block by combining transactions broadcasted on the network after validating them.
  5. Perform Proof of Work: This task is the core of the mining process and this is where miners find a valid block by solving a computational puzzle. The block header contains a 32-bit nonce field and miners are required to repeatedly vary the nonce until the resultant hash is less than a predetermined target.
  6. Fetch reward: Once a node solves the hash puzzle (PoW), it immediately broadcasts the results, and other nodes verify it and accept the block. There is a slight chance that the newly minted block will not be accepted by other miners on the network due to a clash with another block found at roughly the same time, but once accepted, the miner is rewarded with 12.5 bitcoins and any associated transaction fees.

Mining Rewards

When Bitcoin started in 2009, the mining reward used to be 50 bitcoins. After every 210,000 blocks, the block reward halves. In November 2012, it halved down to 25 bitcoins. Currently, it is 12.5 BTC per block since July 2016. Next halving is on Friday, 12 June 2020 after which the block reward will be reduced down to 6.25 BTC per block. This mechanism is hardcoded in Bitcoin to regulate, control inflation and limit the supply of bitcoins.

Proof of Work (PoW)

This is a proof that enough computational resources have been spent in order to build a valid block. PoW is based on the idea that a random node is selected every time to create a new block. In this model, nodes compete with each other in order to be selected in proportion to their computing capacity. The following equation sums up the PoW requirement in bitcoin:

H ( N || P_hash || Tx || Tx || . . . Tx) < Target)

Where N is a nonce, P_hash is a hash of the previous block, Tx represents transactions in the block, and Target is the target network difficulty value. This means that the hash of the previously mentioned concatenated fields should be less than the target hash value. The only way to find this nonce is the brute force method. Once a certain pattern of a certain number of zeroes is met by a miner, the block is immediately broadcasted and accepted by other miners.

The Mining Algorithm

The mining algorithm consists of the following steps:

  1. The previous block's header is retrieved from the bitcoin network.
  2. Assemble a set of transactions broadcasted on the network into a block to be proposed.
  3. Compute the double hash of the previous block's header combined with a nonce and the newly proposed block using the SHA-256 algorithm.
  4. Check if the resultant hash is lower than the current difficulty level (target) then PoW is solved. As a result of successful PoW, the discovered block is broadcasted to the network and miners fetch the reward.
  5. If the resultant hash is not less than the current difficulty level (target), then repeat the process after incrementing the nonce.

As the hash rate of the bitcoin network increased, the total amount of 32-bit nonce was exhausted too quickly. In order to address this issue, the extra nonce solution was implemented, whereby the coinbase transaction is used as a source of extra nonce to provide a larger range of nonce to be searched by the miners.

This process can be visualized by using the following flowchart:

Image 1

Mining process

Mining difficulty increased over time and bitcoins that could be mined by single CPU laptop computers now require dedicated mining centers to solve the hash puzzle. The current difficulty level can be queried using the Bitcoin command-line interface using the following command:

$ bitcoin-cli getdifficulty
1452839779145

This number represents the difficulty level of the Bitcoin network. Recall from previous sections that miners compete to find a solution to a problem. This number, in fact shows, that how difficult it is to find a hash which is lower than the network difficulty target. All successfully mined blocks must contain a hash that is less than this target number. This number is updated every 2 weeks or 2016 blocks to ensure that on average 10-minute block generation time is maintained.

Bitcoin network difficulty has increased exponentially, the following graph shows this difficulty level over a period of one year:

Image 2

Mining difficulty over the last year

The preceding graph shows the difficulty of the Bitcoin network over a period of last year and it has increased quite significantly. The reason why mining difficulty increases is because in Bitcoin, the block generation time has to be always around 10 minutes. This means that if blocks are being mined too quickly by fast hardware then the difficulty increases so that the block generation time can remain at roughly 10 minutes per block. This is also true in reverse if blocks are not mined every 10 minutes than the difficulty is decreased. Difficulty, is calculated every 2016 blocks (in two weeks) and adjusted accordingly. If the previous set of 2016 blocks were mined in less than a period of two weeks then difficulty will be increased. Similarly, if 2016 blocks were found in more than two weeks (If blocks are mined every 10 minutes then 2016 blocks take 2 weeks to be mined), then the difficulty is decreased.

The Hash Rate

The hashing rate basically represents the rate of calculating hashes per second. In other words, this is the speed at which miners in the Bitcoin network are calculating hashes to find a block. In early days of bitcoin, it used to be quite small as CPUs were used. However, with dedicated mining pools and ASICs now, this has gone up exponentially in the last few years. This has resulted in increased difficulty of the Bitcoin network. The following hash rate graph shows the hash rate increase over time and is currently measured in Exa hashes. This means that in 1 second, the Bitcoin network miners are computing more than 24,000,000,000,000,000,000 hashes per second.

Image 3

Hashing rate (measured in Exa-hashes) as of March 2018, shown over a period of 1 year

Mining Systems

Over time, bitcoin miners have used various methods to mine bitcoins. As the core principle behind mining is based on the double SHA-256 algorithm, overtime experts have developed sophisticated systems to calculate the hash faster and faster. The following is a review of the different types of mining methods used in bitcoin and how they evolved with time.

CPU

CPU mining was the first type of mining available in the original bitcoin client. Users could even use laptop or desktop computers to mine bitcoins. CPU mining is no longer profitable and now more advanced mining methods such as ASIC-based mining is used. CPU mining only lasted for around just over a year since the introduction of Bitcoin and soon other methods were explored and tried by the miners.

GPU

Due to the increased difficulty of the bitcoin network and the general tendency of finding faster methods to mine, miners started to use GPUs or graphics cards available in PCs to perform mining. GPUs support faster and parallelized calculations that are usually programmed using the OpenCL language. This turned out to be a faster option as compared to CPUs. Users also used techniques such as overclocking to gain maximum benefit of the GPU power. Also, the possibility of using multiple graphics cards increased the popularity of graphics cards' usage for bitcoin mining. GPU mining, however, has some limitations, such as overheating and the requirement for specialized motherboards and extra hardware to house multiple graphics cards. From another angle, graphics cards have become quite expensive due to increased demand and this has impacted gamers and graphic software users.

FPGA

Even GPU mining did not last long, and soon miners found another way to perform mining using FPGAs. Field Programmable Gate Array (FPGA) is basically an integrated circuit that can be programmed to perform specific operations. FPGAs are usually programmed in Hardware Description Languages (HDLs), such as Verilog and VHDL. Double SHA-256 quickly became an attractive programming task for FPGA programmers and several open source projects started too. FPGA offered much better performance as compared to GPUs; however, issues such as accessibility, programming difficulty, and the requirement for specialized knowledge to program and configure FPGAs resulted in a short life of the FPGA era for bitcoin mining.

ASICs

Application Specific Integrated Circuit (ASIC) was designed to perform the SHA-256 operation. These special chips were sold by various manufacturers and offered a very high hashing rate. This worked for some time, but due to the quickly increasing mining difficulty level, single-unit ASICs are no longer profitable. Currently, mining is out of the reach of individuals as vast amounts of energy and money is needed to be spent in order to build a profitable mining platform. Now professional mining centers using thousands of ASIC units in parallel are offering mining contracts to users to perform mining on their behalf. There is no technical limitation, a single user can run thousands of ASICs in parallel but it will require dedicated data centers and hardware, therefore, cost for a single individual can become prohibitive. The following are the four types of mining hardware:

Image 4

CPU

Image 5

GPU

Image 6

FPGA

Image 7

ASIC

Mining Pools

A mining pool forms when group of miners work together to mine a block. The pool manager receives the coinbase transaction if the block is successfully mined, which is then responsible for distributing the reward to the group of miners who invested resources to mine the block. This is profitable as compared to solo mining, where only one sole miner is trying to solve the partial hash inversion function (hash puzzle) because, in mining pools, the reward is paid to each member of the pool regardless of whether they (more specifically, their individual node) solved the puzzle or not.

There are various models that a mining pool manager can use to pay to the miners, such as the Pay Per Share (PPS) model and the proportional model. In the PPS model, the mining pool manager pays a flat fee to all miners who participated in the mining exercise, whereas in the proportional model, the share is calculated based on the amount of computing resources spent to solve the hash puzzle.

Many commercial pools now exist and provide mining service contracts via the cloud and easy-to-use web interfaces. The most commonly used ones are AntPool (https://www.antpool.com), BTC (https://btc.com), and BTC TOP (http://www.btc.top). A comparison of hashing power for all major mining pools is shown in the following diagram:

Image 8

Mining pools and their hashing power (hash rate) as of 28/10/2017
Source: https://blockchain.info/pools

Mining centralization can occur if a pool manages to control more than 51% of the network by generating more than 51% hash rate of the Bitcoin network. As discussed earlier in the introduction section, a 51% attack can result in successful double-spending attacks, and it can impact consensus and in fact impose another version of transaction history on the Bitcoin network.

This event has happened once in the Bitcoin history when GHash.IO, a large mining pool, managed to acquire more than 51% of the network capacity. Theoretical solutions, such as two-phase PoW (http://hackingdistributed.com/2014/06/18/how-to-disincentivize-large-bitcoin-mining-pools/), have been proposed in academia to disincentivize large mining pools. This scheme introduces a second cryptographic puzzle that results in mining pools to either reveal their private keys or provide a considerable portion of the hash rate of their mining pool, thus reducing the overall hash rate of the pool.

Various types of hardware are commercially available for mining purposes. Currently, the most profitable one is ASIC mining, and specialized hardware is available from a number of vendors such as Antminer, AvalonMiner and Whatsminer. Solo mining is not much profitable now unless a vast amount of money and energy is spent to build your own mining rig or even a data center. With the current difficulty factor (March 2018), if a user manages to produce a hash rate of 12 TH/s, they can hope to make 0.0009170 BTC (around $6) per day, which is very low as compared to the investment required to source the equipment that can produce 12 TH. Including running costs such as electricity, this turns out to be not very profitable.

For example, Antminer S9, is an efficient ASIC miner which produces hash power of 13.5 TH/s and it seems that it can produce some profit per day, which is true but a single Antminer S9 costs around 1700 GBP and combining it with electricity cost the return on investment is almost after a year's mining when it produces around 0.3 BTC. It may seem still OK, to invest but also think about the fact that the Bitcoin network difficulty keeps going up with time and during a year it will become more difficult to mine and the mining hardware will run out its utility in a few months.

Summary

In this article, we presented the process of bitcoin mining, how the blocks are added in blockchain, and how it keeps blockchain growing. We looked at different tasks performed by the bitcoin miner. We also discussed how the hashing puzzle is solved.

You enjoyed an excerpt from the book Mastering Blockchain - Second Edition written by Imran Bashir. If you want to learn about cryptography and cryptocurrencies, so you can build highly secure, decentralized applications and conduct trusted in-app transactions; this is the book for you.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)