Inside Bitcoin Code is now live natively on #nostr
Check out the new episode 👇🏻
quoting
naddr1qq…7cj2Hello everyone and welcome to this new episode of Inside Bitcoin Code.
This is the first episode of the newsletter coming officially on Nostr!
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 anint64_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{};int64_t best_block_time{};Then, the code starts gathering information about the chain. First of all, it locks the mutex of the
ChainstateManagerobject, 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 notnullptr.\ Finally, it logs the currentsize()of the block tree to the user and assigns the current tip height and the tip timestamp tochain_active_heightandbest_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_infovariable comes directly from the signature of theAppInitMain()function — Discussed in IBC#007. This is an object of typeBlockAndHeaderTipInfo, a structure that contains information about the block and the header.First of all, an
ifclause checks thattip_infois notnullptr. 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
ifclause checks that bothtip_infoandm_best_header— the best header, seen so far, which is not known to be invalid — are both notnullptr. If so, theheader_heightandheader_timeare assigned the height and the timestamp ofm_best_headerrespectively.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(); }
Notify Peer Manager
Then, the
chain_active_heightis logged to the user asnBestHeigth.LogInfo(”nBestHeight = %d”, chain_active_height);Finally, the code checks if the
PeerManagerobject in theNodeContextisnullptr. 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!
