Build Your First Blockchain

In simple terms Blockchain is a continuously growing list of records called blocks, which are linked and secured using cryptography.

Blockchain Technology:

The blockchain is an incorruptible digital ledger of economic transactions that can be programmed to record not just financial transactions but virtually everything of value.
– Don & Alex Tapscott, authors Blockchain Revolution

You can get a detailed information regarding blockchain here – Blockchain Basics

Now lets move to creating our very own Blockchain. In this article, we will create a simple Blockchain in python to demonstrate in a simple way, how the Blockchain technology works.

 
Block Structure

First things first, we need to define the structure of a block in our Blockchain. Each block is stored with a timestamp, an index, datahash (of the present block), previous hash (hash of the previous block). Hash is required to ensure integrity of the blockchain. The hash of the previous block must be found in the present block to preserve the chain integrity.
To create hash of a block we will use index, timestamp, data and previous hash. Here for the cryptographic hash we are using sha256 from the hashlib package.  We create a class Block and _init_() and hash_block() method. init method initialises our block and the hash_block is used to generate the hash.

 
Block Generation

Since now we have the structure of the block we need a function that will generate new blocks. To generate a block we need to know the hash of the previous block and the other elements like  index, hash, data and timestamp of the new block. The function will take the latest block/ previous block in the blockchain and return a new block in the blockchain network.

 
Genesis Block

By now we have defined the structure of a block and created a function to generate new blocks. One legitimate question that arises now is how do we get the very first block of the blockchain??, because a block also need the previous hash to generate a new block. So the answer to this is “genesis block”. The first block is a special block known as “genesis block”. Usually this block is manually added or hard coded. For genesis block we create a function to return the first block for our blockchain. The index of the genesis block is 0, hash is 0 and we can give any arbitrary value to data.

 
Creating Blockchain

We have defined everything we need to get going with creating our blockchain. To create blockchain we first need to generate the genesis block and then the subsequent succeeding new blocks can be added. In our demonstration, the blockchain is a simple Python list. Below we have added the genesis block in the blockchain list. Then we use a for loop to add 5 new blocks to the blockchain list.

 

That’s it now we can run the blockchain to see the demonstration. For this we need to run the blockchain.py to see our blockchain work.

So we can see that demo works and we have successfully created a very simple blockchain. Our blockchain runs on a single machine so not distributed/decentralised. In the future articles we will add more features and make an actual blockchain with proof of work and also add a server layer to run it on multiple machine.

Share This Post!

Leave a Reply

Your email address will not be published. Required fields are marked *