This project demonstrates a P2P network with two main use cases:
- Normal peer-to-peer interaction
- Power law distribution testing
- Acts as a central registry for peer discovery
- Maintains a list of active peers
- Handles peer registration and peer list distribution
- Manages dead node removal
- Connects to seed nodes for network discovery
- Maintains connections with other peers
- Implements gossip protocol for message propagation
- Performs liveness tests to detect disconnected peers
- Auto-adjusts connections based on node capacity
cd p2p_protocolpython runner.py # Start seed nodes
python peer.py # Start individual peers (enter port when prompted)- Seed nodes start listening on configured ports.
- Each peer:
- Connects to available seeds
- Registers itself
- Gets a list of other peers
- Establishes connections with other peers
- Starts sending gossip messages
- Monitors peer liveness
- Start 3-4 peers in different terminals.
- Observe connection messages.
- Watch message propagation between peers.
- Close a peer and observe how others detect and handle disconnection.
python runner.py # Start seeds
python PeerRunner.py # Start multiple peers
# Wait ~2 minutes for network stabilization
python visualizer.py # View distribution- Socket Management: Peer socket for listening, seed connections, peer connections
- Data Structures:
available_peers: List of known peersmessage_list: Hash table for message deduplicationalive_peers: Active peer trackingpeer_timestamps: Liveness monitoring
- Connection Limits:
def calculate_max_peers(self):
base = 1.5
return int(40 * math.pow(base, -self.node_id / 10))- Sends:
register:IP:PORT - Receives:
registered successfully - Requests: Peer list
- Receives:
peer list:IP1#PORT1:IP2#PORT2...
def handle_peer(self, new_socket):
# Verify peer capacity
# Exchange connection messages
# Start message handling threads
# Initialize liveness monitoring- Message Types:
- Connection messages
- Liveness requests/replies
- Gossip messages
- Dead node notifications
def generate_messages(self, new_socket):
# Generates 10 messages with 5-second intervals
# Uses secure hash for message deduplicationdef handle_peer(self, peer, addr):
# Handles:
# - Registration
# - Peer list requests
# - Dead node notificationsdef dead_node(self, peer, message):
# Removes dead peer from list
# Logs removal
# Notifies other peersdef secure_hash(message):
salt = b"ASSIGNMENT_1"
iterations = 100000
# SHA-512 hashing implementation- Peer capacity checks
- Node ID verification
- Duplicate connection prevention
def liveness_test(self, new_socket):
# Sends request every 13 seconds
# Tracks failed attempts
# Handles peer removal after 3 failures- Records last response time
- Calculates response delays
- Updates peer status
outputfile.log: Network eventsfreqtrack.log: Peer degree tracking
timestamp:event_description
peer_id=>degree
- Based on node ID
- Exponential decay function
- Maximum of 60 connections
def analyze_degrees_plot():
# Reads freqtrack.log
# Calculates distribution
# Generates matplotlib visualizationModify config.txt:
127.0.0.1:8080
127.0.0.1:8081
...
- Start multiple peers
- Disconnect seed nodes
- Observe reconnection behavior
- Monitor gossip message flow
- Verify message deduplication
- Check propagation delays
- Port Conflicts: Use different ports or kill existing processes
- Connection Timeouts: Check seed node availability
- Message Propagation Issues: Verify peer connections
cal connection
- Python 3.x
- Required packages:
pip install matplotlib socket threading logging hashlib- P2P Network Protocols
- Distributed Systems Architecture
- Network Security Best Practices
- Power Law in Network Topology

