DHT stands for Distributed Hash Table. [TODO]: Describe why TON needs it...
This guide outlines process of conifguration and initialization of dht server for Telegram Open Network based blockchain networks on FreeBSD host, based on example of Newton Blockchain Testnet.
Following this guide you should be able to spawn dht servers for other networks as well, even on the same machine / virtual host if you desire.
Completion of chapter 1 of FreeBSD Telegram Open Network installation guide and as a result:
- Compiled and installed distribution under
/usr/local/opt/ton
- Presence of
/var/db/ton
data directory - Access to internet from the host
We also assume that you chose to create a dedicated user called tond and this user has read/write access to /var/db/ton
directory. You can substitute this username for any other user, just make sure that this account can access /var/db/ton
.
In our example server work directory will be named: /var/db/ton/newton-testnet-dht
.
sudo -u tond mkdir -p /var/db/ton/newton-testnet-dht/{etc,db,log}
sudo -u tond chmod -R 700 /var/db/ton/newton-testnet-dht
This should work if you have a user tond and this user has r/w rights to /var/db/ton
We have created a work directory for our ton dht server.
Configuration of each TON dht server begins with downloading of global configuration file for your network. This file contains some basic information about network, for example it specifies at least one dht server. [TODO]: Why? What's the point here?
Each network (Newton testnet, TFC testnet, TON testnet2, Freeton etc.) has it's own global configuration file. Source of those files depends on the network. We will be using Newton Blockchain testnet global configuration file:
sudo -u tond fetch https://raw.githubusercontent.com/newton-blockchain/newton-blockchain.github.io/master/newton-test.global.config.json -o /var/db/ton/newton-testnet-dht/etc/global_config.json
After this file you should have a file /var/db/ton/newton-testnet-dht/etc/global_config.json
with some data in it. Please do not edit this file, there is nothing what you as dht server operator need to change there.
We have downloaded global configuration file for our dht server, and can generate local configuration.
Next step is to create local configuration using global configuration file downloaded in previous step.
sudo -u tond /usr/local/opt/ton/bin/dht-server --global-config /var/db/ton/newton-testnet-dht/etc/global_config.json --db /var/db/ton/newton-testnet-dht/db --ip IP:PORT --logname /var/db/ton/newton-testnet-dht/log/init.log
Replace IP and PORT with fixed public IP of your dht server and PORT number of your choice, I would advise to use high port number beginning from 20000.
This command will run and exit without any output but if execution was success then local configuration file /var/db/ton/newton-testnet-dht/db/config.json
would be generated. If the file is not there then something went wrong, check log files.
Important: Please note that the command used here is pretty much identical to the one that is used to run actual dht server. If the local configuration file already exists then you will start a dht server. (command will not exit automatically). This is not a problem, just break it off (ctrl_c).
We have generated a local configuration file /var/db/ton/newton-testnet-dht/db/config.json
for our dht server. This file contains quite a few things: our IP address, network port, generated adnl address etc. This is the holy grail of our dht server instance.
Essentially, this is it: your machine can operate as a dht server, you can try and start it by running:
sudo -u tond /usr/local/opt/ton/bin/dht-server --global-config /var/db/ton/newton-testnet-dht/etc/global_config.json --db /var/db/ton/newton-testnet-dht/db --logname /var/db/ton/newton-testnet-dht/log/server.log
If all went well then this command should stay in foreground as long as you do not kill it. Check out the log files for information on what is going on.
It is at this step that I advise you to automate start/stop of the dht server as a system service, I strongly advise to utilize daemontools do do that, please consult Chapter 3 of FreeBSD Telegram Open Network installation guide.
dht-server produces logs in same format as validator-engine, please consult FreeBSD Telegram Open Network full node configuration guide on how the logs are named.
If you want to define your DHT server in network configuration file you must create a signed dht record.
To do so, I have added a helpful mkdht.sh script. This script takes three arguments:
- db directory of your dht server, this is same path you specify with
--db
parameter for dht-server - public ip address of your dht server in decimal notation! you can read it out in
/var/db/ton/newton-testnet-node/db/config.json
- port number of your dht server
Example:
sudo -u tond ./mkdht.sh /var/db/ton/newton-testnet-dht/db 1243007544 22222
If all went ok you should be presented with JSON structure identical to other dht records in network config. Inject it into the config or send to the people wo can do so.