# How mining works?

**Cryptocurrencies** are one of the hottest topics being discussed on the Internet today. But before understanding how mining works, we can ask ourselves the following two questions, these are:

**Why is mining necessary?**

Cryptocurrency mining is not just about making money. Miners have an important function in the blockchain network. They solve mathematical problems and approve other users’ transactions over the internet. Miners save blockchains from block fraud and hacker attacks and guarantee the network’s decentralization.But the main function of mining is different. The main function is to reach an agreement the basis on which a transaction can be considered valid so that no user will be able to spend coins which were already spent in another transaction. That’s why receiving bitcoin is just a side effect of doing useful work.

**What is the purpose of crypto miners?**

Whenever a cryptocurrency transaction is made, a cryptocurrency miner is responsible for ensuring the authenticity of information and updating the blockchain with the valid transaction. The mining process involves competing with other crypto miners to solve complicated mathematical problems with cryptographic hash functions that are associated with a block containing the transaction data.The first cryptocurrency miner to solve the puzzle is rewarded by being able to authorize the transaction, and in return for the service provided, crypto miners earn small amounts of cryptocurrency of their own.

Now that we know the purpose of cryptocurrency mining and miners we can move ahead and understand how it works. For that, we can have a look at the structure of a block in the blockchain:

**Table –**Block structure

Block | 8896 |

No. of transactions | 1800 |

Transaction valume | $ 1349873 |

Timestamp | 2017-11-11 01:35:55 |

Relayed by | ViaBTC |

Difficulty | 1765987453897.67 |

Size | 1063.67 kB |

Nonce | 880 |

Block hash | 0000abcdcee78ab745b9f … |

A block actually contains many more fields other than the ones mentioned above. But we will focus on the Block Hash and Nonce field for now.

**Block Hash –**

Well, the data in the blockchain can be “stored” by storing the hashes of data, such that you can at least verify that that’s the data whose hash was stored if you have the data to hash yourself and check against the one in the blockchain. A Merkle tree is one way of doing this. Refer to the following diagram to understand why a Merkle tree is used.

**Figure –**Merkle treeSo to construct a Merkle tree you make a tree data structure with each of the leaf nodes containing the hash of a section of the data to be stored. From there you take the hash of the concatenation of the child nodes, and propagate the value up to the parent, continuing this process up to the tree until a final hash is generated. Even a small change at the leaves will be propagated upwards and change the resultant hash.

A hash is a 256-bit hexadecimal number which is generated using a hashing function, e.g., Bitcoin uses the SHA256 Algorithm to generate a hash.

We know that the purpose of a miner is to calculate the hash of the block for which he’ll be awarded. Seems easy enough. In order to level the field, every miner is restricted to a certain target hash. A target hash implies that for every block that miner mines if it’s calculated hash is greater than the target hash then it’s discarded.

**Nonce –**

This is where the concept of Nonce comes into play. Nonce stands for “Number used only once”. As we know the miner has to calculate the hash which should be below the target hash assigned to him. But he cannot change the Block number, Data, Previous Hash in order to guess a new hash due to the avalanche effect. The only field that he can vary is the Nonce.A nonce is a 32-bit number. So there can be a maximum of approximately 4 billion possible Nonce values as 2^(32) = 4, 294, 967, 296.

The nonce is randomly selected for each iteration. So for each iteration, a random integer between 0 and 4, 294, 967, 296 is selected.

Consider the following table:

Block 8896 No. of transactions 1800 Transaction valume $ 13849673 Timestamp 2017-11-11 01:35:55 Relayed by ViaBTC Difficulty 1456324543655.677 Size 1066.34 kB Nonce 880 Block hash 000abddbcdef673bedb4 …

**Figure –**all possible hashesIn the above figure the circles represent a hash value obtained for a particular nonce value. The Black circles imply that a hash calculated is invalid. Green circle represents a valid hash. The TARGET assigned to a miner implies that a block can be mined by the miner only if the calculated hash consists of 4 leading zeros here (only as an example. Target hash can vary with multiple leading zeros). All hashes greater than that will be discarded.

We start with a nonce value of 88 which as we can see, yields a hash greater than target hash. The nonce value is changed to 777 and the hash is recalculated. We repeat the process until we find a nonce which yields a hash whose value is less than the target hash. According to above figure, this happens at Nonce value 7778. Now the block can be authenticated by the miner and added to the Blockchain.

## Recommended Posts:

- How GPS works?
- How GSM works ?
- Relationship between Data Mining and Machine Learning
- How an Antivirus Works?
- How Google Search Works!!
- How React Native works?
- How Cloud Storage Actually Works !!
- What is 5G Wireless Technology and How it Works?
- How Content Writing at GeeksforGeeks works?
- How ranking in Google Search Works !
- Types of Sources of Data in Data Mining
- Which Programming Language Should I Choose as a Beginner?
- Top Machine Learning Trends in 2019
- Neuralink - A Brain-Computer Interface Technology

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.