I'm currently learning how blockchain works, just out of personal curiosity. I'm going through this course and now I've setup the peer-to-peer connectivity using web sockets. Multiple instances of the blockchain application can now run and communicate with each other using these sockets.
The one downside of the course implementation is how the instances know how to find each other. Essentially they need to be explicitly configured to communicate. In my current project, I have it setup with 3 instances. One opens a socket on port 5001 and connects to nothing else. The other opens a socket on port 5002 and connects to the instance on 5001. And the third opens a socket on port 5003 and connects to the instances on 5002 and 5001.
The point is, all three are explicitly configured like that. All three must be started in exactly that order so they can properly connect to the others. While this is fine for a practice implementation, I know that's not how a real blockchain implementation must be working out there in the wild. There must be some mechanism of discovery that takes place which would allow any of these instances to locate whichever others are currently running.
Networking is not my area of expertise, so I'm at a loss on how this could be done.
P2P cryptocurrency clients usually have hardcoded list of peers. These peers are managed by community. If you start you client for the first time, these peers is all you have.
When you connect to another node, it saves your IP in its internal list. Any node can request this list from another node. In this way your client can discover other nodes. Client application saves list of nodes to disk. On the next startup you have hardcoded nodes and nodes you were connected last time. Some nodes may be offline, but this is ok.
More detailed explanation in case of Bitcoin: https://developer.bitcoin.org/devguide/p2p_network.html#peer-discovery
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With