Patrick Malara

1 December 2021

Here is brief overview of a Peer-to-Peer network and how it relates to Bitcoin’s. This has to be a brief post, there’s just too much on this topic; however, at the end of the post I have compiled my sources for further reading.

Prerequisites:

What is a Peer-to-Peer network? It’s essentially a network of devices where every device is equal to another. Unlike a Client and Server architecture, where State is managed in one place, data in a Peer-to-peer network is divided among all the Nodes(devices). Each Node in the Network must know about other nodes in the network right, but how are they connected? This is done through a Routing Table or Peer List, which is basically a list of other Nodes in the Network. Once that’s established, a Node can then communicate to other Nodes through whatever protocol the Network follows. Every Node has a list of other Peers in the Network it can communicate with. There’s a lot of complex ways in which this list of Peers is organized and optimized but this is enough to continue.

Okay, how does a network like this start? Well, every aspiring Node will need a copy of the Software or Client that will follow the Peer-to-peer protocol. This software will usually come with a list of cached Nodes or seed Nodes that any joining Node can connect to. Here is Bitcoin’s for example https://github.com/bitcoin/bitcoin/blob/master/src/chainparams.cpp#L121

Easy enough to follow. But how is this Network laid out? There are two options, an unstructured approach where the physical distance is ignored, or structured, where the physical underlay of the Network is considered when building the Overlay. Okay, what is an Underlay and Overlay? Underlay is the actual topology of the Physical distance between nodes, whereas the Overlay is just a digital topology of the nodes. On the one hand, with a structured network, you get better performance, but on the other, you have a much more complicated job of managing the Overlay.

The Network’s topology will frequently update because there are downtimes for nodes where they may be offline for a short period or just never return to the Network. So, one option of having nodes keep up to date on whether another Node is still participating in the Network is by Pinging them. A Ping is sent to one Node, and that Node returns a Pong. This will happen often, and if a certain time elapses between a Ping and the expected Pong, that Node is dropped from the Peer List since its more than likely disconnected. This is a pretty standard Type of Message in a Peer-to-Peer network. But how does this communication to other Nodes work? Most Peer-to-Peer Networks, a historically popular one being Gnutella, follow something called a Gossip Protocol. A Node broadcasts a message to surrounding nodes, which they then echo the broadcast to their surrounding nodes until the Node that needs to hear it, hears it.

And that’s the basic idea of how a message propagates through the Network. But what are some examples of Messages? Well, we went over two types of Messages already, Ping and Pong. However, Gnutella had several more, and Bitcoin has plenty more. In general, though, a way of thinking about a Node is that it acts as both a Client and a Server. So, just like how a Server starts a new Thread when a client connects, whenever a Node is sent a message, it starts a Thread to process it. And the function it uses to process the message is based on its Message type. Below is a scene of how a Bitcoin Node hears about a new block.

Bitcoin’s Network is mostly unstructured, where Transactions and blocks are propagated through the Network. Even though the Network is large, it doesn’t take minutes to harmonize on the latest version of the chain. There are many types of messages in the Bitcoin Protocol; more can be read about them here: https://en.bitcoin.it/wiki/Protocol_documentation.

There is a ton of specifics that I didn’t get to. But hopefully, this provided a little understanding of how a Peer-to-Peer network works. Below is a list of the resources I used that will help get you in-depth on this stuff.


Sources:

https://nakamoto.com/bitcoins-p2p-network/

https://www.esat.kuleuven.be/cosic/publications/article-2631.pdf

http://lia.disi.unibo.it/Courses/PMA4DS1617/materiale/23.P2P.x2.pdf

https://www.youtube.com/watch?v=seGwYw2u3TY&t=522s

https://en.bitcoin.it/wiki/Bitcoin_Core_0.11_(ch_4):_P2P_Network

https://cs.berry.edu/~nhamid/p2p/framework-python.html

https://www.eecg.utoronto.ca/~jacobsen/mie456/slides/p2p-mie.pdf https://www.researchgate.net/publication/3940901_A_Definition_of_Peer-to-Peer_Networking_for_the_Classification_of_Peer-to-Peer_Architectures_and_Applications