P2P Network
1. Steps
When a node connects to the blockchain network for the first time, it follows a series of steps to establish a connection, synchronize with the network, and become a fully participating node. Here is an overview of these steps along with the messages typically exchanged:
- Network Discovery:
- The node initiates a connection to one or more seed nodes, which are well-known nodes in the network.
- The node sends a
version message
to the seed node, containing its protocol version, network services, and a nonce.
- Handshaking:
- The seed node responds with a
version message
, containing its own protocol version, services, and nonce.
- The node verifies the seed node's identity and checks protocol compatibility.
- The node may send a
verack message
to acknowledge the successful connection.
- Synchronization:
- The node sends a
getheaders message
to request block headers from the seed node.
- The seed node responds with headers in a
headers message
, containing a sequence of block headers.
- The node verifies the headers, checks their validity and links to the existing blockchain, and requests missing headers if needed.
- The node may send a
getdata message
to request full blocks for the missing headers.
- The seed node responds with the requested blocks in a
block message
.
- Validation and Consensus:
- The node verifies each received block, checking its validity and adherence to the consensus rules.
- The node may send
getdata messages
to request missing transactions referenced in the blocks.
- The seed node or other connected nodes respond with the requested transactions in a
tx message
.
- The node validates and verifies each transaction.
- Peer-to-Peer Networking:
- Once the node has synchronized with the blockchain and validated the data, it becomes a fully participating peer.
- The node can connect to additional nodes in the network and exchange messages such as
version
, verack
, ping
, pong
, inv
, getdata
, etc.
- The node actively participates in the decentralized peer-to-peer network, receiving new transactions and blocks, propagating its own transactions and blocks, and contributing to the network's security and consensus.
1.1. Connection Path
send version message
→ receive version message
→ check protocol compatibility → send verack message
→ getheaders message
→ receive headers message
→ check their validity and links to the existing blockchain
→ getdata message
→ receives block message
→ verifies each block