Creating a Blockchain in Python
Understanding Through Implementation
Digital currencies are a hot topic right now. I wanted to ensure that I have a grasp of the core principles that govern this technology before it somehow impacts my life hence I set out on a mission to learn them.
After spending a a few hours studying the fundamentals of blockchain and its architecture, I embarked on a project where I aimed to create a simplified blockchain in python, in order to cement my understanding and consolidate my learning.
It is always a good idea to not spend too much time engrossed in theory and to reach the practical stage as soon as possible. This way, by actively engaging the brain in problem solving linked to concept that we are trying to solidify, we form neural connections must faster and are able to visualise concepts much clearer.
Moving from a natural language explanation of blockchain to a python implementation has granted me a modular and robust understanding of blockchain architecture and functionality, and has allowed me to build a strong foundation as I continue to learn more about blockchain technology.
In this blog I will detail the process took, the blockchain concepts demonstrated and how this project has benefited me.
Structural Overview
I adopted an object-oriented programming approach where each piece of the blockchain, including the blockchain itself, is a separate class. In Python, classes are like blueprints, and each time we use them, we’re cutting out a unique instance, like cookies from a cookie cutter. When you see self
, think of it as “this specific cookie” that was cut from the mould.
I began with the core components of a blockchain, building each individually, before integrating them all together into a working chain. Each class lives in a separate Python file and is imported into the final blockchain script.
The Block
In blockchain, a block is essentially a storage container that stores transactions. The architecture of a block as shown below consists of various elements.
self.Height
: This is like a block's street number it tells you where it sits in the chain (1st, 2nd, 3rd…).self.Blocksize
: Think of this as how much data the block can hold — like the size of your storage box.self.BlockHeader
: This is the “label” on the outside of the box — it contains key info about what’s inside and where it came from.self.TxCount
: The number of individual transactions inside the block — like how many letters are inside an envelope.self.Txs
: These are the actual transactions, the “contents” of the block.
The Block Header
The block header is one of the properties of the Block class that we created above and it forms a very crucial part of the blockchain architecture.
We notice that the self.blockHash
property of the Block is initially blank. I will cover this after discussing what hashing is and how it is implemented in our python project.
self.version
: Think of this as the rulebook version, which set of instructions this block follows.self.prevBlockHash
: This is a fingerprint (hash) of the previous block. It’s like a link in a chainself.merkleRoot
: his is a single hash that represents all the transactions in the block.self.timestamp
: When the block was created, essentially its date of birth.self.bits
: Represents the target difficulty for mining the block.self.nonce
: A number miners keep changing to find a hash that fits the difficulty rule.self.blockHash
: The unique identifier for this specific block, calculated by hashing the entire header (updated during the mining process).
Hashing is like feeding a sentence into a magical blender that always produces a fixed-length smoothie. No matter how big or small your input is, the blender gives you the same-sized result. More importantly, even the tiniest change in the input gives you a completely different smoothie.
And once it's blended, there's no way to unblend it — you can't reverse a hash to get the original message.
In Python, I used the hashlib
library to create hashes:
This is our hashing function that will be used repeatedly in our blockchain implementation.
Returning to our BlockHeader class, we now wish to implement the mining process. In order for a block to be successfully mined, miners must satisfy a difficulty requirement. Every time they try to solve it with a different ‘solution’ , a new blockHash value is obtained.
The mining function creates a blockHash by concatenating all of the properties of the block header and then running it through the hash function that we created.
Hence we see again that the self.blockHash
is still bank. This is because this value is eventually found by miners during the mining process. They iterate over many nonce values self.nonce
; changing the nonce value changes the header, and every time the header is hashed, we obtain a completely different blockHash value. When the nonce value is so that the newly created blockHash value satisfies the difficulty level, the final blockHash is established.
In my implementation, it is so that the difficulty requirement is reached when the first 4 digits of the newly created blockHash are all 0.
We recall that the header is composed of self.prevBlockHash
, hence with our new blockHash, we form a chain to the previous block.
Putting it all together: The Blockchain
We begin by importing all the required modules that we created and other python modules:
The main features of a a newly instantiated blockchain are the instantiation of the Genesis block (the first block created on the blockchain) and also a mechanism to add mined blocks to the blockchain. This is all exemplified below.
When we run this we see the following:

This is the generation of our Genesis Block. We see all the properties of the block.
We now wish to add a structure to allow us to add more blocks. The full implementation is shown below. I have chosen to create 4 blocks.
The output is shown below:

and so on….
This project has given me a strong understanding of how Blockchain works at a fundamental level. As I now start to research use cases of this technology in applications as well as build my own , I can do so with a strong understanding of the underlying concepts.
I recommend to anyone who is interested in getting into blockchain development to do the same.
Adios