Good morning beautiful 86 people following this newsletter and welcome to a new episode of Inside Bitcoin Code.
Let’s continue with our discovery of Bitcoin Core initialization1. In the previous episode you voted to have shorter articles in order not to add too many things at a time, so I will start to follow this request.
Good, no more pleasantries and let’s start!
AppInitMain()
— RPC Server
In computer science a Remote Procedure Call (RPC) is when a program executes a procedure or a subroutine on a different machine; the peculiarity of this procedures is that is written in the same way wether is called locally or remotely2.
/* Register RPC commands regardless of -server setting so they will be
* available in the GUI RPC console even if external calls are disabled.
*/
RegisterAllCoreRPCCommands(tableRPC);
for (const auto& client : node.chain_clients) {
client->registerRpcs();
}
In this code snippet, the RPC commands are registered so that they can be available to other parts of the code (the GUI RPC console, as the comment above reports).
Then, using a range-based for-loop, the RPCs are registered for each client. A range-based for-loop iterates on a list — node.chain_clients
3 is a vector — and gets a reference to each element on the list (client
) and acts on it.
Then, in case the ZeroMQ library is enabled, this type or RPC commands is registered too:
#if ENABLE_ZMQ
RegisterZMQRPCCommands(tableRPC);
#endif
ZMQ is an asynchronous messaging library which is usually used in distributed applications (exactly as Bitcoin Core)4.
Later, in case the -server
command line argument is provided, the RPC server is started and enters warm-up mode. If the server initialization fails, an error is returned signaling that it was impossible to start the HTTP server.
if (args.GetBoolArg("-server", false)) {
uiInterface.InitMessage_connect(SetRPCWarmupStatus);
if (!AppInitServers(node))
return InitError(_("Unable to start HTTP server. See debug log for details."));
}
As the last step of the application initialization, the integrity of the wallet database is verified:
for (const auto& client : node.chain_clients) {
if (!client->verify()) {
return false;
}
}
Here, another range-based for-loop iterated on the clients and checks if the wallet database is ok; in case of negative answer, the function returns FALSE.
Enough for today! Please let me know in the comments if everything was clear or I need to explain something better. Every suggestion or critic is more than welcome.
Thank you again for following Inside Bitcoin Code. See you next time!
Usque in Finem
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!
I love learning new stuff!