Python Blockchain — How to create Genesis Block and Creating Blockchain

How to create Genesis Block and Creating Blockchain

The tutorial deeply dealt with the intricacies of Bitcoin explaining fully the blockchain architecture. The next step is to build our own blockchain.

Satoshi Nakamoto created the first virtual currency in the world called Bitcoin. Looking at the success of Bitcoin, many others created their own virtual currencies. To name a few − Litecoin, Zcash, and so on.

Now, you may also like to launch your own currency. Let us call this as TPCoin (TutorialsPoint Coin). You will write a blockchain to record all transactions that deal with TPCoin.

The TPCoin can be used for buying Pizzas, Burgers, Salads, etc.

There may be other service providers who would join your network and start accepting TPCoin as the currency for giving out their services. The possibilities are endless.

In this tutorial, let us understand how to construct such a system, — How to create Genesis Block and Creating Blockchain and launch your own digital currency in the market.

Components Involved in creating Blockchain Project Development

  • Client
  • Miners
  • Blockchain


We assume here that the client can both be a supplier and a recipient of TPCoins. Thus, we will create a client class in our code that has the ability to send and receive money.


The miner has to provide a valid proof-of-work to get the mining reward. All the money that miner collects as a fee will be for him to keep.

He may spend that money on buying goods or services from other registered vendors on the network, just the way a Client described above does.

Creating Blockchain

Now, Let’s start procedure making a genesis block.

We assume that the originator of TPCoins initially gives out 500 TPCoins to a known client Dinesh. For this, he first creates a Dinesh instance −

Dinesh = Client()

We then create a genesis transaction and send 500 TPCoins to Dinesh’s public address.

t0 = Transaction (

Now, we create an instance of Block class and call it block0.

block0 = Block()

We initialize the previous_block_hash and Nonce instance variables to None, as this is the very first transaction to be stored in our blockchain.

block0.previous_block_hash = None
Nonce = None

Next, we will add the above t0 transaction to the verified_transactions list maintained within the block −

block0.verified_transactions.append (t0)

At this point, the block is completely initialized and is ready to be added to our blockchain. We will be creating the blockchain for this purpose.

Before we add the block to the blockchain, we will hash the block and store its value in the global variable called last_block_hash that we declared previously. This value will be used by the next miner in his block.

We use the following two lines of coding for hashing the block and storing the digest value.

digest = hash (block0)
last_block_hash = digest

Python Creating Blockchain

TPCoins = []

We will also write a utility method called dump_blockchain for dumping the contents of the entire blockchain.

We first print the length of the blockchain so that we know how many blocks are currently present in the blockchain.

def dump_blockchain (self):
print ("Number of blocks in the chain: " + str(len (self)))

Note that as the time passes, the number of blocks in the blockchain would be extraordinarily high for printing.

Thus, when you print the contents of the blockchain you may have to decide on the range that you would like to examine.

In the code below, we have printed the entire blockchain as we would not be adding too many blocks in the current demo.

To iterate through the chain, we set up a for loop as follows −

for x in range (len(TPCoins)):
block_temp = TPCoins[x]

Each referenced block is copied to a temporary variable called block_temp.

We print the block number as a heading for each block. Note that the numbers would start with zero, the first block is a genesis block that is numbered zero.

print ("block # " + str(x))

Within each block, we have stored a list of three transactions (except for the genesis block) in a variable called verified_transactions.

We iterate this list in a for loop and for each retrieved item, we call display_transaction function to display the transaction details.

for transaction in block_temp.verified_transactions:
display_transaction (transaction)

The entire function definition is shown below −

def dump_blockchain (self):
print ("Number of blocks in the chain: " + str(len (self)))
for x in range (len(TPCoins)):
block_temp = TPCoins[x]
print ("block # " + str(x))
for transaction in block_temp.verified_transactions:
display_transaction (transaction)
print ('--------------')
print ('=====================================')

Note that here we have inserted the separators at appropriate points in the code to demarcate the blocks and transactions within it.

As we have now created a blockchain for storing blocks, our next task is to create blocks and start adding it to the blockchain.

For this purpose, we will add a genesis block that you have already created in the earlier step.

Adding Genesis Block for Creating Blockchain

Adding a block to the blockchain involves appending the created block to our TPCoins list.

TPCoins.append (block0)

Note that unlike the rest of the blocks in the system, the genesis block contains only one transaction which is initiated by the originator of the TPCoins system.

Now, you will dump the contents of the blockchain by calling our global function dump_blockchain


When you execute this function, you will see the following output −

Number of blocks in the chain: 1
block # 0
sender: Genesis
value: 500.0
time: 2019-01-14 16:18:02.042739

At this point the blockchain system is ready to use. We will now enable the interested clients to become miners by providing them a mining functionality!

Affiliate disclosure: This post contains affiliate links,means when you click on it and make a purchase, we receive a small commission



At we help IT students and Professionals by providing important info. about latest IT Trends & for selecting various Academic Training courses.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store

At we help IT students and Professionals by providing important info. about latest IT Trends & for selecting various Academic Training courses.