Hello everyone and welcome to this new episode of Inside Bitcoin Code.
A special welcome to the new people that recently subscribed to this newsletter, I hope that you will enjoy what I write. A small suggestion for you: if you know nothing about coding start reading the posts from Learn2Code to get some basic knowledge before diving in the real deal.
Let’s start!
AppInitMain()
— Network Initialization
Before starting with the network initialization the developers of Bitcoin Core warn not to start any connection before the node is started, since the UTXO1 state in not set up yet and there is the risk to set it up twice. This is reported in the following comment inside the code:
// Note that we absolutely cannot open any actual connections
// until the very end ("start node") as the UTXO/block state
// is not yet setup and may end up being set up twice if we
// need to reindex later.
Comments are very useful when someone has to put his hands in another person’s code, or when someone has to understand what a developer is doing and why.
Then some command line arguments are taken from the ArgsManager
:
fListen = args.GetBoolArg("-listen", DEFAULT_LISTEN);
fDiscover = args.GetBoolArg("-discover", true);
The first argument defines if connections should be set from the outside or not, while the second specifies whether or not the node should discover its own IP address. Then, the Options
of the PeerManager
2 are loaded:
PeerManager::Options peerman_opts{};
ApplyArgsManOptions(args, peerman_opts);
After that, the asmap file is read, in case it is configured. An asmap3 is a map that contains a collection of connected IPs; this structure allows to improve the security of the Bitcoin network by diversifying connections and not having the node connect to a single network operator.
At first, a std::vector
containing bool
values is declared. Then, an if
clause checks is the -asmap
argument was passed from the command line; if so, the asmap_path
is read from the command line arguments, otherwise the default way of choosing connections is used.
Some checks are then performed:
checks if the path is not absolute.
checks if the
asmap
path exists.after reading the
asmap
, checks if there is any data inside thestd::vector
.
In the second and third cases, the code returns an error, otherwise the hash of the asmap
version is provided and logged.
{
// Read asmap file if configured
std::vector<bool> asmap;
if (args.IsArgSet("-asmap")) {
fs::path asmap_path = args.GetPathArg("-asmap", DEFAULT_ASMAP_FILENAME);
if (!asmap_path.is_absolute()) {
asmap_path = args.GetDataDirNet() / asmap_path;
}
if (!fs::exists(asmap_path)) {
InitError(strprintf(_("Could not find asmap file %s"), fs::quoted(fs::PathToString(asmap_path))));
return false;
}
asmap = DecodeAsmap(asmap_path);
if (asmap.size() == 0) {
InitError(strprintf(_("Could not parse asmap file %s"), fs::quoted(fs::PathToString(asmap_path))));
return false;
}
const uint256 asmap_version = (HashWriter{} << asmap).GetHash();
LogPrintf("Using asmap version %s for IP bucketing\n", asmap_version.ToString());
} else {
LogPrintf("Using /16 prefix for IP bucketing\n");
}
Later, after checking the existence of the network group manager the netgroupman
object itself is initialized by passing the asmap
to it.
// Initialize netgroup manager
assert(!node.netgroupman);
node.netgroupman = std::make_unique<NetGroupManager>(std::move(asmap));
Finally, the address manager is initialized, too. A temporary address manager is created by passing the netgroupman
and, in case it exists, is then assigned to the addrman
inside the NodeContext
object, node
.
// Initialize addrman
assert(!node.addrman);
uiInterface.InitMessage(_("Loading P2P addresses…").translated);
auto addrman{LoadAddrman(*node.netgroupman, args)};
if (!addrman) return InitError(util::ErrorString(addrman));
node.addrman = std::move(*addrman);
}
Enough for today! Thank you, as always, for reading this article. It really means the world to me!
See you next time!
Tuma
Did you find value in this newsletter? Three way you can support my work:
Offer me a coffee at tuma@getalby.com
Zap me some sats on Nostr (tuma@nostrplebs.com)
Share my work to other plebs who might be interested!
This class manages the connection with other peer nodes.