An Interactive Guide to the Blockchain, Part III – A Chain of Hashes
Written by Hannes Westermann, Researcher at the Cyberjustice Laboratory and PhD Student at Université de Montréal
Yesterday, we learnt about cryptographic hashing. Today, we will use this concept to tie multiple blocks of data together, in a chain. This can be used to be sure that no one has changed a previous block.
The blockchain
As we learnt last week, hashes of data can be calculated very quickly. However, it is basically impossible to calculate the data from just the hash. Further, if a tiny part of the data changes, the entire hash is different. The inventors of the blockchain use these properties to create a chain of immutable data. Immutable, in this case, means that the data cannot be altered after it has been created.
The data saved in blockchains often involves monetary transactions. Here, it is extremely important that a transaction cannot be reversed. Imagine the following situation – a person pays for a car, and then receives the car. If the transaction can be reversed, the seller of the car has lost both the car and the money.
In order to create a decentralized currency this immutable data store has to be created in a completely decentralized manner. All nodes in the network have to agree together on which transactions have occurred.
The solution is to create a chain of blocks of information that are linked together, using hashes. This means that the hash of each block does not only use information from only this block, but also adds some information from the hash of the previous block. The result is that if any of the information from the previous block is changed, the hash of the following block will also change – with the consequence that even the following block is no longer valid. In order to verify that the data has not been changed, anyone is able to hash every block in the chain to see that the stored hashes align.
Try this out here:
Block number | previous block hash | Information | Hash |
---|---|---|---|
{{block.number}} | {{block.previous_hash}} | {{block.hashed}} |
{{result_text}}
If you change the information contained in the first block, the hash will change. This is similar to what we tried yesterday. However, here it is possible to add new blocks to the chain. Once you add a block, it contains the hash of the previous block, and some more information. Go ahead and add some blocks to the chain.
Now, try changing the information contained in the first block. As you see, suddenly the chain is no longer valid! The “previous hash” section no longer matches the hash of the previous block. This way, you can see that the data has been modified. In order to make the chain valid again, you have to recalculate each and every block after the changed one, by clicking the “refresh” button for each block succeeding the changed one. Congratulations, we have created our first simple blockchain!
As you can see above, however, even after changing a block that is older i.e. comes before in the blockchain, it is possible to make the blockchain valid again by recalculating each succeeding block. What if we could make this recalculation process slower, in order to make it more difficult to change the previous data?
Let us use proof-of-work, as we discussed yesterday, in order to do this. Until now, we have used one rule to determine whether a block is valid: Checking if the “previous hash”-field in a block matches the hash of the previous hash. Let us add a new rule: The hash of each block has to start with three zeroes. For this purpose, a special number, called a “nonce”, is added to the information in each block. The nonce is then changed and the entire block rehashed, until the calculated hash starts with 3 zeros.
Try this below:
Block number | previous block hash | Information | Nonce | Hash |
---|---|---|---|---|
{{block.number}} | {{block.previous_hash}} | {{block.nonce}} | {{block.hashed}} |
In order for a block to be « valid », you will now have to click the “confirm” button on each block, which will make the computer try successive numbers until it finds one that, combined with the block information, gives a hash that starts with 3 zeros. Further, as you can see if you add some blocks and change some information in an early block, you will have to recalculate the number to add for this, and each and every block after the changed block, in order to make the entire chain valid again! In blockchain-terms, the act of confirming a block of information like this is referred to as “mining” the block.
In our case, mining is pretty quick, since we ask for only 3 zeros at the beginning of a block. However, on real blockchains, the number of zeros required is much higher – around 19 zeros! (You can see the most recently mined blocks on the bitcoin blockchain here). It would take an average computer tens of thousands of years to find a matching hash. Since there are thousands of powerful computers currently trying to find a matching hash for each new block, the average calculation time for the correct nonce is around 10 minutes.
So far, we have looked at the hash in isolation, on one computer. However, remember that the blockchain is shared between many computers. How can mining be used to find consensus between participants in a network? We will learn this in the next presentation.
Conclusion
Today, we learnt how to link blocks of information together into a blockchain using hashes, to create an immutable data store. Tomorrow, we will learn how this is used in the blockchain to find a distributed consensus.
See you soon!
Learn more
Ce contenu a été mis à jour le 9 juin 2020 à 13 h 35 min.