Branch data Line data Source code
1 : : // Copyright (c) 2017-present The Bitcoin Core developers
2 : : // Distributed under the MIT software license, see the accompanying
3 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 : :
5 : : #include <index/base.h>
6 : :
7 : : #include <chain.h>
8 : : #include <common/args.h>
9 : : #include <dbwrapper.h>
10 : : #include <interfaces/chain.h>
11 : : #include <interfaces/types.h>
12 : : #include <kernel/chain.h>
13 : : #include <kernel/types.h>
14 : : #include <logging.h>
15 : : #include <node/abort.h>
16 : : #include <node/blockstorage.h>
17 : : #include <node/context.h>
18 : : #include <node/database_args.h>
19 : : #include <node/interface_ui.h>
20 : : #include <primitives/block.h>
21 : : #include <sync.h>
22 : : #include <tinyformat.h>
23 : : #include <uint256.h>
24 : : #include <undo.h>
25 : : #include <util/fs.h>
26 : : #include <util/string.h>
27 : : #include <util/thread.h>
28 : : #include <util/threadinterrupt.h>
29 : : #include <util/time.h>
30 : : #include <util/translation.h>
31 : : #include <validation.h>
32 : : #include <validationinterface.h>
33 : :
34 : : #include <cassert>
35 : : #include <compare>
36 : : #include <cstdint>
37 : : #include <memory>
38 : : #include <optional>
39 : : #include <span>
40 : : #include <stdexcept>
41 : : #include <string>
42 : : #include <thread>
43 : : #include <utility>
44 : : #include <vector>
45 : :
46 : : using kernel::ChainstateRole;
47 : :
48 : : constexpr uint8_t DB_BEST_BLOCK{'B'};
49 : :
50 : : constexpr auto SYNC_LOG_INTERVAL{30s};
51 : : constexpr auto SYNC_LOCATOR_WRITE_INTERVAL{30s};
52 : :
53 : : template <typename... Args>
54 : 0 : void BaseIndex::FatalErrorf(util::ConstevalFormatString<sizeof...(Args)> fmt, const Args&... args)
55 : : {
56 : 0 : auto message = tfm::format(fmt, args...);
57 [ # # # # : 0 : node::AbortNode(m_chain->context()->shutdown_request, m_chain->context()->exit_status, Untranslated(message), m_chain->context()->warnings.get());
# # # # #
# # # ]
58 : 0 : }
59 : :
60 : 0 : CBlockLocator GetLocator(interfaces::Chain& chain, const uint256& block_hash)
61 : : {
62 : 0 : CBlockLocator locator;
63 [ # # ]: 0 : bool found = chain.findBlock(block_hash, interfaces::FoundBlock().locator(locator));
64 [ # # ]: 0 : assert(found);
65 [ # # ]: 0 : assert(!locator.IsNull());
66 : 0 : return locator;
67 : 0 : }
68 : :
69 : 0 : BaseIndex::DB::DB(const fs::path& path, size_t n_cache_size, bool f_memory, bool f_wipe, bool f_obfuscate) :
70 : 0 : CDBWrapper{DBParams{
71 : : .path = path,
72 : : .cache_bytes = n_cache_size,
73 : : .memory_only = f_memory,
74 : : .wipe_data = f_wipe,
75 : : .obfuscate = f_obfuscate,
76 [ # # # # ]: 0 : .options = [] { DBOptions options; node::ReadDatabaseArgs(gArgs, options); return options; }()}}
77 : 0 : {}
78 : :
79 : 0 : CBlockLocator BaseIndex::DB::ReadBestBlock() const
80 : : {
81 : 0 : CBlockLocator locator;
82 : :
83 [ # # ]: 0 : bool success = Read(DB_BEST_BLOCK, locator);
84 [ # # ]: 0 : if (!success) {
85 [ # # ]: 0 : locator.SetNull();
86 : : }
87 : :
88 : 0 : return locator;
89 : 0 : }
90 : :
91 : 0 : void BaseIndex::DB::WriteBestBlock(CDBBatch& batch, const CBlockLocator& locator)
92 : : {
93 : 0 : batch.Write(DB_BEST_BLOCK, locator);
94 : 0 : }
95 : :
96 : 0 : BaseIndex::BaseIndex(std::unique_ptr<interfaces::Chain> chain, std::string name)
97 [ # # ]: 0 : : m_chain{std::move(chain)}, m_name{std::move(name)} {}
98 : :
99 : 0 : BaseIndex::~BaseIndex()
100 : : {
101 : 0 : Interrupt();
102 : 0 : Stop();
103 : 0 : }
104 : :
105 : 0 : bool BaseIndex::Init()
106 : : {
107 : 0 : AssertLockNotHeld(cs_main);
108 : :
109 : : // May need reset if index is being restarted.
110 : 0 : m_interrupt.reset();
111 : :
112 : : // m_chainstate member gives indexing code access to node internals. It is
113 : : // removed in followup https://github.com/bitcoin/bitcoin/pull/24230
114 [ # # # # ]: 0 : m_chainstate = WITH_LOCK(::cs_main,
115 : : return &m_chain->context()->chainman->ValidatedChainstate());
116 : : // Register to validation interface before setting the 'm_synced' flag, so that
117 : : // callbacks are not missed once m_synced is true.
118 : 0 : m_chain->context()->validation_signals->RegisterValidationInterface(this);
119 : :
120 : 0 : const auto locator{GetDB().ReadBestBlock()};
121 : :
122 [ # # ]: 0 : LOCK(cs_main);
123 : 0 : CChain& index_chain = m_chainstate->m_chain;
124 : :
125 [ # # ]: 0 : if (locator.IsNull()) {
126 [ # # ]: 0 : SetBestBlockIndex(nullptr);
127 : : } else {
128 : : // Setting the best block to the locator's top block. If it is not part of the
129 : : // best chain, we will rewind to the fork point during index sync
130 [ # # # # ]: 0 : const CBlockIndex* locator_index{m_chainstate->m_blockman.LookupBlockIndex(locator.vHave.at(0))};
131 [ # # ]: 0 : if (!locator_index) {
132 [ # # # # : 0 : return InitError(Untranslated(strprintf("best block of %s not found. Please rebuild the index.", GetName())));
# # ]
133 : : }
134 [ # # ]: 0 : SetBestBlockIndex(locator_index);
135 : : }
136 : :
137 : : // Child init
138 [ # # ]: 0 : const CBlockIndex* start_block = m_best_block_index.load();
139 [ # # # # : 0 : if (!CustomInit(start_block ? std::make_optional(interfaces::BlockRef{start_block->GetBlockHash(), start_block->nHeight}) : std::nullopt)) {
# # ]
140 : : return false;
141 : : }
142 : :
143 : : // Note: this will latch to true immediately if the user starts up with an empty
144 : : // datadir and an index enabled. If this is the case, indexation will happen solely
145 : : // via `BlockConnected` signals until, possibly, the next restart.
146 [ # # ]: 0 : m_synced = start_block == index_chain.Tip();
147 : 0 : m_init = true;
148 : 0 : return true;
149 : 0 : }
150 : :
151 : 0 : static const CBlockIndex* NextSyncBlock(const CBlockIndex* pindex_prev, CChain& chain) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
152 : : {
153 : 0 : AssertLockHeld(cs_main);
154 : :
155 [ # # ]: 0 : if (!pindex_prev) {
156 [ # # ]: 0 : return chain.Genesis();
157 : : }
158 : :
159 : 0 : const CBlockIndex* pindex = chain.Next(pindex_prev);
160 [ # # ]: 0 : if (pindex) {
161 : : return pindex;
162 : : }
163 : :
164 : : // Since block is not in the chain, return the next block in the chain AFTER the last common ancestor.
165 : : // Caller will be responsible for rewinding back to the common ancestor.
166 : 0 : return chain.Next(chain.FindFork(pindex_prev));
167 : : }
168 : :
169 : 0 : bool BaseIndex::ProcessBlock(const CBlockIndex* pindex, const CBlock* block_data)
170 : : {
171 : 0 : interfaces::BlockInfo block_info = kernel::MakeBlockInfo(pindex, block_data);
172 : :
173 : 0 : CBlock block;
174 [ # # ]: 0 : if (!block_data) { // disk lookup if block data wasn't provided
175 [ # # # # ]: 0 : if (!m_chainstate->m_blockman.ReadBlock(block, *pindex)) {
176 [ # # ]: 0 : FatalErrorf("Failed to read block %s from disk",
177 [ # # ]: 0 : pindex->GetBlockHash().ToString());
178 : 0 : return false;
179 : : }
180 : 0 : block_info.data = █
181 : : }
182 : :
183 : 0 : CBlockUndo block_undo;
184 [ # # # # ]: 0 : if (CustomOptions().connect_undo_data) {
185 [ # # # # : 0 : if (pindex->nHeight > 0 && !m_chainstate->m_blockman.ReadBlockUndo(block_undo, *pindex)) {
# # ]
186 [ # # ]: 0 : FatalErrorf("Failed to read undo block data %s from disk",
187 [ # # ]: 0 : pindex->GetBlockHash().ToString());
188 : 0 : return false;
189 : : }
190 : 0 : block_info.undo_data = &block_undo;
191 : : }
192 : :
193 [ # # # # ]: 0 : if (!CustomAppend(block_info)) {
194 [ # # ]: 0 : FatalErrorf("Failed to write block %s to index database",
195 [ # # ]: 0 : pindex->GetBlockHash().ToString());
196 : 0 : return false;
197 : : }
198 : :
199 : : return true;
200 : 0 : }
201 : :
202 : 0 : void BaseIndex::Sync()
203 : : {
204 [ # # ]: 0 : const CBlockIndex* pindex = m_best_block_index.load();
205 [ # # ]: 0 : if (!m_synced) {
206 : 0 : auto last_log_time{NodeClock::now()};
207 : 0 : auto last_locator_write_time{last_log_time};
208 : 0 : while (true) {
209 [ # # ]: 0 : if (m_interrupt) {
210 : 0 : LogInfo("%s: m_interrupt set; exiting ThreadSync", GetName());
211 : :
212 : 0 : SetBestBlockIndex(pindex);
213 : : // No need to handle errors in Commit. If it fails, the error will be already be
214 : : // logged. The best way to recover is to continue, as index cannot be corrupted by
215 : : // a missed commit to disk for an advanced index state.
216 : 0 : Commit();
217 : 0 : return;
218 : : }
219 : :
220 [ # # # # ]: 0 : const CBlockIndex* pindex_next = WITH_LOCK(cs_main, return NextSyncBlock(pindex, m_chainstate->m_chain));
221 : : // If pindex_next is null, it means pindex is the chain tip, so
222 : : // commit data indexed so far.
223 [ # # ]: 0 : if (!pindex_next) {
224 : 0 : SetBestBlockIndex(pindex);
225 : : // No need to handle errors in Commit. See rationale above.
226 : 0 : Commit();
227 : :
228 : : // If pindex is still the chain tip after committing, exit the
229 : : // sync loop. It is important for cs_main to be locked while
230 : : // setting m_synced = true, otherwise a new block could be
231 : : // attached while m_synced is still false, and it would not be
232 : : // indexed.
233 : 0 : LOCK(::cs_main);
234 [ # # ]: 0 : pindex_next = NextSyncBlock(pindex, m_chainstate->m_chain);
235 [ # # ]: 0 : if (!pindex_next) {
236 [ # # ]: 0 : m_synced = true;
237 [ # # ]: 0 : break;
238 : : }
239 : 0 : }
240 [ # # # # ]: 0 : if (pindex_next->pprev != pindex && !Rewind(pindex, pindex_next->pprev)) {
241 : 0 : FatalErrorf("Failed to rewind %s to a previous chain tip", GetName());
242 : 0 : return;
243 : : }
244 : 0 : pindex = pindex_next;
245 : :
246 : :
247 [ # # ]: 0 : if (!ProcessBlock(pindex)) return; // error logged internally
248 : :
249 : 0 : auto current_time{NodeClock::now()};
250 [ # # ]: 0 : if (current_time - last_log_time >= SYNC_LOG_INTERVAL) {
251 : 0 : LogInfo("Syncing %s with block chain from height %d", GetName(), pindex->nHeight);
252 : 0 : last_log_time = current_time;
253 : : }
254 : :
255 [ # # ]: 0 : if (current_time - last_locator_write_time >= SYNC_LOCATOR_WRITE_INTERVAL) {
256 : 0 : SetBestBlockIndex(pindex);
257 : 0 : last_locator_write_time = current_time;
258 : : // No need to handle errors in Commit. See rationale above.
259 : 0 : Commit();
260 : : }
261 : : }
262 : : }
263 : :
264 [ # # ]: 0 : if (pindex) {
265 : 0 : LogInfo("%s is enabled at height %d", GetName(), pindex->nHeight);
266 : : } else {
267 : 0 : LogInfo("%s is enabled", GetName());
268 : : }
269 : : }
270 : :
271 : 0 : bool BaseIndex::Commit()
272 : : {
273 : : // Don't commit anything if we haven't indexed any block yet
274 : : // (this could happen if init is interrupted).
275 [ # # ]: 0 : bool ok = m_best_block_index != nullptr;
276 [ # # ]: 0 : if (ok) {
277 : 0 : CDBBatch batch(GetDB());
278 [ # # ]: 0 : ok = CustomCommit(batch);
279 [ # # ]: 0 : if (ok) {
280 [ # # # # : 0 : GetDB().WriteBestBlock(batch, GetLocator(*m_chain, m_best_block_index.load()->GetBlockHash()));
# # ]
281 [ # # # # ]: 0 : GetDB().WriteBatch(batch);
282 : : }
283 : 0 : }
284 [ # # ]: 0 : if (!ok) {
285 : 0 : LogError("Failed to commit latest %s state", GetName());
286 : 0 : return false;
287 : : }
288 : : return true;
289 : : }
290 : :
291 : 0 : bool BaseIndex::Rewind(const CBlockIndex* current_tip, const CBlockIndex* new_tip)
292 : : {
293 [ # # ]: 0 : assert(current_tip->GetAncestor(new_tip->nHeight) == new_tip);
294 : :
295 : 0 : CBlock block;
296 : 0 : CBlockUndo block_undo;
297 : :
298 [ # # ]: 0 : for (const CBlockIndex* iter_tip = current_tip; iter_tip != new_tip; iter_tip = iter_tip->pprev) {
299 [ # # ]: 0 : interfaces::BlockInfo block_info = kernel::MakeBlockInfo(iter_tip);
300 [ # # # # ]: 0 : if (CustomOptions().disconnect_data) {
301 [ # # # # ]: 0 : if (!m_chainstate->m_blockman.ReadBlock(block, *iter_tip)) {
302 [ # # # # ]: 0 : LogError("Failed to read block %s from disk",
303 : : iter_tip->GetBlockHash().ToString());
304 : 0 : return false;
305 : : }
306 : 0 : block_info.data = █
307 : : }
308 [ # # # # : 0 : if (CustomOptions().disconnect_undo_data && iter_tip->nHeight > 0) {
# # ]
309 [ # # # # ]: 0 : if (!m_chainstate->m_blockman.ReadBlockUndo(block_undo, *iter_tip)) {
310 : : return false;
311 : : }
312 : 0 : block_info.undo_data = &block_undo;
313 : : }
314 [ # # # # ]: 0 : if (!CustomRemove(block_info)) {
315 : : return false;
316 : : }
317 : : }
318 : :
319 : : // Don't commit here - the committed index state must never be ahead of the
320 : : // flushed chainstate, otherwise unclean restarts would lead to index corruption.
321 : : // Pruning has a minimum of 288 blocks-to-keep and getting the index
322 : : // out of sync may be possible but a users fault.
323 : : // In case we reorg beyond the pruned depth, ReadBlock would
324 : : // throw and lead to a graceful shutdown
325 [ # # ]: 0 : SetBestBlockIndex(new_tip);
326 : : return true;
327 : 0 : }
328 : :
329 : 0 : void BaseIndex::BlockConnected(const ChainstateRole& role, const std::shared_ptr<const CBlock>& block, const CBlockIndex* pindex)
330 : : {
331 : : // Ignore events from not fully validated chains to avoid out-of-order indexing.
332 : : //
333 : : // TODO at some point we could parameterize whether a particular index can be
334 : : // built out of order, but for now just do the conservative simple thing.
335 [ # # ]: 0 : if (!role.validated) {
336 : : return;
337 : : }
338 : :
339 : : // Ignore BlockConnected signals until we have fully indexed the chain.
340 [ # # ]: 0 : if (!m_synced) {
341 : : return;
342 : : }
343 : :
344 [ # # ]: 0 : const CBlockIndex* best_block_index = m_best_block_index.load();
345 [ # # ]: 0 : if (!best_block_index) {
346 [ # # ]: 0 : if (pindex->nHeight != 0) {
347 : 0 : FatalErrorf("First block connected is not the genesis block (height=%d)",
348 : 0 : pindex->nHeight);
349 : 0 : return;
350 : : }
351 : : } else {
352 : : // Ensure block connects to an ancestor of the current best block. This should be the case
353 : : // most of the time, but may not be immediately after the sync thread catches up and sets
354 : : // m_synced. Consider the case where there is a reorg and the blocks on the stale branch are
355 : : // in the ValidationInterface queue backlog even after the sync thread has caught up to the
356 : : // new chain tip. In this unlikely event, log a warning and let the queue clear.
357 [ # # ]: 0 : if (best_block_index->GetAncestor(pindex->nHeight - 1) != pindex->pprev) {
358 [ # # # # ]: 0 : LogWarning("Block %s does not connect to an ancestor of "
359 : : "known best chain (tip=%s); not updating index",
360 : : pindex->GetBlockHash().ToString(),
361 : : best_block_index->GetBlockHash().ToString());
362 : 0 : return;
363 : : }
364 [ # # # # ]: 0 : if (best_block_index != pindex->pprev && !Rewind(best_block_index, pindex->pprev)) {
365 : 0 : FatalErrorf("Failed to rewind %s to a previous chain tip",
366 : 0 : GetName());
367 : 0 : return;
368 : : }
369 : : }
370 : :
371 : : // Dispatch block to child class; errors are logged internally and abort the node.
372 [ # # ]: 0 : if (ProcessBlock(pindex, block.get())) {
373 : : // Setting the best block index is intentionally the last step of this
374 : : // function, so BlockUntilSyncedToCurrentChain callers waiting for the
375 : : // best block index to be updated can rely on the block being fully
376 : : // processed, and the index object being safe to delete.
377 : 0 : SetBestBlockIndex(pindex);
378 : : }
379 : : }
380 : :
381 : 0 : void BaseIndex::ChainStateFlushed(const ChainstateRole& role, const CBlockLocator& locator)
382 : : {
383 : : // Ignore events from not fully validated chains to avoid out-of-order indexing.
384 [ # # ]: 0 : if (!role.validated) {
385 : : return;
386 : : }
387 : :
388 [ # # ]: 0 : if (!m_synced) {
389 : : return;
390 : : }
391 : :
392 : 0 : const uint256& locator_tip_hash = locator.vHave.front();
393 : 0 : const CBlockIndex* locator_tip_index;
394 : 0 : {
395 : 0 : LOCK(cs_main);
396 [ # # # # ]: 0 : locator_tip_index = m_chainstate->m_blockman.LookupBlockIndex(locator_tip_hash);
397 : 0 : }
398 : :
399 [ # # ]: 0 : if (!locator_tip_index) {
400 [ # # ]: 0 : FatalErrorf("First block (hash=%s) in locator was not found",
401 : 0 : locator_tip_hash.ToString());
402 : 0 : return;
403 : : }
404 : :
405 : : // This checks that ChainStateFlushed callbacks are received after BlockConnected. The check may fail
406 : : // immediately after the sync thread catches up and sets m_synced. Consider the case where
407 : : // there is a reorg and the blocks on the stale branch are in the ValidationInterface queue
408 : : // backlog even after the sync thread has caught up to the new chain tip. In this unlikely
409 : : // event, log a warning and let the queue clear.
410 : 0 : const CBlockIndex* best_block_index = m_best_block_index.load();
411 [ # # ]: 0 : if (best_block_index->GetAncestor(locator_tip_index->nHeight) != locator_tip_index) {
412 [ # # # # ]: 0 : LogWarning("Locator contains block (hash=%s) not on known best "
413 : : "chain (tip=%s); not writing index locator",
414 : : locator_tip_hash.ToString(),
415 : : best_block_index->GetBlockHash().ToString());
416 : 0 : return;
417 : : }
418 : :
419 : : // No need to handle errors in Commit. If it fails, the error will be already be logged. The
420 : : // best way to recover is to continue, as index cannot be corrupted by a missed commit to disk
421 : : // for an advanced index state.
422 : 0 : Commit();
423 : : }
424 : :
425 : 0 : bool BaseIndex::BlockUntilSyncedToCurrentChain() const
426 : : {
427 : 0 : AssertLockNotHeld(cs_main);
428 : :
429 [ # # ]: 0 : if (!m_synced) {
430 : : return false;
431 : : }
432 : :
433 : 0 : {
434 : : // Skip the queue-draining stuff if we know we're caught up with
435 : : // m_chain.Tip().
436 : 0 : LOCK(cs_main);
437 [ # # ]: 0 : const CBlockIndex* chain_tip = m_chainstate->m_chain.Tip();
438 [ # # ]: 0 : const CBlockIndex* best_block_index = m_best_block_index.load();
439 [ # # # # ]: 0 : if (best_block_index->GetAncestor(chain_tip->nHeight) == chain_tip) {
440 [ # # ]: 0 : return true;
441 : : }
442 : 0 : }
443 : :
444 : 0 : LogInfo("%s is catching up on block notifications", GetName());
445 : 0 : m_chain->context()->validation_signals->SyncWithValidationInterfaceQueue();
446 : 0 : return true;
447 : : }
448 : :
449 : 0 : void BaseIndex::Interrupt()
450 : : {
451 : 0 : m_interrupt();
452 : 0 : }
453 : :
454 : 0 : bool BaseIndex::StartBackgroundSync()
455 : : {
456 [ # # # # ]: 0 : if (!m_init) throw std::logic_error("Error: Cannot start a non-initialized index");
457 : :
458 : 0 : m_thread_sync = std::thread(&util::TraceThread, GetName(), [this] { Sync(); });
459 : 0 : return true;
460 : : }
461 : :
462 : 0 : void BaseIndex::Stop()
463 : : {
464 [ # # ]: 0 : if (m_chain->context()->validation_signals) {
465 : 0 : m_chain->context()->validation_signals->UnregisterValidationInterface(this);
466 : : }
467 : :
468 [ # # ]: 0 : if (m_thread_sync.joinable()) {
469 : 0 : m_thread_sync.join();
470 : : }
471 : 0 : }
472 : :
473 : 0 : IndexSummary BaseIndex::GetSummary() const
474 : : {
475 [ # # ]: 0 : IndexSummary summary{};
476 [ # # ]: 0 : summary.name = GetName();
477 [ # # ]: 0 : summary.synced = m_synced;
478 [ # # ]: 0 : if (const auto& pindex = m_best_block_index.load()) {
479 : 0 : summary.best_block_height = pindex->nHeight;
480 : 0 : summary.best_block_hash = pindex->GetBlockHash();
481 : : } else {
482 : 0 : summary.best_block_height = 0;
483 [ # # ]: 0 : summary.best_block_hash = m_chain->getBlockHash(0);
484 : : }
485 : 0 : return summary;
486 : 0 : }
487 : :
488 : 0 : void BaseIndex::SetBestBlockIndex(const CBlockIndex* block)
489 : : {
490 [ # # # # ]: 0 : assert(!m_chainstate->m_blockman.IsPruneMode() || AllowPrune());
491 : :
492 [ # # # # ]: 0 : if (AllowPrune() && block) {
493 : 0 : node::PruneLockInfo prune_lock;
494 : 0 : prune_lock.height_first = block->nHeight;
495 [ # # ]: 0 : WITH_LOCK(::cs_main, m_chainstate->m_blockman.UpdatePruneLock(GetName(), prune_lock));
496 : : }
497 : :
498 : : // Intentionally set m_best_block_index as the last step in this function,
499 : : // after updating prune locks above, and after making any other references
500 : : // to *this, so the BlockUntilSyncedToCurrentChain function (which checks
501 : : // m_best_block_index as an optimization) can be used to wait for the last
502 : : // BlockConnected notification and safely assume that prune locks are
503 : : // updated and that the index object is safe to delete.
504 : 0 : m_best_block_index = block;
505 : 0 : }
|