In the past few days or weeks, you must have heard a lot about Bitcoin and its ever-growing demand and hence value. It recently crossed the mark of $10,000/bitcoin but a day later fell down $2500 in value, which initiated the discussion of this being just another bubble, which will eventually lose the hype and will fade away. But amidst all this, the developer community has been introduced to the concept of Blockchain which is like the engine that runs or generate a Bitcoin every 10 minutes. Yes, by the time I finish writing this post, a new Bitcoin will have been generated, added to the existing Blockchain. So the question of the day is, What is Blockchain?
Do you know what a LinkedList is? Or any other data structure, which holds data which is connected to each other? If yes, then consider Blockchain, just like those data structure. If you do not know what a LinkedList is, then visualize a chain or a rope connecting multiple containers, to each other forming a chain. A blockchain is immutable, that is it cannot be changed.
Each Block in a blockchain holds some data/record, which can be a transaction data, file, or anything that you want. In case of Bitcoin, it probably contains the information related to the new bitcoin mined. Ignore the mining part as of now, we will explain how that works later.
The most crucial point in a blockchain is that each block is connected to each other using hashes, means every block holds the hash of the previous block, just like LinkedList, where every node holds the reference of the node next to it.
A Block generally contains an Index number, a timestamp, data that we want to keep in the blockchain, Proof of Work and the hash of the previous block.
The index number is simple numbering given to the blocks, which is generally auto-increment field and is automatically set.
The timestamp is to store the time when a block is created.
We will talk about what the Data part can be, and what is Proof of Work in details below.
Hash of the previous block is just the hash representation of the block. A Hash is nothing but an encrypted form of any number or a word or anything so that no one can know what it is. Functions like md5, sha1, sha256 etc are used to encrypt any string or number into a hash.
Proof of work algorithm is a how a new block is added or mined on the blockchain. Proof of work should be a problem which is difficult to solve but easy to verify. It can be a mathematical problem or anything else. In blockchain programs, hexadecimal hashes are used to make the problem difficult to solve, even for a computer with good computational power.
The problem can be as simple as finding the square root of any number, it's always difficult to find the square root of any number, but to verify whether it is correct or not, all you have to do is multiply it by itself.
Let's take a simple example which involves hash function too. In the below equation, if x=5, what should be the value of y so that the hash for multiplication of x and y ends with a 0.
hash(x*y) = adec231....0
Now to solve this problem, all you have to do is find the right value of y, present it for adding a new block to the blockchain, the proof of work will be verified and if matched, the new block will be added to the blockchain.
You must be wondering that this is pretty simple, but in the above example, we have set a simple problem.
In Bitcoin, the proof of work algorithm used is HashCash, which was designed to reduce spam emails in the early 90's. Who would have thought that an algorithm designed in the 90's will lead to the creation of the largest cryptocurrency in the world.
The main reason behind a blockchain running on a network with machines distributed over different geographical locations is to keep it decentralized. But if its decentralized how do we keep all the nodes updated with the same chain? This is done using the principle of consensus, which makes the longest chain authoritative, which means that if chain on any node is corrupted or hacked, then the other node with the correct chain will update the corrupted node. In this way, a consensus is maintained in the network at any given time.
So this is a great way to keep any public data away from harm's way as no one can corrupt it intentionally. Hence a blockchain can be implemented and utilized to store any data which needs to be transparent and which requires a safety mechanism to keep it away from alteration and changes for individual gains.
In country's where corruption rate is way too high, this can be a way to keep important government and public data files safe and secure.
Bitcoin is a virtual cryptocurrency, in reality, which is just a hash code, and is now(in 2017) more valuable than gold. The aim behind bitcoin is to remove banks from the center stage, and device a system of open decentralized ledger - "a blockchain" where the record for all the exchange transactions is kept after verifying them. This decentralized ledger is stored on thousands of mining machines which form the bitcoin network. There are multiple exchanges where anyone can buy and sell bitcoins along with people using their bitcoins to buy anything, all these transactions are collected by the miners in the network, converted into a "block" and is turned into a mathematical puzzle. The first miner in the network to solve the problem and present the Proof of Work is rewarded with 25 bitcoins which they get only after 99 more blocks get added to the blockchain. In the meantime, miners verify all the transaction of the "block" with their data to make sure that all the transactions are valid and not spoofed. Once verified, the proof of work is attached to the block and the block is added to the blockchain.
Hope this tutorial helps you in understanding how Blockchain works and how it is used in Bitcoin. In the next part, we will implement a working blockchain system using python programming language. So stay tuned!