Blockchain is the buzz word these days. Wherever I go if I see some irregularity happening, I say to myself that I shall replace this with a blockchain. But then looking at it closely, Blockchain can’t be applied to every problem we face in our life.
I knew what bitcoins were. I knew it was some sort of a peer to peer, unregulated, digital currency that shook the world. I knew people like Chamath Palihapitiya, Naval Ravikant who were prominent advocates for bitcoins. But seriously, we are not here to learn about bitcoins, so what is a blockchain?
Bitcoin is the first practical implementation of a blockchain. So, there’ll be no blockchain article out there that doesn’t talk a bit about bitcoins.
Despite knowing what Bitcoins were, I only learnt about blockchain over a year ago, I read an article on Linkedin about Dubai becoming the world’s first blockchain powered government by 2020 and ever since got curious to know what blockchain can do.
Being a techie, I did what most of my peers would do. I asked “Google”. Going through few articles recommended I learnt lots about Blockchain. However, information was scattered everywhere, and I couldn’t find a single post that spoke everything I wanted to know about blockchain development.
So because of this, I thought to write a series of blog post on blockchain. You can consider this as a zero to hero guide as well :) . First I’ll run through the theory behind blockchain and then progressively talk about how to implement one. I am trying my best to write these tutorials so anyone can understand and build their own apps using blockchain irrespective to if you know to program or not. If in case, if anyone runs into difficulty in understanding any concept spoken on this post, please contact me via the comment section or email me your queries to firstname.lastname@example.org
Setting the stage
We all know how the world wide web operates (ehh, I don’t. what is WWW anyway?).
The client-server-model and the peer-to-peer models are 2 of the most widely used networking models used during our times.
The most popular setup is the mighty client-server. It allows all clients (mobile phones, tablets, computers/laptops) to connect to a centralized point (called the server), and this means the centralized point can control anything that passes through it. Files, Chats, emails and what not. Gmail, Facebook and Paypal are all fine examples of a client-server model.
A limitation of the client-server model was noted as the computing power by the central infrastructure was being limited, and so came some scaling inefficiencies. For example, if everyone in the world tend to download a particular video file from a single server, there poses a risk that the server will crash as it cannot handle the load.
To overcome this problem, came the peer-to-peer model where all clients on the network connect to each other with no centralized server. Peers (the devices connected) make a potion of their resources available to other clients on the network, this could be disk storage, network bandwidth or processing power.
Blockchain technology follows the peer-to-peer model where all nodes are equal.
Example application that uses such peer-to-peer network are ‘BitTorrent’ (allows file sharing) and ‘BitCoin’ (Open Source peer-to-peer application that supports exchange of digital currency).
Finally! the golden question. What is a blockchain ?
A blockchain is:
- A decentralized system (no centralized server) where transactions can be appended but not changed (cannot edit or delete a transaction, but can add)
- Uses a shared distributed ledger.
- Maintains transaction immutability by blocks and chaining.
- Leverage consensus mechanism for validating the transaction (The Blockchain to provide an unalterable, shared distributed ledger)
- Uses Cryptography for security.
However, it might seem that the definition I put wasn’t really a definition but I am sure it made you puke.
Don’t just give up hope on becoming a blockchain expert. I will be explaining each point that I have mentioned on the definition, so read on to understand it better:
- Blockchain is a decentralized system
The first point on my definition states that a blockchain is a decentralized system. To understand what a decentralized system is, we need to understand the difference between a centralized and decentralized system.
A centralized system is where there’s a central server, where clients connect to. We discussed this in a previous section thus, noted that these systems have a limitation of having a single failure point and issues with scaling etc.
A decentralized system however is a P2P network (as described in detail above) where all clients connect to each other with no central server, hence a blockchain is a decentralized system.
2. Blockchain uses a shared distributed ledger
To understand what a distributed ledger we need to understand the difference between a standalone ledger, and a distributed ledger.
For example: A bank has a standalone ledger, which is not visible to anyone but only the bank itself. The bank’s customers carryout transaction and that’s when the ledger gets updated.
On the other hand in a distributed ledger, peers in the network will have a copy of the upto-date ledger. Nodes in the network can independently validate the data that’s in the ledger. This keeps data consistent around the network. Each node need not trust any outside entity (like a bank acting as a central authority) to validate that the data is legitimate. More on this in a later section. But hope you understood the main difference between the two.
3. Maintains transaction immutability by blocks and chaining
So what does it mean by transaction immutability: It means that once a transaction-data has been written to a blockchain no one can change it. Not even the system administrator !
Such a functionality can be benefited for audits. As an audit company you can prove that your data hasn’t been altered under any circumstance and as a recipient of the data you can be assured that the data is legitimate and not tampered.
Keep in mind that Blockchain only provides functionalities for adding and retrieving a transaction to and from the ledger. So, this is how they maintain transaction immutability by not allowing editing or deleting of transactions.
How data is managed on the blockchain
Transactions are grouped together, and this called a Block. The first block is called the ‘Genesis Block’ (It’s just a name given, nothing fancy about it). It has an index of zero. Apart from the ‘index’, and the ‘transactions’ the block contains several other attributes such as the ‘timestamp’, the ‘hash value of the data’ and ‘previous hash’. When you add all these together, it looks something like this :
So, as mentioned before all blocks has an index. The first block (genesis) will have an index of zero. Then it contains the ‘Hash’ of that particular block. Then the ‘PreviousHash’ — the previous hash contains the hash value of the previous block. For example: the value of the ‘previousHash’ of block1 (index == 1) contains the value of the ‘Hash’ of block0 (index == 0)
Oh shoot… !!!!! If so how can the first ever block created have a previous hash value ???? Because it’s the first block and there can’t be any other block created before it. In this unique case of the Genesis Block, where there is no previous block, Nakamoto (creator of bitcoin) filled this field with zero bits. So, the ‘previousHash’ value of block0 (index==0) will have the value as 0.
Can I tamper a block and edit the hashes accordingly ?
Just imagine someone changed a transaction of block2 (Index == 2) in the above illustration. Then the hash value of the block1 (index == 1) should match the ‘previoushash’ of block2 (index == 2) and the ‘hash’ value of block2 (index == 2) should be equal to the ‘previoushash’ of block3 (index == 3). This process is repeated until you reach block0 (index == 0). If there’s a mismatch with any of the hash values, then the block can be considered to have been tampered.
However, having such a setting in the blockchain makes it hard or impossible to hack.
Did you know that the following is the block header hash for the first ever block created:
It is being said that the genesis block contains a hidden message in the coinbase transaction. it says “The Times 03/Jan/2009 Chancellor on brink of second bailout for banks.”. It is believed to be a note from the bitcoin creator Satoshi Nakamoto, stating 2 functions. First being marking the date/time of the first block ever created and secondly, economically pointing out the problems with the present Government bailouts of too big to fail banks and the moral hazard problem.
It is very important that you understand the previous section clearly before proceeding.
4. Leverage consensus mechanism for validating the transaction
This is point 4 from my definition of blockchain. So let’s try to understand what it is.
So far you might be overwhelmed by the capabilities a blockchain posses. But, wait! Blockchain doesn’t have a central authority of control and is often governed by the masses, so how can you assure that the data is consistent across the network ?
Consensus algorithms ensures that all peers in the network contains the exact same copy of the ledger. Further, consensus algorithm ensures that all fraudulent transactions from being added to the ledger and guarantees to maintain the chronological ordering of the transactions.
It’s easier said than done, so how does a consensus algorithm work?
There are several consensus protocols available, and the most commonly used protocols are listed below:
- Proof of work
- Proof of state
Let’s discuss these protocols one at a time.
- Proof of work
Any node in the blockchain network can mine (in simple terms Mining means validating the transactions and getting rewarded). In the proof of work protocol, the blockchain will trust whichever ledger that has put the most computational work into solving the puzzle.
Yes, in proof of work, the nodes in the network will have to solve a puzzle in order to add blocks to the blockchain. The puzzle looks something like the following illustration.
You have the list of transactions and there’s also a hidden random value which is used to generate the SHA256 Hash. So, as a miner, taking the Hash value and data into account, you need to guess what the hidden random value is.
It’s like solving the following equation, where X is 10, and you’re suppose to find the value of Y.
Anyway, to increase the level of difficulty to guess this number, the network decides on the difficulty level. The difficulty level is determined by the amount of leading ‘0’s in the hash value. The more the ‘0’s are, the more computational work it requires to solve it.
The first miner to guess this value (it’s something like performing a brute force to guess this value) will win the bid, and since he was the first miner to put in the most computational work to solve the puzzle he will get rewarded. More on this later.
There’s also a term given to refer the ‘hidden random value’, it’s called a NONCE.
BONUS: Did you know that hash keys are ‘’one way hashes’’. Meaning you can’t use the hash key to recreate the original data that was used to generate it ?
The model Proof of Work is resource oriented. It requires loads of computational power such as CPU, memory etc. proof of work is considered to be environmentally unhealthy as it takes a lot of electricity to keep the network alive. If cryptocurrency is used as a central global currency then the earth might run out of electricity if to keep the network alive. Both Ethereum and Bitcoins use proof of work model. However, it is noted that Ethereum will be changing to the ‘Proof of state’ model from August 2017. However, Bitcoin has no such plans for the near future.
Don’t know what Ethereum is? Don’t worry I will be covering this next week.
2. Proof of state
Similar to the proof of work protocol, proof of state will have to solve a puzzle in order to get rewarded. However, there will not be any competition as in the ‘proof of work’ model, where multiple miners compete with each other to solve the puzzle, thus wasting a lot of resources.
In the proof of state model the network will select a node by considering the amount of Stake the particular node holds on the network. The selected node is called the ‘Validator’.
If in case the selected node (or the validator) is not connected to the network at that time, the next eligible validator will get chosen to solve the puzzle.
5. Uses Cryptography for security.
Each node who’s part of the blockchain will have a public and private key pair. The asset owner will use the private key to sign the transaction and anyone can validate this transaction by using the public key of the asset owner. If this is not clear i am sure that the following diagram will make this understand better.
- Denoted by [1 ] Is the owner. He has some asset where he’s going to sell (or transferring the ownership) to [ 8 ].
- The owner creates the transaction — denoted by [ 2 ].
- Using sha256 generates the hash for the transaction — denoted by [ 3 ]
- Signs it with the Owner’s private key — denoted by [ 4 ]
- Therefore generating the encrypted hash value — denoted by [ 5 ]
- The buyer — denoted by [ 8 ] will use the Owners [ 1 ] public key to decrypt the encrypted hash value.
- This will produce a hash value — denoted by [ 7 ]
If both Hash values are equal then it can be denoted as the transaction is authentic and not been tampered.
So, that’s it for the first post on blockchain. I will be giving you more details about some of the sections that I covered in this post in my next article. I have also planned to cover Ethereum, and then start on developing Ethereum blockchain applications in the week after.
If in case you have to clarify anything or any suggestions please email me at email@example.com