MCPcopy Create free account
hub / github.com/bitcoin/bitcoin / Init

Method Init

src/index/base.cpp:104–148  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

102}
103
104bool 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
150static const CBlockIndex* NextSyncBlock(const CBlockIndex* const pindex_prev, CChain& chain) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
151{

Callers 8

CheckSignetBlockSolutionFunction · 0.45
InitAndLoadChainstateFunction · 0.45
AppInitMainFunction · 0.45
CheckInputScriptsFunction · 0.45
PrecomputePSBTDataFunction · 0.45
BlockFilterIndexSyncFunction · 0.45
VerifyScriptBenchFunction · 0.45

Calls 12

InitErrorFunction · 0.85
UntranslatedFunction · 0.85
LookupBlockIndexMethod · 0.80
resetMethod · 0.45
contextMethod · 0.45
ReadBestBlockMethod · 0.45
IsNullMethod · 0.45
atMethod · 0.45
loadMethod · 0.45
GetBlockHashMethod · 0.45
TipMethod · 0.45

Tested by

no test coverage detected