An Interactive Guide to the Blockchain, Part IV – Mining and Consensus

Written by Hannes Westermann, Researcher at the Cyberjustice Laboratory and PhD Student at Université de Montréal

Yesterday, we learnt how to create an immutable chain of blocks of information, by linking them together with hashes.

Today, we will explore how this is being used to create consensus between all participants in a huge user network which is the agreed upon version of a blockchain. This is one of the key innovations of the blockchain, as it ensures that all computers in a network will reach agreement over which is the real, valid chain. As said, this is necessary so that participants in the network will be able to look at the chain to read confirmed information, such as verified transactions. They can then rely on this information and, for example, ship goods in response to a payment.

Consensus

Until now, we have looked at a blockchain isolated on only one single computer. However, the blockchain exists in a network, with thousands of computers. The problem here is that several different versions of the same blockchain can exist at the same time. Therefore, it is important to understand how in such a situation the computers agree on the one and only valid version of the blockchain.

To understand this mechanism, we have to look at the rules that we considered so far as characteristics of a valid block:

  • The block has to contain the hash of the previous block.
  • The information stored in the current block + a number (called a nonce) has to result in a nonce starting with 3 zeros.

Let us now consider how these ideas can be used on a network of different computers to reach agreement on some information. In summary, the way this works is that we add the rule that the longest valid chain is the best chain. Once one miner finds (« mines ») a number that makes the hash of a block starts with 3 zeros, he will add this block to the chain and share it  with the entire network, including all other miners. Since this chain is now longer, the miners will not continue to work on an older version, but switch to this new, longer version of the chain to avoid unnecessary work on their outdated shorter version. And because the miners switch to the longer version of the chain, this version  will now add new blocks quicker than any other possible version of the chain, granting it the status of the real, valid chain. Consensus has been established.

Let us explore how this works in detail with an example scenario.

We have Alice and Bob, two writers that want to determine the next sentence in a short story. (This represents transactions being added to the chain). They do not always agree on what should be the next sentence.

We also have three miners, Carol, Dan and Frank. They take the sentences proposed by Alice and Bob and try to confirm these, by hashing the block, and then increasing the nonce until they find a hash that begins with 3 zeros. They also add their own name to the block in order to get credit for confirming the next part of the story. In our example, Carol, Dan and Frank are equally fast at trying new hashes.

Try this chain below. Add a sentence and press “mine”, and Carol, Dan and Frank will try to find a correct hash. Who will get there first?

previous block hash Sentence Nonce Confirmed by Hash
{{prevHash}}0Unconfirmed

Let us consider what happens if there are two versions of this chain. For example, Carol might have received the sentence “It was a beautiful day in Montreal” (version A) and starts trying to confirm this. Dan and Frank, on the other hand, receive the sentence “The sun was shining in Quebec” (version B). Which will be the real beginning of the story?

In our example, both Dan and Frank are working on version B of the story. Therefore, they can try more combinations in the same time, and are likely to find the correct nonce before Carol. Once one of them has found the correct nonce, they will send it to Carol, who will run the hash and see that the solution is indeed correct, e.g. the hash starts with 3 zeros. She will switch to version B and, like Dan and Frank, try to find the hash for the next sentence. Consensus has been established, and version B won.

What prevents Carol from continuing to work on version A? In this case, it is self-interest. Remember that the miners can add their name to the blocks they confirmed. In the real world, this includes a significant monetary reward that each miner receives for the confirmation of a new block in the valid chain. Carol knows that version A of the chain is currently shorter than version B. Therefore, all other miners will switch to version B. If she stays on chain A, she will therefore not be able to catch up to the length of chain B. Her chain is therefore very unlikely to ever become the accepted, valid version. Therefore, she is wasting her efforts – even if she does confirm a block, no other miner will accept the version and she will not get any reward. In order to stand a chance of receiving the reward, she will therefore switch to version B. Consensus has been established.

Play with the two chains below to see how the number of miners affects the chance of a block being confirmed. There are two different chains, each with 0-5 miners. Each miner can do 1000 hashes per step – advance one step by pressing the button below. In this case, the chains save different information. Despite this, the chain with the most miners will confirm blocks (e.g. find a number so that the hash starts with 2 zeros, in this case) faster than the other chain. The number of miners that work on a chain is therefore decisive for the speed of its further development. Catching up to it with another chain on which less miners work is therefore basically impossible.

Name of chain Number of miners Blocks confirmed Total calculations performed

In the Real Blockchain

While most real blockchains do not contain short stories, the considerations taken are exactly the same as in this scenario.

Instead of sentences, the blockchain usually consists of a list of transactions, indicating who sent how much money to whom. And as stated above, instead of competing for the credit of being listed in the “confirmed by” section, real data miners compete for a monetary reward for confirming a block of information in the valid, longest chain.

However, the incentives work the same as in our scenario. Miners compete by trying billions of hashes until they find one that matches a set of criteria – in the case of bitcoin, this is a hash that starts with 19 zeros! This difficulty, e.g. how many zeros a block has to start with, is adjusted roughly every two weeks, so that the average time to confirm a block remains at roughly 10 minutes.

The miner that finds the solution for one valid block - that typically contains a large number of financial transactions - first receives a reward of 6.25 bitcoins, which at today's exchange corresponds to tens of thousands of dollars. These coins are not taken from anybody, rather they are added to the total pool of bitcoins. Further, each transaction that is confirmed by the miner pays a small transaction cost, that also goes to the miner. In order to obtain the reward, people build huge datacenters filled with computers that do nothing but crunch numbers to find a matching hash in the bitcoin blockchain!

As soon as a miner finds the matching hash, they will spread this to the other miners. These will verify the block, and then start mining on this new, longer chain. Occasionally, there are two chains. However, miners face a strong incentive to switch to longer chains in order to avoid being left behind and wasting their efforts. Therefore, consensus is soon re-established. The bitcoin blockchain is today over 600k blocks long – you can see the latest block mined here.

Mining thus serves several different purposes. First of all, it confirms transactions, as people can look at the mined blocks to see which transactions have occurred. Secondly, mining secures the blockchain. The more computers simultaneously calculate hashes, the more difficult it is for a rogue entity to modify previous blocks. Remember, in order to take over a blockchain, an attacker would have to control more than 50% of all computing power on the blockchain. In the case of the bitcoin blockchain, this would be extremely expensive – at the day of writing, it would cost 300k USD per hour. Finally, the mining process serves as the way that many cryptocurrencies add coins to the system. In bitcoin, for example, all coins that are in circulation have been mined at some point. The reward for mining in bitcoin halves every 4 years, until in roughly 2140, when no more rewards are given for mining a block. By this point, there will be a total of 21,000,000 bitcoin – the upper limit.

Conclusion

Today, we learnt how consensus is established on the blockchain. Miners compete to confirm the latest block of information. Once a solution has been found, this block is added to the chain, and miners switch to trying to confirm the next block.

Since miners will switch to the longest chain, an actor who wants to change a previous block on the blockchain - e.g. a hacker who wants reverse a previously registered transaction faces a huge up-hill battle. If the hacker manages to change a block on the blockchain,  all succeeding blocks become invalid, and the hacker would be forced to re-calculate all succeeding blocks faster that the entire miner community and finally overtake them in order to make the hacked block part of the valid, longest blockchain. For large blockchains, such as the bitcoin blockchain, this is practically impossible, which is why it is so secure from hacking.

Read More:

Longest Chain (learn me a bitcoin, 2019)

Bitcoin Halving (Investopedia, 2020)

What is bitcoin mining and how does it work? (Buy bitcoin wordwide, 2020)

Ce contenu a été mis à jour le 9 juin 2020 à 13 h 35 min.