Proof of Work
Remember, the challenge is to randomly choose a node that is allowed to add a block to the blockchain. This process is accomplished by a puzzle the nodes need to solve. Nodes that participate in this puzzle are often called miners.
The task goes as follows.
- Create a candidate block with transaction data and the metadata.
- Take the data of the candidate block and hash them. This gives us the block hash from section Consensus.
- If the hash is below a certain target and you are the first one who published the solution, you win.
This sounds easy at first glance. But if the target is low enough (remember that you need a hash lower than the target), it takes many tries to find a correct input combination.
But if we need many attempts, we need to change the input every time at least a little bit (remember the property of our hash function? It said that if you change the input a tiny bit, the hash changes completely).
In order to achieve this small change, there is a variable called nonce in the metadata of each block. And this nonce is our value which is incremented each round.
And now we have a race between all miners. The one who succeeds in finding the correct nonce and its corresponding block hash first wins the race and is allowed to append a new block to the blockchain.
The starting input values are for every miner a bit different. First, because each miner can choose different sets of transactions. And second, because the miner’s address also goes into the hash input. What we need this for we will explain in the incentives section.
The miner with the fastest computer can try more nonces than its competitors. That’s why he has the best chance to win this race. But since the hash output is unpredictable, a miner with lower computing power can be lucky too. It is a bit like an easter egg hunt. If you run fast through the garden searching for easter eggs, you can check many spots, and it is very likely that you find an easter egg before your siblings. But if you happen to look at the wrong spots, your younger and slower siblings can be lucky too by looking in different places that might happen to be the right ones.
If the correct block hash was found, the miner broadcasts this block to the network, and every node checks the puzzle solution. If everything is right, they append the block to the blockchain. Then the race starts over with new values (new transactions and a new previous block hash) for the next block.
You can play a bit with our proof of work simulator. If you click on “mine”, it tries to find a valid nonce and a hash that solves the puzzle. Once the algorithm found the nonce, the block turns green. You can now mine the next block.
If you change anything in a block, all following blocks will turn red.