Skip to content
Tutorials Center
My learning

Topics

Lesson 19 of 41 ยท Consensus and Networks

Peer-to-Peer Networking and Gossip

See how blockchain nodes find peers, gossip blocks and transactions across the network, why latency causes forks, and how light nodes differ from full nodes.

A blockchain has no central server. Instead, thousands of computers called nodes talk directly to each other in a peer-to-peer (P2P) network. When you send a transaction, it does not go to "the blockchain company". It goes to one node, which tells a few others, which tell a few more, until almost everyone has heard about it. This spreading process is called gossip.

The previous lessons treated the network as magic: a miner finds a block and everybody simply agrees. In reality the block takes time to travel, and that travel time is the reason forks happen. In this lesson you will build a small network in memory, measure how fast a block spreads, and compute how often slow propagation creates competing blocks. You already know what forks and reorganizations are from Longest Chain Rule, Forks and Reorgs; here you see where they come from.

Nodes and peers

A node is any computer that runs the blockchain software and follows the protocol. A peer is another node it keeps an open connection to. No node connects to everybody; a typical node keeps somewhere between a handful and a few dozen connections.

A new node needs to find its first peers. It usually does this in three steps:

  1. It contacts a few well-known seed addresses that are built into the software or published as DNS names.
  2. It asks those peers for the addresses of other peers they know.
  3. It connects to a random selection of them and keeps asking for more over time.

Random connections matter. If every node connected only to its geographic neighbors, a network split along a cable failure would be easy. Random links create shortcuts across the whole graph, which keeps the network small in the sense that matters: any node can reach any other in only a few hops.

Nodes also differ in what they do:

Node type Stores Verifies Typical user
Full node Every block (or a pruned recent part) Every rule, itself Exchanges, miners, enthusiasts
Archive node Full history plus all past states Every rule Block explorers, analytics
Light node Block headers only Trusts proofs from full nodes Phone wallets
Miner / validator Full chain plus a mempool Every rule, and produces blocks Mining pools, stakers

How gossip works

Gossip is how an office rumor spreads: you tell a few colleagues, each tells a few more. For a blockchain the sequence is usually:

  1. A node receives a new transaction or block from a peer.
  2. It validates it. An invalid message is dropped and the sender may be penalized or disconnected.
  3. If the message is valid and new, the node forwards it to the peers that have not seen it yet.

Nodes do not blindly push whole blocks. To save bandwidth, a node first sends a short announcement ("I have block 9f3cโ€ฆ"), and the peer asks for the full data only if it does not already have it. Skipping step 2 would let one bad node flood the network with garbage, so validate-then-forward is the rule that keeps gossip safe.

flowchart LR
    M["Miner finds block"] --> A["Peer A"]
    M --> B["Peer B"]
    A --> C["Peer C"]
    A --> D["Peer D"]
    B --> D
    B --> E["Peer E"]
    C --> F["Peer F"]
    D --> F
    E --> F

Each node validates the block and forwards it to peers that lack it; the number of informed nodes grows in rounds.

Simulating the spread of a block

Let us build 40 nodes, give each about four random links with random delays between 20 and 200 milliseconds, and find out how quickly a block created at node 0 reaches everybody. Because every node forwards as soon as it hears, the time a node learns the block is the length of the shortest path from the origin, which Dijkstra's algorithm finds.

Python A block spreading through 40 nodes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Half of the nodes know the block after 175 ms and the last one after 320 ms, even though no node has more than eight peers and the farthest node is only five hops away. That is the power of random links: the time grows roughly with the logarithm of the network size, not with the size itself. Double the number of nodes and you add about one more hop.

Real networks are slower than this toy. Blocks are larger than a few bytes, so each hop includes validation and transfer time. Bitcoin blocks typically reach most of the network within a few seconds. Modern designs shorten this by sending a compact block (only transaction IDs, since peers already have most transactions in their mempool) and by letting nodes start relaying a block before they have fully validated it.

Latency causes forks

While the block from the previous section is still travelling, another miner on the far side of the network may not have heard about it. If that miner finds a valid block at the same height during that window, two competing blocks exist. Half the network follows one, half follows the other: a fork. It resolves when the next block lands on one of them and the longest-chain rule makes everyone switch.

Blocks are found at random moments, so the time between blocks is roughly exponentially distributed. A fork happens whenever the gap to the next block is shorter than the propagation delay. If the average block interval is I seconds and the propagation delay is d seconds, the chance is about 1 - e^(-d/I). Check that against a simulation:

Python Fork rate against propagation delay

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The simulation matches the formula. With a 600-second interval, even a 10-second delay forks under 2% of the time. With a 12-second interval, a 6-second delay forks about 39% of the time, and with a 2-second interval almost two blocks in three would be wasted. This is the real reason a chain cannot simply shorten its block time to be "faster": the faster blocks come, the more work and security is lost to forks. Designs that want short blocks pair them with other rules (such as including competing "uncle" blocks in the reward calculation, or using proof of stake with scheduled proposers).

Also notice a security consequence. Stale blocks are wasted work. An attacker with a very good network connection to many nodes finds out about blocks sooner and spreads their own faster, so they waste less. Fast, well-connected relay networks therefore matter to large miners.

Light nodes and full nodes

Running a full node today means storing and verifying hundreds of gigabytes. A phone wallet cannot do that, so it runs as a light node that downloads only block headers. A header is tiny, about 80 bytes in Bitcoin-like designs, and it contains the Merkle root of the block's transactions. When the wallet wants to know whether a payment is included, it asks a full node for a Merkle proof and checks it against the header. You built this check in Merkle Trees and Proofs of Inclusion.

Python Why header-only is cheap

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The trade-off is trust. A light node checks that a transaction is in a block, and that the block header has valid proof of work, but it does not verify the full rules. If most miners agreed on an invalid block, a light node would follow them, while a full node would reject it. The lighter your node, the more you rely on others being honest.

The network layer is also an attack surface. An eclipse attack surrounds a victim node with malicious peers so it sees a false chain, and a Sybil attack floods the network with fake nodes. Random peer selection, many outbound connections and a few trusted seeds are defenses. See The Double-Spend Problem and Byzantine Faults for the Sybil idea.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is "gossip" in a blockchain network?

    Gossip spreads information through repeated peer-to-peer forwarding. A node validates new data first, then relays it to peers that lack it.
  2. 2. Why do random peer connections help a P2P network?

    Random links keep the graph well connected, so propagation time grows roughly with the logarithm of the network size.
  3. 3. A chain wants to cut its average block interval from 12 s to 2 s without improving the network. What is the likely effect?

    The fork chance is about 1 - e^(-d/I). A smaller interval I with the same delay d makes it much larger.
  4. 4. What does a light node store, and what must it trust?

    A light node keeps small headers and verifies Merkle proofs against them, but it does not re-check all transaction rules, so it depends on the honesty of the majority.

Summary

  • A blockchain runs on a peer-to-peer network of nodes; each node keeps connections to a few random peers.
  • New nodes find peers through seed addresses and by asking existing peers for more addresses.
  • Gossip means validate first, then forward to peers that have not seen the data, usually announcing before sending.
  • Random links keep propagation time proportional to the logarithm of the network size.
  • Forks happen when a competing block is found before the previous one has spread; the chance is about 1 - e^(-d/I).
  • Light nodes store only headers and use Merkle proofs, which saves space but increases trust in full nodes.

Next: Proof of Stake, a consensus method that replaces mining with locked-up money.