Introduction to Blockchain

Yashod Perera
7 min readApr 30, 2020

--

It is chain of blocks with maximum security

Once you read the above description then you will think “What? How the chain of blocks having security and how it map with information technology”. Let me explain every concept behind this with examples one by one.

Roadmap to the blockchain

In this post I am going to describe blockchain using several topics and each and every topic I will cover important building block in blockchains and following are the topics.

  • Hashing
  • Basic structure of a block
  • Chain of blocks
  • Mining
  • Immutable ledger
  • Distributed system

Please go through one by one and understand how the blockchain implements security.

Hashing

Hashing is way of converting a string to a fixed size key or a value to represent the data.

Hash is a key to identify a digital document and it is one way which means we cannot produce the original document using the hash like encrypting decrypting. It is like the fingerprint to identify a person and we cannot describe the person using a fingerprint.

There are lots of hashing algorithms out there like MD4, MD5, SHA256, SHA 512. In blockchains SHA256 algorithm is widely used which is developed by NSA(National Security Agency).

Generated hash using an online tool

Any hashing algorithm should follow some requirements.

  • Must output fixed number of digits — In SHA256 it has 64digits and 256bits.
  • It should be one way — The original value must not be retrieved from hash value
  • Same digital document should get the same value.
  • Must be fast to generate the hash.
  • Can hash any size document (even 1TB+).
  • Must support avalanche effect.

Everything is clear except this avalanche effect. What is it?

Avalanche Effect.

Avalanche effect is when you change even a one bit it should change the hash drastically as follows.

As you see in the above example even removing a one dot it will change the whole hash. Avalanche effect makes the hashing so important to blockchain. I will show it in the mining section.

Can two files have the same hash? Yes. But the chance is very rare and it is called as a collision.

Basic structure of a block

Then will look at the structure of a block.

Basic structure of a block

In the above block I have included the important fields and there are fields such as block version, block size and might be some more depend on the requirements. Let’s go one by one and understand what are those and why they are important.

  • Index — This is the unique key for the block.
  • Time stamp — The time the block is created.
  • Data — The transactions or any kind of data which are stored in the block. This might take hundreds of transaction per block.
  • Previous hash — The previous block hash and this is how blocks are connected as a chain.
Connect using the Previous hash.
  • Merkle tree root hash — This is the next hot topic.

Merkle tree root hash

As I say there are lots of data in a block(This is regarding the Data section in the block). So there should be a way of calculating the hash using those transactions or data. Merkel tree is used to calculate the hash because it is fast as it uses hashes to create the hash as follows. Merkle tree root hash not the hash of the block.

How merkle tree generates the root hash

What if the transactions are even number? Let me explain

If the transactions are odd number it will copy the last block again to make it even and generate the hashes as follows.

Lets’s take a block which has only 5 transactions. First it will copy the hash of the block 6 and make it odd to create the next layer of hashes as follows.

If the blocks are odd then merkle tree duplicate the last block hash and generate till it gets the merkle tree root hash.

  • Hash of the block —Hash of the block is made using the merkle tree root hash, previous hash, nonce etc. I will explain how it is happen in the mining section.

Chain of blocks

In the blockchain, chains of blocks are connected to implement the security. Let’s discuss the first layer of security in the blockchain.

So what is the first block? It is the genesis block

How blocks are connected

To describe the blockchain connectivity I took only the data, previous hash and hash of the block.

  • Scenario 1 — If hacker needs to change a transaction value in the data it changed the hash of the block then the connectivity between the block and the block behind will break.

Oh then the blocks are connected again. No that’s the beginning of the problem

The hash of the block is generated using not only the data of the block. It used many fields such as nonce, previous hash, data, timestamp etc. Then once the block 2 previous hash changed it become invalid because it wont match with the block 2 hash then hacker needs to changed the block 2 hash also to make the block valid. Then hacker have to generate all the hashes behind the originally changed block and replace previous hashes in the blockchain to make it a valid blockchain.

Then generating a hash and replacing can be done fast. Then how the heck this blockchain is secure? Wait that is the fist step only there are more steps to secure it. Let’s talk about mining

Mining

Mining makes the blockchain more secure. In previous I mentioned that the avalanche effect makes hashes more important to blockchain. In mining miners are given a target and they have to calculate the nonce and the hash according to the target.

What is that? Let me explain.

Hash of the block is calculating using the merkle tree hash, previous hash, version, nonce etc.

In the target they given a hash value and miners have to generate the hash value of the block changing the nonce which should be below the target value.

Then miners change the nonce and try to generate a hash value below the target and the first minor who gets the hash value below the target will get a reward.

Then it is easy to be a minor? No it is not.

For mining the hash of the block they need lots of computing power which consumes lots of electricity then it is not easy to be a minor and it is lots of investment and they are given rewards for mining blocks.

Proof of work (POW)

Proof of work comes under mining and there are some other technics such as POC, POS, POA etc for secure blocks (Reference). Proof of work is mining the block below the target by changing the nonce which will take more computer power and time.

Then you can see if hacker change a single piece of data then he/she have to mine each and every block behind that block which takes much computing power and time. Thats the second step of the security. Let’s dive deeper.

Immutable ledger

The Mining process makes the blockchain immutable ledger which means anyone cannot change.

If one have the blockchain and if hacker able to mine all the blocks behind the changed block it can be possible right? No it is not because of the distributed system

Distributed system

Blockchain is not in a single machine it is worked in a peer to peer network avoiding any intermediate parties to get commissions for the transactions done.

In blockchains, blockchains are distributed across many peers and the network always validate blockchains. If any anomaly detects it will check the majority of the blockchains and update the abnormal block. These updates are done within seconds then you can see it is hardly hack this system.

As you see if hacker changes the E blockchain then the network validate all the blockchain and update the E blockchain using valid values. Then it is harder to mine blocks and update more than 50% of the peers within seconds.

Hope you get a better understanding about Blockchains.

If you have found this helpful please hit that 👏 and share it on social media :)

References

  1. Merkel Tree
  2. PoS, PoW, and 12 Other Blockchain Protocols You Didn’t Know About

--

--

Yashod Perera
Yashod Perera

Written by Yashod Perera

Technical Writer | Tech Enthusiast | Open source contributor

Responses (1)