- Install Python dependencies:
pip install -r requirements.txt- Start the Network:
To start the P2P network, use the provided run.py script. This script automates the creation of the Docker network, starts the bootstrap node, launches peer nodes, and checks their registration.
python run.pyThis will:
- Create a docker network (p2p_network)
- Start the bootstrap node
- Launch 100 peer nodes
- Wait for nodes to reigster and discover peers
Check Peer Registration -The script automatically checks peer registration by querying the /peers endpoint of the bootstrap node. You can manually verify it by running:
curl http://localhost:5000/peersExpected Output:
{"peers": ["http://node1:5000", "http://node2:5000"]}You can manually start individual nodes using Docker. Here is how to start two nodes:
docker run -d --name node1 -p 5001:5000 p2p-node
docker run -d --name node2 -p 5002:5000 p2p-nodeFile upload and download are supported with local storage on each node.
- Upload a file to a node:
curl -F 'file=@mydoc.txt' http://localhost:5001/upload- Download a file from a node:
Visit
http://localhost:5001/download/mydoc.txtin your browser, or use:
curl http://localhost:5001/download/mydoc.txt -o downloaded_file.txtThe system supports an in-memory key-value store on each node.
- Store a key-value pair:
curl -X POST http://localhost:5001/kv -H "Content-Type: application/json" -d '{"key": "color", "value": "blue"}'- Retrieve a value by key:
curl http://localhost:5001/kv/colorThe system now uses distributed hash table (DHT) for routing key-value storage requests to the appropriate node.
-
How it works:
- SHA-1 hashing is used to determine which node is responsible for a key
- Requests are automatically forwarded to the responsible node
- The node responsible for a key is determined by consistent hashing
-
Benefits:
- Evenly distributes storage responsibilities across all nodes
- Any node can handle requests for any key
- No central coordinator required
curl -X POST http://localhost:5002/message -H "Content-Type:
application/json" -d '{"sender_id": "Node1", "message": "Hello Node2!"}'- Logs will display the following message:
Received message from Node1: Hello Node2!- Receiving node will display the following message:
{"status": "received"}This test script will run a series of automated tests to verify functionality of the various endpoints in the distributed system. The tests will check the ability to upload and download files as well as perform key-value insertions and queries. To execute these tests, simply run the script:
python test_main_endpoints.pyThe result should look like this:
$ python test_main_endpoints.py
[*] Uploading file...
Upload Response: {'filename': 'mydoc.txt', 'status': 'uploaded'}
[*] Downloading file...
Downloaded content: Hello, this is a test document.
[*] Inserting key-value pair...
Insert Response: {'key': 'color', 'status': 'stored', 'value': 'blue'}
[*] Querying key-value pair...
Query Response: {'key': 'color', 'value': 'blue'}• Periodically share peer lists with random neighbors.
• Limit message flooding using TTL (time-to-live) metadata.
To test this run:
python test_gossip.pyThe message received should be:
Gossip sent: {'known_peers': ['http://node1:5000', 'http://localhost:5001', 'http://node2:5000'], 'status': 'gossip received'}
Gossip received at node2: {'known_peers': ['http://node1:5000', 'http://localhost:5002', 'http://node2:5000'], 'status': 'gossip received'}