Reading Time: 6 minutes
Let’s understand Blockchain and how it works in simple language and through hands-on exercises.
The blockchain is like a distributed database (ledger) where everyone can securely access the data. It is a continuously growing list of records, linked and secured using cryptography.
Cryptocurrencies like Bitcoin are based on blockchain technology and there are many companies have started to use blockchain technology for their different types of applications.
Blockchain technology is based on the following concepts-
Hashing is to execute the mathematical algorithm like SHA 256, it has below characteristics-
- Same output length – Hashing produce the same given length of output regardless of input size.
- It is one-way – Means you can’t generate the input based on the output.
- It is like a digital fingerprint – Generates the same output for the given input every time. And the different output for any change, even any minor change.
Click here or use the box below to play with hashing process. Add or modify input value and it will generate the different hashed value of the same length.
Merkle Tree– It is hash of hashes. It is used to validate a large amount of data quickly and easily in Blockchain.
For e.g. please see below diagram, there are four words (Alexandra, Amy, John, and Maria) in the different block and it has different hash value. In Merkle tree, the group of hashes is used to produce the hash of hashes. And then finally these hashes are used to create the root hash.
You can find any change in the Merkle tree using root hash and follow the down tree.
Block in a Blockchain-
Block consist of data and its resulting hash (this is not final block, we will add more section).
Now, try to add some data in the below input data section and observe the hash value and screen color. Click here to open it in a new window.
It is regenerating the hash but why is screen color getting changed to pink?
Becuase the block is getting invalided even it is regenerating the new hash. But why?
There is one more concept, the first part of the hash should be predefined in the block, it is four zero in this example. So when you are changing the input data, you are not getting the four zero in the resulting hash.
Have you noticed Nonce input box? The Nonce is the input to the hashing algorithm, that will result in first part of hashing value to be predefined like four zero.
Now try to change the Nonce value until you will get the four 0 in the resulting hash.
No luck? It is not possible to predict this value, this is generated by a machine and it is called proof-of-work (remember this word, we will use later).
So whenever we change anything in the block, the hashing algorithm is re-run until its figure out which Nonce to be set to get the four zero. This process is called mining (remember this word too, we will use later).
Ok, so let’s do the mining to get the valid Nonce and make this block valid. Please use the mine button in the above demo section and observer the Nonce, hash value and screen color.
So block will have Nonce, message, and hash with predefined values like four zero, like the below diagram-
Message (data) section of the block can have multiple transactions. The hash of the block is calculated using the hash of the block header and hash of the transactions based on Merkle tree data structure.
A Chain of Blocks-
Block in the chain contains the block number, meaning which order it has in the blockchain. Block also contains the timestamp and hash of the previous block like below diagram-
Now let’s understand through below demo app or click here to access it directly.
Please try to change few things and observe the hash value and behavior-
- Add the data to the last block (#5) – It will invalidate the last block only due to new hash value. You have to use the mine button to make the last block valid again.
- Add the data to the previous blocks – It will invalidate current and all child blocks (remember Merkle Tree) because the hash value will be changed for the current block and it will also update the ‘PrevBHash’ value in all child blocks.
You have to re-mine current and all child blocks to re-generate the nonce and hash value again to make these blocks valid. This helps blockchain to achieve the mutation and resist the change.
But there is a security issue, someone can change the value and re-mined the chain. How do I know my blockchain is re-mined? And how it can be prevented? Read the next section for it.
A Distributed Chain of Blocks-
Chain of Blocks is distributed to a vast number of computers and it exists in the multiple locations. Depending on the blockchain implementation, it could be millions of replication of the chain.
How blockchain is immutable. Rember proof-of-work (mining)? The longest chain (branch) that has the most proof-of-work (using mining), will win and accepted by distributed blockchain. The altered chain would then be rejected by the distributed blockchain and be removed.
Proof-of-work (PoW) is based on computationally intensive puzzles and it uses a lot of energy. Proof-of-stake (PoS) is used by some implementation like Ethereum instead of Proof-of-work and it is based on wealth.
Can anyone alter anything in the blockchain? It is very hard and prevented by math puzzle. Almost same computing resources are required as the rest of blockchain peers in order to altered chain to be accepted. Because he/she has to prove that they put more work (mine) than rest of the peers and created the longest chain. This is called Forks.
Data in the last block of the Blockchain is less secure than previous blocks because if something is changed in the last block, the attacker has to re-mine the last block only. If an attacker wants to change something in the previous block, they have to re-mine all child blocks as well. They need to defeat all the other Peers and create the longest chain (more work than other chain) so that it can be accepted.
Now let’s understand immutability concept through below demo app or you can click here to access this app directly.
There are three peers – Peer A, Peer B, and Peer C and all are having the same copy of blockchain. Let’s say if Peer A will change the data of any block, he/she will have to do the re-mining to make chain valid again. But Peer A will get different hash value and, Peer B and Peer C will have the same value (you just need to compare the last block hash value ).
Peer A can make his blockchain valid using mining but meanwhile, Peer B and Peer C can add new blocks in the chain because mining process takes some time (depending on complexity on the math puzzle). Peer A has to add more blocks and put more work compare to PeerB and Peer C to get his chain accepted. In the real scenario, there could be millions of Peers and attacker has to defeat all other Peers.
The blockchain is global singleton distributed digital ledger and can be used to store everything of value. Blockchain can record transactions between two parties and it is managed by a peer-to-peer network. There are many uses cases like cryptocurrency, food traceability, record management, smart contract etc.
But there is a security issue, everyone can access and read the data from the blockchain and this is not acceptable behavior for many applications. To handle this, the sender encrypts the message and the only receiver with the key can decrypt and understand the message. Click here to read more about encryption and decryption.
This is all about blockchain technology basic. Now you might be wondering how blockchain technology is used by cryptocurrencies like Bitcoin or Ethereum. Please go to my next article to read and understand it through demo app.
And sample code can be downloaded from GitHub.