How a blockchain works… Let’s make one!
Have you ever wondered why the blocks on the blockchain are stored one after another?
In this article we are going to talk about the basic blockchain concepts; these concepts will be fundamental to grasp how the proof-of-work works, why the mining requires lots of computational power and how the blocks become immutable.
The blockchain technology uses a mathematical function called Hash which turns data into a 64 character long code. The hash code identifies the block in the blockchain and protects its content.
What is the hash?
The hash is a mathematical function that turns every type of data (like text or file) into a string of 64 characters. The output code never changes for the same input. The hash is a one way function: it is easy to generate a code but starting from a hash code it is impossible to get the original data.
Let’s take an example: this is the link to my website where you can find the hash calculator https://www.danielefavi.com/sha256-hash-calculator/.
Let’s type “test” in the text field and then press the button Calculate SHA256 hash. The generated code is:
9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08
Now let’s type “test.โ (add only a full stop) in the text field Data and press again the button Calculate SHA256 hash. The generated hash is:
4ee3df88f682d376531d8803f2ccbee56d075cd248fc300f55dfe8596a7354b7
Adding a full stop makes a difference, the code of “test” is totally different from the code of “test.” Even the addition of a space or a blank line or change a letter to the capital letter can change completely the hash of the output code.
It is easy to calculate the hash, but it is very difficult to get back the starting data from the hash. For example try to find the word that matches with the following hash:
3866daf66ad5f7e6dbf8c983b06287d7ad254336a172d8f34eb1cb4f25f12b70
You can search for a starting word doing lots of random attempts until you will find the corresponding word (this method is called brute-force).
Let’s create a simple blockchain to understand what the hash is for.
Our blockchain contains simple information: a list of people with their names and ages; the name and the age are separated by a dash:
- Block 1: Natalia-28
- Block 2: John-45
- Block 3: Paul-21
- Block 4: Claire-67
Every block must have an identification code: this code will be calculated with the hash.
In order to build a chain of blocks you must link each block to its previous one: so a block must contains the identification code of the previous block.
For a better understanding let’s build our blockchain.
Let’s start with the first block: since the block 1 has no previous blocks we can choose zero: 0-natalia-28.
Now go to the hash calculator and calculate the hash of 0-natalia-28; the hash is:
51831cdd6acfaee6c7402cea12c58f514bc014d495e185610ee6cc664452d23d
Let’s take only the first 5 digits of this code (to make our life easy) and put them at the beginning of the second block: 51831-john-45.
Go back to the hash calculator and calculate the hash of 51831-john-45; the generated hash is:
4e942233bba491ab530669fa78c03b3e72c4e3940834e3e960d9ca32ed959429
Again, take only the first 5 digits and put them at the beginning of the third block, obtaining 4e942-paul-21.
Repeat the same process for the blocks #3 and #4; the chain that you have built is:
Let’s tamper with the blockchain!
For our friend Natalia, from the first block, letโs change the age from 28 to 25. So, changing the content of the block will result in the change of the hash of the block too.
The new content of the first block is 0-natalia-25 and the first 5 digits of the new hash are 07698.
Since the identification code of the first block changed (before it was 51831 and now it is 07698) we must update the hash code inside the second block that refers to the first block. With this update the content of the second block changes (now it is 07698-john-45) so we have to calculate again the hash of the second block: the first 5 digits of the hash code of 07698-john-45 are 43339.
The hash code of the block #2 is changed, as well as the content of the block #3. The content of the block #3 is 77e83-paul-21.
As you have already guessed, since the content of the block #3 is changed, we must calculate again its hash, which will cause the change of the content and the hash of the block #4.
The change of the content of the block not only invalidates the block itself but causes the invalidation of all the following blocks.
This article showed us how a blockchain is structured. In the next article we will see how to prevent a blockchain from being tampered _
This is the English translation of a post that I wrote for comprarebitcoin.com