I like the idea of bitcoin and blockchain. This is a basic proof-of-concept blockchain that I've built. It is based on a peer-to-peer decentralized TCP network.
Warning! It is working only on Windows for now.
Class structure
- Blockchain
- Block
- Transaction
- Server
- Client
- SuperPeer
- Peer
The P2P network is based on Client
and Server
classes.
- Server:
- waits for client to connect,
- accepts connection,
- adds client to list of clients (= list of peers),
- broadcasts the list to all the clients (lets clients know about new client),
- listens to client messages
- Client:
- connects to the server
- lets user interact with the program logic (i.e. blockchain and networking)
- listens to server messages
The core logic lies in two concepts: Peer and SuperPeer
Peer - a classic peer, it's just a client
SuperPeer - a super peer, it's both client
and server
How it all works together
- first, program becomes a Peer
- it tries to connect to the SuperPeer by public address, which is stored in
server_tracker.txt
- if connection is established successfully, it remains Peer
- if it can't connect to the SuperPeer for some reason (most likely there's no SuperPeer to connect to), it becomes a SuperPeer itself and stores its address in
server_tracker.txt
for other peers to connect
If something is not clear for you, check out the source code. I tried to write as much comments as possible to make it easier to understand.
- Self-repairing network. When SuperPeer disables/disconnects, one of peers becomes SuperPeer, which makes it impossible to stop the network.
- Persistent state. Blockchain saves its state across all peers even when SuperPeer disconnects.
- Data broadcasting. All the data is broadcasted between peers.
- CLI interface. A convenient interface built with
PyInquirer
. - Blockchain implementation. This blockchain implementation is similar to original Bitcoin's blockchain, but I simplified some stuff.
- Data serialization -
pickle
- CLI interface -
PyInquirer
- Parallelism -
threading
- Networking -
socket
- Hashes -
hashlib
- Download project
git clone https://github.com/thedenisnikulin/poc_p2p_blockchain_project.git
- Install dependencies
pip install -r ./requirements.txt
- Run
python src/main.py