EPQ Crypto Currency
EPQ Project – Building a Cryptocurrency from Scratch in Python
For my EPQ, I built a working cryptocurrency system completely from scratch using Python. I wanted to understand how cryptocurrencies like Bitcoin actually work underneath, so I implemented everything myself from the blockchain and mining system to the peer-to-peer networking between nodes.
Project Overview
The goal was to create a decentralised network where each computer could store and verify transactions without needing a central authority. I broke the project down into several modules:
- Blockchain module to store blocks and transactions.
- Peer-to-peer network so nodes could talk to each other directly.
- Miner to carry out proof-of-work.
- Web API so users could interact with the blockchain.
I planned and tracked the whole build using a Kanban board, splitting the work into features I could test one by one.
Blockchain Implementation
Each block contained its own hash, the previous block’s hash, and a list of transactions. I used SHA-256 hashing (from Python’s Crypto library) to make each block verifiable, any change in a previous block would break the chain.
For transaction security, I implemented RSA encryption so transactions were signed with a private key and could be verified with a public one. This stopped fake transactions being accepted.
The blockchain data was stored persistently using SQLite, which worked well for testing but wasn’t very efficient for verifying large amounts of data. I had to write custom functions to convert between JSON and tuples to get around data format issues.
Peer-to-Peer Networking
The network used Python sockets so that each node could connect directly to others. Once connected, nodes shared new transactions and blocks. I added a system using threading and “heartbeat” messages to check which nodes were still online, removing any that had dropped out.
Writing this part was probably the hardest. I had to figure out how to handle multiple connections at once without blocking the program, so I used Python’s threading module to give each connection its own thread. I also added a clean shutdown system so all threads would close properly when the program exited.
Proof-of-Work Mining
I wrote a mining module that looked for a valid hash by repeatedly changing a number called the nonce until it produced a hash below the target difficulty. To make this faster, I used the multiprocessing library to split the work across all CPU cores.
Each successful block was broadcast to the rest of the network, and other nodes verified it before adding it to their copy of the chain. My miner managed around 0.6 MH/s, which was decent for Python but far behind real miners.
Web API
Finally, I built a simple Flask web API so I could interact with the blockchain from a browser or another program. It allowed me to view blocks, add transactions, and trigger mining.
The API couldn’t run at the same time as the miner because both used the main thread, something I’d fix in a later version by running them on separate threads or processes.
Results
The finished program could:
- Connect multiple computers into a small network.
- Add and verify transactions.
- Mine blocks using proof-of-work.
- Keep all nodes synchronised automatically.
- Let users interact through an API.
It handled around 2 transactions per second, which is slow but expected for a prototype written in Python.
If I did it again, I’d use a faster language like Go or Rust, replace SQLite with my own data store, and look at GPU mining. I’d also add features like smart contracts to run small programs on the blockchain.
This project taught me a lot about how decentralised systems actually function, from hashing and encryption to concurrency and inter-process communication. It also gave me a much better understanding of how modern cryptocurrencies are built under the hood.