P2P Network Architecture
Blockchain networks are peer-to-peer (P2P) systems � there is no central server. Every node is simultaneously a client and a server, sharing data with and receiving data from other nodes. This architecture is what makes blockchain censorship-resistant and fault-tolerant.
Client-Server vs P2P
- Client-server: Central server holds all data. Clients request data from the server. Single point of failure � if the server goes down, nothing works. Single point of censorship � server can block specific users
- P2P: Every peer holds a copy of the data. Peers communicate directly with each other. No single point of failure � Bitcoin has never had a network-wide outage in 15+ years. No single point of censorship � blocking one node has zero effect
- Bitcoin's P2P design: Each node connects to 8-125 outbound peers (default: 8 outbound + up to 117 inbound). Messages flow from peer to peer in a gossip pattern � no broadcasting authority
- NAT traversal: Many nodes are behind home routers (NAT). Bitcoin uses techniques like UPnP, manual port forwarding, or VPS hosting to become reachable. Unreachable nodes can still participate as outbound-only peers
P2P Network Simulation
import random
class BlockchainNode:
def __init__(self, node_id: str, max_peers: int = 8):
self.node_id = node_id
self.peers: set = set()
self.max_peers = max_peers
self.mempool: set = set()
self.blockchain: list = []
def connect_to(self, other_node: "BlockchainNode") -> bool:
if len(self.peers) >= self.max_peers:
return False
self.peers.add(other_node.node_id)
other_node.peers.add(self.node_id)
return True
def broadcast_transaction(self, tx: str, all_nodes: dict, hops: int = 0):
"""Gossip protocol: forward tx to all peers who haven't seen it"""
if tx in self.mempool:
return # Already seen � stop propagation
self.mempool.add(tx)
print(f"{' ' * hops}Node {self.node_id} received tx: {tx} (hop {hops})")
for peer_id in self.peers:
peer = all_nodes.get(peer_id)
if peer and tx not in peer.mempool:
peer.broadcast_transaction(tx, all_nodes, hops + 1)
# Create a small P2P network
nodes = {f"N{i}": BlockchainNode(f"N{i}") for i in range(6)}
# Random connections (like DNS seed bootstrapping)
node_list = list(nodes.values())
for i, node in enumerate(node_list):
for _ in range(3):
other = random.choice(node_list)
if other.node_id != node.node_id:
node.connect_to(other)
# Show network topology
print("Network connections:")
for nid, node in nodes.items():
print(f" {nid}: peers={list(node.peers)}")
# Broadcast a transaction from N0
print("\nTransaction propagation from N0:")
nodes["N0"].broadcast_transaction("TX:Alice->Bob:1BTC", nodes)
print(f"\nNodes with tx: {[nid for nid, n in nodes.items() if 'TX:Alice->Bob:1BTC' in n.mempool]}")Common Mistakes
- Thinking 'decentralized' means no structure � Bitcoin's P2P network has well-defined message types, handshake procedures, and peer management rules. It is structured but not centrally controlled
- Assuming all nodes are equal � full nodes, light nodes, mining nodes, and archival nodes serve different roles and have different network capabilities
Tip
Tip
Practice P2P Network Architecture in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Each block references the previous block's hash, forming an immutable chain
Practice Task
Note
Practice Task — (1) Write a working example of P2P Network Architecture from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with P2P Network Architecture is skipping edge case testing — empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready blockchain code.
Key Takeaways
- Blockchain networks are peer-to-peer (P2P) systems � there is no central server.
- Client-server: Central server holds all data. Clients request data from the server. Single point of failure � if the server goes down, nothing works. Single point of censorship � server can block specific users
- P2P: Every peer holds a copy of the data. Peers communicate directly with each other. No single point of failure � Bitcoin has never had a network-wide outage in 15+ years. No single point of censorship � blocking one node has zero effect
- Bitcoin's P2P design: Each node connects to 8-125 outbound peers (default: 8 outbound + up to 117 inbound). Messages flow from peer to peer in a gossip pattern � no broadcasting authority