In my previous article we saw how to fork from Bitcoin and create our own blockchain network. We created a new genesis block, changed ports, removed DNS seed configurations, etc. Now we want our nodes to discover other nodes automatically. To do that we must understand how peer discovery works.
How peer discovery works?
As you know, Bitcoin is a decentralized network, meaning there is no central node that we can rely on and that it will be always active. Nodes go up and down. When we start a new local node, it won’t know the IPs of the current active nodes. In order to discover them, it queries one or more DNS names (DNS Seeders). The seeder responses with several IPs of nodes that are active. Now on a local node tries to connect to them. If for some reason this process fails and no IPs were returned, there is a fallback option.
After every release, IPs of the current active nodes are hardcoded. If the local node could not connect to the network via DNS it will try to connect to those hardcoded IPs. If it could not connect to anything then you could manually type IPs of nodes that you know are active.
What is a DNS Seeder?
DNS seeder is a crawler for your new network, which exposes a list of reliable nodes via a built-in DNS server.
At the initial start the DNS Seeder doesn’t know any IPs of active nodes. That’s why we hardcoded IPs in the code. The Seeder tries to connect to the hardcoded nodes. Upon respond the Seeder adds them to the list of active nodes. On their behalf, the responding nodes will be asked for the IPs of other nodes that they are connected to and the Seeder will try to connect to them, too. This is a continuous process. Nodes that are in the list are pinged after a certain time period, if they don’t respond they are removed from the list.
Changes in DNS Seeder.
First, we will need to do some changes on the seeder in order to make him work with the new network.
Update hardcoded IPs
We need to replace bitcoin IPs/ URLs with IPs from our network.
Set Minimum Required Height to 0
Because our network is just in the beginning, we need to set our height to zero otherwise nodes will not be added to the list. It is good to update it when the network grows. So, the DNS Seeder returns a list with working nodes.
Update protocol version
We need to update the protocol version to the one that we are using. List with protocol version and releases here.
Change magic value/ message start
We need to have the same value that we put in our node configuration. Otherwise the seeder won’t recognize nodes as part of this network.
In the readme of the repository there information how to compile it.
REQUIREMENTS ------------ $ sudo apt-get install build-essential libboost-all-dev libssl-dev COMPILING --------- Compiling will require boost and ssl. On debian systems, these are provided by `libboost-dev` and `libssl-dev` respectively. $ make This will produce the `dnsseed` binary.
Setup the Seeder
After the Seeder is compiled. I want to run DNS Seeder on seed.tbtc.petertodd.org. Because DNS Seeder is a Name Server. I need to create a NS record pointing to it(seed-ns1.tbtc.petertodd.org).
seed-ns1.tbtc.petertodd.org 300 IN NS seed-ns1.tbtc.petertodd.org
In the server that seed-ns1.tbtc.petertodd.org is pointing to I can run the seeder with this command:
./dnsseed -h seed.tbtc.petertodd.org -n seed-ns1.tbtc.petertodd.org
Note that you will need root privileges for DNS Seeder to listen on port 53. If you don’t want it to run with root privileges. You could redirect it to other port.
$ iptables -t nat -A PREROUTING -p udp --dport 53 -j REDIRECT --to-port 5353