Distributed Blockchain Synchronization
University Projects #Distributed Computing#Python
Overview
A distributed computing application that builds and validates a blockchain by requesting blocks from peers. This application was built in Python and uses Sockets to send requests and receive responses from peers.
You can view the full project here.
Key Achievements
- Implemented GOSSIP protocol for peer discovery
- Built consensus mechanism that correctly identifies and syncs to longest chain
- Handled out-of-order block delivery through storage and reordering
- Maintained synchronization through periodic consensus (every 2 minutes)
- Responded to peer requests including BLOCK, CONSENSUS, STATS_REPLY, and GOSSIP_REPLY
Implementation
Network
- Implemented sockets to perform peer-to-peer communication
- Build GOSSIP protocol for peer discovery
- Handled peer timeout (1 minute inactivity) and removal
- Maintained peer list through keep-alive messages (every 30 seconds)
Consensus
- Requested STATS from all known peers to determine chain heights
- Identified longest chain using
max(height,hash)comparison - Broke longest chain ties by counting peers reporting the same (height, hash)
- Requestd blocks round-robin from all peers with longest chain
Block Validation
- Validated nonce length (max 40 characters)
- Verified message count (max 10 per block)
- Checked message length (max 20 characters per message)
- Confirmed hash follows predefined rules
Protocol Details
Joining Network
- Send GOSSIP to 3 random peers
- Reply to unknown GOSSIP IDs via GOSSIP_REPLY
- Start keep-alive timer to timeout peers that don’t GOSSIP_REPLY within 1 minute
- Begin consensus process
Local Block Synchronization
- Request STATS from all known peers
- Wait for STAT_REPLYs
- Identify longest chain and which peers have it
- Request blocks round-robin from peers with longest chain
- Validate blocks and add blocks to local chain
- Handle ANNOUNCEMENTs of newly mined blocks via block validation
Network Block Synchronization
- Perform consensus on local chain before sending STATS_REPLY to peers
- Do consensus every 2 minutes to ensure local chain synchronized with network chain
Technologies
Python, Sockets, GOSSIP Protocol, Consensus Algorithms, Distributed Systems