A closure look at Mining approach: Nonce and Target
You won’t actually be reading numbers once you’ve learned them, any more than you read words when you read a book. You’ll be reading meanings!
The most fundamental aspect of mining is nothing but a play of numbers and codes, wrapped in hashing algorithm with security valves of the blockchain system. Well, it amuses me! Isn't it to you?
Before we begin with the mining approach, go through Mining in blockchain, to figure out better.
Here in the above diagram, take a look at this xx…xxx value in the nonce field. This is the value that is responsible for changing the hash so as to solve the problem assigned to miners for mining the block.
Let's have a quick recap of the mining concept before moving ahead.
- Miners: computers that process transactions in the blockchain.
- Mining: transaction processing in the blockchain system.
Picking the transaction from mempool by one of the computer/ nodes of the network and adding its block to the blockchain and verification.
How do the miners pick the transaction and add them in form of blocks in the blockchain ledger?
Miners take the data from the block and change it into something different by applying a mathematical formula to it. That something else is a hash (unique for a block-Hashing algorithm in Blockchain and its properties- read for further clarity)
Miners do the mathematical calculations and apply some logic and try to reach the target. Target is a condition for the hash, which is set by the algorithm of the blockchain itself. Miners keep on changing the hashes of the block so as to reach the target.
So now let us understand what is this mathematical problem for generating hash number and what is this term nonce signify here!
If I ask you to solve the following problem:
2 + _ = 5
The answer is a straightforward number 3. However, if I ask the same to a kindergarten child who learned this, he may take some attempts, following a brute force technique, listing all the possible candidate solutions for the problem.
i.e, 2 + 1 = 3 ; not equal to 5
2 + 2= 4; not equal to 5
2 + 3 = 5; is equal to 5
Similarly, when a problem is generated to mine a block, that mathematical problem has a range of target values. Miners keep on generating the hash to get a value within the target range. Any answer lying between that target range will get accepted.
Here in the above example, in order to get the answer 5, several trials are taken. Number 3 fits which were our target value, however in hashing there is no particular value for the problem. Instead, there is a range of values above a target value which is the answer to the problem.
The blockchain version number, the hash of the previous block, the timestamp, the Merkel Root, the date, the difficulty target, and the nonce are all included in the header’s metadata. Some of which are shown above.
The value that miners are attempting to solve is the nonce.
Nonce stands for numbers only used once.
A nonce is a number that is added to a blockchain’s hashed or encrypted block that, when rehashed, complies with the limitations of the difficulty level.
‘ In the most simple words, a nonce is just the number that miners keep on changing. At every nonce value, a hash value is generated which is compared with the target value. If it meets the target, the problem is solved, else the nonce is changed again and the process continues.’
This basically means, that the mining application generates a random integer, adds it to the hash of the current header, rehashes the value, and then compares this to the target hash. If the calculated hash value satisfies the criteria, the miner has found a solution and is given the block. If the value falls outside of the target range, the nonce is raised by one and the procedure is repeated (just like how the number was increased by one every time in the above example). Until one miner reaches the target, this process continues.
The nonce will most likely not be guessed on the first try. Before getting it right, miners typically test a lot of nonce choices. The harder it is to build a hash smaller than the objective, or the difficulty, the longer it will probably take to find a solution.