How the actual blockchain works : what goes in to a block? is it incorruptible?

--

Continuing from the my previous story Blockchain — a simple explanation, which will give the basic understanding about the concept behind blockchain technology, I will explain how the actual blockchain is built and how it becomes incorruptible, as simply as possible

-Thilina Chandrathilaka

Most of the rules in the examples applies to the two biggest blockchains out there, Bitcoin and Ethereum.

This post will cover 4 key points

  1. Break the Spreadsheet/Digital Ledger in to blocks
  2. What goes in to a “block”
  3. Who adds a “New” block to the chain ?
  4. Why a blockchain is incorruptible ?

1. Break the Spreadsheet/Digital Ledger in to blocks

The Digital Ledger is one “Big Ledger” in our mind, so lets break the ledger in to small parts which we will call blocks. The full ledger will have multiple transactions in an order and we break it in to few blocks taking one or more transactions in to one block.

[Diagram 01 — Break the digital ledger in to blocks]

and then try to make a chain out of those blocks,

[Diagram 02 — Put the blocks together and build a chain]

There you go ! We just created a blockchain out of the digital ledger.

2. What goes in to a “Block”

In our example, we just added “transactions” in to the blocks. Different blockchains will have their own content defined for a block. Lets take an example of a simple blockchain that keeps transnational data.

[Diagram 3 — What is in a block ?]

Every block will have details of a transaction like

  • A sender : FROM
  • A receiver : TO
  • An Amount

So what is this “Previous Hash” and “Current Hash” ?

These two are used to make sure that the data in a blockchain is not changed or tampered with by anyone once its added to the blockchain. Hashing is a mathematical function that will generate a unique “hash value” for a set of data you provide.

Hash Value for the word “food” is

c1f026582fe6e8cb620d0c85a72fe421ddded756662a8ec00ed4c297ad10676b

in SHA-256

Hash Value for the word “foot” is

22e4f6912a6980d6fd59e0c5409247407491282cc8dbd009734066b8393a5761

in SHA-256

Lets say you provide a set of data to the hash function on a sunny day and get a “Hash Value X”

[Diagram 4 — Sunny day hash generation]

On another day (which is raining like crazy!) if you provide the same set of data to the hashing function, it will provide the same hash value again.

[Diagram 5 — Rainy day hash generation]

So even if its a sunny day or a rainy day, if you try to generate the hash value for the word “food”, it will be “ c1f026582fe6e8cb620d0c85a72fe421ddded756662a8ec00ed4c297ad10676b”

Unique hash for each block

Each block will have their own unique “Current Hash”, which takes the data in that block as the source and create a hash value.

This Current hash becomes the “Previous Hash” value for the next block building a link between all the blocks in the blockchain.

[Diagram 6 — Blockchain, previous hash and current hash]
[Diagram 7 — Data + Previous hash becomes the base data for the Current hash]

Previous block data becomes the data to create the hash, this includes the previous hash.

After creating the hash for the new data, we will add it to the same block. This is called the “current hash”.

When the hash value is in place, it makes sure that the data is not tampered with. Because at any given time, when generating the hash for a specific block, that specific set of data should give the same hash value as recorded in the next block IF the data was never changed.

Lets assume you are a “thief” and want to add your own data, and get away with it !

So if you wants to change a block 1 and add your own data, you need to calculate the hash values for all the blocks after the changed block.

[Diagram 8 — Changing the data in a block]

You change one bit of data in one place and that will create a ripple in the blockchain and the link between blocks, which was created by adding the hash values, is broken.

Lets say you change data in Block 1, generate the hash and manage to add the new hash to Block 2, and this happens..

[Diagram 9 — Change one block and add the hash to the next]

So if you need to get away with what you are doing, you will have to calculate and change each and every block, starting from the one you just changed.

Generating the hash is not easy (in a blockchain) …!

You might be thinking, What if the person who change the data, re-generate the hash values for all blocks after the changed block and update the whole blockchain? True this can happen, given that that person will have the required computing power to accomplish this task.

There is a concept called Proof of Work involved in creating the hash and adding a new block that will make sure no one will be able to have the ability or computing power to do this.

3. Who adds a “New” block to the chain

Any node in the blockchain network can add a new block to the chain.

[Diagram 10 — Nodes in a blockchain network]

If anyone can add a block like this, they can add their own data and corrupt the blockchain and create transactions to their advantage. To avoid this, the creator of the blockchain included something called the “Proof of Work” in to blockchain technology.

What is “Proof of Work” and How it works

From Diagram 03, you can see each block will have the hash of the previous blocks data, this is to verify that the data in the previous block has not been tampered with.

Proof of Work (PoW) — simply makes it difficult to create the Hash, and from all the nodes in the blockchain, whoever gets the correct hash (will explain this in a bit) will get to add the next block.

PoW is NOT something that they created for blockchain, but something they integrated to make sure its secure.

Proof of work defines what the hash value structure should look like

As an example : proof of work will require a node to create the hash with 30 zeros as the first 30 characters of the hash. So the nodes who are trying to come up with the matching hash, will be trying to create a hash with the 30 zeros at the beginning. To do this, they will add a random number to the block data and hash until they get a matching hash with the structure defined by PoW.

“Hashing Algorithm” is a random mathematical algorithm and adding different values to the block data, nodes will try to compute the matching hash. This random value is called a “nonce”

So the task or what you have to do for “Proof of Work” is to find the magical nonce for the word “food” and get a hash value with 30 zeros in the first 30 characters.

I just added 30 zeros to show how it looks like, the actual task is to try different nonce until the “30 zeros” condition is met and finally you can hash the data with the nonce and get the result through the hashing function.

It becomes a race to look for this “Magical Nonce” , prove that you actually did the work and get PAID !

First one who finds a nonce that will give the first 30 zeros for the hash wins and gets to add the new block. And they get PAID for doing this.

So this becomes a race between all nodes. It takes a considerable amount of time to try different nonce and come up with a matching hash to fulfill the PoW.

[Diagram 11— Proof of work]

4. Why a blockchain is incorruptible ?

Changing the data in a previous block and creating the next hash values to mach the changed data becomes impossible because its a race between nodes to add the latest block.

[Diagram 12— Lets assume it takes 10 minutes to complete the POW]

By the time a thief calculate all the proof of work and come up with the new blockchain, a genuine node would have already added the new block after calculating the real hash with a matching nonce that gives the 30 zeros that PoW requires, because the genuine node will have to do this for ONE Block.

As an example : if you change the 2nd block and the new block that needs to be added is block 4, thief need to change blocks 2,3 and 4 to match the changes in the hash values of all blocks.

[Diagram 13 — Genuine node vs a thief]

by the time the thief finish the fake chain, the real node have already won the race and the new block is added to the blockchain.

Lets say our blockchain considers the longest blochchain that is distributed as the current one, so in this scenario, the thief fails and the genuine nodes win.

[Diagram 14— How the thief loose the game]

Just in case..

[Diagram 15— Just in case :)]

Last Word…

In layman terms, this is how the standards and protocols are set for a blockchain to make sure it is an incorruptible technology.

Proof of Work requires a large amount of computing power which leads to high energy consumption. There is an alternative to this called Proof of Stake, my next post will be a short one on Proof of Work and Proof of Stake.

Next : Proof of Work vs Proof of Stake

A short post on how these two methods work and pros and cons of each method.

This article will also include, “ consensus” which is an algorithm used by the blockchain to achieve agreement among nodes to add a new block and a bit on “Mining” .

--

--