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:
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.
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.
Figure – Merkle tree
So 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.
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 hashes
In 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.