Is it hard to build a blockchain from scratch?

Sinai Nday
DataDrivenInvestor
Published in
3 min readJan 28, 2021

--

Episode 3
In Python : Python 2.7.16

Website : https://open-blockchain-project.com/

Improved Golang code (Frontend+backend) : https://github.com/ndaysinaiK/baby-blockchain

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

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

Create a class called Block

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

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.

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

The block is made and the new block method is created, now we need to initialize our blockchain for receiving all incoming blocks. Remember a blockchain(digital ledger) contains blocks.

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

Here is the result:

Oops! block number 1 has a hash of the genesis block, but we can’t see the first block on the ledger. 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 :

Here is the final result:

Now the genesis block becomes visible in the ledger.

Congratulations! You just made another baby blockchain in python.

Stay tuned for the next advanced concepts😁.

Do you know you can click 50 times the medium like button? so go ahead and smash it if you liked this post.👍

The entire code:

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

LinkedIn: Sinai Nday

Some coffee will be much appreciated😉

With Paypal

--

--