What is a peer-to-peer model and how does Blockchain use it?
By: Verified On Chain
Tags: Distributed Systems, Blockchain, Blockchain Technology, Bitcoin, Peer To Peer

Whenever I talk about Blockchain, I say that it works on a peer-to-peer network. A peer-to-peer network is the key using which Blockchain attains decentralization. Let’s see how

What is a peer-to-peer network?

Suppose there are 4 computers, each connected to the other using a LAN wire. These computers are called peers/nodes.

(Note: A LAN cable is not necessary; computers can be connected by other means as well)

This forms a network of peers which are connected to each other directly, fully capable of communicating with each other without the need of a third party.

Unlike the client-server architecture, where if 2 clients want to communicate, it must go through the server. Suppose there are 2 clients, Client A and Client B. Client A want to send a message to Client B. Now, A will send the message to the server, which will communicate it to Client B. However, in a peer-to-peer network, two peers Peer A and Peer B can communicate with each other directly. Peer A will send the message to Peer B directly.

In a peer-to-peer network, a peer can be both a server as well as a client, meaning, can ask for and share data.

Today’s internet is mostly based on a client-server architecture where data is centralized because it is stored on the server. This is the reason why in client-server, all the clients are connected to the server. For example, in Gmail, all the email data will be stored on a server. When you log into your Gmail account, you are requesting for your data and on successful login, you can retrieve this data. For example, your inbox, sent emails, etc. In the client-server model, all the user data management is done on the server.

A peer-to-peer network is a distributed network, where any one of the peers is tantamount to any other peer in the network. Thus, data between the peers can be fully or partially replicated, or not replicated at all. Replication means that a piece of data will reside locally in each system in the network. In a peer-to-peer network, data management is done on the peer's system.

A centralized network has a single point of failure, meaning if someone takes down the server, the network would come to a halt. Since peer-to-peer network does not have a single point failure, if one system fails, it won’t affect the network.

There are 2 types of peer-to-peer network

Unstructured networks Structured networks

1- Unstructured networks: As the name suggest, in this type of networks peers are connected to each other randomly.

Since no structure imposed, it is very easy to build and has local optimization. However, it is this structure that also brings limitations with itself. When a peer wants to find some data, the request must be flooded through the network so that it reaches the maximum number of peers, but flooding does not ensure that the peer will get what it is finding and also causes unnecessary usage of CPU memory. If the data is common and is stored in many peers of the network, the likelihood of finding the data is high. If the data is rare, it will be unlikely that the peer finds it. Some of the examples of unstructured network protocols are Gnutella and Gossip. Blockchain network uses Gossip protocol, thus making it an unstructured network.

2- Structured networks: In a structured network, peers are arranged in a specific topology.

Structured networks are implemented mostly using distributed hash tables. Here, the peers maintain a list of neighbors. An example of a distributed network that uses distributed hash tables is BitTorrent.

As we have already discussed, Blockchain uses peer-to-peer networks. Let’s see how nodes discover other nodes and connect to them. To explain, let’s consider an example of the Bitcoin client- Bitcoin core. To run a full node in Bitcoin Blockchain, the Bitcoin core client is used in addition to other clients. When you download a Bitcoin core client, you connect with other nodes in order to download the Blockchain. And you will be connected to them in order to update the Blockchain after the initial sync. (Note: Peer is also known as a node)

To connect to a node, the IP address and port of that node are required. Let’s see different methods of node discovery and connection:

Local client's external address (Nodes discover their own external address by various methods):

Firstly, this node will send a request to public web services to get his routable IP address. It will make an HTTP request to the web server, for which it will return a response which will be the IP address of the node. If this call is successful, then it would broadcast the IP address to all the connected nodes. The first attempt to get the IP is made to 91.198.22.70 port 80, which is the checkip.dyndns.org server. If the connection fails, it will make a DNS call to checkip.dyndns.org. Even if this fails, the same procedure will be repeated for another server,www.showmyip.com server.

Connect callback address (Nodes receive the callback address of remote nodes that connect to them):

If a node sends an initial “version” message, then in order to connect, it sends its address in response. After that, your node sends a “getaddr” request message to that node in order to get its address.

DNS addresses (Nodes make DNS request to receive IP addresses):

In order to find other nodes, a node can make DNS requests to different servers, if required. But the node which is requesting the addresses of other peers must include their hostnames. A list is returned as a response, which may contain multiple IP addresses against a hostname. The list of DNS services are as follows:

bitcoin.sipa.be

dnsseed.bluematt.me

bitcoin.dashjr.org

bitcoinstats.com

bitcoin.jonasschnelli.ch

btc.petertodd.org

Hardcoded "Seed" addresses (Nodes can use addresses hard-coded into the software):

The client has hard-coded IP addresses of Bitcoin nodes. It is the last resort to connect to nodes. These nodes are mostly used as a backup, so whenever possible the node moves away from the “Seed” addresses to avoid overloading “Seed” nodes.

Command line provided addresses (Nodes can be provided addresses as command line arguments):

One can add nodes using the command line. The command is -addnode<ip>. Here, <ip> is the IP address of the remote node.

Text file provided addresses (Nodes read addresses from a user provided text file during startup):

Whenever the client starts, it automatically reads a text file with the name “addr.txt” and will add the nodes with the addresses in the text file.

Address relay (Nodes exchange addresses with other nodes):

Upon successful addition of nodes addresses, a node can relay it to other nodes as well. Whenever addresses are added using “addr” message, it can be relayed further.

Addresses stored in the database (Nodes store addresses in a database and read that database on startup):

Whenever AddAddress () is called, the addresses get stored in a database. On startup, these addresses are read from the database and added.

Once a node is connected, it receives data transferred from that node and can message the other nodes with different requests. Blockchain uses gossip protocol for attaining consensus. Suppose, a miner has created a block, he relays this block to other nodes to which it is connected and the other nodes transmit it further. Basically, nodes start gossiping about the block. If you want to learn more about gossip protocol, click here.