This is a read note of Programming Bitcoin Ch10: Networking. The Bitcoin network is a broadcast network, or gossip network. Every node is announcing different transactions, blocks, and peers that it knows about. One thing to note about the networking protocol is that it is not consensus-critical. The same data can be sent from one node to another using some other protocol and the blockchain itself will not be affected.

1 Network Messages

A network message has the following parts:

  • network magic number: 4-bytes, used to identify the network and resume communication. 0xf9beb4d9 for bitcoin mainnet.
  • command: 12 bytes, human-readable. Each command has a specific payload scheme.
  • payload length: 4 bytes, little endian.
  • payload checksum: 4 bytes, the first 4 bytes of hash256 of the payload.
  • payload

2 Network Handshake

The network handshake is how nodes establish communication:

  • A wants to connect to B and sends a version message.
  • B receives the version message, responds with a verack message, and sends its own version message.
  • A receives the version and verack messages and sends back a verack message.
  • B receives the verack message and continues communication.

The payloads exchanged are VersionMessage and VerAckMessage.

3 Getting Block Headers

The command to get the block header is called getheaders. In request, it send number of hashes, start block and end block.