Is it hard to build a blockchain from scratch?

Episode 3
In Python

This is the third episode in our series on “is it hard to build a blockchain from scratch ?”, please check the first and second episodes to catch up.

To get started we are going to use the figure from the first episode, let’s copy and paste it here.

fig1. Blockchain (by Sinai)

This is our favorite figure, We will be using it quite often to explain the basic concepts of blockchain technology.

In the previous episodes, we built two basic prototypes using Go and Javascript, let’s now build another baby ledger using python one of the fastest-growing and much-loved programming languages.

To recap, a blockchain is a chain of blocks, each block contains some information listed in figure1. Since we are building a baby ledger let’s stay away from complicated terms and mechanisms that will be covered in the future. I will use the comment sign(#) to explain each line of code, remember everything that comes after the # is a comment.

let’s jump right into it!

let’s first import two important libraries

https://medium.com/media/beed16a650b6a7ff4871aa0cf4198a40/href

These two libraries are for hashing and timestamping each block that is generated.

Create a class called Block

https://medium.com/media/0b067930010322cf1a7b57e680f1e184/href

This class has an initial method that contains all the block information,but there is no any method that returns a block hash, so let’s go ahead and create it under the class Block.

https://medium.com/media/63b5dda706414b4cb0201eb24b82da13/href

When a blockchain is deployed, it has only one block, the first ever mined block, that first block is called the genesis block, all the following blocks will be added on top of the first one, so let’s create a static method that will return the genesis block.

https://medium.com/media/62cf7adb967ff52f54a302f1673c4cd5/href

Each block is followed by the next block, the next block is the recent added block on the chain, we have to declare another static method for returning each new block , let’s create it.

https://medium.com/media/c284454fb057e65948148d89bae2451a/href

The block is made and the new block method is created, now we need to initialize our blockchain for receiving all incoming blocks.

https://medium.com/media/0de4e51bbef5c1757f01d78b3cf8a186/href

Only the genesis block is on the chain, let’s add more blocks to our ledger and print it.

https://medium.com/media/f2048c8e79cc3a76a22fb286ecd7bdc4/href

Here is the result:

Oops! the block number 1 has a hash of the genesis block that is not shown in our blockchain, it’s up to us to show the genesis block or not, let me show you how you can print its contents. Before the for loop, add these lines :

https://medium.com/media/7524535e4af5647313e807386d99d38b/href

Here is the final result:

Now the genesis block becomes visible in the ledger.

Congratulations! You just made another baby blockchain in python.

Stay turned for the next advanced concepts😁.

The entire code:

https://medium.com/media/dd3dbce7a5b4df3d1834d34c0a81117d/href

In case you find any typos or error when running the code, reach out to me:

LinkedIn: Sinai Nday Kabulo

Insta: sinai_nk


Is it hard to build a blockchain from scratch? was originally published in Data Driven Investor on Medium, where people are continuing the conversation by highlighting and responding to this story.