IBC#025: Bitcoin Core Initialization, Step 11: Importing Blocks
Checking Disk Space
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.
A huge thanks to each of the 156 subscribers of this newsletter and to those who decided to contribute with some sats to this project. It means the world to me!
Last episode was focused on step 9 and 10. Today we are going to move to step 11 of the initialization of Bitcoin Core, with the first step needed before importing the blocks. [Code Link]
Let’s start!
Critical Space Checks
First of all, the code checks that the data directory has enough space available for storing various types of data such as wallets, chainstate, indexes and logs. This is done through the CheckDiskSpace() functions, which checks that the disk has at least 50MiB of free space.
if (!CheckDiskSpace(args.GetDataDirNet())) {
InitError(strprintf(_(”Error: Disk space is low for %s”), fs::quoted(fs::PathToString(args.GetDataDirNet()))));
return false;
}If not, an error is returned with the information related to the chosen directory and the initialization is stopped.
Then, the same is done for the blocks directory.
if (!CheckDiskSpace(args.GetBlocksDirPath())) {
InitError(strprintf(_(”Error: Disk space is low for %s”), fs::quoted(fs::PathToString(args.GetBlocksDirPath()))));
return false;
}Here too an error is returned with the information related to the chosen directory, in case there is not enough space available. If so, the initialization is stopped.
Consider supporting this newsletter by using one of my affiliate links. These are not sponsorships, just products I use everyday. Thank you!
Check Low Storage Space
The, the code checks the current height of the chain stored, which is stored in the chain_active_height integer variable. This is done in a thread-safe manner, through the WITH_LOCK() macro, which means that this will be the only thread able to access the CChain in that specific moment.
int chain_active_height = WITH_LOCK(cs_main, return chainman.ActiveChain().Height());Once this information has been retrieved, the code enters an if clause in case do_reindex and do_reindex_chainstate are false and chain_active_height is either 0 or 1, signaling that we are downloading the blocks for the first time.
if (!do_reindex && !do_reindex_chainstate && chain_active_height <= 1) {
.
.
}Here, the code checks whether the disk has enough space for the projected size of the whole chain.
First it calculates the assumed size in bytes for the block space through the AssumedBlockchainSize() function. The requirement, today, is 810GiB of free space needed for storage. It also calculates the additional bytes needed with respect to the 50GiB required in the previous step.
uint64_t assumed_chain_bytes{chainparams.AssumedBlockchainSize() * 1024 * 1024 * 1024};
uint64_t additional_bytes_needed{
chainman.m_blockman.IsPruneMode() ?
std::min(chainman.m_blockman.GetPruneTarget(), assumed_chain_bytes) :
assumed_chain_bytes};Additional bytes needed are calculated by taking into account whether we are running a pruned node or not. The following syntax allows for conditional code:
Condition ? if condition TRUE choose this : else choose thisWhich results in:
Pruned node ? assumed size of pruned node : assumed size of whole chainFinally, the disk space is checked again with CheckDiskSpace() by taking into account also the expected size:
if (!CheckDiskSpace(args.GetBlocksDirPath(), additional_bytes_needed)) {
InitWarning(strprintf(_(
“Disk space for %s may not accommodate the block files. “ \
“Approximately %u GB of data will be stored in this directory.”
),
fs::quoted(fs::PathToString(args.GetBlocksDirPath())),
chainparams.AssumedBlockchainSize()
));
}In case the available space is less than a threshold (i.e. 810GiB + 50GiB = 860 GiB for the whole chain), a warning is launched showing the user that the available disk space may not be enough for downloading the whole chain.
Let’s keep in touch:
Check out my writings on btc++ insider edition
Try my new app Sats Tracker, an expense tracker app for people living in the Bitcoin standard.
Zap me a coffee and leave me a message: tuma@wallet.yakihonne.com


