IBC#009: Bitcoin Core initialization — Part 3
Good morning dear readers and welcome to the new episode of Inside Bitcoin Code!
This is the third part of our deep dive inside the Bitcoin Core Initialization. Hope you will enjoy it! You can find part 2 here.
Let’s start!
AppInitMain()
— The scheduler
We finished the previous article talking about threads. Today we start by looking at the object dedicated to the scheduling of the background threads, CScheduler1
. A scheduler is a component whose task is to plan the access of different processes to common resources, in order to optimize the usage of the resources themselves.
The CSheduler
object is found inside the NodeContext
structure (See IBC#004). First of all the assert
function checks if the scheduler has already been defined and, if so, it blocks the program run; on the other hand, the program goes on and initializes the CScheduler
object by allocating the memory for a unique_ptr
.
assert(!node.scheduler);
node.scheduler = std::make_unique<CScheduler>();
Once defined, the scheduler is launched on a separate thread object, of type std::thread
. This object, called m_service_thread
, is a member of the CScheduler
class. When a std::thread
is initialized, it is launched by passing to it a function to be executed as a separate process, and the inputs that the functions itself requires. In this case it calls the util::TraceThread
function, which is basically a wrapper for threads. A wrapper is code that literally wraps around other program components; in fact the TraceThread
function takes as input another function which is then called inside the wrapper. In this case, the function called is a member of the CScheduler
class, serviceQueue()
, which takes the tasks put in its queue and runs them.
// Start the lightweight task scheduler thread
node.scheduler->m_service_thread = std::thread(util::TraceThread, "scheduler", [&] { node.scheduler->serviceQueue(); });
Then the function scheduleEvery()
is called; the goal of this function is to call the function passed as input ( RandAddPeriodic()
in this case) every minute. The RandAddPeriodic()
helps increase the entropy of the system, for when high quality randomness is needed.
// Gather some entropy once per minute.
node.scheduler->scheduleEvery([]{
RandAddPeriodic();
}, std::chrono::minutes{1});
Finally, our CScheduler
is registered in order to allow it to send callback functions ( which are parts of code given as input to other functions).
GetMainSignals().RegisterBackgroundSignalScheduler(*node.scheduler);
AppInitMain()
— Wallet client initialization
In case the corresponding command line optioins are set (-wallet
and -disablewallet
), Bitcoin Core initializes a client interfaces for wallets. Only the interface is initialized, the wallet data will be loaded later on.
g_wallet_init_interface.Construct(node);
uiInterface.InitWallet();
Aaand we are done for today. I am trying to keep the article short in order not to have too many irons in the fire. What do you think? Better longer or shorter articles? Let me know what you think on this poll 👇🏻
Have a nice day guys!
Usque in Finem
Tuma