How Blockchain / Bitcoin Mining Works

A beginners guide to blockchain and bitcoin mining

Amine Soufaih
African Blockchain Initiative
7 min readSep 10, 2018

--

Credits: Qilai Shen/Bloomberg

A few years ago, the word “mining” was used solely to refer to digging in the ground and extracting materials such as gold. However, with the recent blockchain wave, a new meaning of mining has emerged referring to bitcoin or cryptocurrency mining. The process that bitcoin mining follows is somewhat similar to that of gold mining: You have to invest using your resources and effort in order to receive a reward that you may trade against fiat currencies.

What is Bitcoin Mining?

Bitcoin mining refers to the process of verifying, validating and adding transaction records to the Bitcoin public ledger. New transactions are compiled into a new block that is created and linked to the most recent block, forming a chain of blocks (called block chain). After a block is created, a new bitcoin is released. As an incentive and in exchange to the amount of effort spent, the miner is rewarded through the newly released bitcoin.

Mining bitcoin requires the use of electricity and computational power in order to approve the transactions. To verify a new block, the miner needs to solve mathematical puzzles and find the solution. These calculations cannot be done by humans and are left to powerful computers. These computers can make trillions of calculations per second depending on the transaction and block size as well as other factors.

A while ago, miners could use their own computer CPU to mine bitcoin. However, that is not possible nowadays. If you use your own computer CPU, you’ll be spending more money on electricity than what you would actually earn. Since mining difficulty increased over time, miners now need to acquire special mining hardware such as ASIC chips (Application-specific integrated circuit). Built solely for the purpose of mining bitcoin, these circuits offer high performance and consume much less energy than older systems. As a result, mining has become only profitable at a large scale, usually done by equipped mining farms.

Figure 1: Large bitcoin mining farm

How Does Bitcoin Mining Work?

Bitcoin mining follows the Proof of Work protocol to validate transactions and reward the miner of the new block. The probability of mining a new block through PoW is random and relies on the amount of effort put by the miner. As a result, it is an energy-consuming activity since it requires a huge number of attempts to be done by the mining computers. For a better understanding of Proof of Work and other blockchain consensus protocols, visit Consensus Protocols Article.

Bitcoin Mining follows a simple process to create a new block and link it to the existing chain of blocks:

Figure 2: Bitcoin mining process for new block creation / Copyright: ABI

First, a number of transactions are requested and a new block is proposed to be created. These transactions can be anything: from medical records to real estate contracts but for the case of the bitcoin network: monetary transactions. The newly proposed block is linked to the header of the most recent block. In blockchain, each block needs to include the hash number of the previous block before it in order ensuring that all information is well structured in all nodes of the network.

The new block then goes through a hashing algorithm called SHA-256. The term hashing refers to the use of a mathematical function to generate an output value (Hash number) from a given input which is the transaction in this case. The hash number has always the same length of 64 hexadecimal digits, no matter how big or small the input is.

When the hash number is generated, it is then compared to the hash target. The hash target is a 64-character string similar to that of SHA-256 but which starts with a bunch of zeros.

Example of a hash target:

00000000000063gfy800000000000000000000000000000000000000000000

The goal is to generate a hash number that is less or equal to the hash target. If the hash number is less or equal to the hash target, the puzzle is solved and the block is created, rewarding at the same time the miner through newly issued bitcoin. If the hash number is more than the hash target, the hashed transaction is incremented by adding a random number (called nonce) and the calculation is done again. This process can go on and on until a hash number less than the target is found.

If validating a block only depends on finding a hash number that is less than the hash target, why is it so difficult to mine bitcoin? Well, it all comes out to the fact that finding a correct hash number requires a significant number of attempts. Hashing is like guessing at random, so the probability of generating a hash number that is less than the target is extremely small. The difficulty of mining bitcoin depends on the magnitude of the target value, therefore, the smaller it gets the harder it becomes to mine. Let’s take the example of the hash target above:

00000000000063gfy800000000000000000000000000000000000000000000

In the hexadecimal system, every leading zero decreases the magnitude a number by a factor of 16 (by a factor of 10 in the decimal system). Therefore, since the hash target has 14 leading zeroes, the probability of randomly generating a valid hash number is:

16^(-14) = 0.00000000000000001%

The probability of finding the right nonce in order to generate a valid hash number is extremely small which makes the mining process a tedious and energy-consuming activity.

Byzantine Generals’ Problem

The Byzantine Generals’ Problem is a widely known computing problem that stems from a consensus-making challenge that the Byzantine Empire army was facing. It is until recently that blockchain came up with a solution to this problem.

Imagine the following scenario: The Byzantine army plans an attack on a neighboring city. The army consists of multiple fractions that surround the city, each led by a general. The generals are then under the orders of the supreme commander. The commander and all the generals need to agree on the exact time to attack. The only way for the attack to be successful is if all fractions attack all at once.

Figure 3: Illustration of the Byzantine Generals’ Problem / Copyright: ABI

However, it’s very hard for the commander to communicate with the generals since they are too far from him. Therefore, he has to send a messenger but nothing guarantees that the messenger will send the right message; if he’s a traitor, he could mislead the generals and send a wrong message such as: “Attack Later”.

Blockchain provides a hypothetical, yet valid, way to encrypt the message so that the generals can make sure that the message they received is valid. Somewhat similar to the bitcoin mining process, the commander has to use a hashing algorithm to calculate the hash number of his message. For example, “Attack Friday” would become something like:

85hi6s8a6ihiauh7a78syay8tdiuud9i2190jodehg3782h2gfr7r9jge4

The commander also needs to communicate, in advance, a hash target with the generals. Then, through Proof of Work, the commander needs to find a nonce which, when combined with the message, gives a hash number that contains the hash target. This process requires trying out all numbers until finding the valid nonce. Once having found the right nonce, the commander can append the nonce to the message and send it to the generals. This way, if the messenger is a traitor and tampers with the message, the generals will easily be able to notice that since the message, once hashed, will not contain the hash target. Problem solved!

How is the mining difficulty controlled?

Back to the bitcoin mining, the block creation rate or the mining difficulty depend on the number of miners in the network and the amount of effort put into mining. To keep the rate of block creation constant, the bitcoin mining process adheres to the following cycle:

Figure 4: Cycle of block creation rate / Copyright: ABI

As more miners join the bitcoin network, the block creation rate increases which leads to a decrease in the mining time required. As a result, the hash target becomes smaller which increases the mining difficulty as explained previously. The block creation rate decreases simultaneously as it becomes harder to mine, resulting in the average mining time back to normal. This cycle happens every 2016 blocks and ensures that the rate of block creation stays constant, averaging at around 10 minutes per block.

As for the block reward, it’s halved every 216,000 blocks (4 years), starting at 50 bitcoin since its foundation back to 2009. Block rewards will keep decreasing geometrically until reaching a total release of 21 million bitcoins by 2140. Even when no more coins are left for mining, miners can still be incentivized through transaction fees in order to keep the network secure.

How to start mining?

Quite often, the question to ask is not HOW to start mining but whether you SHOULD. Most of the time, mining is not a profitable activity at all. It requires a significant prior investment before seeing any returns. Only if you’re serious about mining at large scale and have access to cheap electricity, you can follow these chosen step-by-step guides:

Bitcoin Mining Guide — Getting started with Bitcoin mining
How to Mine Bitcoins

--

--