A minimal blockchain network implementation in Python. This project demonstrates the core concepts of blockchain, including blocks, mining, proof of work, and node-to-node communication using a simple REST API.
This project is a Python-based blockchain network where nodes communicate over a REST API. The blockchain supports basic functionalities like transaction handling, mining with proof-of-work, and peer-to-peer communication, making it an excellent way to understand blockchain infrastructure.
- Blockchain Structure: Manages a chain of blocks, each containing transactions.
- Proof of Work (PoW): Implements a mining mechanism that adds computational difficulty to secure the network.
- REST API Node Communication: Exposes endpoints to interact with nodes (e.g., mining, adding transactions).
- Peer-to-Peer Networking: Allows nodes to connect and share information with other nodes in the network.
- Dockerized Node Setup: Easily simulate a multi-node blockchain network using Docker.
- Python 3.9+
- Flask: For creating RESTful API endpoints.
- Hashlib: Used to secure block hashes.
- Docker: For containerizing nodes and simulating a networked environment.
- Python 3.9+: Ensure you have Python installed on your system.
- Docker: Install Docker to containerize and manage multiple nodes.
- Clone the repository:
git clone https://github.com/federico2102/SimpleBlockchainNetwork.git
- Navigate to the project directory:
cd SimpleBlockchainNetwork - Install dependencies:
pip install -r requirements.txt
To simulate a multi-node network, Dockerize each node and connect them within a Docker network.
- Create a Docker Network:
docker network create blockchain-network
- Build the Docker Image:
docker build -t blockchain-node . - Run Containers for Each Node:
docker run -d --name node1 --network blockchain-network -p 5000:5000 blockchain-node docker run -d --name node2 --network blockchain-network -p 5001:5000 blockchain-node docker run -d --name node3 --network blockchain-network -p 5002:5000 blockchain-node
After starting nodes as Docker containers, register each node with others to enable peer-to-peer communication.
-
Register
node2withnode1:Invoke-WebRequest -Uri http://localhost:5000/nodes/register -Method POST -Body '{"nodes": ["http://node2:5000"]}' -ContentType "application/json"
-
Register
node3withnode1:Invoke-WebRequest -Uri http://localhost:5000/nodes/register -Method POST -Body '{"nodes": ["http://node3:5000"]}' -ContentType "application/json"
-
Mine a Block
- URL:
http://localhost:5000/mine - Method:
GET - Description: Mines a new block with pending transactions and adds it to the blockchain.
- Example:
Invoke-WebRequest -Uri http://localhost:5000/mine -Method GET
- URL:
-
View the Full Blockchain
- URL:
http://localhost:5000/chain - Method:
GET - Description: Returns the entire blockchain in JSON format.
- Example:
Invoke-WebRequest -Uri http://localhost:5000/chain -Method GET
- URL:
-
Add a New Transaction
- URL:
http://localhost:5000/transactions/new - Method:
POST - Description: Adds a new transaction to the list of pending transactions.
- Data Format:
{ "sender": "A", "receiver": "B", "amount": 50 } - Example:
Invoke-WebRequest -Uri http://localhost:5000/transactions/new -Method POST -Body '{"sender": "A", "receiver": "B", "amount": 50}' -ContentType "application/json"
- URL:
-
Register a Node
- URL:
http://localhost:5000/nodes/register - Method:
POST - Description: Register a list of peer nodes to enable decentralized communication.
- Data Format:
{ "nodes": ["http://localhost:5001", "http://localhost:5002"] }
- URL:
-
Confirm Node Registration:
- Retrieve the list of registered peers from
node1:Invoke-WebRequest -Uri http://localhost:5000/nodes -Method GET
- Retrieve the list of registered peers from
-
Add and Mine Transactions:
- Add a transaction to
node1:Invoke-WebRequest -Uri http://localhost:5000/transactions/new -Method POST -Body '{"sender": "Alice", "receiver": "Bob", "amount": 25}' -ContentType "application/json"
- Mine a block on
node1to add the transaction to the blockchain:Invoke-WebRequest -Uri http://localhost:5000/mine -Method GET
- Add a transaction to
-
Verify Blockchain Synchronization:
- Retrieve the blockchain from each node to verify they are synchronized:
Invoke-WebRequest -Uri http://localhost:5000/chain -Method GET Invoke-WebRequest -Uri http://localhost:5001/chain -Method GET Invoke-WebRequest -Uri http://localhost:5002/chain -Method GET
- Retrieve the blockchain from each node to verify they are synchronized:
Some possible expansions to this project include:
- Consensus Algorithm: Implement a consensus protocol to synchronize the longest chain across nodes.
- Web Interface: Add a user-friendly front-end for easier interaction with the blockchain.
- Enhanced Peer Discovery: Develop automated peer discovery and dynamic connection handling.
- Improved Security: Use SSL/TLS for secure communication between nodes.