IBC#029: Bitcoin Core Initialization, Step 12: Start Node
Gathering chain information
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 187 subscribers of this newsletter and to those who decided to contribute with some sats to this project. It means the world to me!
Today we start diving into step 12 of the initialization of Bitcoin Core. [Code Link]
Let’s start!
Gather Chain Information
First of all, the program defines a variable called best_block_time. This variable — defined as an int64_t, a signed integer type 64 bits wide — will store the timestamp of the current tip in UNIX time format.
int64_t best_block_time{};Then, the code starts gathering information about the chain. First of all, it locks the mutex of the ChainstateManager object, so that there is no risk of race conditions. Then, it retrieves the tip of the chain, which is the latest block of the chain presenting the highest amount of work. Assert() makes sure that the returned tip is not nullptr.
Finally, it logs the current size() of the block tree to the user and assigns the current tip height and the tip timestamp to chain_active_height and best_block_time, respectively.
LOCK(chainman.GetMutex());
const auto& tip{*Assert(chainman.ActiveTip())};
LogInfo(”block tree size = %u”, chainman.BlockIndex().size());
chain_active_height = tip.nHeight;
best_block_time = tip.GetBlockTime();Consider supporting this newsletter by using one of my affiliate links. These are not sponsorships, just products I use everyday. Thank you!
Storing Tip Info
The tip_info variable comes directly from the signature of the AppInitMain() function — Discussed in IBC#007. This is an object of type BlockAndHeaderTipInfo, a structure that contains information about the block and the header.
First of all, an if clause checks that tip_info is not nullptr. Then, the software assigns to the struct attributes different values. In particular:
block_heightis assigned the value of the height of the tip of the chain presenting the highest amount of work,chain_active_height.block_timeis assigned the timestamp of the tip of the chain presenting the highest amount of work,best_block_time.verification_progressis assigned the value of the progress, as a fraction between 0.0 — genesis block— and 1.0 — current tip.
if (tip_info) {
tip_info->block_height = chain_active_height;
tip_info->block_time = best_block_time;
tip_info->verification_progress = chainman.GuessVerificationProgress(&tip);
}Then, a second if clause checks that both tip_info and m_best_header — the best header, seen so far, which is not known to be invalid — are both not nullptr. If so, the header_height and header_time are assigned the height and the timestamp of m_best_header respectively.
if (tip_info && chainman.m_best_header) {
tip_info->header_height = chainman.m_best_header->nHeight;
tip_info->header_time = chainman.m_best_header->GetBlockTime();
}Unlock the Secrets of Bitcoin Code with My Upcoming “How to C++” Guide!
Are you ready to dive deep into the world of Bitcoin programming? I’m crafting a concise, beginner-friendly “How to C++” guide – your essential toolkit for navigating and understanding Bitcoin’s codebase on your own. Think of it as a streamlined, action-packed version of Learn2Code!
Launching soon at just 21,000 sats. Pre-order now and get a 21% discount!
Don’t miss out – DM me today for pre-order details, more info, or to secure your spot!
Notify Peer Manager
Then, the chain_active_height is logged to the user as nBestHeigth.
LogInfo(”nBestHeight = %d”, chain_active_height);Finally, the code checks if the PeerManager object in the NodeContext is nullptr. If not, the SetBestBlock() method is called, which sets the height and the timestamp of the best block.
if (node.peerman) node.peerman->SetBestBlock(chain_active_height, std::chrono::seconds{best_block_time});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


