Have you ever wondered if we can directly transfer money to others without an intermediary body like bank? Have you ever been frustrated with the online transaction fees incurred while booking tickets? Do you think the data about us that we give to third parties are safe? Have you ever wondered a system in which we can store our data with high privacy and trust? Have you ever heard about bitcoin and how is it implemented?
All the above questions and much more will be answered by blockchain, a technology that can change the way in which we see everything, a technology that can be incorporated into any field wherever the data is present. In this blog I’ll explain on the basic fundamentals of blockchain.
What is a blockchain?
In a high level view, we can define blockchain as a series or list of blocks where each of these blocks are cryptographically connected to each other. These blocks constitute data based on the context in which we are using blockchain.
What constitutes a block in blockchain?
A block mainly constitutes the following:
1) Block number
2) Nonce : This field is an integer that will be set when a block will be accepted to be added to the blockchain. More detail on this will be explained in the mining section.
3) Data : This is the data that we need to store with trust and security. This data depends on the context. If the blockchain is implemented for transactions, this data represents each transaction occurring. If the blockchain is implemented for medical patients, this data represents the medical data of each of the patients.
4) Previous block hash : This is a cryptographic hash(eg: SHA-256) of the previous block contents.
5) Current block hash : This is a cryptographic hash( eg: SHA-256) of the current block contents( data, previous block hash, current block hash).
The above image explains how the various blocks in blockchain are connected to each other. The previous hash component is like a reference to the previous block in the chain thereby enabling to traverse through the blockchain.
Why is hashing needed in blockchain?
Each of the blocks in the blockchain must be encrypted so that the data remains protected and secured. This need is implemented with the help of cryptographic hashing. Similar to the fingerprints in human beings which uniquely identify them, it is this cryptographic hash which uniquely maps to a block in blockchain.
The five requirements of hashing algorithm:
1) One way : This says that, with the hash value we shouldn’t be able to recreate the contents of a block.
2) Deterministic : This says that, for a particular data the hash value should remain same all the time. It shouldn’t get varied each time.
3) Fast Computation : Hash value should get calculated very fastly.
4) Avalanche effect : A small change in the data should produce a completely different hash.
5) Must withstand collisions : No 2 sets of data should have the same hash value. This is needed in blockchain inorder to for example : avoid forged documents which claims the ownership of a property which is truly owned by another person. This can happen if another person is able to construct a block with his name in the block that produces the same hash as other one.
One of the most commonly used hashing algorithm in blockchain is SHA256.
Properties of SHA256 are it’s hash value is 256 bits( 64 characters). For any amount of data as input it will only give 64 characters hex value as output.
One of the property of blockchain is that it’s an immutable ledger. What it basically means is that none of the block present already in the blockchain cannot be tampered or hacked by a third party. This is possible because of the above cryptographic linking between the blocks. When any of the block is tampered, then it’s hash will change thereby breaking the link with the rest of the blocks in the chain. So inorder for a hacker to successfully do it, the hash data present in the subsequent blocks should also be updated and inserted which is practically impossible. The reason why is impossible will be discussed in the further topics.
Distributed P2P Network
Another property of the blockchain is decentralization. Blockchain enables a distributed network in which there a large number of nodes of computers present which are interconnected to each other. In all these nodes, the same blockchain for the application is maintained. No single node is the head. All the nodes are having equal priority in holding this ledger thereby making it decentralized. Whenever a block is added by a node in its blockchain copy, it is updated in the rest of the nodes in the network. In the previous section I told that it’s practically impossible to hack the blockchain because, same changes needed to be updated in the rest of the nodes simultaneously. If not, the changed blockchain will recognize this change because rest all the nodes(> 50% nodes) are having different ledger copy. Thereby the changed blockchain will get updated with the correct one. Inorder to hack, either more than 50% of the nodes must be malicious or the hacker needs to update more than 50% nodes simultaneously which is not possible because we are talking about a network which can have millions of computers.
How mining works : The nonce and cryptographic puzzle
On what basis a node is been chosen to add the next block in the blockchain? This is done by a process known as blockchain mining. In mining, all the nodes in the blockchain will be given a cryptographic puzzle that should meet a target. Whoever solves this puzzle in less time will be given the right to add the block in the blockchain. Thereby a reward will also be given to that node. What is this cryptographic puzzle and target? Target is basically a hash that starts with a specific number of zeroes. This specific number or target can vary with different blockchains. The cryptographic puzzle is to find a nonce suitable for the block whose hash will abide to the target specified. Therefore all the nodes will iterate through different nonce inorder to find the suitable one.
Consensus Protocol : Proof-Of-work
This is a protocol used inorder to agree on an addition of a block by a node in the blockchain. When a block is added by a node, it is then propagated to the rest of the nodes in the network. These nodes do a verification process to check whether it’s valid to add that block. This verification process is a very scrutinizing and lengthy process. If more than 50% of the total nodes agree on adding that block, that block will be added to the ledger. If not it will be discarded. Atleast 10 minutes will be taken to add a new block in the blockchain.
Proof-of-work is a common example of this consensus protocol used in bitcoin. This is done inorder to make sure that the miner does some work by using his/her resources to solve the cryptographic puzzle to find the correct hash that meets the target.
Consensus protocol helps mainly in the following:
1) Avoid attacking of hackers : This is explained in the previous section covered.
2) Chain competing : What happens if two nodes solve the cryptographic puzzle at the same time and add the block to their nodes? When this happen, two different blocks will be propagated to the network, thereby making two different copies of the blockchain in the network. This blockchain should be unique. So how is this situation solved? How will they choose which is the correct chain? In this scenario, this consensus protocol comes into play.
Fundamentals of Blockchain was originally published in Data Driven Investor on Medium, where people are continuing the conversation by highlighting and responding to this story.