Introduction to Blockchain

Hi, First of all let me introduce myself in brief. I am Thathsara Raviraj Udugampala. When i am writing this post i have accomplished 2 years of experience in the software industry as a JavaScript(NodeJS, Angular), Android, PHP and Java Developer. Also I graduated from University College of Dublin. Right now i am working for Virtusa in Java Technology - Telecom Domain. Here I've planned to post on Blockchain for you guys while I'm doing experiments on this field. As the first post of this blog here i will go through the basic terms of Blockchain 😊. I hope after reading this post you will get a brief idea on Blockchain  and it will help you to read and understand my second topic will be: Ethereum. Further on i write articles up to developing a complete Ethereum Application with a UI.
(Contract : +94772183501 / mail: thathsararaviraj@gmail.com)

 

 

What is Blockchain ?

Simply it is a decentralized world wide database. Block mean a record of new Transaction. It can be a record of Medical Data, Money or even about Vote. Once each Block is completed it is added to the chain. With the usage of cryptography Blockchain provides decentralized open database of every transactions involving value. Actually this database is being watched by everyone publicly. Anyone who access the internet can get involve with Blockchain.

Why should you learn Blockchain ?

Because it is the next generation of digital commerce.

Evaluation of Blockchain

If you think the Blockchain concept is a fresh technology you are completely wrong and i am not here to teach you a history lesson but let me showcase important points of the history of Blockchain.

About Blockchain

  • Global singleton instances. 
  • In Blockchain, all transactions are stored in block of data. 
  • All Transactions are atomic - it means if we consider one transaction that whole operation is completed at once or not.
  • Transactions will run independently.
  • immortal - it means you can not remove object from Blockchain. If you want to remove you have to program it to self-remove.
  • It is natively object oriented.
  • It is always accessible if we have internet.
  • It is verifiable - it means anyone can access the Blockchain.
  • It can verify every single transaction from the beginning of the time.
  • Every Transactions executions and validations are done by node in the Blockchain according to the same set of protocols. 
  • After validation they are added to the Blockchain by compromising every node in the Blockchain.
  • These blocks are very hard to manipulate
     
But keep in your mind Blockchain is not a place to save large amount of data for each transaction (Ex : Do not store image). Most data stored in Blockchain is about transactions rather than actual objects.

What is a Node ?

Node is a software that that running on a computer which connected to the Blockchain network. There are two types of nodes we called them Full Client and Light Client. Full node is a node that it participate the mining process. But Light Node are just connected nodes to the network and they do not participate for mining. So full node has the full Blockchain downloaded and available.

What is a Transaction ?

A transaction is broadcasted to the network, processed by miners, and if valid,published on the Blockchain.
It is a write-operation that will affect other accounts, update the state of the Blockchain, and consume Ether (unless a miner accepts it with a gas price of zero).

What is a Block ?

Block is a collection of Transactions (i will explain the Block in details later of this post). Normally once new transaction came to the network it will move the unconfirmed transaction pool. Then miners will pick your transaction and start the mining. The first miner who solve the complex math(Nounce) will broadcast the solution among the others. Then they add that particular solved transaction to their current block after doing some validations. So there is nothing like that your solved transaction should go to a specific block. It can be placed in any available block of each node. And new transaction never be added to an older block because once block is completed it becomes immutable. So what is the limit of a block ?. Actually there is a limitation(size) of a block. In current Blockchain Network average block size is 1MB (real time source).

How does Blockchain work ?

 

Someone should do a transaction or transactions. It (Transaction) sends data and the Contract details(i will explain on Contract in my second Post) which going to be run for this transaction details. Those transactions are sent to the massive peer to peer network called Blockchain. It is a world wide distributed network. Each node consists copy of existing data(Block). Then Transactions are executed and validate according to the Contracts' instructions (ensure all nodes executes same set of rules). When Transactions are executed results will add to the Blockchain as a Block.

Blockchain is built around Hashing. 

What did i mean ?

First of all let me remind you what is Hashing. Hashing is a result of mathematical algorithm which has fixed length we provided before run the algorithm and it doesn't take care about input length. But as you know the result is always unique. As well as hashing is a one way function. Normally we use hashing and salt to save passwords of users on tables to prevent Rainbow attacks (EX : $hash = md5($salt.$password)). we also call hashing as digital fingerprint.

I think most of you are not familiar what i am going to mention now.
Merkle Tree :- is hash of hashes. It means we hash the combination of two hashes.


Any Blockchain will reside on the concept called hashing. Let me prove that. This is the demonstration of Blocks in a Blockchain.

  1. Number :- Block Number, it represent the order in Blockchain(nonce).
  2. Message :- Data (it is saved as encrypted message).
  3. Hash :- Hash of the block header(Block Header = Nonce + Merkle Root + Previous hash)
  4. Nounce :- it is input to the hashing algorithm and it force to change the first part of the hash result is predefined (ex : 000addjw4SFsdfser5rt52 | Three zeros as a prefix). Keep in your mind we can not predict the Nounce. It is done by the algorithm.
  5. Previous message hash :- it is kind of a pointer to the previous Block's Hash but it is not a actual pointer. It contains only a value.
  6. Timestamp :- Block's birthday 😎.
In Ethereum Current Block size is 89KB. So up to that memory, a block in Ethereum can store transactions. So simply it means Block can have many transactions. And the other thing is average time of a block mining is 15 Sec in Ethereum.
As you can see connection between blocks are built around Hashing and Merkle Tree (But where is Merkle Proof on the block?. Keep calm and continue reading). If you change single Block's Message you will break all the Blocks following that particular Block in the Blockchain. To re-correct the Blockchain you have to mine(😮) all the Blocks after the changed Block and change their Nounce and Hash.

What is Mining a Block? if you change an existing Block Message you have to rerun the hashing algorithm to decide which Nounce and Hash to be set or if you are going to add a new transaction record you have to run the hashing algorithm to decide which Nounce and Hash to be set. Once you get your Geth(Ethereum Node), it is not in the state of mining. You have to explicit command the Geth to start mining. There are two ways of mining. They are,
  • CPU Mining - start the mining using your machine Central Processing Unit. But CPU is performance wise slow when it do heavy mathematical calculations comparing to the GPU.
  • GPU Mining - start the mining using your Graphical Processing unit. This is more faster than GPU mining because it is more flexible in their applications. The cost and the electricity taken for mining of GPU is less comparing to the mining.
Also there are two concepts in  mining a block we called, Proof of Work and Proof of Stake. Actually BitCoin uses Proof of Work and Ethereum uses Proof of Stake.
  • Proof of Work - a reward is given to the first miner who solve the complex math (Generating Nounce)
  • Proof of Stake - there is no reward for the first miner but that particular node will be paid by the transaction owner only for the transaction cost. So there is no profit earning in this concept but contribution to this network will bring you some benefits like you build very trustworthy and fast network for your applications. 

What is the importance of the Nounce? in Blockchain networks, there is a rule that every hash string generated for each an every transaction, should have fixed (the predefined no of zeros) no of leading zeros. To generate those fixed no of leading zeros we have to generate different Nounce for those Transactions. So calculating this Nounce takes higher computational power. So generating the Nounce is the most important part of the mining.

What is an Invalid Block? in case of changing the block message without changing the Block Hash related to the message then that particular block becomes invalid block.

Why they introduce this mining mechanism?. The simple answer is they want to make it hard and costly to decide the Nounce and add an invalid transaction to the Blockchain by the mining node. Also they wanted to make it easy to validate transactions by the other nodes after miner decide the Nounce before it add to the Blockchain.

Whole Story (related to Bitcoin Blockchain): when new transaction came to the network they are moved to unconfirmed transactions pool. Then nodes will start the processing on that particular transaction by adding that transaction to their Block. Before they add the transaction they validate it by checking the transaction owner's account balance (just check the relevant address balance). Many nodes can start the processing on same transaction at the same time. But end of the day one node will finish the mining. Then that miner will broadcast the solution(Nounce) to other nodes and they validate that particular Nounce is for the relevant transaction and if it is validated they will add the Block to their Blockchain (Proof of Work).

Let's we dig into Merkle Proof (Merkle Tree)

Besides that the "Previous Hash" section of a Block that it contains Merkle Tree section as well (I mention that in previous diagram as the "message" because otherwise it going to be a more complex diagram). What is a Merkle tree ? The answer to this question is that it is the place where all transactions are abstracted related to this block( keep in your mind, Block is a collection of Transactions ). Let's we study this using a diagram.


As you can see we save the Root Hash / Message in the block. To explain  the whole point of saving the Merkle Tree in a block, just imagine there are many Blocks (5 Blocks) in the chain and you just want to search the particular transaction (Transaction A) is available in 5th Block. Then you just want to query the Merkle Tree of 5th block and dig into that for finding the Transaction A. If it is not available then you can simply move to the previous block using the "Previous Message Hash" section and like wise you can go backward until you find the relevant transaction. 

 Now i believe now you have a some sort of understanding on the term : "Blockchain is built around Hashing". 

What is Fork in Blockchain? 

Dividing the Blockchain into two paths. This can occurs if you want to change the protocol of an existing Blockchain (Intentional) or two or more Miners find the same block nearly the same time (accidental).
Intentional Forks are divided into two categories : Hard Fork, Soft Fork.
  1. soft fork -  updated version of existing protocols but backwards compatible. It means old software can recognize new blocks.
  2. hard fork - updated version of existing protocols but not backwards compatible.

What are the types of Blockchains?

  1. Public Blockchain - In simple terms, public Blockchains can receive and send transactions from anybody in the world (Bitcoin, Ethereum).
  2. Private Blockchain -  This is centralized. One node controls the Blockchain.
  3.  Consortium Blockchain - This is public but this is controlled by selected set of nodes.

Security of Blockchain

  1. Obfuscation - Address of a account is a long string. No one knows who's  physically connected to the relevant address. But everyone knows transactions happened between different account addresses. 
  2. Encryption - Password and Passphrase (AES128). You can't depend on the Obfuscation only because if some connected to the system data then he is freely available all of your data 😇. To prevent that you can encrypt the message/transaction payload. Now data is available for everyone but who knows the key can decrypt the data.

The Big Picture of Distributed Chain of Blocks

  • As you can see, in the network there can be many nodes. 
  • Each an every node contains same set of "chain of Blocks".

Traditional Distributed system VS Blockchain

As my final topic of this post i will demonstrate you the difference between Traditional D.S and the Blockchain.

Now i want to answer to a famous question of Blockchain
Question : Is it true to say 51% of nodes should validate a transaction
Answer : It is not the percentage of nodes that matters, but the percentage of total network hashing power. If 51% of the network's hashing power is mining on top of a block, then you could say the network considers all the transactions in that block to be valid, and accepted.

My Next Post : Ethereum Blockchain

Comments