| 4561 | } |
| 4562 | |
| 4563 | bool Chainstate::LoadChainTip() |
| 4564 | { |
| 4565 | AssertLockHeld(cs_main); |
| 4566 | const CCoinsViewCache& coins_cache = CoinsTip(); |
| 4567 | assert(!coins_cache.GetBestBlock().IsNull()); // Never called when the coins view is empty |
| 4568 | CBlockIndex* tip = m_chain.Tip(); |
| 4569 | |
| 4570 | if (tip && tip->GetBlockHash() == coins_cache.GetBestBlock()) { |
| 4571 | return true; |
| 4572 | } |
| 4573 | |
| 4574 | // Load pointer to end of best chain |
| 4575 | CBlockIndex* pindex = m_blockman.LookupBlockIndex(coins_cache.GetBestBlock()); |
| 4576 | if (!pindex) { |
| 4577 | return false; |
| 4578 | } |
| 4579 | m_chain.SetTip(*pindex); |
| 4580 | m_chainman.UpdateIBDStatus(); |
| 4581 | tip = m_chain.Tip(); |
| 4582 | |
| 4583 | // nSequenceId is one of the keys used to sort setBlockIndexCandidates. Ensure all |
| 4584 | // candidate sets are empty to avoid UB, as nSequenceId is about to be modified. |
| 4585 | for (const auto& cs : m_chainman.m_chainstates) { |
| 4586 | assert(cs->setBlockIndexCandidates.empty()); |
| 4587 | } |
| 4588 | |
| 4589 | // Make sure our chain tip before shutting down scores better than any other candidate |
| 4590 | // to maintain a consistent best tip over reboots in case of a tie. |
| 4591 | auto target = tip; |
| 4592 | while (target) { |
| 4593 | target->nSequenceId = SEQ_ID_BEST_CHAIN_FROM_DISK; |
| 4594 | target = target->pprev; |
| 4595 | } |
| 4596 | |
| 4597 | LogInfo("Loaded best chain: hashBestChain=%s height=%d date=%s progress=%f", |
| 4598 | tip->GetBlockHash().ToString(), |
| 4599 | m_chain.Height(), |
| 4600 | FormatISO8601DateTime(tip->GetBlockTime()), |
| 4601 | m_chainman.GuessVerificationProgress(tip)); |
| 4602 | |
| 4603 | // Ensure KernelNotifications m_tip_block is set even if no new block arrives. |
| 4604 | if (!this->GetRole().historical) { |
| 4605 | // Ignoring return value for now. |
| 4606 | (void)m_chainman.GetNotifications().blockTip( |
| 4607 | /*state=*/GetSynchronizationState(/*init=*/true, m_chainman.m_blockman.m_blockfiles_indexed), |
| 4608 | /*index=*/*pindex, |
| 4609 | /*verification_progress=*/m_chainman.GuessVerificationProgress(tip)); |
| 4610 | } |
| 4611 | |
| 4612 | CheckForkWarningConditions(); |
| 4613 | |
| 4614 | return true; |
| 4615 | } |
| 4616 | |
| 4617 | CVerifyDB::CVerifyDB(Notifications& notifications) |
| 4618 | : m_notifications{notifications} |