| 102 | } |
| 103 | |
| 104 | bool BaseIndex::Init() |
| 105 | { |
| 106 | AssertLockNotHeld(cs_main); |
| 107 | |
| 108 | // May need reset if index is being restarted. |
| 109 | m_interrupt.reset(); |
| 110 | |
| 111 | // m_chainstate member gives indexing code access to node internals. It is |
| 112 | // removed in followup https://github.com/bitcoin/bitcoin/pull/24230 |
| 113 | m_chainstate = WITH_LOCK(::cs_main, |
| 114 | return &m_chain->context()->chainman->ValidatedChainstate()); |
| 115 | // Register to validation interface before setting the 'm_synced' flag, so that |
| 116 | // callbacks are not missed once m_synced is true. |
| 117 | m_chain->context()->validation_signals->RegisterValidationInterface(this); |
| 118 | |
| 119 | const auto locator{GetDB().ReadBestBlock()}; |
| 120 | |
| 121 | LOCK(cs_main); |
| 122 | CChain& index_chain = m_chainstate->m_chain; |
| 123 | |
| 124 | if (locator.IsNull()) { |
| 125 | SetBestBlockIndex(nullptr); |
| 126 | } else { |
| 127 | // Setting the best block to the locator's top block. If it is not part of the |
| 128 | // best chain, we will rewind to the fork point during index sync |
| 129 | const CBlockIndex* locator_index{m_chainstate->m_blockman.LookupBlockIndex(locator.vHave.at(0))}; |
| 130 | if (!locator_index) { |
| 131 | return InitError(Untranslated(strprintf("best block of %s not found. Please rebuild the index.", GetName()))); |
| 132 | } |
| 133 | SetBestBlockIndex(locator_index); |
| 134 | } |
| 135 | |
| 136 | // Child init |
| 137 | const CBlockIndex* start_block = m_best_block_index.load(); |
| 138 | if (!CustomInit(start_block ? std::make_optional(interfaces::BlockRef{start_block->GetBlockHash(), start_block->nHeight}) : std::nullopt)) { |
| 139 | return false; |
| 140 | } |
| 141 | |
| 142 | // Note: this will latch to true immediately if the user starts up with an empty |
| 143 | // datadir and an index enabled. If this is the case, indexation will happen solely |
| 144 | // via `BlockConnected` signals until, possibly, the next restart. |
| 145 | m_synced = start_block == index_chain.Tip(); |
| 146 | m_init = true; |
| 147 | return true; |
| 148 | } |
| 149 | |
| 150 | static const CBlockIndex* NextSyncBlock(const CBlockIndex* const pindex_prev, CChain& chain) EXCLUSIVE_LOCKS_REQUIRED(cs_main) |
| 151 | { |
no test coverage detected